Shader "Unlit/TestCrossShader" { Properties { _MainTex ("Texture", 2D) = "white" {} _AlphaData("Alpha Data",float)=1.0 _IntersectPower("Intersect Power",float)= 1.0 _ScanValue("Scan Value",float)= 1.0 _ScanLineWidth("Scan Line Width",float)= 1.0 _ScanLineColor("Scan Line Color",COLOR)=(1,1,1,1) } SubShader { Tags { "RenderType"="Opaque" "Queue"="Transparent" } LOD 100 Pass { ZWrite Off //ZTest Always Blend SrcAlpha OneMinusSrcAlpha CGPROGRAM #pragma vertex vert #pragma fragment frag // make fog work #pragma multi_compile_fog #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float3 normal : NORMAL; float2 uv : TEXCOORD0; }; struct v2f { float2 uv : TEXCOORD0; UNITY_FOG_COORDS(1) float4 vertex : SV_POSITION; float4 scrPos : TEXCOORD1; float3 worldNormal : TEXCOORD2; }; sampler2D _MainTex; float4 _MainTex_ST; float _AlphaData; sampler2D _CameraDepthTexture; float _ScanValue; float _ScanLineWidth; fixed4 _ScanLineColor; float _IntersectPower; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); o.scrPos = ComputeScreenPos(o.vertex); o.worldNormal = UnityObjectToWorldNormal(v.normal); COMPUTE_EYEDEPTH(o.scrPos.z); return o; } fixed4 frag (v2f i) : SV_Target { // sample the texture fixed4 col = tex2D(_MainTex, i.uv); fixed4 refrCol = tex2D(_CameraDepthTexture, i.scrPos.xy/i.scrPos.w); //float d = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture,i.uv); //float d = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture,UNITY_PROJ_COORD(i.scrPos)); //float depth = tex2D(_CameraDepthTexture, i.uv).r; float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv); float linearDepth = LinearEyeDepth(refrCol.r); float partZ = i.scrPos.z; // 两者相减就是深度的差异diff,再用1 - diff就得到了一个“相交程度”。 float diff = linearDepth - partZ; // 两个值都在同一空间下, 就可以做比较了 float intersect = (1 - diff) * _IntersectPower; col = float4(0,0, intersect*_AlphaData, 1 ); //col = fixed4(i.worldNormal,col.a*_AlphaData); // apply fog // UNITY_APPLY_FOG(i.fogCoord, col); return col; /*float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv); float lnr01Depth = Linear01Depth(depth); fixed4 screenTexture = tex2D(_MainTex, i.uv); float near = smoothstep(_ScanValue, lnr01Depth, _ScanValue - _ScanLineWidth); // _ScanValue 就是 控制值, 在 [0, 1] 区间 float far = smoothstep(_ScanValue, lnr01Depth, _ScanValue + _ScanLineWidth); fixed4 emissionClr = _ScanLineColor * (near + far); return screenTexture + emissionClr;*/ } ENDCG } } }