Shader "Custom/Chapter11-Billboard" {
Properties
{
_MainTex("Main Tex",2D) = "White"{}
_Color("Color Tint",Color) = (1,1,1,1)
_VerticalBillboarding("Vertical Restraints",Range(0,1)) = 1
}
SubShader
{
Tags {"Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" "DisableBatching" = "True"}
pass
{
Tags {"LightMode" = "ForwardBase"}
ZWrite off
Blend SrcAlpha OneMinusSrcAlpha
Cull off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
#include "unityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Color;
fixed _VerticalBillboarding;
struct a2v
{
float4 vertex :POSITION;
float4 texcoord:TEXCOORD0;
};
struct v2f
{
float4 pos:SV_POSITION;
float2 uv:TEXCOORD0;
//V2F_SHADOW_CASTER;
};
v2f vert(a2v v)
{
v2f o;
float3 center = float3(0,0,0);//设置广告牌的锚点
float3 viewer =mul(_World2Object,float4(_WorldSpaceCameraPos,1));//获取模型空间下的视角坐标
float3 normalDir = viewer-center;//根据视角以及锚点的位置,计算目标法线方向
float3 upDir = abs(normalDir.y>0.999?float3(0,0,1):float3(0,1,0));//计算向上的方向,前提是保证其不与法线方向平行,
float3 rightDir = normalize(cross(upDir,normalDir));//根据向上方向和法线方向得到向右的方向
upDir = normalize(cross(normalDir,rightDir));//根据向右方向和法线方向得到向上防线
float3 centerOffs = v.vertex.xyz-center;//计算当前顶点与锚点的偏移量
//更新模型坐标
float3 localPos = center+rightDir*centerOffs.x+upDir*centerOffs.y+normalDir*centerOffs.z;
//模型空间坐标转换到剪裁空间下的坐标
o.pos = mul(UNITY_MATRIX_MVP,float4(localPos,1));
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
//TRANSFER_SHADOW_CASTER_NORMALOFFSET(o);
return o;
}
fixed4 frag(v2f i):SV_TARGET
{
fixed4 c = tex2D(_MainTex,i.uv);
c.rgb*=_Color.rgb;
//SHADOW_CASTER_FRAGMENT(i);
return c;
}
ENDCG
}
Pass {
Tags { "LightMode" = "ShadowCaster" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_shadowcaster
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Color;
fixed _VerticalBillboarding;
struct v2f {
float4 pos:SV_POSITION;
float2 uv:TEXCOORD0;
};
v2f vert(appdata_base v) {
v2f o;
float4 offset;
o.uv=TRANSFORM_TEX(v.texcoord,_MainTex);
float3 center = float3(0,0,0);//设置广告牌的锚点
float3 viewer =mul(_World2Object,float4(_WorldSpaceCameraPos,1));//获取模型空间下的视角坐标
float3 normalDir = viewer-center;//根据视角以及锚点的位置,计算目标法线方向
float3 upDir = abs(normalDir.y>0.999?float3(0,0,1):float3(0,1,0));//计算向上的方向,前提是保证其不与法线方向平行,
float3 rightDir = normalize(cross(upDir,normalDir));//根据向上方向和法线方向得到向右的方向
upDir = normalize(cross(normalDir,rightDir));//根据向右方向和法线方向得到向上防线
float3 centerOffs = v.vertex.xyz-center;//计算当前顶点与锚点的偏移量
//更新模型坐标
float3 localPos = center+rightDir*centerOffs.x+upDir*centerOffs.y+normalDir*centerOffs.z;
//模型空间坐标转换到剪裁空间下的坐标
//TRANSFER_SHADOW_CASTER_NORMALOFFSET(o);
o.pos = mul(UNITY_MATRIX_MVP,float4(localPos,1));
TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed4 tex=tex2D(_MainTex,i.uv);
if(tex.a<0.5){
discard;
}
SHADOW_CASTER_FRAGMENT(i);
}
ENDCG
}
}
Fallback "VertexLit"
}
Unity顶点动画的阴影实现
最新推荐文章于 2024-07-27 10:20:52 发布