模型阴影
我们在实际项目中,经常会有模型影子的需求。
这个时候如果使用光照的话,在移动端性能消耗太大。
如果使用一个假的阴影片,效果又不太好。
我们希望有能够有和灯光系统一样的阴影效果。
我们通过模拟灯光的方式来实现
之前我们写的shader都是对顶点颜色的控制,也就是fragment shader的编写。
这次我们需要产生阴影,那么就要用到vector shader的编写。
我们首先写一个简单模型渲染shader
这次还是一个双pass的shader实现。
Shader "Unlit/ModelShadow" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_Color ("Color", Color) = (1,1,1,1)
}
SubShader {
Tags {
"RenderPipeline" = "UniversalPipeline"
"RenderType"="Opaque"
}
Pass {
Tags { }
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
CBUFFER_START(UnityPerMaterial)
half4 _DayColor;
float _ShadowY;
float3 _LightDir;
half4 _ShadowColor;
half _ShadowFalloff;
half _ShadowAlpha;
CBUFFER_END
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
float4 _MainTex_ST;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = TransformObjectToHClip(v.vertex.xyz);
o.texcoord =TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}
half4 frag (v2f i) : SV_Target
{
half4 col = SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex, i.texcoord);
half4 color = col*_DayColor;
return half4(color.xyz,1);
}
ENDHLSL
}
}
}
下面我们来写第二个pass实现
第二个pass主要是Vector shader的编写。
我们投影的实现,其实相当于是更改了我们模