#ifndef __DECAL_INC #define __DECAL_INC #include "globals.cginc" #include "pbr.cginc" #include "interpolators.cginc" #include "texture_utils.cginc" float4 decal_sample(texture2D tex, float2 uv, int uv_mode) { [forcecase] switch (uv_mode) { case DECAL_UV_MODE_REPEAT: return tex.Sample(aniso4_trilinear_repeat_s, uv); case DECAL_UV_MODE_MIRROR: return tex.Sample(aniso4_trilinear_mirror_s, uv); case DECAL_UV_MODE_CLAMP: return tex.Sample(aniso4_trilinear_clamp_s, uv); default: return 0; } } float2 decal_rotate(float2 uv, float rotation) { float s, c; sincos(rotation * TAU, s, c); float2 d = uv - 0.5; return float2(d.x * c - d.y * s, d.x * s + d.y * c) + 0.5; } void applyDecals(v2f i, inout Pbr pbr) { #if defined(_DECAL0) { float2 uv = get_uv_by_channel(i, _Decal0_UV_Channel); uv -= _Decal0_MainTex_ST.zw; uv *= _Decal0_MainTex_ST.xy; #if defined(_DECAL0_ROTATION) uv = decal_rotate(uv, _Decal0_Rotation); #endif float4 albedo = decal_sample(_Decal0_MainTex, uv, _Decal0_UV_Mode); albedo *= _Decal0_Color; albedo.a *= _Decal0_Opacity; #if defined(_DECAL0_MASK) float2 mask_uv = get_uv_by_channel(i, _Decal0_Mask_UV_Channel); mask_uv -= _Decal0_Mask_ST.zw; mask_uv *= _Decal0_Mask_ST.xy; // For now, mask gets the same sampling mode as the decal. float mask = decal_sample(_Decal0_Mask, mask_uv, _Decal0_UV_Mode); #if defined(_DECAL0_MASK_INVERT) mask = 1 - mask; #endif // _DECAL0_MASK_INVERT albedo.a *= mask; #endif // _DECAL0_MASK #if defined(_DECAL0_ALBEDO_CLAMP) albedo.rgb = saturate(albedo.rgb); #endif // _DECAL0_ALBEDO_CLAMP [forcecase] switch (_Decal0_Mix_Mode) { case DECAL_MIX_MODE_ALPHA_BLEND: pbr.albedo = alpha_blend(albedo, pbr.albedo); break; case DECAL_MIX_MODE_MULTIPLY: pbr.albedo.rgb *= lerp(1, albedo.rgb, albedo.a); break; case DECAL_MIX_MODE_ADD_PRODUCT: pbr.albedo.rgb += lerp(0, albedo.rgb * pbr.albedo.rgb, albedo.a); break; } #if defined(_DECAL0_METALLIC_GLOSS) float4 mg = decal_sample(_Decal0_Metallic_Gloss, uv, _Decal0_UV_Mode); pbr.metallic = lerp(pbr.metallic, mg.r, albedo.a); pbr.smoothness = lerp(pbr.smoothness, mg.a, albedo.a); pbr.roughness_perceptual = clamp(1 - pbr.smoothness, MIN_PERCEPTUAL_ROUGHNESS, 1); pbr.roughness = clamp(pbr.roughness_perceptual * pbr.roughness_perceptual, MIN_ROUGHNESS, 1); #endif // _DECAL0_METALLIC_GLOSS } #endif #if defined(_DECAL1) { float2 uv = get_uv_by_channel(i, _Decal1_UV_Channel); uv -= _Decal1_MainTex_ST.zw; uv *= _Decal1_MainTex_ST.xy; #if defined(_DECAL1_ROTATION) uv = decal_rotate(uv, _Decal1_Rotation); #endif float4 albedo = decal_sample(_Decal1_MainTex, uv, _Decal1_UV_Mode); albedo *= _Decal1_Color; albedo.a *= _Decal1_Opacity; #if defined(_DECAL1_MASK) float2 mask_uv = get_uv_by_channel(i, _Decal1_Mask_UV_Channel); mask_uv -= _Decal1_Mask_ST.zw; mask_uv *= _Decal1_Mask_ST.xy; float mask = decal_sample(_Decal1_Mask, mask_uv, _Decal1_UV_Mode); #if defined(_DECAL1_MASK_INVERT) mask = 1 - mask; #endif // _DECAL1_MASK_INVERT albedo.a *= mask; #endif // _DECAL1_MASK #if defined(_DECAL1_ALBEDO_CLAMP) albedo.rgb = saturate(albedo.rgb); #endif // _DECAL1_ALBEDO_CLAMP [forcecase] switch (_Decal1_Mix_Mode) { case DECAL_MIX_MODE_ALPHA_BLEND: pbr.albedo = alpha_blend(albedo, pbr.albedo); break; case DECAL_MIX_MODE_MULTIPLY: pbr.albedo.rgb *= lerp(1, albedo.rgb, albedo.a); break; case DECAL_MIX_MODE_ADD_PRODUCT: pbr.albedo.rgb += lerp(0, albedo.rgb * pbr.albedo.rgb, albedo.a); break; } #if defined(_DECAL1_METALLIC_GLOSS) float4 mg = decal_sample(_Decal1_Metallic_Gloss, uv, _Decal1_UV_Mode); pbr.metallic = lerp(pbr.metallic, mg.r, albedo.a); pbr.smoothness = lerp(pbr.smoothness, mg.a, albedo.a); pbr.roughness_perceptual = clamp(1 - pbr.smoothness, MIN_PERCEPTUAL_ROUGHNESS, 1); pbr.roughness = clamp(pbr.roughness_perceptual * pbr.roughness_perceptual, MIN_ROUGHNESS, 1); #endif // _DECAL1_METALLIC_GLOSS } #endif #if defined(_DECAL2) { float2 uv = get_uv_by_channel(i, _Decal2_UV_Channel); uv -= _Decal2_MainTex_ST.zw; uv *= _Decal2_MainTex_ST.xy; #if defined(_DECAL2_ROTATION) uv = decal_rotate(uv, _Decal2_Rotation); #endif float4 albedo = decal_sample(_Decal2_MainTex, uv, _Decal2_UV_Mode); albedo *= _Decal2_Color; albedo.a *= _Decal2_Opacity; #if defined(_DECAL2_MASK) float2 mask_uv = get_uv_by_channel(i, _Decal2_Mask_UV_Channel); mask_uv -= _Decal2_Mask_ST.zw; mask_uv *= _Decal2_Mask_ST.xy; float mask = decal_sample(_Decal2_Mask, mask_uv, _Decal2_UV_Mode); #if defined(_DECAL2_MASK_INVERT) mask = 1 - mask; #endif // _DECAL2_MASK_INVERT albedo.a *= mask; #endif // _DECAL2_MASK #if defined(_DECAL2_ALBEDO_CLAMP) albedo.rgb = saturate(albedo.rgb); #endif // _DECAL2_ALBEDO_CLAMP [forcecase] switch (_Decal2_Mix_Mode) { case DECAL_MIX_MODE_ALPHA_BLEND: pbr.albedo = alpha_blend(albedo, pbr.albedo); break; case DECAL_MIX_MODE_MULTIPLY: pbr.albedo.rgb *= lerp(1, albedo.rgb, albedo.a); break; case DECAL_MIX_MODE_ADD_PRODUCT: pbr.albedo.rgb += lerp(0, albedo.rgb * pbr.albedo.rgb, albedo.a); break; } #if defined(_DECAL2_METALLIC_GLOSS) float4 mg = decal_sample(_Decal2_Metallic_Gloss, uv, _Decal2_UV_Mode); pbr.metallic = lerp(pbr.metallic, mg.r, albedo.a); pbr.smoothness = lerp(pbr.smoothness, mg.a, albedo.a); pbr.roughness_perceptual = clamp(1 - pbr.smoothness, MIN_PERCEPTUAL_ROUGHNESS, 1); pbr.roughness = clamp(pbr.roughness_perceptual * pbr.roughness_perceptual, MIN_ROUGHNESS, 1); #endif // _DECAL2_METALLIC_GLOSS } #endif #if defined(_DECAL3) { float2 uv = get_uv_by_channel(i, _Decal3_UV_Channel); uv -= _Decal3_MainTex_ST.zw; uv *= _Decal3_MainTex_ST.xy; #if defined(_DECAL3_ROTATION) uv = decal_rotate(uv, _Decal3_Rotation); #endif float4 albedo = decal_sample(_Decal3_MainTex, uv, _Decal3_UV_Mode); albedo *= _Decal3_Color; albedo.a *= _Decal3_Opacity; #if defined(_DECAL3_MASK) float2 mask_uv = get_uv_by_channel(i, _Decal3_Mask_UV_Channel); mask_uv -= _Decal3_Mask_ST.zw; mask_uv *= _Decal3_Mask_ST.xy; float mask = decal_sample(_Decal3_Mask, mask_uv, _Decal3_UV_Mode); #if defined(_DECAL3_MASK_INVERT) mask = 1 - mask; #endif // _DECAL3_MASK_INVERT albedo.a *= mask; #endif // _DECAL3_MASK #if defined(_DECAL3_ALBEDO_CLAMP) albedo.rgb = saturate(albedo.rgb); #endif // _DECAL3_ALBEDO_CLAMP [forcecase] switch (_Decal3_Mix_Mode) { case DECAL_MIX_MODE_ALPHA_BLEND: pbr.albedo = alpha_blend(albedo, pbr.albedo); break; case DECAL_MIX_MODE_MULTIPLY: pbr.albedo.rgb *= lerp(1, albedo.rgb, albedo.a); break; case DECAL_MIX_MODE_ADD_PRODUCT: pbr.albedo.rgb += lerp(0, albedo.rgb * pbr.albedo.rgb, albedo.a); break; } #if defined(_DECAL3_METALLIC_GLOSS) float4 mg = decal_sample(_Decal3_Metallic_Gloss, uv, _Decal3_UV_Mode); pbr.metallic = lerp(pbr.metallic, mg.r, albedo.a); pbr.smoothness = lerp(pbr.smoothness, mg.a, albedo.a); pbr.roughness_perceptual = clamp(1 - pbr.smoothness, MIN_PERCEPTUAL_ROUGHNESS, 1); pbr.roughness = clamp(pbr.roughness_perceptual * pbr.roughness_perceptual, MIN_ROUGHNESS, 1); #endif // _DECAL3_METALLIC_GLOSS } #endif } #endif // __DECAL_INC