summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--3ner.shader13
-rw-r--r--features.cginc4
-rw-r--r--globals.cginc9
-rw-r--r--vertex.cginc43
-rw-r--r--vertex_deformation.slang160
5 files changed, 224 insertions, 5 deletions
diff --git a/3ner.shader b/3ner.shader
index 396dc86..982faa7 100644
--- a/3ner.shader
+++ b/3ner.shader
@@ -108,6 +108,18 @@ Shader "yum_food/3ner"
[HideInInspector] m_end_Vertex_Deformation_Sine_Waves("Sine Waves", Float) = 0
//endex
+ //ifex _Vertex_Deformation_FBM_Enabled==0
+ [HideInInspector] m_start_Vertex_Deformation_FBM("FBM", Float) = 0
+ [ThryToggle(_VERTEX_DEFORMATION_FBM)] _Vertex_Deformation_FBM_Enabled("Enable", Float) = 0
+ _Vertex_Deformation_FBM_Velocity("Velocity", Vector) = (0, 0, .01)
+ _Vertex_Deformation_FBM_Gain("Gain", Range(0,1)) = 0.5
+ _Vertex_Deformation_FBM_Lacunarity("Lacunarity", Range(1,10)) = 2
+ _Vertex_Deformation_FBM_Amplitude("Amplitude", Range(0,1)) = 1
+ _Vertex_Deformation_FBM_Scale("Scale", Float) = 1
+ _Vertex_Deformation_FBM_Octaves("Octaves", Range(1, 5.1)) = 3
+ [HideInInspector] m_end_Vertex_Deformation_FBM("FBM", Float) = 0
+ //endex
+
[HideInInspector] m_end_Vertex_Deformation("Vertex Deformation", Float) = 0
//ifex _UV_Scroll_Enabled==0
@@ -303,4 +315,3 @@ Shader "yum_food/3ner"
}
CustomEditor "Thry.ShaderEditor"
}
-
diff --git a/features.cginc b/features.cginc
index c76bb9a..cb5a1d7 100644
--- a/features.cginc
+++ b/features.cginc
@@ -44,6 +44,10 @@
#pragma shader_feature_local _VERTEX_DEFORMATION_SINE_WAVES
//endex
+//ifex _Vertex_Deformation_FBM_Enabled==0
+#pragma shader_feature_local _VERTEX_DEFORMATION_FBM
+//endex
+
//ifex _Unlit==0
#pragma shader_feature_local _UNLIT
//endex
diff --git a/globals.cginc b/globals.cginc
index 75030ac..fd865dc 100644
--- a/globals.cginc
+++ b/globals.cginc
@@ -81,6 +81,15 @@ float3 _Vertex_Deformation_Sine_Waves_k;
float3 _Vertex_Deformation_Sine_Waves_omega;
#endif // _VERTEX_DEFORMATION_SINE_WAVES
+#if defined(_VERTEX_DEFORMATION_FBM)
+float3 _Vertex_Deformation_FBM_Velocity;
+float _Vertex_Deformation_FBM_Amplitude;
+float _Vertex_Deformation_FBM_Gain;
+float _Vertex_Deformation_FBM_Lacunarity;
+float _Vertex_Deformation_FBM_Scale;
+float _Vertex_Deformation_FBM_Octaves;
+#endif // _VERTEX_DEFORMATION_FBM
+
#if defined(_UV_SCROLL)
float2 _UV_Scroll_Speed;
#endif // _UV_SCROLL
diff --git a/vertex.cginc b/vertex.cginc
index 84185c6..eba7031 100644
--- a/vertex.cginc
+++ b/vertex.cginc
@@ -44,6 +44,26 @@ void deform(inout float3 objPos) {
objPos = sine_wave(objPos.xyz, amplitude, direction, k, omega, t);
}
#endif // _VERTEX_DEFORMATION_SINE_WAVES
+#if defined(_VERTEX_DEFORMATION_FBM)
+ {
+ float t = _Time[3];
+ float amplitude = _Vertex_Deformation_FBM_Amplitude;
+ float gain = _Vertex_Deformation_FBM_Gain;
+ float lacunarity = _Vertex_Deformation_FBM_Lacunarity;
+ float scale = _Vertex_Deformation_FBM_Scale;
+ float octaves = _Vertex_Deformation_FBM_Octaves;
+ float3 velocity = _Vertex_Deformation_FBM_Velocity;
+ objPos = fbm(
+ objPos,
+ t,
+ amplitude,
+ gain,
+ lacunarity,
+ scale,
+ octaves,
+ velocity);
+ }
+#endif // _VERTEX_DEFORMATION_FBM
}
void deform_normal(float3 objPos, inout float3 objNorm, inout float3 objTan) {
@@ -83,6 +103,28 @@ void deform_normal(float3 objPos, inout float3 objNorm, inout float3 objTan) {
sine_wave_normal(objPos, objNorm, objTan, amplitude, direction, k, omega, t);
}
#endif // _VERTEX_DEFORMATION_SINE_WAVES
+#if defined(_VERTEX_DEFORMATION_FBM)
+ {
+ float t = _Time[3];
+ float amplitude = _Vertex_Deformation_FBM_Amplitude;
+ float gain = _Vertex_Deformation_FBM_Gain;
+ float lacunarity = _Vertex_Deformation_FBM_Lacunarity;
+ float scale = _Vertex_Deformation_FBM_Scale;
+ float octaves = _Vertex_Deformation_FBM_Octaves;
+ float3 velocity = _Vertex_Deformation_FBM_Velocity;
+ fbm_normal(
+ objPos,
+ objNorm,
+ objTan,
+ t,
+ amplitude,
+ gain,
+ lacunarity,
+ scale,
+ octaves,
+ velocity);
+ }
+#endif // _VERTEX_DEFORMATION_FBM
}
void propagateObjPos(inout v2f i) {
@@ -92,4 +134,3 @@ void propagateObjPos(inout v2f i) {
}
#endif // __VERTEX_INC
-
diff --git a/vertex_deformation.slang b/vertex_deformation.slang
index 354290c..42cf6b8 100644
--- a/vertex_deformation.slang
+++ b/vertex_deformation.slang
@@ -1,15 +1,26 @@
#ifndef __CUSTOM31_INC
#define __CUSTOM31_INC
-#define PI 3.14159265f
-#define PI_RCP 0.31830988f
-#define TAU (2.0f * PI)
+#define PI 3.14159265f
+#define PI_RCP 0.31830988f
+#define TAU (2.0f * PI)
+#define HALF_PI (0.5f * PI)
#define glsl_mod(x,y) (((x)-(y)*floor((x)/(y))))
// Differentiable versions of common operators.
#define dabs(x) sqrt((x) * (x) + 1e-6)
#define dlerp(x, y, t) ((x) * (1-t) + (y) * t)
+// This was derived using fourier analysis. See Scripts/approximate.py.
+#define dfrac(x) \
+ 4.997559e-01 - \
+ 3.183096e-01 * sin(6.283185e+00*(x)) - \
+ 1.591544e-01 * sin(1.256637e+01*(x)) - \
+ 1.061025e-01 * sin(1.884956e+01*(x)) - \
+ 7.957647e-02 * sin(2.513274e+01*(x))
+
+#define cubic(t) ((t) * (t) * (3.0 - 2.0 * (t)))
+#define d_cubic(t) (6.0f * (t) * (1.0f-(t)))
// Macros for transforming normal and tangent using autodiff.
// r3r3 refers to "r3 to r3 transform", aka a mapping between 3d real-valued
@@ -126,4 +137,147 @@ public void sine_wave_normal(inout float3 xyz, inout float3 normal, inout float3
R3R3_NORMALS(xyz, normal, tangent, sine_wave, amplitude, direction, k, omega, t);
}
+[Differentiable]
+float3 rand3_hash3(float3 p)
+{
+ // Improved Murmurhash3 by Squirrel Eiserloh (GDC 2017)
+ p = float3(dot(p, float3(127.1, 311.7, 74.7)),
+ dot(p, float3(269.5, 183.3, 246.1)),
+ dot(p, float3(113.5, 271.9, 124.6)));
+ return frac(sin(p) * 43758.5453123);
+}
+
+[Differentiable]
+float rand3_hash1(float3 p)
+{
+ // Improved Murmurhash3 by Squirrel Eiserloh (GDC 2017)
+ float p0 = dot(p, float3(127.1, 311.7, 74.7));
+ return frac(sin(p0) * 43758.5453123);
+}
+
+// Calculate value noise and jacobian in one shot.
+// Based on https://iquilezles.org/articles/morenoise/
+[Differentiable]
+float3 value_noise(float3 xyz, out float3 dx, out float3 dy, out float3 dz) {
+ float3 cell = floor(xyz);
+ float3 f = xyz - cell;
+
+ float ux = cubic(f.x);
+ float uy = cubic(f.y);
+ float uz = cubic(f.z);
+
+ float dux = d_cubic(f.x);
+ float duy = d_cubic(f.y);
+ float duz = d_cubic(f.z);
+
+ float3 n000 = rand3_hash3(cell + float3(0.0f, 0.0f, 0.0f));
+ float3 n001 = rand3_hash3(cell + float3(0.0f, 0.0f, 1.0f));
+ float3 n010 = rand3_hash3(cell + float3(0.0f, 1.0f, 0.0f));
+ float3 n011 = rand3_hash3(cell + float3(0.0f, 1.0f, 1.0f));
+ float3 n100 = rand3_hash3(cell + float3(1.0f, 0.0f, 0.0f));
+ float3 n101 = rand3_hash3(cell + float3(1.0f, 0.0f, 1.0f));
+ float3 n110 = rand3_hash3(cell + float3(1.0f, 1.0f, 0.0f));
+ float3 n111 = rand3_hash3(cell + float3(1.0f, 1.0f, 1.0f));
+
+ float3 n00 = lerp(n000, n001, uz);
+ float3 n01 = lerp(n010, n011, uz);
+ float3 n10 = lerp(n100, n101, uz);
+ float3 n11 = lerp(n110, n111, uz);
+
+ float3 n0 = lerp(n00, n01, uy);
+ float3 n1 = lerp(n10, n11, uy);
+
+ float oneMinusUx = 1.0f - ux;
+ float oneMinusUy = 1.0f - uy;
+
+ float3 noise_half = lerp(n0, n1, ux);
+
+ float3 dnoise_half_dx = (n1 - n0) * dux;
+
+ float3 dn0_dy = (n01 - n00) * duy;
+ float3 dn1_dy = (n11 - n10) * duy;
+ float3 dnoise_half_dy = dn0_dy * oneMinusUx + dn1_dy * ux;
+
+ float3 dn00_dz = (n001 - n000) * duz;
+ float3 dn01_dz = (n011 - n010) * duz;
+ float3 dn10_dz = (n101 - n100) * duz;
+ float3 dn11_dz = (n111 - n110) * duz;
+ float3 dn0_dz = dn00_dz * oneMinusUy + dn01_dz * uy;
+ float3 dn1_dz = dn10_dz * oneMinusUy + dn11_dz * uy;
+ float3 dnoise_half_dz = dn0_dz * oneMinusUx + dn1_dz * ux;
+
+ dx = 2.0f * dnoise_half_dx;
+ dy = 2.0f * dnoise_half_dy;
+ dz = 2.0f * dnoise_half_dz;
+
+ return -1.0f + 2.0f * noise_half;
+}
+
+float3 fbm_with_derivatives(float3 xyz,
+ no_diff float t,
+ no_diff float amplitude,
+ no_diff float gain,
+ no_diff float lacunarity,
+ no_diff float scale,
+ no_diff float octaves,
+ no_diff float3 velocity,
+ out float3 dx,
+ out float3 dy,
+ out float3 dz) {
+ float3 noise = float3(0.0f, 0.0f, 0.0f);
+ float gain_i = amplitude;
+ float freq_i = scale;
+
+ dx = float3(0.0f, 0.0f, 0.0f);
+ dy = float3(0.0f, 0.0f, 0.0f);
+ dz = float3(0.0f, 0.0f, 0.0f);
+
+ for (uint i = 0; i < octaves; ++i) {
+ float3 dx_i, dy_i, dz_i;
+ float3 octave_noise = value_noise((xyz - velocity * t) * freq_i, dx_i, dy_i, dz_i);
+ noise += gain_i * octave_noise;
+ dx += gain_i * freq_i * dx_i;
+ dy += gain_i * freq_i * dy_i;
+ dz += gain_i * freq_i * dz_i;
+ freq_i *= lacunarity;
+ gain_i *= gain;
+ }
+
+ dx += float3(1.0f, 0.0f, 0.0f);
+ dy += float3(0.0f, 1.0f, 0.0f);
+ dz += float3(0.0f, 0.0f, 1.0f);
+
+ return xyz + noise;
+}
+
+public float3 fbm(float3 xyz,
+ no_diff float t,
+ no_diff float amplitude,
+ no_diff float gain,
+ no_diff float lacunarity,
+ no_diff float scale,
+ no_diff float octaves,
+ no_diff float3 velocity) {
+ float3 dx_unused, dy_unused, dz_unused;
+ return fbm_with_derivatives(xyz, t, amplitude, gain, lacunarity, scale, octaves,
+ velocity, dx_unused, dy_unused, dz_unused);
+}
+
+public void fbm_normal(inout float3 xyz, inout float3 normal, inout float3 tangent,
+ no_diff float t,
+ no_diff float amplitude, no_diff float gain, no_diff float lacunarity,
+ no_diff float scale, no_diff float octaves, no_diff float3 velocity) {
+ float3 dx, dy, dz;
+ xyz = fbm_with_derivatives(xyz, t, amplitude, gain, lacunarity, scale, octaves,
+ velocity, dx, dy, dz);
+ float3x3 jac = float3x3(
+ dx.x, dy.x, dz.x,
+ dx.y, dy.y, dz.y,
+ dx.z, dy.z, dz.z);
+ float jac_det = determinant(jac);
+ float3x3 itjac = inverse(transpose(jac), jac_det);
+ normal = mul(itjac, normal) * sign(jac_det);
+ tangent = mul(jac, tangent);
+}
+
#endif // __CUSTOM31_INC