@threenative/three-native 0.3.11 → 0.3.13
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/package.json +2 -2
- package/src/addons/ccd-ik-solver.ts +170 -0
- package/src/addons/gltf-loader-web.ts +132 -26
- package/src/addons/screen-space.ts +61 -24
- package/src/animation-utils.ts +366 -0
- package/src/box2.ts +172 -0
- package/src/bridge-meter.ts +33 -12
- package/src/browser-abi-types.ts +621 -0
- package/src/browser-attribute-sync.ts +249 -0
- package/src/browser-backend.ts +266 -1650
- package/src/browser-entry.ts +79 -22
- package/src/browser-loaders.ts +12 -0
- package/src/browser-marshal.ts +79 -0
- package/src/browser-renderer.ts +297 -87
- package/src/browser-tsl.ts +146 -23
- package/src/browser-value-views.ts +634 -0
- package/src/browser-wasm-runtime.ts +525 -0
- package/src/buffer-attributes.ts +19 -0
- package/src/compressed-texture.ts +116 -0
- package/src/debug-telemetry.ts +23 -6
- package/src/edges-geometry.ts +111 -0
- package/src/generated/console-markers.ts +13 -0
- package/src/generated/web-frame-fields.ts +84 -0
- package/src/gpu-stages.ts +17 -16
- package/src/grid-helper.ts +49 -0
- package/src/object-surface.ts +47 -6
- package/src/pass-node.ts +354 -10
- package/src/render-target.ts +515 -16
- package/src/shadow-node.ts +24 -1
- package/src/storage.ts +16 -5
- package/src/texture-sources.ts +80 -23
- package/src/tsl-extras.ts +50 -0
- package/src/tsl-uniforms.ts +294 -63
- package/src/update-ranges.ts +50 -1
package/src/texture-sources.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type EngineValue,
|
|
3
|
+
type IBrowserRuntime,
|
|
4
|
+
type IEngineRef,
|
|
5
|
+
engineRef,
|
|
6
|
+
} from "./browser-backend.js";
|
|
1
7
|
/**
|
|
2
8
|
* three's texture sources on the browser back end: the typed array a `DataTexture` was built
|
|
3
9
|
* from, a 2D canvas, and an image (`ImageBitmap`, `<img>`, `ImageData`). The engine holds a copy of
|
|
@@ -5,12 +11,7 @@
|
|
|
5
11
|
* before an upload. The V8 player's equivalent is `runtime-native/src/engine/player/core-textures.mjs`;
|
|
6
12
|
* the browser decodes images itself, so no bitmap needs the engine decoder here.
|
|
7
13
|
*/
|
|
8
|
-
import {
|
|
9
|
-
type EngineValue,
|
|
10
|
-
type IBrowserRuntime,
|
|
11
|
-
type IEngineRef,
|
|
12
|
-
engineRef,
|
|
13
|
-
} from "./browser-backend.js";
|
|
14
|
+
import { defineCompressedTexture } from "./compressed-texture.js";
|
|
14
15
|
|
|
15
16
|
type EngineClass = new (...args: unknown[]) => object;
|
|
16
17
|
|
|
@@ -23,9 +24,10 @@ interface IPixels {
|
|
|
23
24
|
interface ICanvasLike {
|
|
24
25
|
readonly width: number;
|
|
25
26
|
readonly height: number;
|
|
26
|
-
getContext(
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
getContext(kind: "2d"): {
|
|
28
|
+
getImageData(x: number, y: number, w: number, h: number): IPixels;
|
|
29
|
+
getContextAttributes?(): { willReadFrequently?: boolean };
|
|
30
|
+
} | null;
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
/** three's Texture defaults: an image is flipped, linear-filtered and mipmapped, unlike DataTexture's. */
|
|
@@ -40,26 +42,34 @@ function isPixels(image: unknown): image is IPixels {
|
|
|
40
42
|
return ArrayBuffer.isView(candidate?.data) && typeof candidate?.width === "number";
|
|
41
43
|
}
|
|
42
44
|
|
|
43
|
-
/** An image's RGBA pixels: its own `data`, a canvas
|
|
45
|
+
/** An image's RGBA pixels: its own `data`, a CPU-backed canvas read directly, or a drawable copied. */
|
|
44
46
|
function pixelsOf(image: unknown): IPixels {
|
|
45
47
|
if (isPixels(image)) return image;
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
const own = isCanvas(image) ? image.getContext("2d") : null;
|
|
49
|
+
// A canvas made `willReadFrequently` is CPU-backed, so its own readback is exact and needs no copy,
|
|
50
|
+
// which keeps a canvas redrawn and re-sent each frame at one getImageData.
|
|
51
|
+
if (own?.getContextAttributes?.().willReadFrequently === true)
|
|
52
|
+
return own.getImageData(0, 0, (image as ICanvasLike).width, (image as ICanvasLike).height);
|
|
51
53
|
const drawable = image as { width?: number; height?: number } | null;
|
|
52
|
-
if (
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
54
|
+
if (typeof OffscreenCanvas !== "undefined" && drawable?.width && drawable.height) {
|
|
55
|
+
// A GPU-backed canvas does not hand its pixels back: measured on Chromium over Vulkan,
|
|
56
|
+
// `getImageData` of a red `fillRect` reads 0/0/0 there and 255/0/0 in a `willReadFrequently`
|
|
57
|
+
// canvas, which uploaded every `assets.texture()` as black (the Strata tundra ground). A canvas
|
|
58
|
+
// made with `document.createElement` and a default 2D context is GPU-backed, and a webgl canvas
|
|
59
|
+
// has no 2D context at all. Drawing either into a CPU-backed canvas is a plain copy the browser
|
|
60
|
+
// performs correctly, whatever context the caller created.
|
|
61
|
+
const context = new OffscreenCanvas(drawable.width, drawable.height).getContext("2d", {
|
|
62
|
+
willReadFrequently: true,
|
|
63
|
+
});
|
|
59
64
|
context?.drawImage(image as CanvasImageSource, 0, 0);
|
|
60
65
|
const pixels = context?.getImageData(0, 0, drawable.width, drawable.height);
|
|
61
66
|
if (pixels !== undefined) return pixels;
|
|
62
67
|
}
|
|
68
|
+
if (own !== null) {
|
|
69
|
+
// No OffscreenCanvas to copy into: the caller's canvas is the only readback left.
|
|
70
|
+
const { width, height } = image as ICanvasLike;
|
|
71
|
+
return own.getImageData(0, 0, width, height);
|
|
72
|
+
}
|
|
63
73
|
throw new TypeError(
|
|
64
74
|
"TN_BROWSER_TEXTURE_SOURCE: expected ImageData, a canvas with a 2D context, or an image OffscreenCanvas can draw",
|
|
65
75
|
);
|
|
@@ -72,12 +82,17 @@ export function defineTextureSources(
|
|
|
72
82
|
): Record<string, unknown> {
|
|
73
83
|
const BaseTexture = classes.Texture as EngineClass;
|
|
74
84
|
const BaseVolume = classes.Data3DTexture as EngineClass | undefined;
|
|
85
|
+
const BaseLayers = classes.DataArrayTexture as EngineClass | undefined;
|
|
75
86
|
const BaseData = classes.DataTexture as EngineClass;
|
|
76
87
|
const BaseCanvas = classes.CanvasTexture as EngineClass | undefined;
|
|
88
|
+
const CompressedTexture = defineCompressedTexture(
|
|
89
|
+
classes.CompressedTexture as EngineClass | undefined,
|
|
90
|
+
runtime,
|
|
91
|
+
);
|
|
77
92
|
if (BaseCanvas === undefined) throw new TypeError("TN_BROWSER_UNBOUND: CanvasTexture");
|
|
78
93
|
const sources = new WeakMap<object, unknown>();
|
|
79
94
|
const prototype = BaseData.prototype as object;
|
|
80
|
-
for (const Native of [BaseTexture, BaseData, BaseCanvas, BaseVolume]) {
|
|
95
|
+
for (const Native of [BaseTexture, BaseData, BaseCanvas, BaseVolume, BaseLayers]) {
|
|
81
96
|
if (Native === undefined) continue;
|
|
82
97
|
const target = Native.prototype as { clone(): object };
|
|
83
98
|
const clone = target.clone;
|
|
@@ -87,6 +102,19 @@ export function defineTextureSources(
|
|
|
87
102
|
return result;
|
|
88
103
|
};
|
|
89
104
|
}
|
|
105
|
+
// three's `texture.source`: `dataReady = false` tells renderer.initTexture that copyTextureToTexture writes the texels.
|
|
106
|
+
const dataSources = new WeakMap<object, { dataReady: boolean }>();
|
|
107
|
+
for (const Native of [BaseTexture, BaseData, BaseCanvas, BaseVolume, BaseLayers]) {
|
|
108
|
+
if (Native === undefined) continue;
|
|
109
|
+
Object.defineProperty(Native.prototype, "source", {
|
|
110
|
+
configurable: true,
|
|
111
|
+
get(this: object) {
|
|
112
|
+
const source = dataSources.get(this) ?? { dataReady: true };
|
|
113
|
+
dataSources.set(this, source);
|
|
114
|
+
return source;
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
}
|
|
90
118
|
/** `image` is the JS source; `needsUpdate` re-sends its pixels, as three re-reads `texture.image`. */
|
|
91
119
|
const followSource = (target: object, name: string): void => {
|
|
92
120
|
const needsUpdate = Object.getOwnPropertyDescriptor(target, "needsUpdate")?.set;
|
|
@@ -122,6 +150,7 @@ export function defineTextureSources(
|
|
|
122
150
|
};
|
|
123
151
|
followSource(prototype, "DataTexture");
|
|
124
152
|
if (BaseVolume !== undefined) followSource(BaseVolume.prototype as object, "Data3DTexture");
|
|
153
|
+
if (BaseLayers !== undefined) followSource(BaseLayers.prototype as object, "DataArrayTexture");
|
|
125
154
|
followSource(BaseCanvas.prototype as object, "CanvasTexture");
|
|
126
155
|
|
|
127
156
|
/** An engine DataTexture holding `pixels`, re-read from `image` on every needsUpdate. */
|
|
@@ -172,6 +201,32 @@ export function defineTextureSources(
|
|
|
172
201
|
}
|
|
173
202
|
if (BaseVolume !== undefined) Data3DTexture.prototype = BaseVolume.prototype;
|
|
174
203
|
|
|
204
|
+
/** three's DataArrayTexture(data, width, height, depth): the engine's own class, `depth` layers of RGBA texels. */
|
|
205
|
+
function DataArrayTexture(
|
|
206
|
+
this: unknown,
|
|
207
|
+
data?: ArrayLike<number> | null,
|
|
208
|
+
width = 1,
|
|
209
|
+
height = 1,
|
|
210
|
+
depth = 1,
|
|
211
|
+
) {
|
|
212
|
+
if (BaseLayers === undefined) throw new TypeError("TN_BROWSER_UNBOUND: DataArrayTexture");
|
|
213
|
+
const texture = Reflect.construct(
|
|
214
|
+
BaseLayers,
|
|
215
|
+
[data ?? undefined, width, height, depth],
|
|
216
|
+
new.target ?? DataArrayTexture,
|
|
217
|
+
);
|
|
218
|
+
sources.set(texture, { data: data ?? null, width, height, depth });
|
|
219
|
+
return texture;
|
|
220
|
+
}
|
|
221
|
+
if (BaseLayers !== undefined) {
|
|
222
|
+
DataArrayTexture.prototype = BaseLayers.prototype;
|
|
223
|
+
// three's flags: r185 names the array class both ways.
|
|
224
|
+
Object.assign(BaseLayers.prototype as object, {
|
|
225
|
+
isArrayTexture: true,
|
|
226
|
+
isDataArrayTexture: true,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
175
230
|
/** three's CanvasTexture: the engine's own class over the canvas's pixels, with a full mip chain. */
|
|
176
231
|
function CanvasTexture(this: unknown, canvas: unknown, ...rest: unknown[]) {
|
|
177
232
|
const pixels = pixelsOf(canvas);
|
|
@@ -265,8 +320,10 @@ export function defineTextureSources(
|
|
|
265
320
|
|
|
266
321
|
return {
|
|
267
322
|
CanvasTexture,
|
|
323
|
+
...(CompressedTexture === undefined ? {} : { CompressedTexture }),
|
|
268
324
|
DataTexture,
|
|
269
325
|
...(BaseVolume === undefined ? {} : { Data3DTexture }),
|
|
326
|
+
...(BaseLayers === undefined ? {} : { DataArrayTexture }),
|
|
270
327
|
ImageBitmapLoader,
|
|
271
328
|
Texture,
|
|
272
329
|
TextureLoader,
|
package/src/tsl-extras.ts
CHANGED
|
@@ -7,6 +7,16 @@ interface IExtrasTsl {
|
|
|
7
7
|
readonly clamp: (value: unknown, low: number, high: number) => unknown;
|
|
8
8
|
readonly float: (value: number) => IFogNode;
|
|
9
9
|
readonly linearDepth: () => IFogNode;
|
|
10
|
+
// quality-allow: three's TSL export name.
|
|
11
|
+
// biome-ignore lint/style/useNamingConvention: three's TSL export name.
|
|
12
|
+
readonly mx_noise_float: (position: unknown) => IFogNode;
|
|
13
|
+
readonly vec3: (value: unknown) => IFogNode;
|
|
14
|
+
readonly vec2: (
|
|
15
|
+
x: unknown,
|
|
16
|
+
y?: unknown,
|
|
17
|
+
) => IFogNode & { readonly x: IFogNode; readonly y: IFogNode };
|
|
18
|
+
readonly cos: (value: unknown) => IFogNode;
|
|
19
|
+
readonly sin: (value: unknown) => IFogNode;
|
|
10
20
|
readonly vec4: (color: unknown, factor: unknown) => IFogNode;
|
|
11
21
|
}
|
|
12
22
|
|
|
@@ -43,6 +53,46 @@ export function defineTslExtras(tsl: IExtrasTsl) {
|
|
|
43
53
|
Object.defineProperty(node, "isFogNode", { value: true });
|
|
44
54
|
return node;
|
|
45
55
|
},
|
|
56
|
+
// r187's mx_fractal_noise_float(position, octaves, lacunarity, diminish, amplitude)
|
|
57
|
+
// (materialx/MaterialXNoise.js): the octave sum of amplitude * perlin(p), amplitude *= diminish and
|
|
58
|
+
// p *= lacunarity per octave, over p as a vec3. The octave count is a number here, so the loop is
|
|
59
|
+
// unrolled; a node count refuses by name.
|
|
60
|
+
// quality-allow: three's TSL export name.
|
|
61
|
+
// biome-ignore lint/style/useNamingConvention: three's TSL export name.
|
|
62
|
+
mx_fractal_noise_float: (
|
|
63
|
+
position: unknown,
|
|
64
|
+
octaves: unknown = 3,
|
|
65
|
+
lacunarity: number | IFogNode = 2,
|
|
66
|
+
diminish: number | IFogNode = 0.5,
|
|
67
|
+
amplitude: number | IFogNode = 1,
|
|
68
|
+
) => {
|
|
69
|
+
if (typeof octaves !== "number" || !Number.isInteger(octaves) || octaves < 0 || octaves > 16)
|
|
70
|
+
throw new Error(
|
|
71
|
+
"TN_NATIVE_FRACTAL_OCTAVES: mx_fractal_noise_float needs an integer octave count 0..16 on the native engine",
|
|
72
|
+
);
|
|
73
|
+
const node = (value: number | IFogNode) =>
|
|
74
|
+
typeof value === "number" ? tsl.float(value) : value;
|
|
75
|
+
let p = tsl.vec3(position);
|
|
76
|
+
let weight = tsl.float(1);
|
|
77
|
+
let sum = tsl.float(0);
|
|
78
|
+
for (let octave = 0; octave < octaves; octave += 1) {
|
|
79
|
+
sum = sum.add(weight.mul(tsl.mx_noise_float(p)));
|
|
80
|
+
weight = weight.mul(node(diminish));
|
|
81
|
+
p = p.mul(node(lacunarity));
|
|
82
|
+
}
|
|
83
|
+
return sum.mul(node(amplitude));
|
|
84
|
+
},
|
|
85
|
+
// r187's rotateUV(uv, rotation, center = vec2(0.5)) (utils/UVUtils.js): rotate(uv - center, rotation) + center,
|
|
86
|
+
// where RotateNode turns a vec2 by mat2(cos, sin, -sin, cos).
|
|
87
|
+
rotateUV: (uv: unknown, rotation: unknown, center: unknown = tsl.vec2(0.5, 0.5)) => {
|
|
88
|
+
const p = tsl.vec2(uv).sub(center) as IFogNode & {
|
|
89
|
+
readonly x: IFogNode;
|
|
90
|
+
readonly y: IFogNode;
|
|
91
|
+
};
|
|
92
|
+
const c = tsl.cos(rotation);
|
|
93
|
+
const s = tsl.sin(rotation);
|
|
94
|
+
return tsl.vec2(c.mul(p.x).sub(s.mul(p.y)), s.mul(p.x).add(c.mul(p.y))).add(center);
|
|
95
|
+
},
|
|
46
96
|
// context() and velocity: the context that carries the previous-frame velocity, and that accessor.
|
|
47
97
|
// The native engine has neither, so they refuse at use, by name.
|
|
48
98
|
context: unsupportedExport("context"),
|