# 模擬實時光照下的陰影

{

ax+by+cz-(a*x2+b*y2+c*z2)=0           1

}

{

x1=x + k*LightVec.x             2

y1=y+ k*LightVec.y             3

z1=z+ k*LightVec.z              4

}

（x1,y1,z1）和（x2,y2,z2）同樣是平面上的點，故可以將234代入1式中，得到:

k=a(x2-x)+b(y2-y)+c(z2-z)/(a*LightVec.x + b*LightVec.y +c*LightVec.z )

{

x1=0.3

y1=k*y + _LightVec.y;

z1=k*z + _LightVec.z;

}

``````Shader "Expand/RealTimeShadow" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_LightVec ("Light Position", Vector) = (1, 0, 0, 0)	//平行光向量
_Hight("Hight", Range (0.0, 2)) = 0.32
}
Tags {"Queue"="Geometry"}
LOD 200
//影子
Pass {
CGINCLUDE
#include "UnityCG.cginc"

struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : POSITION;
};
ENDCG

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 _LightVec;
float _Hight;
v2f vert(appdata v) {
v2f o;
//假設投影的平面上有一個點（0.32, 0, 0）爲1x+0y+0z-(1*0.32+b*0+c*0)=0; 平行光向量;
float3 vec3 = _LightVec;
float k = (_Hight-v.vertex.x)/vec3.x;
v.vertex.y = k*vec3.y + v.vertex.y;
v.vertex.z = k*vec3.z + v.vertex.z;
v.vertex.x = _Hight;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
}
half4 frag(v2f i) : COLOR {
return 0;
}
ENDCG
}
}
}``````