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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
| Shader "HHF/Tutorial/ParallaxMap" { Properties { [MainTexture]_BaseMap("BaseMap", 2D) = "white" {} [Normal]_NormalMap("NormalMap",2D) = "bump"{} _ParallaxMap("ParallaxMap",2D) = "white"{} [KeywordEnum(Default,Limit,Steep,Relief,POM)]_ParallaxType("ParallaxType",int)=0 _ParallaxStrength("ParallaxStrength",range(0,1)) = 1 _ParallaxAmount("ParallaxAmount",int) = 10 }
SubShader { Tags { "Queue"="Geometry" "RenderType" = "Opaque" "IgnoreProjector" = "True" "RenderPipeline" = "UniversalPipeline" }
Pass { HLSLPROGRAM #pragma prefer_hlslcc gles #pragma exclude_renderers d3d11_9x #pragma vertex vert #pragma fragment frag #pragma shader_feature _ _PARALLAXTYPE_DEFAULT _PARALLAXTYPE_LIMIT _PARALLAXTYPE_STEEP _PARALLAXTYPE_RELIEF _PARALLAXTYPE_POM #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"
struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; half3 normal : NORMAL; half4 tangent : TANGENT; };
struct v2f { float4 positionCS : SV_POSITION; float2 uv : TEXCOORD0; half4 normalWS : TEXCOORD1; half4 tangentWS : TEXCOORD2; half4 bitangentWS : TEXCOORD3; };
CBUFFER_START(UnityPerMaterial) half4 _BaseColor; float4 _BaseMap_ST; half _ParallaxStrength; half _ParallaxAmount; CBUFFER_END TEXTURE2D (_BaseMap);SAMPLER(sampler_BaseMap); TEXTURE2D (_NormalMap);SAMPLER(sampler_NormalMap); TEXTURE2D (_ParallaxMap);SAMPLER(sampler_ParallaxMap);
v2f vert(appdata v) { v2f o = (v2f)0;
float3 positionWS = TransformObjectToWorld(v.vertex.xyz); o.positionCS = TransformObjectToHClip(v.vertex.xyz); o.uv = TRANSFORM_TEX(v.uv, _BaseMap); o.normalWS.xyz = TransformObjectToWorldNormal(v.normal); o.tangentWS.xyz = TransformObjectToWorldDir(v.tangent.xyz); half sign = v.tangent.w * GetOddNegativeScale(); o.bitangentWS.xyz = cross(o.normalWS.xyz, o.tangentWS.xyz) * sign; o.tangentWS.w = positionWS.x; o.bitangentWS.w = positionWS.y; o.normalWS.w = positionWS.z; return o; }
half4 frag(v2f i) : SV_Target { half4 c;
float2 offset = 0; half3 V = normalize(float3(i.tangentWS.w,i.bitangentWS.w,i.normalWS.w) - _WorldSpaceCameraPos.xyz); V = mul(float3x3(i.tangentWS.xyz, i.bitangentWS.xyz, i.normalWS.xyz),V);
#if _PARALLAXTYPE_DEFAULT half parallaxMap = 1 - SAMPLE_TEXTURE2D(_ParallaxMap, sampler_ParallaxMap, i.uv).r; offset = V.xy * (parallaxMap/-V.z) * _ParallaxStrength; #elif _PARALLAXTYPE_LIMIT half parallaxMap = 1 - SAMPLE_TEXTURE2D(_ParallaxMap, sampler_ParallaxMap, i.uv).r; offset = V.xy * parallaxMap * _ParallaxStrength; #elif _PARALLAXTYPE_STEEP half currentDepth = 0; half parallaxDepth = 0; half heightStep = 1/_ParallaxAmount; half2 offsetTemp = V.xy/-V.z * _ParallaxStrength; for(int j=0;j<_ParallaxAmount;j++) { parallaxDepth = 1 - SAMPLE_TEXTURE2D_LOD(_ParallaxMap, sampler_ParallaxMap, i.uv + offset,0).r; if(currentDepth>parallaxDepth) break;
currentDepth += heightStep; offset = offsetTemp * currentDepth; } #elif _PARALLAXTYPE_RELIEF half currentDepth = 0; half parallaxDepth = 0; half heightStep = 1/_ParallaxAmount; half2 offsetTemp = V.xy/-V.z * _ParallaxStrength; for(int j=0;j<_ParallaxAmount;j++) { parallaxDepth = 1 - SAMPLE_TEXTURE2D_LOD(_ParallaxMap, sampler_ParallaxMap, i.uv + offset,0).r; if(currentDepth>parallaxDepth) break;
currentDepth += heightStep; offset = offsetTemp * currentDepth; }
for(int j = 0; j < 5; j++) { heightStep/=2; if(currentDepth>parallaxDepth) currentDepth -= heightStep; else currentDepth += heightStep;
offset = offsetTemp * currentDepth; parallaxDepth = 1 - SAMPLE_TEXTURE2D_LOD(_ParallaxMap, sampler_ParallaxMap, i.uv + offset,0).r; } #elif _PARALLAXTYPE_POM half currentDepth = 0; half parallaxDepth = 0; half preParallaxDepth = 0; half heightStep = 1/_ParallaxAmount; half2 offsetTemp = V.xy/-V.z * _ParallaxStrength; for(int j=0;j<_ParallaxAmount;j++) { parallaxDepth = 1 - SAMPLE_TEXTURE2D_LOD(_ParallaxMap, sampler_ParallaxMap, i.uv + offset,0).r; if(currentDepth>parallaxDepth) break;
preParallaxDepth = parallaxDepth; currentDepth += heightStep; offset = offsetTemp * currentDepth; }
half preDepth = currentDepth - heightStep; half A_C = preDepth - preParallaxDepth; half D_B = parallaxDepth - currentDepth; half t = A_C/(D_B+A_C); half height = lerp(preDepth,currentDepth,t); offset = offsetTemp * height; #endif
i.uv += offset;
half4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, i.uv); c = baseMap;
half3 normalMap = UnpackNormal(SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, i.uv)); half3 normalWS = mul(normalMap,float3x3(i.tangentWS.xyz, i.bitangentWS.xyz, i.normalWS.xyz));
Light mainLight = GetMainLight(); half3 L = mainLight.direction; half NdotL = max(0.5,dot(normalWS,L)); c *= NdotL;
return c; } ENDHLSL } }
SubShader { Tags { "Queue"="Geometry" }
Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma shader_feature _ _PARALLAXTYPE_DEFAULT _PARALLAXTYPE_LIMIT _PARALLAXTYPE_STEEP _PARALLAXTYPE_RELIEF _PARALLAXTYPE_POM #include "UnityCG.cginc"
struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; half3 normal : NORMAL; half4 tangent : TANGENT; };
struct v2f { float4 positionCS : SV_POSITION; float2 uv : TEXCOORD0; half4 normalWS : TEXCOORD1; half4 tangentWS : TEXCOORD2; half4 bitangentWS : TEXCOORD3; };
half4 _BaseColor; sampler2D _BaseMap; float4 _BaseMap_ST; sampler2D _NormalMap; sampler2D _ParallaxMap; half _ParallaxStrength; half _ParallaxAmount;
v2f vert(appdata v) { v2f o = (v2f)0;
float3 positionWS = mul(UNITY_MATRIX_M, v.vertex); o.positionCS = UnityObjectToClipPos(v.vertex.xyz); o.uv = TRANSFORM_TEX(v.uv, _BaseMap); o.normalWS.xyz = UnityObjectToWorldNormal(v.normal); o.tangentWS.xyz = UnityObjectToWorldDir(v.tangent); half sign = v.tangent.w * unity_WorldTransformParams.w; o.bitangentWS.xyz = cross(o.normalWS.xyz, o.tangentWS.xyz) * sign; o.tangentWS.w = positionWS.x; o.bitangentWS.w = positionWS.y; o.normalWS.w = positionWS.z; return o; }
half4 frag(v2f i) : SV_Target { half4 c;
float2 offset = 0; half3 V = normalize(float3(i.tangentWS.w,i.bitangentWS.w,i.normalWS.w) - _WorldSpaceCameraPos.xyz); V = mul(float3x3(i.tangentWS.xyz, i.bitangentWS.xyz, i.normalWS.xyz),V);
#if _PARALLAXTYPE_DEFAULT half parallaxMap = 1 - tex2D(_ParallaxMap, i.uv).r; offset = V.xy * (parallaxMap/-V.z) * _ParallaxStrength; #elif _PARALLAXTYPE_LIMIT half parallaxMap = 1 - tex2D(_ParallaxMap, i.uv).r; offset = V.xy * parallaxMap * _ParallaxStrength; #elif _PARALLAXTYPE_STEEP half currentDepth = 0; half parallaxDepth = 0; half heightStep = 1/_ParallaxAmount; half2 offsetTemp = V.xy/-V.z * _ParallaxStrength; for(int j=0;j<_ParallaxAmount;j++) { float4 uvMipmap = float4(i.uv + offset, 0, 0); parallaxDepth = 1 - tex2Dlod(_ParallaxMap, uvMipmap).r; if(currentDepth>parallaxDepth) break;
currentDepth += heightStep; offset = offsetTemp * currentDepth; } #elif _PARALLAXTYPE_RELIEF half currentDepth = 0; half parallaxDepth = 0; half heightStep = 1/_ParallaxAmount; half2 offsetTemp = V.xy/-V.z * _ParallaxStrength; for(int j=0;j<_ParallaxAmount;j++) { float4 uvMipmap = float4(i.uv + offset, 0, 0); parallaxDepth = 1 - tex2Dlod(_ParallaxMap, uvMipmap).r; if(currentDepth>parallaxDepth) break;
currentDepth += heightStep; offset = offsetTemp * currentDepth; }
for(int j = 0; j < 5; j++) { heightStep/=2; if(currentDepth>parallaxDepth) currentDepth -= heightStep; else currentDepth += heightStep;
offset = offsetTemp * currentDepth; parallaxDepth = 1 - tex2D(_ParallaxMap, i.uv + offset).r; } #elif _PARALLAXTYPE_POM half currentDepth = 0; half parallaxDepth = 0; half preParallaxDepth = 0; half heightStep = 1/_ParallaxAmount; half2 offsetTemp = V.xy/-V.z * _ParallaxStrength; for(int j=0;j<_ParallaxAmount;j++) { float4 uvMipmap = float4(i.uv + offset, 0, 0); parallaxDepth = 1 - tex2Dlod(_ParallaxMap, uvMipmap).r; if(currentDepth>parallaxDepth) break;
preParallaxDepth = parallaxDepth; currentDepth += heightStep; offset = offsetTemp * currentDepth; }
half preDepth = currentDepth - heightStep; half A_C = preDepth - preParallaxDepth; half D_B = parallaxDepth - currentDepth; half t = A_C/(D_B+A_C); half height = lerp(preDepth,currentDepth,t); offset = offsetTemp * height; #endif
i.uv += offset;
half4 baseMap = tex2D(_BaseMap, i.uv); c = baseMap;
half3 normalMap = UnpackNormal(tex2D(_NormalMap, i.uv)); half3 normalWS = mul(normalMap,float3x3(i.tangentWS.xyz, i.bitangentWS.xyz, i.normalWS.xyz));
half3 L = _WorldSpaceLightPos0; half NdotL = max(0.5,dot(normalWS,L)); c *= NdotL;
return c; }
ENDCG } } }
|