@vim3d/threejs 0.2.0 → 0.2.2
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/README.md +9 -0
- package/audio.d.ts +84 -0
- package/audio.js +453 -0
- package/esm/audio.js +449 -0
- package/esm/geometry.js +74 -1
- package/esm/index.js +2 -1
- package/esm/player.js +23 -1
- package/esm/scene.js +21 -10
- package/geometry.d.ts +3 -1
- package/geometry.js +75 -0
- package/index.d.ts +4 -2
- package/index.js +7 -3
- package/package.json +2 -2
- package/player.d.ts +9 -2
- package/player.js +23 -1
- package/scene.js +20 -9
package/geometry.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isLine = isLine;
|
|
4
4
|
exports.createGeometry = createGeometry;
|
|
5
|
+
exports.disposeProceduralGeometry = disposeProceduralGeometry;
|
|
6
|
+
exports.updateProceduralGeometry = updateProceduralGeometry;
|
|
5
7
|
const three_1 = require("three");
|
|
6
8
|
function isLine(primitive) {
|
|
7
9
|
return primitive === 'curve' || primitive === 'ellipse' || primitive === 'circle';
|
|
@@ -66,3 +68,76 @@ function createGeometry({ primitive, args, vertices }) {
|
|
|
66
68
|
default: throw new Error('Unsupported Vim3D geometry: ' + primitive);
|
|
67
69
|
}
|
|
68
70
|
}
|
|
71
|
+
function numbers(value, count) {
|
|
72
|
+
return Array.isArray(value) && value.length === count && Array.from(value).every(Number.isFinite);
|
|
73
|
+
}
|
|
74
|
+
function disposeProceduralGeometry(root) {
|
|
75
|
+
root.traverse(object => {
|
|
76
|
+
if (object instanceof three_1.Mesh)
|
|
77
|
+
object.geometry.dispose();
|
|
78
|
+
});
|
|
79
|
+
root.clear();
|
|
80
|
+
}
|
|
81
|
+
function updateProceduralGeometry(root, descriptor, material) {
|
|
82
|
+
const ancestors = new Set();
|
|
83
|
+
function updateChildren(group, descriptors) {
|
|
84
|
+
const previous = [...group.children];
|
|
85
|
+
const next = [];
|
|
86
|
+
for (const child of descriptors) {
|
|
87
|
+
const object = update(previous[next.length], child);
|
|
88
|
+
if (object)
|
|
89
|
+
next.push(object);
|
|
90
|
+
}
|
|
91
|
+
if (previous.length === next.length && previous.every((object, i) => object === next[i]))
|
|
92
|
+
return;
|
|
93
|
+
group.clear();
|
|
94
|
+
for (let i = 0; i < previous.length; i++) {
|
|
95
|
+
if (previous[i] !== next[i])
|
|
96
|
+
disposeProceduralGeometry(previous[i]);
|
|
97
|
+
}
|
|
98
|
+
for (const object of next)
|
|
99
|
+
group.add(object);
|
|
100
|
+
}
|
|
101
|
+
function update(previous, value) {
|
|
102
|
+
if (!value || typeof value !== 'object' || Array.isArray(value) || ancestors.has(value))
|
|
103
|
+
return;
|
|
104
|
+
const { kind, primitive, args, geometry, geometries, translate, scale } = value;
|
|
105
|
+
if (primitive === 'box' && numbers(args, 3)) {
|
|
106
|
+
const [width, height, depth] = args;
|
|
107
|
+
if (previous instanceof three_1.Mesh && previous.geometry instanceof three_1.BoxGeometry) {
|
|
108
|
+
const p = previous.geometry.parameters;
|
|
109
|
+
if (p.width === width && p.height === height && p.depth === depth)
|
|
110
|
+
return previous;
|
|
111
|
+
}
|
|
112
|
+
return new three_1.Mesh(new three_1.BoxGeometry(width, height, depth), material);
|
|
113
|
+
}
|
|
114
|
+
if (primitive === 'sphere' && numbers(args, 3)) {
|
|
115
|
+
const [radius, , detail] = args;
|
|
116
|
+
const widthSegments = segments(detail, 3), heightSegments = segments(detail / 2, 2);
|
|
117
|
+
if (previous instanceof three_1.Mesh && previous.geometry instanceof three_1.SphereGeometry) {
|
|
118
|
+
const p = previous.geometry.parameters;
|
|
119
|
+
if (p.radius === radius && p.widthSegments === widthSegments && p.heightSegments === heightSegments)
|
|
120
|
+
return previous;
|
|
121
|
+
}
|
|
122
|
+
return new three_1.Mesh(new three_1.SphereGeometry(radius, widthSegments, heightSegments), material);
|
|
123
|
+
}
|
|
124
|
+
const transform = kind === 'transform' && numbers(translate, 3) && numbers(scale, 3);
|
|
125
|
+
const merge = kind === 'merge' && Array.isArray(geometries);
|
|
126
|
+
if (!transform && !merge)
|
|
127
|
+
return;
|
|
128
|
+
const group = previous instanceof three_1.Group ? previous : new three_1.Group();
|
|
129
|
+
if (transform) {
|
|
130
|
+
group.position.fromArray(translate);
|
|
131
|
+
group.scale.fromArray(scale);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
group.position.set(0, 0, 0);
|
|
135
|
+
group.scale.setScalar(1);
|
|
136
|
+
}
|
|
137
|
+
ancestors.add(value);
|
|
138
|
+
updateChildren(group, transform ? [geometry] : geometries);
|
|
139
|
+
ancestors.delete(value);
|
|
140
|
+
return group;
|
|
141
|
+
}
|
|
142
|
+
updateChildren(root, [descriptor]);
|
|
143
|
+
}
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export declare const name = "@vim3d/threejs";
|
|
2
|
-
export {
|
|
2
|
+
export type { AnalyserLike, RecordedChannelTable, WebAudioConnectOptions, WebAudioMicrophoneOptions, WebAudioSourceOptions, } from './audio.js';
|
|
3
|
+
export { DEFAULT_AUDIO_BANDS, DEFAULT_AUDIO_FEATURE_PORTS, WebAudioChannelSource, } from './audio.js';
|
|
4
|
+
export type { FrameRenderer, FrameScheduler, LiveChannelProvider, PlayerOptions } from './player.js';
|
|
3
5
|
export { Vim3DPlayer } from './player.js';
|
|
4
|
-
export
|
|
6
|
+
export { Vim3DThreeScene } from './scene.js';
|
package/index.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Vim3DPlayer = exports.
|
|
3
|
+
exports.Vim3DThreeScene = exports.Vim3DPlayer = exports.WebAudioChannelSource = exports.DEFAULT_AUDIO_FEATURE_PORTS = exports.DEFAULT_AUDIO_BANDS = exports.name = void 0;
|
|
4
4
|
exports.name = '@vim3d/threejs';
|
|
5
|
-
var
|
|
6
|
-
Object.defineProperty(exports, "
|
|
5
|
+
var audio_js_1 = require("./audio.js");
|
|
6
|
+
Object.defineProperty(exports, "DEFAULT_AUDIO_BANDS", { enumerable: true, get: function () { return audio_js_1.DEFAULT_AUDIO_BANDS; } });
|
|
7
|
+
Object.defineProperty(exports, "DEFAULT_AUDIO_FEATURE_PORTS", { enumerable: true, get: function () { return audio_js_1.DEFAULT_AUDIO_FEATURE_PORTS; } });
|
|
8
|
+
Object.defineProperty(exports, "WebAudioChannelSource", { enumerable: true, get: function () { return audio_js_1.WebAudioChannelSource; } });
|
|
7
9
|
var player_js_1 = require("./player.js");
|
|
8
10
|
Object.defineProperty(exports, "Vim3DPlayer", { enumerable: true, get: function () { return player_js_1.Vim3DPlayer; } });
|
|
11
|
+
var scene_js_1 = require("./scene.js");
|
|
12
|
+
Object.defineProperty(exports, "Vim3DThreeScene", { enumerable: true, get: function () { return scene_js_1.Vim3DThreeScene; } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vim3d/threejs",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "A Three.js renderer for Vim3D scenes.",
|
|
5
5
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/pyramation/vim3d/tree/master/js/packages/threejs",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"url": "https://github.com/pyramation/vim3d/issues"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@vim3d/core": "^0.2.
|
|
24
|
+
"@vim3d/core": "^0.2.2"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
27
|
"three": ">=0.170.0 <1.0.0"
|
package/player.d.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
import { type CompiledScene, type Diagnostic, type FrameSnapshot, type Result } from '@vim3d/core';
|
|
2
|
-
import {
|
|
1
|
+
import { type CompiledScene, type Diagnostic, type FrameSnapshot, type LiveChannelInput, type Result } from '@vim3d/core';
|
|
2
|
+
import { type Camera, WebGLRenderer } from 'three';
|
|
3
3
|
import { Vim3DThreeScene } from './scene.js';
|
|
4
4
|
export type FrameRenderer = Pick<WebGLRenderer, 'render'> & Partial<Pick<WebGLRenderer, 'getSize'>>;
|
|
5
5
|
export interface FrameScheduler {
|
|
6
6
|
request(callback: (timestamp: number) => void): number;
|
|
7
7
|
cancel(handle: number): void;
|
|
8
8
|
}
|
|
9
|
+
export interface LiveChannelProvider {
|
|
10
|
+
readonly ports?: readonly string[];
|
|
11
|
+
sample(frame: number, time: number): LiveChannelInput;
|
|
12
|
+
}
|
|
9
13
|
export interface PlayerOptions {
|
|
10
14
|
readonly canvas?: HTMLCanvasElement | OffscreenCanvas;
|
|
11
15
|
readonly renderer?: FrameRenderer;
|
|
@@ -13,6 +17,7 @@ export interface PlayerOptions {
|
|
|
13
17
|
readonly scheduler?: FrameScheduler;
|
|
14
18
|
readonly fps?: number;
|
|
15
19
|
readonly loop?: boolean;
|
|
20
|
+
readonly liveChannels?: Readonly<Record<string, LiveChannelProvider>>;
|
|
16
21
|
readonly onFrame?: (snapshot: FrameSnapshot) => void;
|
|
17
22
|
readonly onDiagnostics?: (diagnostics: readonly Diagnostic[]) => void;
|
|
18
23
|
}
|
|
@@ -24,6 +29,7 @@ export declare class Vim3DPlayer {
|
|
|
24
29
|
private readonly ownedRenderer?;
|
|
25
30
|
private readonly scheduler;
|
|
26
31
|
private readonly rendererSize;
|
|
32
|
+
private readonly liveChannels;
|
|
27
33
|
private fps;
|
|
28
34
|
private loop;
|
|
29
35
|
private frame;
|
|
@@ -34,6 +40,7 @@ export declare class Vim3DPlayer {
|
|
|
34
40
|
private startTime?;
|
|
35
41
|
private startFrame;
|
|
36
42
|
constructor(scene: CompiledScene, options: PlayerOptions);
|
|
43
|
+
setLiveChannel(name: string, provider: LiveChannelProvider | undefined): void;
|
|
37
44
|
getFrame(): number;
|
|
38
45
|
setFps(fps: number): void;
|
|
39
46
|
setLoop(loop: boolean): void;
|
package/player.js
CHANGED
|
@@ -20,6 +20,7 @@ class Vim3DPlayer {
|
|
|
20
20
|
ownedRenderer;
|
|
21
21
|
scheduler;
|
|
22
22
|
rendererSize = new three_1.Vector2();
|
|
23
|
+
liveChannels = new Map();
|
|
23
24
|
fps;
|
|
24
25
|
loop;
|
|
25
26
|
frame = 0;
|
|
@@ -42,6 +43,16 @@ class Vim3DPlayer {
|
|
|
42
43
|
this.ownedRenderer = new three_1.WebGLRenderer({ canvas: options.canvas, antialias: true });
|
|
43
44
|
}
|
|
44
45
|
this.renderer = options.renderer ?? this.ownedRenderer;
|
|
46
|
+
for (const [name, provider] of Object.entries(options.liveChannels ?? {})) {
|
|
47
|
+
this.liveChannels.set(name, provider);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
setLiveChannel(name, provider) {
|
|
51
|
+
this.assertActive();
|
|
52
|
+
if (provider)
|
|
53
|
+
this.liveChannels.set(name, provider);
|
|
54
|
+
else
|
|
55
|
+
this.liveChannels.delete(name);
|
|
45
56
|
}
|
|
46
57
|
getFrame() {
|
|
47
58
|
return this.frame;
|
|
@@ -121,7 +132,18 @@ class Vim3DPlayer {
|
|
|
121
132
|
return ((frame % count) + count) % count;
|
|
122
133
|
}
|
|
123
134
|
draw(frame) {
|
|
124
|
-
|
|
135
|
+
let evaluateOptions;
|
|
136
|
+
const validFrame = Number.isSafeInteger(frame) && frame >= 0
|
|
137
|
+
&& (this.scene.frameCount === undefined || frame < this.scene.frameCount);
|
|
138
|
+
if (validFrame && this.liveChannels.size) {
|
|
139
|
+
const time = frame / this.scene.fps;
|
|
140
|
+
const sampled = {};
|
|
141
|
+
for (const [name, provider] of this.liveChannels) {
|
|
142
|
+
sampled[name] = provider.sample(frame, time);
|
|
143
|
+
}
|
|
144
|
+
evaluateOptions = { liveChannels: sampled };
|
|
145
|
+
}
|
|
146
|
+
const result = (0, core_1.evaluateFrame)(this.scene, frame, evaluateOptions);
|
|
125
147
|
if ('diagnostics' in result) {
|
|
126
148
|
this.options.onDiagnostics?.(result.diagnostics);
|
|
127
149
|
return result;
|
package/scene.js
CHANGED
|
@@ -8,6 +8,8 @@ function renderType(snapshot) {
|
|
|
8
8
|
return 'camera';
|
|
9
9
|
if (snapshot.light)
|
|
10
10
|
return snapshot.light.kind;
|
|
11
|
+
if (snapshot.proceduralGeometry !== undefined)
|
|
12
|
+
return 'procedural';
|
|
11
13
|
if (!snapshot.geometry)
|
|
12
14
|
return 'group';
|
|
13
15
|
return (0, geometry_js_1.isLine)(snapshot.geometry.primitive) ? 'line' : 'mesh';
|
|
@@ -62,9 +64,13 @@ function applyMaterial(material, sample) {
|
|
|
62
64
|
material.shininess = sample?.shininess ?? 0;
|
|
63
65
|
}
|
|
64
66
|
}
|
|
65
|
-
function disposeEntry(object) {
|
|
67
|
+
function disposeEntry({ object, proceduralMaterial }) {
|
|
66
68
|
object.removeFromParent();
|
|
67
|
-
if (
|
|
69
|
+
if (proceduralMaterial) {
|
|
70
|
+
(0, geometry_js_1.disposeProceduralGeometry)(object);
|
|
71
|
+
proceduralMaterial.dispose();
|
|
72
|
+
}
|
|
73
|
+
else if (object instanceof three_1.Mesh || object instanceof three_1.Line) {
|
|
68
74
|
object.geometry.dispose();
|
|
69
75
|
object.material.dispose();
|
|
70
76
|
}
|
|
@@ -100,14 +106,15 @@ class Vim3DThreeScene {
|
|
|
100
106
|
if (!entry || entry.type !== type) {
|
|
101
107
|
const object = createObject(type, geometry);
|
|
102
108
|
if (entry)
|
|
103
|
-
disposeEntry(entry
|
|
109
|
+
disposeEntry(entry);
|
|
104
110
|
object.name = path;
|
|
105
111
|
object.matrixAutoUpdate = false;
|
|
106
112
|
object.matrixWorldAutoUpdate = false;
|
|
107
113
|
this.scene.add(object);
|
|
108
114
|
if (object instanceof three_1.SpotLight || object instanceof three_1.DirectionalLight)
|
|
109
115
|
this.scene.add(object.target);
|
|
110
|
-
entry = { object, type, geometry
|
|
116
|
+
entry = { object, type, geometry,
|
|
117
|
+
...(type === 'procedural' && { proceduralMaterial: new three_1.MeshPhongMaterial({ side: three_1.DoubleSide }) }) };
|
|
111
118
|
this.entries.set(path, entry);
|
|
112
119
|
}
|
|
113
120
|
const { object } = entry;
|
|
@@ -120,7 +127,11 @@ class Vim3DThreeScene {
|
|
|
120
127
|
object.matrixWorld.copy(object.matrix);
|
|
121
128
|
object.matrixWorldNeedsUpdate = true;
|
|
122
129
|
object.visible = sample.visible;
|
|
123
|
-
if (
|
|
130
|
+
if (entry.proceduralMaterial && object instanceof three_1.Group) {
|
|
131
|
+
applyMaterial(entry.proceduralMaterial, sample.material);
|
|
132
|
+
(0, geometry_js_1.updateProceduralGeometry)(object, sample.proceduralGeometry, entry.proceduralMaterial);
|
|
133
|
+
}
|
|
134
|
+
else if (object instanceof three_1.Mesh || object instanceof three_1.Line) {
|
|
124
135
|
if (geometry !== entry.geometry) {
|
|
125
136
|
const next = (0, geometry_js_1.createGeometry)(geometry);
|
|
126
137
|
object.geometry.dispose();
|
|
@@ -145,16 +156,16 @@ class Vim3DThreeScene {
|
|
|
145
156
|
}
|
|
146
157
|
}
|
|
147
158
|
}
|
|
148
|
-
for (const [path,
|
|
159
|
+
for (const [path, entry] of this.entries) {
|
|
149
160
|
if (!seen.has(path)) {
|
|
150
|
-
disposeEntry(
|
|
161
|
+
disposeEntry(entry);
|
|
151
162
|
this.entries.delete(path);
|
|
152
163
|
}
|
|
153
164
|
}
|
|
154
165
|
}
|
|
155
166
|
dispose() {
|
|
156
|
-
for (const
|
|
157
|
-
disposeEntry(
|
|
167
|
+
for (const entry of this.entries.values())
|
|
168
|
+
disposeEntry(entry);
|
|
158
169
|
this.entries.clear();
|
|
159
170
|
}
|
|
160
171
|
}
|