转载自 冯乐乐的 《Unity Shader 入门精要》
消融效果
消融效果常见于游戏中的角色死亡、地图烧毁等效果。这这些效果中,消融往往从不同的区域开始,并向看似随机的方向扩张,最后整个物体都将消失不见。我们将学习如何在Unity中实现这种效果。效果如下图所示。
要实现上图中的效果,原理非常简单,概括来说就是噪声纹理+透明度测试。我们使用对噪声纹理采样的结果和某个控制消融程度的阈值比较,如果小于阈值,就使用clip函数把它对应的像素裁剪掉,这些部分就对应了图中被“烧毁”的区域。而镂空区域边缘的烧焦效果则是将两种颜色混合,再用pow函数处理后,与原纹理颜色混合后的效果。
我们进行如下准备工作。
1)新建一个场景,去掉天空盒子。
2)新建一个材质,新建一个Shader,赋给材质
3)构建一个包含3面墙的房间,并放置一个立方体,上步材质赋给立方体
我们修改Shader 代码。
- Shader "Unity Shaders Book/Chapter 15/Dissolve" {
- Properties {
- //控制消融程度
- _BurnAmount ("Burn Amount", Range(0.0, 1.0)) = 0.0
- //控制模拟烧焦效果时的线宽
- _LineWidth("Burn Line Width", Range(0.0, 0.2)) = 0.1
- _MainTex ("Base (RGB)", 2D) = "white" {}
- _BumpMap ("Normal Map", 2D) = "bump" {}
- //火焰边缘颜色
- _BurnFirstColor("Burn First Color", Color) = (1, 0, 0, 1)
- //火焰边缘颜色
- _BurnSecondColor("Burn Second Color", Color) = (1, 0, 0, 1)
- _BurnMap("Burn Map", 2D) = "white"{}
- }
- SubShader {
- Tags { "RenderType"="Opaque" "Queue"="Geometry"}
- Pass {
- Tags { "LightMode"="ForwardBase" }
- //关闭剔除,正面和背面都会被渲染
- Cull Off
- CGPROGRAM
- #include "Lighting.cginc"
- #include "AutoLight.cginc"
- #pragma multi_compile_fwdbase
- #pragma vertex vert
- #pragma fragment frag
- fixed _BurnAmount;
- fixed _LineWidth;
- sampler2D _MainTex;
- sampler2D _BumpMap;
- fixed4 _BurnFirstColor;
- fixed4 _BurnSecondColor;
- sampler2D _BurnMap;
- float4 _MainTex_ST;
- float4 _BumpMap_ST;
- float4 _BurnMap_ST;
- struct a2v {
- float4 vertex : POSITION;
- float3 normal : NORMAL;
- float4 tangent : TANGENT;
- float4 texcoord : TEXCOORD0;
- };
- struct v2f {
- float4 pos : SV_POSITION;
- float2 uvMainTex : TEXCOORD0;
- float2 uvBumpMap : TEXCOORD1;
- float2 uvBurnMap : TEXCOORD2;
- float3 lightDir : TEXCOORD3;
- float3 worldPos : TEXCOORD4;
- SHADOW_COORDS(5)
- };
- v2f vert(a2v v) {
- v2f o;
- o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
- o.uvMainTex = TRANSFORM_TEX(v.texcoord, _MainTex);
- o.uvBumpMap = TRANSFORM_TEX(v.texcoord, _BumpMap);
- o.uvBurnMap = TRANSFORM_TEX(v.texcoord, _BurnMap);
- TANGENT_SPACE_ROTATION;
- o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz;
- o.worldPos = mul(_Object2World, v.vertex).xyz;
- TRANSFER_SHADOW(o);
- return o;
- }
- fixed4 frag(v2f i) : SV_Target {
- //对噪声纹理采样
- fixed3 burn = tex2D(_BurnMap, i.uvBurnMap).rgb;
- //将采样结果与_BurnAmount相减后 传递给clip
- //小于0则会被剔除,不会显示到屏幕上
- clip(burn.r - _BurnAmount);
- float3 tangentLightDir = normalize(i.lightDir);
- fixed3 tangentNormal = UnpackNormal(tex2D(_BumpMap, i.uvBumpMap));
- fixed3 albedo = tex2D(_MainTex, i.uvMainTex).rgb;
- fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
- fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(tangentNormal, tangentLightDir));
- fixed t = 1 - smoothstep(0.0, _LineWidth, burn.r - _BurnAmount);
- fixed3 burnColor = lerp(_BurnFirstColor, _BurnSecondColor, t);
- burnColor = pow(burnColor, 5);
- UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
- fixed3 finalColor = lerp(ambient + diffuse * atten, burnColor, t * step(0.0001, _BurnAmount));
- return fixed4(finalColor, 1);
- }
- ENDCG
- }
- //用于投射阴影
- Pass {
- Tags { "LightMode" = "ShadowCaster" }
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #pragma multi_compile_shadowcaster
- #include "UnityCG.cginc"
- fixed _BurnAmount;
- sampler2D _BurnMap;
- float4 _BurnMap_ST;
- struct v2f {
- V2F_SHADOW_CASTER;
- float2 uvBurnMap : TEXCOORD1;
- };
- v2f vert(appdata_base v) {
- v2f o;
- TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
- o.uvBurnMap = TRANSFORM_TEX(v.texcoord, _BurnMap);
- return o;
- }
- fixed4 frag(v2f i) : SV_Target {
- fixed3 burn = tex2D(_BurnMap, i.uvBurnMap).rgb;
- clip(burn.r - _BurnAmount);
- SHADOW_CASTER_FRAGMENT(i)
- }
- ENDCG
- }
- }
- FallBack "Diffuse"
- }
在本例中,我们使用的噪声纹理如下图所示。
水波效果
在模拟实时水面的过程中,我们往往也会使用噪声纹理。此时,噪声纹理通常会用作一个高度图,以不断修改水面的法线方向。为了模拟水不断流动的效果,我们会使用和时间相关的变量来对噪声纹理进行采样,当得到法线信息后,再进行正常的反射+折射计算,得到最后的水面波动效果。
我们将使用一个由噪声纹理得到的法线贴图,实现一个包含菲涅耳发射的水面效果,如下图所示。
我们在之前介绍过如何使用反射和折射来模拟一个透明玻璃的效果。我们要使用的Shader 和之前的类似。我们使用一张立方体纹理作为环境纹理,模拟反射。为了模拟折射效果,我们使用GrabPass来获取当前屏幕的渲染纹理,并使用切线空间下的法线方向对像素的屏幕坐标进行偏移,再使用该坐标对渲染为进行屏幕采样,从而模拟近似的折射效果。水波的法线纹理是由一张噪声纹理生成而得,而且会随着时间变化不断平移,模拟波光粼粼的效果。除此之外,我们没有使用一个定值来混合反射和折射颜色,而是使用之前提到的菲涅耳系数来动态决定混合系数。我们使用如下公式来计算菲涅耳系数:
其中,v 和 n 分别对应了视角方向和法线方向。它们之间的夹角越小,fresnel 值越小,反射越弱,折射越强。菲涅耳系数还经常会用于边缘光照的计算中。
我们做如下准备工作。
1)新建一个场景,去掉天空盒子
2)新建一个材质,新建一个Shader,赋给材质
3)我们构建一个由6面墙围成的封闭房间,房间里放置了一个平面来模拟水面。上步材质赋给平面。
4)为了得到本场景适用的环境纹理,我们使用了之前实现的创建立方体纹理的脚本 (通过GameObject -> Render into Cubemap 打开编辑器窗口)来创建它,如下图所示。
然后我们开始编写Shader
- Shader "Unity Shaders Book/Chapter 15/Water Wave" {
- Properties {
- //控制水面颜色
- _Color ("Main Color", Color) = (0, 0.15, 0.115, 1)
- //水面波纹材质纹理
- _MainTex ("Base (RGB)", 2D) = "white" {}
- //由噪声纹理生成的法线纹理
- _WaveMap ("Wave Map", 2D) = "bump" {}
- //模拟反射的立方体纹理
- _Cubemap ("Environment Cubemap", Cube) = "_Skybox" {}
- _WaveXSpeed ("Wave Horizontal Speed", Range(-0.1, 0.1)) = 0.01
- _WaveYSpeed ("Wave Vertical Speed", Range(-0.1, 0.1)) = 0.01
- //控制模拟折射时图像的扭曲程度
- _Distortion ("Distortion", Range(0, 100)) = 10
- }
- SubShader {
- // We must be transparent, so other objects are drawn before this one.
- Tags { "Queue"="Transparent" "RenderType"="Opaque" }
- //使用GrabPass 来获取屏幕图像
- GrabPass { "_RefractionTex" }
- Pass {
- Tags { "LightMode"="ForwardBase" }
- CGPROGRAM
- #include "UnityCG.cginc"
- #include "Lighting.cginc"
- #pragma multi_compile_fwdbase
- #pragma vertex vert
- #pragma fragment frag
- fixed4 _Color;
- sampler2D _MainTex;
- float4 _MainTex_ST;
- sampler2D _WaveMap;
- float4 _WaveMap_ST;
- samplerCUBE _Cubemap;
- fixed _WaveXSpeed;
- fixed _WaveYSpeed;
- float _Distortion;
- sampler2D _RefractionTex;
- float4 _RefractionTex_TexelSize;
- struct a2v {
- float4 vertex : POSITION;
- float3 normal : NORMAL;
- float4 tangent : TANGENT;
- float4 texcoord : TEXCOORD0;
- };
- struct v2f {
- float4 pos : SV_POSITION;
- float4 scrPos : TEXCOORD0;
- float4 uv : TEXCOORD1;
- float4 TtoW0 : TEXCOORD2;
- float4 TtoW1 : TEXCOORD3;
- float4 TtoW2 : TEXCOORD4;
- };
- v2f vert(a2v v) {
- v2f o;
- o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
- o.scrPos = ComputeGrabScreenPos(o.pos);
- o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
- o.uv.zw = TRANSFORM_TEX(v.texcoord, _WaveMap);
- float3 worldPos = mul(_Object2World, v.vertex).xyz;
- fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
- fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
- fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;
- o.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
- o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
- o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
- return o;
- }
- fixed4 frag(v2f i) : SV_Target {
- float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w);
- fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));
- float2 speed = _Time.y * float2(_WaveXSpeed, _WaveYSpeed);
- // Get the normal in tangent space
- fixed3 bump1 = UnpackNormal(tex2D(_WaveMap, i.uv.zw + speed)).rgb;
- fixed3 bump2 = UnpackNormal(tex2D(_WaveMap, i.uv.zw - speed)).rgb;
- fixed3 bump = normalize(bump1 + bump2);
- // Compute the offset in tangent space
- float2 offset = bump.xy * _Distortion * _RefractionTex_TexelSize.xy;
- i.scrPos.xy = offset * i.scrPos.z + i.scrPos.xy;
- fixed3 refrCol = tex2D( _RefractionTex, i.scrPos.xy/i.scrPos.w).rgb;
- // Convert the normal to world space
- bump = normalize(half3(dot(i.TtoW0.xyz, bump), dot(i.TtoW1.xyz, bump), dot(i.TtoW2.xyz, bump)));
- fixed4 texColor = tex2D(_MainTex, i.uv.xy + speed);
- fixed3 reflDir = reflect(-viewDir, bump);
- fixed3 reflCol = texCUBE(_Cubemap, reflDir).rgb * texColor.rgb * _Color.rgb;
- fixed fresnel = pow(1 - saturate(dot(viewDir, bump)), 4);
- fixed3 finalColor = reflCol * fresnel + refrCol * (1 - fresnel);
- return fixed4(finalColor, 1);
- }
- ENDCG
- }
- }
- // Do not cast shadow
- FallBack Off
- }
再谈全局雾效
我们之前讲到了如何使用深度纹理来实现一种基于屏幕后处理的全局雾效。我们由深度纹理重建每个像素在世界空间系下的位置,再使用一个基于高度的公式来计算雾效的混合系数,最后使用该系数来混合雾的颜色和原屏幕颜色。这种雾效是一个基于高度的均匀雾效,即在同一个高度上,雾的浓度是相同的,如下图左图所示。然而,一些时候我们希望可以模拟一种不均匀的雾效,同时让雾不断飘动,使雾看起来更加缥缈,如下图右图所示。而这就可通过使用一张噪声纹理来实现。
实现非常简单,绝大代码和之前的一样,我们只是添加了噪声相关的参数和属性,并在Shader 的片元着色器中对高度的计算添加了噪声影响。
准备工作如下:
1)新建一个场景,去掉天空盒子
2)构建一个包含3面墙的房间,并放置两个立方体和两个球体
3)摄像机上添加脚本FogWithNoise.cs
4)新建一个Shader。
首先,我们编写FogWithNoise.cs
- public class FogWithNoise : PostEffectsBase {
- public Shader fogShader;
- private Material fogMaterial = null;
- public Material material {
- get {
- fogMaterial = CheckShaderAndCreateMaterial(fogShader, fogMaterial);
- return fogMaterial;
- }
- }
- private Camera myCamera;
- public Camera camera {
- get {
- if (myCamera == null) {
- myCamera = GetComponent<Camera>();
- }
- return myCamera;
- }
- }
- private Transform myCameraTransform;
- public Transform cameraTransform {
- get {
- if (myCameraTransform == null) {
- myCameraTransform = camera.transform;
- }
- return myCameraTransform;
- }
- }
- [Range(0.1f, 3.0f)]
- public float fogDensity = 1.0f;
- public Color fogColor = Color.white;
- public float fogStart = 0.0f;
- public float fogEnd = 2.0f;
- public Texture noiseTexture;
- [Range(-0.5f, 0.5f)]
- public float fogXSpeed = 0.1f;
- [Range(-0.5f, 0.5f)]
- public float fogYSpeed = 0.1f;
- [Range(0.0f, 3.0f)]
- public float noiseAmount = 1.0f;
- void OnEnable() {
- GetComponent<Camera>().depthTextureMode |= DepthTextureMode.Depth;
- }
- void OnRenderImage (RenderTexture src, RenderTexture dest) {
- if (material != null) {
- Matrix4x4 frustumCorners = Matrix4x4.identity;
- float fov = camera.fieldOfView;
- float near = camera.nearClipPlane;
- float aspect = camera.aspect;
- float halfHeight = near * Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad);
- Vector3 toRight = cameraTransform.right * halfHeight * aspect;
- Vector3 toTop = cameraTransform.up * halfHeight;
- Vector3 topLeft = cameraTransform.forward * near + toTop - toRight;
- float scale = topLeft.magnitude / near;
- topLeft.Normalize();
- topLeft *= scale;
- Vector3 topRight = cameraTransform.forward * near + toRight + toTop;
- topRight.Normalize();
- topRight *= scale;
- Vector3 bottomLeft = cameraTransform.forward * near - toTop - toRight;
- bottomLeft.Normalize();
- bottomLeft *= scale;
- Vector3 bottomRight = cameraTransform.forward * near + toRight - toTop;
- bottomRight.Normalize();
- bottomRight *= scale;
- frustumCorners.SetRow(0, bottomLeft);
- frustumCorners.SetRow(1, bottomRight);
- frustumCorners.SetRow(2, topRight);
- frustumCorners.SetRow(3, topLeft);
- material.SetMatrix("_FrustumCornersRay", frustumCorners);
- material.SetFloat("_FogDensity", fogDensity);
- material.SetColor("_FogColor", fogColor);
- material.SetFloat("_FogStart", fogStart);
- material.SetFloat("_FogEnd", fogEnd);
- material.SetTexture("_NoiseTex", noiseTexture);
- material.SetFloat("_FogXSpeed", fogXSpeed);
- material.SetFloat("_FogYSpeed", fogYSpeed);
- material.SetFloat("_NoiseAmount", noiseAmount);
- Graphics.Blit (src, dest, material);
- } else {
- Graphics.Blit(src, dest);
- }
- }
- }
然后,再修改Shader
- Shader "Unity Shaders Book/Chapter 15/Fog With Noise" {
- Properties {
- _MainTex ("Base (RGB)", 2D) = "white" {}
- _FogDensity ("Fog Density", Float) = 1.0
- _FogColor ("Fog Color", Color) = (1, 1, 1, 1)
- _FogStart ("Fog Start", Float) = 0.0
- _FogEnd ("Fog End", Float) = 1.0
- _NoiseTex ("Noise Texture", 2D) = "white" {}
- _FogXSpeed ("Fog Horizontal Speed", Float) = 0.1
- _FogYSpeed ("Fog Vertical Speed", Float) = 0.1
- _NoiseAmount ("Noise Amount", Float) = 1
- }
- SubShader {
- CGINCLUDE
- #include "UnityCG.cginc"
- float4x4 _FrustumCornersRay;
- sampler2D _MainTex;
- half4 _MainTex_TexelSize;
- sampler2D _CameraDepthTexture;
- half _FogDensity;
- fixed4 _FogColor;
- float _FogStart;
- float _FogEnd;
- sampler2D _NoiseTex;
- half _FogXSpeed;
- half _FogYSpeed;
- half _NoiseAmount;
- struct v2f {
- float4 pos : SV_POSITION;
- float2 uv : TEXCOORD0;
- float2 uv_depth : TEXCOORD1;
- float4 interpolatedRay : TEXCOORD2;
- };
- v2f vert(appdata_img v) {
- v2f o;
- o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
- o.uv = v.texcoord;
- o.uv_depth = v.texcoord;
- #if UNITY_UV_STARTS_AT_TOP
- if (_MainTex_TexelSize.y < 0)
- o.uv_depth.y = 1 - o.uv_depth.y;
- #endif
- int index = 0;
- if (v.texcoord.x < 0.5 && v.texcoord.y < 0.5) {
- index = 0;
- } else if (v.texcoord.x > 0.5 && v.texcoord.y < 0.5) {
- index = 1;
- } else if (v.texcoord.x > 0.5 && v.texcoord.y > 0.5) {
- index = 2;
- } else {
- index = 3;
- }
- #if UNITY_UV_STARTS_AT_TOP
- if (_MainTex_TexelSize.y < 0)
- index = 3 - index;
- #endif
- o.interpolatedRay = _FrustumCornersRay[index];
- return o;
- }
- fixed4 frag(v2f i) : SV_Target {
- float linearDepth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv_depth));
- float3 worldPos = _WorldSpaceCameraPos + linearDepth * i.interpolatedRay.xyz;
- float2 speed = _Time.y * float2(_FogXSpeed, _FogYSpeed);
- float noise = (tex2D(_NoiseTex, i.uv + speed).r - 0.5) * _NoiseAmount;
- float fogDensity = (_FogEnd - worldPos.y) / (_FogEnd - _FogStart);
- fogDensity = saturate(fogDensity * _FogDensity * (1 + noise));
- fixed4 finalColor = tex2D(_MainTex, i.uv);
- finalColor.rgb = lerp(finalColor.rgb, _FogColor.rgb, fogDensity);
- return finalColor;
- }
- ENDCG
- Pass {
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- ENDCG
- }
- }
- FallBack Off
- }