unity-shader-曲面细分与置换贴图
低模通过 置换贴图 增加曲面, 表现出高模效果
相比较 法线贴图 , 置换贴图 是真实改变顶点的位置, 会有遮挡, 而 法线贴图 只是在改变 片段 光照的视觉效果, 并不会修改模型的顶点位置.
前篇
- unity 官网说明 - https://docs.unity3d.com/Manual/SL-SurfaceShaderTessellation.html
- Unity默认管线置换贴图与曲面细分 (有法线解决方案) - https://www.wonderm.cc/2019/03/30/Unity-Tess-Displacement/
在细分后采样置换贴图,对顶点进行偏移
需要注意的是在 surf 和 frag 以外采样贴图只能使用 tex2Dlod
使用限制
PC/Console: Shader Model 4.6 + DX11/OpenGL Core
安卓平台: OpenGL ES 3.1 + AEP(Android Extension Pack)
测试
使用的资源 Organic 2
shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58Shader "test/Tessellation" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_DispTex ("Disp Texture", 2D) = "gray" {}
_NormalMap ("Normalmap", 2D) = "bump" {}
_RoughnessMap ("RoughnessMap", 2D) = "white" {}
_AoMap ("AoMap", 2D) = "white" {}
_Displacement ("Displacement", Range(0, 1.0)) = 0.3
_Color ("Color", color) = (1,1,1,0)
_Metallic ("Metallic", Range(0, 1.0)) = 1.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 300
CGPROGRAM
struct appdata {
float4 vertex : POSITION;
float4 tangent : TANGENT;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
};
sampler2D _DispTex;
float _Displacement;
void disp (inout appdata v) {
float d = tex2Dlod(_DispTex, float4(v.texcoord.xy,0,0)).r * _Displacement;
v.vertex.xyz += v.normal * d;
}
struct Input {
float2 uv_MainTex;
};
sampler2D _MainTex;
sampler2D _NormalMap;
sampler2D _RoughnessMap;
sampler2D _AoMap;
fixed4 _Color;
float _Metallic;
void surf (Input IN, inout SurfaceOutputStandard o) {
half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Metallic = _Metallic;
o.Normal = UnpackNormal(tex2D(_NormalMap, IN.uv_MainTex));
o.Smoothness = 1 - tex2D(_RoughnessMap, IN.uv_MainTex).r;
o.Occlusion = tex2D(_AoMap, IN.uv_MainTex).r;
}
ENDCG
}
FallBack "Diffuse"
}效果