Shader "Cg planar shadow" { Properties{ _Color("Object‘s Color",Color) = (0,1,1) _ShadowColor("Shadow‘s Color",1) } SubShader{ Pass{ Tags{ "lightmode" = "ForwardBase" } // rendering of object CGPROGRAM #pragma vertex vert #pragma fragment frag // User-specified properties uniform float4 _Color; float4 vert(float4 vertexPos : POSITION) : SV_POSITION { return mul(UNITY_MATRIX_MVP,vertexPos); } float4 frag(void) : COLOR { return _Color; } ENDCG } Pass{ Tags{ "lightmode" = "ForwardBase" } // rendering of projected shadow Offset -2.0,-2.0 // make sure shadow polygons are on top of shadow receiver CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" uniform float4 _ShadowColor; uniform float4x4 _World2Receiver; // transformation from float4 vert(float4 vertexPos : POSITION) : SV_POSITION{ float4x4 modelMatrix = _Object2World; float4 lightDirection; if (0.0 != _WorldspaceLightPos0.w){ lightDirection = normalize(mul(modelMatrix,vertexPos - _WorldspaceLightPos0)); } else{ lightDirection = -normalize(_WorldspaceLightPos0); } float4 vertexInWorldspace = mul(modelMatrix,vertexPos); //从矩阵从获取Y轴向量 float4 world2ReceiverRow1 = float4(_World2Receiver[1][0],_World2Receiver[1][1],_World2Receiver[1][2],_World2Receiver[1][3]); //顶点向量到Y轴投影 float distanceOfVertex = dot(world2ReceiverRow1,vertexInWorldspace); //灯光向量到Y轴的投影 float lengthOfLightDirectionInY = dot(world2ReceiverRow1,lightDirection); if (distanceOfVertex > 0.0 && lengthOfLightDirectionInY < 0.0){ //根据相似三角形定理计算出顶点延伸长度 lightDirection = lightDirection * (distanceOfVertex / (-lengthOfLightDirectionInY)); } else { lightDirection = float4(0,0); } return mul(UNITY_MATRIX_VP,vertexInWorldspace + lightDirection); } float4 frag(void) : COLOR{ return _ShadowColor; } ENDCG } } }
参考自:https://en.wikibooks.org/wiki/GLSL_Programming/Unity/Shadows_on_Planes
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。