summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2024-06-29 03:17:55 -0700
committeryum <yum.food.vr@gmail.com>2024-06-29 03:17:55 -0700
commita57eca5f6f8756209018d034a12e0d75f9ee1ef5 (patch)
tree8f98a715c902a6251e79740a49cc4668f18f46fc
parent24d1a84d2b18ee771585e22ec00fa190afde3df5 (diff)
Add basic decals
Also add polar masking rim lighting. Polar masking calculates the angle of the matcap UV (theta) and evalutes this function: m_{polar} = 1 / (1 + length(theta - theta_{configured}) It then masks the rim lighting effect using m_{polar}. Quantization applies to this effect.
-rw-r--r--Editor/tooner.cs53
-rw-r--r--feature_macros.cginc6
-rw-r--r--globals.cginc31
-rw-r--r--tooner.shader22
-rw-r--r--tooner_lighting.cginc54
5 files changed, 161 insertions, 5 deletions
diff --git a/Editor/tooner.cs b/Editor/tooner.cs
index 7063972..a484bc8 100644
--- a/Editor/tooner.cs
+++ b/Editor/tooner.cs
@@ -208,7 +208,35 @@ public class ToonerGUI : ShaderGUI {
}
}
- void DoCubemap() {
+ void DoDecal() {
+ for (int i = 0; i < 4; i++) {
+ GUILayout.Label($"Decal {i}", EditorStyles.boldLabel);
+ EditorGUI.indentLevel += 1;
+
+ MaterialProperty bc = FindProperty($"_Decal{i}_Enable");
+ bool enabled = bc.floatValue > 1E-6;
+ EditorGUI.BeginChangeCheck();
+ enabled = EditorGUILayout.Toggle("Enable", enabled);
+ EditorGUI.EndChangeCheck();
+ bc.floatValue = enabled ? 1.0f : 0.0f;
+ SetKeyword($"_DECAL{i}", enabled);
+
+ if (enabled) {
+ bc = FindProperty($"_Decal{i}_BaseColor");
+ editor.TexturePropertySingleLine(
+ MakeLabel(bc, "Base color (RGBA)"),
+ bc);
+ if (bc.textureValue) {
+ editor.TextureScaleOffsetProperty(bc);
+ }
+ bc = FindProperty($"_Decal{i}_Emission_Strength");
+ editor.FloatProperty(
+ bc,
+ "Emission strength");
+ }
+
+ EditorGUI.indentLevel -= 1;
+ }
}
void DoBrightness() {
@@ -410,6 +438,27 @@ public class ToonerGUI : ShaderGUI {
EditorGUI.indentLevel -= 1;
}
+ bc = FindProperty($"_Rim_Lighting{i}_PolarMask_Enabled");
+ enabled = bc.floatValue > 1E-6;
+ EditorGUI.BeginChangeCheck();
+ enabled = EditorGUILayout.Toggle("Polar mask", enabled);
+ EditorGUI.EndChangeCheck();
+ bc.floatValue = enabled ? 1.0f : 0.0f;
+ SetKeyword($"_RIM_LIGHTING{i}_POLAR_MASK", enabled);
+
+ if (enabled) {
+ EditorGUI.indentLevel += 1;
+ bc = FindProperty($"_Rim_Lighting{i}_PolarMask_Theta");
+ editor.FloatProperty(
+ bc,
+ "Theta");
+ bc = FindProperty($"_Rim_Lighting{i}_PolarMask_Power");
+ editor.FloatProperty(
+ bc,
+ "Power");
+ EditorGUI.indentLevel -= 1;
+ }
+
EditorGUI.indentLevel -= 1;
}
}
@@ -961,6 +1010,8 @@ public class ToonerGUI : ShaderGUI {
DoPBROverlay();
+ DoDecal();
+
GUILayout.Label("Lighting", EditorStyles.boldLabel);
EditorGUI.indentLevel += 1;
DoLighting();
diff --git a/feature_macros.cginc b/feature_macros.cginc
index 231cd5d..885ffad 100644
--- a/feature_macros.cginc
+++ b/feature_macros.cginc
@@ -24,9 +24,11 @@
#pragma shader_feature_local _ _RIM_LIGHTING0
#pragma shader_feature_local _ _RIM_LIGHTING0_MASK
#pragma shader_feature_local _ _RIM_LIGHTING0_GLITTER
+#pragma shader_feature_local _ _RIM_LIGHTING0_POLAR_MASK
#pragma shader_feature_local _ _RIM_LIGHTING1
#pragma shader_feature_local _ _RIM_LIGHTING1_MASK
#pragma shader_feature_local _ _RIM_LIGHTING1_GLITTER
+#pragma shader_feature_local _ _RIM_LIGHTING1_POLAR_MASK
#pragma shader_feature_local _ _OKLAB
#pragma shader_feature_local _ _CLONES
#pragma shader_feature_local _ _PBR_OVERLAY0
@@ -73,6 +75,10 @@
#pragma shader_feature_local _ _PBR_OVERLAY3_MIX_ADD
#pragma shader_feature_local _ _PBR_OVERLAY3_MIX_MIN
#pragma shader_feature_local _ _PBR_OVERLAY3_MIX_MAX
+#pragma shader_feature_local _ _DECAL0
+#pragma shader_feature_local _ _DECAL1
+#pragma shader_feature_local _ _DECAL2
+#pragma shader_feature_local _ _DECAL3
#pragma shader_feature_local _ _LTCGI
#pragma shader_feature_local _ _TESSELLATION
#pragma shader_feature_local _ _MATCAP0_DISTORTION0
diff --git a/globals.cginc b/globals.cginc
index e4708c0..51f8a2f 100644
--- a/globals.cginc
+++ b/globals.cginc
@@ -112,6 +112,27 @@ texture2D _PBR_Overlay3_Mask;
float _PBR_Overlay3_Mask_Invert;
#endif
+#if defined(_DECAL0)
+texture2D _Decal0_BaseColor;
+float4 _Decal0_BaseColor_ST;
+float _Decal0_Emission_Strength;
+#endif
+#if defined(_DECAL1)
+texture2D _Decal1_BaseColor;
+float4 _Decal1_BaseColor_ST;
+float _Decal1_Emission_Strength;
+#endif
+#if defined(_DECAL2)
+texture2D _Decal2_BaseColor;
+float4 _Decal2_BaseColor_ST;
+float _Decal2_Emission_Strength;
+#endif
+#if defined(_DECAL3)
+texture2D _Decal3_BaseColor;
+float4 _Decal3_BaseColor_ST;
+float _Decal3_Emission_Strength;
+#endif
+
#if defined(_EMISSION)
texture2D _EmissionTex;
float _EmissionStrength;
@@ -201,6 +222,11 @@ float _Rim_Lighting0_Glitter_Amount;
float _Rim_Lighting0_Glitter_Speed;
float _Rim_Lighting0_Glitter_Quantization;
#endif
+#if defined(_RIM_LIGHTING0_POLAR_MASK)
+float _Rim_Lighting0_PolarMask_Enabled;
+float _Rim_Lighting0_PolarMask_Theta;
+float _Rim_Lighting0_PolarMask_Power;
+#endif
#endif
#if defined(_RIM_LIGHTING1)
@@ -221,6 +247,11 @@ float _Rim_Lighting1_Glitter_Amount;
float _Rim_Lighting1_Glitter_Speed;
float _Rim_Lighting1_Glitter_Quantization;
#endif
+#if defined(_RIM_LIGHTING1_POLAR_MASK)
+float _Rim_Lighting1_PolarMask_Enabled;
+float _Rim_Lighting1_PolarMask_Theta;
+float _Rim_Lighting1_PolarMask_Power;
+#endif
#endif
#if defined(_OKLAB)
diff --git a/tooner.shader b/tooner.shader
index b747945..22cbac2 100644
--- a/tooner.shader
+++ b/tooner.shader
@@ -71,6 +71,22 @@ Shader "yum_food/tooner"
_PBR_Overlay3_Mask_Invert("Mask invert", Float) = 0.0
_PBR_Overlay3_Mix("Mix mode", Float) = 0.0
+ _Decal0_Enable("Enable decal", Float) = 0.0
+ _Decal0_BaseColor("Base color", 2D) = "white" {}
+ _Decal0_Emission_Strength("Emission strength", Float) = 0
+
+ _Decal1_Enable("Enable decal", Float) = 0.0
+ _Decal1_BaseColor("Base color", 2D) = "white" {}
+ _Decal1_Emission_Strength("Emission strength", Float) = 0
+
+ _Decal2_Enable("Enable decal", Float) = 0.0
+ _Decal2_BaseColor("Base color", 2D) = "white" {}
+ _Decal2_Emission_Strength("Emission strength", Float) = 0
+
+ _Decal3_Enable("Enable decal", Float) = 0.0
+ _Decal3_BaseColor("Base color", 2D) = "white" {}
+ _Decal3_Emission_Strength("Emission strength", Float) = 0
+
[NoScaleOffset] _EmissionTex("Emission map", 2D) = "black" {}
_EmissionStrength("Emission strength", Range(0, 2)) = 0
@@ -156,6 +172,9 @@ Shader "yum_food/tooner"
_Rim_Lighting0_Glitter_Amount("Rim lighting glitter amount", Float) = 100
_Rim_Lighting0_Glitter_Speed("Rim lighting glitter speed", Float) = 1
_Rim_Lighting0_Glitter_Quantization("Rim lighting glitter quantization", Float) = 1000
+ _Rim_Lighting0_PolarMask_Enabled("Rim lighting polar mask enabled", Float) = 0
+ _Rim_Lighting0_PolarMask_Theta("Rim lighting polar mask - theta", Float) = 0
+ _Rim_Lighting0_PolarMask_Power("Rim lighting polar mask - power", Float) = 3
_Rim_Lighting1_Enabled("Enable rim lighting", Float) = 0
_Rim_Lighting1_Mode("Rim lighting mode", Float) = 0
@@ -172,6 +191,9 @@ Shader "yum_food/tooner"
_Rim_Lighting1_Glitter_Amount("Rim lighting glitter amount", Float) = 100
_Rim_Lighting1_Glitter_Speed("Rim lighting glitter speed", Float) = 1
_Rim_Lighting1_Glitter_Quantization("Rim lighting glitter quantization", Float) = 1000
+ _Rim_Lighting1_PolarMask_Enabled("Rim lighting polar mask enabled", Float) = 0
+ _Rim_Lighting1_PolarMask_Theta("Rim lighting polar mask - theta", Float) = 0
+ _Rim_Lighting1_PolarMask_Power("Rim lighting polar mask - power", Float) = 3
_OKLAB_Enabled("Enable OKLAB", Float) = 0.0
_OKLAB_Mask("Mask", 2D) = "white" {}
diff --git a/tooner_lighting.cginc b/tooner_lighting.cginc
index 1fddfca..2632250 100644
--- a/tooner_lighting.cginc
+++ b/tooner_lighting.cginc
@@ -464,6 +464,20 @@ void getOverlayAlbedo(inout PbrOverlay ov,
#endif // _PBR_OVERLAY3
}
+void applyDecalAlbedo(inout float3 albedo,
+ inout float decal_emission,
+ v2f i, float iddx, float iddy)
+{
+#if defined(_DECAL0)
+ float4 d0_c = _Decal0_BaseColor.SampleGrad(linear_repeat_s,
+ saturate((i.uv - _Decal0_BaseColor_ST.zw) * _Decal0_BaseColor_ST.xy),
+ iddx * _Decal0_BaseColor_ST.x,
+ iddy * _Decal0_BaseColor_ST.y);
+ albedo.rgb = lerp(albedo.rgb, d0_c.rgb, d0_c.a);
+ decal_emission += d0_c.rgb * _Decal0_Emission_Strength;
+#endif // _DECAL0
+}
+
void mixOverlayAlbedo(inout float3 albedo, PbrOverlay ov) {
#if defined(_PBR_OVERLAY0)
#if defined(_PBR_OVERLAY0_MIX_ALPHA_BLEND)
@@ -650,12 +664,16 @@ float4 effect(inout v2f i)
#endif
mixOverlayAlbedo(albedo.rgb, ov);
+#if defined(_DECAL0) || defined(_DECAL1) || defined(_DECAL2) || defined(_DECAL3)
+ float decal_emission = 0;
+ applyDecalAlbedo(albedo.rgb, decal_emission, i, iddx, iddy);
+#endif
#if defined(_MATCAP0) || defined(_MATCAP1) || defined(_RIM_LIGHTING0) || defined(_RIM_LIGHTING1)
float3 matcap_emission = 0;
-#endif
-
-#if defined(_MATCAP0) || defined(_MATCAP1)
+ float2 matcap_uv;
+ float matcap_theta;
+ float matcap_radius;
{
const float3 cam_normal = normalize(mul(UNITY_MATRIX_V, float4(normal, 0)));
const float3 cam_view_dir = normalize(mul(UNITY_MATRIX_V, float4(view_dir, 0)));
@@ -664,7 +682,13 @@ float4 effect(inout v2f i)
refl.x * refl.x +
refl.y * refl.y +
(refl.z + 1) * (refl.z + 1));
- float2 matcap_uv = refl.xy / m + 0.5;
+ matcap_uv = refl.xy / m + 0.5;
+ matcap_radius = length(matcap_uv - 0.5);
+ matcap_theta = atan2(matcap_uv.y - 0.5, matcap_uv.x - 0.5);
+ }
+#endif
+#if defined(_MATCAP0) || defined(_MATCAP1)
+ {
float iddx = ddx(i.uv.x);
float iddy = ddy(i.uv.y);
#if defined(_MATCAP0)
@@ -780,6 +804,7 @@ float4 effect(inout v2f i)
rl = floor(rl * q) / q;
}
float3 matcap = rl * _Rim_Lighting0_Color * _Rim_Lighting0_Strength;
+
#if defined(_RIM_LIGHTING0_MASK)
float4 matcap_mask_raw = _Rim_Lighting0_Mask.SampleGrad(linear_repeat_s, i.uv.xy, iddx, iddy);
float matcap_mask = matcap_mask_raw.r;
@@ -788,6 +813,13 @@ float4 effect(inout v2f i)
#else
float matcap_mask = 1;
#endif
+#if defined(_RIM_LIGHTING0_POLAR_MASK)
+ if (_Rim_Lighting0_PolarMask_Enabled) {
+ float pmask_theta = _Rim_Lighting0_PolarMask_Theta;
+ float pmask_pow = _Rim_Lighting0_PolarMask_Power;
+ matcap_mask *= abs(1.0 / (1.0 + pow(abs(matcap_theta - pmask_theta), pmask_pow)));;
+ }
+#endif
#if defined(_RIM_LIGHTING0_GLITTER)
float rl_glitter = get_glitter(i.uv.xy, i.worldPos, normal,
_Rim_Lighting0_Glitter_Density,
@@ -844,6 +876,17 @@ float4 effect(inout v2f i)
#else
float matcap_mask = 1;
#endif
+#if defined(_RIM_LIGHTING1_POLAR_MASK)
+ if (_Rim_Lighting1_PolarMask_Enabled) {
+ float pmask_theta = _Rim_Lighting1_PolarMask_Theta;
+ float pmask_pow = _Rim_Lighting1_PolarMask_Power;
+ float filter = abs(1.0 / (1.0 + pow(abs(matcap_theta - pmask_theta), pmask_pow)));;
+ if (q > 0) {
+ filter = floor(filter * q) / q;
+ }
+ matcap_mask *= filter;
+ }
+#endif
#if defined(_RIM_LIGHTING1_GLITTER)
float rl_glitter = get_glitter(i.uv, i.worldPos, normal,
_Rim_Lighting1_Glitter_Density,
@@ -941,6 +984,9 @@ float4 effect(inout v2f i)
#if defined(_MATCAP0) || defined(_MATCAP1) || defined(_RIM_LIGHTING0) || defined(_RIM_LIGHTING1)
result.rgb += matcap_emission;
#endif
+#if defined(_DECAL0) || defined(_DECAL1) || defined(_DECAL2) || defined(_DECAL3)
+ result.rgb += decal_emission;
+#endif
#if defined(_GLITTER)
float glitter_mask = _Glitter_Mask.SampleGrad(linear_repeat_s, i.uv, iddx, iddy);
float glitter = get_glitter(i.uv, i.worldPos, normal,