- 流动的河流
Shader "Unity Shaders Book/Chapter 11/Water" {
Properties {
_MainTex ("Main Tex", 2D) = "white" {} //河流纹理
_Color ("Color Tint", Color) = (1, 1, 1, 1) //整体颜色
_Magnitude ("Distortion Magnitude", Float) = 1 //水流波动幅度
_Frequency ("Distortion Frequency", Float) = 1 //波动频率
_InvWaveLength ("Distortion Inverse Wave Length", Float) = 10 //波长的倒数
_Speed ("Speed", Float) = 0.5 //河流纹理的移动速度
}
SubShader {
// Need to disable batching because of the vertex animation
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 "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Color;
float _Magnitude;
float _Frequency;
float _InvWaveLength;
float _Speed;
struct a2v {
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
v2f vert(a2v v) {
v2f o;
float4 offset;
offset.yzw = float3(0.0, 0.0, 0.0);
offset.x = sin(_Frequency * _Time.y + v.vertex.x * _InvWaveLength + v.vertex.y * _InvWaveLength + v.vertex.z * _InvWaveLength) * _Magnitude;
o.pos = UnityObjectToClipPos(v.vertex + offset);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
o.uv += float2(0.0, _Time.y * _Speed);
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed4 c = tex2D(_MainTex, i.uv);
c.rgb *= _Color.rgb;
return c;
}
ENDCG
}
}
FallBack "Transparent/VertexLit"
}
在上面的设置中,我们除了为透明效果设置Queue、IgnoreProjector 和RenderType外,还设置了一个新的标签一DisableBatching。该标签的含义:一些SubShader在使用Unity的批处理功能时会出现问题,这时可以通过该标签来直接指明是否对该SubShader使用批处理。而这些需要特殊处理的Shader通常就是指包含了模型空间的顶点动画的Shader。这是因为,批处理会合并所有相关的模型,而这些模型各自的模型空间就会丢失。而在本例中,我们需要在物体的模型空间下对顶点位置进行偏移。因此,在这里需要取消对该Shader的批处理操作。
我们首先计算顶点位移量。我们只希望对顶点的x方向进行位移,因此yzw的位移量被设置为0。然后,我们利用_ Frequency 属性和内置的_Time.y 变量来控制正弦函数的频率。为了让不同位置具有不同的位移,我们对,上述结果加上了模型空间下的位置分量,并乘以InvWaveLength 来控制波长。最后,我们对结果值乘以_ Magnitude 属性来控制波动幅度,得到最终的位移。剩下的工作,我们只需要把位移量添加到顶点位置上,再进行正常的顶点变换即可。在上面的代码中,我们还进行了纹理动画,即使用_Time.y 和Speed 来控制在水平方向上的纹理动画。
河流动
- 广告牌
广告牌技术会根据视角方向来旋转一个被纹理着色的多边形(通常就是简单的四边形,这个多边形就是广告牌),使得多边形看起来好像总是面对着摄像机。广告牌技术被用于很多应用,比如渲染烟雾、云朵、闪光效果等。广告牌技术的本质就是构建旋转矩阵,而我们知道一个变换矩阵需要3个基向量。广告牌技术使用的基向量通常就是表面法线(normal)、指向上的方向(up)以及指向右的方向(right)。除此之外,我们还需要指定一个锚点(anchor location),这个锚点在旋转过程中是固定不变的,以此来确定多边形在空间中的位置。
Shader "Unity Shaders Book/Chapter 11/Billboard" {
Properties {
_MainTex ("Main Tex", 2D) = "white" {} //材质
_Color ("Color Tint", Color) = (1, 1, 1, 1) //整体颜色
_VerticalBillboarding ("Vertical Restraints", Range(0, 1)) = 1 //用于调整是固定法线还是固定指向上的方向,即约束垂直方向的程度。
}
SubShader {
// Need to disable batching because of the vertex animation
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"
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 vert (a2v v) {
v2f o;
//选择模型空间的原点作为广告牌的锚点,并利用内置变量获取模型空间下的视角位置
float3 center = float3(0, 0, 0);
float3 viewer = mul(unity_WorldToObject,float4(_WorldSpaceCameraPos, 1));
//计算目标发现方向
float3 normalDir = viewer - center;
//当_VerticalBillboarding为1时,意味着法线方向固定为视角方向;当_VericalBillboarding为0时,意味着向上方向固定为(0, 1,0)。 最后需要对计算得到的法线方向进行归一化操作来得到单位矢量。
normalDir.y =normalDir.y * _VerticalBillboarding;
normalDir = normalize(normalDir);
//为了防止法线方向和向上方向平行(如果平行,那么叉积得到的结果将是错误的),我们对法线方向的y分量进行判断,以得到合适的向上方向,但此时向上方向不准确
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 = UnityObjectToClipPos(float4(localPos, 1));
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target {
fixed4 c = tex2D (_MainTex, i.uv);
c.rgb *= _Color.rgb;
return c;
}
ENDCG
}
}
FallBack "Transparent/VertexLit"
}
DisableBatching设置为True原理同上。
需要说明的是,在上面的例子中,我们使用的是Unity自带的四边形(Quad) 来作为广告牌,而不能使用自带的平面(Plane)。 这是因为,我们的代码是建立在一一个竖直摆放的多边形的基础上的,也就是说,这个多边形的顶点结构需要满足在模型空间下是竖直排列的。只有这样,我们才能使用v.vertex来计算得到正确的相对于中心的位置偏移量。