Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 3개 있습니다.)

Unity - LightMode의 ForwardBase에 따른 _WorldSpaceLightPos0 값 변화

_WorldSpaceLightPos0 내장 변수를 사용한 지난 글들을 실습해 보면,

Unity로 실습하는 Shader (2) - 고로 셰이딩(gouraud shading) + 퐁 모델(Phong model)
; https://www.sysnet.pe.kr/2/0/11609

Unity로 실습하는 Shader (3) - 고로 셰이딩(gouraud shading) + 퐁 모델(Phong model) + Texture
; https://www.sysnet.pe.kr/2/0/11610

Unity로 실습하는 Shader (4) - 퐁 셰이딩(phong shading)
; https://www.sysnet.pe.kr/2/0/11611

이상한 현상이 하나 눈에 띕니다. 바로 다음과 같이 색상의 반전이 일어나는 것입니다.

[그림: 앞에서 2개는 고로 셰이딩, 제일 뒤에 있는 6면체는 unity 기본 셰이딩 적용]
unity_light_pos_0.png

위에서 볼 수 있는 것처럼, (Game 뷰 화면에서) Unity 기본 셰이딩은 정상적으로 조명값이 적용되었지만 제가 만든 shader를 적용한 앞의 2개는 조명이 반전되었습니다. 도대체 왜 이런 것인지, shader 소스 코드를 천천히 들여다봤지만 이런 현상에 대한 변수라고는 _WorldSpaceLightPos0 밖에는 없다는 판단이 들었습니다. 그래서 그 값을 확인하기 위해 다음과 같이 (디버깅을 위해) 설정하고,

v2f vert (appdata v)
{
    v2f o;
    o.vertex = UnityObjectToClipPos(v.vertex);
    o.illumination = _WorldSpaceLightPos0;

    return o;
}
			
fixed4 frag (v2f i) : SV_Target
{
    fixed4 col = _Color * i.illumination;
    return col;
}

실행 후 다음과 같은 결과를 얻었습니다.

unity_light_pos_1.png

Unity Scene 뷰와 Game 뷰의 출력이 다릅니다. 재미있는 것은, 이 상태에서 Unity 기본 shader를 적용한 물체를 가까이 끌어오면,

unity_light_pos_2.png

위와 같이 제가 만든 shader를 적용한 물체의 색상이 정상적으로 바뀌었습니다. 당연히 다른 객체에도 가까이 가져왔더니,

unity_light_pos_3.png

보는 바와 같이 정상적으로 _WorldSpaceLightPos0 값이 적용되었습니다. 테스트를 몇 번 하다 보면, Unity shader가 적용된 객체가 (화면에 보여야 하고) 제가 만든 shader의 객체보다 z-축 기준으로 카메라 방향으로 앞에 있게 되면 _WorldSpaceLightPos0가 정상적인 값을 반환한다는 것을 알 수 있습니다.

현상은 일단 그렇고, 이런 문제를 해결하려면 LightMode의 값을 "ForwardBase"로 해야 합니다.

SubShader
{
    Pass
    {
        Tags{ "LightMode" = "ForwardBase" }

        // ...[생략]...
    }
}

Unity 문서와,

ShaderLab: Pass Tags
; https://docs.unity3d.com/Manual/SL-PassTags.html

ForwardBase: Used in Forward rendering, ambient, main directional light, vertex/SH lights and lightmaps


다음의 shader에 있는 주석을 종합해 봤을 때,

psicomante/gouraud.shader 
; https://gist.github.com/psicomante/2109b947a681804f8ba5625238895a2a#file-gouraud-shader-L70

// indicate that our pass is the "base" pass in forward
// rendering pipeline. It gets ambient and main directional
// light data set up; light direction in _WorldSpaceLightPos0
// and color in _LightColor0


_WorldSpaceLightPos0와 _LightColor0 값은 저 모드로 설정된 shader에 한해 정상적인 값을 보장받을 수 있는 것 같습니다. 그나저나 "Forward rendering"이 뭔지 검색해 보면,

[Kgc2012] deferred forward 이창희 
; https://www.slideshare.net/cagetu/kgc2012-deferred-forward

forward rendering이란, 전통적인 렌더링 파이프라인을 통해서 shading되는 방식을 말한다

- Vertex Generation
- Vertex Processing
- Primitive Generation
- Primitive Processing
- Rasterization (Fragment Generation)
- Fragment Processing
- Frame-Buffer Ops
- Frame Buffer


라고 합니다. ^^ 제가 더 이상 comment할 만한 내용의 것이 아닌 듯합니다.

참고로, 테스트 삼아 "ShaderLab: Pass Tags" 문서에 있는 가능한 Mode 들에 대해 테스트해보니, ForwardBase 이외의 모든 값은 정상적인 동작을 하지 않았습니다. 가령, 다음의 2개는 이 글에서 설명한 것과 동일한 현상을 발생시켰고,

  • Always
  • Vertex

다음의 모드들은 아예 객체를 화면에서 사라지게 만들었습니다.

  • ForwardAdd
  • Deferred
  • MotionVectors
  • PrepassBase
  • PrepassFinal
  • VertexLMRGBM
  • VertexLM

암튼... 모르는 저 너머의 것들이 너무나 많군요. ^^




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 8/3/2018]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 46  47  48  49  50  51  52  [53]  54  55  56  57  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
12310정성태9/3/202010338오류 유형: 644. Windows could not start the Elasticsearch 7.9.0 (elasticsearch-service-x64) service on Local Computer.
12309정성태9/3/202010103개발 환경 구성: 507. Elasticsearch 6.6부터 기본 추가된 한글 형태소 분석기 노리(nori) 사용법
12308정성태9/2/202011345개발 환경 구성: 506. Windows - 단일 머신에서 단일 바이너리로 여러 개의 ElasticSearch 노드를 실행하는 방법
12307정성태9/2/202012136오류 유형: 643. curl - json_parse_exception / Invalid UTF-8 start byte
12306정성태9/1/202010328오류 유형: 642. SQL Server 시작 오류 - error code 10013
12305정성태9/1/202011184Windows: 172. "Administered port exclusions"이 아닌 포트 범위 항목을 삭제하는 방법
12304정성태8/31/202010140개발 환경 구성: 505. 윈도우 - (네트워크 어댑터의 우선순위로 인한) 열거되는 IP 주소 순서를 조정하는 방법
12303정성태8/30/202010295개발 환경 구성: 504. ETW - 닷넷 프레임워크 기반의 응용 프로그램을 위한 명령행 도구 etrace 소개
12302정성태8/30/202010216.NET Framework: 936. C# - ETW 관련 Win32 API 사용 예제 코드 (5) - Private Logger파일 다운로드1
12301정성태8/30/202010511오류 유형: 641. error MSB4044: The "Fody.WeavingTask" task was not given a value for the required parameter "IntermediateDir".
12300정성태8/29/20209937.NET Framework: 935. C# - ETW 관련 Win32 API 사용 예제 코드 (4) CLR ETW Consumer파일 다운로드1
12299정성태8/27/202010859.NET Framework: 934. C# - ETW 관련 Win32 API 사용 예제 코드 (3) ETW Consumer 구현파일 다운로드1
12298정성태8/27/202010604오류 유형: 640. livekd - Could not resolve symbols for ntoskrnl.exe: MmPfnDatabase
12297정성태8/25/20209807개발 환경 구성: 503. SHA256 테스트 인증서 생성 방법
12296정성태8/24/202010223.NET Framework: 933. C# - ETW 관련 Win32 API 사용 예제 코드 (2) NT Kernel Logger파일 다운로드1
12295정성태8/24/20209673오류 유형: 639. Bitvise - Address is already in use; bind() in ListeningSocket::StartListening() failed: Windows error 10013: An attempt was made to access a socket ,,,
12293정성태8/24/202010996Windows: 171. "Administered port exclusions" 설명
12292정성태8/20/202012292.NET Framework: 932. C# - ETW 관련 Win32 API 사용 예제 코드 (1)파일 다운로드2
12291정성태8/15/202011225오류 유형: 638. error 1297: Device driver does not install on any devices, use primitive driver if this is intended.
12290정성태8/11/202011882.NET Framework: 931. C# - IP 주소에 따른 국가별 위치 확인 [8]파일 다운로드1
12289정성태8/6/20209393개발 환경 구성: 502. Portainer에 윈도우 컨테이너를 등록하는 방법
12288정성태8/5/20209390오류 유형: 637. WCF - The protocol 'net.tcp' does not have an implementation of HostedTransportConfiguration type registered.
12287정성태8/5/20209853오류 유형: 636. C# - libdl.so를 DllImport로 연결 시 docker container 내에서 System.DllNotFoundException 예외 발생
12286정성태8/5/202010706개발 환경 구성: 501. .NET Core 용 container 이미지 만들 때 unzip이 필요한 경우
12285정성태8/4/202011111오류 유형: 635. 윈도우 10 업데이트 - 0xc1900209 [2]
12284정성태8/4/202010401디버깅 기술: 169. Hyper-V의 VM에 대한 메모리 덤프를 뜨는 방법
... 46  47  48  49  50  51  52  [53]  54  55  56  57  58  59  60  ...