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

비밀번호

댓글 작성자
 




... 91  92  93  94  95  96  97  98  99  100  101  [102]  103  104  105  ...
NoWriterDateCnt.TitleFile(s)
11379정성태11/30/201718965오류 유형: 435. System.Web.HttpException - Session state has created a session id, but cannot save it because the response was already flushed by the application.
11378정성태11/29/201720449.NET Framework: 701. 한글이 포함된 바이트 배열을 나눈 경우 한글이 깨지지 않도록 다시 조합하는 방법 [1]파일 다운로드1
11377정성태11/29/201719662.NET Framework: 700. CommonOpenFileDialog 사용 시 사용자가 선택한 파일 목록을 구하는 방법 [3]파일 다운로드1
11376정성태11/28/201724009VS.NET IDE: 123. Visual Studio 편집기의 \r\n (crlf) 개행을 \n으로 폴더 단위로 설정하는 방법
11375정성태11/28/201718876오류 유형: 434. Visual Studio로 ASP.NET 디버깅 중 System.Web.HttpException - Could not load type 오류
11374정성태11/27/201723879사물인터넷: 14. 라즈베리 파이 - (윈도우의 NT 서비스처럼) 부팅 시 시작하는 프로그램 설정 [1]
11373정성태11/27/201722910오류 유형: 433. Raspberry Pi/Windows 다중 플랫폼 지원 컴파일 관련 오류 기록
11372정성태11/25/201725922사물인터넷: 13. 윈도우즈 사용자를 위한 라즈베리 파이 제로 W 모델을 설정하는 방법 [4]
11371정성태11/25/201719625오류 유형: 432. Hyper-V 가상 스위치 생성 시 Failed to connect Ethernet switch port 0x80070002 오류 발생
11370정성태11/25/201719488오류 유형: 431. Hyper-V의 Virtual Switch 생성 시 "External network" 목록에 특정 네트워크 어댑터 항목이 없는 경우
11369정성태11/25/201721612사물인터넷: 12. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 키보드 및 마우스로 쓰는 방법 (절대 좌표, 상대 좌표, 휠) [1]
11368정성태11/25/201727235.NET Framework: 699. UDP 브로드캐스트 주소 255.255.255.255와 192.168.0.255의 차이점과 이를 고려한 C# UDP 서버/클라이언트 예제 [2]파일 다운로드1
11367정성태11/25/201727255개발 환경 구성: 337. 윈도우 운영체제의 route 명령어 사용법
11366정성태11/25/201718894오류 유형: 430. 이벤트 로그 - Cryptographic Services failed while processing the OnIdentity() call in the System Writer Object.
11365정성태11/25/201721155오류 유형: 429. 이벤트 로그 - User Policy could not be updated successfully
11364정성태11/24/201722971사물인터넷: 11. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스로 쓰는 방법 (절대 좌표) [2]
11363정성태11/23/201723020사물인터넷: 10. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스 + 키보드로 쓰는 방법 (두 번째 이야기)
11362정성태11/22/201719559오류 유형: 428. 윈도우 업데이트 KB4048953 - 0x800705b4 [2]
11361정성태11/22/201722393오류 유형: 427. 이벤트 로그 - Filter Manager failed to attach to volume '\Device\HarddiskVolume??' 0xC03A001C
11360정성태11/22/201722144오류 유형: 426. 이벤트 로그 - The kernel power manager has initiated a shutdown transition.
11359정성태11/16/201721647오류 유형: 425. 윈도우 10 Version 1709 (OS Build 16299.64) 업그레이드 시 발생한 문제 2가지
11358정성태11/15/201726347사물인터넷: 9. Visual Studio 2017에서 Raspberry Pi C++ 응용 프로그램 제작 [1]
11357정성태11/15/201726769개발 환경 구성: 336. 윈도우 10 Bash 쉘에서 C++ 컴파일하는 방법
11356정성태11/15/201728426사물인터넷: 8. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스 + 키보드로 쓰는 방법 [4]
11355정성태11/15/201724323사물인터넷: 7. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스로 쓰는 방법 [2]파일 다운로드2
11354정성태11/14/201728519사물인터넷: 6. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 키보드로 쓰는 방법 [8]
... 91  92  93  94  95  96  97  98  99  100  101  [102]  103  104  105  ...