"Unity3D ShaderLab Development combat explain" Chapter 10 plane shadow

The parallel light projection plane

Principle: The transformed vertices into the local object received spatial plane in shadow, and calculating the vertex coordinates in a direction parallel to the projection of light on a plane.

  • Receiving plane matrix shadow object passed to each transmit shadow.
public class PlaneShadowCaster : MonoBehaviour{
	public Transform reciver;
	
	void Update(){
		renderer.shaderMaterial.SetMatrix("_World2Ground", reciever.renderer.worldToLocalMatrix);
		renderer.shaderMaterial.SetMatrix("_Ground2World", reciever.renderer.LocalToWorldMatrix);
	}
}
  • The shadow of plane material objects in the generation of a shadow.
Shader "Tul/Shadow/PlanarShadow_1" {
	SubShader{
		Pass{
			//对物体本身做一个简单的光照计算。
			Tags{"LightMode" = "ForwardBase" }
			Material{Diffuse(1,1,1,1)}
			Lighting On
		}
		Pass{
			Tags{ "LightMode" = "ForwardBase"}
			Blend DstColor SrcColor
			Offset -1 -1
			CGPROGRAM
			#pragma vertex vert;
			#pragma fragment frag
			#include "UnityCG.cginc"
			float4x4 _World2Ground;
			float4x4 _Ground2World;
		
			float4 vert(float4 vertex:POSITION):SV_POSITION;
				float3 litDir;
				litDir = WorldSpaceLightDir(vertex);
				litDir = mul(float3x3(_World2Ground), litDir);
				litDir = normalize(litDir);
				float4 vt;
				vt = mul(_Object2World, vertex);
				vt = mul(_World2Ground, vt);
				vt.xy = vt.xz - (vt.y/litDir.y) * litDir.xz;//根据三角形相似计算沿光源方向投射后的xz坐标。
				vt.y = 0;
				vt = mul(_Ground2World, vt);
				vt = mul(_World2Object, vt);
				return mul(UNITY_MATRIX_MVP, vt);
			}
			float4 frag(void):COLOR{
				return float4(0.3,0.3,0.3,1);//投射一个灰色的影子。
			}
			ENDCG
		}
	}
}

Projection of point light plane

It is different from the direction of the point light source is calculated by the vertex positions, the above and other exactly.

Shadow fade out

Principle: the farther from the vertex vertex distance in front of the projection after projection, it is the more natural light shadow.

Tags{ "LightMode" = "ForwardBase"}
	Blend DstColor SrcColor
	Offset -1 -1
	CGPROGRAM
	#pragma vertex vert;
	#pragma fragment frag
	#include "UnityCG.cginc"
	float4x4 _World2Ground;
	float4x4 _Ground2World;
		
	struct v2f{
		float4 pos:SV_POSITION;
		float atten:TEXCOORD0;
	}
	v2f vert(float4 vertex:POSITION):SV_POSITION;
		v2f o;
		float3 litDir;
		litDir = WorldSpaceLightDir(vertex);
		litDir = mul(float3x3(_World2Ground), litDir);
		litDir = normalize(litDir);
		float4 vt;
		vt = mul(_Object2World, vertex);
		vt = mul(_World2Ground, vt);
		vt.xy = vt.xz - (vt.y/litDir.y) * litDir.xz;//根据三角形相似计算沿光源方向投射后的xz坐标。
		vt.y = 0;
		vt = mul(_Ground2World, vt);
		vt = mul(_World2Object, vt);
		o.pos = mul(UNITY_MATRIX_MVP, vt);
		o.atten = distance(vertex, vt)/_Intensity;//计算衰减。
		return o;
	}
	float4 frag(v2f i):COLOR{
		return smoothstep(0, 1, i.atten/2);
	}
	ENDCG
}
	
Published 41 original articles · won praise 4 · Views 3890

Guess you like

Origin blog.csdn.net/weixin_42487874/article/details/103292450