summaryrefslogtreecommitdiffstats
path: root/triplanar.cginc
blob: 0deb8a7e2dd534d548782ff3453b4c7be08ed240 (plain)
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
#ifndef __TRIPLANAR_INC
#define __TRIPLANAR_INC

#include "burley.cginc"
#include "globals.cginc"
#include "math.cginc"

#if defined(TRIPLANAR)
void sample_triplanar(texture2D tex, float3 uv, out float4 s0, out float4 s1, out float4 s2) {
    s0 = tex.Sample(aniso4_trilinear_repeat_s, uv.yz);
    s1 = tex.Sample(aniso4_trilinear_repeat_s, uv.xz);
    s2 = tex.Sample(aniso4_trilinear_repeat_s, uv.xy);
}

float3 triplanar_weights(float3 worldNormal, float3 uv) {
    float3 weights = abs(worldNormal);
    return weights / (weights.x + weights.y + weights.z);
}
#endif

void apply_triplanar_layers(float3 worldPos, float3 normal_world, inout Pbr pbr, inout float3 normal_tangent) {
#if defined(TRIPLANAR)
    float3 weights = triplanar_weights(normal_world, worldPos);
#if defined(_TRIPLANAR_LAYER0)
    {
        float3 uv = worldPos * _Triplanar_Layer0_Scale;
        float4 a0, a1, a2;
        sample_triplanar(_Triplanar_Layer0_MainTex, uv, a0, a1, a2);
        float3 albedo = a0 * weights.x + a1 * weights.y + a2 * weights.z;
#if defined(_TRIPLANAR_LAYER0_BURLEY)
        float3 burley_weights = burley_apply_blend_gamma(weights, _Triplanar_Layer0_Burley_Blend_Gamma);
        albedo = burley_degaussianize(
            _Triplanar_Layer0_MainTex_Burley_LUT,
            burley_apply_soft_clipping(albedo, burley_weights),
            true);
#endif  // _TRIPLANAR_LAYER0_BURLEY
        pbr.albedo.rgb = albedo;
    }
#endif  // _TRIPLANAR_LAYER0
#endif  // TRIPLANAR
}

#endif  // __TRIPLANAR_INC