summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2025-11-01 11:45:25 -0700
committeryum <yum.food.vr@gmail.com>2025-11-01 11:45:25 -0700
commit12c223b5b8bf1860d77f41551cf9e5374196aed7 (patch)
tree093233b97e2f4f91377de17817ba5f5fc45143f3
parent2edd7a2333ebc36f19f8726cb2d2f79aa501cb18 (diff)
meow
-rw-r--r--features.cginc4
-rw-r--r--ray_marching.cginc4
-rw-r--r--vertex.cginc124
-rw-r--r--vertex_deformation.slang40
4 files changed, 165 insertions, 7 deletions
diff --git a/features.cginc b/features.cginc
index 4547929..b5c7e64 100644
--- a/features.cginc
+++ b/features.cginc
@@ -70,10 +70,6 @@
//ifex _Ray_Marching_Enabled==0
#pragma shader_feature_local _RAY_MARCHING
-#if defined(_RAY_MARCHING)
-#undef _VERTEX_DEFORMATION_FRAGMENT_NORMALS
-#undef _VERTEX_DEFORMATION_TESSELLATION
-#endif
//endex
//ifex _Ray_Marching_Baked_Origins_Enabled==0
diff --git a/ray_marching.cginc b/ray_marching.cginc
index 9c6653f..1efbdf4 100644
--- a/ray_marching.cginc
+++ b/ray_marching.cginc
@@ -32,8 +32,8 @@ RayMarchResult ray_march(v2f i) {
float3 rd_perp = i.normal * dot(i.normal, rd);
float3 rd_tan = rd - rd_perp;
float3 tmp = ro;
- deform_normal(tmp, rd_perp, rd_tan);
- rd = rd_perp + rd_tan;
+ undeform_normal(ro, rd_perp, rd_tan);
+ //rd = rd_perp + rd_tan;
#endif
const float kMinDist = 1e-3;
diff --git a/vertex.cginc b/vertex.cginc
index 303fc00..cd3382c 100644
--- a/vertex.cginc
+++ b/vertex.cginc
@@ -178,7 +178,129 @@ void deform_normal(inout float3 objPos, inout float3 objNorm, inout float3 objTa
// 3. Repeat step 2 until we're back in the un-deforme coordinate system. At
// each step, we use the cached objPos from the forward pass (step 1), so
// that we evaluate the normal deformation at the correct point in space.
-void undeform_normal(float3 objPos, inout float3 objNorm, inout float3 objTan) {
+void undeform_normal(inout float3 objPos, inout float3 objNorm, inout float3 objTan) {
+ const float t = getTime();
+ const int kMaxDeformations = 8;
+ // Cache each intermediate position so we can undo stages in reverse order.
+ float3 posHistory[kMaxDeformations + 1];
+ int posCount = 0;
+ posHistory[posCount++] = objPos;
+
+#if defined(_VERTEX_DEFORMATION_XZ_TUBE)
+ {
+ VERTEX_DEFORM_XZ_TUBE_PREAMBLE;
+ VERTEX_DEFORM_XZ_TUBE_POS;
+ posHistory[posCount++] = objPos;
+ }
+#endif
+#if defined(_VERTEX_DEFORMATION_YZ_TUBE)
+ {
+ VERTEX_DEFORM_YZ_TUBE_PREAMBLE;
+ VERTEX_DEFORM_YZ_TUBE_POS;
+ posHistory[posCount++] = objPos;
+ }
+#endif
+#if defined(_VERTEX_DEFORMATION_XY_TUBE)
+ {
+ VERTEX_DEFORM_XY_TUBE_PREAMBLE;
+ VERTEX_DEFORM_XY_TUBE_POS;
+ posHistory[posCount++] = objPos;
+ }
+#endif
+#if defined(_VERTEX_DEFORMATION_SEAL)
+ {
+ VERTEX_DEFORM_SEAL_PREAMBLE;
+ VERTEX_DEFORM_SEAL_POS;
+ posHistory[posCount++] = objPos;
+ }
+#endif
+#if defined(_VERTEX_DEFORMATION_SINE_WAVES)
+ {
+ VERTEX_DEFORM_SINE_WAVES_PREAMBLE;
+ VERTEX_DEFORM_SINE_WAVES_POS;
+ posHistory[posCount++] = objPos;
+ }
+#endif
+#if defined(_VERTEX_DEFORMATION_FBM)
+ {
+ VERTEX_DEFORM_FBM_PREAMBLE;
+ VERTEX_DEFORM_FBM_POS;
+ posHistory[posCount++] = objPos;
+ }
+#endif
+ float3 objPos_deformed = objPos;
+
+ int cursor = posCount - 1;
+ objPos = posHistory[cursor];
+
+#if defined(_VERTEX_DEFORMATION_FBM)
+ if (cursor > 0) {
+ VERTEX_DEFORM_FBM_PREAMBLE;
+ float3 stageInput = posHistory[cursor - 1];
+ fbm_undeform_normal(stageInput, st, amplitude, gain, lacunarity, scale,
+ octaves, velocity, objNorm, objTan);
+ objPos = stageInput;
+ cursor -= 1;
+ }
+#endif
+#if defined(_VERTEX_DEFORMATION_SINE_WAVES)
+ if (cursor > 0) {
+ VERTEX_DEFORM_SINE_WAVES_PREAMBLE;
+ float3 stageInput = posHistory[cursor - 1];
+ sine_wave_undeform_normal(stageInput, objNorm, objTan, amplitude, direction,
+ k, omega, st);
+ objPos = stageInput;
+ cursor -= 1;
+ }
+#endif
+#if defined(_VERTEX_DEFORMATION_SEAL)
+ if (cursor > 0) {
+ VERTEX_DEFORM_SEAL_PREAMBLE;
+ float3 stageInput = posHistory[cursor - 1];
+ seal_undeform_normal(stageInput, objNorm, objTan, A, k, st);
+ objPos = stageInput;
+ cursor -= 1;
+ }
+#endif
+#if defined(_VERTEX_DEFORMATION_XY_TUBE)
+ if (cursor > 0) {
+ VERTEX_DEFORM_XY_TUBE_PREAMBLE;
+ float3 stageInput = posHistory[cursor - 1];
+ float3 xyPos = stageInput.xzy;
+ float3 xyNorm = objNorm.xzy;
+ float3 xyTan = objTan.xzy;
+ plane_to_tube_undeform_normal(xyPos, xyNorm, xyTan, t);
+ objNorm = xyNorm.xzy;
+ objTan = xyTan.xzy;
+ objPos = stageInput;
+ cursor -= 1;
+ }
+#endif
+#if defined(_VERTEX_DEFORMATION_YZ_TUBE)
+ if (cursor > 0) {
+ VERTEX_DEFORM_YZ_TUBE_PREAMBLE;
+ float3 stageInput = posHistory[cursor - 1];
+ float3 yzPos = stageInput.yxz;
+ float3 yzNorm = objNorm.yxz;
+ float3 yzTan = objTan.yxz;
+ plane_to_tube_undeform_normal(yzPos, yzNorm, yzTan, t);
+ objNorm = yzNorm.yxz;
+ objTan = yzTan.yxz;
+ objPos = stageInput;
+ cursor -= 1;
+ }
+#endif
+#if defined(_VERTEX_DEFORMATION_XZ_TUBE)
+ if (cursor > 0) {
+ VERTEX_DEFORM_XZ_TUBE_PREAMBLE;
+ float3 stageInput = posHistory[cursor - 1];
+ plane_to_tube_undeform_normal(stageInput, objNorm, objTan, t);
+ objPos = stageInput;
+ cursor -= 1;
+ }
+#endif
+
+ objPos = objPos_deformed;
}
void propagateObjPos(inout v2f i) {
diff --git a/vertex_deformation.slang b/vertex_deformation.slang
index 63804de..678cc24 100644
--- a/vertex_deformation.slang
+++ b/vertex_deformation.slang
@@ -107,6 +107,13 @@ public void plane_to_tube_normal(inout float3 xyz, inout float3 normal,
R3R3_NORMALS(xyz, normal, tangent, plane_to_tube, t);
}
+public void plane_to_tube_undeform_normal(float3 xyz, inout float3 normal,
+ inout float3 tangent, float t) {
+ R3R3_DECLARE_BASIS_VECTORS(xyz);
+ R3R3_AUTODIFF_BASIS_VECTORS(plane_to_tube, t);
+ R3R3_UNDEFORM_NORMAL_AND_TANGENT(normal, tangent);
+}
+
[Differentiable]
public float3 seal(float3 xyz, no_diff float A, no_diff float k, no_diff float t) {
float x = xyz.x;
@@ -133,6 +140,13 @@ public void seal_normal(inout float3 xyz, inout float3 normal,
R3R3_NORMALS(xyz, normal, tangent, seal, A, k, t);
}
+public void seal_undeform_normal(float3 xyz, inout float3 normal,
+ inout float3 tangent, float A, float k, float t) {
+ R3R3_DECLARE_BASIS_VECTORS(xyz);
+ R3R3_AUTODIFF_BASIS_VECTORS(seal, A, k, t);
+ R3R3_UNDEFORM_NORMAL_AND_TANGENT(normal, tangent);
+}
+
[Differentiable]
public float3 sine_wave(float3 xyz,
no_diff float3 amplitude,
@@ -149,6 +163,14 @@ 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);
}
+public void sine_wave_undeform_normal(float3 xyz, inout float3 normal,
+ inout float3 tangent, float3 amplitude, float3 direction, float3 k,
+ float3 omega, float t) {
+ R3R3_DECLARE_BASIS_VECTORS(xyz);
+ R3R3_AUTODIFF_BASIS_VECTORS(sine_wave, amplitude, direction, k, omega, t);
+ R3R3_UNDEFORM_NORMAL_AND_TANGENT(normal, tangent);
+}
+
[Differentiable]
float3 rand3_hash3(float3 p)
{
@@ -292,4 +314,22 @@ public void fbm_normal(inout float3 xyz, inout float3 normal, inout float3 tange
tangent = mul(jac, tangent);
}
+public void fbm_undeform_normal(float3 xyz, float t,
+ float amplitude, float gain, float lacunarity,
+ float scale, float octaves, float3 velocity,
+ inout float3 normal, inout float3 tangent) {
+ float3 dx, dy, dz;
+ 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 inv_jac = inverse(jac, jac_det);
+ float3x3 trans_jac = transpose(jac);
+ normal = mul(trans_jac, normal) * sign(jac_det);
+ tangent = mul(inv_jac, tangent);
+}
+
#endif // __CUSTOM31_INC