Hello,
I have a texture which contains a custom Depth, and I would like to apply shadows on this depth.
My rendering path is forward.
For the moment here is the result, I have applied the depth and the normals, but the shadows are missing
Here is the depth:
When I try to add the shadows, the texture “_ShadowMapTexture” is always empty. But with the frame debugger I have this:
The image above shows the shadow at the end of the pass collecting shadows, so it’s not empty and it’s well calculated.
To get my current result I have two passes at different places in the pipeline. The first pass is “after the Depth texture” where I add the depth, and the second pass is “AfterForwardOpaque”.
Each pass draws a quad in front of the camera.
The first pass uses this shader:
Shader "Custom/Custom_Depth" {
Properties
{
_MainTex("Base (RGB) Trans (A)", 2D) = "" {}
_Cutoff("Alpha cutoff", Range(0,1)) = 0.5
[HideInInspector] _Color("Main Color", Color) = (1,1,1,1)
}
SubShader
{
Tags{
"Queue" = "Background"
}
Pass
{
ZWrite On
ZTest LEqual
Fog { Mode Off }
Cull Off
Blend SrcAlpha Zero
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 4.0
#include "UnityCG.cginc"
#include "S_Utils.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct Input {
float2 uv_MainTex;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
uniform float4x4 _ProjectionMatrix;
float4 _MainTex_ST;
float4 _Color;
uniform float4 _MainTex_TexelSize;
fixed3 computeNormals(fixed2 coordTexture, fixed4 coordinate3D)
{
fixed2 texelSize = fixed2(_MainTex_TexelSize.x, _MainTex_TexelSize.y) * 2;
const int HALF_RADIUS = 2;
float3 averageNormal = float3(0, 0, 0);
int i = 0; int j = 0;
float2 offsetX;
float2 offsetY;
float3 vX, vY, vC;
float3 vXC, vYC, crossPdt;
for (j = -HALF_RADIUS; j < HALF_RADIUS; ++j)
{
for (i = -HALF_RADIUS; i < HALF_RADIUS; ++i)
{
offsetX = coordTexture + float2(texelSize.x * (i + 1), texelSize.y * j);
offsetY = coordTexture + float2(texelSize.x * i, texelSize.y * (j + 1));
vX = tex2D(_MainTex, offsetX).xyz;
vY = tex2D(_MainTex, offsetY).xyz;
vC = tex2D(_MainTex, float2(offsetY.x, offsetX.y)).xyz;
averageNormal += normalize(cross(normalize(vX - vC), normalize(vY - vC)));
vXC = normalize(vX - vC);
vYC = normalize(vY - vC);
crossPdt = float3(vXC.y*vYC.z - vXC.z*vYC.y, vXC.z*vYC.x - vXC.x*vYC.z, vXC.x*vYC.y - vXC.y*vYC.x);
averageNormal += normalize(crossPdt);
}
}
fixed3 normals = normalize(averageNormal);
if (dot(normals, normalize(coordinate3D.xyz)) > 0)
{
normals = -normals;
}
//normals.z = -normals.z;
return normals;
}
v2f vert(appdata v)
{
v2f o;
float4x4 copy_projection = UNITY_MATRIX_P;
copy_projection[0][2] = _ProjectionMatrix[0][2];
copy_projection[1][2] = _ProjectionMatrix[1][2];
o.vertex = mul(mul(mul(copy_projection, UNITY_MATRIX_V), UNITY_MATRIX_M), v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag(v2f i, out float outDepth : SV_Depth) : SV_Target
{
//Get the depth in XYZ format
float3 colorXYZ = tex2D(_MainTex, (i.uv*fixed2(1, -1))).rgb;
//Compute the depth to work with Unity (1m real = 1m to Unity)
float depthReal = computeDepthXYZ(colorXYZ);
fixed2 coordTexture = i.uv * fixed2(1, -1);
fixed3 normals = computeNormals(coordTextureZED, tex2D(_MainTex, coordTexture));
outDepth = depthReal;
return fixed4(normals.x, normals.y, normals.z, depthReal);
}
ENDCG
}
}
}
The second pass uses this pass :
// Upgrade NOTE: replaced '_LightMatrix0' with 'unity_WorldToLight'
// Upgrade NOTE: replaced '_LightMatrix0' with 'unity_WorldToLight'
Shader "Hidden/ZED_Depth_Light"
{
Properties{
_Color("Color", Color) = (1,1,1,1)
_MainTex("Albedo (RGB)", 2D) = "white" {}
_Glossiness("Smoothness", Range(0,1)) = 0.5
_Metallic("Metallic", Range(0,1)) = 0.0
_ShadowMapTexture("", any) = "" {}
}
SubShader{
Tags{ "RenderType" = "Opaque" }
LOD 200
// ------------------------------------------------------------
// Surface shader code generated out of a CGPROGRAM block:
// ---- forward rendering base pass:
Pass{
ZWrite Off
//ZTest Always
Name "FORWARD"
Tags{ "LightMode" = "ForwardBase" }
CGPROGRAM
// compile directives
#pragma vertex vert_surf
#pragma fragment frag_surf
#pragma debug
#pragma target 3.0
#pragma multi_compile_fog
#pragma multi_compile_fwdbase
#include "HLSLSupport.cginc"
#include "UnityShaderVariables.cginc"
#include "S_Utils.cginc"
// Surface shader code generated based on:
// writes to per-pixel normal: no
// writes to emission: no
// writes to occlusion: no
// needs world space reflection vector: no
// needs world space normal vector: no
// needs screen space position: no
// needs world space position: no
// needs view direction: no
// needs world space view direction: no
// needs world space position for lighting: YES
// needs world space view direction for lighting: YES
// needs world space view direction for lightmaps: no
// needs vertex color: no
// needs VFACE: no
// passes tangent-to-world matrix to pixel shader: no
// reads from normal: no
// 1 texcoords actually used
// float2 _MainTex
#define UNITY_PASS_FORWARDBASE
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "UnityPBSLighting.cginc"
#include "AutoLight.cginc"
#define INTERNAL_DATA
#define WorldReflectionVector(data,normal) data.worldRefl
#define WorldNormalVector(data,normal) normal
// Original surface shader snippet:
#line 8 ""
#ifdef DUMMY_PREPROCESSOR_TO_WORK_AROUND_HLSL_COMPILER_LINE_HANDLING
#endif
//#pragma debug
// Physically based Standard lighting model, and enable shadows on all light types
//#pragma surface surf Standard fullforwardshadows nometa exclude_path:deferred
// Use shader model 3.0 target, to get nicer looking lighting
//#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
fixed4 _Color;
void surf(Input IN, inout SurfaceOutputStandard o) {
// Albedo comes from a texture tinted by color
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
sampler2D _ShadowMapTexture;
float4 _ShadowMapTexture_TexelSize;
// vertex-to-fragment interpolation data
// no lightmaps:
#ifdef LIGHTMAP_OFF
struct v2f_surf {
float4 pos : SV_POSITION;
float2 pack0 : TEXCOORD0; // _MainTex
half3 worldNormal : TEXCOORD1;
float3 worldPos : TEXCOORD2;
#if UNITY_SHOULD_SAMPLE_SH
half3 sh : TEXCOORD3; // SH
#endif
SHADOW_COORDS(4)
UNITY_FOG_COORDS(5)
#if SHADER_TARGET >= 30
float4 lmap : TEXCOORD6;
#endif
UNITY_INSTANCE_ID
};
#endif
// with lightmaps:
#ifndef LIGHTMAP_OFF
struct v2f_surf {
float4 pos : SV_POSITION;
float2 pack0 : TEXCOORD0; // _MainTex
half3 worldNormal : TEXCOORD1;
float3 worldPos : TEXCOORD2;
float4 lmap : TEXCOORD3;
SHADOW_COORDS(4)
UNITY_FOG_COORDS(5)
#ifdef DIRLIGHTMAP_COMBINED
fixed3 tSpace0 : TEXCOORD6;
fixed3 tSpace1 : TEXCOORD7;
fixed3 tSpace2 : TEXCOORD8;
#endif
UNITY_INSTANCE_ID
};
#endif
float4 _MainTex_ST;
// vertex shader
v2f_surf vert_surf(appdata_full v) {
UNITY_SETUP_INSTANCE_ID(v);
v2f_surf o;
UNITY_INITIALIZE_OUTPUT(v2f_surf,o);
UNITY_TRANSFER_INSTANCE_ID(v,o);
o.pos = UnityObjectToClipPos(v.vertex);
o.pack0.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
#if !defined(LIGHTMAP_OFF) && defined(DIRLIGHTMAP_COMBINED)
fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
fixed tangentSign = v.tangent.w * unity_WorldTransformParams.w;
fixed3 worldBinormal = cross(worldNormal, worldTangent) * tangentSign;
#endif
#if !defined(LIGHTMAP_OFF) && defined(DIRLIGHTMAP_COMBINED)
o.tSpace0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
o.tSpace1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
o.tSpace2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
#endif
o.worldPos = worldPos;
o.worldNormal = worldNormal;
#ifndef DYNAMICLIGHTMAP_OFF
o.lmap.zw = v.texcoord2.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
#endif
#ifndef LIGHTMAP_OFF
o.lmap.xy = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
#endif
// SH/ambient and vertex lights
#ifdef LIGHTMAP_OFF
#if UNITY_SHOULD_SAMPLE_SH
o.sh = 0;
// Approximated illumination from non-important point lights
#ifdef VERTEXLIGHT_ON
o.sh += Shade4PointLights(
unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0,
unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb,
unity_4LightAtten0, worldPos, worldNormal);
#endif
o.sh = ShadeSHPerVertex(worldNormal, o.sh);
#endif
#endif // LIGHTMAP_OFF
TRANSFER_SHADOW(o); // pass shadow coordinates to pixel shader
UNITY_TRANSFER_FOG(o,o.pos); // pass fog coordinates to pixel shader
return o;
}
/****************/
sampler2D _DepthXYZTex;
sampler2D _CameraTex;
struct fragOut {
float depth : SV_Depth;
fixed4 color : SV_Target;
};
uniform float4 _DepthXYZTex_TexelSize;
fixed3 computeNormals(fixed2 coordTexture, fixed4 coordinate3D)
{
fixed2 texelSize = fixed2(_DepthXYZTex_TexelSize.x, _DepthXYZTex_TexelSize.y) * 2;
const int HALF_RADIUS = 2;
float3 averageNormal = float3(0, 0, 0);
int i = 0; int j = 0;
float2 offsetX;
float2 offsetY;
float3 vX, vY, vC;
float3 vXC, vYC, crossPdt;
for (j = -HALF_RADIUS; j < HALF_RADIUS; ++j)
{
for (i = -HALF_RADIUS; i < HALF_RADIUS; ++i)
{
offsetX = coordTexture + float2(texelSize.x * (i + 1), texelSize.y * j);
offsetY = coordTexture + float2(texelSize.x * i, texelSize.y * (j + 1));
vX = tex2D(_DepthXYZTex, offsetX).xyz;
vY = tex2D(_DepthXYZTex, offsetY).xyz;
vC = tex2D(_DepthXYZTex, float2(offsetY.x, offsetX.y)).xyz;
averageNormal += normalize(cross(normalize(vX - vC), normalize(vY - vC)));
vXC = normalize(vX - vC);
vYC = normalize(vY - vC);
crossPdt = float3(vXC.y*vYC.z - vXC.z*vYC.y, vXC.z*vYC.x - vXC.x*vYC.z, vXC.x*vYC.y - vXC.y*vYC.x);
averageNormal += normalize(crossPdt);
}
}
fixed3 normals = normalize(averageNormal);
if (dot(normals, normalize(coordinate3D.xyz)) > 0)
{
normals = -normals;
}
//normals.z = -normals.z;
return normals;
}
float calculateShadowDepth(float3 worldPos) {
float4 projPos = mul(UNITY_MATRIX_VP, float4(worldPos, 1));
projPos = UnityApplyLinearShadowBias(projPos);
return projPos.z / projPos.w;
}
sampler2D _CameraDepthTexture;
// fragment shader
fragOut frag_surf(v2f_surf IN){
fragOut finalFrag;
float3 colorXYZ = tex2D(_DepthXYZTex, (IN.pack0.xy*fixed2(1, -1))).rgb;
float depthReal = computeDepthXYZ(colorXYZ);
//IN.worldPos.z *= depthReal;
UNITY_SETUP_INSTANCE_ID(IN);
// prepare and unpack data
Input surfIN;
UNITY_INITIALIZE_OUTPUT(Input,surfIN);
surfIN.uv_MainTex.x = 1.0;
surfIN.uv_MainTex = IN.pack0.xy;
float3 worldPos = IN.worldPos;
#ifndef USING_DIRECTIONAL_LIGHT
fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));
#else
fixed3 lightDir = _WorldSpaceLightPos0.xyz;
#endif
fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos));
#ifdef UNITY_COMPILER_HLSL
SurfaceOutputStandard o = (SurfaceOutputStandard)0;
#else
SurfaceOutputStandard o;
#endif
o.Albedo = 0.0;
o.Emission = 0.0;
o.Alpha = 0.0;
o.Occlusion = 1.0;
fixed3 normalWorldVertex = fixed3(0,0,1);
o.Normal = IN.worldNormal;
normalWorldVertex = IN.worldNormal;
float3 colorCamera = tex2D(_CameraTex, IN.pack0.xy*fixed2(1, -1)).bgr;
o.Albedo.rgb = colorCamera;
//Get the depth in XYZ format
finalFrag.depth = depthReal;
//finalFrag.depth = 0;
fixed2 coordTextureZED = surfIN.uv_MainTex * fixed2(1, -1);
fixed4 coordinate3D = tex2D(_DepthXYZTex, coordTextureZED);
fixed3 normals = computeNormals(coordTextureZED, coordinate3D);
o.Normal = normals;
o.Albedo = colorCamera;
// compute lighting & shadowing factor
UNITY_LIGHT_ATTENUATION(atten, IN, worldPos)
fixed4 c = 0;
// Setup lighting environment
UnityGI gi;
UNITY_INITIALIZE_OUTPUT(UnityGI, gi);
gi.indirect.diffuse = 0;
gi.indirect.specular = 0;
#if !defined(LIGHTMAP_ON)
gi.light.color = _LightColor0.rgb;
gi.light.dir = lightDir;
gi.light.ndotl = LambertTerm(o.Normal, gi.light.dir);
#endif
// Call GI (lightmaps/SH/reflections) lighting function
UnityGIInput giInput;
UNITY_INITIALIZE_OUTPUT(UnityGIInput, giInput);
giInput.light = gi.light;
giInput.worldPos = worldPos;
giInput.worldViewDir = worldViewDir;
giInput.atten = atten;
#if defined(LIGHTMAP_ON) || defined(DYNAMICLIGHTMAP_ON)
giInput.lightmapUV = IN.lmap;
#else
giInput.lightmapUV = 0.0;
#endif
#if UNITY_SHOULD_SAMPLE_SH
giInput.ambient = IN.sh;
#else
giInput.ambient.rgb = 0.0;
#endif
giInput.probeHDR[0] = unity_SpecCube0_HDR;
giInput.probeHDR[1] = unity_SpecCube1_HDR;
#if UNITY_SPECCUBE_BLENDING || UNITY_SPECCUBE_BOX_PROJECTION
giInput.boxMin[0] = unity_SpecCube0_BoxMin; // .w holds lerp value for blending
#endif
#if UNITY_SPECCUBE_BOX_PROJECTION
giInput.boxMax[0] = unity_SpecCube0_BoxMax;
giInput.probePosition[0] = unity_SpecCube0_ProbePosition;
giInput.boxMax[1] = unity_SpecCube1_BoxMax;
giInput.boxMin[1] = unity_SpecCube1_BoxMin;
giInput.probePosition[1] = unity_SpecCube1_ProbePosition;
#endif
LightingStandard_GI(o, giInput, gi);
// realtime lighting: call lighting function
c += LightingStandard(o, worldViewDir, gi);
UNITY_APPLY_FOG(IN.fogCoord, c); // apply fog
UNITY_OPAQUE_ALPHA(c.a);
//c = tex2D(_ShadowMapTexture, uv).r is all white
finalFrag.color = c;
return finalFrag;
}
ENDCG
}
}
FallBack "Diffuse"
}
And the order of the passes is done in a script attached to a camera:
void Start()
{
mainCamera.depthTextureMode |= DepthTextureMode.Depth;
matRGB.SetTexture("_CameraTex", camZedLeft);
matRGB.SetTexture("_DepthXYZTex", depthXYZZed);
bufferDepth = new CommandBuffer();
bufferDepth.name = "Depth";
mainCamera.AddCommandBuffer(CameraEvent.AfterDepthTexture, bufferDepth);
// mainCamera.AddCommandBuffer(CameraEvent.AfterDepthNormalsTexture, bufferDepth);
matDepthOnly.SetTexture("_MainTex", depthXYZZed);
buffer = new CommandBuffer();
buffer.name = "Image";
buffer.SetRenderTarget(BuiltinRenderTextureType.CameraTarget);
mainCamera.AddCommandBuffer(CameraEvent.AfterForwardOpaque, buffer);
}
private void OnPreRender()
{
bufferDepth.Clear();
buffer.Clear();
bufferDepth.SetRenderTarget(BuiltinRenderTextureType.Depth);
bufferDepth.DrawMesh(mesh, screen.transform.localToWorldMatrix, matDepthOnly);
buffer.SetRenderTarget(BuiltinRenderTextureType.CameraTarget);
buffer.DrawMesh(mesh, screen.transform.localToWorldMatrix, matRGB);
}
Why my shadows are not displayed ?
PS: I have only a directional light



