当前位置:网站首页>[unity shader] insert pass to realize the X-ray perspective effect of model occlusion

[unity shader] insert pass to realize the X-ray perspective effect of model occlusion

2022-07-07 18:26:00 InfoQ

The original shader unchanged , Insert an extra one pass Channels are used to render occluded parts , Realization X The perspective effect renders normally Pass, Render only the unobstructed part ;X Fluoroscopy Pass, Render only the occluded part .

It can be used to process the stroke display when the player is blocked by obstacles , Or see through the enemy and other special display effects .

Realization effect :



branch Pass Rendering :

  • Perspective effect Pass, Use... When rendering ZTest Greater, The first rendering is the front occlusion Cube, At this time, there is Cube Depth value of . Then render the model , The depth test of the uncovered part of the model fails , Does not render , The occluded part passed the depth test , Normal display .
  • Normal rendering Pass, The first rendering is the front occlusion Cube, At this time, there is Cube Depth value of . Then render the model , The depth test of the occluded part of the model failed , Does not render , The unobstructed part is displayed normally .

Depth testing ZTest Settable test rules :
  • ZTest Less: If the depth is less than the current cache, pass
  • ZTest Greater: If the depth is greater than the current cache, pass
  • ZTest LEqual: If the depth is less than or equal to the current cache, it passes
  • ZTest GEqual: If the depth is greater than or equal to the current cache, it passes through
  • ZTest Equal: If the depth is equal to the current cache
  • ZTest NotEqual: If the depth is not equal to the current cache, pass
  • ZTest Always: Pass anyway

Shader Code :

// primary Pass unchanged , Insert a Pass, Used to display the occluded part of the model
 
Shader "Test/Model_XRay"
{
Properties
{
_MainTex ("Diffuse (RGB)", 2D) = "grey" {}
_Color("Color (RGBA)", Color) = (1,1,1,1)
 
_XRayColor("XRay Color", Color) = (1,1,1,1)
}

SubShader
{
Tags
 {
 "RenderType"="Opaque"
 "Queue"="AlphaTest+1"
 "IgnoreProjector"="True"
 }
 
LOD 200
Fog { Mode Off }
 
// Rendering X Perspective effect Pass
Pass
{
Blend SrcAlpha One
ZWrite Off
ZTest Greater
 
CGPROGRAM
 
#pragma vertex vert
#pragma fragment frag
 
#include "Lighting.cginc"
 
fixed4 _XRayColor;
 
struct v2f
{
float4 pos : SV_POSITION;
float3 normal : normal;
float3 viewDir : TEXCOORD0;
};
 
v2f vert (appdata_base v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.viewDir = ObjSpaceViewDir(v.vertex); // Calculate the vector from vertex to camera
o.normal = v.normal;
return o;
}
 
fixed4 frag(v2f i) : SV_Target
{
// Calculate the color concentration by using the dot multiplication value of the normal and the line of sight vector
float3 normal = normalize(i.normal);
float3 viewDir = normalize(i.viewDir);
float rim = 1 - dot(normal, viewDir);
return _XRayColor * rim;
}
 
ENDCG
}
 
Pass
{
 CGPROGRAM
 
 // Defining vertices / Chip shader code
 #pragma vertex vert
 #pragma fragment frag
 
 #include "UnityCG.cginc"
 
 sampler2D _MainTex; 
 
 // Define the input of the vertex shader
 struct a2v
 {
 float4 vertex : POSITION;
 float2 uv :TEXCOORD0;
 };
 // Define the output of vertex shaders 、 Input of the slice shader
 struct v2f
 {
 float4 vertex : SV_POSITION;
 float2 uv:TEXCOORD0;
 };
 
 v2f vert (a2v v)
 {
 v2f o;
 o.vertex = UnityObjectToClipPos(v.vertex);
 o.uv = v.uv;
 return o;
 } 
 fixed4 frag (v2f i) : SV_Target
 {
 return tex2D(_MainTex,i.uv);
 }
 ENDCG
 
}
 
}
FallBack "Diffuse"
}

原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071630478737.html