@needle-tools/three-mesh-effects 0.2.3 → 0.3.1
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/CHANGELOG.md +25 -8
- package/README.md +68 -1
- package/SKILL.md +9 -1
- package/build/box-impostor-runtime.js +1298 -0
- package/build/box-impostor-runtime.js.map +7 -0
- package/build/deformation-runtime.js +30 -19
- package/build/deformation-runtime.js.map +3 -3
- package/build/hull-effects.js +60 -28
- package/build/hull-effects.js.map +3 -3
- package/build/impostor-runtime.js +152 -87
- package/build/impostor-runtime.js.map +3 -3
- package/build/index.js +4 -0
- package/build/index.js.map +2 -2
- package/build/internal/box-impostor/box-impostor-display-settings.js +50 -0
- package/build/internal/box-impostor/box-impostor-display-settings.js.map +7 -0
- package/build/internal/box-impostor/box-impostor-display.js +1005 -0
- package/build/internal/box-impostor/box-impostor-display.js.map +7 -0
- package/build/internal/box-impostor/box-impostor-face-axes.js +15 -0
- package/build/internal/box-impostor/box-impostor-face-axes.js.map +7 -0
- package/build/internal/box-impostor/box-impostor-faces.js +125 -0
- package/build/internal/box-impostor/box-impostor-faces.js.map +7 -0
- package/build/internal/box-impostor/box-impostor-geometry.js +230 -0
- package/build/internal/box-impostor/box-impostor-geometry.js.map +7 -0
- package/build/internal/box-impostor/box-impostor-material.js +964 -0
- package/build/internal/box-impostor/box-impostor-material.js.map +7 -0
- package/build/internal/box-impostor/box-impostor-metadata.js +108 -0
- package/build/internal/box-impostor/box-impostor-metadata.js.map +7 -0
- package/build/internal/box-impostor/box-impostor-projection.js +256 -0
- package/build/internal/box-impostor/box-impostor-projection.js.map +7 -0
- package/build/internal/box-impostor/box-impostor-runtime.js +1252 -0
- package/build/internal/box-impostor/box-impostor-runtime.js.map +7 -0
- package/build/internal/box-impostor/box-impostor-surface-search.js +21 -0
- package/build/internal/box-impostor/box-impostor-surface-search.js.map +7 -0
- package/build/internal/deformation/cloth-surface.js +23 -17
- package/build/internal/deformation/cloth-surface.js.map +3 -3
- package/build/internal/deformation/pin-cloth.js +7 -2
- package/build/internal/deformation/pin-cloth.js.map +2 -2
- package/build/internal/deformation/rig-runtime.js +30 -19
- package/build/internal/deformation/rig-runtime.js.map +3 -3
- package/build/internal/impostor/impostor-node-material.js +152 -87
- package/build/internal/impostor/impostor-node-material.js.map +3 -3
- package/build/internal/rendering/hull-effect-runtime.js +59 -27
- package/build/internal/rendering/hull-effect-runtime.js.map +3 -3
- package/build/internal/rendering/wireframe.js +41 -9
- package/build/internal/rendering/wireframe.js.map +2 -2
- package/build/internal/rendering/wrap-preview.js +59 -27
- package/build/internal/rendering/wrap-preview.js.map +3 -3
- package/build/types/box-impostor/box-impostor-display-settings.d.ts +30 -0
- package/build/types/box-impostor/box-impostor-display-settings.js +1 -0
- package/build/types/box-impostor/box-impostor-display.d.ts +5 -0
- package/build/types/box-impostor/box-impostor-display.js +1 -0
- package/build/types/box-impostor/box-impostor-face-axes.d.ts +5 -0
- package/build/types/box-impostor/box-impostor-face-axes.js +1 -0
- package/build/types/box-impostor/box-impostor-faces.d.ts +11 -0
- package/build/types/box-impostor/box-impostor-faces.js +1 -0
- package/build/types/box-impostor/box-impostor-geometry.d.ts +5 -0
- package/build/types/box-impostor/box-impostor-geometry.js +1 -0
- package/build/types/box-impostor/box-impostor-material.d.ts +10 -0
- package/build/types/box-impostor/box-impostor-material.js +1 -0
- package/build/types/box-impostor/box-impostor-metadata.d.ts +31 -0
- package/build/types/box-impostor/box-impostor-metadata.js +1 -0
- package/build/types/box-impostor/box-impostor-projection.d.ts +19 -0
- package/build/types/box-impostor/box-impostor-projection.js +1 -0
- package/build/types/box-impostor/box-impostor-runtime.d.ts +25 -0
- package/build/types/box-impostor/box-impostor-runtime.js +1 -0
- package/build/types/box-impostor/box-impostor-surface-search.d.ts +13 -0
- package/build/types/box-impostor/box-impostor-surface-search.js +1 -0
- package/build/types/deformation/cloth-surface.d.ts +1 -0
- package/build/types/deformation/pin-cloth.d.ts +1 -0
- package/build/types/public/box-impostor-runtime.d.ts +6 -0
- package/build/types/public/box-impostor-runtime.js +1 -0
- package/build/types/public/hull-effects.d.ts +1 -0
- package/build/types/public/index.d.ts +1 -1
- package/build/types/rendering/wireframe.d.ts +2 -2
- package/package.json +27 -6
- package/src/box-impostor/box-impostor-display-settings.js +54 -0
- package/src/box-impostor/box-impostor-display.js +38 -0
- package/src/box-impostor/box-impostor-face-axes.js +11 -0
- package/src/box-impostor/box-impostor-faces.js +118 -0
- package/src/box-impostor/box-impostor-geometry.js +140 -0
- package/src/box-impostor/box-impostor-material.js +721 -0
- package/src/box-impostor/box-impostor-metadata.js +122 -0
- package/src/box-impostor/box-impostor-projection.js +287 -0
- package/src/box-impostor/box-impostor-runtime.js +122 -0
- package/src/box-impostor/box-impostor-surface-search.js +11 -0
- package/src/deformation/cloth-surface.ts +28 -17
- package/src/deformation/pin-cloth.ts +9 -3
- package/src/impostor/impostor-node-material.js +177 -117
- package/src/public/box-impostor-runtime.js +14 -0
- package/src/public/hull-effects.js +1 -0
- package/src/public/index.js +5 -1
- package/src/rendering/wireframe.js +51 -13
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { normalizeBoxImpostorDisplay } from "./box-impostor-display-settings.js";
|
|
3
|
+
export const NEEDLE_BOX_IMPOSTOR = "NEEDLE_box_impostor";
|
|
4
|
+
export const BOX_OCCUPANCY_RESOLUTION = 8;
|
|
5
|
+
export const BOX_OCCUPANCY_WORDS = BOX_OCCUPANCY_RESOLUTION ** 3 / 32;
|
|
6
|
+
export { BOX_FACE_AXES } from "./box-impostor-face-axes.js";
|
|
7
|
+
/** @typedef {{center:number[],axes:number[][],halfSize:number[],views:number[][],planes?:number[],planeErrors?:number[],depthBounds?:number[],occupancy?:number[]}} ImpostorBox */
|
|
8
|
+
/** @typedef {{version:1,depthMode?:'auto'|'projection'|'depth',alphaMode?:'opaque'|'mask'|'blend',interpolation?:'none'|'linear'|'smooth',transparency?:'auto'|'cutout'|'dithered'|'blended',mode:'basic'|'cards'|'fitted',atlasResolution:number,padding:number,raySteps:8|16|32,alphaCutoff:number,boxes:ImpostorBox[]}} BoxImpostorDescriptor */
|
|
9
|
+
/** @param {unknown} raw @returns {BoxImpostorDescriptor} */
|
|
10
|
+
export function parseBoxImpostorDescriptor(raw) {
|
|
11
|
+
const d = /** @type {BoxImpostorDescriptor} */ (raw);
|
|
12
|
+
const fail = () => {
|
|
13
|
+
throw new Error("Invalid NEEDLE_box_impostor descriptor.");
|
|
14
|
+
};
|
|
15
|
+
const vector = (/** @type {unknown} */ v) =>
|
|
16
|
+
Array.isArray(v) && v.length === 3 && v.every(Number.isFinite);
|
|
17
|
+
if (
|
|
18
|
+
!d ||
|
|
19
|
+
d.version !== 1 ||
|
|
20
|
+
!["basic", "cards", "fitted"].includes(d.mode) ||
|
|
21
|
+
![8, 16, 32].includes(d.raySteps) ||
|
|
22
|
+
!Number.isInteger(d.atlasResolution) ||
|
|
23
|
+
d.atlasResolution < 32 ||
|
|
24
|
+
d.atlasResolution > 8192 ||
|
|
25
|
+
!Number.isInteger(d.padding) ||
|
|
26
|
+
d.padding < 1 ||
|
|
27
|
+
d.padding > 16 ||
|
|
28
|
+
!Number.isFinite(d.alphaCutoff) ||
|
|
29
|
+
d.alphaCutoff <= 0 ||
|
|
30
|
+
d.alphaCutoff >= 1 ||
|
|
31
|
+
!Array.isArray(d.boxes) ||
|
|
32
|
+
!d.boxes.length ||
|
|
33
|
+
d.boxes.length > 64
|
|
34
|
+
)
|
|
35
|
+
fail();
|
|
36
|
+
if (
|
|
37
|
+
d.alphaMode !== undefined &&
|
|
38
|
+
!["opaque", "mask", "blend"].includes(d.alphaMode)
|
|
39
|
+
)
|
|
40
|
+
fail();
|
|
41
|
+
if (d.mode === "basic" && d.boxes.length !== 1) fail();
|
|
42
|
+
for (const b of d.boxes) {
|
|
43
|
+
if (
|
|
44
|
+
b.occupancy !== undefined &&
|
|
45
|
+
(!Array.isArray(b.occupancy) ||
|
|
46
|
+
b.occupancy.length !== BOX_OCCUPANCY_WORDS ||
|
|
47
|
+
!b.occupancy.every(
|
|
48
|
+
(v) => Number.isInteger(v) && v >= 0 && v <= 0xffffffff,
|
|
49
|
+
))
|
|
50
|
+
)
|
|
51
|
+
fail();
|
|
52
|
+
if (
|
|
53
|
+
b.depthBounds !== undefined &&
|
|
54
|
+
(!Array.isArray(b.depthBounds) ||
|
|
55
|
+
b.depthBounds.length !== 12 ||
|
|
56
|
+
!b.depthBounds.every(
|
|
57
|
+
(v) => Number.isInteger(v) && v >= 0 && v <= 65535,
|
|
58
|
+
))
|
|
59
|
+
)
|
|
60
|
+
fail();
|
|
61
|
+
if (
|
|
62
|
+
b.planeErrors !== undefined &&
|
|
63
|
+
(!Array.isArray(b.planeErrors) ||
|
|
64
|
+
b.planeErrors.length !== 6 ||
|
|
65
|
+
!b.planeErrors.every((v) => Number.isFinite(v) && v >= 0 && v <= 1))
|
|
66
|
+
)
|
|
67
|
+
fail();
|
|
68
|
+
if (
|
|
69
|
+
b.planes !== undefined &&
|
|
70
|
+
(!Array.isArray(b.planes) ||
|
|
71
|
+
b.planes.length !== 6 ||
|
|
72
|
+
!b.planes.every((v) => Number.isFinite(v) && v >= 0 && v <= 1))
|
|
73
|
+
)
|
|
74
|
+
fail();
|
|
75
|
+
if (
|
|
76
|
+
!vector(b.center) ||
|
|
77
|
+
!vector(b.halfSize) ||
|
|
78
|
+
b.halfSize.some((v) => v <= 0) ||
|
|
79
|
+
!Array.isArray(b.axes) ||
|
|
80
|
+
b.axes.length !== 3 ||
|
|
81
|
+
!b.axes.every(vector) ||
|
|
82
|
+
!Array.isArray(b.views) ||
|
|
83
|
+
b.views.length !== 6
|
|
84
|
+
)
|
|
85
|
+
fail();
|
|
86
|
+
for (let i = 0; i < 3; i++)
|
|
87
|
+
for (let j = 0; j < 3; j++) {
|
|
88
|
+
const dot = b.axes[i].reduce((s, v, k) => s + v * b.axes[j][k], 0);
|
|
89
|
+
if (Math.abs(dot - (i === j ? 1 : 0)) > 1e-4) fail();
|
|
90
|
+
}
|
|
91
|
+
const [x, y, z] = b.axes;
|
|
92
|
+
if (
|
|
93
|
+
(x[1] * y[2] - x[2] * y[1]) * z[0] +
|
|
94
|
+
(x[2] * y[0] - x[0] * y[2]) * z[1] +
|
|
95
|
+
(x[0] * y[1] - x[1] * y[0]) * z[2] <
|
|
96
|
+
0.999
|
|
97
|
+
)
|
|
98
|
+
fail();
|
|
99
|
+
for (const r of b.views)
|
|
100
|
+
if (
|
|
101
|
+
!Array.isArray(r) ||
|
|
102
|
+
r.length !== 4 ||
|
|
103
|
+
!r.every(Number.isInteger) ||
|
|
104
|
+
r[0] < 0 ||
|
|
105
|
+
r[1] < 0 ||
|
|
106
|
+
r[2] <= d.padding * 2 ||
|
|
107
|
+
r[3] <= d.padding * 2 ||
|
|
108
|
+
r[0] + r[2] > d.atlasResolution ||
|
|
109
|
+
r[1] + r[3] > d.atlasResolution
|
|
110
|
+
)
|
|
111
|
+
fail();
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
version: d.version,
|
|
115
|
+
mode: d.mode,
|
|
116
|
+
atlasResolution: d.atlasResolution,
|
|
117
|
+
padding: d.padding,
|
|
118
|
+
boxes: d.boxes,
|
|
119
|
+
...(d.alphaMode ? { alphaMode: d.alphaMode } : {}),
|
|
120
|
+
...normalizeBoxImpostorDisplay(d),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
// TSL struct member types are verified by rendered GPU regressions.
|
|
2
|
+
// @ts-nocheck
|
|
3
|
+
import { DataTexture, FloatType, RGBAFormat, NearestFilter } from 'three';
|
|
4
|
+
import {
|
|
5
|
+
Fn,
|
|
6
|
+
If,
|
|
7
|
+
vec2,
|
|
8
|
+
vec3,
|
|
9
|
+
vec4,
|
|
10
|
+
float,
|
|
11
|
+
int,
|
|
12
|
+
ivec2,
|
|
13
|
+
dot,
|
|
14
|
+
abs,
|
|
15
|
+
max,
|
|
16
|
+
sign,
|
|
17
|
+
normalize,
|
|
18
|
+
texture,
|
|
19
|
+
textureLoad,
|
|
20
|
+
attribute,
|
|
21
|
+
struct,
|
|
22
|
+
modelViewMatrix,
|
|
23
|
+
modelWorldMatrix,
|
|
24
|
+
cameraNear,
|
|
25
|
+
cameraFar,
|
|
26
|
+
viewZToPerspectiveDepth,
|
|
27
|
+
viewZToOrthographicDepth,
|
|
28
|
+
transformNormalToView,
|
|
29
|
+
} from 'three/tsl';
|
|
30
|
+
import { BOX_FACE_AXES } from './box-impostor-metadata.js';
|
|
31
|
+
|
|
32
|
+
/** A ray/plane division supplies each UV directly. This graph deliberately has
|
|
33
|
+
* no depth search, refinement or depth-consistency texture fetches. */
|
|
34
|
+
export function applyBoxProjection({
|
|
35
|
+
material,
|
|
36
|
+
descriptor,
|
|
37
|
+
maps,
|
|
38
|
+
settings,
|
|
39
|
+
transparency,
|
|
40
|
+
alphaCutoff,
|
|
41
|
+
rects,
|
|
42
|
+
center,
|
|
43
|
+
half,
|
|
44
|
+
toBox,
|
|
45
|
+
fromBox,
|
|
46
|
+
eye,
|
|
47
|
+
viewAxis,
|
|
48
|
+
perspective,
|
|
49
|
+
raster,
|
|
50
|
+
}) {
|
|
51
|
+
const data = new Float32Array(descriptor.boxes.length * 24);
|
|
52
|
+
descriptor.boxes.forEach((box, i) => {
|
|
53
|
+
for (let face = 0; face < 6; face++)
|
|
54
|
+
data[(i * 6 + face) * 4] = box.planes?.[face] ?? 0.5;
|
|
55
|
+
});
|
|
56
|
+
const planes = new DataTexture(
|
|
57
|
+
data,
|
|
58
|
+
6,
|
|
59
|
+
descriptor.boxes.length,
|
|
60
|
+
RGBAFormat,
|
|
61
|
+
FloatType,
|
|
62
|
+
);
|
|
63
|
+
planes.minFilter = planes.magFilter = NearestFilter;
|
|
64
|
+
planes.needsUpdate = true;
|
|
65
|
+
material.addEventListener('dispose', () => planes.dispose());
|
|
66
|
+
const Surface = struct(
|
|
67
|
+
{
|
|
68
|
+
color: 'vec3',
|
|
69
|
+
alpha: 'float',
|
|
70
|
+
orm: 'vec3',
|
|
71
|
+
emission: 'vec3',
|
|
72
|
+
normal: 'vec3',
|
|
73
|
+
position: 'vec3',
|
|
74
|
+
},
|
|
75
|
+
'BoxProjectedSurface',
|
|
76
|
+
);
|
|
77
|
+
const readNormal = (uv, right, up, n) => {
|
|
78
|
+
const packed = textureLoad(
|
|
79
|
+
maps.normalDepth,
|
|
80
|
+
ivec2(uv.mul(descriptor.atlasResolution)),
|
|
81
|
+
)
|
|
82
|
+
.rg.mul(2)
|
|
83
|
+
.sub(1);
|
|
84
|
+
const decoded = vec3(
|
|
85
|
+
packed,
|
|
86
|
+
float(1).sub(abs(packed.x)).sub(abs(packed.y)),
|
|
87
|
+
).toVar();
|
|
88
|
+
If(decoded.z.lessThan(0), () => {
|
|
89
|
+
decoded.xy.assign(
|
|
90
|
+
vec2(1)
|
|
91
|
+
.sub(abs(decoded.yx))
|
|
92
|
+
.mul(sign(decoded.xy.add(1e-20))),
|
|
93
|
+
);
|
|
94
|
+
});
|
|
95
|
+
return fromBox(
|
|
96
|
+
right.mul(decoded.x).add(up.mul(decoded.y)).add(n.mul(decoded.z)),
|
|
97
|
+
).normalize();
|
|
98
|
+
};
|
|
99
|
+
const surface = Fn(() => {
|
|
100
|
+
const ray = perspective.select(
|
|
101
|
+
normalize(raster.sub(eye)),
|
|
102
|
+
viewAxis.negate(),
|
|
103
|
+
);
|
|
104
|
+
const origin = perspective.select(
|
|
105
|
+
eye,
|
|
106
|
+
raster.sub(ray.mul(half.length().mul(4))),
|
|
107
|
+
);
|
|
108
|
+
const o = toBox(origin.sub(center)).div(half),
|
|
109
|
+
r = toBox(ray).div(half);
|
|
110
|
+
const look = normalize(toBox(ray).negate()),
|
|
111
|
+
angles = abs(look);
|
|
112
|
+
const positions = vec3(0).toVar();
|
|
113
|
+
const selectedUv = vec2(0).toVar(),
|
|
114
|
+
selectedRight = vec3(1, 0, 0).toVar(),
|
|
115
|
+
selectedUp = vec3(0, 1, 0).toVar(),
|
|
116
|
+
selectedNormal = vec3(0, 0, 1).toVar();
|
|
117
|
+
const nearest = float(1e20).toVar('boxPlaneDistance');
|
|
118
|
+
const opacity = float(0).toVar();
|
|
119
|
+
const candidates = [];
|
|
120
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
121
|
+
const angle = angles.element(axis);
|
|
122
|
+
const positive = look.element(axis).greaterThanEqual(0),
|
|
123
|
+
a = BOX_FACE_AXES[axis * 2],
|
|
124
|
+
b = BOX_FACE_AXES[axis * 2 + 1];
|
|
125
|
+
const n = positive.select(vec3(...a.normal), vec3(...b.normal)),
|
|
126
|
+
right = positive.select(vec3(...a.right), vec3(...b.right)),
|
|
127
|
+
up = positive.select(vec3(...a.up), vec3(...b.up));
|
|
128
|
+
const weight =
|
|
129
|
+
settings.interpolation === 'smooth' ? angle.mul(angle) : angle;
|
|
130
|
+
const candidate =
|
|
131
|
+
settings.interpolation === 'none'
|
|
132
|
+
? null
|
|
133
|
+
: {
|
|
134
|
+
uv: vec2(0).toVar(),
|
|
135
|
+
base: vec4(0).toVar(),
|
|
136
|
+
orm: vec4(0).toVar(),
|
|
137
|
+
t: float(1e20).toVar(),
|
|
138
|
+
tolerance: float(0).toVar(),
|
|
139
|
+
right,
|
|
140
|
+
up,
|
|
141
|
+
n,
|
|
142
|
+
weight,
|
|
143
|
+
};
|
|
144
|
+
if (candidate) candidates.push(candidate);
|
|
145
|
+
If(angle.greaterThan(0.0001), () => {
|
|
146
|
+
const face = positive.select(int(axis * 2), int(axis * 2 + 1)),
|
|
147
|
+
id = int(attribute('boxId', 'float'));
|
|
148
|
+
const plane = float(1).sub(
|
|
149
|
+
textureLoad(planes, ivec2(face, id)).r.mul(2),
|
|
150
|
+
);
|
|
151
|
+
const divisor = dot(r, n),
|
|
152
|
+
safe = sign(divisor.add(1e-20)).mul(max(abs(divisor), 1e-10));
|
|
153
|
+
const t = plane.sub(dot(o, n)).div(safe),
|
|
154
|
+
point = o.add(r.mul(t));
|
|
155
|
+
const xy = vec2(dot(point, right), dot(point, up)).mul(0.5).add(0.5);
|
|
156
|
+
// Clamping an unsupported plane to its edge would extrude its last texel.
|
|
157
|
+
let supported = t
|
|
158
|
+
.greaterThanEqual(0)
|
|
159
|
+
.and(xy.x.greaterThanEqual(0))
|
|
160
|
+
.and(xy.y.greaterThanEqual(0))
|
|
161
|
+
.and(xy.x.lessThanEqual(1))
|
|
162
|
+
.and(xy.y.lessThanEqual(1));
|
|
163
|
+
if (!candidate) supported = supported.and(t.lessThan(nearest));
|
|
164
|
+
If(supported, () => {
|
|
165
|
+
const rect = textureLoad(rects, ivec2(face, id)),
|
|
166
|
+
uv = rect.xy.add(xy.mul(rect.zw));
|
|
167
|
+
const base = texture(maps.baseColor, uv).level(0),
|
|
168
|
+
orm = texture(maps.orm, uv).level(0);
|
|
169
|
+
const alpha = base.a.clamp(0, 1);
|
|
170
|
+
If(
|
|
171
|
+
alpha
|
|
172
|
+
.greaterThanEqual(
|
|
173
|
+
transparency === 'cutout' ? alphaCutoff : float(1 / 255),
|
|
174
|
+
)
|
|
175
|
+
.and(orm.a.greaterThan(0)),
|
|
176
|
+
() => {
|
|
177
|
+
If(t.lessThan(nearest), () => {
|
|
178
|
+
nearest.assign(t);
|
|
179
|
+
selectedUv.assign(uv);
|
|
180
|
+
opacity.assign(alpha);
|
|
181
|
+
selectedRight.assign(right);
|
|
182
|
+
selectedUp.assign(up);
|
|
183
|
+
selectedNormal.assign(n);
|
|
184
|
+
positions.assign(center.add(fromBox(point.mul(half))));
|
|
185
|
+
});
|
|
186
|
+
if (candidate) {
|
|
187
|
+
candidate.uv.assign(uv);
|
|
188
|
+
candidate.base.assign(base);
|
|
189
|
+
candidate.orm.assign(orm);
|
|
190
|
+
candidate.t.assign(t);
|
|
191
|
+
candidate.tolerance.assign(
|
|
192
|
+
max(
|
|
193
|
+
dot(abs(right), half).div(
|
|
194
|
+
rect.z.mul(descriptor.atlasResolution),
|
|
195
|
+
),
|
|
196
|
+
dot(abs(up), half).div(
|
|
197
|
+
rect.w.mul(descriptor.atlasResolution),
|
|
198
|
+
),
|
|
199
|
+
)
|
|
200
|
+
.mul(4)
|
|
201
|
+
.div(max(angle, 0.1)),
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
);
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
if (settings.interpolation === 'none') {
|
|
210
|
+
// Select coordinates first, then shade one winning view.
|
|
211
|
+
const base = texture(maps.baseColor, selectedUv).level(0),
|
|
212
|
+
orm = texture(maps.orm, selectedUv).level(0);
|
|
213
|
+
const coverage = max(orm.a, 1e-10);
|
|
214
|
+
return Surface(
|
|
215
|
+
base.rgb.div(coverage),
|
|
216
|
+
opacity,
|
|
217
|
+
orm.rgb.div(coverage),
|
|
218
|
+
texture(maps.emissive, selectedUv).level(0).rgb.div(coverage),
|
|
219
|
+
readNormal(selectedUv, selectedRight, selectedUp, selectedNormal),
|
|
220
|
+
positions,
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
const colors = vec3(0).toVar(),
|
|
224
|
+
orms = vec3(0).toVar(),
|
|
225
|
+
emissions = vec3(0).toVar(),
|
|
226
|
+
normals = vec3(0).toVar();
|
|
227
|
+
const weights = float(0).toVar(),
|
|
228
|
+
support = float(0).toVar(),
|
|
229
|
+
alpha = float(0).toVar();
|
|
230
|
+
// Resolve every candidate against the final nearest surface, so the blend
|
|
231
|
+
// cannot retain a farther plane because it happened to be visited first.
|
|
232
|
+
for (const c of candidates) {
|
|
233
|
+
If(
|
|
234
|
+
c.base.a
|
|
235
|
+
.greaterThan(0)
|
|
236
|
+
.and(c.t.lessThanEqual(nearest.add(c.tolerance))),
|
|
237
|
+
() => {
|
|
238
|
+
const coverage = max(c.orm.a, 1e-10),
|
|
239
|
+
w = c.base.a.mul(c.weight);
|
|
240
|
+
weights.addAssign(c.weight.mul(coverage));
|
|
241
|
+
support.assign(max(support, coverage));
|
|
242
|
+
colors.addAssign(c.base.rgb.div(coverage).mul(w));
|
|
243
|
+
orms.addAssign(c.orm.rgb.div(coverage).mul(w));
|
|
244
|
+
emissions.addAssign(
|
|
245
|
+
texture(maps.emissive, c.uv).level(0).rgb.div(coverage).mul(w),
|
|
246
|
+
);
|
|
247
|
+
normals.addAssign(readNormal(c.uv, c.right, c.up, c.n).mul(w));
|
|
248
|
+
alpha.addAssign(w);
|
|
249
|
+
},
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
const divisor = max(alpha, 1e-10);
|
|
253
|
+
return Surface(
|
|
254
|
+
colors.div(divisor),
|
|
255
|
+
alpha.div(max(weights, 1e-10)).mul(support),
|
|
256
|
+
orms.div(divisor),
|
|
257
|
+
emissions.div(divisor),
|
|
258
|
+
normals.div(max(normals.length(), 1e-10)),
|
|
259
|
+
positions,
|
|
260
|
+
);
|
|
261
|
+
})().toVar('boxProjectedSurface');
|
|
262
|
+
material.colorNode = surface.get('color');
|
|
263
|
+
material.normalNode = transformNormalToView(surface.get('normal'));
|
|
264
|
+
material.roughnessNode = surface.get('orm').g;
|
|
265
|
+
material.metalnessNode = surface.get('orm').b;
|
|
266
|
+
material.aoNode = surface.get('orm').r;
|
|
267
|
+
material.emissiveNode = surface.get('emission');
|
|
268
|
+
material.maskNode = surface
|
|
269
|
+
.get('alpha')
|
|
270
|
+
.greaterThanEqual(transparency === 'cutout' ? alphaCutoff : float(1 / 255));
|
|
271
|
+
material.maskShadowNode = material.maskNode;
|
|
272
|
+
if (transparency !== 'cutout') material.opacityNode = surface.get('alpha');
|
|
273
|
+
material.depthNode = Fn(() => {
|
|
274
|
+
const z = modelViewMatrix
|
|
275
|
+
.mul(vec4(surface.get('position'), 1))
|
|
276
|
+
.z.toVar('boxProjectionDepthZ');
|
|
277
|
+
return perspective.select(
|
|
278
|
+
viewZToPerspectiveDepth(z, cameraNear, cameraFar),
|
|
279
|
+
viewZToOrthographicDepth(z, cameraNear, cameraFar),
|
|
280
|
+
);
|
|
281
|
+
})();
|
|
282
|
+
material.receivedShadowPositionNode = modelWorldMatrix.mul(
|
|
283
|
+
vec4(surface.get('position'), 1),
|
|
284
|
+
).xyz;
|
|
285
|
+
material.userData.boxImpostor = { descriptor, maps, alphaCutoff };
|
|
286
|
+
return material;
|
|
287
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import {
|
|
3
|
+
ClampToEdgeWrapping,
|
|
4
|
+
LinearFilter,
|
|
5
|
+
NearestFilter,
|
|
6
|
+
NoColorSpace,
|
|
7
|
+
SRGBColorSpace,
|
|
8
|
+
} from 'three';
|
|
9
|
+
import {
|
|
10
|
+
NEEDLE_BOX_IMPOSTOR,
|
|
11
|
+
parseBoxImpostorDescriptor,
|
|
12
|
+
} from './box-impostor-metadata.js';
|
|
13
|
+
import { createBoxImpostorGeometry } from './box-impostor-geometry.js';
|
|
14
|
+
import { createBoxImpostorMaterial } from './box-impostor-material.js';
|
|
15
|
+
|
|
16
|
+
export class NeedleBoxImpostorLoaderPlugin {
|
|
17
|
+
/** @param {import('three/addons/loaders/GLTFLoader.js').GLTFParser} parser
|
|
18
|
+
* @param {{renderer?:{isWebGPURenderer?:boolean}}} [options] */
|
|
19
|
+
constructor(parser, options = {}) {
|
|
20
|
+
this.name = NEEDLE_BOX_IMPOSTOR;
|
|
21
|
+
this.parser = parser;
|
|
22
|
+
this.options = options;
|
|
23
|
+
}
|
|
24
|
+
/** @param {import('three/addons/loaders/GLTFLoader.js').GLTF} result */
|
|
25
|
+
async afterRoot(result) {
|
|
26
|
+
const seen = new Set();
|
|
27
|
+
const geometries = new Set();
|
|
28
|
+
const materials = new Set();
|
|
29
|
+
const scenes = result.scenes ?? [result.scene];
|
|
30
|
+
const pending = /** @type {Promise<void>[]} */ ([]);
|
|
31
|
+
for (const scene of scenes)
|
|
32
|
+
scene?.traverse((object) => {
|
|
33
|
+
const mesh = /** @type {import('three').Mesh} */ (object);
|
|
34
|
+
if (!mesh.isMesh || seen.has(mesh)) return;
|
|
35
|
+
seen.add(mesh);
|
|
36
|
+
const a = /** @type {{meshes?:number,primitives?:number}|undefined} */ (
|
|
37
|
+
this.parser.associations.get(mesh)
|
|
38
|
+
);
|
|
39
|
+
if (a?.meshes === undefined || a.primitives === undefined) return;
|
|
40
|
+
const raw =
|
|
41
|
+
this.parser.json.meshes?.[a.meshes]?.primitives[a.primitives]
|
|
42
|
+
?.extensions?.[NEEDLE_BOX_IMPOSTOR];
|
|
43
|
+
if (raw === undefined) return;
|
|
44
|
+
const descriptor = parseBoxImpostorDescriptor(raw);
|
|
45
|
+
if (!this.options.renderer?.isWebGPURenderer) {
|
|
46
|
+
if (
|
|
47
|
+
this.parser.json.extensionsRequired?.includes(NEEDLE_BOX_IMPOSTOR)
|
|
48
|
+
)
|
|
49
|
+
throw new Error(
|
|
50
|
+
'NEEDLE_box_impostor requires Three WebGPURenderer (WebGPU or WebGL2 backend).',
|
|
51
|
+
);
|
|
52
|
+
mesh.userData.boxImpostorFallback = true;
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
pending.push(
|
|
56
|
+
(async () => {
|
|
57
|
+
const entries = await Promise.all(
|
|
58
|
+
['baseColor', 'normalDepth', 'orm', 'emissive'].map(
|
|
59
|
+
async (role) => {
|
|
60
|
+
const index = raw.textures?.[role]?.index;
|
|
61
|
+
if (!Number.isInteger(index) || index < 0)
|
|
62
|
+
throw new Error(
|
|
63
|
+
`NEEDLE_box_impostor has no valid ${role} texture.`,
|
|
64
|
+
);
|
|
65
|
+
const texture = await this.parser.getDependency(
|
|
66
|
+
'texture',
|
|
67
|
+
index,
|
|
68
|
+
);
|
|
69
|
+
texture.colorSpace =
|
|
70
|
+
role === 'baseColor' || role === 'emissive'
|
|
71
|
+
? SRGBColorSpace
|
|
72
|
+
: NoColorSpace;
|
|
73
|
+
texture.magFilter = texture.minFilter =
|
|
74
|
+
role === 'normalDepth' ? NearestFilter : LinearFilter;
|
|
75
|
+
texture.wrapS = texture.wrapT = ClampToEdgeWrapping;
|
|
76
|
+
texture.generateMipmaps = false;
|
|
77
|
+
texture.needsUpdate = true;
|
|
78
|
+
return [role, texture];
|
|
79
|
+
},
|
|
80
|
+
),
|
|
81
|
+
);
|
|
82
|
+
const maps =
|
|
83
|
+
/** @type {Parameters<typeof createBoxImpostorMaterial>[1]} */ (
|
|
84
|
+
Object.fromEntries(entries)
|
|
85
|
+
);
|
|
86
|
+
geometries.add(mesh.geometry);
|
|
87
|
+
for (const material of Array.isArray(mesh.material)
|
|
88
|
+
? mesh.material
|
|
89
|
+
: [mesh.material])
|
|
90
|
+
materials.add(material);
|
|
91
|
+
mesh.geometry = createBoxImpostorGeometry(descriptor);
|
|
92
|
+
mesh.material = createBoxImpostorMaterial(descriptor, maps);
|
|
93
|
+
mesh.userData.boxImpostor = { descriptor, maps };
|
|
94
|
+
mesh.frustumCulled = descriptor.mode === 'fitted';
|
|
95
|
+
})(),
|
|
96
|
+
);
|
|
97
|
+
});
|
|
98
|
+
await Promise.all(pending);
|
|
99
|
+
// A fallback may be shared by a node without this extension. Dispose only
|
|
100
|
+
// resources made unreachable by replacement; textures remain document-owned.
|
|
101
|
+
for (const scene of scenes)
|
|
102
|
+
scene?.traverse((object) => {
|
|
103
|
+
const mesh = /** @type {import('three').Mesh} */ (object);
|
|
104
|
+
if (!mesh.isMesh) return;
|
|
105
|
+
geometries.delete(mesh.geometry);
|
|
106
|
+
for (const material of Array.isArray(mesh.material)
|
|
107
|
+
? mesh.material
|
|
108
|
+
: [mesh.material])
|
|
109
|
+
materials.delete(material);
|
|
110
|
+
});
|
|
111
|
+
for (const geometry of geometries) geometry.dispose();
|
|
112
|
+
for (const material of materials) material.dispose();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/** @template {import('three/addons/loaders/GLTFLoader.js').GLTFLoader} T
|
|
116
|
+
* @param {T} loader @param {{renderer?:{isWebGPURenderer?:boolean}}} [options] @returns {T} */
|
|
117
|
+
export function registerNeedleBoxImpostorLoader(loader, options = {}) {
|
|
118
|
+
loader.register(
|
|
119
|
+
(parser) => new NeedleBoxImpostorLoaderPlugin(parser, options),
|
|
120
|
+
);
|
|
121
|
+
return loader;
|
|
122
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
// TSL node algebra for crossing refinement. Texture layouts and renderer state
|
|
3
|
+
// belong to the material adapter.
|
|
4
|
+
export function refineSurfaceCrossing({float,If,Loop},{lo,hi,loValue,hiValue,height,steps,secant}) {
|
|
5
|
+
const a=lo.toVar(),b=hi.toVar(),fa=loValue.toVar(),fb=secant?hiValue.toVar():null;
|
|
6
|
+
Loop(steps,()=>{
|
|
7
|
+
const mid=a.add(b).mul(.5).toVar(),d=height(mid).toVar();
|
|
8
|
+
If(d.mul(fa).greaterThan(0),()=>{a.assign(mid);if(secant)fa.assign(d);}).Else(()=>{b.assign(mid);if(secant)fb.assign(d);});
|
|
9
|
+
});
|
|
10
|
+
return secant?a.add(b.sub(a).mul(fa.abs().div(fa.abs().add(fb.abs()).max(1e-20)).clamp(0,1))):a.add(b).mul(.5);
|
|
11
|
+
}
|
|
@@ -59,6 +59,7 @@ export class ClothSurface {
|
|
|
59
59
|
readonly edges: Edge[];
|
|
60
60
|
readonly scale: number;
|
|
61
61
|
private time = 0;
|
|
62
|
+
private accumulator = 0;
|
|
62
63
|
|
|
63
64
|
constructor(restVertices: Float32Array, indices: ArrayLike<number>, pins: readonly PinRecord[]) {
|
|
64
65
|
const vertexCount = restVertices.length / 3;
|
|
@@ -129,6 +130,7 @@ export class ClothSurface {
|
|
|
129
130
|
|
|
130
131
|
reset() {
|
|
131
132
|
this.time = 0;
|
|
133
|
+
this.accumulator = 0;
|
|
132
134
|
for (const particle of this.particles) particle.position.copy(particle.base), particle.previous.copy(particle.base);
|
|
133
135
|
}
|
|
134
136
|
|
|
@@ -142,7 +144,12 @@ export class ClothSurface {
|
|
|
142
144
|
const particle = this.particles[i], delta = base[i].clone().sub(particle.base);
|
|
143
145
|
particle.position.add(delta); particle.previous.add(delta); particle.base.copy(base[i]);
|
|
144
146
|
}
|
|
145
|
-
|
|
147
|
+
// Keep Verlet displacement in one time unit even when GPU frames arrive
|
|
148
|
+
// unevenly; carry unused time rather than changing the integration step.
|
|
149
|
+
const h = 1 / 60;
|
|
150
|
+
this.accumulator += Math.min(Math.max(seconds, 0), .05);
|
|
151
|
+
const steps = Math.floor((this.accumulator + 1e-12) / h);
|
|
152
|
+
this.accumulator = Math.max(0, this.accumulator - steps * h);
|
|
146
153
|
const profile = windProfile(windStrength), pressureScale = profile.load ? .025 * profile.surfaceLoad / profile.load : 0;
|
|
147
154
|
const radius = Math.max(1e-4, ...colliders.map(collider => collider.radius));
|
|
148
155
|
const cell = radius * 2;
|
|
@@ -153,7 +160,7 @@ export class ClothSurface {
|
|
|
153
160
|
const p = collider.position, id = key(Math.floor(p[0] / cell), Math.floor(p[1] / cell), Math.floor(p[2] / cell));
|
|
154
161
|
const bin = bins.get(id) ?? []; bin.push(collider); bins.set(id, bin);
|
|
155
162
|
}
|
|
156
|
-
for (let step = 0; step < steps
|
|
163
|
+
for (let step = 0; step < steps; step++) {
|
|
157
164
|
this.time += h;
|
|
158
165
|
const force = Array.from({ length: count }, () => new Vector3());
|
|
159
166
|
const area = new Float32Array(count);
|
|
@@ -194,22 +201,26 @@ export class ClothSurface {
|
|
|
194
201
|
}
|
|
195
202
|
if (colliders.length) for (const particle of this.particles) {
|
|
196
203
|
if (particle.share < .35) continue;
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
let
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
const
|
|
209
|
-
|
|
204
|
+
// A point projected out of one proxy sphere can still be inside its
|
|
205
|
+
// neighbor. Resolve that overlap here instead of alternating contacts
|
|
206
|
+
// on successive animation frames.
|
|
207
|
+
for (let contact = 0; contact < 8; contact++) {
|
|
208
|
+
const p = particle.position, px = p.x / cell, py = p.y / cell, pz = p.z / cell;
|
|
209
|
+
const x = Math.floor(px), y = Math.floor(py), z = Math.floor(pz);
|
|
210
|
+
const sx = px - x < .5 ? -1 : 1, sy = py - y < .5 ? -1 : 1, sz = pz - z < .5 ? -1 : 1;
|
|
211
|
+
let chosen: ClothCollider | undefined, penetration = 0;
|
|
212
|
+
// Cell width is twice the largest radius, so only the nearest neighbor
|
|
213
|
+
// on each axis can contain a sphere reaching this particle.
|
|
214
|
+
for (let iz = 0; iz < 2; iz++) for (let iy = 0; iy < 2; iy++) for (let ix = 0; ix < 2; ix++) {
|
|
215
|
+
for (const collider of bins.get(key(x + ix * sx, y + iy * sy, z + iz * sz)) ?? []) {
|
|
216
|
+
const center = collider.position, bx = particle.base.x - center[0], by = particle.base.y - center[1], bz = particle.base.z - center[2];
|
|
217
|
+
if (bx * bx + by * by + bz * bz < collider.radius * collider.radius * 1.04) continue;
|
|
218
|
+
const vx = p.x - center[0], vy = p.y - center[1], vz = p.z - center[2];
|
|
219
|
+
const depth = collider.radius - Math.hypot(vx, vy, vz);
|
|
220
|
+
if (depth > penetration) { penetration = depth; chosen = collider; }
|
|
221
|
+
}
|
|
210
222
|
}
|
|
211
|
-
|
|
212
|
-
if (chosen) {
|
|
223
|
+
if (!chosen || penetration < this.scale * 1e-7) break;
|
|
213
224
|
const center = chosen.position, delta = p.clone().sub(new Vector3().fromArray(center));
|
|
214
225
|
if (delta.lengthSq() < 1e-12) delta.copy(particle.base).sub(new Vector3().fromArray(center));
|
|
215
226
|
p.copy(new Vector3().fromArray(center).add(delta.setLength(chosen.radius)));
|
|
@@ -56,7 +56,8 @@ export class PinCloth {
|
|
|
56
56
|
private particles = new Map<string, { position: Vector3; previous: Vector3 }>();
|
|
57
57
|
private layout = '';
|
|
58
58
|
private windTime = 0;
|
|
59
|
-
|
|
59
|
+
private accumulator = 0;
|
|
60
|
+
reset() { this.particles.clear(); this.layout = ''; this.windTime = 0; this.accumulator = 0; }
|
|
60
61
|
step(pins: readonly PinRecord[], parents: PinParents, seconds: number, links: readonly ClothLink[], colliders: readonly ClothCollider[] = [], windStrength = 0): { pins: readonly PinRecord[]; active: boolean } {
|
|
61
62
|
const cloth = pins.filter(p => p.cloth && !p.guide);
|
|
62
63
|
if (!cloth.length) { this.reset(); return { pins, active: false }; }
|
|
@@ -67,7 +68,12 @@ export class PinCloth {
|
|
|
67
68
|
const position = new Vector3().fromArray(pin.pose.position);
|
|
68
69
|
this.particles.set(pin.id, { position, previous: position.clone() });
|
|
69
70
|
}
|
|
70
|
-
|
|
71
|
+
// Verlet stores displacement per step. Changing the step with render
|
|
72
|
+
// timing changes its velocity and keeps a calm sheet moving.
|
|
73
|
+
const h = 1 / 60;
|
|
74
|
+
this.accumulator += Math.min(Math.max(seconds, 0), .05);
|
|
75
|
+
const steps = Math.floor((this.accumulator + 1e-12) / h);
|
|
76
|
+
this.accumulator = Math.max(0, this.accumulator - steps * h);
|
|
71
77
|
const scale = Math.max(1e-4, ...links.map(link => link.length));
|
|
72
78
|
// A sampled voxel shell can contain thousands of spheres. Bin it once
|
|
73
79
|
// per frame, then refresh each particle's nearby set after integration so
|
|
@@ -95,7 +101,7 @@ export class PinCloth {
|
|
|
95
101
|
} else for (const collider of colliders) search(collider);
|
|
96
102
|
return found;
|
|
97
103
|
};
|
|
98
|
-
for (let substep = 0; substep < steps
|
|
104
|
+
for (let substep = 0; substep < steps; substep++) {
|
|
99
105
|
this.windTime += h;
|
|
100
106
|
for (const pin of cloth) {
|
|
101
107
|
const particle = this.particles.get(pin.id)!;
|