Shader "Mgin/DepthNormals" { Properties { _MainTex ("", 2D) = "white" {} // 이 텍스처는 post processing 이전에 그려진 화면이다. _HighlightDirection ("Highlight Direction", Vector) = (1, 0, 0) } SubShader { Tags { "RenderType"="Opaque" } Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" sampler2D _CameraDepthNormalsTexture; // 카메라의 깊이 텍스처 float _StartingTime; // 시작 시간 (현재시간) float _showNormalColors = 1; // 1일 때, 노멀값이 색상으로 보인다. 0일때, 깊이값이 색상으로 보인다. struct v2f { float4 pos : SV_POSITION; float4 scrPos:TEXCOORD1; }; //Our Vertex Shader v2f vert (appdata_base v) { v2f o; o.pos = mul (UNITY_MATRIX_MVP, v.vertex); o.scrPos=ComputeScreenPos(o.pos); #if UNITY_UV_STARTS_AT_TOP if( o.scrPos.y < 0.0 ) { o.scrPos.y = 1.0 - o.scrPos.y; } #endif return o; } // 두가지 색상채널을 사용하여 float 값으로 디코딩 한다. // http://docs.unity3d.com/kr/current/Manual/SL-BuiltinIncludes.html // http://www.gamedev.net/topic/630239-encode-float-to-rg-or-rgb/ // http://forum.unity3d.com/threads/encoding-float-to-rg-rgba-and-blending-hard-issue.289078/ float CustomDecodeFloatRG( float2 enc ) { float2 kDecodeDot = float2(1.0, 1/255.0); return dot( enc, kDecodeDot ); // (enc.x * 1) + (enc.y * 1/255.0) } // 뷰 공간 노멀을 0~1 사이의 2D 벡터로 인코딩/ 디코딩 // http://aras-p.info/texts/CompactNormalStorage.html inline float3 CustomDecodeViewNormalStereo( float4 enc4 ) { float kScale = 1.7777; float3 nn = enc4.xyz*float3(2*kScale,2*kScale,0) + float3(-kScale,-kScale,1); float g = 2.0 / dot(nn.xyz,nn.xyz); float3 n; n.xy = g*nn.xy; n.z = g-1; return n; } void CustomDecodeDepthNormal( float4 enc, out float depth, out float3 normal ) { depth = CustomDecodeFloatRG (enc.zw); normal = CustomDecodeViewNormalStereo (enc); } sampler2D _MainTex; float4 _HighlightDirection; half4 frag (v2f i) : COLOR { float3 normalValues; float depthValue; // depth와 normal 값을 뽑아낸다. CustomDecodeDepthNormal(tex2D(_CameraDepthNormalsTexture, i.scrPos.xy), depthValue, normalValues); if(_showNormalColors == 1) { float4 normalColor = float4(normalValues, 1); return normalColor; } else { float4 depth = depthValue; return depth; } } ENDCG } } FallBack "Diffuse" }