Creating a See-Through | X-Ray Effect In Unity

Again we have the properties which is a texture and a color declared on lines 4 and 5. These properties, same as the ones in the previous shader will be visible in the Inspector tab when we select the material which uses this shader.

In the SubShader on line 10 we have the Queue = Geometry + 5 which will draw the object holding this shader after other objects are drawn because it will check if the object holding this shader needs to be drawn behind or in front of other objects.

Now the Pass code block is slightly different than what we saw in the previous shader.

Again we’re using ZWrite Off and ZTest Greater to make sure the object is rendered behind other objects.

The CGPROGRAM on line 19 indicates the start of the block where the “actual shader code” is and it’s simply HLSL or high level shader language.

Pragma at lines 21 and 22 are declarations of vertex function called vert on line 21, and fragment function called frag on line 22.

The vertex function actually represents a vertex shader which transforms shape positions into 3D drawing coordinates.

The fragment shader compute the renderings of a shape’s color and other attributes.

The #include on line 24 means we’re including UnityCG.cginc which has the render functions that Unity provides.

We need to use the include keyword because shader scripts are not like C# scripts. They don’t inherit functionality from other shaders like C# classes do, so anything we want to use we need to include it.

float4 on line 24 is actually a Vector 4 variable which contains variables for x, y, z, and w.

Struct vertInput on line 28 is basically an object that contains two variables. The first one is a float4 or Vector 4 variable which we’ll use to pass the vertices of the 3D model holding this shader.

float3 normal variable will be used to pass the information about the normal maps on the shader.

On line 33 we have the fragInput struct which has one float4 variable that will give us information about the screen space position.

The vert function on line 37 uses UnityObjectToClipPos which will transform the mesh vertex position from local object space to clip space.

And the frag function on line 43 will return the silhouette color which we specified.

Then we indicate the end of the shader code on line 47 with ENDCG.

The new shader code block from line 51 to line 67 is taking a texture inside a surface function and with the help from tex2D function it will use the float2 parameter that’s passed along with the texture parameter and return a color on the texture at the point where float2 indicates.

To test this shader, select the Player Material and for the shader select FX -> Silhouette:

Source link