title: unity-shader-曲面细分与置换贴图
categories: Unity3d-Shader
tags: [unity, shader, tessellation, displacement]
date: 2019-05-22 15:37:34
comments: false
低模通过 置换贴图 增加曲面, 表现出高模效果
相比较 法线贴图 , 置换贴图 是真实改变顶点的位置, 会有遮挡, 而 法线贴图 只是在改变 片段 光照的视觉效果, 并不会修改模型的顶点位置.
前篇
- unity 官网说明 - https://docs.unity3d.com/Manual/SL-SurfaceShaderTessellation.html
- Unity默认管线置换贴图与曲面细分 (有法线解决方案) - https://www.wonderm.cc/2019/03/30/Unity-Tess-Displacement/
在细分后采样置换贴图,对顶点进行偏移
需要注意的是在 surf 和 frag 以外采样贴图只能使用 tex2Dlod
使用限制: the shader is automatically compiled into the Shader Model 4.6 target, 只能在 sm4.6 以后才能使用.
测试
-
使用的资源 Organic 2
-
shader
Shader "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 #include "UnityPBSLighting.cginc" #pragma surface surf Standard addshadow fullforwardshadows vertex:disp nolightmap #pragma target 4.6 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" }
-
效果