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)
12461정성태12/21/202011897.NET Framework: 985. .NET 코드 리뷰 팁 [3]
12460정성태12/18/20209644기타: 78. 도서 소개 - C#으로 배우는 암호학
12459정성태12/16/202010035Linux: 35. C# - 리눅스 환경에서 클라이언트 소켓의 ephemeral port 재사용파일 다운로드1
12458정성태12/16/20209480오류 유형: 694. C# - Task.Start 메서드 호출 시 "System.InvalidOperationException: 'Start may not be called on a task that has completed.'" 예외 발생 [1]
12457정성태12/15/20209029Windows: 185. C# - Windows 10/2019부터 추가된 SIO_TCP_INFO파일 다운로드1
12456정성태12/15/20209325VS.NET IDE: 156. Visual Studio - "Migrate packages.config to PackageReference"
12455정성태12/15/20208818오류 유형: 693. DLL 로딩 시 0x800704ec - This Program is Blocked by Group Policy
12454정성태12/15/20209420Windows: 184. Windows - AppLocker의 "DLL Rules"를 이용해 임의 경로에 설치한 DLL의 로딩을 막는 방법 [1]
12453정성태12/14/202010366.NET Framework: 984. C# - bool / BOOL / VARIANT_BOOL에 대한 Interop [1]파일 다운로드1
12452정성태12/14/202010619Windows: 183. 설정은 가능하지만 구할 수는 없는 TcpTimedWaitDelay 값
12451정성태12/14/20209761Windows: 182. WMI Namespace를 열거하고, 그 안에 정의된 클래스를 열거하는 방법 [5]
12450정성태12/13/202010494.NET Framework: 983. C# - TIME_WAIT과 ephemeral port 재사용파일 다운로드1
12449정성태12/11/202010840.NET Framework: 982. C# - HttpClient에서의 ephemeral port 재사용 [2]파일 다운로드1
12448정성태12/11/202012485.NET Framework: 981. C# - HttpWebRequest, WebClient와 ephemeral port 재사용파일 다운로드1
12447정성태12/10/202010636.NET Framework: 980. C# - CopyFileEx API 사용 예제 코드파일 다운로드1
12446정성태12/10/202011343.NET Framework: 979. C# - CoCreateInstanceEx 사용 예제 코드파일 다운로드1
12445정성태12/8/20208652오류 유형: 692. C# Marshal.PtrToStructure - The structure must not be a value class.파일 다운로드1
12444정성태12/8/20209496.NET Framework: 978. C# - GUID 타입 전용의 UnmanagedType.LPStruct [1]파일 다운로드1
12443정성태12/8/20209401.NET Framework: 977. C# PInvoke - C++의 매개변수에 대한 마샬링을 tlbexp.exe를 이용해 확인하는 방법
12442정성태12/4/20208188오류 유형: 691. Visual Studio - Build Events에 robocopy를 사용할때 "Invalid Parameter #1" 오류가 발행하는 경우
12441정성태12/4/20207923오류 유형: 690. robocopy - ERROR : No Destination Directory Specified.
12440정성태12/4/20208977오류 유형: 689. SignTool Error: Invalid option: /as
12439정성태12/4/202010196디버깅 기술: 176. windbg - 특정 Win32 API에서 BP가 안 걸리는 경우 (2) [1]
12438정성태12/2/202010132오류 유형: 688. .Visual C++ - Error C2011 'sockaddr': 'struct' type redefinition
12437정성태12/1/20209790VS.NET IDE: 155. pfx의 암호 키 파일을 Visual Studio 없이 등록하는 방법
12436정성태12/1/202010087오류 유형: 687. .NET Core 2.2 빌드 - error MSB4018: The "RazorTagHelper" task failed unexpectedly.
... 46  [47]  48  49  50  51  52  53  54  55  56  57  58  59  60  ...