summaryrefslogtreecommitdiffstats
path: root/vertex_deformation.slang
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2025-10-17 20:51:32 -0700
committeryum <yum.food.vr@gmail.com>2025-10-17 20:51:32 -0700
commit98e115ebfc40e72a8ee1c6453735d8f6076f7890 (patch)
tree70910197508a860adc505ef545d8d8825d48b98b /vertex_deformation.slang
parent5bfbb660c7213cdd14c9bde2349b3a09ccdaff4e (diff)
tessellate in screen space, and add sine wave deformation effect
Diffstat (limited to 'vertex_deformation.slang')
-rw-r--r--vertex_deformation.slang46
1 files changed, 29 insertions, 17 deletions
diff --git a/vertex_deformation.slang b/vertex_deformation.slang
index 63ce1a4..f4d4132 100644
--- a/vertex_deformation.slang
+++ b/vertex_deformation.slang
@@ -3,15 +3,18 @@
#define PI 3.14159265f
#define PI_RCP 0.31830988f
+#define TAU (2.0f * PI)
+
+#define glsl_mod(x,y) (((x)-(y)*floor((x)/(y))))
// Differentiable versions of common operators.
-#define dabs(x) length(float2(x, 1e-6))
+#define dabs(x) sqrt((x) * (x) + 1e-6)
#define dlerp(x, y, t) ((x) * (1-t) + (y) * t)
// Macros for transforming normal and tangent using autodiff.
// r3r3 refers to "r3 to r3 transform", aka a mapping between 3d real-valued
// spaces.
-#define R3R3_DECLARE_BASIS_VECTORS \
+#define R3R3_DECLARE_BASIS_VECTORS(xyz) \
DifferentialPair<float3> dp_x = diffPair(xyz, float3(1, 0, 0)); \
DifferentialPair<float3> dp_y = diffPair(xyz, float3(0, 1, 0)); \
DifferentialPair<float3> dp_z = diffPair(xyz, float3(0, 0, 1))
@@ -33,18 +36,13 @@
tangent = mul(jacobian, tangent)
// Syntactic sugar - wraps the previous three macros.
-#define R3R3_NORMALS(normal, tangent, fun, ...) \
- R3R3_DECLARE_BASIS_VECTORS; \
- R3R3_AUTODIFF_BASIS_VECTORS(fun, __VA_ARGS__); \
+#define R3R3_NORMALS(xyz, normal, tangent, fun, ...) \
+ R3R3_DECLARE_BASIS_VECTORS(xyz); \
+ R3R3_AUTODIFF_BASIS_VECTORS(fun, __VA_ARGS__); \
R3R3_DEFORM_NORMAL_AND_TANGENT(normal, tangent)
float3x3 inverse(float3x3 m, float det) {
- if (abs(det) < 1e-9) {
- return float3x3(
- 1,0,0,
- 0,1,0,
- 0,0,1);
- }
+ det = max(1e-6 * abs(det), abs(det)) * sign(det);
float invDet = 1.0f / det;
float3x3 inv;
@@ -71,7 +69,7 @@ public float3 plane_to_tube(float3 xyz, no_diff float t) {
float z0 = xyz.z;
float theta = x0 * PI;
- float radius = ((1.0f - z0) / (t * t + 1e-3)) * sign(t);
+ float radius = ((1.0f - z0) / (dabs(t) + 1e-4f)) * sign(t);
float x = sin(theta / radius) * radius * PI_RCP;
// The z0 term here is required to make the jacobian invertible.
@@ -82,7 +80,7 @@ public float3 plane_to_tube(float3 xyz, no_diff float t) {
public void plane_to_tube_normal(float3 xyz, inout float3 normal,
inout float3 tangent, float t) {
- R3R3_NORMALS(normal, tangent, plane_to_tube, t);
+ R3R3_NORMALS(xyz, normal, tangent, plane_to_tube, t);
}
[Differentiable]
@@ -93,7 +91,7 @@ public float3 seal(float3 xyz, no_diff float A, no_diff float k, no_diff float t
float x0 = x + sin(y * k) * 0.1;
float y0 = y + sin(x * k * 2) * 0.5 + cos(z);
- float z0 = (z + sin(x * y * k * PI + t) * A) * (1.0 + sin(y * k) * sin(x * k));
+ float z0 = (z + sin(x * y * k * PI + t)) * A * (1.0 + sin(y * k) * sin(x * k));
x0 += z0 * 0.1 * sin(z0 * PI + 1.5);
y0 += z0 * 0.1 * sin(z0 * PI + 1.5);
@@ -106,11 +104,25 @@ public float3 seal(float3 xyz, no_diff float A, no_diff float k, no_diff float t
);
}
-// Deform a normal vector using the inverse transpose of the jacobian.
public void seal_normal(float3 xyz, inout float3 normal,
inout float3 tangent, float A, float k, float t) {
- R3R3_NORMALS(normal, tangent, seal, A, k, t);
+ R3R3_NORMALS(xyz, normal, tangent, seal, A, k, t);
}
-#endif // __CUSTOM31_INC
+[Differentiable]
+public float3 sine_wave(float3 xyz,
+ no_diff float3 amplitude,
+ no_diff float3 direction,
+ no_diff float3 k,
+ no_diff float3 omega,
+ no_diff float t) {
+ xyz += amplitude * sin(k * dot(xyz, direction) + omega * t);
+ return xyz;
+}
+public void sine_wave_normal(float3 xyz, inout float3 normal, inout float3 tangent,
+ float3 amplitude, float3 direction, float3 k, float3 omega, float t) {
+ R3R3_NORMALS(xyz, normal, tangent, sine_wave, amplitude, direction, k, omega, t);
+}
+
+#endif // __CUSTOM31_INC