angular-three 0.0.0-replace
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 +7 -0
- package/esm2022/angular-three.mjs +5 -0
- package/esm2022/index.mjs +20 -0
- package/esm2022/lib/canvas.mjs +187 -0
- package/esm2022/lib/directives/args.mjs +35 -0
- package/esm2022/lib/directives/common.mjs +42 -0
- package/esm2022/lib/directives/parent.mjs +35 -0
- package/esm2022/lib/dom/events.mjs +73 -0
- package/esm2022/lib/events.mjs +362 -0
- package/esm2022/lib/instance.mjs +70 -0
- package/esm2022/lib/loader.mjs +64 -0
- package/esm2022/lib/loop.mjs +153 -0
- package/esm2022/lib/portal.mjs +208 -0
- package/esm2022/lib/ref.mjs +46 -0
- package/esm2022/lib/renderer/catalogue.mjs +7 -0
- package/esm2022/lib/renderer/constants.mjs +21 -0
- package/esm2022/lib/renderer/index.mjs +432 -0
- package/esm2022/lib/renderer/store.mjs +423 -0
- package/esm2022/lib/renderer/utils.mjs +201 -0
- package/esm2022/lib/roots.mjs +261 -0
- package/esm2022/lib/routed-scene.mjs +33 -0
- package/esm2022/lib/store.mjs +181 -0
- package/esm2022/lib/three-types.mjs +2 -0
- package/esm2022/lib/types.mjs +2 -0
- package/esm2022/lib/utils/apply-props.mjs +123 -0
- package/esm2022/lib/utils/attach.mjs +32 -0
- package/esm2022/lib/utils/before-render.mjs +12 -0
- package/esm2022/lib/utils/create-api-token.mjs +13 -0
- package/esm2022/lib/utils/inputs.mjs +18 -0
- package/esm2022/lib/utils/is.mjs +52 -0
- package/esm2022/lib/utils/make.mjs +53 -0
- package/esm2022/lib/utils/signal-store.mjs +101 -0
- package/esm2022/lib/utils/update.mjs +34 -0
- package/fesm2022/angular-three.mjs +3167 -0
- package/fesm2022/angular-three.mjs.map +1 -0
- package/index.d.ts +22 -0
- package/lib/canvas.d.ts +1170 -0
- package/lib/directives/args.d.ts +11 -0
- package/lib/directives/common.d.ts +26 -0
- package/lib/directives/parent.d.ts +13 -0
- package/lib/dom/events.d.ts +5 -0
- package/lib/events.d.ts +83 -0
- package/lib/instance.d.ts +46 -0
- package/lib/loader.d.ts +32 -0
- package/lib/loop.d.ts +68 -0
- package/lib/portal.d.ts +58 -0
- package/lib/ref.d.ts +7 -0
- package/lib/renderer/catalogue.d.ts +13 -0
- package/lib/renderer/constants.d.ts +20 -0
- package/lib/renderer/index.d.ts +56 -0
- package/lib/renderer/store.d.ts +64 -0
- package/lib/renderer/utils.d.ts +31 -0
- package/lib/roots.d.ts +12 -0
- package/lib/routed-scene.d.ts +10 -0
- package/lib/store.d.ts +141 -0
- package/lib/three-types.d.ts +302 -0
- package/lib/types.d.ts +12 -0
- package/lib/utils/apply-props.d.ts +3 -0
- package/lib/utils/attach.d.ts +11 -0
- package/lib/utils/before-render.d.ts +6 -0
- package/lib/utils/create-api-token.d.ts +23 -0
- package/lib/utils/inputs.d.ts +5 -0
- package/lib/utils/is.d.ts +23 -0
- package/lib/utils/make.d.ts +19 -0
- package/lib/utils/signal-store.d.ts +30 -0
- package/lib/utils/update.d.ts +4 -0
- package/metadata.json +1 -0
- package/package.json +73 -0
- package/plugin/README.md +11 -0
- package/plugin/generators.json +17 -0
- package/plugin/src/generators/init/compat.d.ts +2 -0
- package/plugin/src/generators/init/compat.js +6 -0
- package/plugin/src/generators/init/compat.js.map +1 -0
- package/plugin/src/generators/init/files/experience/experience.component.html__tmpl__ +4 -0
- package/plugin/src/generators/init/files/experience/experience.component.ts__tmpl__ +18 -0
- package/plugin/src/generators/init/generator.d.ts +3 -0
- package/plugin/src/generators/init/generator.js +143 -0
- package/plugin/src/generators/init/generator.js.map +1 -0
- package/plugin/src/generators/init/schema.json +6 -0
- package/plugin/src/generators/utils.d.ts +2 -0
- package/plugin/src/generators/utils.js +34 -0
- package/plugin/src/generators/utils.js.map +1 -0
- package/plugin/src/generators/version.d.ts +14 -0
- package/plugin/src/generators/version.js +18 -0
- package/plugin/src/generators/version.js.map +1 -0
- package/plugin/src/index.d.ts +0 -0
- package/plugin/src/index.js +1 -0
- package/plugin/src/index.js.map +1 -0
- package/web-types.json +1 -0
|
@@ -0,0 +1,3167 @@
|
|
|
1
|
+
import * as i0 from '@angular/core';
|
|
2
|
+
import { untracked, computed, signal, ElementRef, inject, effect, InjectionToken, Optional, SkipSelf, ViewContainerRef, NgZone, TemplateRef, DestroyRef, Directive, input, getDebugNode, RendererFactory2, Injectable, Injector, makeEnvironmentProviders, EnvironmentInjector, booleanAttribute, output, viewChild, afterNextRender, createEnvironmentInjector, Component, ChangeDetectionStrategy, CUSTOM_ELEMENTS_SCHEMA, contentChild, forwardRef, reflectComponentType, isSignal } from '@angular/core';
|
|
3
|
+
import { outputFromObservable, takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
|
4
|
+
import { injectAutoEffect } from 'ngxtension/auto-effect';
|
|
5
|
+
import { provideResizeOptions, NgxResize } from 'ngxtension/resize';
|
|
6
|
+
import * as THREE from 'three';
|
|
7
|
+
import { DOCUMENT } from '@angular/common';
|
|
8
|
+
import { createInjectionToken } from 'ngxtension/create-injection-token';
|
|
9
|
+
import { Subject, ReplaySubject, filter } from 'rxjs';
|
|
10
|
+
import { assertInjector } from 'ngxtension/assert-injector';
|
|
11
|
+
import * as i1 from '@angular/router';
|
|
12
|
+
import { ActivationEnd, RouterOutlet } from '@angular/router';
|
|
13
|
+
|
|
14
|
+
const STORE_COMPUTED_KEY = '__ngt_signal_store_computed__';
|
|
15
|
+
function reducer(state) {
|
|
16
|
+
return (previous) => {
|
|
17
|
+
const partial = typeof state === 'function' ? state(previous) : state;
|
|
18
|
+
Object.keys(partial).forEach((key) => {
|
|
19
|
+
const typedKey = key;
|
|
20
|
+
if (partial[typedKey] === undefined && previous[typedKey] != null) {
|
|
21
|
+
partial[typedKey] = previous[typedKey];
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return partial;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function updater(_source) {
|
|
28
|
+
return (state) => {
|
|
29
|
+
const updater = reducer(state);
|
|
30
|
+
untracked(() => {
|
|
31
|
+
_source.update((previous) => ({ ...previous, ...updater(previous) }));
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function patcher(_source) {
|
|
36
|
+
return (state) => {
|
|
37
|
+
const updater = reducer(state);
|
|
38
|
+
untracked(() => {
|
|
39
|
+
_source.update((previous) => ({ ...updater(previous), ...previous }));
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function getter(_source) {
|
|
44
|
+
return (...keys) => {
|
|
45
|
+
const root = untracked(_source);
|
|
46
|
+
if (keys.length === 0)
|
|
47
|
+
return root;
|
|
48
|
+
return keys.reduce((value, key) => value[key], root);
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function selector(_state, computedCache) {
|
|
52
|
+
return (...keysAndOptions) => {
|
|
53
|
+
if (keysAndOptions.length === 0)
|
|
54
|
+
return _state;
|
|
55
|
+
if (keysAndOptions.length === 1 && typeof keysAndOptions[0] === 'object') {
|
|
56
|
+
const cachedKey = STORE_COMPUTED_KEY.concat(JSON.stringify(keysAndOptions[0]));
|
|
57
|
+
if (!computedCache.has(cachedKey)) {
|
|
58
|
+
computedCache.set(cachedKey, computed(_state, keysAndOptions));
|
|
59
|
+
}
|
|
60
|
+
return computedCache.get(cachedKey);
|
|
61
|
+
}
|
|
62
|
+
const [keys, options] = parseStoreOptions(keysAndOptions);
|
|
63
|
+
const joinedKeys = keys.join('-');
|
|
64
|
+
const cachedKeys = joinedKeys.concat(JSON.stringify(options));
|
|
65
|
+
if (!computedCache.has(cachedKeys)) {
|
|
66
|
+
computedCache.set(cachedKeys, computed(() => keys.reduce((value, key) => value[key], _state()), options));
|
|
67
|
+
}
|
|
68
|
+
return computedCache.get(cachedKeys);
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function parseStoreOptions(keysAndOptions) {
|
|
72
|
+
if (typeof keysAndOptions.at(-1) === 'object') {
|
|
73
|
+
return [keysAndOptions.slice(0, -1), keysAndOptions.at(-1)];
|
|
74
|
+
}
|
|
75
|
+
return [keysAndOptions, { equal: Object.is }];
|
|
76
|
+
}
|
|
77
|
+
function signalStore(initialState = {}, options) {
|
|
78
|
+
let source;
|
|
79
|
+
let update;
|
|
80
|
+
let get;
|
|
81
|
+
let patch;
|
|
82
|
+
let select;
|
|
83
|
+
let state;
|
|
84
|
+
const computedCache = new Map();
|
|
85
|
+
if (!options) {
|
|
86
|
+
options = { equal: Object.is };
|
|
87
|
+
}
|
|
88
|
+
if (typeof initialState === 'function') {
|
|
89
|
+
source = signal({}, options);
|
|
90
|
+
state = source.asReadonly();
|
|
91
|
+
get = getter(source);
|
|
92
|
+
update = updater(source);
|
|
93
|
+
patch = patcher(source);
|
|
94
|
+
select = selector(state, computedCache);
|
|
95
|
+
source.set(initialState({ update, get, patch, select }));
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
source = signal(initialState, options);
|
|
99
|
+
state = source.asReadonly();
|
|
100
|
+
get = getter(source);
|
|
101
|
+
update = updater(source);
|
|
102
|
+
patch = patcher(source);
|
|
103
|
+
select = selector(state, computedCache);
|
|
104
|
+
}
|
|
105
|
+
const store = { select, get, update, patch, state };
|
|
106
|
+
Object.defineProperty(store, 'snapshot', {
|
|
107
|
+
get: untracked.bind({}, state),
|
|
108
|
+
configurable: false,
|
|
109
|
+
enumerable: false,
|
|
110
|
+
});
|
|
111
|
+
return store;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const is = {
|
|
115
|
+
obj: (a) => a === Object(a) && !Array.isArray(a) && typeof a !== 'function',
|
|
116
|
+
material: (a) => !!a && a.isMaterial,
|
|
117
|
+
geometry: (a) => !!a && a.isBufferGeometry,
|
|
118
|
+
orthographicCamera: (a) => !!a && a.isOrthographicCamera,
|
|
119
|
+
perspectiveCamera: (a) => !!a && a.isPerspectiveCamera,
|
|
120
|
+
camera: (a) => !!a && a.isCamera,
|
|
121
|
+
renderer: (a) => !!a && typeof a === 'object' && 'render' in a && typeof a['render'] === 'function',
|
|
122
|
+
scene: (a) => !!a && a.isScene,
|
|
123
|
+
ref: (a) => a instanceof ElementRef,
|
|
124
|
+
instance: (a) => !!a && !!a['__ngt__'],
|
|
125
|
+
object3D: (a) => !!a && a.isObject3D,
|
|
126
|
+
// instance: (a: unknown): a is NgtInstanceNode => !!a && !!(a as NgtAnyRecord)['__ngt__'],
|
|
127
|
+
// ref: (a: unknown): a is ElementRef => a instanceof ElementRef,
|
|
128
|
+
colorSpaceExist: (object) => 'colorSpace' in object || 'outputColorSpace' in object,
|
|
129
|
+
equ(a, b, { arrays = 'shallow', objects = 'reference', strict = true } = {}) {
|
|
130
|
+
// Wrong type or one of the two undefined, doesn't match
|
|
131
|
+
if (typeof a !== typeof b || !!a !== !!b)
|
|
132
|
+
return false;
|
|
133
|
+
// Atomic, just compare a against b
|
|
134
|
+
if (typeof a === 'string' || typeof a === 'number')
|
|
135
|
+
return a === b;
|
|
136
|
+
const isObj = is.obj(a);
|
|
137
|
+
if (isObj && objects === 'reference')
|
|
138
|
+
return a === b;
|
|
139
|
+
const isArr = Array.isArray(a);
|
|
140
|
+
if (isArr && arrays === 'reference')
|
|
141
|
+
return a === b;
|
|
142
|
+
// Array or Object, shallow compare first to see if it's a match
|
|
143
|
+
if ((isArr || isObj) && a === b)
|
|
144
|
+
return true;
|
|
145
|
+
// Last resort, go through keys
|
|
146
|
+
let i;
|
|
147
|
+
for (i in a)
|
|
148
|
+
if (!(i in b))
|
|
149
|
+
return false;
|
|
150
|
+
for (i in strict ? b : a)
|
|
151
|
+
if (a[i] !== b[i])
|
|
152
|
+
return false;
|
|
153
|
+
if (i === void 0) {
|
|
154
|
+
if (isArr && a.length === 0 && b.length === 0)
|
|
155
|
+
return true;
|
|
156
|
+
if (isObj && Object.keys(a).length === 0 && Object.keys(b).length === 0)
|
|
157
|
+
return true;
|
|
158
|
+
if (a !== b)
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
return true;
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
function checkNeedsUpdate(value) {
|
|
166
|
+
if (value !== null && is.obj(value) && 'needsUpdate' in value) {
|
|
167
|
+
value['needsUpdate'] = true;
|
|
168
|
+
if ('uniformsNeedUpdate' in value)
|
|
169
|
+
value['uniformsNeedUpdate'] = true;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function checkUpdate(value) {
|
|
173
|
+
if (is.object3D(value))
|
|
174
|
+
value.updateMatrix();
|
|
175
|
+
if (is.camera(value)) {
|
|
176
|
+
if (is.perspectiveCamera(value) || is.orthographicCamera(value))
|
|
177
|
+
value.updateProjectionMatrix();
|
|
178
|
+
value.updateMatrixWorld();
|
|
179
|
+
}
|
|
180
|
+
checkNeedsUpdate(value);
|
|
181
|
+
}
|
|
182
|
+
function updateCamera(camera, size) {
|
|
183
|
+
if (!camera.manual) {
|
|
184
|
+
if (is.orthographicCamera(camera)) {
|
|
185
|
+
camera.left = size.width / -2;
|
|
186
|
+
camera.right = size.width / 2;
|
|
187
|
+
camera.top = size.height / 2;
|
|
188
|
+
camera.bottom = size.height / -2;
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
camera.aspect = size.width / size.height;
|
|
192
|
+
}
|
|
193
|
+
camera.updateProjectionMatrix();
|
|
194
|
+
camera.updateMatrixWorld();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function getLocalState(obj) {
|
|
199
|
+
if (!obj)
|
|
200
|
+
return undefined;
|
|
201
|
+
return obj['__ngt__'];
|
|
202
|
+
}
|
|
203
|
+
function invalidateInstance(instance) {
|
|
204
|
+
const state = getLocalState(instance)?.store.snapshot;
|
|
205
|
+
if (state && state.internal.frames === 0)
|
|
206
|
+
state.invalidate();
|
|
207
|
+
checkUpdate(instance);
|
|
208
|
+
}
|
|
209
|
+
function prepare(object, localState) {
|
|
210
|
+
const instance = object;
|
|
211
|
+
if (localState?.primitive || !instance.__ngt__) {
|
|
212
|
+
const { instanceStore = signalStore({
|
|
213
|
+
nativeProps: {},
|
|
214
|
+
parent: null,
|
|
215
|
+
objects: [],
|
|
216
|
+
nonObjects: [],
|
|
217
|
+
}), ...rest } = localState || {};
|
|
218
|
+
instance.__ngt__ = {
|
|
219
|
+
previousAttach: null,
|
|
220
|
+
store: null,
|
|
221
|
+
memoized: {},
|
|
222
|
+
eventCount: 0,
|
|
223
|
+
handlers: {},
|
|
224
|
+
instanceStore,
|
|
225
|
+
parent: instanceStore.select('parent'),
|
|
226
|
+
objects: instanceStore.select('objects'),
|
|
227
|
+
nonObjects: instanceStore.select('nonObjects'),
|
|
228
|
+
nativeProps: instanceStore.select('nativeProps'),
|
|
229
|
+
add(object, type) {
|
|
230
|
+
const current = instance.__ngt__.instanceStore.get(type);
|
|
231
|
+
const foundIndex = current.indexOf((node) => object === node);
|
|
232
|
+
if (foundIndex > -1) {
|
|
233
|
+
current.splice(foundIndex, 1, object);
|
|
234
|
+
instance.__ngt__.instanceStore.update({ [type]: current });
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
instance.__ngt__.instanceStore.update((prev) => ({ [type]: [...prev[type], object] }));
|
|
238
|
+
}
|
|
239
|
+
notifyAncestors(instance.__ngt__.instanceStore.get('parent'));
|
|
240
|
+
},
|
|
241
|
+
remove(object, type) {
|
|
242
|
+
instance.__ngt__.instanceStore.update((prev) => ({ [type]: prev[type].filter((node) => node !== object) }));
|
|
243
|
+
notifyAncestors(instance.__ngt__.instanceStore.get('parent'));
|
|
244
|
+
},
|
|
245
|
+
setNativeProps(key, value) {
|
|
246
|
+
instance.__ngt__.instanceStore.update((prev) => ({ nativeProps: { ...prev.nativeProps, [key]: value } }));
|
|
247
|
+
},
|
|
248
|
+
setParent(parent) {
|
|
249
|
+
instance.__ngt__.instanceStore.update({ parent });
|
|
250
|
+
},
|
|
251
|
+
...rest,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
return instance;
|
|
255
|
+
}
|
|
256
|
+
function notifyAncestors(instance) {
|
|
257
|
+
if (!instance)
|
|
258
|
+
return;
|
|
259
|
+
const localState = getLocalState(instance);
|
|
260
|
+
if (!localState)
|
|
261
|
+
return;
|
|
262
|
+
localState.instanceStore.update((prev) => ({ objects: prev.objects, nonObjects: prev.nonObjects }));
|
|
263
|
+
notifyAncestors(localState.instanceStore.get('parent'));
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const idCache = {};
|
|
267
|
+
function makeId(event) {
|
|
268
|
+
if (event) {
|
|
269
|
+
return (event.eventObject || event.object).uuid + '/' + event.index + event.instanceId;
|
|
270
|
+
}
|
|
271
|
+
const newId = THREE.MathUtils.generateUUID();
|
|
272
|
+
// ensure not already used
|
|
273
|
+
if (!idCache[newId]) {
|
|
274
|
+
idCache[newId] = true;
|
|
275
|
+
return newId;
|
|
276
|
+
}
|
|
277
|
+
return makeId();
|
|
278
|
+
}
|
|
279
|
+
function makeDpr(dpr, window) {
|
|
280
|
+
// Err on the side of progress by assuming 2x dpr if we can't detect it
|
|
281
|
+
// This will happen in workers where window is defined but dpr isn't.
|
|
282
|
+
const target = typeof window !== 'undefined' ? window.devicePixelRatio ?? 2 : 1;
|
|
283
|
+
return Array.isArray(dpr) ? Math.min(Math.max(dpr[0], target), dpr[1]) : dpr;
|
|
284
|
+
}
|
|
285
|
+
function makeRendererInstance(glOptions, canvas) {
|
|
286
|
+
const customRenderer = (typeof glOptions === 'function' ? glOptions(canvas) : glOptions);
|
|
287
|
+
if (is.renderer(customRenderer))
|
|
288
|
+
return customRenderer;
|
|
289
|
+
return new THREE.WebGLRenderer({
|
|
290
|
+
powerPreference: 'high-performance',
|
|
291
|
+
canvas: canvas,
|
|
292
|
+
antialias: true,
|
|
293
|
+
alpha: true,
|
|
294
|
+
...glOptions,
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
function makeCameraInstance(isOrthographic, size) {
|
|
298
|
+
if (isOrthographic)
|
|
299
|
+
return new THREE.OrthographicCamera(0, 0, 0, 0, 0.1, 1000);
|
|
300
|
+
return new THREE.PerspectiveCamera(75, size.width / size.height, 0.1, 1000);
|
|
301
|
+
}
|
|
302
|
+
function makeObjectGraph(object) {
|
|
303
|
+
const data = { nodes: {}, materials: {} };
|
|
304
|
+
if (object) {
|
|
305
|
+
object.traverse((child) => {
|
|
306
|
+
if (child.name)
|
|
307
|
+
data.nodes[child.name] = child;
|
|
308
|
+
if ('material' in child && !data.materials[child.material.name]) {
|
|
309
|
+
data.materials[child.material.name] = child
|
|
310
|
+
.material;
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
return data;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Release pointer captures.
|
|
319
|
+
* This is called by releasePointerCapture in the API, and when an object is removed.
|
|
320
|
+
*/
|
|
321
|
+
function releaseInternalPointerCapture(capturedMap, obj, captures, pointerId) {
|
|
322
|
+
const captureData = captures.get(obj);
|
|
323
|
+
if (captureData) {
|
|
324
|
+
captures.delete(obj);
|
|
325
|
+
// If this was the last capturing object for this pointer
|
|
326
|
+
if (captures.size === 0) {
|
|
327
|
+
capturedMap.delete(pointerId);
|
|
328
|
+
captureData.target.releasePointerCapture(pointerId);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
function removeInteractivity(store, object) {
|
|
333
|
+
const { internal } = store.snapshot;
|
|
334
|
+
// Removes every trace of an object from the data store
|
|
335
|
+
internal.interaction = internal.interaction.filter((o) => o !== object);
|
|
336
|
+
internal.initialHits = internal.initialHits.filter((o) => o !== object);
|
|
337
|
+
internal.hovered.forEach((value, key) => {
|
|
338
|
+
if (value.eventObject === object || value.object === object) {
|
|
339
|
+
// Clear out intersects, they are outdated by now
|
|
340
|
+
internal.hovered.delete(key);
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
internal.capturedMap.forEach((captures, pointerId) => {
|
|
344
|
+
releaseInternalPointerCapture(internal.capturedMap, object, captures, pointerId);
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
function createEvents(store) {
|
|
348
|
+
/** Calculates delta */
|
|
349
|
+
function calculateDistance(event) {
|
|
350
|
+
const internal = store.get('internal');
|
|
351
|
+
const dx = event.offsetX - internal.initialClick[0];
|
|
352
|
+
const dy = event.offsetY - internal.initialClick[1];
|
|
353
|
+
return Math.round(Math.sqrt(dx * dx + dy * dy));
|
|
354
|
+
}
|
|
355
|
+
/** Returns true if an instance has a valid pointer-event registered, this excludes scroll, clicks etc */
|
|
356
|
+
function filterPointerEvents(objects) {
|
|
357
|
+
return objects.filter((obj) => ['move', 'over', 'enter', 'out', 'leave'].some((name) => {
|
|
358
|
+
const eventName = `pointer${name}`;
|
|
359
|
+
return getLocalState(obj)?.handlers?.[eventName];
|
|
360
|
+
}));
|
|
361
|
+
}
|
|
362
|
+
function intersect(event, filter) {
|
|
363
|
+
const state = store.get();
|
|
364
|
+
const duplicates = new Set();
|
|
365
|
+
const intersections = [];
|
|
366
|
+
// Allow callers to eliminate event objects
|
|
367
|
+
const eventsObjects = filter ? filter(state.internal.interaction) : state.internal.interaction;
|
|
368
|
+
// Reset all raycaster cameras to undefined
|
|
369
|
+
for (let i = 0; i < eventsObjects.length; i++) {
|
|
370
|
+
const state = getLocalState(eventsObjects[i])?.store.snapshot;
|
|
371
|
+
if (state) {
|
|
372
|
+
state.raycaster.camera = undefined;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (!state.previousRoot) {
|
|
376
|
+
// Make sure root-level pointer and ray are set up
|
|
377
|
+
state.events.compute?.(event, store, null);
|
|
378
|
+
}
|
|
379
|
+
function handleRaycast(obj) {
|
|
380
|
+
const objLocalState = getLocalState(obj);
|
|
381
|
+
const objStore = objLocalState?.store;
|
|
382
|
+
const objState = objStore?.snapshot;
|
|
383
|
+
// Skip event handling when noEvents is set, or when the raycasters camera is null
|
|
384
|
+
if (!objState || !objState.events.enabled || objState.raycaster.camera === null)
|
|
385
|
+
return [];
|
|
386
|
+
// When the camera is undefined we have to call the event layers update function
|
|
387
|
+
if (objState.raycaster.camera === undefined) {
|
|
388
|
+
objState.events.compute?.(event, objStore, objState.previousRoot);
|
|
389
|
+
// If the camera is still undefined we have to skip this layer entirely
|
|
390
|
+
if (objState.raycaster.camera === undefined)
|
|
391
|
+
objState.raycaster.camera = null;
|
|
392
|
+
}
|
|
393
|
+
// Intersect object by object
|
|
394
|
+
return objState.raycaster.camera ? objState.raycaster.intersectObject(obj, true) : [];
|
|
395
|
+
}
|
|
396
|
+
// Collect events
|
|
397
|
+
let hits = eventsObjects
|
|
398
|
+
// Intersect objects
|
|
399
|
+
.flatMap(handleRaycast)
|
|
400
|
+
// Sort by event priority and distance
|
|
401
|
+
.sort((a, b) => {
|
|
402
|
+
const aState = getLocalState(a.object)?.store?.snapshot;
|
|
403
|
+
const bState = getLocalState(b.object)?.store?.snapshot;
|
|
404
|
+
if (!aState || !bState)
|
|
405
|
+
return a.distance - b.distance;
|
|
406
|
+
return bState.events.priority - aState.events.priority || a.distance - b.distance;
|
|
407
|
+
})
|
|
408
|
+
// Filter out duplicates
|
|
409
|
+
.filter((item) => {
|
|
410
|
+
const id = makeId(item);
|
|
411
|
+
if (duplicates.has(id))
|
|
412
|
+
return false;
|
|
413
|
+
duplicates.add(id);
|
|
414
|
+
return true;
|
|
415
|
+
});
|
|
416
|
+
// https://github.com/mrdoob/three.js/issues/16031
|
|
417
|
+
// Allow custom userland intersect sort order, this likely only makes sense on the root filter
|
|
418
|
+
if (state.events.filter)
|
|
419
|
+
hits = state.events.filter(hits, store);
|
|
420
|
+
// Bubble up the events, find the event source (eventObject)
|
|
421
|
+
for (const hit of hits) {
|
|
422
|
+
let eventObject = hit.object;
|
|
423
|
+
// bubble event up
|
|
424
|
+
while (eventObject) {
|
|
425
|
+
if (getLocalState(eventObject)?.eventCount) {
|
|
426
|
+
intersections.push({ ...hit, eventObject });
|
|
427
|
+
}
|
|
428
|
+
eventObject = eventObject.parent;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
// If the interaction is captured, make all capturing targets part of the intersect.
|
|
432
|
+
if ('pointerId' in event && state.internal.capturedMap.has(event.pointerId)) {
|
|
433
|
+
for (const captureData of state.internal.capturedMap.get(event.pointerId).values()) {
|
|
434
|
+
if (!duplicates.has(makeId(captureData.intersection)))
|
|
435
|
+
intersections.push(captureData.intersection);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
return intersections;
|
|
439
|
+
}
|
|
440
|
+
/** Handles intersections by forwarding them to handlers */
|
|
441
|
+
function handleIntersects(intersections, event, delta, callback) {
|
|
442
|
+
const rootState = store.snapshot;
|
|
443
|
+
// If anything has been found, forward it to the event listeners
|
|
444
|
+
if (intersections.length) {
|
|
445
|
+
const localState = { stopped: false };
|
|
446
|
+
for (const hit of intersections) {
|
|
447
|
+
const { raycaster, pointer, camera, internal } = getLocalState(hit.object)?.store?.snapshot || rootState;
|
|
448
|
+
const unprojectedPoint = new THREE.Vector3(pointer.x, pointer.y, 0).unproject(camera);
|
|
449
|
+
const hasPointerCapture = (id) => internal.capturedMap.get(id)?.has(hit.eventObject) ?? false;
|
|
450
|
+
const setPointerCapture = (id) => {
|
|
451
|
+
const captureData = { intersection: hit, target: event.target };
|
|
452
|
+
if (internal.capturedMap.has(id)) {
|
|
453
|
+
// if the pointerId was previously captured, we add the hit to the
|
|
454
|
+
// event capturedMap.
|
|
455
|
+
internal.capturedMap.get(id).set(hit.eventObject, captureData);
|
|
456
|
+
}
|
|
457
|
+
else {
|
|
458
|
+
// if the pointerId was not previously captured, we create a map
|
|
459
|
+
// containing the hitObject, and the hit. hitObject is used for
|
|
460
|
+
// faster access.
|
|
461
|
+
internal.capturedMap.set(id, new Map([[hit.eventObject, captureData]]));
|
|
462
|
+
}
|
|
463
|
+
// Call the original event now
|
|
464
|
+
event.target.setPointerCapture(id);
|
|
465
|
+
};
|
|
466
|
+
const releasePointerCapture = (id) => {
|
|
467
|
+
const captures = internal.capturedMap.get(id);
|
|
468
|
+
if (captures) {
|
|
469
|
+
releaseInternalPointerCapture(internal.capturedMap, hit.eventObject, captures, id);
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
// Add native event props
|
|
473
|
+
const extractEventProps = {};
|
|
474
|
+
// This iterates over the event's properties including the inherited ones. Native PointerEvents have most of their props as getters which are inherited, but polyfilled PointerEvents have them all as their own properties (i.e. not inherited). We can't use Object.keys() or Object.entries() as they only return "own" properties; nor Object.getPrototypeOf(event) as that *doesn't* return "own" properties, only inherited ones.
|
|
475
|
+
for (const prop in event) {
|
|
476
|
+
const property = event[prop];
|
|
477
|
+
// Only copy over atomics, leave functions alone as these should be
|
|
478
|
+
// called as event.nativeEvent.fn()
|
|
479
|
+
if (typeof property !== 'function')
|
|
480
|
+
extractEventProps[prop] = property;
|
|
481
|
+
}
|
|
482
|
+
const raycastEvent = {
|
|
483
|
+
...hit,
|
|
484
|
+
...extractEventProps,
|
|
485
|
+
pointer,
|
|
486
|
+
intersections,
|
|
487
|
+
stopped: localState.stopped,
|
|
488
|
+
delta,
|
|
489
|
+
unprojectedPoint,
|
|
490
|
+
ray: raycaster.ray,
|
|
491
|
+
camera,
|
|
492
|
+
// Hijack stopPropagation, which just sets a flag
|
|
493
|
+
stopPropagation() {
|
|
494
|
+
// https://github.com/pmndrs/react-three-fiber/issues/596
|
|
495
|
+
// Events are not allowed to stop propagation if the pointer has been captured
|
|
496
|
+
const capturesForPointer = 'pointerId' in event && internal.capturedMap.get(event.pointerId);
|
|
497
|
+
// We only authorize stopPropagation...
|
|
498
|
+
if (
|
|
499
|
+
// ...if this pointer hasn't been captured
|
|
500
|
+
!capturesForPointer ||
|
|
501
|
+
// ... or if the hit object is capturing the pointer
|
|
502
|
+
capturesForPointer.has(hit.eventObject)) {
|
|
503
|
+
raycastEvent.stopped = localState.stopped = true;
|
|
504
|
+
// Propagation is stopped, remove all other hover records
|
|
505
|
+
// An event handler is only allowed to flush other handlers if it is hovered itself
|
|
506
|
+
if (internal.hovered.size &&
|
|
507
|
+
Array.from(internal.hovered.values()).find((i) => i.eventObject === hit.eventObject)) {
|
|
508
|
+
// Objects cannot flush out higher up objects that have already caught the event
|
|
509
|
+
const higher = intersections.slice(0, intersections.indexOf(hit));
|
|
510
|
+
cancelPointer([...higher, hit]);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
},
|
|
514
|
+
// there should be a distinction between target and currentTarget
|
|
515
|
+
target: { hasPointerCapture, setPointerCapture, releasePointerCapture },
|
|
516
|
+
currentTarget: { hasPointerCapture, setPointerCapture, releasePointerCapture },
|
|
517
|
+
nativeEvent: event,
|
|
518
|
+
};
|
|
519
|
+
// Call subscribers
|
|
520
|
+
callback(raycastEvent);
|
|
521
|
+
// Event bubbling may be interrupted by stopPropagation
|
|
522
|
+
if (localState.stopped)
|
|
523
|
+
break;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return intersections;
|
|
527
|
+
}
|
|
528
|
+
function cancelPointer(intersections) {
|
|
529
|
+
const internal = store.get('internal');
|
|
530
|
+
for (const hoveredObj of internal.hovered.values()) {
|
|
531
|
+
// When no objects were hit or the the hovered object wasn't found underneath the cursor
|
|
532
|
+
// we call onPointerOut and delete the object from the hovered-elements map
|
|
533
|
+
if (!intersections.length ||
|
|
534
|
+
!intersections.find((hit) => hit.object === hoveredObj.object &&
|
|
535
|
+
hit.index === hoveredObj.index &&
|
|
536
|
+
hit.instanceId === hoveredObj.instanceId)) {
|
|
537
|
+
const eventObject = hoveredObj.eventObject;
|
|
538
|
+
const instance = getLocalState(eventObject);
|
|
539
|
+
const handlers = instance?.handlers;
|
|
540
|
+
internal.hovered.delete(makeId(hoveredObj));
|
|
541
|
+
if (instance?.eventCount) {
|
|
542
|
+
// Clear out intersects, they are outdated by now
|
|
543
|
+
const data = { ...hoveredObj, intersections };
|
|
544
|
+
handlers?.pointerout?.(data);
|
|
545
|
+
handlers?.pointerleave?.(data);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
function pointerMissed(event, objects) {
|
|
551
|
+
for (let i = 0; i < objects.length; i++) {
|
|
552
|
+
const instance = getLocalState(objects[i]);
|
|
553
|
+
instance?.handlers.pointermissed?.(event);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
function handlePointer(name) {
|
|
557
|
+
// Deal with cancelation
|
|
558
|
+
switch (name) {
|
|
559
|
+
case 'pointerleave':
|
|
560
|
+
case 'pointercancel':
|
|
561
|
+
return () => cancelPointer([]);
|
|
562
|
+
case 'lostpointercapture':
|
|
563
|
+
return (event) => {
|
|
564
|
+
const { internal } = store.snapshot;
|
|
565
|
+
if ('pointerId' in event && internal.capturedMap.has(event.pointerId)) {
|
|
566
|
+
// If the object event interface had lostpointercapture, we'd call it here on every
|
|
567
|
+
// object that's getting removed. We call it on the next frame because lostpointercapture
|
|
568
|
+
// fires before pointerup. Otherwise pointerUp would never be called if the event didn't
|
|
569
|
+
// happen in the object it originated from, leaving components in a in-between state.
|
|
570
|
+
requestAnimationFrame(() => {
|
|
571
|
+
// Only release if pointer-up didn't do it already
|
|
572
|
+
if (internal.capturedMap.has(event.pointerId)) {
|
|
573
|
+
internal.capturedMap.delete(event.pointerId);
|
|
574
|
+
cancelPointer([]);
|
|
575
|
+
}
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
// Any other pointer goes here ...
|
|
581
|
+
return function handleEvent(event) {
|
|
582
|
+
const pointerMissed$ = store['pointerMissed$'];
|
|
583
|
+
const internal = store.get('internal');
|
|
584
|
+
// prepareRay(event)
|
|
585
|
+
internal.lastEvent.nativeElement = event;
|
|
586
|
+
// Get fresh intersects
|
|
587
|
+
const isPointerMove = name === 'pointermove';
|
|
588
|
+
const isClickEvent = name === 'click' || name === 'contextmenu' || name === 'dblclick';
|
|
589
|
+
const filter = isPointerMove ? filterPointerEvents : undefined;
|
|
590
|
+
const hits = intersect(event, filter);
|
|
591
|
+
const delta = isClickEvent ? calculateDistance(event) : 0;
|
|
592
|
+
// Save initial coordinates on pointer-down
|
|
593
|
+
if (name === 'pointerdown') {
|
|
594
|
+
internal.initialClick = [event.offsetX, event.offsetY];
|
|
595
|
+
internal.initialHits = hits.map((hit) => hit.eventObject);
|
|
596
|
+
}
|
|
597
|
+
// If a click yields no results, pass it back to the user as a miss
|
|
598
|
+
// Missed events have to come first in order to establish user-land side-effect clean up
|
|
599
|
+
if (isClickEvent && !hits.length) {
|
|
600
|
+
if (delta <= 2) {
|
|
601
|
+
pointerMissed(event, internal.interaction);
|
|
602
|
+
pointerMissed$.next(event);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
// Take care of unhover
|
|
606
|
+
if (isPointerMove)
|
|
607
|
+
cancelPointer(hits);
|
|
608
|
+
function onIntersect(data) {
|
|
609
|
+
const eventObject = data.eventObject;
|
|
610
|
+
const instance = getLocalState(eventObject);
|
|
611
|
+
const handlers = instance?.handlers;
|
|
612
|
+
// Check presence of handlers
|
|
613
|
+
if (!instance?.eventCount)
|
|
614
|
+
return;
|
|
615
|
+
/*
|
|
616
|
+
MAYBE TODO, DELETE IF NOT:
|
|
617
|
+
Check if the object is captured, captured events should not have intersects running in parallel
|
|
618
|
+
But wouldn't it be better to just replace capturedMap with a single entry?
|
|
619
|
+
Also, are we OK with straight up making picking up multiple objects impossible?
|
|
620
|
+
|
|
621
|
+
const pointerId = (data as ThreeEvent<PointerEvent>).pointerId
|
|
622
|
+
if (pointerId !== undefined) {
|
|
623
|
+
const capturedMeshSet = internal.capturedMap.get(pointerId)
|
|
624
|
+
if (capturedMeshSet) {
|
|
625
|
+
const captured = capturedMeshSet.get(eventObject)
|
|
626
|
+
if (captured && captured.localState.stopped) return
|
|
627
|
+
}
|
|
628
|
+
}*/
|
|
629
|
+
if (isPointerMove) {
|
|
630
|
+
// Move event ...
|
|
631
|
+
if (handlers?.pointerover || handlers?.pointerenter || handlers?.pointerout || handlers?.pointerleave) {
|
|
632
|
+
// When enter or out is present take care of hover-state
|
|
633
|
+
const id = makeId(data);
|
|
634
|
+
const hoveredItem = internal.hovered.get(id);
|
|
635
|
+
if (!hoveredItem) {
|
|
636
|
+
// If the object wasn't previously hovered, book it and call its handler
|
|
637
|
+
internal.hovered.set(id, data);
|
|
638
|
+
handlers.pointerover?.(data);
|
|
639
|
+
handlers.pointerenter?.(data);
|
|
640
|
+
}
|
|
641
|
+
else if (hoveredItem.stopped) {
|
|
642
|
+
// If the object was previously hovered and stopped, we shouldn't allow other items to proceed
|
|
643
|
+
data.stopPropagation();
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
// Call mouse move
|
|
647
|
+
handlers?.pointermove?.(data);
|
|
648
|
+
}
|
|
649
|
+
else {
|
|
650
|
+
// All other events ...
|
|
651
|
+
const handler = handlers?.[name];
|
|
652
|
+
if (handler) {
|
|
653
|
+
// Forward all events back to their respective handlers with the exception of click events,
|
|
654
|
+
// which must use the initial target
|
|
655
|
+
if (!isClickEvent || internal.initialHits.includes(eventObject)) {
|
|
656
|
+
// Missed events have to come first
|
|
657
|
+
pointerMissed(event, internal.interaction.filter((object) => !internal.initialHits.includes(object)));
|
|
658
|
+
// Now call the handler
|
|
659
|
+
handler(data);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
else {
|
|
663
|
+
// Trigger onPointerMissed on all elements that have pointer over/out handlers, but not click and weren't hit
|
|
664
|
+
if (isClickEvent && internal.initialHits.includes(eventObject)) {
|
|
665
|
+
pointerMissed(event, internal.interaction.filter((object) => !internal.initialHits.includes(object)));
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
handleIntersects(hits, event, delta, onIntersect);
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
return { handlePointer };
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
const DOM_EVENTS = {
|
|
677
|
+
click: false,
|
|
678
|
+
contextmenu: false,
|
|
679
|
+
dblclick: false,
|
|
680
|
+
wheel: false, // passive wheel errors with OrbitControls
|
|
681
|
+
pointerdown: true,
|
|
682
|
+
pointerup: true,
|
|
683
|
+
pointerleave: true,
|
|
684
|
+
pointermove: true,
|
|
685
|
+
pointercancel: true,
|
|
686
|
+
lostpointercapture: true,
|
|
687
|
+
};
|
|
688
|
+
const supportedEvents = [
|
|
689
|
+
'click',
|
|
690
|
+
'contextmenu',
|
|
691
|
+
'dblclick',
|
|
692
|
+
'pointerup',
|
|
693
|
+
'pointerdown',
|
|
694
|
+
'pointerover',
|
|
695
|
+
'pointerout',
|
|
696
|
+
'pointerenter',
|
|
697
|
+
'pointerleave',
|
|
698
|
+
'pointermove',
|
|
699
|
+
'pointermissed',
|
|
700
|
+
'pointercancel',
|
|
701
|
+
'wheel',
|
|
702
|
+
];
|
|
703
|
+
function createPointerEvents(store) {
|
|
704
|
+
const { handlePointer } = createEvents(store);
|
|
705
|
+
return {
|
|
706
|
+
priority: 1,
|
|
707
|
+
enabled: true,
|
|
708
|
+
compute: (event, root) => {
|
|
709
|
+
const state = root.get();
|
|
710
|
+
// https://github.com/pmndrs/react-three-fiber/pull/782
|
|
711
|
+
// Events trigger outside of canvas when moved, use offsetX/Y by default and allow overrides
|
|
712
|
+
state.pointer.set((event.offsetX / state.size.width) * 2 - 1, -(event.offsetY / state.size.height) * 2 + 1);
|
|
713
|
+
state.raycaster.setFromCamera(state.pointer, state.camera);
|
|
714
|
+
},
|
|
715
|
+
connected: undefined,
|
|
716
|
+
handlers: Object.keys(DOM_EVENTS).reduce((handlers, supportedEventName) => {
|
|
717
|
+
handlers[supportedEventName] = handlePointer(supportedEventName);
|
|
718
|
+
return handlers;
|
|
719
|
+
}, {}),
|
|
720
|
+
update: () => {
|
|
721
|
+
const { events, internal } = store.get();
|
|
722
|
+
if (internal.lastEvent?.nativeElement && events.handlers)
|
|
723
|
+
events.handlers.pointermove(internal.lastEvent.nativeElement);
|
|
724
|
+
},
|
|
725
|
+
connect: (target) => {
|
|
726
|
+
const state = store.get();
|
|
727
|
+
state.events.disconnect?.();
|
|
728
|
+
state.setEvents({ connected: target });
|
|
729
|
+
Object.entries(state.events.handlers ?? {}).forEach(([eventName, eventHandler]) => {
|
|
730
|
+
const passive = DOM_EVENTS[eventName];
|
|
731
|
+
target.addEventListener(eventName, eventHandler, { passive });
|
|
732
|
+
});
|
|
733
|
+
},
|
|
734
|
+
disconnect: () => {
|
|
735
|
+
const { events, setEvents } = store.get();
|
|
736
|
+
if (events.connected) {
|
|
737
|
+
Object.entries(events.handlers ?? {}).forEach(([eventName, eventHandler]) => {
|
|
738
|
+
if (events.connected instanceof HTMLElement) {
|
|
739
|
+
events.connected.removeEventListener(eventName, eventHandler);
|
|
740
|
+
}
|
|
741
|
+
});
|
|
742
|
+
setEvents({ connected: undefined });
|
|
743
|
+
}
|
|
744
|
+
},
|
|
745
|
+
};
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
// This function prepares a set of changes to be applied to the instance
|
|
749
|
+
function diffProps(instance, props) {
|
|
750
|
+
const propsEntries = Object.entries(props);
|
|
751
|
+
const changes = [];
|
|
752
|
+
for (const [propKey, propValue] of propsEntries) {
|
|
753
|
+
let key = propKey;
|
|
754
|
+
if (is.colorSpaceExist(instance)) {
|
|
755
|
+
if (propKey === 'encoding') {
|
|
756
|
+
key = 'colorSpace';
|
|
757
|
+
}
|
|
758
|
+
else if (propKey === 'outputEncoding') {
|
|
759
|
+
key = 'outputColorSpace';
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
if (is.equ(propValue, instance[key]))
|
|
763
|
+
continue;
|
|
764
|
+
changes.push([propKey, propValue]);
|
|
765
|
+
}
|
|
766
|
+
return changes;
|
|
767
|
+
}
|
|
768
|
+
// This function applies a set of changes to the instance
|
|
769
|
+
function applyProps(instance, props) {
|
|
770
|
+
// if props is empty
|
|
771
|
+
if (!Object.keys(props).length)
|
|
772
|
+
return instance;
|
|
773
|
+
// filter equals, and reserved props
|
|
774
|
+
const localState = getLocalState(instance);
|
|
775
|
+
const rootState = localState?.store?.snapshot ?? {};
|
|
776
|
+
const changes = diffProps(instance, props);
|
|
777
|
+
for (let i = 0; i < changes.length; i++) {
|
|
778
|
+
let [key, value] = changes[i];
|
|
779
|
+
// Alias (output)encoding => (output)colorSpace (since r152)
|
|
780
|
+
// https://github.com/pmndrs/react-three-fiber/pull/2829
|
|
781
|
+
if (is.colorSpaceExist(instance)) {
|
|
782
|
+
const sRGBEncoding = 3001;
|
|
783
|
+
const SRGBColorSpace = 'srgb';
|
|
784
|
+
const LinearSRGBColorSpace = 'srgb-linear';
|
|
785
|
+
if (key === 'encoding') {
|
|
786
|
+
key = 'colorSpace';
|
|
787
|
+
value = value === sRGBEncoding ? SRGBColorSpace : LinearSRGBColorSpace;
|
|
788
|
+
}
|
|
789
|
+
else if (key === 'outputEncoding') {
|
|
790
|
+
key = 'outputColorSpace';
|
|
791
|
+
value = value === sRGBEncoding ? SRGBColorSpace : LinearSRGBColorSpace;
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
const currentInstance = instance;
|
|
795
|
+
const targetProp = currentInstance[key];
|
|
796
|
+
// special treatmen for objects with support for set/copy, and layers
|
|
797
|
+
if (targetProp && targetProp['set'] && (targetProp['copy'] || targetProp instanceof THREE.Layers)) {
|
|
798
|
+
const isColor = targetProp instanceof THREE.Color;
|
|
799
|
+
// if value is an array
|
|
800
|
+
if (Array.isArray(value)) {
|
|
801
|
+
if (targetProp['fromArray'])
|
|
802
|
+
targetProp['fromArray'](value);
|
|
803
|
+
else
|
|
804
|
+
targetProp['set'](...value);
|
|
805
|
+
} // test again target.copy
|
|
806
|
+
else if (targetProp['copy'] &&
|
|
807
|
+
value &&
|
|
808
|
+
value.constructor &&
|
|
809
|
+
targetProp.constructor.name === value.constructor.name) {
|
|
810
|
+
targetProp['copy'](value);
|
|
811
|
+
if (!THREE.ColorManagement && !rootState.linear && isColor)
|
|
812
|
+
targetProp['convertSRGBToLinear']();
|
|
813
|
+
} // if nothing else fits, just set the single value, ignore undefined
|
|
814
|
+
else if (value !== undefined) {
|
|
815
|
+
const isColor = targetProp instanceof THREE.Color;
|
|
816
|
+
// allow setting array scalars
|
|
817
|
+
if (!isColor && targetProp['setScalar'])
|
|
818
|
+
targetProp['setScalar'](value);
|
|
819
|
+
// layers have no copy function, copy the mask
|
|
820
|
+
else if (targetProp instanceof THREE.Layers && value instanceof THREE.Layers)
|
|
821
|
+
targetProp.mask = value.mask;
|
|
822
|
+
// otherwise just set ...
|
|
823
|
+
else
|
|
824
|
+
targetProp['set'](value);
|
|
825
|
+
// auto-convert srgb
|
|
826
|
+
if (!THREE.ColorManagement && !rootState?.linear && isColor)
|
|
827
|
+
targetProp.convertSRGBToLinear();
|
|
828
|
+
}
|
|
829
|
+
} // else just overwrite the value
|
|
830
|
+
else {
|
|
831
|
+
currentInstance[key] = value;
|
|
832
|
+
// auto-convert srgb textures
|
|
833
|
+
if (currentInstance[key] instanceof THREE.Texture &&
|
|
834
|
+
currentInstance[key].format === THREE.RGBAFormat &&
|
|
835
|
+
currentInstance[key].type === THREE.UnsignedByteType) {
|
|
836
|
+
const texture = currentInstance[key];
|
|
837
|
+
if (rootState?.gl) {
|
|
838
|
+
if (is.colorSpaceExist(texture) && is.colorSpaceExist(rootState.gl))
|
|
839
|
+
texture.colorSpace = rootState.gl.outputColorSpace;
|
|
840
|
+
// @ts-expect-error - these are removed in new version of three but still available in older versions
|
|
841
|
+
else
|
|
842
|
+
texture['encoding'] = rootState.gl['outputEncoding'];
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
checkUpdate(currentInstance[key]);
|
|
847
|
+
checkUpdate(targetProp);
|
|
848
|
+
invalidateInstance(instance);
|
|
849
|
+
}
|
|
850
|
+
const instanceHandlersCount = localState?.eventCount;
|
|
851
|
+
const parent = localState?.instanceStore?.get('parent');
|
|
852
|
+
if (parent && rootState.internal && instance['raycast'] && instanceHandlersCount !== localState?.eventCount) {
|
|
853
|
+
// Pre-emptively remove the instance from the interaction manager
|
|
854
|
+
const index = rootState.internal.interaction.indexOf(instance);
|
|
855
|
+
if (index > -1)
|
|
856
|
+
rootState.internal.interaction.splice(index, 1);
|
|
857
|
+
// Add the instance to the interaction manager only when it has handlers
|
|
858
|
+
if (localState?.eventCount)
|
|
859
|
+
rootState.internal.interaction.push(instance);
|
|
860
|
+
}
|
|
861
|
+
if (parent && localState?.afterUpdate && localState.afterUpdate.observed && changes.length) {
|
|
862
|
+
localState.afterUpdate.next(instance);
|
|
863
|
+
}
|
|
864
|
+
return instance;
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
const shallowLoose = { objects: 'shallow', strict: false };
|
|
868
|
+
const roots = new Map();
|
|
869
|
+
function injectCanvasRootInitializer(injector) {
|
|
870
|
+
return assertInjector(injectCanvasRootInitializer, injector, () => {
|
|
871
|
+
const injectedStore = injectNgtStore();
|
|
872
|
+
const loop = injectNgtLoop();
|
|
873
|
+
return (canvas) => {
|
|
874
|
+
const exist = roots.has(canvas);
|
|
875
|
+
let store = roots.get(canvas);
|
|
876
|
+
if (store) {
|
|
877
|
+
console.warn('[NGT] Same canvas root is being created twice');
|
|
878
|
+
}
|
|
879
|
+
store ||= injectedStore;
|
|
880
|
+
if (!store) {
|
|
881
|
+
throw new Error('[NGT] No store initialized');
|
|
882
|
+
}
|
|
883
|
+
if (!exist) {
|
|
884
|
+
roots.set(canvas, store);
|
|
885
|
+
}
|
|
886
|
+
let isConfigured = false;
|
|
887
|
+
let lastCamera;
|
|
888
|
+
return {
|
|
889
|
+
isConfigured,
|
|
890
|
+
destroy: (timeout = 500) => {
|
|
891
|
+
const root = roots.get(canvas);
|
|
892
|
+
if (root) {
|
|
893
|
+
root.update((state) => ({ internal: { ...state.internal, active: false } }));
|
|
894
|
+
setTimeout(() => {
|
|
895
|
+
try {
|
|
896
|
+
const state = root.get();
|
|
897
|
+
state.events.disconnect?.();
|
|
898
|
+
state.gl?.renderLists?.dispose?.();
|
|
899
|
+
state.gl?.forceContextLoss?.();
|
|
900
|
+
if (state.gl?.xr)
|
|
901
|
+
state.xr.disconnect();
|
|
902
|
+
dispose(state);
|
|
903
|
+
roots.delete(canvas);
|
|
904
|
+
}
|
|
905
|
+
catch (e) {
|
|
906
|
+
console.error('[NGT] Unexpected error while destroying Canvas Root', e);
|
|
907
|
+
}
|
|
908
|
+
}, timeout);
|
|
909
|
+
}
|
|
910
|
+
},
|
|
911
|
+
configure: (inputs) => {
|
|
912
|
+
const { shadows = false, linear = false, flat = false, legacy = false, orthographic = false, frameloop = 'always', dpr = [1, 2], gl: glOptions, size: sizeOptions, camera: cameraOptions, raycaster: raycasterOptions, scene: sceneOptions, events, lookAt, performance, } = inputs;
|
|
913
|
+
const state = store.snapshot;
|
|
914
|
+
const stateToUpdate = {};
|
|
915
|
+
// setup renderer
|
|
916
|
+
let gl = state.gl;
|
|
917
|
+
if (!state.gl)
|
|
918
|
+
stateToUpdate.gl = gl = makeRendererInstance(glOptions, canvas);
|
|
919
|
+
// setup raycaster
|
|
920
|
+
let raycaster = state.raycaster;
|
|
921
|
+
if (!raycaster)
|
|
922
|
+
stateToUpdate.raycaster = raycaster = new THREE.Raycaster();
|
|
923
|
+
// set raycaster options
|
|
924
|
+
const { params, ...options } = raycasterOptions || {};
|
|
925
|
+
if (!is.equ(options, raycaster, shallowLoose))
|
|
926
|
+
applyProps(raycaster, options);
|
|
927
|
+
if (!is.equ(params, raycaster.params, shallowLoose)) {
|
|
928
|
+
applyProps(raycaster, { params: { ...raycaster.params, ...(params || {}) } });
|
|
929
|
+
}
|
|
930
|
+
// Create default camera, don't overwrite any user-set state
|
|
931
|
+
if (!state.camera || (state.camera === lastCamera && !is.equ(lastCamera, cameraOptions, shallowLoose))) {
|
|
932
|
+
lastCamera = cameraOptions;
|
|
933
|
+
const isCamera = is.camera(cameraOptions);
|
|
934
|
+
let camera = isCamera ? cameraOptions : makeCameraInstance(orthographic, state.size);
|
|
935
|
+
if (!isCamera) {
|
|
936
|
+
camera.position.z = 5;
|
|
937
|
+
if (cameraOptions) {
|
|
938
|
+
applyProps(camera, cameraOptions);
|
|
939
|
+
if ('aspect' in cameraOptions ||
|
|
940
|
+
'left' in cameraOptions ||
|
|
941
|
+
'right' in cameraOptions ||
|
|
942
|
+
'top' in cameraOptions ||
|
|
943
|
+
'bottom' in cameraOptions) {
|
|
944
|
+
Object.assign(camera, { manual: true });
|
|
945
|
+
camera?.updateProjectionMatrix();
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
// always look at center or passed-in lookAt by default
|
|
949
|
+
if (!state.camera && !cameraOptions?.rotation && !cameraOptions?.quaternion) {
|
|
950
|
+
if (Array.isArray(lookAt))
|
|
951
|
+
camera.lookAt(lookAt[0], lookAt[1], lookAt[2]);
|
|
952
|
+
else if (lookAt instanceof THREE.Vector3)
|
|
953
|
+
camera.lookAt(lookAt);
|
|
954
|
+
else
|
|
955
|
+
camera.lookAt(0, 0, 0);
|
|
956
|
+
}
|
|
957
|
+
// update projection matrix after applyprops
|
|
958
|
+
camera.updateProjectionMatrix?.();
|
|
959
|
+
}
|
|
960
|
+
if (!is.instance(camera))
|
|
961
|
+
camera = prepare(camera, { store });
|
|
962
|
+
stateToUpdate.camera = camera;
|
|
963
|
+
// Configure raycaster
|
|
964
|
+
// https://github.com/pmndrs/react-xr/issues/300
|
|
965
|
+
raycaster.camera = camera;
|
|
966
|
+
}
|
|
967
|
+
// Set up scene (one time only!)
|
|
968
|
+
if (!state.scene) {
|
|
969
|
+
let scene;
|
|
970
|
+
if (sceneOptions instanceof THREE.Scene) {
|
|
971
|
+
scene = sceneOptions;
|
|
972
|
+
}
|
|
973
|
+
else {
|
|
974
|
+
scene = new THREE.Scene();
|
|
975
|
+
if (sceneOptions)
|
|
976
|
+
applyProps(scene, sceneOptions);
|
|
977
|
+
}
|
|
978
|
+
stateToUpdate.scene = prepare(scene, { store });
|
|
979
|
+
}
|
|
980
|
+
// Set up XR (one time only!)
|
|
981
|
+
if (!state.xr) {
|
|
982
|
+
// Handle frame behavior in WebXR
|
|
983
|
+
const handleXRFrame = (timestamp, frame) => {
|
|
984
|
+
const state = store.snapshot;
|
|
985
|
+
if (state.frameloop === 'never')
|
|
986
|
+
return;
|
|
987
|
+
loop.advance(timestamp, true, store, frame);
|
|
988
|
+
};
|
|
989
|
+
// Toggle render switching on session
|
|
990
|
+
const handleSessionChange = () => {
|
|
991
|
+
const state = store.snapshot;
|
|
992
|
+
state.gl.xr.enabled = state.gl.xr.isPresenting;
|
|
993
|
+
state.gl.xr.setAnimationLoop(state.gl.xr.isPresenting ? handleXRFrame : null);
|
|
994
|
+
if (!state.gl.xr.isPresenting)
|
|
995
|
+
loop.invalidate(store);
|
|
996
|
+
};
|
|
997
|
+
// WebXR session manager
|
|
998
|
+
const xr = {
|
|
999
|
+
connect: () => {
|
|
1000
|
+
gl.xr.addEventListener('sessionstart', handleSessionChange);
|
|
1001
|
+
gl.xr.addEventListener('sessionend', handleSessionChange);
|
|
1002
|
+
},
|
|
1003
|
+
disconnect: () => {
|
|
1004
|
+
gl.xr.removeEventListener('sessionstart', handleSessionChange);
|
|
1005
|
+
gl.xr.removeEventListener('sessionend', handleSessionChange);
|
|
1006
|
+
},
|
|
1007
|
+
};
|
|
1008
|
+
// Subscribe to WebXR session events
|
|
1009
|
+
if (gl.xr && typeof gl.xr.addEventListener === 'function')
|
|
1010
|
+
xr.connect();
|
|
1011
|
+
stateToUpdate.xr = xr;
|
|
1012
|
+
}
|
|
1013
|
+
// Set shadowmap
|
|
1014
|
+
if (gl.shadowMap) {
|
|
1015
|
+
const oldEnabled = gl.shadowMap.enabled;
|
|
1016
|
+
const oldType = gl.shadowMap.type;
|
|
1017
|
+
gl.shadowMap.enabled = !!shadows;
|
|
1018
|
+
if (typeof shadows === 'boolean') {
|
|
1019
|
+
gl.shadowMap.type = THREE.PCFSoftShadowMap;
|
|
1020
|
+
}
|
|
1021
|
+
else if (typeof shadows === 'string') {
|
|
1022
|
+
const types = {
|
|
1023
|
+
basic: THREE.BasicShadowMap,
|
|
1024
|
+
percentage: THREE.PCFShadowMap,
|
|
1025
|
+
soft: THREE.PCFSoftShadowMap,
|
|
1026
|
+
variance: THREE.VSMShadowMap,
|
|
1027
|
+
};
|
|
1028
|
+
gl.shadowMap.type = types[shadows] ?? THREE.PCFSoftShadowMap;
|
|
1029
|
+
}
|
|
1030
|
+
else if (is.obj(shadows)) {
|
|
1031
|
+
Object.assign(gl.shadowMap, shadows);
|
|
1032
|
+
}
|
|
1033
|
+
if (oldEnabled !== gl.shadowMap.enabled || oldType !== gl.shadowMap.type)
|
|
1034
|
+
checkNeedsUpdate(gl.shadowMap);
|
|
1035
|
+
}
|
|
1036
|
+
// Safely set color management if available.
|
|
1037
|
+
// Avoid accessing THREE.ColorManagement to play nice with older versions
|
|
1038
|
+
if (THREE.ColorManagement) {
|
|
1039
|
+
const ColorManagement = THREE.ColorManagement;
|
|
1040
|
+
if ('enabled' in ColorManagement)
|
|
1041
|
+
ColorManagement['enabled'] = !legacy || false;
|
|
1042
|
+
else if ('legacyMode' in ColorManagement)
|
|
1043
|
+
ColorManagement['legacyMode'] = legacy || true;
|
|
1044
|
+
}
|
|
1045
|
+
if (!isConfigured) {
|
|
1046
|
+
// set color space and tonemapping preferences once
|
|
1047
|
+
const LinearEncoding = 3000;
|
|
1048
|
+
const sRGBEncoding = 3001;
|
|
1049
|
+
applyProps(gl, {
|
|
1050
|
+
outputEncoding: linear ? LinearEncoding : sRGBEncoding,
|
|
1051
|
+
toneMapping: flat ? THREE.NoToneMapping : THREE.ACESFilmicToneMapping,
|
|
1052
|
+
});
|
|
1053
|
+
}
|
|
1054
|
+
// Update color management state
|
|
1055
|
+
if (state.legacy !== legacy)
|
|
1056
|
+
stateToUpdate.legacy = legacy;
|
|
1057
|
+
if (state.linear !== linear)
|
|
1058
|
+
stateToUpdate.linear = linear;
|
|
1059
|
+
if (state.flat !== flat)
|
|
1060
|
+
stateToUpdate.flat = flat;
|
|
1061
|
+
// Set gl props
|
|
1062
|
+
gl.setClearAlpha(0);
|
|
1063
|
+
gl.setPixelRatio(makeDpr(state.viewport.dpr));
|
|
1064
|
+
gl.setSize(state.size.width, state.size.height);
|
|
1065
|
+
if (is.obj(glOptions) &&
|
|
1066
|
+
!(typeof glOptions === 'function') &&
|
|
1067
|
+
!is.renderer(glOptions) &&
|
|
1068
|
+
!is.equ(glOptions, gl, shallowLoose)) {
|
|
1069
|
+
applyProps(gl, glOptions);
|
|
1070
|
+
}
|
|
1071
|
+
// Store events internally
|
|
1072
|
+
if (events && !state.events.handlers)
|
|
1073
|
+
stateToUpdate.events = events(store);
|
|
1074
|
+
// Check performance
|
|
1075
|
+
if (performance && !is.equ(performance, state.performance, shallowLoose)) {
|
|
1076
|
+
stateToUpdate.performance = { ...state.performance, ...performance };
|
|
1077
|
+
}
|
|
1078
|
+
store.update(stateToUpdate);
|
|
1079
|
+
// Check size, allow it to take on container bounds initially
|
|
1080
|
+
const size = computeInitialSize(canvas, sizeOptions);
|
|
1081
|
+
if (!is.equ(size, state.size, shallowLoose)) {
|
|
1082
|
+
state.setSize(size.width, size.height, size.top, size.left);
|
|
1083
|
+
}
|
|
1084
|
+
// Check pixelratio
|
|
1085
|
+
if (dpr && state.viewport.dpr !== makeDpr(dpr))
|
|
1086
|
+
state.setDpr(dpr);
|
|
1087
|
+
// Check frameloop
|
|
1088
|
+
if (state.frameloop !== frameloop)
|
|
1089
|
+
state.setFrameloop(frameloop);
|
|
1090
|
+
isConfigured = true;
|
|
1091
|
+
},
|
|
1092
|
+
};
|
|
1093
|
+
};
|
|
1094
|
+
});
|
|
1095
|
+
}
|
|
1096
|
+
function computeInitialSize(canvas, defaultSize) {
|
|
1097
|
+
if (defaultSize) {
|
|
1098
|
+
return defaultSize;
|
|
1099
|
+
}
|
|
1100
|
+
if (typeof HTMLCanvasElement !== 'undefined' && canvas instanceof HTMLCanvasElement && canvas.parentElement) {
|
|
1101
|
+
return canvas.parentElement.getBoundingClientRect();
|
|
1102
|
+
}
|
|
1103
|
+
if (typeof OffscreenCanvas !== 'undefined' && canvas instanceof OffscreenCanvas) {
|
|
1104
|
+
return { width: canvas.width, height: canvas.height, top: 0, left: 0 };
|
|
1105
|
+
}
|
|
1106
|
+
return { width: 0, height: 0, top: 0, left: 0 };
|
|
1107
|
+
}
|
|
1108
|
+
// Disposes an object and all its properties
|
|
1109
|
+
function dispose(obj) {
|
|
1110
|
+
if (obj.dispose && obj.type !== 'Scene')
|
|
1111
|
+
obj.dispose();
|
|
1112
|
+
for (const p in obj) {
|
|
1113
|
+
p.dispose?.();
|
|
1114
|
+
delete obj[p];
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
function createSubs(callback, subs) {
|
|
1119
|
+
const sub = { callback };
|
|
1120
|
+
subs.add(sub);
|
|
1121
|
+
return () => void subs.delete(sub);
|
|
1122
|
+
}
|
|
1123
|
+
const globalEffects = new Set();
|
|
1124
|
+
const globalAfterEffects = new Set();
|
|
1125
|
+
const globalTailEffects = new Set();
|
|
1126
|
+
/**
|
|
1127
|
+
* Adds a global render callback which is called each frame.
|
|
1128
|
+
* @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#addEffect
|
|
1129
|
+
*/
|
|
1130
|
+
const addEffect = (callback) => createSubs(callback, globalEffects);
|
|
1131
|
+
/**
|
|
1132
|
+
* Adds a global after-render callback which is called each frame.
|
|
1133
|
+
* @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#addAfterEffect
|
|
1134
|
+
*/
|
|
1135
|
+
const addAfterEffect = (callback) => createSubs(callback, globalAfterEffects);
|
|
1136
|
+
/**
|
|
1137
|
+
* Adds a global callback which is called when rendering stops.
|
|
1138
|
+
* @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#addTail
|
|
1139
|
+
*/
|
|
1140
|
+
const addTail = (callback) => createSubs(callback, globalTailEffects);
|
|
1141
|
+
function run(effects, timestamp) {
|
|
1142
|
+
if (!effects.size)
|
|
1143
|
+
return;
|
|
1144
|
+
for (const { callback } of effects.values()) {
|
|
1145
|
+
callback(timestamp);
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
function flushGlobalEffects(type, timestamp) {
|
|
1149
|
+
switch (type) {
|
|
1150
|
+
case 'before':
|
|
1151
|
+
return run(globalEffects, timestamp);
|
|
1152
|
+
case 'after':
|
|
1153
|
+
return run(globalAfterEffects, timestamp);
|
|
1154
|
+
case 'tail':
|
|
1155
|
+
return run(globalTailEffects, timestamp);
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
function render(timestamp, store, frame) {
|
|
1159
|
+
const state = store.snapshot;
|
|
1160
|
+
// Run local effects
|
|
1161
|
+
let delta = state.clock.getDelta();
|
|
1162
|
+
// In frameloop='never' mode, clock times are updated using the provided timestamp
|
|
1163
|
+
if (state.frameloop === 'never' && typeof timestamp === 'number') {
|
|
1164
|
+
delta = timestamp - state.clock.elapsedTime;
|
|
1165
|
+
state.clock.oldTime = state.clock.elapsedTime;
|
|
1166
|
+
state.clock.elapsedTime = timestamp;
|
|
1167
|
+
}
|
|
1168
|
+
// Call subscribers (beforeRender)
|
|
1169
|
+
const subscribers = state.internal.subscribers;
|
|
1170
|
+
for (let i = 0; i < subscribers.length; i++) {
|
|
1171
|
+
const subscription = subscribers[i];
|
|
1172
|
+
subscription.callback({ ...subscription.store.snapshot, delta, frame });
|
|
1173
|
+
}
|
|
1174
|
+
// Render content
|
|
1175
|
+
if (!state.internal.priority && state.gl.render)
|
|
1176
|
+
state.gl.render(state.scene, state.camera);
|
|
1177
|
+
// Decrease frame count
|
|
1178
|
+
state.internal.frames = Math.max(0, state.internal.frames - 1);
|
|
1179
|
+
return state.frameloop === 'always' ? 1 : state.internal.frames;
|
|
1180
|
+
}
|
|
1181
|
+
function createLoop(roots) {
|
|
1182
|
+
let running = false;
|
|
1183
|
+
let injectBeforeRenderInProgress = false;
|
|
1184
|
+
let repeat;
|
|
1185
|
+
let frame;
|
|
1186
|
+
function loop(timestamp) {
|
|
1187
|
+
frame = requestAnimationFrame(loop);
|
|
1188
|
+
running = true;
|
|
1189
|
+
repeat = 0;
|
|
1190
|
+
// Run effects
|
|
1191
|
+
flushGlobalEffects('before', timestamp);
|
|
1192
|
+
// Render all roots
|
|
1193
|
+
injectBeforeRenderInProgress = true;
|
|
1194
|
+
for (const root of roots.values()) {
|
|
1195
|
+
const state = root.snapshot;
|
|
1196
|
+
// If the frameloop is invalidated, do not run another frame
|
|
1197
|
+
if (state.internal.active &&
|
|
1198
|
+
(state.frameloop === 'always' || state.internal.frames > 0) &&
|
|
1199
|
+
!state.gl.xr?.isPresenting) {
|
|
1200
|
+
repeat += render(timestamp, root);
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
injectBeforeRenderInProgress = false;
|
|
1204
|
+
// Run after-effects
|
|
1205
|
+
flushGlobalEffects('after', timestamp);
|
|
1206
|
+
// Stop the loop if nothing invalidates it
|
|
1207
|
+
if (repeat === 0) {
|
|
1208
|
+
// Tail call effects, they are called when rendering stops
|
|
1209
|
+
flushGlobalEffects('tail', timestamp);
|
|
1210
|
+
// Flag end of operation
|
|
1211
|
+
running = false;
|
|
1212
|
+
return cancelAnimationFrame(frame);
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
function invalidate(store, frames = 1) {
|
|
1216
|
+
const state = store?.snapshot;
|
|
1217
|
+
if (!state)
|
|
1218
|
+
return roots.forEach((root) => invalidate(root, frames));
|
|
1219
|
+
if (state.gl.xr?.isPresenting || !state.internal.active || state.frameloop === 'never')
|
|
1220
|
+
return;
|
|
1221
|
+
if (frames > 1) {
|
|
1222
|
+
// legacy support for people using frames parameters
|
|
1223
|
+
// Increase frames, do not go higher than 60
|
|
1224
|
+
state.internal.frames = Math.min(60, state.internal.frames + frames);
|
|
1225
|
+
}
|
|
1226
|
+
else {
|
|
1227
|
+
if (injectBeforeRenderInProgress) {
|
|
1228
|
+
// called from within a injectBeforeRender, it means the user wants an additional frame
|
|
1229
|
+
state.internal.frames = 2;
|
|
1230
|
+
}
|
|
1231
|
+
else {
|
|
1232
|
+
// the user needs a new frame, no need to increment
|
|
1233
|
+
state.internal.frames = 1;
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
// If the render-loop isn't active, start it
|
|
1237
|
+
if (!running) {
|
|
1238
|
+
running = true;
|
|
1239
|
+
requestAnimationFrame(loop);
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
function advance(timestamp, runGlobalEffects = true, store, frame) {
|
|
1243
|
+
if (runGlobalEffects)
|
|
1244
|
+
flushGlobalEffects('before', timestamp);
|
|
1245
|
+
if (!store)
|
|
1246
|
+
for (const root of roots.values())
|
|
1247
|
+
render(timestamp, root);
|
|
1248
|
+
else
|
|
1249
|
+
render(timestamp, store, frame);
|
|
1250
|
+
if (runGlobalEffects)
|
|
1251
|
+
flushGlobalEffects('after', timestamp);
|
|
1252
|
+
}
|
|
1253
|
+
return {
|
|
1254
|
+
loop,
|
|
1255
|
+
/**
|
|
1256
|
+
* Invalidates the view, requesting a frame to be rendered. Will globally invalidate unless passed a root's state.
|
|
1257
|
+
* @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#invalidate
|
|
1258
|
+
*/
|
|
1259
|
+
invalidate,
|
|
1260
|
+
/**
|
|
1261
|
+
* Advances the frameloop and runs render effects, useful for when manually rendering via `frameloop="never"`.
|
|
1262
|
+
* @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#advance
|
|
1263
|
+
*/
|
|
1264
|
+
advance,
|
|
1265
|
+
};
|
|
1266
|
+
}
|
|
1267
|
+
const [injectNgtLoop] = createInjectionToken(() => createLoop(roots));
|
|
1268
|
+
|
|
1269
|
+
function storeFactory(previousStore) {
|
|
1270
|
+
const document = inject(DOCUMENT);
|
|
1271
|
+
const window = document.defaultView;
|
|
1272
|
+
if (!window) {
|
|
1273
|
+
// TODO: revisit this when we need to support multiple platforms
|
|
1274
|
+
throw new Error(`[NGT] Window is not available.`);
|
|
1275
|
+
}
|
|
1276
|
+
const loop = injectNgtLoop();
|
|
1277
|
+
// NOTE: using Subject because we do not care about late-subscribers
|
|
1278
|
+
const pointerMissed$ = new Subject();
|
|
1279
|
+
const store = signalStore(({ get, update }) => {
|
|
1280
|
+
const { invalidate, advance } = loop;
|
|
1281
|
+
const position = new THREE.Vector3();
|
|
1282
|
+
const defaultTarget = new THREE.Vector3();
|
|
1283
|
+
const tempTarget = new THREE.Vector3();
|
|
1284
|
+
function getCurrentViewport(camera = get('camera'), target = defaultTarget, size = get('size')) {
|
|
1285
|
+
const { width, height, top, left } = size;
|
|
1286
|
+
const aspect = width / height;
|
|
1287
|
+
if (target instanceof THREE.Vector3)
|
|
1288
|
+
tempTarget.copy(target);
|
|
1289
|
+
else
|
|
1290
|
+
tempTarget.set(...target);
|
|
1291
|
+
const distance = camera.getWorldPosition(position).distanceTo(tempTarget);
|
|
1292
|
+
if (is.orthographicCamera(camera)) {
|
|
1293
|
+
return { width: width / camera.zoom, height: height / camera.zoom, top, left, factor: 1, distance, aspect };
|
|
1294
|
+
}
|
|
1295
|
+
const fov = (camera.fov * Math.PI) / 180; // convert vertical fov to radians
|
|
1296
|
+
const h = 2 * Math.tan(fov / 2) * distance; // visible height
|
|
1297
|
+
const w = h * (width / height);
|
|
1298
|
+
return { width: w, height: h, top, left, factor: width / w, distance, aspect };
|
|
1299
|
+
}
|
|
1300
|
+
let performanceTimeout = undefined;
|
|
1301
|
+
const setPerformanceCurrent = (current) => update((state) => ({ performance: { ...state.performance, current } }));
|
|
1302
|
+
const pointer = new THREE.Vector2();
|
|
1303
|
+
return {
|
|
1304
|
+
pointerMissed$: pointerMissed$.asObservable(),
|
|
1305
|
+
events: { priority: 1, enabled: true, connected: false },
|
|
1306
|
+
invalidate: (frames = 1) => invalidate(store, frames),
|
|
1307
|
+
advance: (timestamp, runGlobalEffects) => advance(timestamp, runGlobalEffects, store),
|
|
1308
|
+
legacy: false,
|
|
1309
|
+
linear: false,
|
|
1310
|
+
flat: false,
|
|
1311
|
+
controls: null,
|
|
1312
|
+
clock: new THREE.Clock(),
|
|
1313
|
+
pointer,
|
|
1314
|
+
frameloop: 'always',
|
|
1315
|
+
performance: {
|
|
1316
|
+
current: 1,
|
|
1317
|
+
min: 0.5,
|
|
1318
|
+
max: 1,
|
|
1319
|
+
debounce: 200,
|
|
1320
|
+
regress: () => {
|
|
1321
|
+
const state = get();
|
|
1322
|
+
// Clear timeout
|
|
1323
|
+
if (performanceTimeout)
|
|
1324
|
+
clearTimeout(performanceTimeout);
|
|
1325
|
+
// Set lower bound performance
|
|
1326
|
+
if (state.performance.current !== state.performance.min)
|
|
1327
|
+
setPerformanceCurrent(state.performance.min);
|
|
1328
|
+
// Go back to upper bound performance after a while unless something regresses meanwhile
|
|
1329
|
+
performanceTimeout = setTimeout(() => {
|
|
1330
|
+
setPerformanceCurrent(get('performance', 'max'));
|
|
1331
|
+
// safeDetectChanges(cdr);
|
|
1332
|
+
}, state.performance.debounce);
|
|
1333
|
+
},
|
|
1334
|
+
},
|
|
1335
|
+
size: { width: 0, height: 0, top: 0, left: 0, updateStyle: false },
|
|
1336
|
+
viewport: {
|
|
1337
|
+
initialDpr: 0,
|
|
1338
|
+
dpr: 0,
|
|
1339
|
+
width: 0,
|
|
1340
|
+
height: 0,
|
|
1341
|
+
top: 0,
|
|
1342
|
+
left: 0,
|
|
1343
|
+
aspect: 0,
|
|
1344
|
+
distance: 0,
|
|
1345
|
+
factor: 0,
|
|
1346
|
+
getCurrentViewport,
|
|
1347
|
+
},
|
|
1348
|
+
setEvents: (events) => update((state) => ({ ...state, events: { ...state.events, ...events } })),
|
|
1349
|
+
setSize: (width, height, top, left) => {
|
|
1350
|
+
const camera = get('camera');
|
|
1351
|
+
const size = { width, height, top: top || 0, left: left || 0 };
|
|
1352
|
+
update((state) => ({
|
|
1353
|
+
size,
|
|
1354
|
+
viewport: { ...state.viewport, ...getCurrentViewport(camera, defaultTarget, size) },
|
|
1355
|
+
}));
|
|
1356
|
+
},
|
|
1357
|
+
setDpr: (dpr) => update((state) => {
|
|
1358
|
+
const resolved = makeDpr(dpr, window);
|
|
1359
|
+
return { viewport: { ...state.viewport, dpr: resolved, initialDpr: state.viewport.initialDpr || resolved } };
|
|
1360
|
+
}),
|
|
1361
|
+
setFrameloop: (frameloop = 'always') => {
|
|
1362
|
+
const clock = get('clock');
|
|
1363
|
+
// if frameloop === "never" clock.elapsedTime is updated using advance(timestamp)
|
|
1364
|
+
clock.stop();
|
|
1365
|
+
clock.elapsedTime = 0;
|
|
1366
|
+
if (frameloop !== 'never') {
|
|
1367
|
+
clock.start();
|
|
1368
|
+
clock.elapsedTime = 0;
|
|
1369
|
+
}
|
|
1370
|
+
update(() => ({ frameloop }));
|
|
1371
|
+
},
|
|
1372
|
+
previousRoot: previousStore,
|
|
1373
|
+
internal: {
|
|
1374
|
+
active: false,
|
|
1375
|
+
priority: 0,
|
|
1376
|
+
frames: 0,
|
|
1377
|
+
lastEvent: new ElementRef(null),
|
|
1378
|
+
interaction: [],
|
|
1379
|
+
hovered: new Map(),
|
|
1380
|
+
subscribers: [],
|
|
1381
|
+
initialClick: [0, 0],
|
|
1382
|
+
initialHits: [],
|
|
1383
|
+
capturedMap: new Map(),
|
|
1384
|
+
subscribe: (callback, priority = 0, _store = store) => {
|
|
1385
|
+
const internal = get('internal');
|
|
1386
|
+
// If this subscription was given a priority, it takes rendering into its own hands
|
|
1387
|
+
// For that reason we switch off automatic rendering and increase the manual flag
|
|
1388
|
+
// As long as this flag is positive there can be no internal rendering at all
|
|
1389
|
+
// because there could be multiple render subscriptions
|
|
1390
|
+
internal.priority = internal.priority + (priority > 0 ? 1 : 0);
|
|
1391
|
+
internal.subscribers.push({ callback, priority, store: _store });
|
|
1392
|
+
// Register subscriber and sort layers from lowest to highest, meaning,
|
|
1393
|
+
// highest priority renders last (on top of the other frames)
|
|
1394
|
+
internal.subscribers = internal.subscribers.sort((a, b) => (a.priority || 0) - (b.priority || 0));
|
|
1395
|
+
return () => {
|
|
1396
|
+
const internal = get('internal');
|
|
1397
|
+
if (internal?.subscribers) {
|
|
1398
|
+
// Decrease manual flag if this subscription had a priority
|
|
1399
|
+
internal.priority = internal.priority - (priority > 0 ? 1 : 0);
|
|
1400
|
+
// Remove subscriber from list
|
|
1401
|
+
internal.subscribers = internal.subscribers.filter((s) => s.callback !== callback);
|
|
1402
|
+
}
|
|
1403
|
+
};
|
|
1404
|
+
},
|
|
1405
|
+
},
|
|
1406
|
+
};
|
|
1407
|
+
});
|
|
1408
|
+
Object.defineProperty(store, 'pointerMissed$', { get: () => pointerMissed$ });
|
|
1409
|
+
let { size: oldSize, viewport: { dpr: oldDpr }, camera: oldCamera, } = store.snapshot;
|
|
1410
|
+
const [camera, size, viewportDpr] = [store.select('camera'), store.select('size'), store.select('viewport', 'dpr')];
|
|
1411
|
+
effect(() => {
|
|
1412
|
+
const [newCamera, newSize, newDpr, gl] = [camera(), size(), viewportDpr(), store.snapshot.gl];
|
|
1413
|
+
// Resize camera and renderer on changes to size and pixel-ratio
|
|
1414
|
+
if (newSize !== oldSize || newDpr !== oldDpr) {
|
|
1415
|
+
oldSize = newSize;
|
|
1416
|
+
oldDpr = newDpr;
|
|
1417
|
+
// Update camera & renderer
|
|
1418
|
+
updateCamera(newCamera, newSize);
|
|
1419
|
+
gl.setPixelRatio(newDpr);
|
|
1420
|
+
const updateStyle = typeof HTMLCanvasElement !== 'undefined' && gl.domElement instanceof HTMLCanvasElement;
|
|
1421
|
+
gl.setSize(newSize.width, newSize.height, updateStyle);
|
|
1422
|
+
}
|
|
1423
|
+
// Update viewport once the camera changes
|
|
1424
|
+
if (newCamera !== oldCamera) {
|
|
1425
|
+
oldCamera = newCamera;
|
|
1426
|
+
updateCamera(newCamera, newSize);
|
|
1427
|
+
// Update viewport
|
|
1428
|
+
store.update((state) => ({ viewport: { ...state.viewport, ...state.viewport.getCurrentViewport(newCamera) } }));
|
|
1429
|
+
}
|
|
1430
|
+
});
|
|
1431
|
+
return store;
|
|
1432
|
+
}
|
|
1433
|
+
const NGT_STORE = new InjectionToken('NgtStore Token');
|
|
1434
|
+
const [injectNgtStore, provideNgtStore] = createInjectionToken(storeFactory, {
|
|
1435
|
+
isRoot: false,
|
|
1436
|
+
deps: [[new Optional(), new SkipSelf(), NGT_STORE]],
|
|
1437
|
+
token: NGT_STORE,
|
|
1438
|
+
});
|
|
1439
|
+
|
|
1440
|
+
const catalogue = {};
|
|
1441
|
+
function extend(objects) {
|
|
1442
|
+
Object.assign(catalogue, objects);
|
|
1443
|
+
}
|
|
1444
|
+
const [injectNgtCatalogue] = createInjectionToken(() => catalogue);
|
|
1445
|
+
|
|
1446
|
+
const ROUTED_SCENE = '__ngt_renderer_is_routed_scene__';
|
|
1447
|
+
const HTML = '__ngt_renderer_is_html';
|
|
1448
|
+
const SPECIAL_INTERNAL_ADD_COMMENT = '__ngt_renderer_add_comment__';
|
|
1449
|
+
const SPECIAL_DOM_TAG = {
|
|
1450
|
+
NGT_PORTAL: 'ngt-portal',
|
|
1451
|
+
NGT_PRIMITIVE: 'ngt-primitive',
|
|
1452
|
+
NGT_VALUE: 'ngt-value',
|
|
1453
|
+
};
|
|
1454
|
+
const SPECIAL_PROPERTIES = {
|
|
1455
|
+
COMPOUND: 'ngtCompound',
|
|
1456
|
+
RENDER_PRIORITY: 'priority',
|
|
1457
|
+
ATTACH: 'attach',
|
|
1458
|
+
RAW_VALUE: 'rawValue',
|
|
1459
|
+
REF: 'ref',
|
|
1460
|
+
};
|
|
1461
|
+
const SPECIAL_EVENTS = {
|
|
1462
|
+
BEFORE_RENDER: 'beforeRender',
|
|
1463
|
+
AFTER_UPDATE: 'afterUpdate',
|
|
1464
|
+
AFTER_ATTACH: 'afterAttach',
|
|
1465
|
+
};
|
|
1466
|
+
|
|
1467
|
+
const [injectNodeType, provideNodeType] = createInjectionToken(() => '', {
|
|
1468
|
+
isRoot: false,
|
|
1469
|
+
});
|
|
1470
|
+
class NgtCommonDirective {
|
|
1471
|
+
constructor() {
|
|
1472
|
+
this.vcr = inject(ViewContainerRef);
|
|
1473
|
+
this.zone = inject(NgZone);
|
|
1474
|
+
this.template = inject(TemplateRef);
|
|
1475
|
+
this.nodeType = injectNodeType();
|
|
1476
|
+
this.injected = false;
|
|
1477
|
+
this.shouldCreateView = true;
|
|
1478
|
+
const commentNode = this.vcr.element.nativeElement;
|
|
1479
|
+
if (commentNode[SPECIAL_INTERNAL_ADD_COMMENT]) {
|
|
1480
|
+
commentNode[SPECIAL_INTERNAL_ADD_COMMENT](this.nodeType);
|
|
1481
|
+
delete commentNode[SPECIAL_INTERNAL_ADD_COMMENT];
|
|
1482
|
+
}
|
|
1483
|
+
inject(DestroyRef).onDestroy(() => {
|
|
1484
|
+
this.view?.destroy();
|
|
1485
|
+
});
|
|
1486
|
+
}
|
|
1487
|
+
createView() {
|
|
1488
|
+
this.zone.runOutsideAngular(() => {
|
|
1489
|
+
if (this.shouldCreateView) {
|
|
1490
|
+
if (this.view && !this.view.destroyed) {
|
|
1491
|
+
this.view.destroy();
|
|
1492
|
+
}
|
|
1493
|
+
this.view = this.vcr.createEmbeddedView(this.template);
|
|
1494
|
+
this.view.detectChanges();
|
|
1495
|
+
}
|
|
1496
|
+
});
|
|
1497
|
+
}
|
|
1498
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.0.0", ngImport: i0, type: NgtCommonDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive }); }
|
|
1499
|
+
static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "18.0.0", type: NgtCommonDirective, ngImport: i0 }); }
|
|
1500
|
+
}
|
|
1501
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.0", ngImport: i0, type: NgtCommonDirective, decorators: [{
|
|
1502
|
+
type: Directive
|
|
1503
|
+
}], ctorParameters: () => [] });
|
|
1504
|
+
|
|
1505
|
+
class NgtArgs extends NgtCommonDirective {
|
|
1506
|
+
constructor() {
|
|
1507
|
+
super();
|
|
1508
|
+
this.args = input(null);
|
|
1509
|
+
this.injectedArgs = [];
|
|
1510
|
+
effect(() => {
|
|
1511
|
+
const args = this.args();
|
|
1512
|
+
if (args == null || !Array.isArray(args) || (args.length === 1 && args[0] === null))
|
|
1513
|
+
return;
|
|
1514
|
+
this.injected = false;
|
|
1515
|
+
this.injectedArgs = args;
|
|
1516
|
+
this.createView();
|
|
1517
|
+
});
|
|
1518
|
+
}
|
|
1519
|
+
get value() {
|
|
1520
|
+
if (this.validate()) {
|
|
1521
|
+
this.injected = true;
|
|
1522
|
+
return this.injectedArgs;
|
|
1523
|
+
}
|
|
1524
|
+
return null;
|
|
1525
|
+
}
|
|
1526
|
+
validate() {
|
|
1527
|
+
return !this.injected && !!this.injectedArgs.length;
|
|
1528
|
+
}
|
|
1529
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.0.0", ngImport: i0, type: NgtArgs, deps: [], target: i0.ɵɵFactoryTarget.Directive }); }
|
|
1530
|
+
static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "18.0.0", type: NgtArgs, isStandalone: true, selector: "ng-template[args]", inputs: { args: { classPropertyName: "args", publicName: "args", isSignal: true, isRequired: false, transformFunction: null } }, providers: [provideNodeType('args')], usesInheritance: true, ngImport: i0 }); }
|
|
1531
|
+
}
|
|
1532
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.0", ngImport: i0, type: NgtArgs, decorators: [{
|
|
1533
|
+
type: Directive,
|
|
1534
|
+
args: [{ selector: 'ng-template[args]', standalone: true, providers: [provideNodeType('args')] }]
|
|
1535
|
+
}], ctorParameters: () => [] });
|
|
1536
|
+
|
|
1537
|
+
class NgtParent extends NgtCommonDirective {
|
|
1538
|
+
constructor() {
|
|
1539
|
+
super();
|
|
1540
|
+
this.parent = input.required();
|
|
1541
|
+
this.injectedParent = null;
|
|
1542
|
+
effect(() => {
|
|
1543
|
+
const parent = this.parent();
|
|
1544
|
+
if (!parent)
|
|
1545
|
+
return;
|
|
1546
|
+
this.injected = false;
|
|
1547
|
+
this.injectedParent = parent;
|
|
1548
|
+
this.createView();
|
|
1549
|
+
});
|
|
1550
|
+
}
|
|
1551
|
+
get value() {
|
|
1552
|
+
if (this.validate()) {
|
|
1553
|
+
this.injected = true;
|
|
1554
|
+
return this.injectedParent;
|
|
1555
|
+
}
|
|
1556
|
+
return null;
|
|
1557
|
+
}
|
|
1558
|
+
validate() {
|
|
1559
|
+
return !this.injected && !!this.injectedParent;
|
|
1560
|
+
}
|
|
1561
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.0.0", ngImport: i0, type: NgtParent, deps: [], target: i0.ɵɵFactoryTarget.Directive }); }
|
|
1562
|
+
static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "18.0.0", type: NgtParent, isStandalone: true, selector: "ng-template[parent]", inputs: { parent: { classPropertyName: "parent", publicName: "parent", isSignal: true, isRequired: true, transformFunction: null } }, providers: [provideNodeType('parent')], usesInheritance: true, ngImport: i0 }); }
|
|
1563
|
+
}
|
|
1564
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.0", ngImport: i0, type: NgtParent, decorators: [{
|
|
1565
|
+
type: Directive,
|
|
1566
|
+
args: [{ selector: 'ng-template[parent]', standalone: true, providers: [provideNodeType('parent')] }]
|
|
1567
|
+
}], ctorParameters: () => [] });
|
|
1568
|
+
|
|
1569
|
+
function attach(object, value, paths = []) {
|
|
1570
|
+
const [base, ...remaining] = paths;
|
|
1571
|
+
if (!base)
|
|
1572
|
+
return;
|
|
1573
|
+
if (remaining.length === 0) {
|
|
1574
|
+
applyProps(object, { [base]: value });
|
|
1575
|
+
}
|
|
1576
|
+
else {
|
|
1577
|
+
assignEmpty(object, base);
|
|
1578
|
+
attach(object[base], value, remaining);
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
function detach(parent, child, attachProp) {
|
|
1582
|
+
const childLocalState = getLocalState(child);
|
|
1583
|
+
if (childLocalState) {
|
|
1584
|
+
if (Array.isArray(attachProp))
|
|
1585
|
+
attach(parent, childLocalState.previousAttach, attachProp);
|
|
1586
|
+
else
|
|
1587
|
+
childLocalState.previousAttach();
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
function assignEmpty(obj, base) {
|
|
1591
|
+
if ((!Object.hasOwn(obj, base) && Reflect && !!Reflect.has && !Reflect.has(obj, base)) || obj[base] === undefined) {
|
|
1592
|
+
obj[base] = {};
|
|
1593
|
+
}
|
|
1594
|
+
}
|
|
1595
|
+
function createAttachFunction(cb) {
|
|
1596
|
+
return (parent, child, store) => cb({ parent, child, store });
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
// @internal
|
|
1600
|
+
var NgtRendererClassId;
|
|
1601
|
+
(function (NgtRendererClassId) {
|
|
1602
|
+
NgtRendererClassId[NgtRendererClassId["type"] = 0] = "type";
|
|
1603
|
+
NgtRendererClassId[NgtRendererClassId["parent"] = 1] = "parent";
|
|
1604
|
+
NgtRendererClassId[NgtRendererClassId["injectedParent"] = 2] = "injectedParent";
|
|
1605
|
+
NgtRendererClassId[NgtRendererClassId["children"] = 3] = "children";
|
|
1606
|
+
NgtRendererClassId[NgtRendererClassId["destroyed"] = 4] = "destroyed";
|
|
1607
|
+
NgtRendererClassId[NgtRendererClassId["compound"] = 5] = "compound";
|
|
1608
|
+
NgtRendererClassId[NgtRendererClassId["compoundParent"] = 6] = "compoundParent";
|
|
1609
|
+
NgtRendererClassId[NgtRendererClassId["compounded"] = 7] = "compounded";
|
|
1610
|
+
NgtRendererClassId[NgtRendererClassId["queueOps"] = 8] = "queueOps";
|
|
1611
|
+
NgtRendererClassId[NgtRendererClassId["attributes"] = 9] = "attributes";
|
|
1612
|
+
NgtRendererClassId[NgtRendererClassId["properties"] = 10] = "properties";
|
|
1613
|
+
NgtRendererClassId[NgtRendererClassId["rawValue"] = 11] = "rawValue";
|
|
1614
|
+
NgtRendererClassId[NgtRendererClassId["ref"] = 12] = "ref";
|
|
1615
|
+
NgtRendererClassId[NgtRendererClassId["portalContainer"] = 13] = "portalContainer";
|
|
1616
|
+
NgtRendererClassId[NgtRendererClassId["injectorFactory"] = 14] = "injectorFactory";
|
|
1617
|
+
})(NgtRendererClassId || (NgtRendererClassId = {}));
|
|
1618
|
+
// @internal
|
|
1619
|
+
var NgtCompoundClassId;
|
|
1620
|
+
(function (NgtCompoundClassId) {
|
|
1621
|
+
NgtCompoundClassId[NgtCompoundClassId["applyFirst"] = 0] = "applyFirst";
|
|
1622
|
+
NgtCompoundClassId[NgtCompoundClassId["props"] = 1] = "props";
|
|
1623
|
+
})(NgtCompoundClassId || (NgtCompoundClassId = {}));
|
|
1624
|
+
// @internal
|
|
1625
|
+
var NgtQueueOpClassId;
|
|
1626
|
+
(function (NgtQueueOpClassId) {
|
|
1627
|
+
NgtQueueOpClassId[NgtQueueOpClassId["type"] = 0] = "type";
|
|
1628
|
+
NgtQueueOpClassId[NgtQueueOpClassId["op"] = 1] = "op";
|
|
1629
|
+
NgtQueueOpClassId[NgtQueueOpClassId["done"] = 2] = "done";
|
|
1630
|
+
})(NgtQueueOpClassId || (NgtQueueOpClassId = {}));
|
|
1631
|
+
function kebabToPascal(str) {
|
|
1632
|
+
// split the string at each hyphen
|
|
1633
|
+
const parts = str.split('-');
|
|
1634
|
+
// map over the parts, capitalizing the first letter of each part
|
|
1635
|
+
const pascalParts = parts.map((part) => part.charAt(0).toUpperCase() + part.slice(1));
|
|
1636
|
+
// join the parts together to create the final PascalCase string
|
|
1637
|
+
return pascalParts.join('');
|
|
1638
|
+
}
|
|
1639
|
+
function attachThreeChild(parent, child) {
|
|
1640
|
+
const pLS = getLocalState(parent);
|
|
1641
|
+
const cLS = getLocalState(child);
|
|
1642
|
+
if (!pLS || !cLS) {
|
|
1643
|
+
throw new Error(`[NGT] THREE instances need to be prepared with local state.`);
|
|
1644
|
+
}
|
|
1645
|
+
// whether the child is added to the parent with parent.add()
|
|
1646
|
+
let added = false;
|
|
1647
|
+
// assign store on child if not already exist
|
|
1648
|
+
// or child store is the parent of parent store
|
|
1649
|
+
if (!cLS.store || cLS.store === pLS.store.get('previousRoot')) {
|
|
1650
|
+
cLS.store = pLS.store;
|
|
1651
|
+
}
|
|
1652
|
+
if (cLS.attach) {
|
|
1653
|
+
const attachProp = cLS.attach;
|
|
1654
|
+
if (typeof attachProp === 'function') {
|
|
1655
|
+
const attachCleanUp = attachProp(parent, child, cLS.store);
|
|
1656
|
+
if (attachCleanUp)
|
|
1657
|
+
cLS.previousAttach = attachCleanUp;
|
|
1658
|
+
}
|
|
1659
|
+
else {
|
|
1660
|
+
// we skip attach none if set explicitly
|
|
1661
|
+
if (attachProp[0] === 'none') {
|
|
1662
|
+
invalidateInstance(child);
|
|
1663
|
+
return;
|
|
1664
|
+
}
|
|
1665
|
+
// handle material array
|
|
1666
|
+
if (attachProp[0] === 'material' &&
|
|
1667
|
+
attachProp[1] &&
|
|
1668
|
+
typeof Number(attachProp[1]) === 'number' &&
|
|
1669
|
+
is.material(child) &&
|
|
1670
|
+
!Array.isArray(parent['material'])) {
|
|
1671
|
+
parent['material'] = [];
|
|
1672
|
+
}
|
|
1673
|
+
// attach
|
|
1674
|
+
if (cLS.isRaw) {
|
|
1675
|
+
if (cLS.instanceStore.get('parent') !== parent) {
|
|
1676
|
+
cLS.setParent(parent);
|
|
1677
|
+
}
|
|
1678
|
+
// at this point we don't have rawValue yet, so we bail and wait until the Renderer recalls attach
|
|
1679
|
+
if (child.__ngt_renderer__[NgtRendererClassId.rawValue] === undefined)
|
|
1680
|
+
return;
|
|
1681
|
+
attach(parent, child.__ngt_renderer__[NgtRendererClassId.rawValue], attachProp);
|
|
1682
|
+
}
|
|
1683
|
+
else {
|
|
1684
|
+
attach(parent, child, attachProp);
|
|
1685
|
+
}
|
|
1686
|
+
// save value
|
|
1687
|
+
cLS.previousAttach = attachProp.reduce((value, property) => value[property], parent);
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
else if (is.object3D(parent) && is.object3D(child)) {
|
|
1691
|
+
parent.add(child);
|
|
1692
|
+
added = true;
|
|
1693
|
+
}
|
|
1694
|
+
pLS.add(child, added ? 'objects' : 'nonObjects');
|
|
1695
|
+
if (cLS.instanceStore.get('parent') !== parent) {
|
|
1696
|
+
cLS.setParent(parent);
|
|
1697
|
+
}
|
|
1698
|
+
if (cLS.afterAttach)
|
|
1699
|
+
cLS.afterAttach.next({ parent, node: child });
|
|
1700
|
+
invalidateInstance(child);
|
|
1701
|
+
invalidateInstance(parent);
|
|
1702
|
+
}
|
|
1703
|
+
function removeThreeChild(parent, child, dispose) {
|
|
1704
|
+
const pLS = getLocalState(parent);
|
|
1705
|
+
const cLS = getLocalState(child);
|
|
1706
|
+
// clear parent ref
|
|
1707
|
+
cLS?.setParent(null);
|
|
1708
|
+
// remove child from parent
|
|
1709
|
+
pLS?.remove(child, 'objects');
|
|
1710
|
+
pLS?.remove(child, 'nonObjects');
|
|
1711
|
+
if (cLS?.attach) {
|
|
1712
|
+
detach(parent, child, cLS.attach);
|
|
1713
|
+
}
|
|
1714
|
+
else if (is.object3D(parent) && is.object3D(child)) {
|
|
1715
|
+
parent.remove(child);
|
|
1716
|
+
const store = cLS?.store || pLS?.store;
|
|
1717
|
+
if (store)
|
|
1718
|
+
removeInteractivity(store, child);
|
|
1719
|
+
}
|
|
1720
|
+
const isPrimitive = cLS?.primitive;
|
|
1721
|
+
if (!isPrimitive) {
|
|
1722
|
+
removeThreeRecursive(cLS?.instanceStore.get('objects') || [], child, !!dispose);
|
|
1723
|
+
removeThreeRecursive(child.children, child, !!dispose);
|
|
1724
|
+
}
|
|
1725
|
+
// dispose
|
|
1726
|
+
if (!isPrimitive && child['dispose'] && !is.scene(child)) {
|
|
1727
|
+
queueMicrotask(() => child['dispose']());
|
|
1728
|
+
}
|
|
1729
|
+
invalidateInstance(parent);
|
|
1730
|
+
}
|
|
1731
|
+
function removeThreeRecursive(array, parent, dispose) {
|
|
1732
|
+
if (array)
|
|
1733
|
+
[...array].forEach((child) => removeThreeChild(parent, child, dispose));
|
|
1734
|
+
}
|
|
1735
|
+
function processThreeEvent(instance, priority, eventName, callback) {
|
|
1736
|
+
const lS = getLocalState(instance);
|
|
1737
|
+
if (!lS) {
|
|
1738
|
+
console.warn('[NGT] instance has not been prepared yet.');
|
|
1739
|
+
return () => { };
|
|
1740
|
+
}
|
|
1741
|
+
if (eventName === SPECIAL_EVENTS.BEFORE_RENDER) {
|
|
1742
|
+
return lS.store
|
|
1743
|
+
.get('internal')
|
|
1744
|
+
.subscribe((state) => callback({ state, object: instance }), priority || lS.priority || 0);
|
|
1745
|
+
}
|
|
1746
|
+
if (eventName === SPECIAL_EVENTS.AFTER_UPDATE || eventName === SPECIAL_EVENTS.AFTER_ATTACH) {
|
|
1747
|
+
let emitter = lS[eventName];
|
|
1748
|
+
if (!emitter)
|
|
1749
|
+
emitter = lS[eventName] = new ReplaySubject(1);
|
|
1750
|
+
const sub = emitter.subscribe(callback);
|
|
1751
|
+
// NOTE: for afterAttach event, if the instance already has a parent,
|
|
1752
|
+
// then we'll emit the event immediately
|
|
1753
|
+
if (eventName === SPECIAL_EVENTS.AFTER_ATTACH && untracked(lS.parent)) {
|
|
1754
|
+
emitter.next({ parent: untracked(lS.parent), node: instance });
|
|
1755
|
+
}
|
|
1756
|
+
return () => {
|
|
1757
|
+
sub.unsubscribe();
|
|
1758
|
+
if (emitter && !emitter.observed) {
|
|
1759
|
+
emitter.complete();
|
|
1760
|
+
}
|
|
1761
|
+
};
|
|
1762
|
+
}
|
|
1763
|
+
if (!lS.handlers)
|
|
1764
|
+
lS.handlers = {};
|
|
1765
|
+
// try to get the previous handler. compound might have one, the THREE object might also have one with the same name
|
|
1766
|
+
const previousHandler = lS.handlers[eventName];
|
|
1767
|
+
// readjust the callback
|
|
1768
|
+
const updatedCallback = (event) => {
|
|
1769
|
+
if (previousHandler)
|
|
1770
|
+
previousHandler(event);
|
|
1771
|
+
callback(event);
|
|
1772
|
+
};
|
|
1773
|
+
Object.assign(lS.handlers, { [eventName]: updatedCallback });
|
|
1774
|
+
// increment the count everytime
|
|
1775
|
+
lS.eventCount += 1;
|
|
1776
|
+
// but only add the instance (target) to the interaction array (so that it is handled by the EventManager with Raycast)
|
|
1777
|
+
// the first time eventCount is incremented
|
|
1778
|
+
if (lS.eventCount === 1 && instance['raycast'])
|
|
1779
|
+
lS.store.get('internal', 'interaction').push(instance);
|
|
1780
|
+
// clean up the event listener by removing the target from the interaction array
|
|
1781
|
+
return () => {
|
|
1782
|
+
const lS = getLocalState(instance);
|
|
1783
|
+
if (lS) {
|
|
1784
|
+
lS.eventCount -= 1;
|
|
1785
|
+
const interactions = lS.store.get('internal', 'interaction') || [];
|
|
1786
|
+
const index = interactions.findIndex((obj) => obj.uuid === instance.uuid);
|
|
1787
|
+
if (index >= 0)
|
|
1788
|
+
interactions.splice(index, 1);
|
|
1789
|
+
}
|
|
1790
|
+
};
|
|
1791
|
+
}
|
|
1792
|
+
|
|
1793
|
+
const NGT_COMPOUND_PREFIXES = new InjectionToken('NgtCompoundPrefixes');
|
|
1794
|
+
class NgtRendererStore {
|
|
1795
|
+
constructor(rootState) {
|
|
1796
|
+
this.rootState = rootState;
|
|
1797
|
+
this.argsCommentNodes = [];
|
|
1798
|
+
this.parentCommentNodes = [];
|
|
1799
|
+
this.portalCommentsNodes = [];
|
|
1800
|
+
}
|
|
1801
|
+
createNode(type, node) {
|
|
1802
|
+
const state = [
|
|
1803
|
+
type,
|
|
1804
|
+
null,
|
|
1805
|
+
null,
|
|
1806
|
+
[],
|
|
1807
|
+
false,
|
|
1808
|
+
undefined,
|
|
1809
|
+
undefined,
|
|
1810
|
+
undefined,
|
|
1811
|
+
undefined,
|
|
1812
|
+
undefined,
|
|
1813
|
+
undefined,
|
|
1814
|
+
undefined,
|
|
1815
|
+
undefined,
|
|
1816
|
+
undefined,
|
|
1817
|
+
undefined,
|
|
1818
|
+
];
|
|
1819
|
+
const rendererNode = Object.assign(node, { __ngt_renderer__: state });
|
|
1820
|
+
// NOTE: assign ownerDocument to node so we can use HostListener in Component
|
|
1821
|
+
if (!rendererNode['ownerDocument'])
|
|
1822
|
+
rendererNode['ownerDocument'] = this.rootState.document;
|
|
1823
|
+
// NOTE: assign injectorFactory on non-three type since
|
|
1824
|
+
// rendererNode is an instance of DOM Node
|
|
1825
|
+
if (state[NgtRendererClassId.type] !== 'three') {
|
|
1826
|
+
state[NgtRendererClassId.injectorFactory] = () => getDebugNode(rendererNode)?.injector;
|
|
1827
|
+
}
|
|
1828
|
+
if (state[NgtRendererClassId.type] === 'comment') {
|
|
1829
|
+
// NOTE: we attach an arrow function to the Comment node
|
|
1830
|
+
// In our directives, we can call this function to then start tracking the RendererNode
|
|
1831
|
+
// this is done to limit the amount of Nodes we need to process for getCreationState
|
|
1832
|
+
rendererNode[SPECIAL_INTERNAL_ADD_COMMENT] = (node) => {
|
|
1833
|
+
if (node === 'args') {
|
|
1834
|
+
this.argsCommentNodes.push(rendererNode);
|
|
1835
|
+
}
|
|
1836
|
+
else if (node === 'parent') {
|
|
1837
|
+
this.parentCommentNodes.push(rendererNode);
|
|
1838
|
+
}
|
|
1839
|
+
else if (typeof node === 'object') {
|
|
1840
|
+
this.portalCommentsNodes.push(node);
|
|
1841
|
+
}
|
|
1842
|
+
};
|
|
1843
|
+
return rendererNode;
|
|
1844
|
+
}
|
|
1845
|
+
if (state[NgtRendererClassId.type] === 'compound') {
|
|
1846
|
+
state[NgtRendererClassId.queueOps] = new Set();
|
|
1847
|
+
state[NgtRendererClassId.attributes] = state[NgtRendererClassId.properties] = {};
|
|
1848
|
+
return rendererNode;
|
|
1849
|
+
}
|
|
1850
|
+
return rendererNode;
|
|
1851
|
+
}
|
|
1852
|
+
isCompound(name) {
|
|
1853
|
+
return this.rootState.compoundPrefixes.some((prefix) => name.startsWith(prefix));
|
|
1854
|
+
}
|
|
1855
|
+
isDOM(node) {
|
|
1856
|
+
const rS = node['__ngt_renderer__'];
|
|
1857
|
+
return (!rS ||
|
|
1858
|
+
(rS[NgtRendererClassId.type] !== 'compound' &&
|
|
1859
|
+
(node instanceof Element || node instanceof Document || node instanceof Window)));
|
|
1860
|
+
}
|
|
1861
|
+
getClosestParentWithInstance(node) {
|
|
1862
|
+
let parent = node.__ngt_renderer__[NgtRendererClassId.parent];
|
|
1863
|
+
while (parent && parent.__ngt_renderer__[NgtRendererClassId.type] !== 'three') {
|
|
1864
|
+
parent = parent.__ngt_renderer__[NgtRendererClassId.portalContainer]
|
|
1865
|
+
? parent.__ngt_renderer__[NgtRendererClassId.portalContainer]
|
|
1866
|
+
: parent.__ngt_renderer__[NgtRendererClassId.parent];
|
|
1867
|
+
}
|
|
1868
|
+
return parent;
|
|
1869
|
+
}
|
|
1870
|
+
getClosestParentWithCompound(node) {
|
|
1871
|
+
if (node.__ngt_renderer__[NgtRendererClassId.compoundParent]) {
|
|
1872
|
+
return node.__ngt_renderer__[NgtRendererClassId.compoundParent];
|
|
1873
|
+
}
|
|
1874
|
+
let parent = node.__ngt_renderer__[NgtRendererClassId.parent];
|
|
1875
|
+
if (parent &&
|
|
1876
|
+
parent.__ngt_renderer__[NgtRendererClassId.type] === 'compound' &&
|
|
1877
|
+
!parent.__ngt_renderer__[NgtRendererClassId.compounded]) {
|
|
1878
|
+
return parent;
|
|
1879
|
+
}
|
|
1880
|
+
while (parent &&
|
|
1881
|
+
(parent.__ngt_renderer__[NgtRendererClassId.type] === 'three' ||
|
|
1882
|
+
!parent.__ngt_renderer__[NgtRendererClassId.compoundParent] ||
|
|
1883
|
+
parent.__ngt_renderer__[NgtRendererClassId.type] !== 'compound')) {
|
|
1884
|
+
parent = parent.__ngt_renderer__[NgtRendererClassId.parent];
|
|
1885
|
+
}
|
|
1886
|
+
if (!parent)
|
|
1887
|
+
return null;
|
|
1888
|
+
if (parent.__ngt_renderer__[NgtRendererClassId.type] === 'three' &&
|
|
1889
|
+
parent.__ngt_renderer__[NgtRendererClassId.compoundParent]) {
|
|
1890
|
+
return parent.__ngt_renderer__[NgtRendererClassId.compoundParent];
|
|
1891
|
+
}
|
|
1892
|
+
if (!parent.__ngt_renderer__[NgtRendererClassId.compounded]) {
|
|
1893
|
+
return parent;
|
|
1894
|
+
}
|
|
1895
|
+
return null;
|
|
1896
|
+
}
|
|
1897
|
+
processPortalContainer(portal) {
|
|
1898
|
+
const injector = portal.__ngt_renderer__[NgtRendererClassId.injectorFactory]?.();
|
|
1899
|
+
if (!injector)
|
|
1900
|
+
return;
|
|
1901
|
+
const portalStore = injector.get(NGT_STORE, null);
|
|
1902
|
+
if (!portalStore)
|
|
1903
|
+
return;
|
|
1904
|
+
const portalContainer = portalStore.get('scene');
|
|
1905
|
+
if (!portalContainer)
|
|
1906
|
+
return;
|
|
1907
|
+
portal.__ngt_renderer__[NgtRendererClassId.portalContainer] = this.createNode('three', portalContainer);
|
|
1908
|
+
}
|
|
1909
|
+
getCreationState() {
|
|
1910
|
+
return [
|
|
1911
|
+
this.firstNonInjectedDirective('argsCommentNodes', NgtArgs)?.value || [],
|
|
1912
|
+
this.firstNonInjectedDirective('parentCommentNodes', NgtParent)?.value || null,
|
|
1913
|
+
this.tryGetPortalStore(),
|
|
1914
|
+
];
|
|
1915
|
+
}
|
|
1916
|
+
setParent(node, parent) {
|
|
1917
|
+
if (!node.__ngt_renderer__[NgtRendererClassId.parent]) {
|
|
1918
|
+
node.__ngt_renderer__[NgtRendererClassId.parent] = parent;
|
|
1919
|
+
}
|
|
1920
|
+
}
|
|
1921
|
+
addChild(node, child) {
|
|
1922
|
+
if (!node.__ngt_renderer__[NgtRendererClassId.children].includes(child)) {
|
|
1923
|
+
node.__ngt_renderer__[NgtRendererClassId.children].push(child);
|
|
1924
|
+
}
|
|
1925
|
+
}
|
|
1926
|
+
removeChild(node, child) {
|
|
1927
|
+
const index = node.__ngt_renderer__?.[NgtRendererClassId.children].findIndex((c) => child === c);
|
|
1928
|
+
if (index >= 0) {
|
|
1929
|
+
node.__ngt_renderer__[NgtRendererClassId.children].splice(index, 1);
|
|
1930
|
+
}
|
|
1931
|
+
}
|
|
1932
|
+
setCompound(compound, instance) {
|
|
1933
|
+
const instanceRS = instance.__ngt_renderer__;
|
|
1934
|
+
if (instanceRS && instanceRS[NgtRendererClassId.parent]) {
|
|
1935
|
+
const parentRS = instanceRS[NgtRendererClassId.parent].__ngt_renderer__;
|
|
1936
|
+
// NOTE: if instance is already compounded by its parent. skip
|
|
1937
|
+
if (parentRS[NgtRendererClassId.type] === 'compound' && parentRS[NgtRendererClassId.compounded] === instance) {
|
|
1938
|
+
return;
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
const rS = compound.__ngt_renderer__;
|
|
1942
|
+
rS[NgtRendererClassId.compounded] = instance;
|
|
1943
|
+
for (const key of Object.keys(rS[NgtRendererClassId.attributes])) {
|
|
1944
|
+
this.applyAttribute(instance, key, rS[NgtRendererClassId.attributes][key]);
|
|
1945
|
+
}
|
|
1946
|
+
for (const key of Object.keys(rS[NgtRendererClassId.properties])) {
|
|
1947
|
+
this.applyProperty(instance, key, rS[NgtRendererClassId.properties][key]);
|
|
1948
|
+
}
|
|
1949
|
+
this.executeOperation(compound);
|
|
1950
|
+
}
|
|
1951
|
+
queueOperation(node, op) {
|
|
1952
|
+
node.__ngt_renderer__[NgtRendererClassId.queueOps].add(op);
|
|
1953
|
+
}
|
|
1954
|
+
executeOperation(node, type = 'op') {
|
|
1955
|
+
const rS = node.__ngt_renderer__;
|
|
1956
|
+
// TODO: maybe an array with pop() would work better
|
|
1957
|
+
if (rS[NgtRendererClassId.queueOps]?.size) {
|
|
1958
|
+
rS[NgtRendererClassId.queueOps].forEach((op) => {
|
|
1959
|
+
if (op[NgtQueueOpClassId.type] === type) {
|
|
1960
|
+
op[NgtQueueOpClassId.op]();
|
|
1961
|
+
rS[NgtRendererClassId.queueOps].delete(op);
|
|
1962
|
+
}
|
|
1963
|
+
});
|
|
1964
|
+
}
|
|
1965
|
+
}
|
|
1966
|
+
applyAttribute(node, name, value) {
|
|
1967
|
+
const rS = node.__ngt_renderer__;
|
|
1968
|
+
if (rS[NgtRendererClassId.destroyed])
|
|
1969
|
+
return;
|
|
1970
|
+
if (name === SPECIAL_PROPERTIES.RENDER_PRIORITY) {
|
|
1971
|
+
// NOTE: priority needs to be set as an attribute string so that they can be set as early as possible
|
|
1972
|
+
// we convert that string to a number. if it's invalid, default 0
|
|
1973
|
+
let priority = Number(value);
|
|
1974
|
+
if (isNaN(priority)) {
|
|
1975
|
+
priority = 0;
|
|
1976
|
+
console.warn(`[NGT] "priority" is an invalid number, default to 0`);
|
|
1977
|
+
}
|
|
1978
|
+
const localState = getLocalState(node);
|
|
1979
|
+
if (localState) {
|
|
1980
|
+
localState.priority = priority;
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1983
|
+
if (name === SPECIAL_PROPERTIES.COMPOUND) {
|
|
1984
|
+
// NOTE: we set the compound property on instance node now so we know that this instance is being compounded
|
|
1985
|
+
rS[NgtRendererClassId.compound] = [value === '' || value === 'first', {}];
|
|
1986
|
+
return;
|
|
1987
|
+
}
|
|
1988
|
+
if (name === SPECIAL_PROPERTIES.ATTACH) {
|
|
1989
|
+
// NOTE: handle attach as tring
|
|
1990
|
+
const paths = value.split('.');
|
|
1991
|
+
if (paths.length) {
|
|
1992
|
+
const localState = getLocalState(node);
|
|
1993
|
+
if (localState) {
|
|
1994
|
+
localState.attach = paths;
|
|
1995
|
+
}
|
|
1996
|
+
}
|
|
1997
|
+
return;
|
|
1998
|
+
}
|
|
1999
|
+
// NOTE: coercion
|
|
2000
|
+
let maybeCoerced = value;
|
|
2001
|
+
if (maybeCoerced === '' || maybeCoerced === 'true' || maybeCoerced === 'false') {
|
|
2002
|
+
maybeCoerced = maybeCoerced === 'true' || maybeCoerced === '';
|
|
2003
|
+
}
|
|
2004
|
+
else if (!isNaN(Number(maybeCoerced))) {
|
|
2005
|
+
maybeCoerced = Number(maybeCoerced);
|
|
2006
|
+
}
|
|
2007
|
+
if (name === SPECIAL_PROPERTIES.RAW_VALUE) {
|
|
2008
|
+
rS[NgtRendererClassId.rawValue] = maybeCoerced;
|
|
2009
|
+
return;
|
|
2010
|
+
}
|
|
2011
|
+
applyProps(node, { [name]: maybeCoerced });
|
|
2012
|
+
this.updateNativeProps(node, name, maybeCoerced);
|
|
2013
|
+
}
|
|
2014
|
+
applyProperty(node, name, value) {
|
|
2015
|
+
const rS = node.__ngt_renderer__;
|
|
2016
|
+
if (rS[NgtRendererClassId.destroyed])
|
|
2017
|
+
return;
|
|
2018
|
+
// [ref]
|
|
2019
|
+
if (name === SPECIAL_PROPERTIES.REF && is.ref(value)) {
|
|
2020
|
+
rS[NgtRendererClassId.ref] = value;
|
|
2021
|
+
value.nativeElement = node;
|
|
2022
|
+
return;
|
|
2023
|
+
}
|
|
2024
|
+
const localState = getLocalState(node);
|
|
2025
|
+
const parent = localState?.instanceStore.get('parent') || rS[NgtRendererClassId.parent];
|
|
2026
|
+
// [rawValue]
|
|
2027
|
+
if (localState?.isRaw && name === SPECIAL_PROPERTIES.RAW_VALUE) {
|
|
2028
|
+
rS[NgtRendererClassId.rawValue] = value;
|
|
2029
|
+
if (parent)
|
|
2030
|
+
attachThreeChild(parent, node);
|
|
2031
|
+
return;
|
|
2032
|
+
}
|
|
2033
|
+
// [attach]
|
|
2034
|
+
if (name === SPECIAL_PROPERTIES.ATTACH) {
|
|
2035
|
+
if (localState)
|
|
2036
|
+
localState.attach = Array.isArray(value) ? value.map((v) => v.toString()) : value;
|
|
2037
|
+
if (parent)
|
|
2038
|
+
attachThreeChild(parent, node);
|
|
2039
|
+
return;
|
|
2040
|
+
}
|
|
2041
|
+
const compound = rS[NgtRendererClassId.compound];
|
|
2042
|
+
if (compound?.[NgtCompoundClassId.props] &&
|
|
2043
|
+
name in compound[NgtCompoundClassId.props] &&
|
|
2044
|
+
!compound[NgtCompoundClassId.applyFirst]) {
|
|
2045
|
+
value = compound[NgtCompoundClassId.props][name];
|
|
2046
|
+
}
|
|
2047
|
+
applyProps(node, { [name]: value });
|
|
2048
|
+
// NOTE: special case for Geometry and Material
|
|
2049
|
+
if (value && (name === 'geometry' || name === 'material')) {
|
|
2050
|
+
const childLS = getLocalState(value);
|
|
2051
|
+
childLS?.setParent(node);
|
|
2052
|
+
localState?.add(value, 'nonObjects');
|
|
2053
|
+
}
|
|
2054
|
+
this.updateNativeProps(node, name, value);
|
|
2055
|
+
}
|
|
2056
|
+
get rootScene() {
|
|
2057
|
+
return this.rootState.store.get('scene');
|
|
2058
|
+
}
|
|
2059
|
+
destroy(node, parent) {
|
|
2060
|
+
const rS = node.__ngt_renderer__;
|
|
2061
|
+
if (!rS || rS[NgtRendererClassId.destroyed])
|
|
2062
|
+
return;
|
|
2063
|
+
if (rS[NgtRendererClassId.type] === 'three') {
|
|
2064
|
+
rS[NgtRendererClassId.compound] = undefined;
|
|
2065
|
+
rS[NgtRendererClassId.compoundParent] = undefined;
|
|
2066
|
+
const localState = getLocalState(node);
|
|
2067
|
+
if (localState?.instanceStore) {
|
|
2068
|
+
localState.instanceStore.get('objects').forEach((obj) => this.destroy(obj, parent));
|
|
2069
|
+
localState.instanceStore.get('nonObjects').forEach((obj) => this.destroy(obj, parent));
|
|
2070
|
+
}
|
|
2071
|
+
if (localState?.afterUpdate)
|
|
2072
|
+
localState.afterUpdate.complete();
|
|
2073
|
+
if (localState?.afterAttach)
|
|
2074
|
+
localState.afterAttach.complete();
|
|
2075
|
+
delete localState['objects'];
|
|
2076
|
+
delete localState['nonObjects'];
|
|
2077
|
+
delete localState['parent'];
|
|
2078
|
+
delete localState['nativeProps'];
|
|
2079
|
+
delete localState['add'];
|
|
2080
|
+
delete localState['remove'];
|
|
2081
|
+
delete localState['afterUpdate'];
|
|
2082
|
+
delete localState['afterAttach'];
|
|
2083
|
+
delete localState['store'];
|
|
2084
|
+
delete localState['handlers'];
|
|
2085
|
+
if (!localState?.primitive) {
|
|
2086
|
+
delete node['__ngt__'];
|
|
2087
|
+
}
|
|
2088
|
+
}
|
|
2089
|
+
if (rS[NgtRendererClassId.type] === 'comment') {
|
|
2090
|
+
rS[NgtRendererClassId.injectorFactory] = null;
|
|
2091
|
+
delete node[SPECIAL_INTERNAL_ADD_COMMENT];
|
|
2092
|
+
if (!this.removeCommentNode(node, this.argsCommentNodes)) {
|
|
2093
|
+
this.removeCommentNode(node, this.parentCommentNodes);
|
|
2094
|
+
}
|
|
2095
|
+
}
|
|
2096
|
+
if (rS[NgtRendererClassId.type] === 'portal') {
|
|
2097
|
+
rS[NgtRendererClassId.injectorFactory] = null;
|
|
2098
|
+
this.removeCommentNode(node, this.portalCommentsNodes);
|
|
2099
|
+
}
|
|
2100
|
+
if (rS[NgtRendererClassId.type] === 'compound') {
|
|
2101
|
+
rS[NgtRendererClassId.compounded] = undefined;
|
|
2102
|
+
rS[NgtRendererClassId.attributes] = null;
|
|
2103
|
+
rS[NgtRendererClassId.properties] = null;
|
|
2104
|
+
this.executeOperation(node, 'cleanUp');
|
|
2105
|
+
rS[NgtRendererClassId.queueOps].clear();
|
|
2106
|
+
rS[NgtRendererClassId.queueOps] = null;
|
|
2107
|
+
}
|
|
2108
|
+
if (rS[NgtRendererClassId.ref]) {
|
|
2109
|
+
// nullify ref
|
|
2110
|
+
// but we do it later so that it doesn't hinder render
|
|
2111
|
+
// TODO: will this cause memory leak?
|
|
2112
|
+
requestAnimationFrame(() => {
|
|
2113
|
+
rS[NgtRendererClassId.ref].nativeElement = null;
|
|
2114
|
+
rS[NgtRendererClassId.ref] = undefined;
|
|
2115
|
+
});
|
|
2116
|
+
}
|
|
2117
|
+
// nullify parent
|
|
2118
|
+
rS[NgtRendererClassId.parent] = null;
|
|
2119
|
+
for (const renderChild of rS[NgtRendererClassId.children] || []) {
|
|
2120
|
+
if (renderChild.__ngt_renderer__?.[NgtRendererClassId.type] === 'three' && parent) {
|
|
2121
|
+
const closestInstance = this.getClosestParentWithInstance(parent);
|
|
2122
|
+
if (closestInstance) {
|
|
2123
|
+
removeThreeChild(closestInstance, renderChild, true);
|
|
2124
|
+
}
|
|
2125
|
+
}
|
|
2126
|
+
this.destroy(renderChild, parent);
|
|
2127
|
+
}
|
|
2128
|
+
rS[NgtRendererClassId.children] = [];
|
|
2129
|
+
rS[NgtRendererClassId.destroyed] = true;
|
|
2130
|
+
if (parent) {
|
|
2131
|
+
this.removeChild(parent, node);
|
|
2132
|
+
}
|
|
2133
|
+
}
|
|
2134
|
+
removeCommentNode(node, nodes) {
|
|
2135
|
+
const index = nodes.findIndex((comment) => comment === node);
|
|
2136
|
+
if (index > -1) {
|
|
2137
|
+
nodes.splice(index, 1);
|
|
2138
|
+
return true;
|
|
2139
|
+
}
|
|
2140
|
+
return false;
|
|
2141
|
+
}
|
|
2142
|
+
updateNativeProps(node, key, value) {
|
|
2143
|
+
const localState = getLocalState(node);
|
|
2144
|
+
localState?.setNativeProps(key, value);
|
|
2145
|
+
}
|
|
2146
|
+
firstNonInjectedDirective(listProperty, dir) {
|
|
2147
|
+
let directive;
|
|
2148
|
+
const destroyed = [];
|
|
2149
|
+
let i = this[listProperty].length - 1;
|
|
2150
|
+
while (i >= 0) {
|
|
2151
|
+
const comment = this[listProperty][i];
|
|
2152
|
+
if (comment.__ngt_renderer__[NgtRendererClassId.destroyed]) {
|
|
2153
|
+
destroyed.push(i);
|
|
2154
|
+
i--;
|
|
2155
|
+
continue;
|
|
2156
|
+
}
|
|
2157
|
+
const injector = comment.__ngt_renderer__[NgtRendererClassId.injectorFactory]();
|
|
2158
|
+
if (!injector) {
|
|
2159
|
+
i--;
|
|
2160
|
+
continue;
|
|
2161
|
+
}
|
|
2162
|
+
const instance = injector.get(dir, null);
|
|
2163
|
+
if (instance && instance.validate()) {
|
|
2164
|
+
directive = instance;
|
|
2165
|
+
break;
|
|
2166
|
+
}
|
|
2167
|
+
i--;
|
|
2168
|
+
}
|
|
2169
|
+
destroyed.forEach((index) => {
|
|
2170
|
+
this[listProperty].splice(index, 1);
|
|
2171
|
+
});
|
|
2172
|
+
return directive;
|
|
2173
|
+
}
|
|
2174
|
+
tryGetPortalStore() {
|
|
2175
|
+
let store;
|
|
2176
|
+
const destroyed = [];
|
|
2177
|
+
// we only care about the portal states because NgtStore only differs per Portal
|
|
2178
|
+
let i = this.portalCommentsNodes.length - 1;
|
|
2179
|
+
while (i >= 0) {
|
|
2180
|
+
// loop through the portal state backwards to find the closest NgtStore
|
|
2181
|
+
const portal = this.portalCommentsNodes[i];
|
|
2182
|
+
if (portal.__ngt_renderer__[NgtRendererClassId.destroyed]) {
|
|
2183
|
+
destroyed.push(i);
|
|
2184
|
+
i--;
|
|
2185
|
+
continue;
|
|
2186
|
+
}
|
|
2187
|
+
const injector = portal.__ngt_renderer__[NgtRendererClassId.injectorFactory]();
|
|
2188
|
+
if (!injector) {
|
|
2189
|
+
i--;
|
|
2190
|
+
continue;
|
|
2191
|
+
}
|
|
2192
|
+
const instance = injector.get(NGT_STORE, null);
|
|
2193
|
+
// only the instance with previousRoot should pass
|
|
2194
|
+
if (instance && instance.get('previousRoot')) {
|
|
2195
|
+
store = instance;
|
|
2196
|
+
break;
|
|
2197
|
+
}
|
|
2198
|
+
i--;
|
|
2199
|
+
}
|
|
2200
|
+
destroyed.forEach((index) => {
|
|
2201
|
+
this.portalCommentsNodes.splice(index, 1);
|
|
2202
|
+
});
|
|
2203
|
+
return store || this.rootState.store;
|
|
2204
|
+
}
|
|
2205
|
+
}
|
|
2206
|
+
|
|
2207
|
+
class NgtRendererFactory {
|
|
2208
|
+
constructor() {
|
|
2209
|
+
this.delegateRendererFactory = inject(RendererFactory2, { skipSelf: true });
|
|
2210
|
+
this.catalogue = injectNgtCatalogue();
|
|
2211
|
+
this.rendererMap = new Map();
|
|
2212
|
+
this.routedSet = new Set();
|
|
2213
|
+
// NOTE: all Renderer instances under the same NgtCanvas share the same Store
|
|
2214
|
+
this.rendererStore = new NgtRendererStore({
|
|
2215
|
+
store: injectNgtStore(),
|
|
2216
|
+
compoundPrefixes: inject(NGT_COMPOUND_PREFIXES),
|
|
2217
|
+
document: inject(DOCUMENT),
|
|
2218
|
+
});
|
|
2219
|
+
}
|
|
2220
|
+
createRenderer(hostElement, type) {
|
|
2221
|
+
const delegateRenderer = this.delegateRendererFactory.createRenderer(hostElement, type);
|
|
2222
|
+
if (!type)
|
|
2223
|
+
return delegateRenderer;
|
|
2224
|
+
// NOTE: might need to revisit this
|
|
2225
|
+
if (type['type'][HTML]) {
|
|
2226
|
+
this.rendererMap.set(type.id, delegateRenderer);
|
|
2227
|
+
return delegateRenderer;
|
|
2228
|
+
}
|
|
2229
|
+
if (type['type'][ROUTED_SCENE]) {
|
|
2230
|
+
this.routedSet.add(type.id);
|
|
2231
|
+
}
|
|
2232
|
+
let renderer = this.rendererMap.get(type.id);
|
|
2233
|
+
if (!renderer) {
|
|
2234
|
+
this.rendererMap.set(type.id, (renderer = new NgtRenderer(delegateRenderer, this.rendererStore, this.catalogue,
|
|
2235
|
+
// setting root scene if there's no routed scene OR this component is the routed Scene
|
|
2236
|
+
!hostElement && (this.routedSet.size === 0 || this.routedSet.has(type.id)))));
|
|
2237
|
+
}
|
|
2238
|
+
return renderer;
|
|
2239
|
+
}
|
|
2240
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.0.0", ngImport: i0, type: NgtRendererFactory, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
2241
|
+
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.0.0", ngImport: i0, type: NgtRendererFactory }); }
|
|
2242
|
+
}
|
|
2243
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.0", ngImport: i0, type: NgtRendererFactory, decorators: [{
|
|
2244
|
+
type: Injectable
|
|
2245
|
+
}] });
|
|
2246
|
+
class NgtRenderer {
|
|
2247
|
+
constructor(delegate, store, catalogue, isRoot = true) {
|
|
2248
|
+
this.delegate = delegate;
|
|
2249
|
+
this.store = store;
|
|
2250
|
+
this.catalogue = catalogue;
|
|
2251
|
+
this.isRoot = isRoot;
|
|
2252
|
+
this.createText = this.delegate.createText.bind(this.delegate);
|
|
2253
|
+
this.destroy = this.delegate.destroy.bind(this.delegate);
|
|
2254
|
+
this.destroyNode = null;
|
|
2255
|
+
this.selectRootElement = this.delegate.selectRootElement.bind(this.delegate);
|
|
2256
|
+
this.nextSibling = this.delegate.nextSibling.bind(this.delegate);
|
|
2257
|
+
this.addClass = this.delegate.addClass.bind(this.delegate);
|
|
2258
|
+
this.removeClass = this.delegate.removeClass.bind(this.delegate);
|
|
2259
|
+
this.setStyle = this.delegate.setStyle.bind(this.delegate);
|
|
2260
|
+
this.removeStyle = this.delegate.removeStyle.bind(this.delegate);
|
|
2261
|
+
this.setValue = this.delegate.setValue.bind(this.delegate);
|
|
2262
|
+
}
|
|
2263
|
+
createElement(name, namespace) {
|
|
2264
|
+
const element = this.delegate.createElement(name, namespace);
|
|
2265
|
+
// on first pass, we return the Root Scene as the root node
|
|
2266
|
+
if (this.isRoot) {
|
|
2267
|
+
this.isRoot = false;
|
|
2268
|
+
const node = this.store.createNode('three', this.store.rootScene);
|
|
2269
|
+
node.__ngt_renderer__[NgtRendererClassId.injectorFactory] = () => getDebugNode(element)?.injector;
|
|
2270
|
+
return node;
|
|
2271
|
+
}
|
|
2272
|
+
if (this.store.isCompound(name)) {
|
|
2273
|
+
return this.store.createNode('compound', element);
|
|
2274
|
+
}
|
|
2275
|
+
if (name === SPECIAL_DOM_TAG.NGT_PORTAL) {
|
|
2276
|
+
return this.store.createNode('portal', element);
|
|
2277
|
+
}
|
|
2278
|
+
if (name === SPECIAL_DOM_TAG.NGT_VALUE) {
|
|
2279
|
+
const instanceStore = signalStore({
|
|
2280
|
+
nativeProps: {},
|
|
2281
|
+
parent: null,
|
|
2282
|
+
objects: [],
|
|
2283
|
+
nonObjects: [],
|
|
2284
|
+
});
|
|
2285
|
+
return this.store.createNode('three', Object.assign({ __ngt_renderer__: { rawValue: undefined } },
|
|
2286
|
+
// NOTE: we assign this manually to a raw value node
|
|
2287
|
+
// because we say it is a 'three' node but we're not using prepare()
|
|
2288
|
+
{
|
|
2289
|
+
__ngt__: {
|
|
2290
|
+
isRaw: true,
|
|
2291
|
+
instanceStore,
|
|
2292
|
+
setNativeProps(key, value) {
|
|
2293
|
+
instanceStore.update((prev) => ({ nativeProps: { ...prev.nativeProps, [key]: value } }));
|
|
2294
|
+
},
|
|
2295
|
+
setParent(parent) {
|
|
2296
|
+
instanceStore.update({ parent });
|
|
2297
|
+
},
|
|
2298
|
+
},
|
|
2299
|
+
}));
|
|
2300
|
+
}
|
|
2301
|
+
const [injectedArgs, injectedParent, store] = this.store.getCreationState();
|
|
2302
|
+
let parent = injectedParent;
|
|
2303
|
+
if (typeof injectedParent === 'string') {
|
|
2304
|
+
parent = store
|
|
2305
|
+
.get('scene')
|
|
2306
|
+
.getObjectByName(injectedParent);
|
|
2307
|
+
}
|
|
2308
|
+
if (name === SPECIAL_DOM_TAG.NGT_PRIMITIVE) {
|
|
2309
|
+
if (!injectedArgs[0])
|
|
2310
|
+
throw new Error(`[NGT] ngt-primitive without args is invalid`);
|
|
2311
|
+
const object = injectedArgs[0];
|
|
2312
|
+
let localState = getLocalState(object);
|
|
2313
|
+
if (!localState) {
|
|
2314
|
+
// NOTE: if an object isn't already "prepared", we prepare it
|
|
2315
|
+
localState = getLocalState(prepare(object, { store, args: injectedArgs, primitive: true }));
|
|
2316
|
+
}
|
|
2317
|
+
if (!localState.store)
|
|
2318
|
+
localState.store = store;
|
|
2319
|
+
const node = this.store.createNode('three', object);
|
|
2320
|
+
if (parent) {
|
|
2321
|
+
node.__ngt_renderer__[NgtRendererClassId.injectedParent] = parent;
|
|
2322
|
+
}
|
|
2323
|
+
return node;
|
|
2324
|
+
}
|
|
2325
|
+
const threeName = kebabToPascal(name.startsWith('ngt') ? name.slice(4) : name);
|
|
2326
|
+
const threeTarget = this.catalogue[threeName];
|
|
2327
|
+
// we have the THREE constructor here, handle it
|
|
2328
|
+
if (threeTarget) {
|
|
2329
|
+
const instance = prepare(new threeTarget(...injectedArgs), { store, args: injectedArgs });
|
|
2330
|
+
const node = this.store.createNode('three', instance);
|
|
2331
|
+
const localState = getLocalState(instance);
|
|
2332
|
+
// auto-attach for geometry and material
|
|
2333
|
+
if (is.geometry(instance)) {
|
|
2334
|
+
localState.attach = ['geometry'];
|
|
2335
|
+
}
|
|
2336
|
+
else if (is.material(instance)) {
|
|
2337
|
+
localState.attach = ['material'];
|
|
2338
|
+
}
|
|
2339
|
+
if (parent) {
|
|
2340
|
+
node.__ngt_renderer__[NgtRendererClassId.injectedParent] = parent;
|
|
2341
|
+
}
|
|
2342
|
+
return node;
|
|
2343
|
+
}
|
|
2344
|
+
return this.store.createNode('dom', element);
|
|
2345
|
+
}
|
|
2346
|
+
createComment(value) {
|
|
2347
|
+
return this.store.createNode('comment', this.delegate.createComment(value));
|
|
2348
|
+
}
|
|
2349
|
+
appendChild(parent, newChild) {
|
|
2350
|
+
const pRS = parent.__ngt_renderer__;
|
|
2351
|
+
const cRS = newChild.__ngt_renderer__;
|
|
2352
|
+
if (pRS[NgtRendererClassId.type] === 'dom' &&
|
|
2353
|
+
(newChild instanceof Text || cRS[NgtRendererClassId.type] === 'dom')) {
|
|
2354
|
+
this.store.addChild(parent, newChild);
|
|
2355
|
+
this.delegate.appendChild(parent, newChild);
|
|
2356
|
+
if (cRS) {
|
|
2357
|
+
this.store.setParent(newChild, parent);
|
|
2358
|
+
if (this.shouldFindGrandparentInstance(pRS, cRS, newChild)) {
|
|
2359
|
+
// we'll try to get the grandparent instance here so that we can run appendChild with both instances
|
|
2360
|
+
const closestGrandparentInstance = this.store.getClosestParentWithInstance(parent);
|
|
2361
|
+
if (closestGrandparentInstance)
|
|
2362
|
+
this.appendChild(closestGrandparentInstance, newChild);
|
|
2363
|
+
}
|
|
2364
|
+
}
|
|
2365
|
+
return;
|
|
2366
|
+
}
|
|
2367
|
+
if (cRS?.[NgtRendererClassId.type] === 'comment') {
|
|
2368
|
+
this.store.setParent(newChild, parent);
|
|
2369
|
+
return;
|
|
2370
|
+
}
|
|
2371
|
+
if (cRS?.[NgtRendererClassId.injectedParent]) {
|
|
2372
|
+
if (is.ref(cRS[NgtRendererClassId.injectedParent])) {
|
|
2373
|
+
const injector = cRS[NgtRendererClassId.injectorFactory]()?.get(Injector, null);
|
|
2374
|
+
if (!injector) {
|
|
2375
|
+
console.warn(`[NGT] NgtRenderer is attempting to start an effect for injectedParent but no Injector is found.`);
|
|
2376
|
+
return;
|
|
2377
|
+
}
|
|
2378
|
+
const watcher = effect(() => {
|
|
2379
|
+
const injectedParent = cRS[NgtRendererClassId.injectedParent]
|
|
2380
|
+
.nativeElement;
|
|
2381
|
+
if (injectedParent && injectedParent !== parent) {
|
|
2382
|
+
this.appendChild(injectedParent, newChild);
|
|
2383
|
+
// only run this effect once
|
|
2384
|
+
// as soon as we re-run appendChild with the injectedParent, we stop the effect
|
|
2385
|
+
watcher.destroy();
|
|
2386
|
+
}
|
|
2387
|
+
}, { injector, manualCleanup: true });
|
|
2388
|
+
return;
|
|
2389
|
+
}
|
|
2390
|
+
else if (parent !== cRS[NgtRendererClassId.injectedParent]) {
|
|
2391
|
+
this.appendChild(cRS[NgtRendererClassId.injectedParent], newChild);
|
|
2392
|
+
return;
|
|
2393
|
+
}
|
|
2394
|
+
}
|
|
2395
|
+
this.store.setParent(newChild, parent);
|
|
2396
|
+
this.store.addChild(parent, newChild);
|
|
2397
|
+
// if new child is a portal
|
|
2398
|
+
if (cRS?.[NgtRendererClassId.type] === 'portal') {
|
|
2399
|
+
this.store.processPortalContainer(newChild);
|
|
2400
|
+
if (cRS[NgtRendererClassId.portalContainer]) {
|
|
2401
|
+
this.appendChild(parent, cRS[NgtRendererClassId.portalContainer]);
|
|
2402
|
+
}
|
|
2403
|
+
return;
|
|
2404
|
+
}
|
|
2405
|
+
// if parent is a portal
|
|
2406
|
+
if (pRS[NgtRendererClassId.type] === 'portal') {
|
|
2407
|
+
this.store.processPortalContainer(parent);
|
|
2408
|
+
if (pRS[NgtRendererClassId.portalContainer]) {
|
|
2409
|
+
this.appendChild(pRS[NgtRendererClassId.portalContainer], newChild);
|
|
2410
|
+
}
|
|
2411
|
+
return;
|
|
2412
|
+
}
|
|
2413
|
+
// if both are three instances, straightforward case
|
|
2414
|
+
if (pRS[NgtRendererClassId.type] === 'three' && cRS?.[NgtRendererClassId.type] === 'three') {
|
|
2415
|
+
const cLS = getLocalState(newChild);
|
|
2416
|
+
// if child already attached to a parent, skip
|
|
2417
|
+
if (cLS?.instanceStore?.get('parent'))
|
|
2418
|
+
return;
|
|
2419
|
+
// attach THREE child
|
|
2420
|
+
attachThreeChild(parent, newChild);
|
|
2421
|
+
// here, we handle the special case of if the parent has a compoundParent, which means this child is part of a compound parent template
|
|
2422
|
+
if (!cRS[NgtRendererClassId.compound])
|
|
2423
|
+
return;
|
|
2424
|
+
const closestGrandparentWithCompound = this.store.getClosestParentWithCompound(parent);
|
|
2425
|
+
if (!closestGrandparentWithCompound)
|
|
2426
|
+
return;
|
|
2427
|
+
this.appendChild(closestGrandparentWithCompound, newChild);
|
|
2428
|
+
return;
|
|
2429
|
+
}
|
|
2430
|
+
// if only the parent is the THREE instance
|
|
2431
|
+
if (pRS[NgtRendererClassId.type] === 'three') {
|
|
2432
|
+
for (const renderChild of cRS?.[NgtRendererClassId.children] || []) {
|
|
2433
|
+
this.appendChild(parent, renderChild);
|
|
2434
|
+
}
|
|
2435
|
+
}
|
|
2436
|
+
// if parent is a compound
|
|
2437
|
+
if (pRS[NgtRendererClassId.type] === 'compound') {
|
|
2438
|
+
// if compound doesn't have a THREE instance set yet
|
|
2439
|
+
if (!pRS[NgtRendererClassId.compounded] && cRS[NgtRendererClassId.type] === 'three') {
|
|
2440
|
+
// if child is indeed an ngtCompound
|
|
2441
|
+
if (cRS[NgtRendererClassId.compound])
|
|
2442
|
+
this.store.setCompound(parent, newChild);
|
|
2443
|
+
// if not, we track the parent (that is supposedly the compound component) on this three instance
|
|
2444
|
+
else if (!cRS[NgtRendererClassId.compoundParent])
|
|
2445
|
+
cRS[NgtRendererClassId.compoundParent] = parent;
|
|
2446
|
+
}
|
|
2447
|
+
// reset the compound if it's changed
|
|
2448
|
+
if (pRS[NgtRendererClassId.compounded] &&
|
|
2449
|
+
cRS[NgtRendererClassId.type] === 'three' &&
|
|
2450
|
+
cRS[NgtRendererClassId.compound] &&
|
|
2451
|
+
pRS[NgtRendererClassId.compounded] !== newChild) {
|
|
2452
|
+
this.store.setCompound(parent, newChild);
|
|
2453
|
+
}
|
|
2454
|
+
}
|
|
2455
|
+
if (this.shouldFindGrandparentInstance(pRS, cRS, newChild)) {
|
|
2456
|
+
// we'll try to get the grandparent instance here so that we can run appendChild with both instances
|
|
2457
|
+
const closestGrandparentInstance = this.store.getClosestParentWithInstance(parent);
|
|
2458
|
+
if (closestGrandparentInstance)
|
|
2459
|
+
this.appendChild(closestGrandparentInstance, newChild);
|
|
2460
|
+
return;
|
|
2461
|
+
}
|
|
2462
|
+
}
|
|
2463
|
+
insertBefore(parent, newChild) {
|
|
2464
|
+
if (parent == null || !parent.__ngt_renderer__ || parent === newChild)
|
|
2465
|
+
return;
|
|
2466
|
+
this.appendChild(parent, newChild);
|
|
2467
|
+
}
|
|
2468
|
+
removeChild(parent, oldChild, isHostElement) {
|
|
2469
|
+
const pRS = parent.__ngt_renderer__;
|
|
2470
|
+
const cRS = oldChild.__ngt_renderer__;
|
|
2471
|
+
if ((!cRS || !pRS) &&
|
|
2472
|
+
parent instanceof Element &&
|
|
2473
|
+
(oldChild instanceof Element || oldChild instanceof Text || oldChild instanceof Comment)) {
|
|
2474
|
+
this.delegate.removeChild(parent, oldChild);
|
|
2475
|
+
this.store.destroy(oldChild, parent);
|
|
2476
|
+
return;
|
|
2477
|
+
}
|
|
2478
|
+
if (cRS[NgtRendererClassId.type] === 'dom' && (!pRS || pRS[NgtRendererClassId.type] === 'dom')) {
|
|
2479
|
+
this.delegate.removeChild(parent, oldChild);
|
|
2480
|
+
this.store.destroy(oldChild, parent);
|
|
2481
|
+
return;
|
|
2482
|
+
}
|
|
2483
|
+
if (pRS[NgtRendererClassId.type] === 'three' && cRS[NgtRendererClassId.type] === 'three') {
|
|
2484
|
+
removeThreeChild(parent, oldChild, true);
|
|
2485
|
+
this.store.destroy(oldChild, parent);
|
|
2486
|
+
return;
|
|
2487
|
+
}
|
|
2488
|
+
if (pRS[NgtRendererClassId.type] === 'compound' && pRS[NgtRendererClassId.parent]) {
|
|
2489
|
+
this.removeChild(pRS[NgtRendererClassId.parent], oldChild, isHostElement);
|
|
2490
|
+
return;
|
|
2491
|
+
}
|
|
2492
|
+
if (pRS[NgtRendererClassId.type] === 'three') {
|
|
2493
|
+
this.store.destroy(oldChild, parent);
|
|
2494
|
+
return;
|
|
2495
|
+
}
|
|
2496
|
+
const closestGrandparentInstance = this.store.getClosestParentWithInstance(parent);
|
|
2497
|
+
if (closestGrandparentInstance)
|
|
2498
|
+
this.removeChild(closestGrandparentInstance, oldChild, isHostElement);
|
|
2499
|
+
this.store.destroy(oldChild, closestGrandparentInstance);
|
|
2500
|
+
}
|
|
2501
|
+
parentNode(node) {
|
|
2502
|
+
const rS = node.__ngt_renderer__;
|
|
2503
|
+
if (rS?.[NgtRendererClassId.parent])
|
|
2504
|
+
return rS[NgtRendererClassId.parent];
|
|
2505
|
+
return this.delegate.parentNode(node);
|
|
2506
|
+
}
|
|
2507
|
+
setAttributeInternal(el, name, value, namespace) {
|
|
2508
|
+
const rS = el.__ngt_renderer__;
|
|
2509
|
+
if (rS[NgtRendererClassId.type] === 'compound') {
|
|
2510
|
+
// we don't have the compound instance yet
|
|
2511
|
+
rS[NgtRendererClassId.attributes][name] = value;
|
|
2512
|
+
if (!rS[NgtRendererClassId.compounded]) {
|
|
2513
|
+
this.store.queueOperation(el, ['op', () => this.setAttribute(el, name, value, namespace)]);
|
|
2514
|
+
return false;
|
|
2515
|
+
}
|
|
2516
|
+
return this.setAttributeInternal(rS[NgtRendererClassId.compounded], name, value, namespace);
|
|
2517
|
+
}
|
|
2518
|
+
if (rS[NgtRendererClassId.type] === 'three') {
|
|
2519
|
+
this.store.applyAttribute(el, name, value);
|
|
2520
|
+
return false;
|
|
2521
|
+
}
|
|
2522
|
+
return true;
|
|
2523
|
+
}
|
|
2524
|
+
setAttribute(el, name, value, namespace) {
|
|
2525
|
+
const useDelegate = this.setAttributeInternal(el, name, value, namespace);
|
|
2526
|
+
if (useDelegate) {
|
|
2527
|
+
this.delegate.setAttribute(el, name, value);
|
|
2528
|
+
}
|
|
2529
|
+
}
|
|
2530
|
+
removeAttribute(el, name, namespace) {
|
|
2531
|
+
const useDelegate = this.setAttributeInternal(el, name, undefined, namespace);
|
|
2532
|
+
if (useDelegate) {
|
|
2533
|
+
this.delegate.removeAttribute(el, name, namespace);
|
|
2534
|
+
}
|
|
2535
|
+
}
|
|
2536
|
+
setProperty(el, name, value) {
|
|
2537
|
+
// TODO: should we support ref value
|
|
2538
|
+
const rS = el.__ngt_renderer__;
|
|
2539
|
+
if (rS[NgtRendererClassId.type] === 'compound') {
|
|
2540
|
+
// we don't have the compound instance yet
|
|
2541
|
+
rS[NgtRendererClassId.properties][name] = value;
|
|
2542
|
+
if (!rS[NgtRendererClassId.compounded]) {
|
|
2543
|
+
this.store.queueOperation(el, ['op', () => this.setProperty(el, name, value)]);
|
|
2544
|
+
return;
|
|
2545
|
+
}
|
|
2546
|
+
if (rS[NgtRendererClassId.compounded].__ngt_renderer__[NgtRendererClassId.compound]) {
|
|
2547
|
+
Object.assign(rS[NgtRendererClassId.compounded].__ngt_renderer__[NgtRendererClassId.compound][NgtCompoundClassId.props], { [name]: value });
|
|
2548
|
+
}
|
|
2549
|
+
this.setProperty(rS[NgtRendererClassId.compounded], name, value);
|
|
2550
|
+
return;
|
|
2551
|
+
}
|
|
2552
|
+
if (rS[NgtRendererClassId.type] === 'three') {
|
|
2553
|
+
this.store.applyProperty(el, name, value);
|
|
2554
|
+
return;
|
|
2555
|
+
}
|
|
2556
|
+
return this.delegate.setProperty(el, name, value);
|
|
2557
|
+
}
|
|
2558
|
+
listen(target, eventName, callback) {
|
|
2559
|
+
const rS = target.__ngt_renderer__;
|
|
2560
|
+
// if the target doesn't have __ngt_renderer__, we delegate
|
|
2561
|
+
// if target is DOM node, we delegate
|
|
2562
|
+
if (!rS || this.store.isDOM(target)) {
|
|
2563
|
+
return this.delegate.listen(target, eventName, callback);
|
|
2564
|
+
}
|
|
2565
|
+
if (rS[NgtRendererClassId.type] === 'three' ||
|
|
2566
|
+
(rS[NgtRendererClassId.type] === 'compound' && rS[NgtRendererClassId.compounded])) {
|
|
2567
|
+
const instance = rS[NgtRendererClassId.compounded] || target;
|
|
2568
|
+
const localState = getLocalState(instance);
|
|
2569
|
+
const priority = localState?.priority ?? 0;
|
|
2570
|
+
return processThreeEvent(instance, priority || 0, eventName, callback);
|
|
2571
|
+
}
|
|
2572
|
+
if (rS[NgtRendererClassId.type] === 'compound' && !rS[NgtRendererClassId.compounded]) {
|
|
2573
|
+
this.store.queueOperation(target, [
|
|
2574
|
+
'op',
|
|
2575
|
+
() => this.store.queueOperation(target, ['cleanUp', this.listen(target, eventName, callback)]),
|
|
2576
|
+
]);
|
|
2577
|
+
return () => { };
|
|
2578
|
+
}
|
|
2579
|
+
// @ts-expect-error - we know that target is not DOM node
|
|
2580
|
+
if (target === this.store.rootScene) {
|
|
2581
|
+
let [domTarget, event] = eventName.split(':');
|
|
2582
|
+
if (event == null) {
|
|
2583
|
+
event = domTarget;
|
|
2584
|
+
domTarget = '';
|
|
2585
|
+
}
|
|
2586
|
+
const eventTarget = domTarget === 'window'
|
|
2587
|
+
? target['ownerDocument']['defaultView']
|
|
2588
|
+
: target['ownerDocument'];
|
|
2589
|
+
return this.delegate.listen(eventTarget, event, callback);
|
|
2590
|
+
}
|
|
2591
|
+
return () => { };
|
|
2592
|
+
}
|
|
2593
|
+
shouldFindGrandparentInstance(pRS, cRS, child) {
|
|
2594
|
+
const pType = pRS[NgtRendererClassId.type];
|
|
2595
|
+
const cType = cRS[NgtRendererClassId.type];
|
|
2596
|
+
const isParentCompounded = pRS[NgtRendererClassId.compounded];
|
|
2597
|
+
const isChildCompounded = cRS[NgtRendererClassId.compounded];
|
|
2598
|
+
const cLS = getLocalState(child);
|
|
2599
|
+
// if child is three but haven't been attached to a parent yet
|
|
2600
|
+
const isDanglingThreeChild = cType === 'three' && !cLS?.instanceStore?.get('parent');
|
|
2601
|
+
// or both parent and child are DOM elements
|
|
2602
|
+
// or they are compound AND haven't had a THREE instance yet
|
|
2603
|
+
const isParentStillDOM = pType === 'dom' || (pType === 'compound' && !isParentCompounded);
|
|
2604
|
+
const isChildStillDOM = cType === 'dom' || (cType === 'compound' && !isChildCompounded);
|
|
2605
|
+
// and the child is a compounded compound
|
|
2606
|
+
const isCompoundChildCompounded = cType === 'compound' && !!isChildCompounded;
|
|
2607
|
+
return (isDanglingThreeChild || (isParentStillDOM && isChildStillDOM) || (isParentStillDOM && isCompoundChildCompounded));
|
|
2608
|
+
}
|
|
2609
|
+
get data() {
|
|
2610
|
+
return this.delegate.data;
|
|
2611
|
+
}
|
|
2612
|
+
}
|
|
2613
|
+
function provideNgtRenderer(store, compoundPrefixes) {
|
|
2614
|
+
if (!compoundPrefixes.includes('ngts'))
|
|
2615
|
+
compoundPrefixes.push('ngts');
|
|
2616
|
+
if (!compoundPrefixes.includes('ngtp'))
|
|
2617
|
+
compoundPrefixes.push('ngtp');
|
|
2618
|
+
return makeEnvironmentProviders([
|
|
2619
|
+
NgtRendererFactory,
|
|
2620
|
+
{ provide: RendererFactory2, useExisting: NgtRendererFactory },
|
|
2621
|
+
{ provide: NGT_COMPOUND_PREFIXES, useValue: compoundPrefixes },
|
|
2622
|
+
provideNgtStore(store),
|
|
2623
|
+
]);
|
|
2624
|
+
}
|
|
2625
|
+
|
|
2626
|
+
class NgtCanvas {
|
|
2627
|
+
constructor() {
|
|
2628
|
+
this.store = injectNgtStore();
|
|
2629
|
+
this.initRoot = injectCanvasRootInitializer();
|
|
2630
|
+
this.autoEffect = injectAutoEffect();
|
|
2631
|
+
this.host = inject(ElementRef);
|
|
2632
|
+
this.zone = inject(NgZone);
|
|
2633
|
+
this.environmentInjector = inject(EnvironmentInjector);
|
|
2634
|
+
this.injector = inject(Injector);
|
|
2635
|
+
this.sceneGraph = input.required();
|
|
2636
|
+
this.compoundPrefixes = input([]);
|
|
2637
|
+
this.sceneGraphInputs = input({});
|
|
2638
|
+
this.gl = input();
|
|
2639
|
+
this.size = input();
|
|
2640
|
+
this.shadows = input(false, {
|
|
2641
|
+
transform: (value) => {
|
|
2642
|
+
if (value === '') {
|
|
2643
|
+
return booleanAttribute(value);
|
|
2644
|
+
}
|
|
2645
|
+
return value;
|
|
2646
|
+
},
|
|
2647
|
+
});
|
|
2648
|
+
this.legacy = input(false, { transform: booleanAttribute });
|
|
2649
|
+
this.linear = input(false, { transform: booleanAttribute });
|
|
2650
|
+
this.flat = input(false, { transform: booleanAttribute });
|
|
2651
|
+
this.orthographic = input(false, { transform: booleanAttribute });
|
|
2652
|
+
this.frameloop = input('always');
|
|
2653
|
+
this.performance = input();
|
|
2654
|
+
this.dpr = input([1, 2]);
|
|
2655
|
+
this.raycaster = input();
|
|
2656
|
+
this.scene = input();
|
|
2657
|
+
this.camera = input();
|
|
2658
|
+
this.events = input(createPointerEvents);
|
|
2659
|
+
this.eventSource = input();
|
|
2660
|
+
this.eventPrefix = input('offset');
|
|
2661
|
+
this.lookAt = input();
|
|
2662
|
+
this.created = output();
|
|
2663
|
+
this.pointerMissed = outputFromObservable(this.store.get('pointerMissed$'));
|
|
2664
|
+
this.glCanvas = viewChild.required('glCanvas');
|
|
2665
|
+
this.glCanvasViewContainerRef = viewChild.required('glCanvas', { read: ViewContainerRef });
|
|
2666
|
+
// NOTE: this signal is updated outside of Zone
|
|
2667
|
+
this.resizeResult = signal({}, { equal: Object.is });
|
|
2668
|
+
this.hbPointerEvents = computed(() => (this.eventSource() ? 'none' : 'auto'));
|
|
2669
|
+
afterNextRender(() => {
|
|
2670
|
+
this.zone.runOutsideAngular(() => {
|
|
2671
|
+
this.configurator = this.initRoot(this.glCanvas().nativeElement);
|
|
2672
|
+
this.noZoneResizeEffect();
|
|
2673
|
+
this.noZoneSceneGraphInputsEffect();
|
|
2674
|
+
});
|
|
2675
|
+
});
|
|
2676
|
+
inject(DestroyRef).onDestroy(() => {
|
|
2677
|
+
this.glRef?.destroy();
|
|
2678
|
+
this.glEnvironmentInjector?.destroy();
|
|
2679
|
+
this.configurator?.destroy();
|
|
2680
|
+
});
|
|
2681
|
+
}
|
|
2682
|
+
noZoneResizeEffect() {
|
|
2683
|
+
this.autoEffect(() => {
|
|
2684
|
+
const resizeResult = this.resizeResult();
|
|
2685
|
+
if (resizeResult.width > 0 && resizeResult.height > 0) {
|
|
2686
|
+
if (!this.configurator)
|
|
2687
|
+
this.configurator = this.initRoot(this.glCanvas().nativeElement);
|
|
2688
|
+
this.configurator.configure({
|
|
2689
|
+
gl: this.gl(),
|
|
2690
|
+
shadows: this.shadows(),
|
|
2691
|
+
legacy: this.legacy(),
|
|
2692
|
+
linear: this.linear(),
|
|
2693
|
+
flat: this.flat(),
|
|
2694
|
+
orthographic: this.orthographic(),
|
|
2695
|
+
frameloop: this.frameloop(),
|
|
2696
|
+
performance: this.performance(),
|
|
2697
|
+
dpr: this.dpr(),
|
|
2698
|
+
raycaster: this.raycaster(),
|
|
2699
|
+
scene: this.scene(),
|
|
2700
|
+
camera: this.camera(),
|
|
2701
|
+
events: this.events(),
|
|
2702
|
+
eventSource: this.eventSource(),
|
|
2703
|
+
eventPrefix: this.eventPrefix(),
|
|
2704
|
+
lookAt: this.lookAt(),
|
|
2705
|
+
size: resizeResult,
|
|
2706
|
+
});
|
|
2707
|
+
untracked(() => {
|
|
2708
|
+
if (this.glRef) {
|
|
2709
|
+
this.glRef.changeDetectorRef.detectChanges();
|
|
2710
|
+
}
|
|
2711
|
+
else {
|
|
2712
|
+
this.noZoneRender();
|
|
2713
|
+
}
|
|
2714
|
+
});
|
|
2715
|
+
}
|
|
2716
|
+
});
|
|
2717
|
+
}
|
|
2718
|
+
noZoneRender() {
|
|
2719
|
+
// NOTE: destroy previous instances if existed
|
|
2720
|
+
this.glEnvironmentInjector?.destroy();
|
|
2721
|
+
this.glRef?.destroy();
|
|
2722
|
+
// NOTE: Flag the canvas active, rendering will now begin
|
|
2723
|
+
this.store.update((state) => ({ internal: { ...state.internal, active: true } }));
|
|
2724
|
+
const [state, eventSource, eventPrefix] = [
|
|
2725
|
+
this.store.snapshot,
|
|
2726
|
+
untracked(this.eventSource),
|
|
2727
|
+
untracked(this.eventPrefix),
|
|
2728
|
+
];
|
|
2729
|
+
// connect to event source
|
|
2730
|
+
state.events.connect?.(eventSource ? (is.ref(eventSource) ? eventSource.nativeElement : eventSource) : this.host.nativeElement);
|
|
2731
|
+
// setup compute for eventPrefix
|
|
2732
|
+
if (eventPrefix) {
|
|
2733
|
+
state.setEvents({
|
|
2734
|
+
compute: (event, store) => {
|
|
2735
|
+
const { pointer, raycaster, camera, size } = store.snapshot;
|
|
2736
|
+
const x = event[(eventPrefix + 'X')];
|
|
2737
|
+
const y = event[(eventPrefix + 'Y')];
|
|
2738
|
+
pointer.set((x / size.width) * 2 - 1, -(y / size.height) * 2 + 1);
|
|
2739
|
+
raycaster.setFromCamera(pointer, camera);
|
|
2740
|
+
},
|
|
2741
|
+
});
|
|
2742
|
+
}
|
|
2743
|
+
// emit created event if observed
|
|
2744
|
+
this.created.emit(this.store.snapshot);
|
|
2745
|
+
if (!this.store.get('events', 'connected')) {
|
|
2746
|
+
this.store.get('events').connect?.(this.glCanvas().nativeElement);
|
|
2747
|
+
}
|
|
2748
|
+
this.glEnvironmentInjector = createEnvironmentInjector([provideNgtRenderer(this.store, untracked(this.compoundPrefixes))], this.environmentInjector);
|
|
2749
|
+
this.glRef = untracked(this.glCanvasViewContainerRef).createComponent(untracked(this.sceneGraph), {
|
|
2750
|
+
environmentInjector: this.glEnvironmentInjector,
|
|
2751
|
+
injector: this.injector,
|
|
2752
|
+
});
|
|
2753
|
+
this.glRef.changeDetectorRef.detectChanges();
|
|
2754
|
+
this.setSceneGraphInputs(untracked(this.sceneGraphInputs));
|
|
2755
|
+
}
|
|
2756
|
+
noZoneSceneGraphInputsEffect() {
|
|
2757
|
+
this.autoEffect(() => {
|
|
2758
|
+
this.setSceneGraphInputs(this.sceneGraphInputs());
|
|
2759
|
+
});
|
|
2760
|
+
}
|
|
2761
|
+
setSceneGraphInputs(sceneGraphInputs) {
|
|
2762
|
+
if (this.glRef) {
|
|
2763
|
+
for (const [key, value] of Object.entries(sceneGraphInputs)) {
|
|
2764
|
+
this.glRef.setInput(key, value);
|
|
2765
|
+
}
|
|
2766
|
+
}
|
|
2767
|
+
}
|
|
2768
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.0.0", ngImport: i0, type: NgtCanvas, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
|
|
2769
|
+
static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.2.0", version: "18.0.0", type: NgtCanvas, isStandalone: true, selector: "ngt-canvas", inputs: { sceneGraph: { classPropertyName: "sceneGraph", publicName: "sceneGraph", isSignal: true, isRequired: true, transformFunction: null }, compoundPrefixes: { classPropertyName: "compoundPrefixes", publicName: "compoundPrefixes", isSignal: true, isRequired: false, transformFunction: null }, sceneGraphInputs: { classPropertyName: "sceneGraphInputs", publicName: "sceneGraphInputs", isSignal: true, isRequired: false, transformFunction: null }, gl: { classPropertyName: "gl", publicName: "gl", isSignal: true, isRequired: false, transformFunction: null }, size: { classPropertyName: "size", publicName: "size", isSignal: true, isRequired: false, transformFunction: null }, shadows: { classPropertyName: "shadows", publicName: "shadows", isSignal: true, isRequired: false, transformFunction: null }, legacy: { classPropertyName: "legacy", publicName: "legacy", isSignal: true, isRequired: false, transformFunction: null }, linear: { classPropertyName: "linear", publicName: "linear", isSignal: true, isRequired: false, transformFunction: null }, flat: { classPropertyName: "flat", publicName: "flat", isSignal: true, isRequired: false, transformFunction: null }, orthographic: { classPropertyName: "orthographic", publicName: "orthographic", isSignal: true, isRequired: false, transformFunction: null }, frameloop: { classPropertyName: "frameloop", publicName: "frameloop", isSignal: true, isRequired: false, transformFunction: null }, performance: { classPropertyName: "performance", publicName: "performance", isSignal: true, isRequired: false, transformFunction: null }, dpr: { classPropertyName: "dpr", publicName: "dpr", isSignal: true, isRequired: false, transformFunction: null }, raycaster: { classPropertyName: "raycaster", publicName: "raycaster", isSignal: true, isRequired: false, transformFunction: null }, scene: { classPropertyName: "scene", publicName: "scene", isSignal: true, isRequired: false, transformFunction: null }, camera: { classPropertyName: "camera", publicName: "camera", isSignal: true, isRequired: false, transformFunction: null }, events: { classPropertyName: "events", publicName: "events", isSignal: true, isRequired: false, transformFunction: null }, eventSource: { classPropertyName: "eventSource", publicName: "eventSource", isSignal: true, isRequired: false, transformFunction: null }, eventPrefix: { classPropertyName: "eventPrefix", publicName: "eventPrefix", isSignal: true, isRequired: false, transformFunction: null }, lookAt: { classPropertyName: "lookAt", publicName: "lookAt", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { created: "created", pointerMissed: "pointerMissed" }, host: { properties: { "style.pointerEvents": "hbPointerEvents()" }, styleAttribute: "display: block;position: relative;width: 100%;height: 100%;overflow: hidden;" }, providers: [
|
|
2770
|
+
provideResizeOptions({ emitInZone: false, emitInitialResult: true, debounce: 250 }),
|
|
2771
|
+
provideNgtStore(),
|
|
2772
|
+
], viewQueries: [{ propertyName: "glCanvas", first: true, predicate: ["glCanvas"], descendants: true, isSignal: true }, { propertyName: "glCanvasViewContainerRef", first: true, predicate: ["glCanvas"], descendants: true, read: ViewContainerRef, isSignal: true }], ngImport: i0, template: `
|
|
2773
|
+
<div (ngxResize)="resizeResult.set($event)" style="height: 100%; width: 100%;">
|
|
2774
|
+
<canvas #glCanvas style="display: block;"></canvas>
|
|
2775
|
+
</div>
|
|
2776
|
+
`, isInline: true, dependencies: [{ kind: "directive", type: NgxResize, selector: "[ngxResize]", inputs: ["ngxResizeOptions"], outputs: ["ngxResize"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush }); }
|
|
2777
|
+
}
|
|
2778
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.0", ngImport: i0, type: NgtCanvas, decorators: [{
|
|
2779
|
+
type: Component,
|
|
2780
|
+
args: [{
|
|
2781
|
+
selector: 'ngt-canvas',
|
|
2782
|
+
standalone: true,
|
|
2783
|
+
template: `
|
|
2784
|
+
<div (ngxResize)="resizeResult.set($event)" style="height: 100%; width: 100%;">
|
|
2785
|
+
<canvas #glCanvas style="display: block;"></canvas>
|
|
2786
|
+
</div>
|
|
2787
|
+
`,
|
|
2788
|
+
imports: [NgxResize],
|
|
2789
|
+
providers: [
|
|
2790
|
+
provideResizeOptions({ emitInZone: false, emitInitialResult: true, debounce: 250 }),
|
|
2791
|
+
provideNgtStore(),
|
|
2792
|
+
],
|
|
2793
|
+
host: {
|
|
2794
|
+
style: 'display: block;position: relative;width: 100%;height: 100%;overflow: hidden;',
|
|
2795
|
+
'[style.pointerEvents]': 'hbPointerEvents()',
|
|
2796
|
+
},
|
|
2797
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
2798
|
+
}]
|
|
2799
|
+
}], ctorParameters: () => [] });
|
|
2800
|
+
|
|
2801
|
+
const cached = new Map();
|
|
2802
|
+
function normalizeInputs(input) {
|
|
2803
|
+
if (Array.isArray(input))
|
|
2804
|
+
return input;
|
|
2805
|
+
if (typeof input === 'string')
|
|
2806
|
+
return [input];
|
|
2807
|
+
return Object.values(input);
|
|
2808
|
+
}
|
|
2809
|
+
function load(loaderConstructorFactory, inputs, { extensions, onProgress, } = {}) {
|
|
2810
|
+
return () => {
|
|
2811
|
+
const urls = normalizeInputs(inputs());
|
|
2812
|
+
const loader = new (loaderConstructorFactory(urls))();
|
|
2813
|
+
if (extensions)
|
|
2814
|
+
extensions(loader);
|
|
2815
|
+
// TODO: reevaluate this
|
|
2816
|
+
return urls.map((url) => {
|
|
2817
|
+
if (!cached.has(url)) {
|
|
2818
|
+
cached.set(url, new Promise((resolve, reject) => {
|
|
2819
|
+
loader.load(url, (data) => {
|
|
2820
|
+
if ('scene' in data) {
|
|
2821
|
+
Object.assign(data, makeObjectGraph(data['scene']));
|
|
2822
|
+
}
|
|
2823
|
+
resolve(data);
|
|
2824
|
+
}, onProgress, (error) => reject(new Error(`[NGT] Could not load ${url}: ${error}`)));
|
|
2825
|
+
}));
|
|
2826
|
+
}
|
|
2827
|
+
return cached.get(url);
|
|
2828
|
+
});
|
|
2829
|
+
};
|
|
2830
|
+
}
|
|
2831
|
+
function _injectNgtLoader(loaderConstructorFactory, inputs, { extensions, onProgress, injector, } = {}) {
|
|
2832
|
+
return assertInjector(_injectNgtLoader, injector, () => {
|
|
2833
|
+
const response = signal(null);
|
|
2834
|
+
const effector = load(loaderConstructorFactory, inputs, { extensions, onProgress });
|
|
2835
|
+
effect(() => {
|
|
2836
|
+
const originalUrls = inputs();
|
|
2837
|
+
Promise.all(effector()).then((results) => {
|
|
2838
|
+
response.update(() => {
|
|
2839
|
+
if (Array.isArray(originalUrls))
|
|
2840
|
+
return results;
|
|
2841
|
+
if (typeof originalUrls === 'string')
|
|
2842
|
+
return results[0];
|
|
2843
|
+
const keys = Object.keys(originalUrls);
|
|
2844
|
+
return keys.reduce((result, key) => {
|
|
2845
|
+
result[key] = results[keys.indexOf(key)];
|
|
2846
|
+
return result;
|
|
2847
|
+
}, {});
|
|
2848
|
+
});
|
|
2849
|
+
});
|
|
2850
|
+
});
|
|
2851
|
+
return response.asReadonly();
|
|
2852
|
+
});
|
|
2853
|
+
}
|
|
2854
|
+
_injectNgtLoader.preload = (loaderConstructorFactory, inputs, extensions) => {
|
|
2855
|
+
void Promise.all(load(loaderConstructorFactory, inputs, { extensions })());
|
|
2856
|
+
};
|
|
2857
|
+
_injectNgtLoader.destroy = () => {
|
|
2858
|
+
cached.clear();
|
|
2859
|
+
};
|
|
2860
|
+
const injectNgtLoader = _injectNgtLoader;
|
|
2861
|
+
|
|
2862
|
+
function injectNgtRef(initial = null, injector) {
|
|
2863
|
+
return assertInjector(injectNgtRef, injector, () => {
|
|
2864
|
+
const ref = is.ref(initial) ? initial : new ElementRef(initial);
|
|
2865
|
+
const refSignal = signal(ref.nativeElement, { equal: Object.is });
|
|
2866
|
+
const readonlyRef = refSignal.asReadonly();
|
|
2867
|
+
const computedCached = new Map();
|
|
2868
|
+
inject(DestroyRef).onDestroy(() => void computedCached.clear());
|
|
2869
|
+
const children = (type = 'objects') => {
|
|
2870
|
+
if (!computedCached.has(type)) {
|
|
2871
|
+
computedCached.set(type, computed(() => {
|
|
2872
|
+
const instance = readonlyRef();
|
|
2873
|
+
if (!instance)
|
|
2874
|
+
return [];
|
|
2875
|
+
const localState = getLocalState(instance);
|
|
2876
|
+
if (!localState?.instanceStore)
|
|
2877
|
+
return [];
|
|
2878
|
+
if (type === 'objects')
|
|
2879
|
+
return localState.objects();
|
|
2880
|
+
if (type === 'nonObjects')
|
|
2881
|
+
return localState.nonObjects();
|
|
2882
|
+
return [...localState.objects(), ...localState.nonObjects()];
|
|
2883
|
+
}));
|
|
2884
|
+
}
|
|
2885
|
+
return computedCached.get(type);
|
|
2886
|
+
};
|
|
2887
|
+
Object.defineProperties(ref, {
|
|
2888
|
+
nativeElement: {
|
|
2889
|
+
set: (newElement) => {
|
|
2890
|
+
untracked(() => {
|
|
2891
|
+
if (newElement !== readonlyRef()) {
|
|
2892
|
+
refSignal.set(newElement);
|
|
2893
|
+
}
|
|
2894
|
+
});
|
|
2895
|
+
},
|
|
2896
|
+
get: readonlyRef,
|
|
2897
|
+
},
|
|
2898
|
+
children: { value: children },
|
|
2899
|
+
});
|
|
2900
|
+
return ref;
|
|
2901
|
+
});
|
|
2902
|
+
}
|
|
2903
|
+
|
|
2904
|
+
function injectBeforeRender(cb, { priority = 0, injector } = {}) {
|
|
2905
|
+
return assertInjector(injectBeforeRender, injector, () => {
|
|
2906
|
+
const store = injectNgtStore();
|
|
2907
|
+
const sub = store.get('internal').subscribe(cb, priority, store);
|
|
2908
|
+
inject(DestroyRef).onDestroy(() => void sub());
|
|
2909
|
+
return sub;
|
|
2910
|
+
});
|
|
2911
|
+
}
|
|
2912
|
+
|
|
2913
|
+
const privateKeys = [
|
|
2914
|
+
'get',
|
|
2915
|
+
'set',
|
|
2916
|
+
'select',
|
|
2917
|
+
'setSize',
|
|
2918
|
+
'setDpr',
|
|
2919
|
+
'setFrameloop',
|
|
2920
|
+
'events',
|
|
2921
|
+
'invalidate',
|
|
2922
|
+
'advance',
|
|
2923
|
+
'size',
|
|
2924
|
+
'viewport',
|
|
2925
|
+
];
|
|
2926
|
+
const [, providePortalStore] = createInjectionToken((parentStore) => {
|
|
2927
|
+
const parentState = parentStore.snapshot;
|
|
2928
|
+
const pointer = new THREE.Vector2();
|
|
2929
|
+
const raycaster = new THREE.Raycaster();
|
|
2930
|
+
return signalStore(({ update }) => {
|
|
2931
|
+
return {
|
|
2932
|
+
...parentState,
|
|
2933
|
+
pointer,
|
|
2934
|
+
raycaster,
|
|
2935
|
+
previousRoot: parentStore,
|
|
2936
|
+
// Layers are allowed to override events
|
|
2937
|
+
setEvents: (events) => update((state) => ({ ...state, events: { ...state.events, ...events } })),
|
|
2938
|
+
};
|
|
2939
|
+
});
|
|
2940
|
+
}, { isRoot: false, token: NGT_STORE, deps: [[new SkipSelf(), NGT_STORE]] });
|
|
2941
|
+
class NgtPortalBeforeRender {
|
|
2942
|
+
constructor() {
|
|
2943
|
+
this.portalStore = injectNgtStore();
|
|
2944
|
+
this.injector = inject(Injector);
|
|
2945
|
+
this.renderPriority = input(1);
|
|
2946
|
+
this.parentScene = input.required();
|
|
2947
|
+
this.parentCamera = input.required();
|
|
2948
|
+
afterNextRender(() => {
|
|
2949
|
+
let oldClear;
|
|
2950
|
+
injectBeforeRender(() => {
|
|
2951
|
+
const { gl, scene, camera } = this.portalStore.get();
|
|
2952
|
+
oldClear = gl.autoClear;
|
|
2953
|
+
if (this.renderPriority() === 1) {
|
|
2954
|
+
// clear scene and render with default
|
|
2955
|
+
gl.autoClear = true;
|
|
2956
|
+
gl.render(this.parentScene(), this.parentCamera());
|
|
2957
|
+
}
|
|
2958
|
+
// disable cleaning
|
|
2959
|
+
gl.autoClear = false;
|
|
2960
|
+
gl.clearDepth();
|
|
2961
|
+
gl.render(scene, camera);
|
|
2962
|
+
// restore
|
|
2963
|
+
gl.autoClear = oldClear;
|
|
2964
|
+
}, { priority: this.renderPriority(), injector: this.injector });
|
|
2965
|
+
});
|
|
2966
|
+
}
|
|
2967
|
+
onPointerOver() {
|
|
2968
|
+
/* noop */
|
|
2969
|
+
}
|
|
2970
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.0.0", ngImport: i0, type: NgtPortalBeforeRender, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
|
|
2971
|
+
static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.1.0", version: "18.0.0", type: NgtPortalBeforeRender, isStandalone: true, selector: "ngt-portal-before-render", inputs: { renderPriority: { classPropertyName: "renderPriority", publicName: "renderPriority", isSignal: true, isRequired: false, transformFunction: null }, parentScene: { classPropertyName: "parentScene", publicName: "parentScene", isSignal: true, isRequired: true, transformFunction: null }, parentCamera: { classPropertyName: "parentCamera", publicName: "parentCamera", isSignal: true, isRequired: true, transformFunction: null } }, ngImport: i0, template: `
|
|
2972
|
+
<!-- Without an element that receives pointer events state.pointer will always be 0/0 -->
|
|
2973
|
+
<ngt-group (pointerover)="onPointerOver()" attach="none" />
|
|
2974
|
+
`, isInline: true }); }
|
|
2975
|
+
}
|
|
2976
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.0", ngImport: i0, type: NgtPortalBeforeRender, decorators: [{
|
|
2977
|
+
type: Component,
|
|
2978
|
+
args: [{
|
|
2979
|
+
selector: 'ngt-portal-before-render',
|
|
2980
|
+
standalone: true,
|
|
2981
|
+
template: `
|
|
2982
|
+
<!-- Without an element that receives pointer events state.pointer will always be 0/0 -->
|
|
2983
|
+
<ngt-group (pointerover)="onPointerOver()" attach="none" />
|
|
2984
|
+
`,
|
|
2985
|
+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
|
|
2986
|
+
}]
|
|
2987
|
+
}], ctorParameters: () => [] });
|
|
2988
|
+
class NgtPortalContent {
|
|
2989
|
+
constructor(vcr, parentVcr) {
|
|
2990
|
+
const commentNode = vcr.element.nativeElement;
|
|
2991
|
+
if (commentNode[SPECIAL_INTERNAL_ADD_COMMENT]) {
|
|
2992
|
+
commentNode[SPECIAL_INTERNAL_ADD_COMMENT](parentVcr.element.nativeElement);
|
|
2993
|
+
delete commentNode[SPECIAL_INTERNAL_ADD_COMMENT];
|
|
2994
|
+
}
|
|
2995
|
+
}
|
|
2996
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.0.0", ngImport: i0, type: NgtPortalContent, deps: [{ token: i0.ViewContainerRef }, { token: i0.ViewContainerRef, skipSelf: true }], target: i0.ɵɵFactoryTarget.Directive }); }
|
|
2997
|
+
static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "18.0.0", type: NgtPortalContent, isStandalone: true, selector: "ng-template[ngtPortalContent]", ngImport: i0 }); }
|
|
2998
|
+
}
|
|
2999
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.0", ngImport: i0, type: NgtPortalContent, decorators: [{
|
|
3000
|
+
type: Directive,
|
|
3001
|
+
args: [{ selector: 'ng-template[ngtPortalContent]', standalone: true }]
|
|
3002
|
+
}], ctorParameters: () => [{ type: i0.ViewContainerRef }, { type: i0.ViewContainerRef, decorators: [{
|
|
3003
|
+
type: SkipSelf
|
|
3004
|
+
}] }] });
|
|
3005
|
+
class NgtPortal {
|
|
3006
|
+
constructor() {
|
|
3007
|
+
this.options = input({ container: injectNgtRef(prepare(new THREE.Scene())) }, {
|
|
3008
|
+
transform: (value) => ({
|
|
3009
|
+
container: injectNgtRef(prepare(new THREE.Scene())),
|
|
3010
|
+
...value,
|
|
3011
|
+
}),
|
|
3012
|
+
});
|
|
3013
|
+
this.autoRender = input(false);
|
|
3014
|
+
this.autoRenderPriority = input(1);
|
|
3015
|
+
this.portalContentTemplate = contentChild.required(NgtPortalContent, { read: TemplateRef });
|
|
3016
|
+
this.portalContentAnchor = viewChild.required('portalContentAnchor', { read: ViewContainerRef });
|
|
3017
|
+
this.destroyRef = inject(DestroyRef);
|
|
3018
|
+
this.autoEffect = injectAutoEffect();
|
|
3019
|
+
this.parentStore = injectNgtStore({ skipSelf: true });
|
|
3020
|
+
this.portalStore = injectNgtStore({ self: true });
|
|
3021
|
+
this.portalRendered = signal(false);
|
|
3022
|
+
this.renderAutoBeforeRender = computed(() => this.portalRendered() && this.autoRender());
|
|
3023
|
+
this.parentScene = this.parentStore.get('scene');
|
|
3024
|
+
this.parentCamera = this.parentStore.get('camera');
|
|
3025
|
+
afterNextRender(() => {
|
|
3026
|
+
const parentState = this.parentStore.snapshot;
|
|
3027
|
+
const { container, state: { events = {}, size = {}, ...rest } = {} } = untracked(this.options);
|
|
3028
|
+
this.portalStore.update({
|
|
3029
|
+
scene: (is.ref(container) ? container.nativeElement : container),
|
|
3030
|
+
events: { ...parentState.events, ...events },
|
|
3031
|
+
size: { ...parentState.size, ...size },
|
|
3032
|
+
...rest,
|
|
3033
|
+
});
|
|
3034
|
+
this.autoEffect(() => {
|
|
3035
|
+
const previous = this.parentStore.state();
|
|
3036
|
+
this.portalStore.update((state) => this.inject(previous, state));
|
|
3037
|
+
});
|
|
3038
|
+
untracked(() => {
|
|
3039
|
+
const portalView = this.portalContentAnchor().createEmbeddedView(this.portalContentTemplate());
|
|
3040
|
+
portalView.detectChanges();
|
|
3041
|
+
this.destroyRef.onDestroy(portalView.destroy.bind(portalView));
|
|
3042
|
+
});
|
|
3043
|
+
this.portalRendered.set(true);
|
|
3044
|
+
});
|
|
3045
|
+
}
|
|
3046
|
+
inject(rootState, injectState) {
|
|
3047
|
+
const intersect = { ...rootState };
|
|
3048
|
+
Object.keys(intersect).forEach((key) => {
|
|
3049
|
+
if (privateKeys.includes(key) ||
|
|
3050
|
+
rootState[key] !== injectState[key]) {
|
|
3051
|
+
delete intersect[key];
|
|
3052
|
+
}
|
|
3053
|
+
});
|
|
3054
|
+
const inputs = untracked(this.options);
|
|
3055
|
+
const { size, events, ...restInputsState } = inputs.state || {};
|
|
3056
|
+
let viewport = undefined;
|
|
3057
|
+
if (injectState && size) {
|
|
3058
|
+
const camera = injectState.camera;
|
|
3059
|
+
viewport = rootState.viewport.getCurrentViewport(camera, new THREE.Vector3(), size);
|
|
3060
|
+
if (camera !== rootState.camera)
|
|
3061
|
+
updateCamera(camera, size);
|
|
3062
|
+
}
|
|
3063
|
+
return {
|
|
3064
|
+
...intersect,
|
|
3065
|
+
scene: is.ref(inputs.container) ? inputs.container.nativeElement : inputs.container,
|
|
3066
|
+
previousRoot: this.parentStore,
|
|
3067
|
+
events: { ...rootState.events, ...(injectState?.events || {}), ...events },
|
|
3068
|
+
size: { ...rootState.size, ...size },
|
|
3069
|
+
viewport: { ...rootState.viewport, ...(viewport || {}) },
|
|
3070
|
+
...restInputsState,
|
|
3071
|
+
};
|
|
3072
|
+
}
|
|
3073
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.0.0", ngImport: i0, type: NgtPortal, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
|
|
3074
|
+
static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "18.0.0", type: NgtPortal, isStandalone: true, selector: "ngt-portal", inputs: { options: { classPropertyName: "options", publicName: "options", isSignal: true, isRequired: false, transformFunction: null }, autoRender: { classPropertyName: "autoRender", publicName: "autoRender", isSignal: true, isRequired: false, transformFunction: null }, autoRenderPriority: { classPropertyName: "autoRenderPriority", publicName: "autoRenderPriority", isSignal: true, isRequired: false, transformFunction: null } }, providers: [providePortalStore()], queries: [{ propertyName: "portalContentTemplate", first: true, predicate: NgtPortalContent, descendants: true, read: TemplateRef, isSignal: true }], viewQueries: [{ propertyName: "portalContentAnchor", first: true, predicate: ["portalContentAnchor"], descendants: true, read: ViewContainerRef, isSignal: true }], ngImport: i0, template: `
|
|
3075
|
+
<ng-container #portalContentAnchor>
|
|
3076
|
+
@if (renderAutoBeforeRender()) {
|
|
3077
|
+
<ngt-portal-before-render
|
|
3078
|
+
[renderPriority]="autoRenderPriority()"
|
|
3079
|
+
[parentScene]="parentScene"
|
|
3080
|
+
[parentCamera]="parentCamera"
|
|
3081
|
+
/>
|
|
3082
|
+
}
|
|
3083
|
+
</ng-container>
|
|
3084
|
+
`, isInline: true, dependencies: [{ kind: "component", type: NgtPortalBeforeRender, selector: "ngt-portal-before-render", inputs: ["renderPriority", "parentScene", "parentCamera"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush }); }
|
|
3085
|
+
}
|
|
3086
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.0", ngImport: i0, type: NgtPortal, decorators: [{
|
|
3087
|
+
type: Component,
|
|
3088
|
+
args: [{
|
|
3089
|
+
selector: 'ngt-portal',
|
|
3090
|
+
standalone: true,
|
|
3091
|
+
template: `
|
|
3092
|
+
<ng-container #portalContentAnchor>
|
|
3093
|
+
@if (renderAutoBeforeRender()) {
|
|
3094
|
+
<ngt-portal-before-render
|
|
3095
|
+
[renderPriority]="autoRenderPriority()"
|
|
3096
|
+
[parentScene]="parentScene"
|
|
3097
|
+
[parentCamera]="parentCamera"
|
|
3098
|
+
/>
|
|
3099
|
+
}
|
|
3100
|
+
</ng-container>
|
|
3101
|
+
`,
|
|
3102
|
+
imports: [NgtPortalBeforeRender],
|
|
3103
|
+
providers: [providePortalStore()],
|
|
3104
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
3105
|
+
}]
|
|
3106
|
+
}], ctorParameters: () => [] });
|
|
3107
|
+
|
|
3108
|
+
var _a;
|
|
3109
|
+
class NgtRoutedScene {
|
|
3110
|
+
static { _a = ROUTED_SCENE; }
|
|
3111
|
+
static { this[_a] = true; }
|
|
3112
|
+
constructor(router, cdr) {
|
|
3113
|
+
router.events
|
|
3114
|
+
.pipe(filter((event) => event instanceof ActivationEnd), takeUntilDestroyed())
|
|
3115
|
+
.subscribe(cdr.detectChanges.bind(cdr));
|
|
3116
|
+
}
|
|
3117
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.0.0", ngImport: i0, type: NgtRoutedScene, deps: [{ token: i1.Router }, { token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Component }); }
|
|
3118
|
+
static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.0.0", type: NgtRoutedScene, isStandalone: true, selector: "ngt-routed-scene", ngImport: i0, template: `
|
|
3119
|
+
<router-outlet />
|
|
3120
|
+
`, isInline: true, dependencies: [{ kind: "directive", type: RouterOutlet, selector: "router-outlet", inputs: ["name"], outputs: ["activate", "deactivate", "attach", "detach"], exportAs: ["outlet"] }] }); }
|
|
3121
|
+
}
|
|
3122
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.0", ngImport: i0, type: NgtRoutedScene, decorators: [{
|
|
3123
|
+
type: Component,
|
|
3124
|
+
args: [{
|
|
3125
|
+
standalone: true,
|
|
3126
|
+
selector: 'ngt-routed-scene',
|
|
3127
|
+
template: `
|
|
3128
|
+
<router-outlet />
|
|
3129
|
+
`,
|
|
3130
|
+
imports: [RouterOutlet],
|
|
3131
|
+
}]
|
|
3132
|
+
}], ctorParameters: () => [{ type: i1.Router }, { type: i0.ChangeDetectorRef }] });
|
|
3133
|
+
|
|
3134
|
+
function apiFactory(obj) {
|
|
3135
|
+
return obj.api;
|
|
3136
|
+
}
|
|
3137
|
+
function createApiToken(forwardedObject) {
|
|
3138
|
+
const [injectFn, provideFn] = createInjectionToken((apiFactory), {
|
|
3139
|
+
isRoot: false,
|
|
3140
|
+
deps: [forwardRef(forwardedObject)],
|
|
3141
|
+
});
|
|
3142
|
+
return [injectFn, () => provideFn()];
|
|
3143
|
+
}
|
|
3144
|
+
|
|
3145
|
+
function injectInputs(dir, dirType) {
|
|
3146
|
+
const mirror = reflectComponentType(dirType);
|
|
3147
|
+
if (!mirror) {
|
|
3148
|
+
throw new Error(`[NGT] The provided symbol is not a component nor a directive`);
|
|
3149
|
+
}
|
|
3150
|
+
const inputs = mirror.inputs.reduce((inputs, cur) => {
|
|
3151
|
+
if (dir[cur.propName] && isSignal(dir[cur.propName])) {
|
|
3152
|
+
inputs[cur.propName] = dir[cur.propName];
|
|
3153
|
+
}
|
|
3154
|
+
return inputs;
|
|
3155
|
+
}, {});
|
|
3156
|
+
return computed(() => Object.keys(inputs).reduce((acc, key) => {
|
|
3157
|
+
acc[key] = inputs[key]();
|
|
3158
|
+
return acc;
|
|
3159
|
+
}, {}));
|
|
3160
|
+
}
|
|
3161
|
+
|
|
3162
|
+
/**
|
|
3163
|
+
* Generated bundle index. Do not edit.
|
|
3164
|
+
*/
|
|
3165
|
+
|
|
3166
|
+
export { HTML, NGT_STORE, NgtArgs, NgtCanvas, NgtPortal, NgtPortalContent, NgtRenderer, NgtRendererFactory, NgtRoutedScene, ROUTED_SCENE, addAfterEffect, addEffect, addTail, applyProps, checkNeedsUpdate, checkUpdate, createApiToken, createAttachFunction, extend, getLocalState, injectBeforeRender, injectInputs, injectNgtLoader, injectNgtRef, injectNgtStore, invalidateInstance, is, makeCameraInstance, makeDpr, makeId, makeObjectGraph, makeRendererInstance, prepare, provideNgtRenderer, provideNgtStore, signalStore, updateCamera };
|
|
3167
|
+
//# sourceMappingURL=angular-three.mjs.map
|