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

비밀번호

댓글 작성자
 




... [31]  32  33  34  35  36  37  38  39  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
12855정성태11/13/20216244개발 환경 구성: 605. Azure App Service - Kudu SSH 환경에서 FTP를 이용한 파일 전송
12854정성태11/13/20217798개발 환경 구성: 604. Azure - 윈도우 VM에서 FTP 여는 방법
12853정성태11/10/20216161오류 유형: 766. Azure App Service - JBoss 호스팅 생성 시 "This region has quota of 0 PremiumV3 instances for your subscription. Try selecting different region or SKU."
12851정성태11/1/20217550스크립트: 34. 파이썬 - MySQLdb 기본 예제 코드
12850정성태10/27/20218696오류 유형: 765. 우분투에서 pip install mysqlclient 실행 시 "OSError: mysql_config not found" 오류
12849정성태10/17/20217837스크립트: 33. JavaScript와 C#의 시간 변환 [1]
12848정성태10/17/20218811스크립트: 32. 파이썬 - sqlite3 기본 예제 코드 [1]
12847정성태10/14/20218657스크립트: 31. 파이썬 gunicorn - WORKER TIMEOUT 오류 발생
12846정성태10/7/20218414스크립트: 30. 파이썬 __debug__ 플래그 변수에 따른 코드 실행 제어
12845정성태10/6/20218241.NET Framework: 1120. C# - BufferBlock<T> 사용 예제 [5]파일 다운로드1
12844정성태10/3/20216246오류 유형: 764. MSI 설치 시 "... is accessible and not read-only." 오류 메시지
12843정성태10/3/20216692스크립트: 29. 파이썬 - fork 시 기존 클라이언트 소켓 및 스레드의 동작파일 다운로드1
12842정성태10/1/202125051오류 유형: 763. 파이썬 오류 - AttributeError: type object '...' has no attribute '...'
12841정성태10/1/20218519스크립트: 28. 모든 파이썬 프로세스에 올라오는 특별한 파일 - sitecustomize.py
12840정성태9/30/20218619.NET Framework: 1119. Entity Framework의 Join 사용 시 다중 칼럼에 대한 OR 조건 쿼리파일 다운로드1
12839정성태9/15/20219668.NET Framework: 1118. C# 11 - 제네릭 타입의 특성 적용파일 다운로드1
12838정성태9/13/20219317.NET Framework: 1117. C# - Task에 전달한 Action, Func 유형에 따라 달라지는 async/await 비동기 처리 [2]파일 다운로드1
12837정성태9/11/20218223VC++: 151. Golang - fmt.Errorf, errors.Is, errors.As 설명
12836정성태9/10/20217830Linux: 45. 리눅스 - 실행 중인 다른 프로그램의 출력을 확인하는 방법
12835정성태9/7/20219072.NET Framework: 1116. C# 10 - (15) CallerArgumentExpression 특성 추가 [2]파일 다운로드1
12834정성태9/7/20217467오류 유형: 762. Visual Studio 2019 Build Tools - 'C:\Program' is not recognized as an internal or external command, operable program or batch file.
12833정성태9/6/20216910VC++: 150. Golang - TCP client/server echo 예제 코드파일 다운로드1
12832정성태9/6/20217773VC++: 149. Golang - 인터페이스 포인터가 의미 있을까요?
12831정성태9/6/20216301VC++: 148. Golang - 채널에 따른 다중 작업 처리파일 다운로드1
12830정성태9/6/20218568오류 유형: 761. Internet Explorer에서 파일 다운로드 시 "Your current security settings do not allow this file to be downloaded." 오류
12829정성태9/5/202110206.NET Framework: 1115. C# 10 - (14) 구조체 타입에 기본 생성자 정의 가능파일 다운로드1
... [31]  32  33  34  35  36  37  38  39  40  41  42  43  44  45  ...