summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2025-11-01 16:25:42 -0700
committeryum <yum.food.vr@gmail.com>2025-11-01 16:25:42 -0700
commitea09e3e5425d935ed9dd277c0f1a7488033d2ede (patch)
tree8b2009c59409cde4f8e00c96e4ff1536359b9d9f
parent195df414b5310dec05d509dcc7d91b786419e360 (diff)
add l2 -> l infinity norm deformation
-rw-r--r--3ner.shader16
-rw-r--r--features.cginc8
-rw-r--r--globals.cginc9
-rw-r--r--ray_marching.cginc19
-rw-r--r--ray_marching_maps.slang23
-rw-r--r--vertex.cginc30
-rw-r--r--vertex_deformation.slang30
7 files changed, 112 insertions, 23 deletions
diff --git a/3ner.shader b/3ner.shader
index 7feb967..e3b9ae9 100644
--- a/3ner.shader
+++ b/3ner.shader
@@ -60,11 +60,18 @@ Shader "yum_food/3ner"
//ifex _Ray_Marching_Baked_Origins_Enabled==0
[HideInInspector] m_start_Ray_Marching_Baked_Origins("Baked Origins", Float) = 0
[ThryToggle(_RAY_MARCHING_BAKED_ORIGINS)] _Ray_Marching_Baked_Origins_Enabled("Enable", Float) = 0
-
[IntRange] _Baked_Origins_UV_Channel_Index("UV Channel Index", Range(0, 3)) = 1
[HideInInspector] m_end_Ray_Marching_Baked_Origins("Baked Origins", Float) = 0
//endex
+ //ifex _Ray_Marching_Ball_Grid_Enabled==0
+ [HideInInspector] m_start_Ray_Marching_Ball_Grid("Ball Grid", Float) = 0
+ [ThryToggle(_RAY_MARCHING_BALL_GRID)] _Ray_Marching_Ball_Grid_Enabled("Enable", Float) = 0
+ _Ray_Marching_Ball_Grid_Radius("Radius", Float) = 1
+ _Ray_Marching_Ball_Grid_Count("Count", Range(0,40)) = 5
+ [HideInInspector] m_end_Ray_Marching_Ball_Grid("Ball Grid", Float) = 0
+ //endex
+
[HideInInspector] m_end_Ray_Marching("Ray Marching", Float) = 0
//endex
@@ -110,6 +117,13 @@ Shader "yum_food/3ner"
[HideInInspector] m_end_Vertex_Deformation_Tubes("Tubes", Float) = 0
//endex
+ //ifex _Vertex_Deformation_L2_Linf_Enabled==0
+ [HideInInspector] m_start_Vertex_Deformation_L2_Linf("L2 -> L inf", Float) = 0
+ [ThryToggle(_VERTEX_DEFORMATION_L2_LINF)] _Vertex_Deformation_L2_Linf_Enabled("Enable", Float) = 0
+ _Vertex_Deformation_L2_Linf_t("t", Range(0,1)) = 0
+ [HideInInspector] m_end_Vertex_Deformation_L2_Linf("L2 -> L inf", Float) = 0
+ //endex
+
//ifex _Vertex_Deformation_Seal_Enabled==0
[HideInInspector] m_start_Vertex_Deformation_Seal("Seal", Float) = 0
[ThryToggle(_VERTEX_DEFORMATION_SEAL)] _Vertex_Deformation_Seal_Enabled("Enable", Float) = 0
diff --git a/features.cginc b/features.cginc
index b5c7e64..b00c86b 100644
--- a/features.cginc
+++ b/features.cginc
@@ -36,6 +36,10 @@
#pragma shader_feature_local _VERTEX_DEFORMATION_XY_TUBE
//endex
+//ifex _Vertex_Deformation_L2_Linf_Enabled==0
+#pragma shader_feature_local _VERTEX_DEFORMATION_L2_LINF
+//endex
+
//ifex _Vertex_Deformation_Seal_Enabled==0
#pragma shader_feature_local _VERTEX_DEFORMATION_SEAL
//endex
@@ -76,4 +80,8 @@
#pragma shader_feature_local _RAY_MARCHING_BAKED_ORIGINS
//endex
+//ifex _Ray_Marching_Ball_Grid_Enabled==0
+#pragma shader_feature_local _RAY_MARCHING_BALL_GRID
+//endex
+
#endif // __FEATURES_INC
diff --git a/globals.cginc b/globals.cginc
index f671b73..6513c32 100644
--- a/globals.cginc
+++ b/globals.cginc
@@ -70,6 +70,10 @@ float _Vertex_Deformation_XZ_Tube_t;
float _Vertex_Deformation_YZ_Tube_t;
#endif // _VERTEX_DEFORMATION_YZ_TUBE
+#if defined(_VERTEX_DEFORMATION_L2_LINF)
+float _Vertex_Deformation_L2_Linf_t;
+#endif // _VERTEX_DEFORMATION_L2_LINF
+
#if defined(_VERTEX_DEFORMATION_SEAL)
float _Vertex_Deformation_Seal_A;
float _Vertex_Deformation_Seal_k;
@@ -118,4 +122,9 @@ float getTime() {
int _Baked_Origins_UV_Channel_Index;
#endif // _RAY_MARCHING_BAKED_ORIGINS
+#if defined(_RAY_MARCHING_BALL_GRID)
+float _Ray_Marching_Ball_Grid_Radius;
+float _Ray_Marching_Ball_Grid_Count;
+#endif // _RAY_MARCHING_BALL_GRID
+
#endif // __GLOBALS_INC
diff --git a/ray_marching.cginc b/ray_marching.cginc
index 01011c6..513178b 100644
--- a/ray_marching.cginc
+++ b/ray_marching.cginc
@@ -29,12 +29,14 @@ RayMarchResult ray_march(v2f i) {
GetRoRd(i, ro, rd);
#if defined(_VERTEX_DEFORMATION)
+ float3 rd_perp = i.normal * dot(i.normal, rd);
+ float3 rd_tan = rd - rd_perp;
float3 tmp = ro;
- undeform_normal(tmp, tmp, rd);
- rd = normalize(rd);
+ undeform_normal(tmp, rd_perp, rd_tan);
+ rd = normalize(rd_perp + rd_tan);
#endif
- const float kMinDist = 1e-3;
+ const float kMinDist = 1e-4;
// TODO dial this in & parameterize
const float kMaxDist = 10;
// TODO parameterize
@@ -42,7 +44,11 @@ RayMarchResult ray_march(v2f i) {
float d_acc = 0;
for (uint ii = 0; ii < kMaxIter; ++ii) {
float3 p = ro + rd * d_acc;
- float d_cur = map(p);
+ float d_cur = 1e9;
+#if defined(_RAY_MARCHING_BALL_GRID)
+ float3 count = _Ray_Marching_Ball_Grid_Count;
+ d_cur = min(d_cur, map_ball_grid(p, count));
+#endif
d_acc += d_cur;
if (d_cur < kMinDist) {
break;
@@ -56,7 +62,10 @@ RayMarchResult ray_march(v2f i) {
float3 lclPos = ro + rd * d_acc;
float3 lclNorm;
float3 lclTan;
- map_normal(lclPos, lclNorm, lclTan);
+#if defined(_RAY_MARCHING_BALL_GRID)
+ float3 count = _Ray_Marching_Ball_Grid_Count;
+ map_ball_grid_normal(count, lclPos, lclNorm, lclTan);
+#endif
RayMarchResult res;
res.objPos = lclPos;
diff --git a/ray_marching_maps.slang b/ray_marching_maps.slang
index 87481a0..3c83a19 100644
--- a/ray_marching_maps.slang
+++ b/ray_marching_maps.slang
@@ -14,9 +14,9 @@
DifferentialPair<float3> dp_z = diffPair(xyz, float3(0, 0, 1))
#define R3R1_AUTODIFF_BASIS_VECTORS(fun, ...) \
- DifferentialPair<float> dp_x_out = fwd_diff(fun)(dp_x, ##__VA_ARGS__); \
- DifferentialPair<float> dp_y_out = fwd_diff(fun)(dp_y, ##__VA_ARGS__); \
- DifferentialPair<float> dp_z_out = fwd_diff(fun)(dp_z, ##__VA_ARGS__)
+ DifferentialPair<float> dp_x_out = fwd_diff(fun)(dp_x, __VA_ARGS__); \
+ DifferentialPair<float> dp_y_out = fwd_diff(fun)(dp_y, __VA_ARGS__); \
+ DifferentialPair<float> dp_z_out = fwd_diff(fun)(dp_z, __VA_ARGS__)
#define R3R1_DEFORM_NORMAL_AND_TANGENT(normal, tangent) \
{ \
@@ -29,29 +29,28 @@
// Syntactic sugar - wraps the previous three macros.
#define R3R1_RAY_MARCH_NORMALS(xyz, normal, tangent, fun, ...) \
R3R1_DECLARE_BASIS_VECTORS(xyz); \
- R3R1_AUTODIFF_BASIS_VECTORS(fun, ##__VA_ARGS__); \
+ R3R1_AUTODIFF_BASIS_VECTORS(fun, __VA_ARGS__); \
R3R1_DEFORM_NORMAL_AND_TANGENT(normal, tangent)
// Just trace a sphere of radius 0.1 for now.
[Differentiable]
-public float map(float3 p) {
- float3 count = float3(20,20,1);
- float3 period = 0.05f;
+public float map_ball_grid(float3 p, no_diff float3 count) {
+ float3 period = 1.0f / count;
+ period *= 2;
float3 half_period = period*0.5f;
- float3 which = abs(floor((p + half_period) / period));
- if (any(abs(which) >= count)) {
+ float3 which = floor((p + half_period) / period);
+ if (any(abs(which) > count/2)) {
p = 1e6;
} else {
p = glsl_mod(p + half_period, period) - half_period;
}
- p.z += half_period.x;
return length(p) - half_period.x;
}
-public void map_normal(inout float3 xyz, inout float3 normal,
+public void map_ball_grid_normal(float3 count, inout float3 xyz, inout float3 normal,
inout float3 tangent) {
- R3R1_RAY_MARCH_NORMALS(xyz, normal, tangent, map);
+ R3R1_RAY_MARCH_NORMALS(xyz, normal, tangent, map_ball_grid, count);
}
#endif // __RAY_MARCHING_MAPS_INC
diff --git a/vertex.cginc b/vertex.cginc
index cd3382c..1cbeb6d 100644
--- a/vertex.cginc
+++ b/vertex.cginc
@@ -59,6 +59,19 @@
#define VERTEX_DEFORM_XY_TUBE_NORM
#endif // VERTEX_DEFORMATION_XY_TUBE
+#if defined(_VERTEX_DEFORMATION_L2_LINF)
+#define VERTEX_DEFORM_L2_LINF_PREAMBLE \
+ float t = _Vertex_Deformation_L2_Linf_t
+#define VERTEX_DEFORM_L2_LINF_POS \
+ objPos = l2_to_linf(objPos.xyz, t)
+#define VERTEX_DEFORM_L2_LINF_NORM \
+ l2_to_linf_normal(objPos, objNorm, objTan, t)
+#else
+#define VERTEX_DEFORM_L2_LINF_PREAMBLE
+#define VERTEX_DEFORM_L2_LINF_POS
+#define VERTEX_DEFORM_L2_LINF_NORM
+#endif // _VERTEX_DEFORMATION_L2_LINF
+
#if defined(_VERTEX_DEFORMATION_SEAL)
#define VERTEX_DEFORM_SEAL_PREAMBLE \
float A = _Vertex_Deformation_Seal_A; \
@@ -125,6 +138,10 @@ void deform(inout float3 objPos) {
VERTEX_DEFORM_XY_TUBE_POS;
}
{
+ VERTEX_DEFORM_L2_LINF_PREAMBLE;
+ VERTEX_DEFORM_L2_LINF_POS;
+ }
+ {
VERTEX_DEFORM_SEAL_PREAMBLE;
VERTEX_DEFORM_SEAL_POS;
}
@@ -153,6 +170,10 @@ void deform_normal(inout float3 objPos, inout float3 objNorm, inout float3 objTa
VERTEX_DEFORM_XY_TUBE_NORM;
}
{
+ VERTEX_DEFORM_L2_LINF_PREAMBLE;
+ VERTEX_DEFORM_L2_LINF_NORM;
+ }
+ {
VERTEX_DEFORM_SEAL_PREAMBLE;
VERTEX_DEFORM_SEAL_NORM;
}
@@ -180,7 +201,7 @@ void deform_normal(inout float3 objPos, inout float3 objNorm, inout float3 objTa
// that we evaluate the normal deformation at the correct point in space.
void undeform_normal(inout float3 objPos, inout float3 objNorm, inout float3 objTan) {
const float t = getTime();
- const int kMaxDeformations = 8;
+ const int kMaxDeformations = 32;
// Cache each intermediate position so we can undo stages in reverse order.
float3 posHistory[kMaxDeformations + 1];
int posCount = 0;
@@ -207,6 +228,13 @@ void undeform_normal(inout float3 objPos, inout float3 objNorm, inout float3 obj
posHistory[posCount++] = objPos;
}
#endif
+#if defined(_VERTEX_DEFORMATION_L2_LINF)
+ {
+ VERTEX_DEFORM_L2_LINF_PREAMBLE;
+ VERTEX_DEFORM_L2_LINF_POS;
+ posHistory[posCount++] = objPos;
+ }
+#endif
#if defined(_VERTEX_DEFORMATION_SEAL)
{
VERTEX_DEFORM_SEAL_PREAMBLE;
diff --git a/vertex_deformation.slang b/vertex_deformation.slang
index c1faf44..5337e8a 100644
--- a/vertex_deformation.slang
+++ b/vertex_deformation.slang
@@ -43,8 +43,8 @@
); \
float jac_det = determinant(jacobian); \
float3x3 itjac = inverse(transpose(jacobian), jac_det); \
- normal = normalize(mul(itjac, normal) * jac_det); \
- tangent = normalize(mul(jacobian, tangent))
+ normal = mul(itjac, normal) * jac_det; \
+ tangent = mul(jacobian, tangent) * jac_det
#define R3R3_UNDEFORM_NORMAL_AND_TANGENT(normal, tangent) \
float3x3 jacobian = float3x3( \
@@ -55,8 +55,8 @@
float jac_det = determinant(jacobian); \
float3x3 inv_jac = inverse(jacobian, jac_det); \
float3x3 trans_jac = transpose(jacobian); \
- normal = normalize(mul(trans_jac, normal) / jac_det); \
- tangent = normalize(mul(inv_jac, tangent) / jac_det)
+ normal = mul(trans_jac, normal) * jac_det; \
+ tangent = mul(inv_jac, tangent) * jac_det
// Syntactic sugar - wraps the previous three macros.
#define R3R3_NORMALS(xyz, normal, tangent, fun, ...) \
@@ -171,6 +171,28 @@ public void sine_wave_undeform_normal(float3 xyz, inout float3 normal,
R3R3_UNDEFORM_NORMAL_AND_TANGENT(normal, tangent);
}
+// Map from l2 (euclidean) norm to l-infinity (supremum, max) norm.
+[Differentiable]
+public float3 l2_to_linf(float3 xyz, no_diff float t) {
+ float l2 = length(xyz);
+ float3 abs_xyz = abs(xyz);
+ float linf = max(abs_xyz.x, max(abs_xyz.y, abs_xyz.z));
+
+ float scale = lerp(1.0f, linf / l2, t);
+
+ return xyz * scale;
+}
+
+public void l2_to_linf_normal(float3 xyz, inout float3 normal, inout float3 tangent, float t) {
+ R3R3_NORMALS(xyz, normal, tangent, l2_to_linf, t);
+}
+
+public void l2_to_linf_undeform_normal(float3 xyz, inout float3 normal, inout float3 tangent, float t) {
+ R3R3_DECLARE_BASIS_VECTORS(xyz);
+ R3R3_AUTODIFF_BASIS_VECTORS(l2_to_linf, t);
+ R3R3_UNDEFORM_NORMAL_AND_TANGENT(normal, tangent);
+}
+
[Differentiable]
float3 rand3_hash3(float3 p)
{