diff options
| author | yum <yum.food.vr@gmail.com> | 2025-10-12 21:52:50 -0700 |
|---|---|---|
| committer | yum <yum.food.vr@gmail.com> | 2025-10-12 21:52:50 -0700 |
| commit | 228e555dbd07dd1a332a07770106dfd98f918c9b (patch) | |
| tree | 8f058e1c897ba818dedc2564676f0278eddce128 /vertex_deformation.slang | |
| parent | a4bf31470f7e2855f13d922e3e7ad1c7767d9afd (diff) | |
fornite update
Diffstat (limited to 'vertex_deformation.slang')
| -rw-r--r-- | vertex_deformation.slang | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/vertex_deformation.slang b/vertex_deformation.slang new file mode 100644 index 0000000..63ce1a4 --- /dev/null +++ b/vertex_deformation.slang @@ -0,0 +1,116 @@ +#ifndef __CUSTOM31_INC +#define __CUSTOM31_INC + +#define PI 3.14159265f +#define PI_RCP 0.31830988f + +// Differentiable versions of common operators. +#define dabs(x) length(float2(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 \ + 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)) + +#define R3R3_AUTODIFF_BASIS_VECTORS(fun, ...) \ + DifferentialPair<float3> dp_x_out = fwd_diff(fun)(dp_x, __VA_ARGS__); \ + DifferentialPair<float3> dp_y_out = fwd_diff(fun)(dp_y, __VA_ARGS__); \ + DifferentialPair<float3> dp_z_out = fwd_diff(fun)(dp_z, __VA_ARGS__) + +#define R3R3_DEFORM_NORMAL_AND_TANGENT(normal, tangent) \ + float3x3 jacobian = float3x3( \ + float3(dp_x_out.d.x, dp_y_out.d.x, dp_z_out.d.x), \ + float3(dp_x_out.d.y, dp_y_out.d.y, dp_z_out.d.y), \ + float3(dp_x_out.d.z, dp_y_out.d.z, dp_z_out.d.z) \ + ); \ + float jac_det = determinant(jacobian); \ + float3x3 itjac = inverse(transpose(jacobian), jac_det); \ + normal = mul(itjac, normal) * sign(jac_det); \ + 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__); \ + 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); + } + + float invDet = 1.0f / det; + float3x3 inv; + + inv._11 = (m._22 * m._33 - m._23 * m._32) * invDet; + inv._12 = (m._13 * m._32 - m._12 * m._33) * invDet; + inv._13 = (m._12 * m._23 - m._13 * m._22) * invDet; + inv._21 = (m._23 * m._31 - m._21 * m._33) * invDet; + inv._22 = (m._11 * m._33 - m._13 * m._31) * invDet; + inv._23 = (m._13 * m._21 - m._11 * m._23) * invDet; + inv._31 = (m._21 * m._32 - m._22 * m._31) * invDet; + inv._32 = (m._31 * m._12 - m._11 * m._32) * invDet; + inv._33 = (m._11 * m._22 - m._12 * m._21) * invDet; + + return inv; +} + +// Takes map a 2x2 quad on the xy plane to a tube. The circular cross section +// is on the xz plane. +[Differentiable] +public float3 plane_to_tube(float3 xyz, no_diff float t) { + float x0 = xyz.x; + float y0 = xyz.y; + float z0 = xyz.z; + + float theta = x0 * PI; + float radius = ((1.0f - z0) / (t * t + 1e-3)) * sign(t); + + float x = sin(theta / radius) * radius * PI_RCP; + // The z0 term here is required to make the jacobian invertible. + float z = z0 + (1.0f - cos(theta / radius)) * radius * PI_RCP; + + return float3(x, y0, z); +} + +public void plane_to_tube_normal(float3 xyz, inout float3 normal, + inout float3 tangent, float t) { + R3R3_NORMALS(normal, tangent, plane_to_tube, t); +} + +[Differentiable] +public float3 seal(float3 xyz, no_diff float A, no_diff float k, no_diff float t) { + float x = xyz.x; + float y = xyz.y; + float z = xyz.z; + + 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)); + + x0 += z0 * 0.1 * sin(z0 * PI + 1.5); + y0 += z0 * 0.1 * sin(z0 * PI + 1.5); + + x0 -= 0.0; + y0 -= 1.2; + + return float3( + x0, y0, z0 + ); +} + +// 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); +} + +#endif // __CUSTOM31_INC + |
