- Home /
Setting up a simple depth-based Shader Replacement material?
I have been trying to get a handle on the concept of Shader Replacement and am having a hard time getting things to work. The first goal is simply to render the difference in depth between a mesh's front faces and back faces; this is my first step toward volume fog.
Here is the code for the script which is attached to the main camera:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class VolumeFogRenderer : MonoBehaviour
{
public Shader m_BackFaceDepthsShader;
public Shader m_FrntFaceDepthsShader;
public Material m_VolumeFogMaterial;
private GameObject _shaderReplacementCamera;
void OnPreRender()
{
if (!enabled || !gameObject.activeSelf)
{
return;
}
if (!_shaderReplacementCamera)
{
_shaderReplacementCamera = new GameObject("CameraVolumeFog");
_shaderReplacementCamera.hideFlags = HideFlags.HideAndDontSave;
_shaderReplacementCamera.AddComponent<Camera>();
_shaderReplacementCamera.camera.enabled = false;
}
Camera cam = _shaderReplacementCamera.camera;
cam.CopyFrom(camera);
cam.backgroundColor = new Color(0, 0, 0, 0);
cam.clearFlags = CameraClearFlags.SolidColor;
RenderTexture backFaceDepthsRT = RenderTexture.GetTemporary(Screen.width, Screen.height, 16);
cam.targetTexture = backFaceDepthsRT;
cam.RenderWithShader(m_BackFaceDepthsShader, "Fog");
RenderTexture frntFaceDepthsRT = RenderTexture.GetTemporary(Screen.width, Screen.height, 16);
cam.targetTexture = frntFaceDepthsRT;
cam.RenderWithShader(m_FrntFaceDepthsShader, "Fog");
m_VolumeFogMaterial.SetTexture("_BackFaceDepths", backFaceDepthsRT);
m_VolumeFogMaterial.SetTexture("_FrntFaceDepths", frntFaceDepthsRT);
RenderTexture.ReleaseTemporary(backFaceDepthsRT);
RenderTexture.ReleaseTemporary(frntFaceDepthsRT);
}
void OnDisable()
{
if (_shaderReplacementCamera != null)
{
DestroyImmediate(_shaderReplacementCamera);
}
}
}
This script requires 3 objects to be set up in the inspector: a back face depth shader, front face depth shader, and a composite material which renders the difference between the two RenderTextures produced from the first two shaders:
Here is the shader for the composite material:
Shader "Custom/VolumeFog"
{
Properties
{
_MainTex ("Base (RGB) Trans (A)", 2D) = "" {}
_BackFaceDepths ("Back Face Depths", 2D) = "" {}
_FrntFaceDepths ("Front Face Depths", 2D) = "" {}
}
SubShader
{
Tags
{
"Queue" = "Transparent-1"
"RenderType" = "Fog"
"IgnoreProjector" = "True"
}
Lighting Off
ZWrite Off
Fog { Mode Off }
LOD 200
CGPROGRAM
#pragma surface surf Lambert alpha
sampler2D _MainTex;
sampler2D _BackFaceDepths;
sampler2D _FrntFaceDepths;
struct Input
{
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 bg = tex2D(_MainTex, IN.uv_MainTex);
fixed4 b = tex2D(_BackFaceDepths, IN.uv_MainTex);
fixed4 f = tex2D(_FrntFaceDepths, IN.uv_MainTex);
fixed4 d = clamp(b - f, 0, 1);
o.Albedo = d.rgb;
o.Alpha = d.a;
}
ENDCG
}
FallBack Off
}
And finally the two shaders used for replacement shading. First the back face depths:
Shader "Custom/Back Face Depths"
{
SubShader
{
Tags
{
"RenderType" = "Fog"
}
Pass
{
Fog { Mode Off }
Lighting Off
Cull Front
CGPROGRAM
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float4 pos : POSITION;
float2 depth : TEXCOORD0;
};
v2f vert( appdata_base v )
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
COMPUTE_EYEDEPTH(o.depth);
return o;
}
float4 frag(v2f i) : COLOR
{
half4 d = i.depth.x / 20;
half4 c;
c.r = d;
c.g = d;
c.b = d;
c.a = d;
return c;
}
ENDCG
}
}
}
And now the front face depths:
Shader "Custom/Front Face Depths"
{
SubShader
{
Tags
{
"RenderType" = "Fog"
}
Pass
{
Fog { Mode Off }
Lighting Off
Cull Back
CGPROGRAM
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float4 pos : POSITION;
float2 depth : TEXCOORD0;
};
v2f vert( appdata_base v )
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
COMPUTE_EYEDEPTH(o.depth);
return o;
}
float4 frag(v2f i) : COLOR
{
half4 d = i.depth.x / 20;
half4 c;
c.r = d;
c.g = d;
c.b = d;
c.a = d;
return c;
}
ENDCG
}
}
}
Now, this is just a very simplified form of my final goal. Ultimately I won't be unpacking the depth textures before passing them over to the composite material, but for now I wanted to see that they were set up properly, and they seem to be. I can verify this by creating materials for each of them and applying them to a mesh--voila, back and front face depths are rendered.
So what's wrong? I suspect it may be my UVs in the VolumeFog shader, as I don't fully understand how to set those up, or it could be something wrong with my script handling the pipeline/property setup, or maybe something else entirely.
For the record, the above code compiles just fine, though the result is a fully transparent mesh. When I play with the Albedo/Alpha settings and input the f or b pixels respectively, they seem to be completely empty...
Looks to me like you need to get the fragments screen position for the UV in the volume shader part I'd imagine. See this: http://answers.unity3d.com/questions/448711/getting-a-pixels-screen-space-coordinate-in-a-frag.html
I was wondering if that's what was going wrong. So to do that I need to generate the screen space position... Is this right? It still produces black:
float3 uv = mul((float3x3)UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, IN.worldPos);
I tried using IN.screenPos as suggested here: http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaderExamples.html but also to no avail, just black.
Your answer
Follow this Question
Related Questions
Represent Land 3D Area End 0 Answers
Unity Sphere object not displaying proper color away from camera 0 Answers
Cubemaps using the camera 0 Answers