您的位置:首页 > 移动开发 > Unity3D

Unity里vertexShader里压扁模型来实现比较low的阴影

2016-01-30 16:59 513 查看
只有阴影pass,请自行合并,需要指定高度,忽略深度检测,需要控制好排序,或者去掉忽略,视情况而定,最后我觉得还是shadowmap好

Shader "Custom/MeshShadow"
{
Properties
{
_ShadowOffset("ShadowOffset",vector) = (0,0,0,0)
_ShadowHeight("ShadowHeight",float) = 0
}
SubShader
{
Tags { "RenderType"="Opaque"  "Queue"="Transparent"}
Pass
{
Name "MeshShadow"
Tags {"LightMode" = "Always"}

Blend One OneMinusSrcAlpha
ZWrite Off
ZTest Always

Stencil
{
Ref 2
Comp NotEqual
Pass Replace
}

CGPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag

float4 _ShadowOffset;
float _ShadowHeight;

struct v2f
{
float4 pos : POSITION;
float4 texcoord : TEXCOORD0;
};

v2f vert ( appdata_base v )
{
v2f o;
float4x4 _RotMatrix = _Object2World;
_RotMatrix[0][3] = 0;
_RotMatrix[1][3] = 0;
_RotMatrix[2][3] = 0;
float3 tempPos = float3(_Object2World[0][3],_Object2World[1][3],_Object2World[2][3]);

float4 vertexPos = mul(_RotMatrix,v.vertex);
vertexPos.x += _ShadowOffset.x * vertexPos.y + _ShadowOffset.x;
vertexPos.z += _ShadowOffset.y * vertexPos.y + _ShadowOffset.y;
vertexPos.xyz += tempPos;
vertexPos.y = _ShadowHeight;

o.pos = mul(UNITY_MATRIX_VP, vertexPos);
o.texcoord = v.texcoord;
return o;
}

fixed4 frag(v2f i) :COLOR
{
return fixed4(0,0,0,0.3);
}
ENDCG
}
}

}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: