diff options
| author | yum <yum.food.vr@gmail.com> | 2026-04-12 19:39:39 -0700 |
|---|---|---|
| committer | yum <yum.food.vr@gmail.com> | 2026-04-12 19:39:39 -0700 |
| commit | 2791567370385c9c8d7a5aeaa64c4588346e1779 (patch) | |
| tree | 4376c9145e38b2e3c547746cf051210f7101c024 | |
| parent | 4a3c970bf665feafcd6cb211f804e89d44a9085f (diff) | |
add 2d voronoi noise
| -rwxr-xr-x | 3ner.shader | 7 | ||||
| -rwxr-xr-x | features.cginc | 4 | ||||
| -rwxr-xr-x | globals.cginc | 4 | ||||
| -rwxr-xr-x | math.cginc | 38 | ||||
| -rwxr-xr-x | pbr.cginc | 10 |
5 files changed, 59 insertions, 4 deletions
diff --git a/3ner.shader b/3ner.shader index 7c71bac..47fee80 100755 --- a/3ner.shader +++ b/3ner.shader @@ -962,6 +962,13 @@ Shader "yum_food/3ner" [HideInInspector] m_end_Aperiodic_Tiling("Aperiodic Tiling", Float) = 0 //endex + //ifex _Zebra_Enabled==0 + [HideInInspector] m_start_Zebra("Zebra", Float) = 0 + [ThryToggle(_ZEBRA)] _Zebra_Enabled("Enable", Float) = 0 + _Zebra_Scale("Scale", Float) = 1 + [HideInInspector] m_end_Zebra("Zebra", Float) = 0 + //endex + //ifex _Custom31_World_Enabled==0 [HideInInspector] m_start_Custom31_World("C31 World Gimmicks", Float) = 0 [ThryToggle(_CUSTOM31_WORLD)] _Custom31_World_Enabled("Enable", Float) = 0 diff --git a/features.cginc b/features.cginc index 793ac08..eb5d994 100755 --- a/features.cginc +++ b/features.cginc @@ -142,6 +142,10 @@ #pragma shader_feature_local _APERIODIC_TILING_NORMALS //endex +//ifex _Zebra_Enabled==0 +#pragma shader_feature_local _ZEBRA +//endex + //ifex _Custom31_World_Enabled==0 #pragma shader_feature_local _CUSTOM31_WORLD #pragma shader_feature_local _CUSTOM31_WORLD_HEXAGONS diff --git a/globals.cginc b/globals.cginc index 098988e..97b8d1d 100755 --- a/globals.cginc +++ b/globals.cginc @@ -205,6 +205,10 @@ float _Aperiodic_Tiling_Normal_Strength; float _Aperiodic_Tiling_Normal_Thickness; #endif // _APERIODIC_TILING_NORMALS +#if defined(_ZEBRA) +float _Zebra_Scale; +#endif // _ZEBRA + #if defined(_CUSTOM31_WORLD) int _Custom31_World_Ray_March_Steps; float _Custom31_World_Ray_March_Min_Dist; @@ -213,8 +213,39 @@ float3 domain_warp_3d_tex(Texture3D noise_tex, SamplerState s, float3 uvw, return noise; } -// Return distance to the nearest cell edge in a Voronoi pattern. -float voronoi_edge_distance(float3 x) { +// Return distance to the nearest voronoi cell edge, clamped to [0, 0.5]. +// 0.5 is on the edge, 0 is far from it. +float voronoi_d_2d(float2 x) { + float2 x_floor = floor(x); + float2 x_frac = frac(x); + float d1 = 1e6; + float d2 = 1e6; + float2 p1 = 0; + float2 p2 = 0; + + for (int j = -1; j <= 1; j++) { + for (int i = -1; i <= 1; i++) { + float2 cell_offset = float2(i, j); + float2 r = cell_offset + hash22_fast(x_floor + cell_offset) - x_frac; + float d = dot(r, r); + if (d < d1) { + d2 = d1; + p2 = p1; + d1 = d; + p1 = r; + } else if (d < d2) { + d2 = d; + p2 = r; + } + } + } + float d = (d2 - d1) / (2.0f * max(1e-4, length(p2 - p1))); + return max(0.0f, 0.5f - d); +} + +// Return distance to the nearest voronoi cell edge, clamped to [0, 0.5]. +// 0.5 is on the edge, 0 is far from it. +float voronoi_d_3d(float3 x) { float3 x_floor = floor(x); float3 x_frac = frac(x); float d1 = 1e6; @@ -240,7 +271,8 @@ float voronoi_edge_distance(float3 x) { } } } - return (d2 - d1) / (2.0 * max(1e-4, length(p2 - p1))); + float d = (d2 - d1) / (2.0f * max(1e-4, length(p2 - p1))); + return max(0.0f, 0.5f - d); } float median(float3 x) { @@ -162,7 +162,7 @@ void apply_kintsugi(float3 world_pos, inout float3 albedo, inout float smoothnes uvw += warp; #endif - float mask = voronoi_edge_distance(uvw) + 0.5f; + float mask = 1.0f - voronoi_d_3d(uvw); float width = max(fwidth(mask) * 0.5f, _Kintsugi_Width); float threshold = _Kintsugi_Threshold; mask = smoothstep(threshold - width, threshold + width, mask); @@ -226,6 +226,13 @@ void apply_burley_tiling(inout Pbr pbr, inout float3 normal_tangent) { #endif // _BURLEY_TILING } +void apply_zebra(float2 uv, inout float3 albedo) { +#if defined(_ZEBRA) + float mask = 2.0f * voronoi_d_2d(uv * _Zebra_Scale); + albedo = lerp(albedo, 0, mask); +#endif +} + Pbr getPbr(v2f i) { Pbr pbr = (Pbr) 0; @@ -288,6 +295,7 @@ Pbr getPbr(v2f i) { apply_triplanar_layers(i.worldPos, i.normal, pbr, normal_tangent); apply_custom31_world(i, pbr, normal_tangent); apply_aperiodic_tiling(i.uv01.xy, pbr.albedo.xyz, pbr.smoothness, normal_tangent); + apply_zebra(i.uv01.xy, pbr.albedo.xyz); applyDecals(i, pbr, normal_tangent); |
