Hi!
I’am using a deferred decal system. I want to have the possibility to switch to forward rendering.
I tried it with using Graphics.DrawMesh only. It works, but with undesired shown decals (without _NormasCopy). I don’t know how to convert the _NormalsCopy part to forward rendering.
Thank you!
Shader Code:
sampler2D _MainTex;
sampler2D _NormalMap;
float _Alpha;
sampler2D_float _CameraDepthTexture;
sampler2D _NormalsCopy;
void frag(v2f i, out half4 outDiffuse : COLOR0, out half4 outNormal : COLOR1)
{
i.ray = i.ray * (_ProjectionParams.z / i.ray.z);
float2 uv = i.screenUV.xy / i.screenUV.w;
float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, uv);
depth = Linear01Depth (depth);
float4 vpos = float4(i.ray * depth,1);
float3 wpos = mul (unity_CameraToWorld, vpos).xyz;
float3 opos = mul (unity_WorldToObject, float4(wpos, 1)).xyz;
clip (float3(0.5, 0.5, 0.5) - abs(opos.xyz));
i.uv = opos.xz + 0.5;
half3 normal = tex2D(_NormalsCopy, uv).rgb;
fixed3 wnormal = normal.rgb * 2.0 - 1.0;
clip (dot(wnormal, i.orientation) - 0.3);
fixed4 col = tex2D (_MainTex, i.uv);
col.a *= _Alpha;
outDiffuse = col;
fixed3 nor = UnpackNormal(tex2D(_NormalMap, i.uv));
half3x3 norMat = half3x3(i.orientationX, i.orientationZ, i.orientation);
nor = mul (nor, norMat);
outNormal = fixed4(nor * 0.5 + 0.5, col.a);
}
Render Code:
Camera camera = Camera.current;
if (!camera)
{
return;
}
CommandBuffer buffer = null;
if (cameras.ContainsKey(camera))
{
buffer = cameras[camera];
buffer.Clear();
}
else
{
buffer = new CommandBuffer();
cameras[camera] = buffer;
camera.AddCommandBuffer(CameraEvent.BeforeLighting, buffer);
}
var normalsID = Shader.PropertyToID("_NormalsCopy");
buffer.GetTemporaryRT(normalsID, -1, -1);
buffer.Blit(BuiltinRenderTextureType.GBuffer2, normalsID);
RenderTargetIdentifier[] mrt = { BuiltinRenderTextureType.GBuffer0, BuiltinRenderTextureType.GBuffer2 };
buffer.SetRenderTarget(mrt, BuiltinRenderTextureType.CameraTarget);
foreach (Decal decal in DecalManager.Instance.decals)
{
if (decal)
{
buffer.DrawMesh(mesh, decal.transform.localToWorldMatrix, decal.material);
}
}
buffer.ReleaseTemporaryRT(normalsID);