summaryrefslogtreecommitdiffstats
path: root/decals.cginc
blob: c120c79a46991ef6ae2db243a1070793ff7f8db8 (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
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
#ifndef __DECALS
#define __DECALS

#include "features.cginc"
#include "globals.cginc"
#include "math.cginc"

// Struct to hold all decal parameters
struct DecalParams {
    float4 color;
    float opacity;
    float angle;
    float uv_channel;
    float4 mainTex_ST;
    int tiling_mode;
    int alpha_blend_mode;
    Texture2D mainTex;
    bool sdf_enabled;
    bool sdf_invert;
    float sdf_threshold;
    bool mask_enabled;
    Texture2D mask;
    bool normal_enabled;
    Texture2D normalTex;
    float normal_scale;
    bool reflections_enabled;
    Texture2D metallicGlossMap;
    float metallic_value;
    float smoothness_value;
};

// Macro to initialize decal parameters
#define INIT_DECAL_PARAMS(params, prefix) \
    params.color = prefix##Color; \
    params.opacity = prefix##Opacity; \
    params.angle = prefix##Angle; \
    params.uv_channel = prefix##UV_Channel; \
    params.mainTex_ST = prefix##MainTex_ST; \
    params.tiling_mode = prefix##Tiling_Mode; \
    params.alpha_blend_mode = prefix##Alpha_Blend_Mode; \
    params.mainTex = prefix##MainTex; \
    params.sdf_enabled = prefix##SDF_Enabled; \
    params.sdf_invert = prefix##SDF_Invert; \
    params.sdf_threshold = prefix##SDF_Threshold; \
    params.mask_enabled = prefix##Mask_Enabled; \
    params.mask = prefix##Mask; \
    params.normal_enabled = prefix##Normal_Enabled; \
    params.normalTex = prefix##Normal; \
    params.normal_scale = prefix##Normal_Scale; \
    params.reflections_enabled = prefix##Reflections_Enabled; \
    params.metallicGlossMap = prefix##MetallicGlossMap; \
    params.metallic_value = prefix##Metallic; \
    params.smoothness_value = prefix##Smoothness

#define APPLY_DECAL_SEC00_GENERIC(i, albedo, normal_tangent, metallic, smoothness, params)          \
    float2x2 decal_rot = float2x2(                                                                  \
        cos(params.angle * TAU), -sin(params.angle * TAU),                                          \
        sin(params.angle * TAU), cos(params.angle * TAU)                                            \
    );                                                                                              \
                                                                                                    \
    float2 raw_decal_uv = (params.uv_channel == 0) ? i.uv01.xy : i.uv01.zw;                         \
    float2 decal_uv = (raw_decal_uv * params.mainTex_ST.xy + params.mainTex_ST.zw);                 \
    decal_uv = mul(decal_rot, decal_uv);                                                            \
    decal_uv = (params.tiling_mode == DECAL_TILING_MODE_CLAMP ? saturate(decal_uv) : decal_uv);

#define APPLY_DECAL_SEC01_SDF_ON(i, albedo, normal_tangent, metallic, smoothness, params)           \
    float4 decal_albedo;                                                                            \
    {                                                                                               \
        float4 sdf_sample = params.mainTex.SampleLevel(linear_repeat_s, decal_uv, 0);               \
        float sd = sdf_sample.r;                                                                    \
        sd = params.sdf_invert ? 1 - sd : sd;                                                       \
        sd = (sd > params.sdf_threshold ? 1 : 0);                                                   \
        decal_albedo = params.color * sd;                                                           \
        decal_albedo.a *= params.opacity;                                                           \
    }

#define APPLY_DECAL_SEC01_SDF_OFF(i, albedo, normal_tangent, metallic, smoothness, params)          \
    float4 decal_albedo;                                                                            \
    {                                                                                               \
        decal_albedo = params.mainTex.Sample(linear_repeat_s, decal_uv);                            \
        decal_albedo *= params.color;                                                               \
        decal_albedo.a *= params.opacity;                                                           \
    }

#define APPLY_DECAL_SEC02_CLAMP_ON(i, albedo, normal_tangent, metallic, smoothness, params)         \
    {                                                                                               \
        float eps = 1e-4;                                                                           \
        float2 uv_clamped = step(eps, decal_uv) * step(decal_uv, 1 - eps);                          \
        decal_albedo.a *= uv_clamped.x * uv_clamped.y;                                              \
    }

#define APPLY_DECAL_SEC02_CLAMP_OFF(i, albedo, normal_tangent, metallic, smoothness, params) {}

#define APPLY_DECAL_SEC03_MASK_ON(i, albedo, normal_tangent, metallic, smoothness, params)          \
    float decal_mask = params.mask.Sample(linear_repeat_s, raw_decal_uv);                           \
    decal_albedo.a *= decal_mask;

#define APPLY_DECAL_SEC03_MASK_OFF(i, albedo, normal_tangent, metallic, smoothness, params)         \
    float decal_mask = 1;

#define APPLY_DECAL_SEC04_BLEND_MODE_ALPHA_BLEND(i, albedo, normal_tangent, metallic, smoothness, params)   \
    albedo = alphaBlend(albedo, decal_albedo);

#define APPLY_DECAL_SEC04_BLEND_MODE_REPLACE(i, albedo, normal_tangent, metallic, smoothness, params)       \
    albedo = lerp(albedo, decal_albedo, decal_mask);

#define APPLY_DECAL_SEC05_NORMAL_ON(i, albedo, normal_tangent, metallic, smoothness, params)        \
    float3 decal_normal = UnpackScaleNormal(                                                        \
            params.normalTex.Sample(linear_repeat_s, decal_uv),                                     \
            params.normal_scale * decal_albedo.a);                                                  \
    normal_tangent = lerp(normal_tangent, decal_normal, decal_albedo.a);

#define APPLY_DECAL_SEC05_NORMAL_OFF(i, albedo, normal_tangent, metallic, smoothness, params) {}

#define APPLY_DECAL_SEC06_REFLECTIONS_ON(i, albedo, normal_tangent, metallic, smoothness, params)   \
    float4 metallic_gloss = params.metallicGlossMap.Sample(linear_repeat_s, decal_uv);              \
    metallic = lerp(metallic, metallic_gloss.r * params.metallic_value, decal_albedo.a);            \
    smoothness = lerp(smoothness, metallic_gloss.a * params.smoothness_value, decal_albedo.a);

#define APPLY_DECAL_SEC06_REFLECTIONS_OFF(i, albedo, normal_tangent, metallic, smoothness, params) {}

void applyDecals(in v2f i, inout float4 albedo, inout float3 normal_tangent, inout float metallic, inout float smoothness) {
    DecalParams decal;
    #if defined(_DECAL0)
    {
        INIT_DECAL_PARAMS(decal, _Decal0_);
        APPLY_DECAL_SEC00_GENERIC(i, albedo, normal_tangent, metallic, smoothness, decal);
        #if defined(_DECAL0_SDF)
        APPLY_DECAL_SEC01_SDF_ON(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC01_SDF_OFF(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
        #if defined(_DECAL0_TILING_MODE)
        APPLY_DECAL_SEC02_CLAMP_OFF(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC02_CLAMP_ON(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
        #if defined(_DECAL0_MASK)
        APPLY_DECAL_SEC03_MASK_ON(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC03_MASK_OFF(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
        #if defined(_DECAL0_REPLACE_ALPHA)
        APPLY_DECAL_SEC04_BLEND_MODE_REPLACE(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC04_BLEND_MODE_ALPHA_BLEND(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
        #if defined(_DECAL0_NORMAL)
        APPLY_DECAL_SEC05_NORMAL_ON(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC05_NORMAL_OFF(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
        #if defined(_DECAL0_REFLECTIONS)
        APPLY_DECAL_SEC06_REFLECTIONS_ON(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC06_REFLECTIONS_OFF(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
    }
    #endif  // _DECAL0
    #if defined(_DECAL1)
    {
        INIT_DECAL_PARAMS(decal, _Decal1_);
        APPLY_DECAL_SEC00_GENERIC(i, albedo, normal_tangent, metallic, smoothness, decal);
        #if defined(_DECAL1_SDF)
        APPLY_DECAL_SEC01_SDF_ON(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC01_SDF_OFF(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
        #if defined(_DECAL1_TILING_MODE)
        APPLY_DECAL_SEC02_CLAMP_OFF(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC02_CLAMP_ON(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
        #if defined(_DECAL1_MASK)
        APPLY_DECAL_SEC03_MASK_ON(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC03_MASK_OFF(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
        #if defined(_DECAL1_REPLACE_ALPHA)
        APPLY_DECAL_SEC04_BLEND_MODE_REPLACE(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC04_BLEND_MODE_ALPHA_BLEND(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
        #if defined(_DECAL1_NORMAL)
        APPLY_DECAL_SEC05_NORMAL_ON(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC05_NORMAL_OFF(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
        #if defined(_DECAL1_REFLECTIONS)
        APPLY_DECAL_SEC06_REFLECTIONS_ON(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC06_REFLECTIONS_OFF(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
    }
    #endif  // _DECAL1
    #if defined(_DECAL2)
    {
        INIT_DECAL_PARAMS(decal, _Decal2_);
        APPLY_DECAL_SEC00_GENERIC(i, albedo, normal_tangent, metallic, smoothness, decal);
        #if defined(_DECAL2_SDF)
        APPLY_DECAL_SEC01_SDF_ON(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC01_SDF_OFF(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
        #if defined(_DECAL2_TILING_MODE)
        APPLY_DECAL_SEC02_CLAMP_OFF(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC02_CLAMP_ON(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
        #if defined(_DECAL2_MASK)
        APPLY_DECAL_SEC03_MASK_ON(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC03_MASK_OFF(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
        #if defined(_DECAL2_REPLACE_ALPHA)
        APPLY_DECAL_SEC04_BLEND_MODE_REPLACE(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC04_BLEND_MODE_ALPHA_BLEND(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
        #if defined(_DECAL2_NORMAL)
        APPLY_DECAL_SEC05_NORMAL_ON(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC05_NORMAL_OFF(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
        #if defined(_DECAL2_REFLECTIONS)
        APPLY_DECAL_SEC06_REFLECTIONS_ON(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC06_REFLECTIONS_OFF(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
    }
    #endif  // _DECAL2
    #if defined(_DECAL3)
    {
        INIT_DECAL_PARAMS(decal, _Decal3_);
        APPLY_DECAL_SEC00_GENERIC(i, albedo, normal_tangent, metallic, smoothness, decal);
        #if defined(_DECAL3_SDF)
        APPLY_DECAL_SEC01_SDF_ON(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC01_SDF_OFF(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
        #if defined(_DECAL3_TILING_MODE)
        APPLY_DECAL_SEC02_CLAMP_OFF(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC02_CLAMP_ON(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
        #if defined(_DECAL3_MASK)
        APPLY_DECAL_SEC03_MASK_ON(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC03_MASK_OFF(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
        #if defined(_DECAL3_REPLACE_ALPHA)
        APPLY_DECAL_SEC04_BLEND_MODE_REPLACE(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC04_BLEND_MODE_ALPHA_BLEND(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
        #if defined(_DECAL3_NORMAL)
        APPLY_DECAL_SEC05_NORMAL_ON(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC05_NORMAL_OFF(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
        #if defined(_DECAL3_REFLECTIONS)
        APPLY_DECAL_SEC06_REFLECTIONS_ON(i, albedo, normal_tangent, metallic, smoothness, decal);
        #else
        APPLY_DECAL_SEC06_REFLECTIONS_OFF(i, albedo, normal_tangent, metallic, smoothness, decal);
        #endif
    }
    #endif  // _DECAL3
}

#endif  // __DECALS