@threenative/three-native 0.0.0-stage → 0.3.6
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 +24 -2
- package/package.json +42 -3
- package/src/addons/buffer-geometry-utils.ts +14 -0
- package/src/addons/data-utils.ts +63 -0
- package/src/addons/decal-geometry.ts +25 -0
- package/src/addons/decal.ts +218 -0
- package/src/addons/gltf-loader-web.ts +323 -0
- package/src/addons/hdr-loader.ts +71 -0
- package/src/addons/hdr.ts +139 -0
- package/src/addons/lut-cube-loader-web.ts +17 -0
- package/src/addons/lut-cube-loader.ts +149 -0
- package/src/addons/merge-geometries.ts +374 -0
- package/src/addons/mesh-bvh-webgpu.ts +13 -0
- package/src/addons/mesh-bvh.ts +50 -0
- package/src/addons/post-effects-web.ts +22 -0
- package/src/addons/post-effects.ts +173 -0
- package/src/audio.ts +564 -0
- package/src/blend-mode.ts +33 -0
- package/src/browser-backend.ts +2156 -0
- package/src/browser-entry.ts +305 -0
- package/src/browser-math.ts +448 -0
- package/src/browser-renderer.ts +527 -0
- package/src/browser-tsl.ts +650 -0
- package/src/catalog.ts +497 -0
- package/src/fixture-format.ts +721 -0
- package/src/fixture-native.ts +426 -0
- package/src/fixture-protocol.ts +315 -0
- package/src/math-utils.ts +65 -0
- package/src/object-surface.ts +250 -0
- package/src/pass-node.ts +286 -0
- package/src/property-binding.ts +93 -0
- package/src/quad-mesh.ts +54 -0
- package/src/reflector.ts +89 -0
- package/src/refused.ts +27 -0
- package/src/render-target.ts +107 -0
- package/src/rtt.ts +158 -0
- package/src/shadow-map.ts +21 -0
- package/src/storage.ts +206 -0
- package/src/texture-sources.ts +252 -0
- package/src/tsl-extras.ts +51 -0
- package/src/tsl-struct.ts +20 -0
- package/src/tsl-uniforms.ts +234 -0
- package/src/update-ranges.ts +109 -0
- package/src/wgsl-fn.ts +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 João Paulo Furtado
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @threenative/three-native
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
ThreeNative's native compatibility catalog and TypeScript adapter sources. The repository's
|
|
4
|
+
generators use the catalog to produce TypeScript declarations and the native C binding ABI.
|
|
5
|
+
Compatibility entries distinguish supported, partial and unsupported operations.
|
|
6
|
+
|
|
7
|
+
This package is an engine implementation component. Games use `@threenative/core` and the
|
|
8
|
+
ordinary `three` API; this package currently exports only its package metadata.
|
|
9
|
+
|
|
10
|
+
## Repository development
|
|
11
|
+
|
|
12
|
+
From the ThreeNative workspace:
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
pnpm --filter @threenative/three-native generate
|
|
16
|
+
pnpm --filter @threenative/three-native typecheck
|
|
17
|
+
pnpm --filter @threenative/three-native test:reference
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
The reference command checks compatibility against Three.js. A catalog status alone does not
|
|
21
|
+
prove a particular platform; use the native conformance and playtest results for that platform.
|
|
22
|
+
|
|
23
|
+
## License
|
|
24
|
+
|
|
25
|
+
MIT.
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@threenative/three-native",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "0.3.6",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "The native engine's machine-readable compatibility catalog and the generators that adapt it to TypeScript declarations and the C binding ABI.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"threenative",
|
|
8
|
+
"three",
|
|
9
|
+
"native",
|
|
10
|
+
"abi",
|
|
11
|
+
"gamedev"
|
|
12
|
+
],
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/ThreeNativeHQ/threenative.git",
|
|
16
|
+
"directory": "packages/three-native"
|
|
17
|
+
},
|
|
18
|
+
"type": "module",
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/three": "0.185.3",
|
|
21
|
+
"publint": "0.3.22",
|
|
22
|
+
"three": "0.185.1",
|
|
23
|
+
"typescript": "npm:@typescript/typescript6@6.0.2"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"three": "0.185.1"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"src/**/*.ts",
|
|
30
|
+
"README.md"
|
|
31
|
+
],
|
|
32
|
+
"exports": {
|
|
33
|
+
"./package.json": "./package.json"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"generate": "pnpm --workspace-root exec tsx packages/three-native/scripts/generate.ts",
|
|
40
|
+
"sync-native-status": "pnpm --workspace-root exec tsx packages/three-native/scripts/sync-native-status.ts",
|
|
41
|
+
"test": "publint --strict",
|
|
42
|
+
"test:reference": "pnpm --workspace-root exec tsx packages/three-native/tests/compatibility/run-reference.ts",
|
|
43
|
+
"typecheck": "pnpm run generate && tsc --noEmit -p tsconfig.json"
|
|
44
|
+
}
|
|
6
45
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `three/addons/utils/BufferGeometryUtils.js` as the engine back ends provide it: three's
|
|
3
|
+
* mergeGeometries, mergeAttributes and mergeVertices (merge-geometries.ts) over whichever engine `three` names in
|
|
4
|
+
* this build, the V8 facade or the Wasm entry. The module's other utilities are not provided, so an
|
|
5
|
+
* import of one fails the build.
|
|
6
|
+
*/
|
|
7
|
+
import { BufferAttribute, BufferGeometry } from "three";
|
|
8
|
+
|
|
9
|
+
import { defineBufferGeometryUtils } from "./merge-geometries.js";
|
|
10
|
+
|
|
11
|
+
export const { mergeGeometries, mergeAttributes, mergeVertices } = defineBufferGeometryUtils({
|
|
12
|
+
BufferAttribute,
|
|
13
|
+
BufferGeometry,
|
|
14
|
+
} as never);
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* three's `DataUtils` (r185, MIT): binary16 conversion as plain JS, shared by both engine back ends.
|
|
3
|
+
*
|
|
4
|
+
* It touches no engine object, so it runs as JS instead of crossing the ABI once per value; a ripple
|
|
5
|
+
* field packs 147,456 values per update. The tables are three's, so every result is bit-identical,
|
|
6
|
+
* including its truncation (not rounding) to binary16 and its clamp to ±65504.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const floatView = new Float32Array(1);
|
|
10
|
+
const uint32View = new Uint32Array(floatView.buffer);
|
|
11
|
+
const baseTable = new Uint32Array(512);
|
|
12
|
+
const shiftTable = new Uint32Array(512);
|
|
13
|
+
const mantissaTable = new Uint32Array(2048);
|
|
14
|
+
const exponentTable = new Uint32Array(64);
|
|
15
|
+
const offsetTable = new Uint32Array(64);
|
|
16
|
+
|
|
17
|
+
for (let i = 0; i < 256; ++i) {
|
|
18
|
+
const e = i - 127;
|
|
19
|
+
let base: number;
|
|
20
|
+
let shift: number;
|
|
21
|
+
if (e < -27) [base, shift] = [0x0000, 24];
|
|
22
|
+
else if (e < -14) [base, shift] = [0x0400 >> (-e - 14), -e - 1];
|
|
23
|
+
else if (e <= 15) [base, shift] = [(e + 15) << 10, 13];
|
|
24
|
+
else [base, shift] = [0x7c00, e < 128 ? 24 : 13];
|
|
25
|
+
baseTable[i] = base;
|
|
26
|
+
baseTable[i | 0x100] = base | 0x8000;
|
|
27
|
+
shiftTable[i] = shiftTable[i | 0x100] = shift;
|
|
28
|
+
}
|
|
29
|
+
for (let i = 1; i < 1024; ++i) {
|
|
30
|
+
let m = i << 13;
|
|
31
|
+
let e = 0;
|
|
32
|
+
while ((m & 0x00800000) === 0) {
|
|
33
|
+
m <<= 1;
|
|
34
|
+
e -= 0x00800000;
|
|
35
|
+
}
|
|
36
|
+
mantissaTable[i] = (m & ~0x00800000) | (e + 0x38800000);
|
|
37
|
+
}
|
|
38
|
+
for (let i = 1024; i < 2048; ++i) mantissaTable[i] = 0x38000000 + ((i - 1024) << 13);
|
|
39
|
+
for (let i = 1; i < 31; ++i) exponentTable[i] = i << 23;
|
|
40
|
+
exponentTable[31] = 0x47800000;
|
|
41
|
+
exponentTable[32] = 0x80000000;
|
|
42
|
+
for (let i = 33; i < 63; ++i) exponentTable[i] = 0x80000000 + ((i - 32) << 23);
|
|
43
|
+
exponentTable[63] = 0xc7800000;
|
|
44
|
+
for (let i = 1; i < 64; ++i) if (i !== 32) offsetTable[i] = 1024;
|
|
45
|
+
|
|
46
|
+
/** A number's binary16 bits, as three stores them in a `HalfFloatType` Uint16Array. */
|
|
47
|
+
export function toHalfFloat(value: number): number {
|
|
48
|
+
floatView[0] = Math.max(-65504, Math.min(65504, value));
|
|
49
|
+
const f = uint32View[0] ?? 0;
|
|
50
|
+
const e = (f >> 23) & 0x1ff;
|
|
51
|
+
return (baseTable[e] ?? 0) + ((f & 0x007fffff) >> (shiftTable[e] ?? 0));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The number binary16 `bits` encode. */
|
|
55
|
+
export function fromHalfFloat(bits: number): number {
|
|
56
|
+
const m = bits >> 10;
|
|
57
|
+
uint32View[0] =
|
|
58
|
+
(mantissaTable[(offsetTable[m] ?? 0) + (bits & 0x3ff)] ?? 0) + (exponentTable[m] ?? 0);
|
|
59
|
+
return floatView[0] ?? 0;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** three's static `DataUtils` namespace. */
|
|
63
|
+
export const DataUtils = { toHalfFloat, fromHalfFloat } as const;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `three/addons/geometries/DecalGeometry.js` as the engine back ends provide it: three's clipping
|
|
3
|
+
* (decal.ts) over whichever engine `three` names in this build, the V8 facade or the Wasm entry.
|
|
4
|
+
*/
|
|
5
|
+
import {
|
|
6
|
+
BufferGeometry,
|
|
7
|
+
Euler,
|
|
8
|
+
Float32BufferAttribute,
|
|
9
|
+
Matrix3,
|
|
10
|
+
Matrix4,
|
|
11
|
+
Mesh,
|
|
12
|
+
Vector3,
|
|
13
|
+
} from "three";
|
|
14
|
+
|
|
15
|
+
import { defineDecalGeometry } from "./decal.js";
|
|
16
|
+
|
|
17
|
+
export const { DecalGeometry, DecalVertex } = defineDecalGeometry({
|
|
18
|
+
BufferGeometry,
|
|
19
|
+
Euler,
|
|
20
|
+
Float32BufferAttribute,
|
|
21
|
+
Matrix3,
|
|
22
|
+
Matrix4,
|
|
23
|
+
Mesh,
|
|
24
|
+
Vector3,
|
|
25
|
+
} as never);
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// Ported from three.js r185 (three@0.185.1). The MIT License, Copyright © 2010-2026 three.js authors.
|
|
2
|
+
/**
|
|
3
|
+
* `three/addons/geometries/DecalGeometry.js` over the classes `T` names: three's own clipping
|
|
4
|
+
* algorithm, for both back ends. The upstream file is plain JavaScript over those classes, but a
|
|
5
|
+
* published addon cannot enter the native bundle, so the port lives here and says nothing different.
|
|
6
|
+
* decal-geometry.ts binds it to the engine's classes.
|
|
7
|
+
*/
|
|
8
|
+
import type * as Three from "three";
|
|
9
|
+
|
|
10
|
+
type Classes = Pick<
|
|
11
|
+
typeof Three,
|
|
12
|
+
"BufferGeometry" | "Euler" | "Float32BufferAttribute" | "Matrix3" | "Matrix4" | "Mesh" | "Vector3"
|
|
13
|
+
>;
|
|
14
|
+
|
|
15
|
+
export function defineDecalGeometry(T: Classes) {
|
|
16
|
+
const { BufferGeometry, Euler, Float32BufferAttribute, Matrix3, Matrix4, Mesh, Vector3 } = T;
|
|
17
|
+
type Vector3 = Three.Vector3;
|
|
18
|
+
type Euler = Three.Euler;
|
|
19
|
+
type Mesh = Three.Mesh;
|
|
20
|
+
|
|
21
|
+
/** One vertex of a clipped face: a position, and a normal when the source geometry has them. */
|
|
22
|
+
class DecalVertex {
|
|
23
|
+
constructor(
|
|
24
|
+
public position: Vector3,
|
|
25
|
+
public normal: Vector3 | null = null,
|
|
26
|
+
) {}
|
|
27
|
+
|
|
28
|
+
clone(): DecalVertex {
|
|
29
|
+
return new DecalVertex(
|
|
30
|
+
this.position.clone(),
|
|
31
|
+
this.normal === null ? null : this.normal.clone(),
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Where the clip plane `p` (offset `s`) cuts the edge `v0` to `v1`. */
|
|
37
|
+
function clip(v0: DecalVertex, v1: DecalVertex, p: Vector3, s: number): DecalVertex {
|
|
38
|
+
const d0 = v0.position.dot(p) - s;
|
|
39
|
+
const d1 = v1.position.dot(p) - s;
|
|
40
|
+
const s0 = d0 / (d0 - d1);
|
|
41
|
+
const position = new Vector3(
|
|
42
|
+
v0.position.x + s0 * (v1.position.x - v0.position.x),
|
|
43
|
+
v0.position.y + s0 * (v1.position.y - v0.position.y),
|
|
44
|
+
v0.position.z + s0 * (v1.position.z - v0.position.z),
|
|
45
|
+
);
|
|
46
|
+
let normal: Vector3 | null = null;
|
|
47
|
+
if (v0.normal !== null && v1.normal !== null)
|
|
48
|
+
normal = new Vector3(
|
|
49
|
+
v0.normal.x + s0 * (v1.normal.x - v0.normal.x),
|
|
50
|
+
v0.normal.y + s0 * (v1.normal.y - v0.normal.y),
|
|
51
|
+
v0.normal.z + s0 * (v1.normal.z - v0.normal.z),
|
|
52
|
+
);
|
|
53
|
+
return new DecalVertex(position, normal);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Clips every face (three consecutive vertices) against one side of the projector box. */
|
|
57
|
+
// quality-allow: three's own clipping stays in three's shape so the port reads against upstream.
|
|
58
|
+
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: three's own clipping, kept in three's shape so the port reads against upstream.
|
|
59
|
+
function clipGeometry(inVertices: DecalVertex[], plane: Vector3, size: Vector3): DecalVertex[] {
|
|
60
|
+
const outVertices: DecalVertex[] = [];
|
|
61
|
+
const s = 0.5 * Math.abs(size.dot(plane));
|
|
62
|
+
for (let i = 0; i < inVertices.length; i += 3) {
|
|
63
|
+
const a = inVertices[i] as DecalVertex;
|
|
64
|
+
const b = inVertices[i + 1] as DecalVertex;
|
|
65
|
+
const c = inVertices[i + 2] as DecalVertex;
|
|
66
|
+
const v1Out = a.position.dot(plane) - s > 0;
|
|
67
|
+
const v2Out = b.position.dot(plane) - s > 0;
|
|
68
|
+
const v3Out = c.position.dot(plane) - s > 0;
|
|
69
|
+
const total = (v1Out ? 1 : 0) + (v2Out ? 1 : 0) + (v3Out ? 1 : 0);
|
|
70
|
+
if (total === 0) {
|
|
71
|
+
// The whole face lies inside the plane.
|
|
72
|
+
outVertices.push(a, b, c);
|
|
73
|
+
} else if (total === 1) {
|
|
74
|
+
// One vertex lies outside: the face becomes two.
|
|
75
|
+
let nV1: DecalVertex;
|
|
76
|
+
let nV2: DecalVertex;
|
|
77
|
+
let nV3: DecalVertex;
|
|
78
|
+
let nV4: DecalVertex;
|
|
79
|
+
if (v1Out) {
|
|
80
|
+
nV1 = b;
|
|
81
|
+
nV2 = c;
|
|
82
|
+
nV3 = clip(a, nV1, plane, s);
|
|
83
|
+
nV4 = clip(a, nV2, plane, s);
|
|
84
|
+
} else if (v2Out) {
|
|
85
|
+
nV1 = a;
|
|
86
|
+
nV2 = c;
|
|
87
|
+
nV3 = clip(b, nV1, plane, s);
|
|
88
|
+
nV4 = clip(b, nV2, plane, s);
|
|
89
|
+
outVertices.push(nV3, nV2.clone(), nV1.clone(), nV2.clone(), nV3.clone(), nV4);
|
|
90
|
+
continue;
|
|
91
|
+
} else {
|
|
92
|
+
nV1 = a;
|
|
93
|
+
nV2 = b;
|
|
94
|
+
nV3 = clip(c, nV1, plane, s);
|
|
95
|
+
nV4 = clip(c, nV2, plane, s);
|
|
96
|
+
}
|
|
97
|
+
outVertices.push(nV1.clone(), nV2.clone(), nV3, nV4, nV3.clone(), nV2.clone());
|
|
98
|
+
} else if (total === 2) {
|
|
99
|
+
// Two vertices lie outside: one corner is left.
|
|
100
|
+
if (!v1Out) {
|
|
101
|
+
const nV1 = a.clone();
|
|
102
|
+
outVertices.push(nV1, clip(nV1, b, plane, s), clip(nV1, c, plane, s));
|
|
103
|
+
}
|
|
104
|
+
if (!v2Out) {
|
|
105
|
+
const nV1 = b.clone();
|
|
106
|
+
outVertices.push(nV1, clip(nV1, c, plane, s), clip(nV1, a, plane, s));
|
|
107
|
+
}
|
|
108
|
+
if (!v3Out) {
|
|
109
|
+
const nV1 = c.clone();
|
|
110
|
+
outVertices.push(nV1, clip(nV1, a, plane, s), clip(nV1, b, plane, s));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// Three outside: the face is dropped.
|
|
114
|
+
}
|
|
115
|
+
return outVertices;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* A decal mesh's geometry: `mesh`'s faces clipped to the box a projector at `position` and
|
|
120
|
+
* `orientation` with `size` covers, with uvs across that box.
|
|
121
|
+
*/
|
|
122
|
+
class DecalGeometry extends BufferGeometry {
|
|
123
|
+
constructor(
|
|
124
|
+
mesh: Mesh = new Mesh(),
|
|
125
|
+
position: Vector3 = new Vector3(),
|
|
126
|
+
orientation: Euler = new Euler(),
|
|
127
|
+
size: Vector3 = new Vector3(1, 1, 1),
|
|
128
|
+
) {
|
|
129
|
+
super();
|
|
130
|
+
const vertices: number[] = [];
|
|
131
|
+
const normals: number[] = [];
|
|
132
|
+
const uvs: number[] = [];
|
|
133
|
+
const plane = new Vector3();
|
|
134
|
+
const normalMatrix = new Matrix3().getNormalMatrix(mesh.matrixWorld);
|
|
135
|
+
// The projector's transformation, and its inverse.
|
|
136
|
+
const projectorMatrix = new Matrix4();
|
|
137
|
+
projectorMatrix.makeRotationFromEuler(orientation);
|
|
138
|
+
projectorMatrix.setPosition(position);
|
|
139
|
+
const projectorMatrixInverse = new Matrix4().copy(projectorMatrix).invert();
|
|
140
|
+
|
|
141
|
+
// A vertex, taken to world space and then to projector space.
|
|
142
|
+
const pushDecalVertex = (
|
|
143
|
+
decalVertices: DecalVertex[],
|
|
144
|
+
vertex: Vector3,
|
|
145
|
+
normal?: Vector3,
|
|
146
|
+
): void => {
|
|
147
|
+
vertex.applyMatrix4(mesh.matrixWorld);
|
|
148
|
+
vertex.applyMatrix4(projectorMatrixInverse);
|
|
149
|
+
if (normal !== undefined) {
|
|
150
|
+
normal.applyNormalMatrix(normalMatrix);
|
|
151
|
+
decalVertices.push(new DecalVertex(vertex.clone(), normal.clone()));
|
|
152
|
+
} else {
|
|
153
|
+
decalVertices.push(new DecalVertex(vertex.clone()));
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
// quality-allow: three's own clipping stays in three's shape so the port reads against upstream.
|
|
158
|
+
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: three's own clipping, kept in three's shape so the port reads against upstream.
|
|
159
|
+
const generate = (): void => {
|
|
160
|
+
let decalVertices: DecalVertex[] = [];
|
|
161
|
+
const vertex = new Vector3();
|
|
162
|
+
const normal = new Vector3();
|
|
163
|
+
const geometry = mesh.geometry;
|
|
164
|
+
const positionAttribute = geometry.getAttribute("position");
|
|
165
|
+
const normalAttribute = geometry.getAttribute("normal");
|
|
166
|
+
if (!positionAttribute) return; // empty geometry (three throws on an indexed one with no positions)
|
|
167
|
+
// Three consecutive DecalVertex objects are one face: the clipping works on faces.
|
|
168
|
+
if (geometry.index !== null) {
|
|
169
|
+
const index = geometry.index;
|
|
170
|
+
for (let i = 0; i < index.count; i += 1) {
|
|
171
|
+
vertex.fromBufferAttribute(positionAttribute, index.getX(i));
|
|
172
|
+
if (normalAttribute) {
|
|
173
|
+
normal.fromBufferAttribute(normalAttribute, index.getX(i));
|
|
174
|
+
pushDecalVertex(decalVertices, vertex, normal);
|
|
175
|
+
} else {
|
|
176
|
+
pushDecalVertex(decalVertices, vertex);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
} else {
|
|
180
|
+
for (let i = 0; i < positionAttribute.count; i += 1) {
|
|
181
|
+
vertex.fromBufferAttribute(positionAttribute, i);
|
|
182
|
+
if (normalAttribute) {
|
|
183
|
+
normal.fromBufferAttribute(normalAttribute, i);
|
|
184
|
+
pushDecalVertex(decalVertices, vertex, normal);
|
|
185
|
+
} else {
|
|
186
|
+
pushDecalVertex(decalVertices, vertex);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
for (const axis of [
|
|
191
|
+
[1, 0, 0],
|
|
192
|
+
[-1, 0, 0],
|
|
193
|
+
[0, 1, 0],
|
|
194
|
+
[0, -1, 0],
|
|
195
|
+
[0, 0, 1],
|
|
196
|
+
[0, 0, -1],
|
|
197
|
+
] as const)
|
|
198
|
+
decalVertices = clipGeometry(decalVertices, plane.set(axis[0], axis[1], axis[2]), size);
|
|
199
|
+
for (const decalVertex of decalVertices) {
|
|
200
|
+
// Still in projector space: the uvs span the box, then the vertex goes back to world space.
|
|
201
|
+
uvs.push(0.5 + decalVertex.position.x / size.x, 0.5 + decalVertex.position.y / size.y);
|
|
202
|
+
decalVertex.position.applyMatrix4(projectorMatrix);
|
|
203
|
+
vertices.push(decalVertex.position.x, decalVertex.position.y, decalVertex.position.z);
|
|
204
|
+
if (decalVertex.normal !== null)
|
|
205
|
+
normals.push(decalVertex.normal.x, decalVertex.normal.y, decalVertex.normal.z);
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
generate();
|
|
210
|
+
this.setAttribute("position", new Float32BufferAttribute(new Float32Array(vertices), 3));
|
|
211
|
+
this.setAttribute("uv", new Float32BufferAttribute(new Float32Array(uvs), 2));
|
|
212
|
+
if (normals.length > 0)
|
|
213
|
+
this.setAttribute("normal", new Float32BufferAttribute(new Float32Array(normals), 3));
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return { DecalGeometry, DecalVertex };
|
|
218
|
+
}
|