Shader "Mgin/DepthRingPass" { Properties { _MainTex ("", 2D) = "white" {} // 이 텍스처는 post processing 이전에 그려진 화면이다. _RingWidth("ring width", Float) = 0.01 _RingPassTimeLength("ring pass time", Float) = 2.0 } SubShader { Tags { "RenderType"="Opaque" } Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" sampler2D _CameraDepthTexture; // 카메라의 깊이 텍스처 float _StartingTime; // 시작 시간 (현재시간) float _RunRingPass = 0; // 이벤트 시작 트리거로 사용하는 불리안 변수 uniform float _RingPassTimeLength; // 링이 모든 깊이를 모두 도는데 걸리는 시간 uniform float _RingWidth; // 링의 너비 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; } sampler2D _MainTex; half4 frag (v2f i) : COLOR { // _CameraDepthExture로 부터 각 화면좌표의 깊이값을 추출한다. float depthValue = Linear01Depth (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos)).r); // 2d 투영 텍스처 샘플링, tex2DProj(s,t) : t.w값으로 나눈다. // https://en.wikibooks.org/wiki/Cg_Programming/Unity/Projectors fixed4 orgColor = tex2Dproj(_MainTex, i.scrPos); //Get the orginal rendered color float4 newColor; // 링이 지나간 후 색상 half4 lightRing; // the ring of light that will pass through the dpeth // t의 값은 1에서 0이 된다. float t = 1 - ((_Time.y - _StartingTime)/_RingPassTimeLength ); // 효과 시작 여부 if (_RunRingPass == 1) { // 이 부분은 링 라이트를 그린다. if (depthValue < t && depthValue > t - _RingWidth) { lightRing.r = 1; lightRing.g = 0; lightRing.b = 0; lightRing.a = 1; return lightRing; } else { if (depthValue < t) { // 아직 링이 지나가지 않은 부분 return orgColor; } else { // 링이 지나간 부분 // 원래 색상에 살짝 레드 너어줌. newColor.r = (orgColor.r + 1)*0.5; newColor.g = orgColor.g*0.5; newColor.b = orgColor.b*0.5; newColor.a = 1; return newColor; } } } else { return orgColor; } } ENDCG } } FallBack "Diffuse" }