From 2edd7a2333ebc36f19f8726cb2d2f79aa501cb18 Mon Sep 17 00:00:00 2001 From: yum Date: Fri, 31 Oct 2025 15:40:20 -0700 Subject: begin sketching out "undeform" codepath --- vertex.cginc | 239 +++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 144 insertions(+), 95 deletions(-) (limited to 'vertex.cginc') diff --git a/vertex.cginc b/vertex.cginc index b5750c6..303fc00 100644 --- a/vertex.cginc +++ b/vertex.cginc @@ -6,130 +6,179 @@ #define FOO(x) (x) +#if defined(_VERTEX_DEFORMATION_XZ_TUBE) +#define VERTEX_DEFORM_XZ_TUBE_PREAMBLE \ + float t = _Vertex_Deformation_XZ_Tube_t +#define VERTEX_DEFORM_XZ_TUBE_POS \ + objPos = plane_to_tube(objPos.xyz, t) +#define VERTEX_DEFORM_XZ_TUBE_NORM \ + plane_to_tube_normal(objPos, objNorm, objTan, t) +#else +#define VERTEX_DEFORM_XZ_TUBE_PREAMBLE +#define VERTEX_DEFORM_XZ_TUBE_POS +#define VERTEX_DEFORM_XZ_TUBE_NORM +#endif // VERTEX_DEFORMATION_XZ_TUBE + +#if defined(_VERTEX_DEFORMATION_YZ_TUBE) +#define VERTEX_DEFORM_YZ_TUBE_PREAMBLE \ + float t = _Vertex_Deformation_YZ_Tube_t +#define VERTEX_DEFORM_YZ_TUBE_POS \ + objPos = plane_to_tube(objPos.yxz, t); \ + objPos = objPos.yxz +#define VERTEX_DEFORM_YZ_TUBE_NORM \ + float3 yzPos = objPos.yxz; \ + float3 yzNorm = objNorm.yxz; \ + float3 yzTan = objTan.yxz; \ + plane_to_tube_normal(yzPos, yzNorm, yzTan, t); \ + objPos = yzPos.yxz; \ + objNorm = yzNorm.yxz; \ + objTan = yzTan.yxz +#else +#define VERTEX_DEFORM_YZ_TUBE_PREAMBLE +#define VERTEX_DEFORM_YZ_TUBE_POS +#define VERTEX_DEFORM_YZ_TUBE_NORM +#endif // VERTEX_DEFORMATION_YZ_TUBE + +#if defined(_VERTEX_DEFORMATION_XY_TUBE) +#define VERTEX_DEFORM_XY_TUBE_PREAMBLE \ + float t = _Vertex_Deformation_XY_Tube_t +#define VERTEX_DEFORM_XY_TUBE_POS \ + objPos = plane_to_tube(objPos.xzy, t); \ + objPos = objPos.xzy +#define VERTEX_DEFORM_XY_TUBE_NORM \ + float3 xyPos = objPos.xzy; \ + float3 xyNorm = objNorm.xzy; \ + float3 xyTan = objTan.xzy; \ + plane_to_tube_normal(xyPos, xyNorm, xyTan, t); \ + objPos = xyPos.xzy; \ + objNorm = xyNorm.xzy; \ + objTan = xyTan.xzy +#else +#define VERTEX_DEFORM_XY_TUBE_PREAMBLE +#define VERTEX_DEFORM_XY_TUBE_POS +#define VERTEX_DEFORM_XY_TUBE_NORM +#endif // VERTEX_DEFORMATION_XY_TUBE + +#if defined(_VERTEX_DEFORMATION_SEAL) +#define VERTEX_DEFORM_SEAL_PREAMBLE \ + float A = _Vertex_Deformation_Seal_A; \ + float k = _Vertex_Deformation_Seal_k; \ + float st = t * _Vertex_Deformation_Seal_t +#define VERTEX_DEFORM_SEAL_POS \ + objPos = seal(objPos.xyz, A, k, st) +#define VERTEX_DEFORM_SEAL_NORM \ + seal_normal(objPos, objNorm, objTan, A, k, st) +#else +#define VERTEX_DEFORM_SEAL_PREAMBLE +#define VERTEX_DEFORM_SEAL_POS +#define VERTEX_DEFORM_SEAL_NORM +#endif // _VERTEX_DEFORMATION_SEAL + +#if defined(_VERTEX_DEFORMATION_SINE_WAVES) +#define VERTEX_DEFORM_SINE_WAVES_PREAMBLE \ + float st = t * 10; \ + float3 amplitude = _Vertex_Deformation_Sine_Waves_Amplitude; \ + float3 direction = _Vertex_Deformation_Sine_Waves_Direction; \ + float3 k = _Vertex_Deformation_Sine_Waves_k; \ + float3 omega = _Vertex_Deformation_Sine_Waves_omega +#define VERTEX_DEFORM_SINE_WAVES_POS \ + objPos = sine_wave(objPos.xyz, amplitude, direction, k, omega, st) +#define VERTEX_DEFORM_SINE_WAVES_NORM \ + sine_wave_normal(objPos, objNorm, objTan, amplitude, direction, k, omega, st) +#else +#define VERTEX_DEFORM_SINE_WAVES_PREAMBLE +#define VERTEX_DEFORM_SINE_WAVES_POS +#define VERTEX_DEFORM_SINE_WAVES_NORM +#endif // _VERTEX_DEFORMATION_SINE_WAVES + +#if defined(_VERTEX_DEFORMATION_FBM) +#define VERTEX_DEFORM_FBM_PREAMBLE \ + float st = t; \ + 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 +#define VERTEX_DEFORM_FBM_POS \ + objPos = fbm(objPos, st, amplitude, gain, lacunarity, scale, octaves, velocity) +#define VERTEX_DEFORM_FBM_NORM \ + fbm_normal(objPos, objNorm, objTan, st, amplitude, gain, lacunarity, scale, octaves, velocity) +#else +#define VERTEX_DEFORM_FBM_PREAMBLE +#define VERTEX_DEFORM_FBM_POS +#define VERTEX_DEFORM_FBM_NORM +#endif // _VERTEX_DEFORMATION_FBM + void deform(inout float3 objPos) { const float t = getTime(); - -#if defined(_VERTEX_DEFORMATION_XZ_TUBE) { - float t = _Vertex_Deformation_XZ_Tube_t; - objPos = plane_to_tube(objPos.xyz, t); + VERTEX_DEFORM_XZ_TUBE_PREAMBLE; + VERTEX_DEFORM_XZ_TUBE_POS; } -#endif // _VERTEX_DEFORMATION_XZ_TUBE -#if defined(_VERTEX_DEFORMATION_YZ_TUBE) { - float t = _Vertex_Deformation_YZ_Tube_t; - objPos = plane_to_tube(objPos.yxz, t); - objPos = objPos.yxz; + VERTEX_DEFORM_YZ_TUBE_PREAMBLE; + VERTEX_DEFORM_YZ_TUBE_POS; } -#endif // _VERTEX_DEFORMATION_YZ_TUBE -#if defined(_VERTEX_DEFORMATION_XY_TUBE) { - float t = _Vertex_Deformation_XY_Tube_t; - objPos = plane_to_tube(objPos.xzy, t); - objPos = objPos.xzy; + VERTEX_DEFORM_XY_TUBE_PREAMBLE; + VERTEX_DEFORM_XY_TUBE_POS; } -#endif // _VERTEX_DEFORMATION_XY_TUBE -#if defined(_VERTEX_DEFORMATION_SEAL) { - float A = _Vertex_Deformation_Seal_A; - float k = _Vertex_Deformation_Seal_k; - float st = t * _Vertex_Deformation_Seal_t; - objPos = seal(objPos.xyz, A, k, st); + VERTEX_DEFORM_SEAL_PREAMBLE; + VERTEX_DEFORM_SEAL_POS; } -#endif // _VERTEX_DEFORMATION_SEAL -#if defined(_VERTEX_DEFORMATION_SINE_WAVES) { - float st = t * 10; - float3 amplitude = _Vertex_Deformation_Sine_Waves_Amplitude; - float3 direction = _Vertex_Deformation_Sine_Waves_Direction; - float3 k = _Vertex_Deformation_Sine_Waves_k; - float3 omega = _Vertex_Deformation_Sine_Waves_omega; - objPos = sine_wave(objPos.xyz, amplitude, direction, k, omega, st); + VERTEX_DEFORM_SINE_WAVES_PREAMBLE; + VERTEX_DEFORM_SINE_WAVES_POS; } -#endif // _VERTEX_DEFORMATION_SINE_WAVES -#if defined(_VERTEX_DEFORMATION_FBM) { - float st = t; - 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, - st, - amplitude, - gain, - lacunarity, - scale, - octaves, - velocity); + VERTEX_DEFORM_FBM_PREAMBLE; + VERTEX_DEFORM_FBM_POS; } -#endif // _VERTEX_DEFORMATION_FBM } -void deform_normal(float3 objPos, inout float3 objNorm, inout float3 objTan) { +void deform_normal(inout float3 objPos, inout float3 objNorm, inout float3 objTan) { const float t = getTime(); - -#if defined(_VERTEX_DEFORMATION_XZ_TUBE) { - float t = _Vertex_Deformation_XZ_Tube_t; - plane_to_tube_normal(objPos, objNorm, objTan, t); + VERTEX_DEFORM_XZ_TUBE_PREAMBLE; + VERTEX_DEFORM_XZ_TUBE_NORM; } -#endif // _VERTEX_DEFORMATION_XZ_TUBE -#if defined(_VERTEX_DEFORMATION_YZ_TUBE) { - float t = _Vertex_Deformation_YZ_Tube_t; - plane_to_tube_normal(objPos.yxz, objNorm.yxz, objTan.yxz, t); + VERTEX_DEFORM_YZ_TUBE_PREAMBLE; + VERTEX_DEFORM_YZ_TUBE_NORM; } -#endif // _VERTEX_DEFORMATION_YZ_TUBE -#if defined(_VERTEX_DEFORMATION_XY_TUBE) { - float t = _Vertex_Deformation_XY_Tube_t; - plane_to_tube_normal(objPos.xzy, objNorm.xzy, objTan.xzy, t); + VERTEX_DEFORM_XY_TUBE_PREAMBLE; + VERTEX_DEFORM_XY_TUBE_NORM; } -#endif // _VERTEX_DEFORMATION_XY_TUBE -#if defined(_VERTEX_DEFORMATION_SEAL) { - float A = _Vertex_Deformation_Seal_A; - float k = _Vertex_Deformation_Seal_k; - float st = t * _Vertex_Deformation_Seal_t; - seal_normal(objPos, objNorm, objTan, A, k, st); + VERTEX_DEFORM_SEAL_PREAMBLE; + VERTEX_DEFORM_SEAL_NORM; } -#endif // _VERTEX_DEFORMATION_SEAL -#if defined(_VERTEX_DEFORMATION_SINE_WAVES) { - float st = t * 10; - float3 amplitude = _Vertex_Deformation_Sine_Waves_Amplitude; - float3 direction = _Vertex_Deformation_Sine_Waves_Direction; - float3 k = _Vertex_Deformation_Sine_Waves_k; - float3 omega = _Vertex_Deformation_Sine_Waves_omega; - sine_wave_normal(objPos, objNorm, objTan, amplitude, direction, k, omega, st); + VERTEX_DEFORM_SINE_WAVES_PREAMBLE; + VERTEX_DEFORM_SINE_WAVES_NORM; } -#endif // _VERTEX_DEFORMATION_SINE_WAVES -#if defined(_VERTEX_DEFORMATION_FBM) { - float st = t; - 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, - st, - amplitude, - gain, - lacunarity, - scale, - octaves, - velocity); + VERTEX_DEFORM_FBM_PREAMBLE; + VERTEX_DEFORM_FBM_NORM; } -#endif // _VERTEX_DEFORMATION_FBM +} + +// `objPos` is in the un-deformed coordinate system, while `objNorm` and +// `objTan` are in the deformed coordinate system. The task is to map `objNorm` +// and `objTan` to the un-deformed coordinate system. +// We do this, coarsely, as follows: +// 1. Map objPos into the deformed coordinate system through stepwise +// transformations, like `deform`. We remember each position along the way. +// 2. Map objNorm and objTan back one step using the deformed final position. +// For the first iteration, we use the last (deformed) `objPos` that we +// remembered from step 1. +// 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 propagateObjPos(inout v2f i) { -- cgit v1.2.3