@needle-tools/three-mesh-effects 0.0.0-stage → 0.1.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +93 -2
- package/build/deformation-runtime.js +2143 -0
- package/build/deformation-runtime.js.map +7 -0
- package/build/impostor-runtime-needle-engine.js +42 -0
- package/build/impostor-runtime-needle-engine.js.map +7 -0
- package/build/impostor-runtime.js +1333 -0
- package/build/impostor-runtime.js.map +7 -0
- package/build/internal/deformation/cloth-surface.js +288 -0
- package/build/internal/deformation/cloth-surface.js.map +7 -0
- package/build/internal/deformation/fur-surface.js +130 -0
- package/build/internal/deformation/fur-surface.js.map +7 -0
- package/build/internal/deformation/ik-constraints.js +44 -0
- package/build/internal/deformation/ik-constraints.js.map +7 -0
- package/build/internal/deformation/joints-json.js +197 -0
- package/build/internal/deformation/joints-json.js.map +7 -0
- package/build/internal/deformation/limits.js +16 -0
- package/build/internal/deformation/limits.js.map +7 -0
- package/build/internal/deformation/motion-settings.js +12 -0
- package/build/internal/deformation/motion-settings.js.map +7 -0
- package/build/internal/deformation/pin-cloth.js +196 -0
- package/build/internal/deformation/pin-cloth.js.map +7 -0
- package/build/internal/deformation/pin-ik.js +650 -0
- package/build/internal/deformation/pin-ik.js.map +7 -0
- package/build/internal/deformation/pin-types.js +12 -0
- package/build/internal/deformation/pin-types.js.map +7 -0
- package/build/internal/deformation/pin-wiggle.js +249 -0
- package/build/internal/deformation/pin-wiggle.js.map +7 -0
- package/build/internal/deformation/pose-slots.js +84 -0
- package/build/internal/deformation/pose-slots.js.map +7 -0
- package/build/internal/deformation/pose-timeline.js +210 -0
- package/build/internal/deformation/pose-timeline.js.map +7 -0
- package/build/internal/deformation/rig-drivers.js +48 -0
- package/build/internal/deformation/rig-drivers.js.map +7 -0
- package/build/internal/deformation/rig-extension.js +334 -0
- package/build/internal/deformation/rig-extension.js.map +7 -0
- package/build/internal/deformation/rig-runtime.js +2141 -0
- package/build/internal/deformation/rig-runtime.js.map +7 -0
- package/build/internal/deformation/wind.js +34 -0
- package/build/internal/deformation/wind.js.map +7 -0
- package/build/internal/impostor/impostor-metadata.js +77 -0
- package/build/internal/impostor/impostor-metadata.js.map +7 -0
- package/build/internal/impostor/impostor-node-material.js +1331 -0
- package/build/internal/impostor/impostor-node-material.js.map +7 -0
- package/build/internal/impostor/impostor-runtime-support.js +162 -0
- package/build/internal/impostor/impostor-runtime-support.js.map +7 -0
- package/build/internal/impostor/octahedral.js +232 -0
- package/build/internal/impostor/octahedral.js.map +7 -0
- package/build/internal/pixel-impostor/pixel-impostor-display.js +728 -0
- package/build/internal/pixel-impostor/pixel-impostor-display.js.map +7 -0
- package/build/internal/pixel-impostor/pixel-impostor-metadata.js +71 -0
- package/build/internal/pixel-impostor/pixel-impostor-metadata.js.map +7 -0
- package/build/internal/pixel-impostor/pixel-impostor-runtime.js +150 -0
- package/build/internal/pixel-impostor/pixel-impostor-runtime.js.map +7 -0
- package/build/internal/rendering/skin-projection.js +4630 -0
- package/build/internal/rendering/skin-projection.js.map +7 -0
- package/build/internal/rendering/surface-query.js +4519 -0
- package/build/internal/rendering/surface-query.js.map +7 -0
- package/build/internal/rendering/wireframe.js +112 -0
- package/build/internal/rendering/wireframe.js.map +7 -0
- package/build/internal/rendering/wrap-preview-settings.js +126 -0
- package/build/internal/rendering/wrap-preview-settings.js.map +7 -0
- package/build/internal/rendering/wrap-preview.js +5610 -0
- package/build/internal/rendering/wrap-preview.js.map +7 -0
- package/build/internal/rendering/wrap-skin.js +4746 -0
- package/build/internal/rendering/wrap-skin.js.map +7 -0
- package/build/pixel-impostor-runtime.js +876 -0
- package/build/pixel-impostor-runtime.js.map +7 -0
- package/build/types/deformation/cloth-surface.d.ts +39 -0
- package/build/types/deformation/cloth-surface.js +1 -0
- package/build/types/deformation/fur-surface.d.ts +27 -0
- package/build/types/deformation/fur-surface.js +1 -0
- package/build/types/deformation/ik-constraints.d.ts +18 -0
- package/build/types/deformation/ik-constraints.js +1 -0
- package/build/types/deformation/joints-json.d.ts +17 -0
- package/build/types/deformation/joints-json.js +1 -0
- package/build/types/deformation/limits.d.ts +23 -0
- package/build/types/deformation/limits.js +1 -0
- package/build/types/deformation/motion-settings.d.ts +9 -0
- package/build/types/deformation/motion-settings.js +1 -0
- package/build/types/deformation/pin-cloth.d.ts +28 -0
- package/build/types/deformation/pin-cloth.js +1 -0
- package/build/types/deformation/pin-ik.d.ts +37 -0
- package/build/types/deformation/pin-ik.js +1 -0
- package/build/types/deformation/pin-types.d.ts +53 -0
- package/build/types/deformation/pin-types.js +1 -0
- package/build/types/deformation/pin-wiggle.d.ts +16 -0
- package/build/types/deformation/pin-wiggle.js +1 -0
- package/build/types/deformation/pose-slots.d.ts +41 -0
- package/build/types/deformation/pose-slots.js +1 -0
- package/build/types/deformation/pose-timeline.d.ts +65 -0
- package/build/types/deformation/pose-timeline.js +1 -0
- package/build/types/deformation/rig-drivers.d.ts +19 -0
- package/build/types/deformation/rig-drivers.js +1 -0
- package/build/types/deformation/rig-extension.d.ts +33 -0
- package/build/types/deformation/rig-extension.js +1 -0
- package/build/types/deformation/rig-runtime.d.ts +95 -0
- package/build/types/deformation/rig-runtime.js +1 -0
- package/build/types/deformation/wind.d.ts +16 -0
- package/build/types/deformation/wind.js +1 -0
- package/build/types/impostor/impostor-metadata.d.ts +61 -0
- package/build/types/impostor/impostor-metadata.js +1 -0
- package/build/types/impostor/impostor-node-material.d.ts +140 -0
- package/build/types/impostor/impostor-node-material.js +1 -0
- package/build/types/impostor/impostor-runtime-support.d.ts +52 -0
- package/build/types/impostor/impostor-runtime-support.js +1 -0
- package/build/types/impostor/octahedral.d.ts +84 -0
- package/build/types/impostor/octahedral.js +1 -0
- package/build/types/pixel-impostor/pixel-impostor-display.d.ts +48 -0
- package/build/types/pixel-impostor/pixel-impostor-display.js +1 -0
- package/build/types/pixel-impostor/pixel-impostor-metadata.d.ts +34 -0
- package/build/types/pixel-impostor/pixel-impostor-metadata.js +1 -0
- package/build/types/pixel-impostor/pixel-impostor-runtime.d.ts +18 -0
- package/build/types/pixel-impostor/pixel-impostor-runtime.js +1 -0
- package/build/types/public/deformation-runtime.d.ts +3 -0
- package/build/types/public/deformation-runtime.js +1 -0
- package/build/types/public/impostor-runtime-needle-engine.d.ts +31 -0
- package/build/types/public/impostor-runtime-needle-engine.js +1 -0
- package/build/types/public/impostor-runtime.d.ts +6 -0
- package/build/types/public/impostor-runtime.js +1 -0
- package/build/types/public/pixel-impostor-runtime.d.ts +3 -0
- package/build/types/public/pixel-impostor-runtime.js +1 -0
- package/build/types/public/wrap-effects.d.ts +4 -0
- package/build/types/public/wrap-effects.js +1 -0
- package/build/types/rendering/skin-projection.d.ts +44 -0
- package/build/types/rendering/skin-projection.js +1 -0
- package/build/types/rendering/surface-query.d.ts +66 -0
- package/build/types/rendering/surface-query.js +1 -0
- package/build/types/rendering/wireframe.d.ts +25 -0
- package/build/types/rendering/wireframe.js +1 -0
- package/build/types/rendering/wrap-preview-settings.d.ts +22 -0
- package/build/types/rendering/wrap-preview-settings.js +1 -0
- package/build/types/rendering/wrap-preview.d.ts +39 -0
- package/build/types/rendering/wrap-preview.js +1 -0
- package/build/types/rendering/wrap-skin.d.ts +14 -0
- package/build/types/rendering/wrap-skin.js +1 -0
- package/build/wrap-effects.js +5640 -0
- package/build/wrap-effects.js.map +7 -0
- package/package.json +105 -4
- package/src/deformation/cloth-surface.ts +223 -0
- package/src/deformation/fur-surface.ts +129 -0
- package/src/deformation/ik-constraints.ts +37 -0
- package/src/deformation/joints-json.ts +94 -0
- package/src/deformation/limits.ts +25 -0
- package/src/deformation/motion-settings.ts +9 -0
- package/src/deformation/pin-cloth.ts +149 -0
- package/src/deformation/pin-ik.ts +582 -0
- package/src/deformation/pin-types.ts +22 -0
- package/src/deformation/pin-wiggle.ts +119 -0
- package/src/deformation/pose-slots.ts +106 -0
- package/src/deformation/pose-timeline.ts +242 -0
- package/src/deformation/rig-drivers.ts +32 -0
- package/src/deformation/rig-extension.ts +87 -0
- package/src/deformation/rig-runtime.ts +416 -0
- package/src/deformation/wind.ts +36 -0
- package/src/impostor/impostor-metadata.js +137 -0
- package/src/impostor/impostor-node-material.js +1581 -0
- package/src/impostor/impostor-runtime-support.js +139 -0
- package/src/impostor/octahedral.js +280 -0
- package/src/pixel-impostor/pixel-impostor-display.js +794 -0
- package/src/pixel-impostor/pixel-impostor-metadata.js +74 -0
- package/src/pixel-impostor/pixel-impostor-runtime.js +78 -0
- package/src/public/deformation-runtime.ts +3 -0
- package/src/public/impostor-runtime-needle-engine.js +56 -0
- package/src/public/impostor-runtime.js +21 -0
- package/src/public/pixel-impostor-runtime.js +5 -0
- package/src/public/wrap-effects.js +21 -0
- package/src/rendering/skin-projection.js +113 -0
- package/src/rendering/surface-query.js +88 -0
- package/src/rendering/wireframe.js +115 -0
- package/src/rendering/wrap-preview-settings.js +123 -0
- package/src/rendering/wrap-preview.js +645 -0
- package/src/rendering/wrap-skin.js +109 -0
|
@@ -0,0 +1,645 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import * as THREE from 'three';
|
|
3
|
+
import { MeshBasicNodeMaterial, MeshPhysicalNodeMaterial, MeshStandardNodeMaterial } from 'three/webgpu';
|
|
4
|
+
import { clone as cloneSkeleton } from 'three/addons/utils/SkeletonUtils.js';
|
|
5
|
+
import { mergeVertices } from 'three/addons/utils/BufferGeometryUtils.js';
|
|
6
|
+
import { WRAP_PREVIEW_CONTROLS } from './wrap-preview-settings.js';
|
|
7
|
+
import { createWrapSkin } from './wrap-skin.js';
|
|
8
|
+
import { createBarycentricWireframeGeometry, createPreviewWireframeMaterial } from './wireframe.js';
|
|
9
|
+
import {
|
|
10
|
+
attribute, atan, color, float, mix, mx_fractal_noise_float, mx_noise_vec3, mx_worley_noise_vec2,
|
|
11
|
+
positionLocal, positionWorld, normalLocal, normalWorld, normalWorldGeometry, cameraPosition, cameraViewMatrix, modelWorldMatrixInverse,
|
|
12
|
+
smoothstep, transformNormalToView, uniform, time, varying, vec2, vec3, vec4,
|
|
13
|
+
} from 'three/tsl';
|
|
14
|
+
|
|
15
|
+
/** Ice normal streams adapted from pocket-sun/src/lib/frozenSpecimenIce.ts. */
|
|
16
|
+
/** @param {THREE.BufferGeometry} source */
|
|
17
|
+
export function prepareIceNormalAttributes(source) {
|
|
18
|
+
const positionOnly = new THREE.BufferGeometry();
|
|
19
|
+
positionOnly.setAttribute('position', source.getAttribute('position').clone());
|
|
20
|
+
if (source.index) positionOnly.setIndex(source.index.clone());
|
|
21
|
+
const welded = mergeVertices(positionOnly, 1e-5);
|
|
22
|
+
positionOnly.dispose();
|
|
23
|
+
welded.computeVertexNormals();
|
|
24
|
+
const wp = welded.getAttribute('position'), wn = welded.getAttribute('normal');
|
|
25
|
+
const key = (/** @type {number} */ x, /** @type {number} */ y, /** @type {number} */ z) => `${x.toFixed(4)}:${y.toFixed(4)}:${z.toFixed(4)}`;
|
|
26
|
+
const byPosition = new Map();
|
|
27
|
+
for (let i = 0; i < wp.count; i++) byPosition.set(key(wp.getX(i), wp.getY(i), wp.getZ(i)), [wn.getX(i), wn.getY(i), wn.getZ(i)]);
|
|
28
|
+
const geometry = source.index ? source.toNonIndexed() : source.clone();
|
|
29
|
+
geometry.computeVertexNormals();
|
|
30
|
+
const p = geometry.getAttribute('position'), n = geometry.getAttribute('normal');
|
|
31
|
+
const smooth = new Float32Array(p.count * 3);
|
|
32
|
+
for (let i = 0; i < p.count; i++) smooth.set(byPosition.get(key(p.getX(i), p.getY(i), p.getZ(i))) ?? [n.getX(i), n.getY(i), n.getZ(i)], i * 3);
|
|
33
|
+
geometry.setAttribute('iceSmoothNormal', new THREE.BufferAttribute(smooth, 3));
|
|
34
|
+
geometry.setAttribute('iceFacetNormal', n.clone());
|
|
35
|
+
welded.dispose();
|
|
36
|
+
return geometry;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Preview-only tessellation: continuous normals prevent cracks between flames.
|
|
40
|
+
* The baked/exported low-poly geometry is never modified.
|
|
41
|
+
* @param {THREE.BufferGeometry} source
|
|
42
|
+
*/
|
|
43
|
+
export function prepareAuraGeometry(source) {
|
|
44
|
+
const smooth = prepareIceNormalAttributes(source);
|
|
45
|
+
let positions = Array.from(smooth.getAttribute('position').array);
|
|
46
|
+
let normals = Array.from(smooth.getAttribute('iceSmoothNormal').array);
|
|
47
|
+
smooth.dispose();
|
|
48
|
+
for (let level = 0; level < 4 && positions.length / 9 * 4 <= 24000; level++) {
|
|
49
|
+
/** @type {number[]} */
|
|
50
|
+
const nextPositions = [], nextNormals = [];
|
|
51
|
+
for (let i = 0; i < positions.length; i += 9) {
|
|
52
|
+
const indices = [[0, 0], [0, 3], [6, 0], [0, 3], [3, 3], [3, 6], [6, 0], [3, 6], [6, 6], [0, 3], [3, 6], [6, 0]];
|
|
53
|
+
for (const [u, v] of indices) for (let axis = 0; axis < 3; axis++) {
|
|
54
|
+
nextPositions.push((positions[i + u + axis] + positions[i + v + axis]) * 0.5);
|
|
55
|
+
nextNormals.push((normals[i + u + axis] + normals[i + v + axis]) * 0.5);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
positions = nextPositions; normals = nextNormals;
|
|
59
|
+
}
|
|
60
|
+
const geometry = new THREE.BufferGeometry();
|
|
61
|
+
geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
|
|
62
|
+
geometry.setAttribute('normal', new THREE.Float32BufferAttribute(normals, 3));
|
|
63
|
+
geometry.normalizeNormals();
|
|
64
|
+
return geometry;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Open extrusion curtains, with no end caps or displaced surface patches.
|
|
68
|
+
* Adjacent face normals let the shader select only directional silhouette edges.
|
|
69
|
+
* @param {THREE.BufferGeometry} source
|
|
70
|
+
*/
|
|
71
|
+
export function prepareRayGeometry(source) {
|
|
72
|
+
const plain = new THREE.BufferGeometry();
|
|
73
|
+
plain.setAttribute('position', source.getAttribute('position').clone());
|
|
74
|
+
if (source.index) plain.setIndex(source.index.clone());
|
|
75
|
+
const welded = mergeVertices(plain, 1e-5);
|
|
76
|
+
plain.dispose();
|
|
77
|
+
const p = welded.getAttribute('position'), index = welded.getIndex();
|
|
78
|
+
if (!index) throw new Error('Ray silhouette requires indexed geometry.');
|
|
79
|
+
/** @type {Map<string, {a:number,b:number,n:THREE.Vector3,m?:THREE.Vector3}>} */
|
|
80
|
+
const edges = new Map();
|
|
81
|
+
for (let i = 0; i < index.count; i += 3) {
|
|
82
|
+
const ids = [index.getX(i), index.getX(i + 1), index.getX(i + 2)];
|
|
83
|
+
const points = ids.map(id => new THREE.Vector3().fromBufferAttribute(p, id));
|
|
84
|
+
const n = points[1].clone().sub(points[0]).cross(points[2].clone().sub(points[0])).normalize();
|
|
85
|
+
for (let e = 0; e < 3; e++) {
|
|
86
|
+
const a = ids[e], b = ids[(e + 1) % 3], key = `${Math.min(a,b)}:${Math.max(a,b)}`;
|
|
87
|
+
const edge = edges.get(key);
|
|
88
|
+
if (edge) edge.m = n; else edges.set(key, { a, b, n });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const positions = [], normals = [], adjacent = [], progress = [], tangents = [];
|
|
92
|
+
for (const edge of edges.values()) {
|
|
93
|
+
const tangent = new THREE.Vector3().fromBufferAttribute(p, edge.b).sub(new THREE.Vector3().fromBufferAttribute(p, edge.a)).normalize();
|
|
94
|
+
for (const [id, t] of [[edge.a,0],[edge.b,0],[edge.a,1],[edge.b,0],[edge.b,1],[edge.a,1]]) {
|
|
95
|
+
positions.push(p.getX(id), p.getY(id), p.getZ(id));
|
|
96
|
+
normals.push(...edge.n.toArray());
|
|
97
|
+
adjacent.push(...(edge.m ?? edge.n.clone().negate()).toArray());
|
|
98
|
+
progress.push(t);
|
|
99
|
+
tangents.push(...tangent.toArray());
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
welded.dispose();
|
|
103
|
+
const geometry = new THREE.BufferGeometry();
|
|
104
|
+
geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
|
|
105
|
+
geometry.setAttribute('normal', new THREE.Float32BufferAttribute(normals, 3));
|
|
106
|
+
geometry.setAttribute('rayAdjacentNormal', new THREE.Float32BufferAttribute(adjacent, 3));
|
|
107
|
+
geometry.setAttribute('rayProgress', new THREE.Float32BufferAttribute(progress, 1));
|
|
108
|
+
geometry.setAttribute('rayTangent', new THREE.Float32BufferAttribute(tangents, 3));
|
|
109
|
+
return geometry;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Borrowed originals are never mutated or disposed. All generated geometry,
|
|
114
|
+
* materials, and instances belong to this effect, not the GLB. Call `dispose()`
|
|
115
|
+
* before unloading the source or wrap. `update(values)` returns false when a
|
|
116
|
+
* compile-time shader variant changed; create a new effect in that case.
|
|
117
|
+
* @param {THREE.Object3D} asset
|
|
118
|
+
* @param {THREE.Object3D} original
|
|
119
|
+
* @param {string} mode
|
|
120
|
+
* @param {Record<string, number>} values
|
|
121
|
+
* @param {{animate?: boolean,
|
|
122
|
+
* geometry?: (source: THREE.BufferGeometry) => THREE.BufferGeometry,
|
|
123
|
+
* material?: (source: THREE.Material, impostor: boolean, width: number, blend: 'white') => MeshBasicNodeMaterial}=} options
|
|
124
|
+
*/
|
|
125
|
+
export function createWrapPreview(asset, original, mode, values, options = {}) {
|
|
126
|
+
const controls = mode === 'control' ? [] : WRAP_PREVIEW_CONTROLS[mode];
|
|
127
|
+
if (!controls) throw new Error(`Unknown wrap preview: ${mode}`);
|
|
128
|
+
/** @param {Record<string, number>} input */
|
|
129
|
+
const validatedValues = input => Object.fromEntries(controls.map(control => [control.key,
|
|
130
|
+
Number.isFinite(input[control.key]) ? THREE.MathUtils.clamp(input[control.key], control.min, control.max) : control.value,
|
|
131
|
+
]));
|
|
132
|
+
values = validatedValues(values);
|
|
133
|
+
const group = new THREE.Group();
|
|
134
|
+
group.name = `Wrap preview: ${mode}`;
|
|
135
|
+
group.userData.wrapPreview = true;
|
|
136
|
+
/** @type {Set<THREE.BufferGeometry>} */
|
|
137
|
+
const geometries = new Set();
|
|
138
|
+
/** @type {Set<THREE.Material>} */
|
|
139
|
+
const materials = new Set();
|
|
140
|
+
/** @type {Map<THREE.Mesh, boolean>} */
|
|
141
|
+
const visibility = new Map();
|
|
142
|
+
const uniforms = Object.fromEntries(Object.entries(values).map(([key, value]) => [key, uniform(value)]));
|
|
143
|
+
const effectColor = uniform(new THREE.Color(values.color ?? 0xffffff));
|
|
144
|
+
const borderColor = uniform(new THREE.Color(values.borderColor ?? 0x000000));
|
|
145
|
+
const warmColor = uniform(new THREE.Color(values.warmColor ?? 0xffb52a));
|
|
146
|
+
const hotColor = uniform(new THREE.Color(values.hotColor ?? 0xfff4d6));
|
|
147
|
+
const dispersionEnabled = mode === 'ice' && values.dispersion > 0;
|
|
148
|
+
const fieldNoiseEnabled = mode === 'force-field' && values.noise > 0;
|
|
149
|
+
const growthNoiseEnabled = (mode === 'ivy' || mode === 'lava') && values.noise > 0;
|
|
150
|
+
const hazeNoiseEnabled = mode === 'heat-haze' && values.strength > 0;
|
|
151
|
+
const shroudCurlEnabled = mode === 'shroud' && values.amplitude > 0;
|
|
152
|
+
asset.updateWorldMatrix(true, true);
|
|
153
|
+
original.updateWorldMatrix(true, true);
|
|
154
|
+
const inverse = asset.matrixWorld.clone().invert();
|
|
155
|
+
/** @type {THREE.BufferGeometry[]} */
|
|
156
|
+
const shells = [];
|
|
157
|
+
asset.traverse(object => {
|
|
158
|
+
if (!(object instanceof THREE.Mesh)) return;
|
|
159
|
+
visibility.set(object, object.visible);
|
|
160
|
+
if (object.userData.previewWireframeOverlay) return;
|
|
161
|
+
const geometry = object.geometry.clone().applyMatrix4(inverse.clone().multiply(object.matrixWorld));
|
|
162
|
+
shells.push(geometry);
|
|
163
|
+
});
|
|
164
|
+
const bounds = new THREE.Box3();
|
|
165
|
+
for (const shell of shells) { shell.computeBoundingBox(); if (shell.boundingBox) bounds.union(shell.boundingBox); }
|
|
166
|
+
const center = uniform(bounds.getCenter(new THREE.Vector3()));
|
|
167
|
+
const size = Math.max(bounds.getSize(new THREE.Vector3()).length(), 1e-5);
|
|
168
|
+
const normalized = positionLocal.sub(center).div(size);
|
|
169
|
+
const sourceCopy = cloneSkeleton(original);
|
|
170
|
+
sourceCopy.name = 'Original inside wrap';
|
|
171
|
+
if (mode === 'control') sourceCopy.visible = false;
|
|
172
|
+
sourceCopy.matrix.copy(inverse.clone().multiply(original.matrixWorld));
|
|
173
|
+
sourceCopy.matrixAutoUpdate = false;
|
|
174
|
+
const skin = createWrapSkin(original, sourceCopy, asset, options.animate);
|
|
175
|
+
/** @type {THREE.Object3D[]} */
|
|
176
|
+
const helpers = [];
|
|
177
|
+
sourceCopy.traverse(object => { if (object.userData.previewWireframeOverlay || object.userData.previewGaussianSplat || object.userData.wrapPreview) helpers.push(object); });
|
|
178
|
+
helpers.forEach(object => object.removeFromParent());
|
|
179
|
+
if (mode === 'godrays') sourceCopy.traverse(object => {
|
|
180
|
+
if (!(object instanceof THREE.Mesh)) return;
|
|
181
|
+
// The faded floor is transparent, so all three layers must share its
|
|
182
|
+
// render list: floor, rays, then the original (including its front faces).
|
|
183
|
+
object.renderOrder = 100;
|
|
184
|
+
const cloneMaterial = (/** @type {THREE.Material} */ source) => {
|
|
185
|
+
const copy = source.clone();
|
|
186
|
+
copy.transparent = true;
|
|
187
|
+
materials.add(copy);
|
|
188
|
+
return copy;
|
|
189
|
+
};
|
|
190
|
+
object.material = Array.isArray(object.material) ? object.material.map(cloneMaterial) : cloneMaterial(object.material);
|
|
191
|
+
});
|
|
192
|
+
group.add(sourceCopy);
|
|
193
|
+
|
|
194
|
+
// Construct only the selected graph. In particular, curl and transmission
|
|
195
|
+
// are absent from the other modes, not hidden behind runtime uniforms.
|
|
196
|
+
const clock = time.mul(uniforms.speed ?? 1);
|
|
197
|
+
/** @param {import('three/webgpu').Node<'vec3'>} p */
|
|
198
|
+
const curl = (p) => {
|
|
199
|
+
const q = p.sub(center).div(size).mul(mode === 'force-field' || mode === 'shroud' ? uniforms.detail : 4).add(vec3(0, clock.mul(0.3), clock.mul(0.2)));
|
|
200
|
+
const dx = mx_noise_vec3(q.add(vec3(0.025, 0, 0))).sub(mx_noise_vec3(q.sub(vec3(0.025, 0, 0))));
|
|
201
|
+
const dy = mx_noise_vec3(q.add(vec3(0, 0.025, 0))).sub(mx_noise_vec3(q.sub(vec3(0, 0.025, 0))));
|
|
202
|
+
const dz = mx_noise_vec3(q.add(vec3(0, 0, 0.025))).sub(mx_noise_vec3(q.sub(vec3(0, 0, 0.025))));
|
|
203
|
+
return vec3(dy.z.sub(dz.y), dz.x.sub(dx.z), dx.y.sub(dy.x)).div(0.05).mul(uniforms.amplitude ?? 0.035).mul(size * 0.25);
|
|
204
|
+
};
|
|
205
|
+
for (const shell of shells) {
|
|
206
|
+
let geometry = shell;
|
|
207
|
+
let material;
|
|
208
|
+
if (mode === 'control') {
|
|
209
|
+
material = new MeshStandardNodeMaterial({ color: 0xcccccc, roughness: 0.65, metalness: 0 });
|
|
210
|
+
} else if (mode === 'shroud') {
|
|
211
|
+
geometry = prepareAuraGeometry(shell);
|
|
212
|
+
shell.dispose();
|
|
213
|
+
for (let layer = 0; layer < 2; layer++) {
|
|
214
|
+
const cloud = new MeshBasicNodeMaterial({ transparent: true, depthWrite: false, side: THREE.FrontSide });
|
|
215
|
+
const rest = attribute('position', /** @type {const} */ ('vec3'));
|
|
216
|
+
const q = rest.sub(center).div(size).mul(uniforms.detail).add(layer * 8.31);
|
|
217
|
+
// Counter-rotating domain warps create eddies on the surface, not a
|
|
218
|
+
// translating vertical flame field. Rest coordinates also follow skin.
|
|
219
|
+
const flow = mx_noise_vec3(q.mul(0.8).add(vec3(clock.mul(0.27), 0, clock.mul(-0.19))));
|
|
220
|
+
const density = mx_fractal_noise_float(q.add(flow.mul(1.8)).add(vec3(0, clock.mul(-0.12), 0)), 4).mul(0.5).add(0.5);
|
|
221
|
+
const n = normalLocal.normalize();
|
|
222
|
+
const cloudPosition = positionLocal.add(n.mul(uniforms.depth).mul(size).mul(density.mul(0.6).add(layer * 0.45 + 0.12)));
|
|
223
|
+
// Full 3D circulation, including normal motion, rather than flattening
|
|
224
|
+
// the curl into the tangent plane. The zero variant omits all six samples.
|
|
225
|
+
cloud.positionNode = shroudCurlEnabled
|
|
226
|
+
? cloudPosition.add(curl(rest.add(vec3(layer * size * 0.37, 0, 0))))
|
|
227
|
+
: cloudPosition;
|
|
228
|
+
const threshold = float(1).sub(uniforms.coverage);
|
|
229
|
+
const fog = density.smoothstep(threshold.sub(0.18), threshold.add(0.2));
|
|
230
|
+
cloud.colorNode = effectColor.mul(density.mul(0.65).add(0.35));
|
|
231
|
+
const facing = normalWorldGeometry.normalize().dot(cameraPosition.sub(positionWorld).normalize()).abs().clamp(0.0001, 1);
|
|
232
|
+
const edgeFade = facing.pow(uniforms.fresnel);
|
|
233
|
+
cloud.opacityNode = fog.mul(uniforms.opacity).mul(uniforms.coverage.smoothstep(0, 0.08)).mul(edgeFade);
|
|
234
|
+
if (layer === 0) material = cloud;
|
|
235
|
+
else {
|
|
236
|
+
const outer = skin ? skin.mesh(geometry, cloud) : new THREE.Mesh(geometry, cloud);
|
|
237
|
+
outer.name = 'Shroud outer clouds'; outer.frustumCulled = false;
|
|
238
|
+
group.add(outer); materials.add(cloud);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
} else if (mode === 'heat-haze') {
|
|
242
|
+
geometry = prepareIceNormalAttributes(shell);
|
|
243
|
+
geometry.setAttribute('normal', geometry.getAttribute('iceSmoothNormal').clone());
|
|
244
|
+
shell.dispose();
|
|
245
|
+
material = new MeshPhysicalNodeMaterial({ transmission: 1, ior: 1.04, specularIntensity: 0, roughness: 0, thickness: size * 0.15, metalness: 0 });
|
|
246
|
+
if (hazeNoiseEnabled) {
|
|
247
|
+
const rest = varying(attribute('position', /** @type {const} */ ('vec3')), 'hazeRest');
|
|
248
|
+
const q = rest.sub(center).div(size).mul(uniforms.detail).sub(vec3(0, clock.mul(0.8), 0));
|
|
249
|
+
const bubbles = mx_noise_vec3(q.add(mx_noise_vec3(q.mul(0.6).add(clock.mul(0.25))).mul(0.6)));
|
|
250
|
+
material.normalNode = normalWorld.add(bubbles.mul(uniforms.strength)).normalize().transformDirection(cameraViewMatrix);
|
|
251
|
+
}
|
|
252
|
+
material.thicknessNode = uniforms.thickness.mul(size);
|
|
253
|
+
} else if (mode === 'lava') {
|
|
254
|
+
material = new MeshStandardNodeMaterial({ alphaTest: 0.5, roughness: 0.95 });
|
|
255
|
+
const rest = varying(attribute('position', /** @type {const} */ ('vec3')), 'lavaRest');
|
|
256
|
+
const q = rest.sub(center).div(size).mul(uniforms.detail);
|
|
257
|
+
const warped = q.add(mx_noise_vec3(q.mul(0.7).sub(vec3(0, clock.mul(0.35), 0))).mul(0.65));
|
|
258
|
+
const cells = mx_worley_noise_vec2(warped);
|
|
259
|
+
const gap = cells.y.sub(cells.x);
|
|
260
|
+
const seam = float(1).sub(gap.smoothstep(uniforms.cracks.mul(0.3), uniforms.cracks));
|
|
261
|
+
const core = float(1).sub(gap.smoothstep(0, uniforms.cracks.mul(0.35)));
|
|
262
|
+
const grain = mx_fractal_noise_float(q.mul(9), 3).mul(0.5).add(0.5).clamp(0, 1);
|
|
263
|
+
const flow = q.y.mul(3).sub(clock.mul(4)).add(mx_fractal_noise_float(q.mul(1.5), 2).mul(5));
|
|
264
|
+
const heat = flow.sin().mul(0.5).add(0.5).pow(2).mul(1.4).add(0.2);
|
|
265
|
+
const molten = mix(effectColor, color('#fff4a0'), core.mul(heat).clamp(0, 1));
|
|
266
|
+
material.colorNode = mix(mix(color('#100c0b'), color('#42312a'), grain), effectColor.mul(0.4), seam);
|
|
267
|
+
material.emissiveNode = molten.mul(seam).mul(heat).mul(uniforms.glow);
|
|
268
|
+
material.roughnessNode = mix(grain.mul(0.2).add(0.8), 0.32, seam);
|
|
269
|
+
const level = rest.y.sub(bounds.min.y).div(Math.max(bounds.max.y - bounds.min.y, 1e-5));
|
|
270
|
+
const fill = uniforms.fill.mul(uniforms.transition.add(1).add(uniforms.noise.mul(0.6))).sub(uniforms.noise.mul(0.3));
|
|
271
|
+
const irregularity = growthNoiseEnabled ? mx_fractal_noise_float(q.mul(0.45), 3).clamp(-1, 1).mul(uniforms.noise).mul(0.3) : float(0);
|
|
272
|
+
const age = fill.sub(level).sub(irregularity).div(uniforms.transition).clamp(0, 1);
|
|
273
|
+
// Opaque cellular islands expand from their centers, not a translucent Y slice.
|
|
274
|
+
const radius = cells.x.sqrt().mul(0.65).clamp(0, 0.95);
|
|
275
|
+
material.opacityNode = age.smoothstep(radius, radius.add(0.035));
|
|
276
|
+
const advancingEdge = float(1).sub(age.sub(radius).smoothstep(0.025, 0.13));
|
|
277
|
+
material.emissiveNode = molten.mul(seam.add(advancingEdge.mul(0.6))).mul(heat).mul(uniforms.glow);
|
|
278
|
+
material.positionNode = positionLocal.add(normalLocal.mul(size * 0.001));
|
|
279
|
+
} else if (mode === 'ivy') {
|
|
280
|
+
material = new MeshStandardNodeMaterial({ side: THREE.DoubleSide, alphaTest: 0.1, roughness: 0.7 });
|
|
281
|
+
// Analytic leaves, stems and veins: no generated geometry or UV atlas.
|
|
282
|
+
// Rest-space triplanar projection keeps the pattern attached to skin.
|
|
283
|
+
const rest = varying(attribute('position', /** @type {const} */ ('vec3')), 'ivyRest');
|
|
284
|
+
const q = rest.sub(center).div(size).mul(uniforms.detail);
|
|
285
|
+
const plant = (/** @type {import('three/webgpu').Node<'vec2'>} */ p, /** @type {'x'|'y'|'z'} */ axis) => {
|
|
286
|
+
const column = p.x.floor();
|
|
287
|
+
const seed = column.mul(127.1).sin().mul(43758.5453).fract();
|
|
288
|
+
const y = p.y.add(seed.mul(3));
|
|
289
|
+
const curve = (/** @type {import('three/webgpu').Node<'float'>} */ h) => h.mul(2.1).add(seed.mul(6.28)).sin().mul(0.13)
|
|
290
|
+
.add(h.mul(4.7).add(seed.mul(15)).sin().mul(0.035));
|
|
291
|
+
const x = p.x.fract().sub(0.5);
|
|
292
|
+
const row = y.mul(3).floor();
|
|
293
|
+
const rootY = row.add(0.25).div(3);
|
|
294
|
+
const rootX = column.add(0.5).add(curve(rootY));
|
|
295
|
+
const rootV = rootY.sub(seed.mul(3));
|
|
296
|
+
const root = (axis === 'x' ? vec3(q.x, rootV, rootX) : axis === 'y' ? vec3(rootX, q.y, rootV) : vec3(rootX, rootV, q.z)).div(uniforms.detail).mul(size).add(center);
|
|
297
|
+
const height = root.y.sub(bounds.min.y).div(Math.max(bounds.max.y - bounds.min.y, 1e-5));
|
|
298
|
+
const irregularity = growthNoiseEnabled ? mx_fractal_noise_float(root.sub(center).div(size).mul(5), 3).clamp(-1, 1).mul(uniforms.noise).mul(0.3) : float(0);
|
|
299
|
+
const front = uniforms.growth.mul(uniforms.transition.add(1).add(uniforms.noise.mul(0.6))).sub(uniforms.noise.mul(0.3));
|
|
300
|
+
const age = front.sub(height).sub(irregularity).div(uniforms.transition).clamp(0, 1);
|
|
301
|
+
const leafAge = age.smoothstep(0.2, 1);
|
|
302
|
+
const stemAge = front.sub(rest.y.sub(bounds.min.y).div(Math.max(bounds.max.y - bounds.min.y, 1e-5))).sub(irregularity).div(uniforms.transition).clamp(0, 1);
|
|
303
|
+
const stem = float(1).sub(x.sub(curve(y)).abs().smoothstep(stemAge.mul(0.012), stemAge.mul(0.025).add(0.0001))).mul(stemAge.smoothstep(0, 0.1));
|
|
304
|
+
const sign = row.mod(2).mul(2).sub(1);
|
|
305
|
+
const dx = x.sub(curve(rootY));
|
|
306
|
+
const dy = y.sub(rootY);
|
|
307
|
+
// Leaves alternate along a winding stem. Their bases coincide with it.
|
|
308
|
+
const longitudinal = dx.mul(sign).mul(0.8).add(dy.mul(0.6));
|
|
309
|
+
const across = dx.mul(sign).mul(-0.6).add(dy.mul(0.8));
|
|
310
|
+
const twigLength = age.smoothstep(0, 0.4).mul(0.065);
|
|
311
|
+
const twig = float(1).sub(across.abs().smoothstep(0.007, 0.013)).mul(longitudinal.smoothstep(0, 0.01))
|
|
312
|
+
.mul(float(1).sub(longitudinal.smoothstep(twigLength, twigLength.add(0.005)))).mul(age.smoothstep(0, 0.05));
|
|
313
|
+
const length = uniforms.leafSize.mul(seed.mul(0.1).add(0.38)).mul(leafAge.max(0.001));
|
|
314
|
+
const v = longitudinal.sub(twigLength).div(length);
|
|
315
|
+
const u = across.div(length);
|
|
316
|
+
const flutter = clock.mul(1.8).add(seed.mul(12)).add(row.mul(1.7)).sin().mul(0.025);
|
|
317
|
+
const leafX = u.add(v.mul(flutter));
|
|
318
|
+
// Three pointed lobes plus smaller basal lobes, rather than round dots.
|
|
319
|
+
const central = float(1).sub(v).mul(0.32);
|
|
320
|
+
const lateral = float(1).sub(v.sub(0.42).abs().div(0.2)).max(0).mul(0.48);
|
|
321
|
+
const basal = float(1).sub(v.sub(0.16).abs().div(0.13)).max(0).mul(0.32);
|
|
322
|
+
const width = central.max(lateral).max(basal).mul(v.smoothstep(0, 0.1)).mul(leafAge.mul(0.7).add(0.3));
|
|
323
|
+
const leaf = float(1).sub(leafX.abs().smoothstep(width.sub(0.012), width.add(0.012)))
|
|
324
|
+
.mul(v.smoothstep(0, 0.03)).mul(float(1).sub(v.smoothstep(0.97, 1))).mul(leafAge.smoothstep(0, 0.03));
|
|
325
|
+
const rib = float(1).sub(leafX.abs().smoothstep(0.007, 0.025));
|
|
326
|
+
const branches = v.mul(7).sub(leafX.abs().mul(5)).fract().sub(0.5).abs();
|
|
327
|
+
const veins = float(1).sub(branches.smoothstep(0.025, 0.08)).mul(0.4).max(rib);
|
|
328
|
+
const shading = leafX.mul(2).add(0.85).clamp(0.45, 1.2).mul(seed.mul(0.3).add(0.8));
|
|
329
|
+
return vec4(stem.max(twig).max(leaf), leaf, veins.mul(leaf), shading);
|
|
330
|
+
};
|
|
331
|
+
const n = varying(attribute('normal', /** @type {const} */ ('vec3')).normalize(), 'ivyRestNormal').abs().pow(8);
|
|
332
|
+
const weights = n.div(n.x.add(n.y).add(n.z).max(0.0001));
|
|
333
|
+
const pattern = plant(q.zy, 'x').mul(weights.x).add(plant(q.xz, 'y').mul(weights.y)).add(plant(q.xy, 'z').mul(weights.z));
|
|
334
|
+
const leaf = pattern.y;
|
|
335
|
+
const mottling = mx_fractal_noise_float(q.mul(18), 2).mul(0.12).add(0.88);
|
|
336
|
+
const green = effectColor.mul(mottling).mul(pattern.w);
|
|
337
|
+
material.colorNode = mix(color('#344519'), mix(green, color('#9fae59'), pattern.z.mul(0.5)), leaf);
|
|
338
|
+
material.roughnessNode = mix(0.95, 0.58, leaf);
|
|
339
|
+
material.opacityNode = pattern.x.mul(uniforms.growth.smoothstep(0, 0.001));
|
|
340
|
+
} else if (mode === 'ice') {
|
|
341
|
+
geometry = prepareIceNormalAttributes(shell);
|
|
342
|
+
shell.dispose();
|
|
343
|
+
material = new MeshPhysicalNodeMaterial({ name: 'Pocket Sun specimen ice', ior: 1.31, transmission: 1, thickness: size * 0.35, metalness: 0, clearcoat: 1, clearcoatRoughness: 0.08, attenuationColor: new THREE.Color('#bcebf5'), attenuationDistance: size * 3.2 });
|
|
344
|
+
if (dispersionEnabled) material.dispersionNode = uniforms.dispersion;
|
|
345
|
+
const noise = mx_fractal_noise_float(normalized.mul(5), 3).mul(0.5).add(0.5);
|
|
346
|
+
const frost = smoothstep(0.4, 0.68, noise).mul(uniforms.frost);
|
|
347
|
+
material.roughnessNode = mix(0.015, 0.92, frost);
|
|
348
|
+
material.transmissionNode = mix(0.995, 0.86, frost);
|
|
349
|
+
material.thicknessNode = uniforms.thickness.mul(size).mul(mix(0.65, 1.3, frost));
|
|
350
|
+
material.colorNode = mix(color('#ffffff'), color('#eefcff'), frost.mul(0.35));
|
|
351
|
+
material.normalNode = skin ? transformNormalToView(normalLocal)
|
|
352
|
+
: mix(transformNormalToView(attribute('iceSmoothNormal', /** @type {const} */ ('vec3'))), transformNormalToView(attribute('iceFacetNormal', /** @type {const} */ ('vec3'))), uniforms.facets).normalize();
|
|
353
|
+
} else if (mode === 'tech-cage') {
|
|
354
|
+
geometry = (options.geometry ?? createBarycentricWireframeGeometry)(shell);
|
|
355
|
+
const placeholder = new THREE.MeshBasicMaterial({ side: THREE.DoubleSide });
|
|
356
|
+
material = (options.material ?? createPreviewWireframeMaterial)(placeholder, false, values.width, 'white');
|
|
357
|
+
placeholder.dispose();
|
|
358
|
+
material.positionNode = positionLocal.add(curl(positionLocal));
|
|
359
|
+
material.colorNode = effectColor.mul(float(1.3).add(normalized.y.mul(14).sub(clock.mul(3)).sin().mul(0.7)));
|
|
360
|
+
const points = new Map();
|
|
361
|
+
const positions = shell.getAttribute('position');
|
|
362
|
+
for (let i = 0; i < positions.count; i++) {
|
|
363
|
+
const p = [positions.getX(i), positions.getY(i), positions.getZ(i)];
|
|
364
|
+
points.set(p.map(v => Math.round(v / size * 1e6)).join(':'), p);
|
|
365
|
+
}
|
|
366
|
+
const anchors = new Float32Array([...points.values()].flat());
|
|
367
|
+
const spheres = new THREE.IcosahedronGeometry(1, 1);
|
|
368
|
+
// IcosahedronGeometry supplies smooth spherical vertex normals.
|
|
369
|
+
spheres.setAttribute('wrapAnchor', new THREE.InstancedBufferAttribute(anchors, 3));
|
|
370
|
+
const nodeMaterial = new MeshStandardNodeMaterial({ color: '#a6f6ff', emissive: '#126879', emissiveIntensity: 0.6, roughness: 0.28, metalness: 0.4 });
|
|
371
|
+
nodeMaterial.colorNode = effectColor;
|
|
372
|
+
nodeMaterial.emissiveNode = effectColor.mul(0.3);
|
|
373
|
+
const anchor = attribute('wrapAnchor', /** @type {const} */ ('vec3'));
|
|
374
|
+
nodeMaterial.positionNode = positionLocal.mul(uniforms.nodeSize).mul(size).add(anchor).add(curl(anchor));
|
|
375
|
+
let instances;
|
|
376
|
+
if (skin) {
|
|
377
|
+
// Native instancing cannot carry an independent skin per instance.
|
|
378
|
+
// One merged skinned draw keeps all node centers on the same rig.
|
|
379
|
+
const normals = spheres.getAttribute('normal');
|
|
380
|
+
const positions = new Float32Array(points.size * normals.count * 3);
|
|
381
|
+
const nodeNormals = new Float32Array(positions.length);
|
|
382
|
+
for (let i = 0; i < points.size; i++) for (let v = 0; v < normals.count; v++) {
|
|
383
|
+
const offset = (i * normals.count + v) * 3;
|
|
384
|
+
positions.set(anchors.subarray(i * 3, i * 3 + 3), offset);
|
|
385
|
+
nodeNormals.set([normals.getX(v), normals.getY(v), normals.getZ(v)], offset);
|
|
386
|
+
}
|
|
387
|
+
const nodes = new THREE.BufferGeometry();
|
|
388
|
+
nodes.setAttribute('position', new THREE.BufferAttribute(positions, 3));
|
|
389
|
+
nodes.setAttribute('normal', new THREE.BufferAttribute(nodeNormals, 3));
|
|
390
|
+
nodeMaterial.positionNode = positionLocal.add(normalLocal.mul(uniforms.nodeSize).mul(size)).add(curl(positionLocal));
|
|
391
|
+
instances = skin.mesh(nodes, nodeMaterial);
|
|
392
|
+
geometries.add(nodes);
|
|
393
|
+
} else {
|
|
394
|
+
instances = new THREE.InstancedMesh(spheres, nodeMaterial, points.size);
|
|
395
|
+
const identity = new THREE.Matrix4();
|
|
396
|
+
for (let i = 0; i < points.size; i++) instances.setMatrixAt(i, identity);
|
|
397
|
+
}
|
|
398
|
+
instances.frustumCulled = false;
|
|
399
|
+
instances.name = 'Tech cage vertex nodes';
|
|
400
|
+
group.add(instances);
|
|
401
|
+
geometries.add(spheres); materials.add(nodeMaterial);
|
|
402
|
+
shell.dispose();
|
|
403
|
+
} else if (mode === 'force-field') {
|
|
404
|
+
geometry = prepareAuraGeometry(shell);
|
|
405
|
+
shell.dispose();
|
|
406
|
+
/** @param {number} layer */
|
|
407
|
+
const fieldMaterial = layer => {
|
|
408
|
+
const field = new MeshBasicNodeMaterial({ name: `Biplanar force field ${layer}`, transparent: true, depthWrite: false, side: THREE.DoubleSide, blending: THREE.AdditiveBlending, toneMapped: false });
|
|
409
|
+
const n = skin ? normalLocal : attribute('normal', /** @type {const} */ ('vec3'));
|
|
410
|
+
const displacement = fieldNoiseEnabled
|
|
411
|
+
? curl(positionLocal.add(vec3(size * layer * 0.37, 0, 0))).dot(n).mul(uniforms.noise)
|
|
412
|
+
: float(0);
|
|
413
|
+
const weights = varying(n.abs(), `fieldNormal${layer}`).normalize().abs();
|
|
414
|
+
const p = normalized.mul(uniforms.density).add(vec3(layer * 0.43, 0, layer * 0.29));
|
|
415
|
+
// Choose the two strongest projection axes before evaluating the grid:
|
|
416
|
+
// exactly two planar evaluations, no longitude singularity at poles.
|
|
417
|
+
const xMajor = weights.x.greaterThanEqual(weights.y).and(weights.x.greaterThanEqual(weights.z));
|
|
418
|
+
const yMajor = weights.y.greaterThanEqual(weights.z);
|
|
419
|
+
const first = xMajor.select(p.yz, yMajor.select(p.xz, p.xy));
|
|
420
|
+
const second = xMajor.select(weights.y.greaterThan(weights.z).select(p.xz, p.xy),
|
|
421
|
+
yMajor.select(weights.x.greaterThan(weights.z).select(p.yz, p.xy), weights.x.greaterThan(weights.y).select(p.yz, p.xz)));
|
|
422
|
+
const largest = weights.x.max(weights.y).max(weights.z);
|
|
423
|
+
const smallest = weights.x.min(weights.y).min(weights.z);
|
|
424
|
+
const middle = weights.x.add(weights.y).add(weights.z).sub(largest).sub(smallest);
|
|
425
|
+
const blend = middle.pow(8).div(largest.pow(8).add(middle.pow(8)).max(0.00001));
|
|
426
|
+
/** @param {import('three/webgpu').Node<'vec2'>} grid */
|
|
427
|
+
const hex = grid => {
|
|
428
|
+
const period = vec2(1.7320508, 1);
|
|
429
|
+
const a = grid.mod(period).sub(period.mul(0.5));
|
|
430
|
+
const b = grid.sub(period.mul(0.5)).mod(period).sub(period.mul(0.5));
|
|
431
|
+
const cell = b.dot(b).lessThan(a.dot(a)).select(b, a);
|
|
432
|
+
const edge = cell.abs().dot(vec2(0.8660254, 0.5)).max(cell.y.abs());
|
|
433
|
+
// Quantize lattice IDs before hashing: grid - cell alone retains
|
|
434
|
+
// subpixel cancellation error, which turns a high-gain hash to speckle.
|
|
435
|
+
const cellCenter = grid.sub(cell).div(period).mul(2).round();
|
|
436
|
+
const seed = cellCenter.dot(vec2(127.1, 311.7)).sin().mul(43758.5453).fract();
|
|
437
|
+
const pulse = clock.mul(2.4).add(seed.mul(Math.PI * 2)).sin().mul(0.5).add(0.5).pow(5);
|
|
438
|
+
// Sample at hex centers, so complete cells activate together rather
|
|
439
|
+
// than having a continuous noise wash painted across the lattice.
|
|
440
|
+
const patch = fieldNoiseEnabled
|
|
441
|
+
? mx_fractal_noise_float(vec3(cellCenter.mul(uniforms.detail).mul(0.07), clock.mul(0.35).add(layer * 4)), 2)
|
|
442
|
+
.mul(uniforms.noise).mul(0.5).add(0.5).smoothstep(0.52, 0.68)
|
|
443
|
+
: seed.smoothstep(0.55, 0.7);
|
|
444
|
+
return vec3(smoothstep(0.405, 0.475, edge), pulse, patch);
|
|
445
|
+
};
|
|
446
|
+
const cells = mix(hex(first), hex(second), blend);
|
|
447
|
+
const lines = cells.x, pulse = cells.y;
|
|
448
|
+
field.positionNode = positionLocal.add(n.mul(displacement.mul(pulse.mul(0.7).add(0.3))
|
|
449
|
+
.add(pulse.mul(size * 0.006)).add(size * (0.008 + layer * 0.018))));
|
|
450
|
+
const rim = float(1).sub(normalWorld.dot(cameraPosition.sub(positionWorld).normalize()).abs()).clamp(0, 1).pow(uniforms.fresnel);
|
|
451
|
+
field.colorNode = mix(effectColor, color('#ffffff'), pulse.mul(0.35)).mul(uniforms.glow);
|
|
452
|
+
const energy = lines.mul(pulse.mul(0.65).add(0.14)).add(pulse.mul(0.045)).add(rim.mul(0.12));
|
|
453
|
+
// Mask every contribution, including Fresnel, on the outer shell.
|
|
454
|
+
// Otherwise its continuous rim still reveals the entire second layer.
|
|
455
|
+
field.opacityNode = energy.mul(layer ? cells.z.mul(0.85) : float(1)).clamp(0, 1);
|
|
456
|
+
return field;
|
|
457
|
+
};
|
|
458
|
+
material = fieldMaterial(0);
|
|
459
|
+
const outerMaterial = fieldMaterial(1);
|
|
460
|
+
const outer = skin ? skin.mesh(geometry, outerMaterial) : new THREE.Mesh(geometry, outerMaterial);
|
|
461
|
+
outer.name = 'Force field outer layer';
|
|
462
|
+
outer.frustumCulled = false;
|
|
463
|
+
group.add(outer);
|
|
464
|
+
materials.add(outerMaterial);
|
|
465
|
+
} else if (mode === 'godrays' || mode === 'wind-frosted') {
|
|
466
|
+
geometry = mode === 'godrays' ? prepareRayGeometry(shell) : prepareAuraGeometry(shell);
|
|
467
|
+
shell.dispose();
|
|
468
|
+
const yaw = uniforms.azimuth.mul(Math.PI / 180), pitch = uniforms.elevation.mul(Math.PI / 180);
|
|
469
|
+
const direction = vec3(yaw.sin().mul(pitch.cos()), pitch.sin(), yaw.cos().mul(pitch.cos()));
|
|
470
|
+
const localDirection = modelWorldMatrixInverse.mul(vec4(direction, 0)).xyz;
|
|
471
|
+
if (mode === 'godrays') {
|
|
472
|
+
// Draw blended curtains before the original opaque model, so even rays
|
|
473
|
+
// pointing toward the camera can never paint across its front surface.
|
|
474
|
+
material = new MeshBasicNodeMaterial({ name: 'Multiplicative dark rays', transparent: true, depthTest: false, depthWrite: false, side: THREE.DoubleSide, toneMapped: false });
|
|
475
|
+
const progress = attribute('rayProgress', /** @type {const} */ ('float'));
|
|
476
|
+
const along = varying(progress, 'rayDistance');
|
|
477
|
+
const d = localDirection.normalize();
|
|
478
|
+
const n = attribute('normal', /** @type {const} */ ('vec3')), other = attribute('rayAdjacentNormal', /** @type {const} */ ('vec3'));
|
|
479
|
+
const facing = n.dot(d), adjacentFacing = other.dot(d);
|
|
480
|
+
// Include tangent neighbors: exact axis-aligned directions otherwise
|
|
481
|
+
// lose every silhouette edge on a box or a flat-sided wrap.
|
|
482
|
+
const transition = n.sub(other).length().mul(0.15).clamp(0.005, 0.08);
|
|
483
|
+
const silhouette = varying(smoothstep(transition.negate(), transition, facing)
|
|
484
|
+
.sub(smoothstep(transition.negate(), transition, adjacentFacing)).abs(), 'raySilhouette');
|
|
485
|
+
const radial = normalized.sub(d.mul(normalized.dot(d)));
|
|
486
|
+
const spread = radial.mul(uniforms.angle.mul(Math.PI / 180).tan());
|
|
487
|
+
const extrusion = d.add(spread).mul(progress).mul(uniforms.length).mul(size);
|
|
488
|
+
material.positionNode = positionLocal.add(extrusion);
|
|
489
|
+
const curtainNormal = varying(attribute('rayTangent', /** @type {const} */ ('vec3')).cross(d.add(spread)).normalize(), 'rayCurtainNormal');
|
|
490
|
+
const localView = modelWorldMatrixInverse.mul(vec4(cameraPosition.sub(positionWorld), 0)).xyz.normalize();
|
|
491
|
+
const sideFade = curtainNormal.normalize().dot(localView).abs().pow(uniforms.fresnel).mul(0.85).add(0.15);
|
|
492
|
+
// Black alpha compositing is destination * (1 - alpha): multiplicative
|
|
493
|
+
// darkening, with a soft grazing-angle falloff rather than hard sides.
|
|
494
|
+
material.colorNode = color('#000000');
|
|
495
|
+
material.opacityNode = silhouette.mul(float(1).sub(smoothstep(0, 1, along))).mul(sideFade).mul(uniforms.opacity);
|
|
496
|
+
} else {
|
|
497
|
+
material = new MeshPhysicalNodeMaterial({ name: 'Directional wind frost', transmission: 1, ior: 1.31, thickness: size * 0.1, metalness: 0 });
|
|
498
|
+
const noise = mx_fractal_noise_float(normalized.mul(uniforms.scale), 3).mul(0.5).add(0.5);
|
|
499
|
+
// Evaluate exposure on the undisplaced smooth surface; shading normals
|
|
500
|
+
// from stretched triangles otherwise stamp the tessellation into frost.
|
|
501
|
+
const exposure = varying(attribute('normal', /** @type {const} */ ('vec3')).dot(localDirection.normalize().negate()).mul(0.5).add(0.5), 'frostExposure');
|
|
502
|
+
const frost = exposure.add(noise.sub(0.5).mul(0.25)).smoothstep(float(1).sub(uniforms.coverage), float(1.15).sub(uniforms.coverage));
|
|
503
|
+
const q = normalized.mul(uniforms.scale).mul(3);
|
|
504
|
+
const tips = mx_fractal_noise_float(q, 3).mul(0.5).add(0.5).smoothstep(0.35, 0.8).pow(3);
|
|
505
|
+
// Only leeward/outward-facing vertices may extend downwind. Pulling
|
|
506
|
+
// windward vertices through the source made apparent holes in the ice.
|
|
507
|
+
const outward = attribute('normal', /** @type {const} */ ('vec3')).dot(localDirection.normalize()).smoothstep(0, 0.35);
|
|
508
|
+
const icicles = uniforms.coverage.mul(outward);
|
|
509
|
+
const windward = attribute('normal', /** @type {const} */ ('vec3')).dot(localDirection.normalize().negate()).smoothstep(0, 0.5);
|
|
510
|
+
material.positionNode = positionLocal.add(localDirection.mul(tips).mul(icicles).mul(uniforms.height).mul(size))
|
|
511
|
+
.add(attribute('normal', /** @type {const} */ ('vec3')).mul(windward).mul(uniforms.normalOffset).mul(size));
|
|
512
|
+
material.colorNode = mix(color('#cceeff'), color('#ffffff'), frost);
|
|
513
|
+
material.roughnessNode = mix(0.035, 0.95, frost);
|
|
514
|
+
material.transmissionNode = float(1).sub(frost);
|
|
515
|
+
}
|
|
516
|
+
} else if (mode === 'outline') {
|
|
517
|
+
geometry = prepareIceNormalAttributes(shell);
|
|
518
|
+
shell.dispose();
|
|
519
|
+
geometry.setAttribute('normal', geometry.getAttribute('iceSmoothNormal'));
|
|
520
|
+
material = new MeshBasicNodeMaterial({ name: 'Solid inner outline', side: THREE.BackSide, toneMapped: false });
|
|
521
|
+
material.colorNode = effectColor;
|
|
522
|
+
const normal = skin ? normalLocal : attribute('normal', /** @type {const} */ ('vec3'));
|
|
523
|
+
material.positionNode = positionLocal.add(normal.mul(uniforms.expand).mul(size));
|
|
524
|
+
const outerMaterial = new MeshBasicNodeMaterial({ name: 'Solid outer outline', side: THREE.BackSide, toneMapped: false });
|
|
525
|
+
outerMaterial.colorNode = borderColor;
|
|
526
|
+
outerMaterial.positionNode = positionLocal.add(normal.mul(uniforms.expand.add(uniforms.borderOffset)).mul(size));
|
|
527
|
+
const outer = skin ? skin.mesh(geometry, outerMaterial) : new THREE.Mesh(geometry, outerMaterial);
|
|
528
|
+
outer.name = 'Outline outer shell';
|
|
529
|
+
outer.frustumCulled = false;
|
|
530
|
+
group.add(outer);
|
|
531
|
+
materials.add(outerMaterial);
|
|
532
|
+
} else if (mode === 'flames' || mode === 'aura') {
|
|
533
|
+
geometry = prepareAuraGeometry(shell);
|
|
534
|
+
shell.dispose();
|
|
535
|
+
material = new MeshBasicNodeMaterial({ name: mode === 'aura' ? 'Spiky golden aura' : 'Rising golden flames', side: THREE.BackSide, transparent: true, depthWrite: false, blending: mode === 'aura' ? THREE.NormalBlending : THREE.AdditiveBlending, toneMapped: false });
|
|
536
|
+
// BackSide flips shading normals, including in the vertex stage. Use
|
|
537
|
+
// geometric normals so the top, never the underside, seeds the flames.
|
|
538
|
+
const worldBounds = bounds.clone().applyMatrix4(asset.matrixWorld);
|
|
539
|
+
const floorPoint = worldBounds.getCenter(new THREE.Vector3());
|
|
540
|
+
floorPoint.y = worldBounds.min.y;
|
|
541
|
+
const fromFloor = positionWorld.sub(uniform(floorPoint));
|
|
542
|
+
const radialUp = vec3(fromFloor.x, fromFloor.y.max(size * 1e-6), fromFloor.z).normalize();
|
|
543
|
+
const direction = mix(vec3(0, 1, 0), radialUp, uniforms.radialUp).normalize();
|
|
544
|
+
const up = smoothstep(uniforms.upThreshold, uniforms.upThreshold.add(0.25), normalWorldGeometry.dot(direction));
|
|
545
|
+
const localUp = modelWorldMatrixInverse.mul(vec4(direction, 0)).xyz;
|
|
546
|
+
if (mode === 'aura') {
|
|
547
|
+
// Piecewise-linear pyramids have pointed tips and angular flanks at
|
|
548
|
+
// every animation state; no rounded noise lobes or noise color wash.
|
|
549
|
+
// Advect the entire irregular field upward, not just the amplitude of
|
|
550
|
+
// fixed columns. The low-amplitude body wave moves the inner shell too.
|
|
551
|
+
// Advect latitude, not Cartesian height: a constant pattern phase
|
|
552
|
+
// moves to smaller polar angles, climbing the sides and converging
|
|
553
|
+
// inward over the shoulders toward the crown. Azimuth stays fixed.
|
|
554
|
+
const centered = positionWorld.sub(uniform(worldBounds.getCenter(new THREE.Vector3())));
|
|
555
|
+
const radius = centered.xz.length().max(size * 0.00001);
|
|
556
|
+
const polar = atan(radius, centered.y);
|
|
557
|
+
const latitude = polar.add(time.mul(uniforms.wanderSpeed).mul(0.35));
|
|
558
|
+
const travelling = vec3(centered.x.div(radius).mul(latitude.sin()), latitude.cos(), centered.z.div(radius).mul(latitude.sin())).mul(0.5);
|
|
559
|
+
const field = travelling.mul(uniforms.scale);
|
|
560
|
+
const warp = mx_noise_vec3(field.mul(1.7)).mul(0.65);
|
|
561
|
+
const q = field.mul(5).add(warp);
|
|
562
|
+
const x = float(1).sub(q.x.fract().mul(2).sub(1).abs());
|
|
563
|
+
const z = float(1).sub(q.z.fract().mul(2).sub(1).abs());
|
|
564
|
+
const spikes = x.min(z).mul(1.8).sub(0.8).max(0);
|
|
565
|
+
const irregular = mx_fractal_noise_float(field.add(vec3(0, clock.mul(0.2), 0)), 3).mul(0.5).add(0.5).clamp(0, 1);
|
|
566
|
+
const pulse = float(1).sub(q.y.sub(clock.mul(0.7)).fract().mul(2).sub(1).abs()).pow(2);
|
|
567
|
+
const body = irregular.mul(0.16).add(pulse.mul(0.08));
|
|
568
|
+
const height = spikes.mul(irregular.mul(1.4).add(0.2)).mul(pulse.mul(0.9).add(0.1));
|
|
569
|
+
// A coherent, low-frequency bend climbs from the floor to the top.
|
|
570
|
+
// Its crest moves upward as time increases, independently of the
|
|
571
|
+
// high-frequency spike detail (which previously hid the travel).
|
|
572
|
+
const phase = fromFloor.y.div(size).mul(Math.PI * 4).sub(time.mul(uniforms.bendSpeed).mul(3));
|
|
573
|
+
const bend = phase.sin().mul(0.5).add(0.5).pow(2);
|
|
574
|
+
const shellNormal = skin ? normalLocal : attribute('normal', /** @type {const} */ ('vec3'));
|
|
575
|
+
const bendDirection = shellNormal.mul(bend.mul(0.55))
|
|
576
|
+
.add(localUp.mul(phase.cos().mul(0.18)));
|
|
577
|
+
material.positionNode = positionLocal.add(localUp.mul(height.add(body)).mul(up).mul(uniforms.height).mul(size))
|
|
578
|
+
.add(shellNormal.mul(body).mul(uniforms.height).mul(size * 0.3))
|
|
579
|
+
.add(bendDirection.mul(uniforms.bendStrength).mul(uniforms.height).mul(size));
|
|
580
|
+
const rim = float(1).sub(normalWorldGeometry.dot(cameraPosition.sub(positionWorld).normalize()).abs()).clamp(0, 1).pow(uniforms.fresnel);
|
|
581
|
+
material.colorNode = effectColor;
|
|
582
|
+
material.opacityNode = rim.mul(0.85).add(0.15).mul(uniforms.opacity);
|
|
583
|
+
} else {
|
|
584
|
+
// Narrow columns advect upwards; smooth eligibility preserves the shell
|
|
585
|
+
// across triangle boundaries even on a 100-triangle source wrap.
|
|
586
|
+
const sourceNormalized = attribute('position', /** @type {const} */ ('vec3')).sub(center).div(size);
|
|
587
|
+
const noise = mx_fractal_noise_float(sourceNormalized.mul(vec3(2.5, 0.7, 2.5)).mul(uniforms.scale).sub(vec3(0, clock.mul(2), 0)), 3).mul(0.5).add(0.5);
|
|
588
|
+
const flame = noise.smoothstep(0.25, 0.8).pow(2).mul(up);
|
|
589
|
+
material.positionNode = positionLocal.add(localUp.mul(flame).mul(uniforms.height).mul(size));
|
|
590
|
+
// Interpolate travel from the vertex stage, measured from each local
|
|
591
|
+
// emitting surface, not world height or noise at the displaced tip.
|
|
592
|
+
// Stylized heat proxy: warm ignition -> hot reaction band -> cooling
|
|
593
|
+
// plume. It deliberately does not equate displacement with heat.
|
|
594
|
+
const travel = varying(flame.div(up.max(0.001)).clamp(0, 1), 'flameTravel');
|
|
595
|
+
const emitter = varying(up, 'flameEmitter');
|
|
596
|
+
const ignition = mix(0.6, 1, smoothstep(0, 0.12, travel));
|
|
597
|
+
const cooling = float(1).sub(smoothstep(0.15, 0.95, travel));
|
|
598
|
+
const sourcePosition = varying(attribute('position', /** @type {const} */ ('vec3')), 'flameSourcePosition');
|
|
599
|
+
const density = mx_fractal_noise_float(sourcePosition.sub(center).div(size).mul(uniforms.scale).mul(3)
|
|
600
|
+
.sub(vec3(0, clock, 0)).add(vec3(8.1, 2.7, 4.3)), 2).mul(0.5).add(0.5).clamp(0, 1);
|
|
601
|
+
const temperature = ignition.mul(cooling).mul(density.mul(0.2).add(0.8)).mul(emitter.smoothstep(0, 0.35)).clamp(0, 1);
|
|
602
|
+
const coolToWarm = mix(effectColor, warmColor, temperature.mul(2).clamp(0, 1));
|
|
603
|
+
material.colorNode = mix(coolToWarm, hotColor, temperature.mul(2).sub(1).clamp(0, 1)).mul(2);
|
|
604
|
+
material.opacityNode = noise.smoothstep(0.25, 0.7).mul(up.mul(0.65).add(0.2));
|
|
605
|
+
}
|
|
606
|
+
} else throw new Error(`Unknown wrap preview: ${mode}`);
|
|
607
|
+
if (!material) throw new Error(`Missing material for wrap preview: ${mode}`);
|
|
608
|
+
const mesh = skin ? skin.mesh(geometry, material) : new THREE.Mesh(geometry, material);
|
|
609
|
+
mesh.name = `${mode} wrap shell`;
|
|
610
|
+
if (mode === 'godrays') mesh.renderOrder = 0;
|
|
611
|
+
mesh.frustumCulled = false;
|
|
612
|
+
group.add(mesh);
|
|
613
|
+
geometries.add(geometry); materials.add(material);
|
|
614
|
+
}
|
|
615
|
+
visibility.forEach((_, mesh) => { mesh.visible = false; });
|
|
616
|
+
asset.add(group);
|
|
617
|
+
skin?.update();
|
|
618
|
+
return {
|
|
619
|
+
group,
|
|
620
|
+
syncAnimation() { skin?.update(); },
|
|
621
|
+
/** @param {Record<string, number>} next */
|
|
622
|
+
update(next) {
|
|
623
|
+
next = validatedValues(next);
|
|
624
|
+
if (mode === 'ice' && (next.dispersion > 0) !== dispersionEnabled) return false;
|
|
625
|
+
if (mode === 'force-field' && (next.noise > 0) !== fieldNoiseEnabled) return false;
|
|
626
|
+
if ((mode === 'ivy' || mode === 'lava') && (next.noise > 0) !== growthNoiseEnabled) return false;
|
|
627
|
+
if (mode === 'heat-haze' && (next.strength > 0) !== hazeNoiseEnabled) return false;
|
|
628
|
+
if (mode === 'shroud' && (next.amplitude > 0) !== shroudCurlEnabled) return false;
|
|
629
|
+
effectColor.value.setHex(next.color ?? 0xffffff);
|
|
630
|
+
borderColor.value.setHex(next.borderColor ?? 0x000000);
|
|
631
|
+
warmColor.value.setHex(next.warmColor ?? 0xffb52a);
|
|
632
|
+
hotColor.value.setHex(next.hotColor ?? 0xfff4d6);
|
|
633
|
+
for (const [key, value] of Object.entries(next)) if (uniforms[key]) uniforms[key].value = value;
|
|
634
|
+
for (const material of materials) if (material.userData.previewWireframeThickness) material.userData.previewWireframeThickness.value = next.width;
|
|
635
|
+
return true;
|
|
636
|
+
},
|
|
637
|
+
dispose() {
|
|
638
|
+
skin?.dispose();
|
|
639
|
+
group.removeFromParent();
|
|
640
|
+
visibility.forEach((visible, mesh) => { mesh.visible = visible; });
|
|
641
|
+
geometries.forEach(geometry => geometry.dispose());
|
|
642
|
+
materials.forEach(material => material.dispose());
|
|
643
|
+
},
|
|
644
|
+
};
|
|
645
|
+
}
|