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" }
- 效果