Home Technical Talk

[Unity] Shader, custom lighting function does not work properly with post processed AO

polycounter lvl 9
Offline / Send Message
CarlCraft polycounter lvl 9
I'm quite new to writing shaders but this one problem has made my hair grey way too quickly:

I'm trying to write a foliage shader with good looking backface lighting. The problem I'm having is the post process AO in the project. Whenever I use anything other than the Standard lighting function, the AO behind the object with my foliage shader is rendered in front of it. See below:

Using my original code "#pragma surface surf StandardTranslucent fullforwardshadows". AO from the rocks in the background is rendered on top of the quad.

Changing code to " #pragma surface surf Standard fullforwardshadows". AO stays in the back. The quad still renders its own AO properly (none in this case. Bad example)

I am even invoking the Standard lighting function, but to no avail. My shader code:
Shader "Custom/testFoliageTwoPass" {
	Properties {
		_Color ("Color", Color) = (1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {}

		_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
	}
	SubShader {
		Tags { 
		"RenderType"="TransparentCutout" 
		"Queue"="alphatest"
		"IgnoreProjector"="True" 
		}
		LOD 200

		Cull Off

		CGPROGRAM
		#pragma surface surf StandardTranslucent fullforwardshadows
		#pragma target 3.0

		sampler2D _MainTex;

		struct Input {
			float2 uv_MainTex;
		};

		fixed4 _Color;

		#include "UnityPBSLighting.cginc"
		inline fixed4 LightingStandardTranslucent(SurfaceOutputStandard s, fixed3 viewDir, UnityGI gi)
		{
			// Original colour
			fixed4 pbr = LightingStandard(s, viewDir, gi);

			return pbr;
		}

		void LightingStandardTranslucent_GI(SurfaceOutputStandard s, UnityGIInput data, inout UnityGI gi)
		{
			LightingStandard_GI(s, data, gi);		
		}

		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;
		}
		ENDCG	
	}
}

Does anyone have any clue to why the AO from the background is rendered on top of the quad? My suspicion is that it must be a deferred rendering path, but as I said I'm pretty new so I don't really know what that means..! But that's what my trail & erroring have showed. Am I on the right track?

Sign In or Register to comment.