天天看点

shader遮挡显示

原理:根据深度建立两个pass,小于当前被挡物体的正常显示,大于被挡物体的返回一个纯色

Shader "Custom/XRay" {
	Properties {
        _MainTex("Albedo", 2D) = "white" {}      
        _AfterColor ("After Color", Color) = (0.435, 0.851, 1, 0.419)  
	}
	SubShader {
		Tags { "RenderType"="transparent" }
		LOD 200
		pass
		{
			Tags { "LightMode" = "Vertex" } 
			Blend One OneMinusSrcColor  
            Cull Off  
            Lighting Off  
            ZWrite Off  
            Ztest Greater 
			CGPROGRAM
			#pragma vertex vert    
            #pragma fragment frag 
            #include "UnityCG.cginc" 
            float4 _AfterColor; 
            struct v2f
            {
            	float4 pos : SV_POSITION;   
                float2 uv : TEXCOORD0; 
            };
            v2f vert (appdata_full v) 
            {
            	v2f o;
            	o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
            	o.uv = v.texcoord;
            	return o;
            };
            
            fixed4 frag(v2f i) : COLOR
            {
            	return 2.0 * _AfterColor;
            }
			ENDCG
		}
		pass
		{
			ZTest LEqual     
            CGPROGRAM  
            #pragma vertex vert    
            #pragma fragment frag    
            #include "UnityCG.cginc"
            sampler2D _MainTex;    
            float4 _MainTex_ST;    
            
            struct v2f
            {
            	float4 pos : SV_POSITION;
            	float4 uv : TEXCOORD0;
            };  
            
            v2f vert(appdata_base v)
            {
            	v2f o;
            	o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
            	o.uv = v.texcoord;
            	return o;
            }
            
            fixed4 frag(v2f i) : COLOR
            {
            	float4 texCol = tex2D(_MainTex, i.uv);
            	return texCol;
            }
            ENDCG
		}
	} 
	FallBack "Diffuse"
}
           

当然被挡住的部也可以做得更好看,比如显示成另外的贴图

Shader "Custom/XRay2" {
	Properties {
		_Color("Color", Color) = (1,1,1,1)      
        _MainTex("Albedo", 2D) = "white" {}    
        _AfterTex("_AfterTex", 2D) = "white" {}    
        _AfterColor ("After Color", Color) = (0.435, 0.851, 1, 0.419)   
	}
	SubShader {
		Tags { "RenderType"="transparent" }
		LOD 300
		Blend SrcAlpha OneMinusSrcAlpha   
		pass
		{
			Tags { "LightMode" = "Vertex" } 
			Blend One OneMinusSrcColor  
            Cull Off  
            Lighting Off  
            ZWrite Off  
            Ztest Greater   
			CGPROGRAM
			#pragma vertex vert    
            #pragma fragment frag 
            #include "UnityCG.cginc"  
            sampler2D _AfterTex; 
            float4 _AfterColor;   
            struct v2f
            {
            	float4 pos : SV_POSITION;   
                float2 uv : TEXCOORD0; 
            };
            v2f vert (appdata_base v) 
            {
            	v2f o;
            	o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
            	half2 coord;
            	float3 normal = mul(UNITY_MATRIX_IT_MV, v.normal);
            	coord.x = normal.x;
            	coord.y = normal.y;
            	o.uv = coord * 0.5 + 0.5;
            	return o;
            };
            
            fixed4 frag(v2f i) : COLOR
            {
            	float4 col = tex2D(_AfterTex, i.uv);
            	return col * 2.0 * _AfterColor;
            }
			ENDCG
		}
		pass
		{
			ZTest LEqual     
            CGPROGRAM  
            #pragma vertex vert    
            #pragma fragment frag    
            #include "UnityCG.cginc"
            sampler2D _MainTex;    
            float4 _MainTex_ST;    
            
            struct v2f
            {
            	float4 pos : SV_POSITION;
            	float4 uv : TEXCOORD0;
            };  
            
            v2f vert(appdata_base v)
            {
            	v2f o;
            	o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
            	o.uv = v.texcoord;
            	return o;
            }
            
            fixed4 frag(v2f i) : COLOR
            {
            	float4 texCol = tex2D(_MainTex, i.uv);
            	return texCol;
            }
            ENDCG
		}
	} 
}