A game about forced loneliness, made by TACStudios
1#ifndef UNIVERSAL_PARALLAX_MAPPING_INCLUDED
2#define UNIVERSAL_PARALLAX_MAPPING_INCLUDED
3
4// Return view direction in tangent space, make sure tangentWS.w is already multiplied by GetOddNegativeScale()
5half3 GetViewDirectionTangentSpace(half4 tangentWS, half3 normalWS, half3 viewDirWS)
6{
7 // must use interpolated tangent, bitangent and normal before they are normalized in the pixel shader.
8 half3 unnormalizedNormalWS = normalWS;
9 const half renormFactor = 1.0 / length(unnormalizedNormalWS);
10
11 // use bitangent on the fly like in hdrp
12 // IMPORTANT! If we ever support Flip on double sided materials ensure bitangent and tangent are NOT flipped.
13 half crossSign = (tangentWS.w > 0.0 ? 1.0 : -1.0); // we do not need to multiple GetOddNegativeScale() here, as it is done in vertex shader
14 half3 bitang = crossSign * cross(normalWS.xyz, tangentWS.xyz);
15
16 half3 WorldSpaceNormal = renormFactor * normalWS.xyz; // we want a unit length Normal Vector node in shader graph
17
18 // to preserve mikktspace compliance we use same scale renormFactor as was used on the normal.
19 // This is explained in section 2.2 in "surface gradient based bump mapping framework"
20 half3 WorldSpaceTangent = renormFactor * tangentWS.xyz;
21 half3 WorldSpaceBiTangent = renormFactor * bitang;
22
23 half3x3 tangentSpaceTransform = half3x3(WorldSpaceTangent, WorldSpaceBiTangent, WorldSpaceNormal);
24 half3 viewDirTS = mul(tangentSpaceTransform, viewDirWS);
25
26 return viewDirTS;
27}
28
29#ifndef BUILTIN_TARGET_API
30half2 ParallaxOffset1Step(half height, half amplitude, half3 viewDirTS)
31{
32 height = height * amplitude - amplitude / 2.0;
33 half3 v = normalize(viewDirTS);
34 v.z += 0.42;
35 return height * (v.xy / v.z);
36}
37#endif
38
39float2 ParallaxMapping(TEXTURE2D_PARAM(heightMap, sampler_heightMap), half3 viewDirTS, half scale, float2 uv)
40{
41 half h = SAMPLE_TEXTURE2D(heightMap, sampler_heightMap, uv).g;
42 float2 offset = ParallaxOffset1Step(h, scale, viewDirTS);
43 return offset;
44}
45
46float2 ParallaxMappingChannel(TEXTURE2D_PARAM(heightMap, sampler_heightMap), half3 viewDirTS, half scale, float2 uv, int channel)
47{
48 half h = SAMPLE_TEXTURE2D(heightMap, sampler_heightMap, uv)[channel];
49 float2 offset = ParallaxOffset1Step(h, scale, viewDirTS);
50 return offset;
51}
52#endif // UNIVERSAL_PARALLAX_MAPPING_INCLUDED