@needle-tools/three-mesh-effects 0.3.7 → 0.4.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/CHANGELOG.md +7 -0
- package/README.md +14 -1
- package/build/box-impostor-runtime.js +321 -195
- package/build/box-impostor-runtime.js.map +4 -4
- package/build/impostor-runtime.js +944 -202
- package/build/impostor-runtime.js.map +4 -4
- package/build/index.js +7 -7
- package/build/index.js.map +2 -2
- package/build/internal/box-impostor/box-impostor-display.js +319 -193
- package/build/internal/box-impostor/box-impostor-display.js.map +4 -4
- package/build/internal/box-impostor/box-impostor-faces.js +8 -6
- package/build/internal/box-impostor/box-impostor-faces.js.map +2 -2
- package/build/internal/box-impostor/box-impostor-material.js +319 -193
- package/build/internal/box-impostor/box-impostor-material.js.map +4 -4
- package/build/internal/box-impostor/box-impostor-projection.js +8 -5
- package/build/internal/box-impostor/box-impostor-projection.js.map +2 -2
- package/build/internal/box-impostor/box-impostor-runtime.js +321 -195
- package/build/internal/box-impostor/box-impostor-runtime.js.map +4 -4
- package/build/internal/impostor/adaptive-impostor-renderer.js +434 -0
- package/build/internal/impostor/adaptive-impostor-renderer.js.map +7 -0
- package/build/internal/impostor/impostor-node-material.js +944 -202
- package/build/internal/impostor/impostor-node-material.js.map +4 -4
- package/build/internal/impostor/impostor-performance.js +136 -0
- package/build/internal/impostor/impostor-performance.js.map +7 -0
- package/build/internal/pixel-impostor/pixel-impostor-runtime.js +6 -1
- package/build/internal/pixel-impostor/pixel-impostor-runtime.js.map +2 -2
- package/build/internal/rendering/instance-space.js +110 -0
- package/build/internal/rendering/instance-space.js.map +7 -0
- package/build/internal/rendering/renderer-support.js +6 -1
- package/build/internal/rendering/renderer-support.js.map +2 -2
- package/build/pixel-impostor-runtime.js +6 -1
- package/build/pixel-impostor-runtime.js.map +2 -2
- package/build/types/box-impostor/box-impostor-faces.d.ts +3 -1
- package/build/types/box-impostor/box-impostor-projection.d.ts +3 -1
- package/build/types/impostor/adaptive-impostor-renderer.d.ts +74 -0
- package/build/types/impostor/adaptive-impostor-renderer.js +1 -0
- package/build/types/impostor/impostor-node-material.d.ts +5 -1
- package/build/types/impostor/impostor-performance.d.ts +90 -0
- package/build/types/impostor/impostor-performance.js +1 -0
- package/build/types/public/impostor-runtime.d.ts +2 -0
- package/build/types/public/index.d.ts +3 -0
- package/build/types/rendering/instance-space.d.ts +26 -0
- package/build/types/rendering/instance-space.js +1 -0
- package/build/types/rendering/renderer-support.d.ts +13 -0
- package/package.json +2 -2
- package/src/box-impostor/box-impostor-faces.js +7 -5
- package/src/box-impostor/box-impostor-material.js +34 -19
- package/src/box-impostor/box-impostor-projection.js +7 -4
- package/src/impostor/adaptive-impostor-renderer.js +423 -0
- package/src/impostor/impostor-node-material.js +347 -157
- package/src/impostor/impostor-performance.js +152 -0
- package/src/public/impostor-runtime.js +2 -0
- package/src/public/index.js +12 -9
- package/src/rendering/instance-space.js +121 -0
- package/src/rendering/renderer-support.js +9 -0
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { OrthographicCamera } from 'three';
|
|
4
|
+
|
|
5
|
+
/** @import {Camera, Sphere} from 'three' */
|
|
6
|
+
export const IMPOSTOR_PERFORMANCE_PRESETS = /** @type {const} */ ({
|
|
7
|
+
fast: {
|
|
8
|
+
interpolation: true,
|
|
9
|
+
reprojection: false,
|
|
10
|
+
depthWeights: false,
|
|
11
|
+
perspectiveCorrection: true,
|
|
12
|
+
cellCorrection: false,
|
|
13
|
+
boundedReprojection: true,
|
|
14
|
+
raySamples: 0,
|
|
15
|
+
depthSamples: 0,
|
|
16
|
+
},
|
|
17
|
+
medium: {
|
|
18
|
+
interpolation: true,
|
|
19
|
+
reprojection: true,
|
|
20
|
+
depthWeights: true,
|
|
21
|
+
perspectiveCorrection: true,
|
|
22
|
+
cellCorrection: false,
|
|
23
|
+
boundedReprojection: true,
|
|
24
|
+
raySamples: 8,
|
|
25
|
+
depthSamples: 2,
|
|
26
|
+
},
|
|
27
|
+
high: {
|
|
28
|
+
interpolation: true,
|
|
29
|
+
reprojection: true,
|
|
30
|
+
depthWeights: true,
|
|
31
|
+
perspectiveCorrection: true,
|
|
32
|
+
cellCorrection: false,
|
|
33
|
+
boundedReprojection: true,
|
|
34
|
+
raySamples: 16,
|
|
35
|
+
depthSamples: 4,
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
export const IMPOSTOR_PERFORMANCE_TIERS = /** @type {const} */ (['fast', 'medium', 'high']);
|
|
40
|
+
/** @typedef {typeof IMPOSTOR_PERFORMANCE_TIERS[number]} FixedPerformancePreset */
|
|
41
|
+
/**
|
|
42
|
+
* @typedef {object} ImpostorAutoOptions
|
|
43
|
+
* @property {number | false} [frameBudgetMs] Whole-renderer frame budget in milliseconds. Use false for size-based selection alone.
|
|
44
|
+
*/
|
|
45
|
+
/** @typedef {'auto' | 'fixed' | ImpostorAutoOptions} ImpostorPerformance */
|
|
46
|
+
const DEFAULT_FRAME_BUDGET_MS = 1000 / 60;
|
|
47
|
+
|
|
48
|
+
/** @param {ImpostorPerformance} performance */
|
|
49
|
+
export function resolveImpostorFrameBudget(performance) {
|
|
50
|
+
const budget = typeof performance === 'object'
|
|
51
|
+
? performance.frameBudgetMs ?? DEFAULT_FRAME_BUDGET_MS
|
|
52
|
+
: DEFAULT_FRAME_BUDGET_MS;
|
|
53
|
+
if (budget !== false && (!Number.isFinite(budget) || budget <= 0)) {
|
|
54
|
+
throw new RangeError('Impostor frameBudgetMs must be a positive finite number or false.');
|
|
55
|
+
}
|
|
56
|
+
return budget;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The same diameter can keep either adjacent tier near a boundary. Zooming
|
|
61
|
+
* back and forth inside that interval keeps the compiled program stable.
|
|
62
|
+
* @param {number} pixels
|
|
63
|
+
* @param {FixedPerformancePreset | 'custom' | null} previous
|
|
64
|
+
* @param {number=} pressure
|
|
65
|
+
* @param {number=} ceiling
|
|
66
|
+
* @returns {FixedPerformancePreset}
|
|
67
|
+
*/
|
|
68
|
+
export function selectImpostorPerformance(pixels, previous, pressure = 1, ceiling = 2) {
|
|
69
|
+
return IMPOSTOR_PERFORMANCE_TIERS[selectImpostorTier(
|
|
70
|
+
pixels, previous === 'high' ? 2 : previous === 'fast' ? 0 : 1, pressure, ceiling,
|
|
71
|
+
)];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** @param {number} pixels @param {number} previous @param {number=} pressure @param {number=} ceiling */
|
|
75
|
+
export function selectImpostorTier(pixels, previous, pressure = 1, ceiling = 2) {
|
|
76
|
+
if (ceiling >= 2 && pixels >= pressure * (previous === 2 ? 160 : 208)) return 2;
|
|
77
|
+
return ceiling >= 1 && pixels >= pressure * (previous === 0 ? 96 : 72) ? 1 : 0;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @param {number} radius @param {number} depth
|
|
82
|
+
* @param {number} scale @param {boolean} orthographic
|
|
83
|
+
*/
|
|
84
|
+
export function impostorPixelDiameter(radius, depth, scale, orthographic) {
|
|
85
|
+
if (orthographic) return radius * scale;
|
|
86
|
+
return depth <= -radius ? 0 : radius * scale / Math.max(radius, depth, 0.0001);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** @param {Sphere} sphere @param {Camera} camera @param {number} viewportHeight */
|
|
90
|
+
export function projectedImpostorDiameter(sphere, camera, viewportHeight) {
|
|
91
|
+
const scale = viewportHeight * Math.abs(camera.projectionMatrix.elements[5]);
|
|
92
|
+
const { x, y, z } = sphere.center;
|
|
93
|
+
const view = camera.matrixWorldInverse.elements;
|
|
94
|
+
const depth = -(view[2] * x + view[6] * y + view[10] * z + view[14]);
|
|
95
|
+
return impostorPixelDiameter(sphere.radius, depth, scale, camera instanceof OrthographicCamera);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
/** Smooth frame pressure across the renderer's display frames. GPU timing is
|
|
100
|
+
* optional and owned by the host profiler. Reading it never adds a query.
|
|
101
|
+
* @param {number} [budget]
|
|
102
|
+
*/
|
|
103
|
+
export function createImpostorFrameBudget(budget = DEFAULT_FRAME_BUDGET_MS) {
|
|
104
|
+
const minimumInterval = Math.min(6, budget / 2);
|
|
105
|
+
const backlogThreshold = budget * (32 / DEFAULT_FRAME_BUDGET_MS);
|
|
106
|
+
let previous = -1, average = budget, pressure = 1;
|
|
107
|
+
let slow = 0, headroom = 0, ceiling = 2;
|
|
108
|
+
return {
|
|
109
|
+
get pressure() { return pressure; },
|
|
110
|
+
get ceiling() { return ceiling; },
|
|
111
|
+
/** @param {number} time @param {number=} gpuMilliseconds @param {number=} queueLag */
|
|
112
|
+
observe(time, gpuMilliseconds = 0, queueLag = 0) {
|
|
113
|
+
if (previous < 0) {
|
|
114
|
+
previous = time;
|
|
115
|
+
return pressure;
|
|
116
|
+
}
|
|
117
|
+
const elapsed = time - previous;
|
|
118
|
+
if (elapsed >= 0 && elapsed < minimumInterval) return pressure;
|
|
119
|
+
previous = time;
|
|
120
|
+
// A suspended tab or a pause between renders is not sustained load.
|
|
121
|
+
if (elapsed > 250 || elapsed <= 0) {
|
|
122
|
+
average = budget;
|
|
123
|
+
slow = headroom = 0;
|
|
124
|
+
return pressure;
|
|
125
|
+
}
|
|
126
|
+
average += (elapsed - average) * 0.14;
|
|
127
|
+
if (queueLag > backlogThreshold) gpuMilliseconds = Math.max(gpuMilliseconds, queueLag);
|
|
128
|
+
const slowFrame = elapsed > budget * 1.13 && average > budget * 1.13;
|
|
129
|
+
if (slowFrame || gpuMilliseconds > budget * 0.95) {
|
|
130
|
+
headroom = 0;
|
|
131
|
+
slow += Math.min(elapsed, 100);
|
|
132
|
+
if (slow >= 160) {
|
|
133
|
+
// At very low FPS, respond in a fraction of a second. If even the
|
|
134
|
+
// widest thresholds can't help, cap the tier for close-up cards.
|
|
135
|
+
if (pressure >= 8) ceiling = Math.max(0, ceiling - 1);
|
|
136
|
+
else pressure = Math.min(8, pressure * Math.max(1.3, Math.min(2.5, average / budget)));
|
|
137
|
+
slow = 0;
|
|
138
|
+
}
|
|
139
|
+
} else if (average < budget * 1.06 && gpuMilliseconds < budget * 0.8) {
|
|
140
|
+
slow = 0;
|
|
141
|
+
if (++headroom >= 60) {
|
|
142
|
+
if (ceiling < 2) ceiling++;
|
|
143
|
+
else pressure = Math.max(1, pressure / 1.18);
|
|
144
|
+
headroom = 0;
|
|
145
|
+
}
|
|
146
|
+
} else {
|
|
147
|
+
slow = headroom = 0;
|
|
148
|
+
}
|
|
149
|
+
return pressure;
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
@@ -19,3 +19,5 @@ export {
|
|
|
19
19
|
/** @typedef {import('../impostor/impostor-node-material.js').ImpostorRuntimeFeatures} ImpostorRuntimeFeatures */
|
|
20
20
|
/** @typedef {import('../impostor/impostor-node-material.js').ResolvedImpostorRuntimeFeatures} ResolvedImpostorRuntimeFeatures */
|
|
21
21
|
/** @typedef {import('../impostor/impostor-node-material.js').ImpostorRenderer} ImpostorRenderer */
|
|
22
|
+
/** @typedef {import('../impostor/impostor-performance.js').ImpostorPerformance} ImpostorPerformance */
|
|
23
|
+
/** @typedef {import('../impostor/impostor-performance.js').ImpostorAutoOptions} ImpostorAutoOptions */
|
package/src/public/index.js
CHANGED
|
@@ -6,9 +6,11 @@
|
|
|
6
6
|
/** @typedef {import('../impostor/impostor-node-material.js').ImpostorRenderer} Renderer */
|
|
7
7
|
|
|
8
8
|
/** @typedef {'NEEDLE_deformation_rig' | 'NEEDLE_mesh_impostor' | 'NEEDLE_pixel_impostor' | 'NEEDLE_hull_effect' | 'NEEDLE_box_impostor'} NeedleExtension */
|
|
9
|
-
/** @typedef {
|
|
9
|
+
/** @typedef {import('../impostor/impostor-performance.js').ImpostorPerformance} ImpostorPerformance */
|
|
10
|
+
/** @typedef {import('../impostor/impostor-performance.js').ImpostorAutoOptions} ImpostorAutoOptions */
|
|
11
|
+
/** @typedef {{disable?: readonly NeedleExtension[], impostorPerformance?: ImpostorPerformance}} LoaderOptions */
|
|
10
12
|
|
|
11
|
-
/** @type {WeakMap<GLTFLoader, {renderer: Renderer, disabled: Set<NeedleExtension
|
|
13
|
+
/** @type {WeakMap<GLTFLoader, {renderer: Renderer, disabled: Set<NeedleExtension>, impostorPerformance: ImpostorPerformance}>} */
|
|
12
14
|
const registrations = new WeakMap();
|
|
13
15
|
|
|
14
16
|
/**
|
|
@@ -27,18 +29,19 @@ const registrations = new WeakMap();
|
|
|
27
29
|
*/
|
|
28
30
|
export function useLoader(loader, renderer, options = {}) {
|
|
29
31
|
const disabled = new Set(options.disable);
|
|
32
|
+
const registration = { renderer, disabled, impostorPerformance: options.impostorPerformance ?? 'auto' };
|
|
30
33
|
const existing = registrations.get(loader);
|
|
31
|
-
if (existing) {
|
|
32
|
-
const state =
|
|
34
|
+
if (existing) { Object.assign(existing, registration); return loader; }
|
|
35
|
+
const state = registration;
|
|
33
36
|
registrations.set(loader, state);
|
|
34
37
|
|
|
35
38
|
// Register each real extension name synchronously so GLTFLoader recognizes
|
|
36
39
|
// required extensions before asynchronous postprocessing starts.
|
|
37
|
-
/** @param {NeedleExtension} name @param {(parser: GLTFParser, result: GLTF, renderer: Renderer) => Promise<void>} apply */
|
|
40
|
+
/** @param {NeedleExtension} name @param {(parser: GLTFParser, result: GLTF, renderer: Renderer, impostorPerformance: ImpostorPerformance) => Promise<void>} apply */
|
|
38
41
|
const register = (name, apply) => {
|
|
39
42
|
loader.register(parser => {
|
|
40
43
|
const disabled = state.disabled.has(name);
|
|
41
|
-
const renderer = state
|
|
44
|
+
const { renderer, impostorPerformance } = state;
|
|
42
45
|
return {
|
|
43
46
|
name,
|
|
44
47
|
async afterRoot(result) {
|
|
@@ -53,7 +56,7 @@ export function useLoader(loader, renderer, options = {}) {
|
|
|
53
56
|
const {enableRendererNodeMaterials} = await import('@needle-tools/three-mesh-effects/internal/rendering/renderer-support');
|
|
54
57
|
await enableRendererNodeMaterials(renderer);
|
|
55
58
|
}
|
|
56
|
-
await apply(parser, result, renderer);
|
|
59
|
+
await apply(parser, result, renderer, impostorPerformance);
|
|
57
60
|
},
|
|
58
61
|
};
|
|
59
62
|
});
|
|
@@ -62,9 +65,9 @@ export function useLoader(loader, renderer, options = {}) {
|
|
|
62
65
|
const { NeedleDeformationRigLoaderPlugin } = await import('@needle-tools/three-mesh-effects/deformation-runtime');
|
|
63
66
|
await new NeedleDeformationRigLoaderPlugin(parser).afterRoot(result);
|
|
64
67
|
});
|
|
65
|
-
register('NEEDLE_mesh_impostor', async (parser, result, renderer) => {
|
|
68
|
+
register('NEEDLE_mesh_impostor', async (parser, result, renderer, performance) => {
|
|
66
69
|
const { NeedleImpostorLoaderPlugin } = await import('@needle-tools/three-mesh-effects/impostor-runtime');
|
|
67
|
-
await new NeedleImpostorLoaderPlugin(parser, { renderer }).afterRoot(result);
|
|
70
|
+
await new NeedleImpostorLoaderPlugin(parser, { renderer, performance }).afterRoot(result);
|
|
68
71
|
});
|
|
69
72
|
register('NEEDLE_pixel_impostor', async (parser, result, renderer) => {
|
|
70
73
|
const { NeedlePixelImpostorLoaderPlugin } = await import('@needle-tools/three-mesh-effects/pixel-impostor-runtime');
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { DataTexture, DynamicDrawUsage, FloatType, InstancedInterleavedBuffer, InterpolationSamplingType, NodeUpdateType, RGBAFormat } from 'three/webgpu';
|
|
3
|
+
import { cross, dot, instanceIndex, instancedBufferAttribute, instancedDynamicBufferAttribute, int, ivec2, textureLoad, varying, vec3, vec4 } from 'three/tsl';
|
|
4
|
+
|
|
5
|
+
/** @import {InstancedBufferAttribute, InstancedMesh, Node, NodeBuilder} from 'three/webgpu' */
|
|
6
|
+
/** @typedef {Node<'vec3'>} Vec3Node */
|
|
7
|
+
/** @typedef {(value: Vec3Node) => Vec3Node} Transform */
|
|
8
|
+
/** @typedef {{toLocalPoint: Transform, toLocalDirection: Transform, toMeshPoint: Transform, toMeshDirection: Transform}} CoordinateSpace */
|
|
9
|
+
/** @typedef {CoordinateSpace & {fragment: CoordinateSpace & {toMeshNormal: Transform}}} InstanceSpace */
|
|
10
|
+
|
|
11
|
+
/** @type {WeakMap<InstancedBufferAttribute, InstanceSpace>} */
|
|
12
|
+
const spaces = new WeakMap();
|
|
13
|
+
/** @type {WeakMap<InstancedBufferAttribute, InstanceSpace>} */
|
|
14
|
+
const webglSpaces = new WeakMap();
|
|
15
|
+
|
|
16
|
+
/** @param {InstancedMesh} object @param {InstancedBufferAttribute} matrix @param {boolean} useTexture */
|
|
17
|
+
function instanceColumns(object, matrix, useTexture) {
|
|
18
|
+
const columns = [0, 1, 2, 3];
|
|
19
|
+
if (useTexture) {
|
|
20
|
+
// Once Three's instance transforms move from uniforms into attributes, a
|
|
21
|
+
// second set of matrix attributes can exceed WebGL2's vertex-slot limit.
|
|
22
|
+
const rowSize = Math.ceil(Math.sqrt(matrix.count) / 2);
|
|
23
|
+
const rows = Math.ceil(matrix.count / rowSize);
|
|
24
|
+
const data = new Float32Array(rowSize * rows * 16);
|
|
25
|
+
const texture = new DataTexture(data, rowSize * 4, rows, RGBAFormat, FloatType);
|
|
26
|
+
const upload = () => {
|
|
27
|
+
data.set(matrix.array);
|
|
28
|
+
texture.needsUpdate = true;
|
|
29
|
+
};
|
|
30
|
+
upload();
|
|
31
|
+
const release = () => {
|
|
32
|
+
object.removeEventListener('dispose', release);
|
|
33
|
+
texture.dispose();
|
|
34
|
+
webglSpaces.delete(matrix);
|
|
35
|
+
};
|
|
36
|
+
object.addEventListener('dispose', release);
|
|
37
|
+
const x = instanceIndex.mod(rowSize).mul(4);
|
|
38
|
+
const y = instanceIndex.div(rowSize);
|
|
39
|
+
return { nodes: columns.map(column => textureLoad(texture, ivec2(int(x.add(column)), int(y)))), upload };
|
|
40
|
+
}
|
|
41
|
+
const interleaved = new InstancedInterleavedBuffer(matrix.array, 16, 1);
|
|
42
|
+
const read = matrix.usage === DynamicDrawUsage ? instancedDynamicBufferAttribute : instancedBufferAttribute;
|
|
43
|
+
return {
|
|
44
|
+
nodes: columns.map(column => read(interleaved, /** @type {const} */ ('vec4'), 16, column * 4)),
|
|
45
|
+
upload: () => { interleaved.needsUpdate = true; },
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Nodes that move between an InstancedMesh's own space and the space of one
|
|
50
|
+
* instance, so a material can trace in its captured coordinates and still
|
|
51
|
+
* light, depth-test and shadow in the mesh's. Returns null for other objects.
|
|
52
|
+
*
|
|
53
|
+
* `toLocal*` and `toMesh*` run in the vertex stage; wrap their result in
|
|
54
|
+
* `varying()` to use it in the fragment stage. `fragment` holds the same
|
|
55
|
+
* conversions for values that only exist per fragment.
|
|
56
|
+
* @param {NodeBuilder & {getUniformBufferLimit?: () => number}} builder */
|
|
57
|
+
export function instanceSpace(builder) {
|
|
58
|
+
const object = /** @type {InstancedMesh} */ (builder.object);
|
|
59
|
+
const matrix = object?.isInstancedMesh ? object.instanceMatrix : null;
|
|
60
|
+
if (!matrix) return null;
|
|
61
|
+
const backend = /** @type {{isWebGPUBackend?: boolean} | undefined} */ (builder.renderer.backend);
|
|
62
|
+
const useTexture = !backend?.isWebGPUBackend
|
|
63
|
+
&& matrix.count * 64 > (builder.getUniformBufferLimit?.() ?? 16_384);
|
|
64
|
+
const cache = useTexture ? webglSpaces : spaces;
|
|
65
|
+
const cached = cache.get(matrix);
|
|
66
|
+
if (cached) return cached;
|
|
67
|
+
const { nodes, upload } = instanceColumns(object, matrix, useTexture);
|
|
68
|
+
let version = matrix.version;
|
|
69
|
+
nodes[0].updateBeforeType = NodeUpdateType.OBJECT;
|
|
70
|
+
nodes[0].updateBefore = () => {
|
|
71
|
+
if (matrix.version !== version) {
|
|
72
|
+
version = matrix.version;
|
|
73
|
+
upload();
|
|
74
|
+
}
|
|
75
|
+
return undefined;
|
|
76
|
+
};
|
|
77
|
+
const [a, b, c, d] = nodes.map((node) => node.xyz);
|
|
78
|
+
const determinant = dot(a, cross(b, c));
|
|
79
|
+
const rows = [cross(b, c), cross(c, a), cross(a, b)].map((row) => row.div(determinant));
|
|
80
|
+
/** @type {Transform} */
|
|
81
|
+
const toLocalDirection = (v) => vec3(dot(rows[0], v), dot(rows[1], v), dot(rows[2], v));
|
|
82
|
+
/** @type {Transform} */
|
|
83
|
+
const toMeshDirection = (v) => a.mul(v.x).add(b.mul(v.y)).add(c.mul(v.z));
|
|
84
|
+
// Three outputs carry the whole transform; shaders with many per-box
|
|
85
|
+
// attributes have few to spare.
|
|
86
|
+
// Flat: the transform is one value per instance, and interpolating equal
|
|
87
|
+
// values still perturbs them enough to move a traced hit.
|
|
88
|
+
const translation = [d.x, d.y, d.z];
|
|
89
|
+
const packed = [a, b, c].map((column, k) =>
|
|
90
|
+
varying(vec4(column, translation[k]), `instanceSpace${k}`).setInterpolation(InterpolationSamplingType.FLAT),
|
|
91
|
+
);
|
|
92
|
+
const [fa, fb, fc] = packed.map((column) => column.xyz);
|
|
93
|
+
const cofactors = () => [cross(fb, fc), cross(fc, fa), cross(fa, fb)];
|
|
94
|
+
/** @type {InstanceSpace} */
|
|
95
|
+
const space = {
|
|
96
|
+
toLocalPoint: (p) => toLocalDirection(p.sub(d)),
|
|
97
|
+
toLocalDirection,
|
|
98
|
+
toMeshPoint: (p) => toMeshDirection(p).add(d),
|
|
99
|
+
toMeshDirection,
|
|
100
|
+
fragment: {
|
|
101
|
+
toMeshPoint: (p) =>
|
|
102
|
+
fa.mul(p.x).add(fb.mul(p.y)).add(fc.mul(p.z)).add(vec3(packed[0].w, packed[1].w, packed[2].w)),
|
|
103
|
+
toMeshDirection: (v) => fa.mul(v.x).add(fb.mul(v.y)).add(fc.mul(v.z)),
|
|
104
|
+
toMeshNormal: (n) => {
|
|
105
|
+
const [x, y, z] = cofactors();
|
|
106
|
+
return x.mul(n.x).add(y.mul(n.y)).add(z.mul(n.z)).mul(dot(fa, x).sign()).normalize();
|
|
107
|
+
},
|
|
108
|
+
toLocalDirection: (v) => {
|
|
109
|
+
const [x, y, z] = cofactors();
|
|
110
|
+
return vec3(dot(x, v), dot(y, v), dot(z, v)).div(dot(fa, x));
|
|
111
|
+
},
|
|
112
|
+
toLocalPoint: (p) => {
|
|
113
|
+
const [x, y, z] = cofactors();
|
|
114
|
+
const v = p.sub(vec3(packed[0].w, packed[1].w, packed[2].w));
|
|
115
|
+
return vec3(dot(x, v), dot(y, v), dot(z, v)).div(dot(fa, x));
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
cache.set(matrix, space);
|
|
120
|
+
return space;
|
|
121
|
+
}
|
|
@@ -24,6 +24,15 @@ export function rendererSupportsImpostorNodeMaterials(renderer) {
|
|
|
24
24
|
|| renderer?.capabilities?.supportsNodeMaterials === true;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
/** Classic WebGL exposes MSAA on the target or the canvas context.
|
|
28
|
+
* @param {{currentSamples?: number, getRenderTarget?: () => {samples: number} | null,
|
|
29
|
+
* getContextAttributes?: () => {antialias?: boolean} | null}} renderer
|
|
30
|
+
*/
|
|
31
|
+
export function rendererUsesMultisampleCoverage(renderer) {
|
|
32
|
+
const samples = renderer.currentSamples ?? renderer.getRenderTarget?.()?.samples;
|
|
33
|
+
return samples === undefined ? renderer.getContextAttributes?.()?.antialias === true : samples > 1;
|
|
34
|
+
}
|
|
35
|
+
|
|
27
36
|
/** @type {WeakMap<ImpostorRenderer, Promise<void>>} */
|
|
28
37
|
const pending = new WeakMap();
|
|
29
38
|
|