choose between legacy deferred and foward mode

37 篇文章 5 订阅

in the article http://blog.csdn.net/wodownload2/article/details/79449760
we give an example of legacy deferred shader. we recall the steps we take to bring legacy deferred into effect.
1, we first give the light mode to implement the deferred lighting mode.
2, we set the camera’s render path to legacy deferred.
then we will set the object will be rendered.

we also know that if we set the camera’s rendering path to forward or others mode. the object will not be seen.

in this article, we will learn how to write a compatible shader that the object will be seen in both forward mode and legacy deferred mode.

yep, we give the shader like this:

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Course/ForwardAndDeferred" 
{
	SubShader 
	{
		pass
		{
			Tags{ "LightMode"="ForwardBase"}
			Blend One Zero
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
			#include "Lighting.cginc"

			struct vertOut{
				float4 pos:SV_POSITION;
				float4 color:COLOR;
			};
			vertOut vert(appdata_base v)
			{
				vertOut o;
				o.pos=UnityObjectToClipPos(v.vertex);
				o.color=_LightColor0.rgba;
				return o;
			}
			float4 frag(vertOut i):COLOR
			{
				return i.color;
			}
			ENDCG
		}

		Blend One Zero
		CGPROGRAM
		#pragma surface surf MyDeferred 
		half4 LightingMyDeferred_PrePass (SurfaceOutput s, half4 light) 
		{
			half4 c;
			c.rgb = s.Albedo;
			c.a = s.Alpha;
			return c;
		}

		struct Input {
			float2 uv_MainTex;
		};
		sampler2D _MainTex;
		void surf (Input IN, inout SurfaceOutput o) {
			o.Albedo=float3(1,0,0);
		}
		ENDCG
		
	}
}

when we set the camera’s rendering path to forward or deferred, the object will be rendered by pass. that means when unity fails from the legacy deferred mode. the unity will use forward pass to render object. now we set the camera’s rendering mode to forward or deferred, the following shader code will be invalid.

Blend One Zero
		CGPROGRAM
		#pragma surface surf MyDeferred 
		half4 LightingMyDeferred_PrePass (SurfaceOutput s, half4 light) 
		{
			half4 c;
			c.rgb = s.Albedo;
			c.a = s.Alpha;
			return c;
		}

		struct Input {
			float2 uv_MainTex;
		};
		sampler2D _MainTex;
		void surf (Input IN, inout SurfaceOutput o) {
			o.Albedo=float3(0,1,0); //green
		}
		ENDCG

the above shader will only be executed when camera’s rendering mode be set to legacy deferred.

ok, when we set the camera’s rendering mode to legacy deferred, the object will we rendered to the color green.

we should pay attention to that we make alpha blend to be equal to one zero, so itis clear to see the effect by changing the render color value in shader.
这里写图片描述

the color of object is the color of the light
这里写图片描述
这里写图片描述

if the rendering path of camera is set to legacy deferred, the effect is like:
这里写图片描述

ok, now we have given the compatible shader.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值