@playcanvas/web-components 0.11.1 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/app.d.cts +246 -0
- package/dist/app.d.ts +38 -13
- package/dist/asset.d.cts +218 -0
- package/dist/asset.d.ts +145 -6
- package/dist/async-element.d.cts +103 -0
- package/dist/async-element.d.ts +11 -8
- package/dist/colors.d.cts +1 -0
- package/dist/components/button-component.d.cts +186 -0
- package/dist/components/button-component.d.ts +1 -1
- package/dist/components/camera-component.d.cts +253 -0
- package/dist/components/camera-component.d.ts +1 -1
- package/dist/components/collision-component.d.cts +73 -0
- package/dist/components/collision-component.d.ts +17 -1
- package/dist/components/component.d.cts +82 -0
- package/dist/components/component.d.ts +20 -1
- package/dist/components/element-component.d.cts +316 -0
- package/dist/components/element-component.d.ts +1 -1
- package/dist/components/gsplat-component.d.cts +108 -0
- package/dist/components/gsplat-component.d.ts +1 -1
- package/dist/components/layoutchild-component.d.cts +110 -0
- package/dist/components/layoutchild-component.d.ts +1 -1
- package/dist/components/layoutgroup-component.d.cts +136 -0
- package/dist/components/layoutgroup-component.d.ts +1 -1
- package/dist/components/light-component.d.cts +264 -0
- package/dist/components/light-component.d.ts +1 -1
- package/dist/components/listener-component.d.cts +20 -0
- package/dist/components/listener-component.d.ts +1 -1
- package/dist/components/particlesystem-component.d.cts +52 -0
- package/dist/components/particlesystem-component.d.ts +1 -1
- package/dist/components/render-component.d.cts +76 -0
- package/dist/components/render-component.d.ts +1 -1
- package/dist/components/rigidbody-component.d.cts +88 -0
- package/dist/components/rigidbody-component.d.ts +1 -1
- package/dist/components/screen-component.d.cts +70 -0
- package/dist/components/screen-component.d.ts +1 -1
- package/dist/components/script-component.d.cts +163 -0
- package/dist/components/script-component.d.ts +1 -1
- package/dist/components/script.d.cts +94 -0
- package/dist/components/script.d.ts +1 -1
- package/dist/components/scrollbar-component.d.cts +69 -0
- package/dist/components/scrollbar-component.d.ts +1 -1
- package/dist/components/scrollview-component.d.cts +178 -0
- package/dist/components/scrollview-component.d.ts +1 -1
- package/dist/components/sound-component.d.cts +108 -0
- package/dist/components/sound-component.d.ts +1 -1
- package/dist/components/sound-slot.d.cts +134 -0
- package/dist/components/sound-slot.d.ts +2 -2
- package/dist/custom-elements.json +1127 -179
- package/dist/entity-base.d.cts +67 -0
- package/dist/entity-base.d.ts +67 -0
- package/dist/entity.d.cts +131 -0
- package/dist/entity.d.ts +3 -38
- package/dist/index.d.cts +79 -0
- package/dist/index.d.ts +35 -32
- package/dist/loading-bar.d.cts +35 -0
- package/dist/material.d.cts +1011 -0
- package/dist/material.d.ts +2 -1
- package/dist/model.d.cts +72 -0
- package/dist/model.d.ts +27 -5
- package/dist/module.d.cts +29 -0
- package/dist/module.d.ts +16 -10
- package/dist/node.d.cts +253 -0
- package/dist/node.d.ts +253 -0
- package/dist/parse.d.cts +147 -0
- package/dist/pwc.cjs +1496 -200
- package/dist/pwc.cjs.map +1 -1
- package/dist/pwc.js +1497 -201
- package/dist/pwc.js.map +1 -1
- package/dist/pwc.min.js +1 -1
- package/dist/pwc.min.js.map +1 -1
- package/dist/pwc.min.mjs +1 -1
- package/dist/pwc.min.mjs.map +1 -1
- package/dist/pwc.mjs +1496 -202
- package/dist/pwc.mjs.map +1 -1
- package/dist/scene.d.cts +117 -0
- package/dist/scene.d.ts +1 -1
- package/dist/sky.d.cts +121 -0
- package/dist/sky.d.ts +1 -1
- package/dist/vscode.html-custom-data.json +127 -5
- package/dist/web-types.json +399 -59
- package/package.json +16 -7
- package/src/app.ts +137 -45
- package/src/asset.ts +439 -9
- package/src/async-element.ts +11 -8
- package/src/components/collision-component.ts +35 -0
- package/src/components/component.ts +93 -3
- package/src/entity-base.ts +136 -0
- package/src/entity.ts +23 -117
- package/src/index.ts +5 -0
- package/src/loading-bar.ts +2 -2
- package/src/material.ts +2 -2
- package/src/model.ts +79 -11
- package/src/module.ts +39 -20
- package/src/node.ts +715 -0
- package/src/sky.ts +0 -1
package/dist/pwc.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
2
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('playcanvas')) :
|
|
3
3
|
typeof define === 'function' && define.amd ? define(['exports', 'playcanvas'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.pwc = {}, global.pc));
|
|
5
5
|
})(this, (function (exports, playcanvas) { 'use strict';
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -31,12 +31,13 @@
|
|
|
31
31
|
return this.parentElement?.closest('pc-app') ?? null;
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
34
|
-
* The nearest ancestor
|
|
35
|
-
* ancestor. The search starts at the parent, so an element
|
|
36
|
-
*
|
|
34
|
+
* The nearest ancestor element that fronts an entity — `<pc-entity>` or `<pc-node>` — or
|
|
35
|
+
* `null` if this element has no such ancestor. The search starts at the parent, so an element
|
|
36
|
+
* never resolves to itself.
|
|
37
|
+
* @returns The closest entity-fronting element, or `null`.
|
|
37
38
|
*/
|
|
38
39
|
get closestEntity() {
|
|
39
|
-
return this.parentElement?.closest('pc-entity') ?? null;
|
|
40
|
+
return this.parentElement?.closest('pc-entity, pc-node') ?? null;
|
|
40
41
|
}
|
|
41
42
|
/**
|
|
42
43
|
* Called when the element is fully initialized and ready. Subclasses should call this when
|
|
@@ -111,12 +112,18 @@
|
|
|
111
112
|
|
|
112
113
|
/**
|
|
113
114
|
* The ModuleElement interface provides properties and methods for manipulating
|
|
114
|
-
* {@link https://developer.playcanvas.com/user-manual/web-components/tags/pc-module/ | `<pc-module>`}
|
|
115
|
-
* The ModuleElement interface also inherits the properties and methods of the
|
|
116
|
-
* {@link
|
|
115
|
+
* {@link https://developer.playcanvas.com/user-manual/web-components/tags/pc-module/ | `<pc-module>`}
|
|
116
|
+
* elements. The ModuleElement interface also inherits the properties and methods of the
|
|
117
|
+
* {@link AsyncElement} interface.
|
|
117
118
|
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
119
|
+
* The attributes are read once, when the module starts loading - on the element's first
|
|
120
|
+
* connection, or earlier if a containing `<pc-app>` boots first and collects it - so changing
|
|
121
|
+
* them later has no effect. The element becomes ready once the module has loaded. WebAssembly
|
|
122
|
+
* modules configure engine-global state that never unloads, so readiness is not re-armed by
|
|
123
|
+
* removing the element, and a re-inserted element does not load again.
|
|
124
|
+
*
|
|
125
|
+
* A `<pc-module>` without a `name` warns and never becomes ready; a containing `<pc-app>` still
|
|
126
|
+
* boots.
|
|
120
127
|
*
|
|
121
128
|
* @attribute {string} name - The name of the WebAssembly module to configure, e.g. `Basis` or
|
|
122
129
|
* `Ammo`.
|
|
@@ -125,19 +132,22 @@
|
|
|
125
132
|
* @attribute {string} fallback - The URL of the module's asm.js fallback script, used when
|
|
126
133
|
* WebAssembly is unavailable.
|
|
127
134
|
*/
|
|
128
|
-
class ModuleElement extends
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
super();
|
|
133
|
-
this.loadPromise = this.loadModule();
|
|
135
|
+
class ModuleElement extends AsyncElement {
|
|
136
|
+
_loadPromise = null;
|
|
137
|
+
connectedCallback() {
|
|
138
|
+
this._getLoadPromise();
|
|
134
139
|
}
|
|
135
|
-
async
|
|
140
|
+
async _loadModule() {
|
|
136
141
|
const name = this.getAttribute('name');
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
142
|
+
if (!name) {
|
|
143
|
+
console.warn("pc-module requires a 'name' attribute - no module was configured");
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const config = {
|
|
147
|
+
glueUrl: this.getAttribute('glue') ?? undefined,
|
|
148
|
+
wasmUrl: this.getAttribute('wasm') ?? undefined,
|
|
149
|
+
fallbackUrl: this.getAttribute('fallback') ?? undefined
|
|
150
|
+
};
|
|
141
151
|
if (name === 'Basis') {
|
|
142
152
|
playcanvas.basisInitialize(config);
|
|
143
153
|
}
|
|
@@ -147,16 +157,22 @@
|
|
|
147
157
|
playcanvas.WasmModule.getInstance(name, () => resolve());
|
|
148
158
|
});
|
|
149
159
|
}
|
|
160
|
+
this._onReady();
|
|
150
161
|
}
|
|
151
162
|
/**
|
|
152
|
-
* Returns the promise that settles when the module has loaded
|
|
153
|
-
* `<pc-app>`
|
|
163
|
+
* Returns the promise that settles when the module has loaded, starting the load if it has
|
|
164
|
+
* not already started - a containing `<pc-app>` boots in document order, so it may collect
|
|
165
|
+
* this element before the element's own connectedCallback has run. A missing `name` resolves
|
|
166
|
+
* the promise without configuring anything, so a misconfigured module never blocks the app.
|
|
154
167
|
*
|
|
155
168
|
* @returns The load promise.
|
|
156
169
|
* @internal
|
|
157
170
|
*/
|
|
158
171
|
_getLoadPromise() {
|
|
159
|
-
|
|
172
|
+
if (!this._loadPromise) {
|
|
173
|
+
this._loadPromise = this._loadModule();
|
|
174
|
+
}
|
|
175
|
+
return this._loadPromise;
|
|
160
176
|
}
|
|
161
177
|
}
|
|
162
178
|
customElements.define('pc-module', ModuleElement);
|
|
@@ -186,9 +202,9 @@
|
|
|
186
202
|
this._track.setAttribute('aria-label', 'Loading');
|
|
187
203
|
this._track.setAttribute('aria-valuemin', '0');
|
|
188
204
|
this._track.setAttribute('aria-valuemax', '100');
|
|
189
|
-
//
|
|
205
|
+
// Anchored to the pc-app element, which the library's base styles make a positioned box
|
|
190
206
|
this._track.style.cssText = [
|
|
191
|
-
'position:
|
|
207
|
+
'position: absolute',
|
|
192
208
|
'top: 0',
|
|
193
209
|
'left: 0',
|
|
194
210
|
'width: 100%',
|
|
@@ -700,16 +716,44 @@
|
|
|
700
716
|
|
|
701
717
|
/** The pointer event types the application synthesizes on `<pc-entity>` elements via picking. */
|
|
702
718
|
const pointerEventTypes = ['pointermove', 'pointerdown', 'pointerup', 'pointerenter', 'pointerleave'];
|
|
719
|
+
/**
|
|
720
|
+
* Gives `pc-app` the sizing contract of a replaced element (`<video>`, `<img>`): a block-level
|
|
721
|
+
* box that the page's CSS sizes, defaulting to the canvas's own 300x150 intrinsic size, with the
|
|
722
|
+
* canvas and loading bar anchored to it. `:where()` keeps every declaration at zero specificity,
|
|
723
|
+
* so any page rule - however plain - overrides these defaults.
|
|
724
|
+
*/
|
|
725
|
+
const ensureBaseStyles = () => {
|
|
726
|
+
const id = 'pc-app-styles';
|
|
727
|
+
if (document.getElementById(id)) {
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
730
|
+
const style = document.createElement('style');
|
|
731
|
+
style.id = id;
|
|
732
|
+
style.textContent = ':where(pc-app) { display: block; position: relative; width: 300px; height: 150px; }';
|
|
733
|
+
document.head.appendChild(style);
|
|
734
|
+
};
|
|
703
735
|
/**
|
|
704
736
|
* The AppElement interface provides properties and methods for manipulating
|
|
705
737
|
* {@link https://developer.playcanvas.com/user-manual/web-components/tags/pc-app/ | `<pc-app>`} elements.
|
|
706
738
|
* The AppElement interface also inherits the properties and methods of the
|
|
707
739
|
* {@link HTMLElement} interface.
|
|
708
740
|
*
|
|
741
|
+
* The element is sized like a replaced element such as `<video>`: a block-level box that the
|
|
742
|
+
* page's CSS controls, 300x150 by default. The application's canvas always fills the element,
|
|
743
|
+
* and the drawing buffer resolution follows the element's size (capped by `max-pixel-ratio`),
|
|
744
|
+
* tracked live via a ResizeObserver — so the element can be embedded at any size, resized by
|
|
745
|
+
* its container, or made fullscreen with ordinary CSS such as `width: 100vw; height: 100dvh`.
|
|
746
|
+
*
|
|
709
747
|
* @fires {ProgressEvent} progress - Fired while the application preloads its assets. `loaded` and
|
|
710
748
|
* `total` are asset counts, not bytes, and an asset that fails to load still counts as loaded.
|
|
711
749
|
* Fired at least once per boot, and the final event always has `loaded` equal to `total`. Does
|
|
712
750
|
* not bubble.
|
|
751
|
+
*
|
|
752
|
+
* @fires {ErrorEvent} error - Fired when the application cannot boot because no graphics device
|
|
753
|
+
* could be created (for example, a browser with WebGL disabled). `message` names the requested
|
|
754
|
+
* backends and `error` holds the underlying failure. The element never becomes ready
|
|
755
|
+
* and `app` stays `null` — listen for this event to show a fallback UI. Removing the element and
|
|
756
|
+
* re-inserting it retries the boot with its current attributes. Does not bubble.
|
|
713
757
|
*/
|
|
714
758
|
class AppElement extends AsyncElement {
|
|
715
759
|
/**
|
|
@@ -746,8 +790,9 @@
|
|
|
746
790
|
_bootGeneration = 0;
|
|
747
791
|
/**
|
|
748
792
|
* The elements backing this application's entities, keyed by the entity itself. Registered
|
|
749
|
-
* by EntityElement at creation and removed when an entity is
|
|
750
|
-
* scene nodes back to their owning elements by
|
|
793
|
+
* by EntityElement at creation (and NodeElement at binding) and removed when an entity is
|
|
794
|
+
* destroyed or unbound, this joins engine scene nodes back to their owning elements by
|
|
795
|
+
* identity - never by name.
|
|
751
796
|
*/
|
|
752
797
|
_entityElements = new Map();
|
|
753
798
|
_picker = null;
|
|
@@ -768,6 +813,12 @@
|
|
|
768
813
|
};
|
|
769
814
|
_app = null;
|
|
770
815
|
_loadProgress = 0;
|
|
816
|
+
/**
|
|
817
|
+
* Tracks the element's box so the drawing buffer and picker follow it. Created per boot once
|
|
818
|
+
* the application exists, and disconnected on teardown. `null` where ResizeObserver is
|
|
819
|
+
* unavailable (jsdom), where the boot-time resolution set is the only sizing that happens.
|
|
820
|
+
*/
|
|
821
|
+
_resizeObserver = null;
|
|
771
822
|
/**
|
|
772
823
|
* The PlayCanvas application instance. `null` until the element is ready, and again once it
|
|
773
824
|
* has been removed from the document — await {@link whenReady} or the element's `ready()`
|
|
@@ -794,8 +845,6 @@
|
|
|
794
845
|
*/
|
|
795
846
|
constructor() {
|
|
796
847
|
super();
|
|
797
|
-
// Bind methods to maintain 'this' context
|
|
798
|
-
this._onWindowResize = this._onWindowResize.bind(this);
|
|
799
848
|
// Track pointer listeners being added to and removed from descendant entities.
|
|
800
849
|
// Registered once here rather than on every boot - the handlers no-op while there is no
|
|
801
850
|
// canvas, and a re-booted element must not stack a second set.
|
|
@@ -806,6 +855,9 @@
|
|
|
806
855
|
}
|
|
807
856
|
async connectedCallback() {
|
|
808
857
|
const generation = ++this._bootGeneration;
|
|
858
|
+
// Installed before the loading bar is created: the bar anchors to this element, which
|
|
859
|
+
// these styles make a positioned block box
|
|
860
|
+
ensureBaseStyles();
|
|
809
861
|
// Created before the first await, so the bar is visible while modules and the graphics
|
|
810
862
|
// device are created, and exists before any disconnect could need to clean it up
|
|
811
863
|
if (this._loadingBar && !this._bar) {
|
|
@@ -820,8 +872,11 @@
|
|
|
820
872
|
if (generation !== this._bootGeneration) {
|
|
821
873
|
return;
|
|
822
874
|
}
|
|
823
|
-
// Create and append the canvas
|
|
875
|
+
// Create and append the canvas, filling the element's content box - the page sizes the
|
|
876
|
+
// element, and everything else follows. touch-action: none keeps touch drags driving the
|
|
877
|
+
// engine's input handlers instead of scrolling the page.
|
|
824
878
|
this._canvas = document.createElement('canvas');
|
|
879
|
+
this._canvas.style.cssText = 'display: block; width: 100%; height: 100%; touch-action: none;';
|
|
825
880
|
this.appendChild(this._canvas);
|
|
826
881
|
// Configure device types based on backend selection
|
|
827
882
|
const backendToDeviceTypes = {
|
|
@@ -831,14 +886,44 @@
|
|
|
831
886
|
};
|
|
832
887
|
const deviceTypes = backendToDeviceTypes[this._backend] || [];
|
|
833
888
|
this._optionsLocked = true;
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
889
|
+
// createGraphicsDevice appends its final null-device fallback to the array in place, so
|
|
890
|
+
// the requested list is captured now for the failure message.
|
|
891
|
+
const requested = deviceTypes.join(', ');
|
|
892
|
+
let device;
|
|
893
|
+
try {
|
|
894
|
+
device = await playcanvas.createGraphicsDevice(this._canvas, {
|
|
895
|
+
// @ts-ignore - alpha needs to be documented
|
|
896
|
+
alpha: this._alpha,
|
|
897
|
+
antialias: this._antialias,
|
|
898
|
+
depth: this._depthBuffer,
|
|
899
|
+
deviceTypes: deviceTypes,
|
|
900
|
+
stencil: this._stencilBuffer
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
catch (error) {
|
|
904
|
+
// The element may have been removed while device creation was failing. The teardown
|
|
905
|
+
// has already cleaned up, and the failure belongs to a boot that no longer owns the
|
|
906
|
+
// element.
|
|
907
|
+
if (generation !== this._bootGeneration) {
|
|
908
|
+
return;
|
|
909
|
+
}
|
|
910
|
+
// Return the element to its pre-boot state - no dead canvas, no loading bar stuck at
|
|
911
|
+
// zero - before announcing the failure. Readiness deliberately stays pending: nothing
|
|
912
|
+
// it would announce (the app, the entity hierarchy) exists, so a device-less element
|
|
913
|
+
// joins the documented never-ready cases and the failure surfaces through the error
|
|
914
|
+
// event instead.
|
|
915
|
+
if (this._canvas && this.contains(this._canvas)) {
|
|
916
|
+
this.removeChild(this._canvas);
|
|
917
|
+
}
|
|
918
|
+
this._canvas = null;
|
|
919
|
+
this._bar?.destroy();
|
|
920
|
+
this._bar = null;
|
|
921
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
922
|
+
const message = `pc-app failed to create a graphics device (${requested}) - ${reason}`;
|
|
923
|
+
console.error(message, error);
|
|
924
|
+
this.dispatchEvent(new ErrorEvent('error', { message, error }));
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
842
927
|
// The element may have been removed while the device was created. disconnectedCallback
|
|
843
928
|
// has already cleaned up the canvas; the device was created inside the await, so it is
|
|
844
929
|
// this boot's to release.
|
|
@@ -917,9 +1002,19 @@
|
|
|
917
1002
|
const app = new playcanvas.AppBase(this._canvas);
|
|
918
1003
|
this._app = app;
|
|
919
1004
|
app.init(createOptions);
|
|
920
|
-
|
|
1005
|
+
// FILLMODE_NONE leaves the canvas's CSS sizing alone (the engine's other fill modes
|
|
1006
|
+
// stamp window-derived pixel sizes onto it); RESOLUTION_AUTO sizes the drawing buffer
|
|
1007
|
+
// from the canvas's client size
|
|
1008
|
+
app.setCanvasFillMode(playcanvas.FILLMODE_NONE);
|
|
921
1009
|
app.setCanvasResolution(playcanvas.RESOLUTION_AUTO);
|
|
922
1010
|
this._pickerCreate();
|
|
1011
|
+
// Track the element's box rather than the window: containers resize without any window
|
|
1012
|
+
// event (splitter drags, flex reflow, animations). Guarded because jsdom has no
|
|
1013
|
+
// ResizeObserver - there, the resolution set above is the only sizing that happens.
|
|
1014
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
1015
|
+
this._resizeObserver = new ResizeObserver(() => this._syncCanvasSize());
|
|
1016
|
+
this._resizeObserver.observe(this);
|
|
1017
|
+
}
|
|
923
1018
|
// Get all pc-asset elements that are direct children of the pc-app element
|
|
924
1019
|
const assetElements = this.querySelectorAll(':scope > pc-asset');
|
|
925
1020
|
for (const assetElement of Array.from(assetElements)) {
|
|
@@ -995,8 +1090,6 @@
|
|
|
995
1090
|
// Dismiss the bar only once a frame has actually rendered; ready fires before the
|
|
996
1091
|
// first rAF tick
|
|
997
1092
|
app.once('frameend', () => this._bar?.complete());
|
|
998
|
-
// Handle window resize to keep the canvas responsive
|
|
999
|
-
window.addEventListener('resize', this._onWindowResize);
|
|
1000
1093
|
this._onReady();
|
|
1001
1094
|
});
|
|
1002
1095
|
}
|
|
@@ -1021,18 +1114,27 @@
|
|
|
1021
1114
|
// no longer holds.
|
|
1022
1115
|
this._hierarchyReady = false;
|
|
1023
1116
|
this._resetReady();
|
|
1024
|
-
//
|
|
1025
|
-
|
|
1117
|
+
// Stop tracking the element's size
|
|
1118
|
+
this._resizeObserver?.disconnect();
|
|
1119
|
+
this._resizeObserver = null;
|
|
1026
1120
|
// Remove the canvas
|
|
1027
1121
|
if (this._canvas && this.contains(this._canvas)) {
|
|
1028
1122
|
this.removeChild(this._canvas);
|
|
1029
1123
|
this._canvas = null;
|
|
1030
1124
|
}
|
|
1031
1125
|
}
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1126
|
+
/**
|
|
1127
|
+
* Syncs the drawing buffer and the picker to the canvas's current CSS size. The picker must
|
|
1128
|
+
* track the buffer, or picks would land at stale coordinates after a resize. Skipped while
|
|
1129
|
+
* an XR session presents - the session owns the buffer size.
|
|
1130
|
+
*/
|
|
1131
|
+
_syncCanvasSize() {
|
|
1132
|
+
if (!this.app || this.app.xr?.active) {
|
|
1133
|
+
return;
|
|
1035
1134
|
}
|
|
1135
|
+
this.app.updateCanvasSize();
|
|
1136
|
+
const { width, height } = this.app.graphicsDevice;
|
|
1137
|
+
this._picker?.resize(width, height);
|
|
1036
1138
|
}
|
|
1037
1139
|
_pickerCreate() {
|
|
1038
1140
|
const { width, height } = this.app.graphicsDevice;
|
|
@@ -1052,7 +1154,7 @@
|
|
|
1052
1154
|
// created from onpointer* attributes when their elements were first upgraded, or
|
|
1053
1155
|
// listeners carried over from before a re-boot)
|
|
1054
1156
|
pointerEventTypes.forEach((type) => {
|
|
1055
|
-
const anyListeners = Array.from(this.querySelectorAll('pc-entity')).some((entity) => entity._hasListeners(type));
|
|
1157
|
+
const anyListeners = Array.from(this.querySelectorAll('pc-entity, pc-node')).some((entity) => entity._hasListeners(type));
|
|
1056
1158
|
if (anyListeners) {
|
|
1057
1159
|
this._onPointerListenerAdded(type);
|
|
1058
1160
|
}
|
|
@@ -1082,11 +1184,11 @@
|
|
|
1082
1184
|
};
|
|
1083
1185
|
}
|
|
1084
1186
|
/**
|
|
1085
|
-
* Registers the element that
|
|
1086
|
-
* entity.
|
|
1187
|
+
* Registers the element that fronts an entity. Called by EntityElement when it creates its
|
|
1188
|
+
* entity, and by NodeElement when it binds one.
|
|
1087
1189
|
*
|
|
1088
1190
|
* @param entity - The entity.
|
|
1089
|
-
* @param element - The element that
|
|
1191
|
+
* @param element - The element that fronts it.
|
|
1090
1192
|
* @internal
|
|
1091
1193
|
*/
|
|
1092
1194
|
_registerEntityElement(entity, element) {
|
|
@@ -1102,21 +1204,22 @@
|
|
|
1102
1204
|
this._entityElements.delete(entity);
|
|
1103
1205
|
}
|
|
1104
1206
|
/**
|
|
1105
|
-
* Returns the `<pc-entity>` element whose backing entity is `entity`, or
|
|
1106
|
-
* entity
|
|
1107
|
-
* model's instantiated hierarchy, or an entity created through the
|
|
1207
|
+
* Returns the `<pc-entity>` or `<pc-node>` element whose backing entity is `entity`, or
|
|
1208
|
+
* `null` if the entity is not fronted by an element of this application - for example, an
|
|
1209
|
+
* unbound node inside a model's instantiated hierarchy, or an entity created through the
|
|
1210
|
+
* engine API.
|
|
1108
1211
|
*
|
|
1109
1212
|
* @param entity - The entity to look up.
|
|
1110
|
-
* @returns The element
|
|
1213
|
+
* @returns The element fronting the entity, or `null`.
|
|
1111
1214
|
*/
|
|
1112
1215
|
elementFromEntity(entity) {
|
|
1113
1216
|
return this._entityElements.get(entity) ?? null;
|
|
1114
1217
|
}
|
|
1115
1218
|
/**
|
|
1116
1219
|
* Resolves the element that owns a picked node: the nearest node up the parent chain -
|
|
1117
|
-
* starting with the node itself - that
|
|
1118
|
-
* A hit inside a model's instantiated hierarchy therefore resolves to the
|
|
1119
|
-
* the model.
|
|
1220
|
+
* starting with the node itself - that is fronted by a `<pc-entity>` or `<pc-node>` of this
|
|
1221
|
+
* application. A hit inside a model's instantiated hierarchy therefore resolves to the
|
|
1222
|
+
* nearest bound `<pc-node>`, or failing that the element hosting the model.
|
|
1120
1223
|
*
|
|
1121
1224
|
* @param node - The picked node, or `null`.
|
|
1122
1225
|
* @returns The owning element, or `null`.
|
|
@@ -1248,7 +1351,7 @@
|
|
|
1248
1351
|
}
|
|
1249
1352
|
}
|
|
1250
1353
|
_onPointerListenerRemoved(type) {
|
|
1251
|
-
const hasListeners = Array.from(this.querySelectorAll('pc-entity')).some((entity) => entity._hasListeners(type));
|
|
1354
|
+
const hasListeners = Array.from(this.querySelectorAll('pc-entity, pc-node')).some((entity) => entity._hasListeners(type));
|
|
1252
1355
|
if (!hasListeners && this._canvas) {
|
|
1253
1356
|
this._hasPointerListeners[type] = false;
|
|
1254
1357
|
const handler = type === 'pointerenter' || type === 'pointerleave'
|
|
@@ -1369,7 +1472,7 @@
|
|
|
1369
1472
|
this._maxPixelRatio = value;
|
|
1370
1473
|
if (this.app) {
|
|
1371
1474
|
this.app.graphicsDevice.maxPixelRatio = value;
|
|
1372
|
-
this.
|
|
1475
|
+
this._syncCanvasSize();
|
|
1373
1476
|
}
|
|
1374
1477
|
}
|
|
1375
1478
|
/**
|
|
@@ -1426,6 +1529,125 @@
|
|
|
1426
1529
|
}
|
|
1427
1530
|
customElements.define('pc-app', AppElement);
|
|
1428
1531
|
|
|
1532
|
+
/**
|
|
1533
|
+
* The attribute names of the inline `onpointer*` event handlers, shared by every element that
|
|
1534
|
+
* fronts an engine entity. Spread into `observedAttributes` by subclasses.
|
|
1535
|
+
* @ignore
|
|
1536
|
+
*/
|
|
1537
|
+
const POINTER_ATTRIBUTES = [
|
|
1538
|
+
'onpointerenter',
|
|
1539
|
+
'onpointerleave',
|
|
1540
|
+
'onpointerdown',
|
|
1541
|
+
'onpointerup',
|
|
1542
|
+
'onpointermove'
|
|
1543
|
+
];
|
|
1544
|
+
/**
|
|
1545
|
+
* The base class for elements that front an engine {@link Entity}: `<pc-entity>`, which creates
|
|
1546
|
+
* one, and `<pc-node>`, which binds to one inside a model's instantiated hierarchy. It carries
|
|
1547
|
+
* what both need — the `entity` contract, registration with the owning application (which joins
|
|
1548
|
+
* picked scene nodes back to elements by identity, never by name), and the pointer listener
|
|
1549
|
+
* bookkeeping that lets the application lazily attach its canvas handlers.
|
|
1550
|
+
*/
|
|
1551
|
+
class EntityBaseElement extends AsyncElement {
|
|
1552
|
+
_entity = null;
|
|
1553
|
+
/**
|
|
1554
|
+
* The application element this entity is registered with, cached at registration time so the
|
|
1555
|
+
* entity can be unregistered even once this element has left the DOM.
|
|
1556
|
+
*/
|
|
1557
|
+
_appElement = null;
|
|
1558
|
+
/**
|
|
1559
|
+
* The pointer event listeners for the entity.
|
|
1560
|
+
*/
|
|
1561
|
+
_listeners = {};
|
|
1562
|
+
/**
|
|
1563
|
+
* The event types for which an inline `onpointer*` attribute is currently present.
|
|
1564
|
+
*/
|
|
1565
|
+
_inlineHandlerTypes = new Set();
|
|
1566
|
+
/**
|
|
1567
|
+
* The PlayCanvas entity instance. `null` until the element is ready, and again once the
|
|
1568
|
+
* entity is gone — await {@link whenReady} or the element's `ready()` promise before
|
|
1569
|
+
* accessing it.
|
|
1570
|
+
* @returns The entity instance, or `null`.
|
|
1571
|
+
*/
|
|
1572
|
+
get entity() {
|
|
1573
|
+
return this._entity;
|
|
1574
|
+
}
|
|
1575
|
+
/**
|
|
1576
|
+
* Registers `entity` as this element's backing entity with the owning application, which
|
|
1577
|
+
* joins engine nodes back to elements by identity (never by name).
|
|
1578
|
+
*
|
|
1579
|
+
* @param entity - The entity to register.
|
|
1580
|
+
*/
|
|
1581
|
+
_registerEntity(entity) {
|
|
1582
|
+
this._appElement = this.closestApp;
|
|
1583
|
+
this._appElement?._registerEntityElement(entity, this);
|
|
1584
|
+
}
|
|
1585
|
+
/**
|
|
1586
|
+
* Removes the registration for `entity`.
|
|
1587
|
+
*
|
|
1588
|
+
* @param entity - The entity to unregister.
|
|
1589
|
+
*/
|
|
1590
|
+
_unregisterEntity(entity) {
|
|
1591
|
+
this._appElement?._unregisterEntityElement(entity);
|
|
1592
|
+
this._appElement = null;
|
|
1593
|
+
}
|
|
1594
|
+
/**
|
|
1595
|
+
* Tracks whether an inline `onpointer*` attribute is present. The browser itself compiles and
|
|
1596
|
+
* runs these attributes — they are standard `GlobalEventHandlers`, so setting one replaces
|
|
1597
|
+
* the previous handler and removing it removes the handler, exactly like `onclick` on any
|
|
1598
|
+
* HTML element. But because they bypass {@link addEventListener}, the connect/disconnect
|
|
1599
|
+
* bookkeeping that lets the application lazily attach its canvas pointer handlers must be
|
|
1600
|
+
* kept in sync here.
|
|
1601
|
+
*
|
|
1602
|
+
* @param name - The attribute name (e.g. 'onpointerdown').
|
|
1603
|
+
* @param value - The attribute value, or `null` when the attribute has been removed.
|
|
1604
|
+
*/
|
|
1605
|
+
_updateInlineHandler(name, value) {
|
|
1606
|
+
const type = name.substring(2);
|
|
1607
|
+
const had = this._inlineHandlerTypes.has(type);
|
|
1608
|
+
const has = value !== null;
|
|
1609
|
+
if (has && !had) {
|
|
1610
|
+
this._inlineHandlerTypes.add(type);
|
|
1611
|
+
this.dispatchEvent(new CustomEvent(`${type}:connect`, { bubbles: true }));
|
|
1612
|
+
}
|
|
1613
|
+
else if (!has && had) {
|
|
1614
|
+
this._inlineHandlerTypes.delete(type);
|
|
1615
|
+
this.dispatchEvent(new CustomEvent(`${type}:disconnect`, { bubbles: true }));
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
addEventListener(type, listener, options) {
|
|
1619
|
+
if (!this._listeners[type]) {
|
|
1620
|
+
this._listeners[type] = [];
|
|
1621
|
+
}
|
|
1622
|
+
this._listeners[type].push(listener);
|
|
1623
|
+
super.addEventListener(type, listener, options);
|
|
1624
|
+
if (type.startsWith('pointer')) {
|
|
1625
|
+
this.dispatchEvent(new CustomEvent(`${type}:connect`, { bubbles: true }));
|
|
1626
|
+
}
|
|
1627
|
+
}
|
|
1628
|
+
removeEventListener(type, listener, options) {
|
|
1629
|
+
if (this._listeners[type]) {
|
|
1630
|
+
this._listeners[type] = this._listeners[type].filter((l) => l !== listener);
|
|
1631
|
+
}
|
|
1632
|
+
super.removeEventListener(type, listener, options);
|
|
1633
|
+
if (type.startsWith('pointer')) {
|
|
1634
|
+
this.dispatchEvent(new CustomEvent(`${type}:disconnect`, { bubbles: true }));
|
|
1635
|
+
}
|
|
1636
|
+
}
|
|
1637
|
+
/**
|
|
1638
|
+
* Whether the element has a listener for an event type, registered either with
|
|
1639
|
+
* {@link addEventListener} or with the matching inline `onpointer*` attribute. Read by the
|
|
1640
|
+
* containing `<pc-app>` element to gate pointer event synthesis.
|
|
1641
|
+
*
|
|
1642
|
+
* @param type - The event type.
|
|
1643
|
+
* @returns Whether a listener is registered.
|
|
1644
|
+
* @internal
|
|
1645
|
+
*/
|
|
1646
|
+
_hasListeners(type) {
|
|
1647
|
+
return Boolean(this._listeners[type]?.length) || this._inlineHandlerTypes.has(type);
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
|
|
1429
1651
|
/**
|
|
1430
1652
|
* The EntityElement interface provides properties and methods for manipulating
|
|
1431
1653
|
* {@link https://developer.playcanvas.com/user-manual/web-components/tags/pc-entity/ | `<pc-entity>`} elements.
|
|
@@ -1450,7 +1672,7 @@
|
|
|
1450
1672
|
* @fires {PointerEvent} pointerdown - Fired when a pointer button is pressed over the entity.
|
|
1451
1673
|
* @fires {PointerEvent} pointerup - Fired when a pointer button is released over the entity.
|
|
1452
1674
|
*/
|
|
1453
|
-
class EntityElement extends
|
|
1675
|
+
class EntityElement extends EntityBaseElement {
|
|
1454
1676
|
/**
|
|
1455
1677
|
* Whether the entity is enabled.
|
|
1456
1678
|
*/
|
|
@@ -1475,33 +1697,10 @@
|
|
|
1475
1697
|
* The tags of the entity.
|
|
1476
1698
|
*/
|
|
1477
1699
|
_tags = [];
|
|
1478
|
-
/**
|
|
1479
|
-
* The pointer event listeners for the entity.
|
|
1480
|
-
*/
|
|
1481
|
-
_listeners = {};
|
|
1482
|
-
/**
|
|
1483
|
-
* The event types for which an inline `onpointer*` attribute is currently present.
|
|
1484
|
-
*/
|
|
1485
|
-
_inlineHandlerTypes = new Set();
|
|
1486
1700
|
/**
|
|
1487
1701
|
* Whether the hierarchy has been built for this entity.
|
|
1488
1702
|
*/
|
|
1489
1703
|
_built = false;
|
|
1490
|
-
_entity = null;
|
|
1491
|
-
/**
|
|
1492
|
-
* The application element this entity is registered with, cached at creation time so the
|
|
1493
|
-
* entity can be unregistered even once this element has left the DOM.
|
|
1494
|
-
*/
|
|
1495
|
-
_appElement = null;
|
|
1496
|
-
/**
|
|
1497
|
-
* The PlayCanvas entity instance. `null` until the element is ready, and again once it has
|
|
1498
|
-
* been removed from the document — await {@link whenReady} or the element's `ready()`
|
|
1499
|
-
* promise before accessing it.
|
|
1500
|
-
* @returns The entity instance, or `null`.
|
|
1501
|
-
*/
|
|
1502
|
-
get entity() {
|
|
1503
|
-
return this._entity;
|
|
1504
|
-
}
|
|
1505
1704
|
/**
|
|
1506
1705
|
* Creates the backing entity. Called by the containing `<pc-app>` element during its boot
|
|
1507
1706
|
* sweep, and on connection for elements inserted while the application is already running.
|
|
@@ -1529,13 +1728,11 @@
|
|
|
1529
1728
|
if (this._tags.length > 0) {
|
|
1530
1729
|
entity.tags.add(this._tags);
|
|
1531
1730
|
}
|
|
1532
|
-
// Register with the owning application
|
|
1533
|
-
//
|
|
1534
|
-
//
|
|
1535
|
-
//
|
|
1536
|
-
|
|
1537
|
-
this._appElement = this.closestApp;
|
|
1538
|
-
this._appElement?._registerEntityElement(entity, this);
|
|
1731
|
+
// Register with the owning application and hook the entity's destruction. The engine
|
|
1732
|
+
// fires 'destroy' for every entity in a destroyed subtree, so the element learns of its
|
|
1733
|
+
// entity's death no matter who causes it: this element, an ancestor, the whole
|
|
1734
|
+
// application, or a user script calling entity.destroy().
|
|
1735
|
+
this._registerEntity(entity);
|
|
1539
1736
|
entity.once('destroy', this._onEntityDestroy, this);
|
|
1540
1737
|
}
|
|
1541
1738
|
/**
|
|
@@ -1548,17 +1745,16 @@
|
|
|
1548
1745
|
* @param entity - The entity that was destroyed.
|
|
1549
1746
|
*/
|
|
1550
1747
|
_onEntityDestroy(entity) {
|
|
1551
|
-
this.
|
|
1552
|
-
this._appElement = null;
|
|
1748
|
+
this._unregisterEntity(entity);
|
|
1553
1749
|
this._entity = null;
|
|
1554
1750
|
this._built = false;
|
|
1555
1751
|
this._resetReady();
|
|
1556
1752
|
}
|
|
1557
1753
|
/**
|
|
1558
|
-
* Parents the backing entity: under the entity of the nearest ancestor `<pc-entity>`
|
|
1559
|
-
* there is one, and under the application root otherwise. Called by the
|
|
1560
|
-
* element once a sweep has created every entity, so a parent's
|
|
1561
|
-
* document order.
|
|
1754
|
+
* Parents the backing entity: under the entity of the nearest ancestor `<pc-entity>` or
|
|
1755
|
+
* `<pc-node>` when there is one, and under the application root otherwise. Called by the
|
|
1756
|
+
* containing `<pc-app>` element once a sweep has created every entity, so a parent's
|
|
1757
|
+
* existence never depends on document order.
|
|
1562
1758
|
*
|
|
1563
1759
|
* @param app - The application whose root adopts parentless entities.
|
|
1564
1760
|
* @internal
|
|
@@ -1566,8 +1762,14 @@
|
|
|
1566
1762
|
_buildHierarchy(app) {
|
|
1567
1763
|
if (!this.entity || this._built)
|
|
1568
1764
|
return;
|
|
1569
|
-
this._built = true;
|
|
1570
1765
|
const closestEntity = this.closestEntity;
|
|
1766
|
+
// A host element without an entity is an unresolved `<pc-node>`: building now would
|
|
1767
|
+
// mis-anchor this entity to the application root while the host is still resolving.
|
|
1768
|
+
// Stay unbuilt - the host drives this subtree itself once it binds.
|
|
1769
|
+
if (closestEntity && !closestEntity.entity) {
|
|
1770
|
+
return;
|
|
1771
|
+
}
|
|
1772
|
+
this._built = true;
|
|
1571
1773
|
if (closestEntity?.entity) {
|
|
1572
1774
|
closestEntity.entity.addChild(this.entity);
|
|
1573
1775
|
}
|
|
@@ -1713,44 +1915,8 @@
|
|
|
1713
1915
|
get tags() {
|
|
1714
1916
|
return this._tags;
|
|
1715
1917
|
}
|
|
1716
|
-
/**
|
|
1717
|
-
* Tracks whether an inline `onpointer*` attribute is present. The browser itself compiles and
|
|
1718
|
-
* runs these attributes — they are standard `GlobalEventHandlers`, so setting one replaces
|
|
1719
|
-
* the previous handler and removing it removes the handler, exactly like `onclick` on any
|
|
1720
|
-
* HTML element. But because they bypass {@link addEventListener}, the connect/disconnect
|
|
1721
|
-
* bookkeeping that lets the application lazily attach its canvas pointer handlers must be
|
|
1722
|
-
* kept in sync here.
|
|
1723
|
-
*
|
|
1724
|
-
* @param name - The attribute name (e.g. 'onpointerdown').
|
|
1725
|
-
* @param value - The attribute value, or `null` when the attribute has been removed.
|
|
1726
|
-
*/
|
|
1727
|
-
_updateInlineHandler(name, value) {
|
|
1728
|
-
const type = name.substring(2);
|
|
1729
|
-
const had = this._inlineHandlerTypes.has(type);
|
|
1730
|
-
const has = value !== null;
|
|
1731
|
-
if (has && !had) {
|
|
1732
|
-
this._inlineHandlerTypes.add(type);
|
|
1733
|
-
this.dispatchEvent(new CustomEvent(`${type}:connect`, { bubbles: true }));
|
|
1734
|
-
}
|
|
1735
|
-
else if (!has && had) {
|
|
1736
|
-
this._inlineHandlerTypes.delete(type);
|
|
1737
|
-
this.dispatchEvent(new CustomEvent(`${type}:disconnect`, { bubbles: true }));
|
|
1738
|
-
}
|
|
1739
|
-
}
|
|
1740
1918
|
static get observedAttributes() {
|
|
1741
|
-
return [
|
|
1742
|
-
'enabled',
|
|
1743
|
-
'name',
|
|
1744
|
-
'position',
|
|
1745
|
-
'rotation',
|
|
1746
|
-
'scale',
|
|
1747
|
-
'tags',
|
|
1748
|
-
'onpointerenter',
|
|
1749
|
-
'onpointerleave',
|
|
1750
|
-
'onpointerdown',
|
|
1751
|
-
'onpointerup',
|
|
1752
|
-
'onpointermove'
|
|
1753
|
-
];
|
|
1919
|
+
return ['enabled', 'name', 'position', 'rotation', 'scale', 'tags', ...POINTER_ATTRIBUTES];
|
|
1754
1920
|
}
|
|
1755
1921
|
attributeChangedCallback(name, _oldValue, newValue) {
|
|
1756
1922
|
switch (name) {
|
|
@@ -1781,37 +1947,6 @@
|
|
|
1781
1947
|
break;
|
|
1782
1948
|
}
|
|
1783
1949
|
}
|
|
1784
|
-
addEventListener(type, listener, options) {
|
|
1785
|
-
if (!this._listeners[type]) {
|
|
1786
|
-
this._listeners[type] = [];
|
|
1787
|
-
}
|
|
1788
|
-
this._listeners[type].push(listener);
|
|
1789
|
-
super.addEventListener(type, listener, options);
|
|
1790
|
-
if (type.startsWith('pointer')) {
|
|
1791
|
-
this.dispatchEvent(new CustomEvent(`${type}:connect`, { bubbles: true }));
|
|
1792
|
-
}
|
|
1793
|
-
}
|
|
1794
|
-
removeEventListener(type, listener, options) {
|
|
1795
|
-
if (this._listeners[type]) {
|
|
1796
|
-
this._listeners[type] = this._listeners[type].filter((l) => l !== listener);
|
|
1797
|
-
}
|
|
1798
|
-
super.removeEventListener(type, listener, options);
|
|
1799
|
-
if (type.startsWith('pointer')) {
|
|
1800
|
-
this.dispatchEvent(new CustomEvent(`${type}:disconnect`, { bubbles: true }));
|
|
1801
|
-
}
|
|
1802
|
-
}
|
|
1803
|
-
/**
|
|
1804
|
-
* Whether the element has a listener for an event type, registered either with
|
|
1805
|
-
* {@link addEventListener} or with the matching inline `onpointer*` attribute. Read by the
|
|
1806
|
-
* containing `<pc-app>` element to gate pointer event synthesis.
|
|
1807
|
-
*
|
|
1808
|
-
* @param type - The event type.
|
|
1809
|
-
* @returns Whether a listener is registered.
|
|
1810
|
-
* @internal
|
|
1811
|
-
*/
|
|
1812
|
-
_hasListeners(type) {
|
|
1813
|
-
return Boolean(this._listeners[type]?.length) || this._inlineHandlerTypes.has(type);
|
|
1814
|
-
}
|
|
1815
1950
|
}
|
|
1816
1951
|
customElements.define('pc-entity', EntityElement);
|
|
1817
1952
|
|
|
@@ -1996,6 +2131,48 @@
|
|
|
1996
2131
|
['sliced', playcanvas.SPRITE_RENDERMODE_SLICED],
|
|
1997
2132
|
['tiled', playcanvas.SPRITE_RENDERMODE_TILED]
|
|
1998
2133
|
]);
|
|
2134
|
+
const addressModes = new Map([
|
|
2135
|
+
['repeat', playcanvas.ADDRESS_REPEAT],
|
|
2136
|
+
['clamp', playcanvas.ADDRESS_CLAMP_TO_EDGE],
|
|
2137
|
+
['mirror', playcanvas.ADDRESS_MIRRORED_REPEAT]
|
|
2138
|
+
]);
|
|
2139
|
+
const minFilterModes = new Map([
|
|
2140
|
+
['nearest', playcanvas.FILTER_NEAREST],
|
|
2141
|
+
['linear', playcanvas.FILTER_LINEAR],
|
|
2142
|
+
['nearest-mip-nearest', playcanvas.FILTER_NEAREST_MIPMAP_NEAREST],
|
|
2143
|
+
['linear-mip-nearest', playcanvas.FILTER_LINEAR_MIPMAP_NEAREST],
|
|
2144
|
+
['nearest-mip-linear', playcanvas.FILTER_NEAREST_MIPMAP_LINEAR],
|
|
2145
|
+
['linear-mip-linear', playcanvas.FILTER_LINEAR_MIPMAP_LINEAR]
|
|
2146
|
+
]);
|
|
2147
|
+
const magFilterModes = new Map([
|
|
2148
|
+
['nearest', playcanvas.FILTER_NEAREST],
|
|
2149
|
+
['linear', playcanvas.FILTER_LINEAR]
|
|
2150
|
+
]);
|
|
2151
|
+
// The engine's texture JSON spells the filter names with underscores ('linear_mip_linear'); the
|
|
2152
|
+
// attribute values are kebab-case like every other enum attribute in this library. The address
|
|
2153
|
+
// mode names contain no dashes, so for them the rename is the identity.
|
|
2154
|
+
const toTextureJson = (name) => name.replace(/-/g, '_');
|
|
2155
|
+
// Engine Texture constructor defaults, restored on a loaded texture when a texture option
|
|
2156
|
+
// attribute is removed.
|
|
2157
|
+
const textureOptionDefaults = {
|
|
2158
|
+
addressU: playcanvas.ADDRESS_REPEAT,
|
|
2159
|
+
addressV: playcanvas.ADDRESS_REPEAT,
|
|
2160
|
+
anisotropy: 1,
|
|
2161
|
+
flipY: false,
|
|
2162
|
+
magFilter: playcanvas.FILTER_LINEAR,
|
|
2163
|
+
minFilter: playcanvas.FILTER_LINEAR_MIPMAP_LINEAR,
|
|
2164
|
+
mipmaps: true,
|
|
2165
|
+
srgb: false
|
|
2166
|
+
};
|
|
2167
|
+
// Attributes that only apply to certain asset types, used to warn when one is set on an asset of
|
|
2168
|
+
// any other type (where it would otherwise be silently ignored).
|
|
2169
|
+
const typeScopedAttributes = [
|
|
2170
|
+
[
|
|
2171
|
+
['address-u', 'address-v', 'anisotropy', 'flip-y', 'mag-filter', 'min-filter', 'mipmaps', 'srgb'],
|
|
2172
|
+
['texture', 'textureatlas']
|
|
2173
|
+
],
|
|
2174
|
+
[['atlas', 'frame-keys', 'pixels-per-unit', 'render-mode'], ['sprite']]
|
|
2175
|
+
];
|
|
1999
2176
|
const extToType = new Map([
|
|
2000
2177
|
['bin', 'binary'],
|
|
2001
2178
|
['css', 'css'],
|
|
@@ -2052,8 +2229,16 @@
|
|
|
2052
2229
|
* immediately unless `lazy`. A `pc-asset` must be a direct child of `pc-app` — elements placed
|
|
2053
2230
|
* elsewhere, or with an unsupported asset type, never become ready.
|
|
2054
2231
|
*
|
|
2055
|
-
*
|
|
2056
|
-
*
|
|
2232
|
+
* For `texture` and `textureatlas` assets, the texture options (`address-u`, `address-v`,
|
|
2233
|
+
* `min-filter`, `mag-filter`, `anisotropy`, `mipmaps`, `srgb`, `flip-y`) apply when the texture is
|
|
2234
|
+
* created and — like `lazy` — are observed: changing one updates a texture that has already
|
|
2235
|
+
* loaded, and removing one restores the engine default. Changing `srgb` or `mipmaps` on a loaded
|
|
2236
|
+
* texture recreates the underlying GPU resource, so prefer declaring those up front. Each option
|
|
2237
|
+
* overrides the matching key in the `data` JSON; options left unset write nothing, leaving the
|
|
2238
|
+
* engine's per-format defaults in force.
|
|
2239
|
+
*
|
|
2240
|
+
* Apart from `lazy` and the texture options, these attributes are read once when the asset is
|
|
2241
|
+
* created, so changing them later has no effect.
|
|
2057
2242
|
*
|
|
2058
2243
|
* @attribute {string} id - The identifier used to reference the asset from other elements.
|
|
2059
2244
|
* @attribute {string} src - The URL of the asset to load.
|
|
@@ -2075,7 +2260,15 @@
|
|
|
2075
2260
|
* not that it succeeded.
|
|
2076
2261
|
*/
|
|
2077
2262
|
class AssetElement extends AsyncElement {
|
|
2263
|
+
_addressU = null;
|
|
2264
|
+
_addressV = null;
|
|
2265
|
+
_anisotropy = null;
|
|
2266
|
+
_flipY = null;
|
|
2078
2267
|
_lazy = false;
|
|
2268
|
+
_magFilter = null;
|
|
2269
|
+
_minFilter = null;
|
|
2270
|
+
_mipmaps = null;
|
|
2271
|
+
_srgb = null;
|
|
2079
2272
|
/**
|
|
2080
2273
|
* The asset that is loaded. Available once the element is ready — await
|
|
2081
2274
|
* {@link whenReady} or the element's `ready()` promise before accessing it.
|
|
@@ -2146,6 +2339,15 @@
|
|
|
2146
2339
|
console.warn(`Unsupported asset type: ${src}`);
|
|
2147
2340
|
return;
|
|
2148
2341
|
}
|
|
2342
|
+
// Attributes scoped to other asset types have no effect here - say so rather than
|
|
2343
|
+
// failing silently.
|
|
2344
|
+
const inapplicable = typeScopedAttributes
|
|
2345
|
+
.filter(([, types]) => !types.includes(type))
|
|
2346
|
+
.flatMap(([attributes]) => attributes)
|
|
2347
|
+
.filter((attribute) => this.hasAttribute(attribute));
|
|
2348
|
+
if (inapplicable.length > 0) {
|
|
2349
|
+
console.warn(`pc-asset '${id || src}' has attributes that do not apply to asset type '${type}' and are ignored: ${inapplicable.join(', ')}`);
|
|
2350
|
+
}
|
|
2149
2351
|
// Optional inline asset data, used by data-driven assets such as texture atlases (frame
|
|
2150
2352
|
// definitions) and sprites (atlas reference, frame keys, etc.).
|
|
2151
2353
|
const data = this._buildData(type);
|
|
@@ -2173,9 +2375,11 @@
|
|
|
2173
2375
|
this.asset.on('error', this._onAssetError, this);
|
|
2174
2376
|
}
|
|
2175
2377
|
/**
|
|
2176
|
-
* Builds the `data` object for the asset from an optional inline `data` attribute (JSON)
|
|
2177
|
-
*
|
|
2178
|
-
* `render-mode`).
|
|
2378
|
+
* Builds the `data` object for the asset from an optional inline `data` attribute (JSON), the
|
|
2379
|
+
* texture option attributes (for `texture` and `textureatlas` assets), and the sprite
|
|
2380
|
+
* convenience attributes (`atlas`, `frame-keys`, `pixels-per-unit`, `render-mode`). An
|
|
2381
|
+
* attribute overrides the matching `data` JSON key. Returns `undefined` when there is no data
|
|
2382
|
+
* to apply.
|
|
2179
2383
|
* @param type - The resolved asset type.
|
|
2180
2384
|
* @returns The asset data, or `undefined`.
|
|
2181
2385
|
*/
|
|
@@ -2190,6 +2394,37 @@
|
|
|
2190
2394
|
console.warn(`Invalid 'data' JSON on pc-asset: ${dataAttr}`);
|
|
2191
2395
|
}
|
|
2192
2396
|
}
|
|
2397
|
+
if (type === 'texture' || type === 'textureatlas') {
|
|
2398
|
+
data = data ?? {};
|
|
2399
|
+
// Only options the user actually set are written: the engine reads these keys with
|
|
2400
|
+
// hasOwnProperty semantics, and an absent key leaves its per-format default (an HDR's
|
|
2401
|
+
// 'rgbe' type, a KTX2's transcoded format) in force.
|
|
2402
|
+
if (this._addressU !== null) {
|
|
2403
|
+
data.addressu = this._addressU;
|
|
2404
|
+
}
|
|
2405
|
+
if (this._addressV !== null) {
|
|
2406
|
+
data.addressv = this._addressV;
|
|
2407
|
+
}
|
|
2408
|
+
if (this._anisotropy !== null) {
|
|
2409
|
+
data.anisotropy = this._anisotropy;
|
|
2410
|
+
}
|
|
2411
|
+
if (this._flipY !== null) {
|
|
2412
|
+
// 'flipY' is the one camelCase key in the engine's texture JSON
|
|
2413
|
+
data.flipY = this._flipY;
|
|
2414
|
+
}
|
|
2415
|
+
if (this._magFilter !== null) {
|
|
2416
|
+
data.magfilter = toTextureJson(this._magFilter);
|
|
2417
|
+
}
|
|
2418
|
+
if (this._minFilter !== null) {
|
|
2419
|
+
data.minfilter = toTextureJson(this._minFilter);
|
|
2420
|
+
}
|
|
2421
|
+
if (this._mipmaps !== null) {
|
|
2422
|
+
data.mipmaps = this._mipmaps;
|
|
2423
|
+
}
|
|
2424
|
+
if (this._srgb !== null) {
|
|
2425
|
+
data.srgb = this._srgb;
|
|
2426
|
+
}
|
|
2427
|
+
}
|
|
2193
2428
|
if (type === 'sprite') {
|
|
2194
2429
|
data = data ?? {};
|
|
2195
2430
|
// Resolve the referenced texture atlas to its (numeric) asset id. The atlas must be
|
|
@@ -2223,6 +2458,56 @@
|
|
|
2223
2458
|
}
|
|
2224
2459
|
return data;
|
|
2225
2460
|
}
|
|
2461
|
+
/**
|
|
2462
|
+
* Returns the engine texture behind this asset, when there is one: the resource itself for a
|
|
2463
|
+
* `texture` asset, the atlas's texture for a `textureatlas` asset, `null` otherwise
|
|
2464
|
+
* (including before the asset has loaded).
|
|
2465
|
+
* @returns The texture, or `null`.
|
|
2466
|
+
*/
|
|
2467
|
+
_texture() {
|
|
2468
|
+
const asset = this.asset;
|
|
2469
|
+
if (!asset?.resource)
|
|
2470
|
+
return null;
|
|
2471
|
+
if (asset.type === 'texture')
|
|
2472
|
+
return asset.resource;
|
|
2473
|
+
if (asset.type === 'textureatlas')
|
|
2474
|
+
return asset.resource.texture ?? null;
|
|
2475
|
+
return null;
|
|
2476
|
+
}
|
|
2477
|
+
/**
|
|
2478
|
+
* Writes one texture option through to the created asset, if any. The engine-JSON key is
|
|
2479
|
+
* written into `asset.data`, mutated in place - replacing the whole object would make the
|
|
2480
|
+
* registry re-patch every key, and a re-patched `srgb` or `mipmaps` recreates the texture
|
|
2481
|
+
* even when unchanged. The in-place key is what a not-yet-started load reads at texture
|
|
2482
|
+
* construction, and what any later reload reads. When the texture already exists, the
|
|
2483
|
+
* corresponding property is assigned directly; `null` (attribute removed) deletes the key
|
|
2484
|
+
* and restores the engine default. Assets of any other type are left untouched.
|
|
2485
|
+
*
|
|
2486
|
+
* @param key - The engine texture JSON key in `asset.data`.
|
|
2487
|
+
* @param property - The Texture property to assign.
|
|
2488
|
+
* @param dataValue - The engine-JSON value for `asset.data`, or `null` to delete the key.
|
|
2489
|
+
* @param textureValue - The value for the Texture property, or `null` for the engine default.
|
|
2490
|
+
*/
|
|
2491
|
+
_applyTextureOption(key, property, dataValue, textureValue) {
|
|
2492
|
+
const asset = this.asset;
|
|
2493
|
+
if (!asset || (asset.type !== 'texture' && asset.type !== 'textureatlas'))
|
|
2494
|
+
return;
|
|
2495
|
+
const data = asset.data;
|
|
2496
|
+
if (dataValue === null) {
|
|
2497
|
+
delete data[key];
|
|
2498
|
+
}
|
|
2499
|
+
else {
|
|
2500
|
+
data[key] = dataValue;
|
|
2501
|
+
}
|
|
2502
|
+
const texture = this._texture();
|
|
2503
|
+
if (texture) {
|
|
2504
|
+
// Every option here is a number- or boolean-valued Texture property; the
|
|
2505
|
+
// value/property pairing is fixed by the callers, which TypeScript cannot see
|
|
2506
|
+
// through the union.
|
|
2507
|
+
texture[property] =
|
|
2508
|
+
textureValue ?? textureOptionDefaults[property];
|
|
2509
|
+
}
|
|
2510
|
+
}
|
|
2226
2511
|
_destroyAsset() {
|
|
2227
2512
|
if (this.asset) {
|
|
2228
2513
|
// A caller that keeps the Asset alive must not dispatch on a removed element
|
|
@@ -2235,22 +2520,164 @@
|
|
|
2235
2520
|
}
|
|
2236
2521
|
}
|
|
2237
2522
|
/**
|
|
2238
|
-
* Sets
|
|
2239
|
-
*
|
|
2523
|
+
* Sets the texture's horizontal (U) address mode: how texture coordinates outside the 0 to 1
|
|
2524
|
+
* range sample the texture. Applies to `texture` and `textureatlas` assets, both when the
|
|
2525
|
+
* texture is created and after it has loaded.
|
|
2526
|
+
* @param value - The address mode, or `null` to use the engine default of 'repeat'.
|
|
2240
2527
|
*/
|
|
2241
|
-
set
|
|
2242
|
-
this.
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
}
|
|
2528
|
+
set addressU(value) {
|
|
2529
|
+
this._addressU = value;
|
|
2530
|
+
const constant = value === null ? null : (addressModes.get(value) ?? playcanvas.ADDRESS_REPEAT);
|
|
2531
|
+
this._applyTextureOption('addressu', 'addressU', value, constant);
|
|
2246
2532
|
}
|
|
2247
2533
|
/**
|
|
2248
|
-
* Gets
|
|
2249
|
-
* @returns The
|
|
2534
|
+
* Gets the texture's horizontal (U) address mode.
|
|
2535
|
+
* @returns The address mode, or `null` when unset.
|
|
2250
2536
|
*/
|
|
2251
|
-
get
|
|
2537
|
+
get addressU() {
|
|
2538
|
+
return this._addressU;
|
|
2539
|
+
}
|
|
2540
|
+
/**
|
|
2541
|
+
* Sets the texture's vertical (V) address mode: how texture coordinates outside the 0 to 1
|
|
2542
|
+
* range sample the texture. Applies to `texture` and `textureatlas` assets, both when the
|
|
2543
|
+
* texture is created and after it has loaded.
|
|
2544
|
+
* @param value - The address mode, or `null` to use the engine default of 'repeat'.
|
|
2545
|
+
*/
|
|
2546
|
+
set addressV(value) {
|
|
2547
|
+
this._addressV = value;
|
|
2548
|
+
const constant = value === null ? null : (addressModes.get(value) ?? playcanvas.ADDRESS_REPEAT);
|
|
2549
|
+
this._applyTextureOption('addressv', 'addressV', value, constant);
|
|
2550
|
+
}
|
|
2551
|
+
/**
|
|
2552
|
+
* Gets the texture's vertical (V) address mode.
|
|
2553
|
+
* @returns The address mode, or `null` when unset.
|
|
2554
|
+
*/
|
|
2555
|
+
get addressV() {
|
|
2556
|
+
return this._addressV;
|
|
2557
|
+
}
|
|
2558
|
+
/**
|
|
2559
|
+
* Sets the texture's maximum anisotropic filtering level, which improves quality at oblique
|
|
2560
|
+
* viewing angles. Applies to `texture` and `textureatlas` assets, both when the texture is
|
|
2561
|
+
* created and after it has loaded.
|
|
2562
|
+
* @param value - The anisotropy level, or `null` to use the engine default of 1.
|
|
2563
|
+
*/
|
|
2564
|
+
set anisotropy(value) {
|
|
2565
|
+
this._anisotropy = value;
|
|
2566
|
+
this._applyTextureOption('anisotropy', 'anisotropy', value, value);
|
|
2567
|
+
}
|
|
2568
|
+
/**
|
|
2569
|
+
* Gets the texture's maximum anisotropic filtering level.
|
|
2570
|
+
* @returns The anisotropy level, or `null` when unset.
|
|
2571
|
+
*/
|
|
2572
|
+
get anisotropy() {
|
|
2573
|
+
return this._anisotropy;
|
|
2574
|
+
}
|
|
2575
|
+
/**
|
|
2576
|
+
* Sets whether the texture's image data is flipped vertically at upload. Applies to `texture`
|
|
2577
|
+
* and `textureatlas` assets, both when the texture is created and after it has loaded.
|
|
2578
|
+
* @param value - The flip flag, or `null` to use the engine default of `false`.
|
|
2579
|
+
*/
|
|
2580
|
+
set flipY(value) {
|
|
2581
|
+
this._flipY = value;
|
|
2582
|
+
this._applyTextureOption('flipY', 'flipY', value, value);
|
|
2583
|
+
}
|
|
2584
|
+
/**
|
|
2585
|
+
* Gets whether the texture's image data is flipped vertically at upload.
|
|
2586
|
+
* @returns The flip flag, or `null` when unset.
|
|
2587
|
+
*/
|
|
2588
|
+
get flipY() {
|
|
2589
|
+
return this._flipY;
|
|
2590
|
+
}
|
|
2591
|
+
/**
|
|
2592
|
+
* Sets whether the asset should be loaded lazily.
|
|
2593
|
+
* @param value - The lazy loading flag.
|
|
2594
|
+
*/
|
|
2595
|
+
set lazy(value) {
|
|
2596
|
+
this._lazy = value;
|
|
2597
|
+
if (this.asset) {
|
|
2598
|
+
this.asset.preload = !value;
|
|
2599
|
+
}
|
|
2600
|
+
}
|
|
2601
|
+
/**
|
|
2602
|
+
* Gets whether the asset should be loaded lazily.
|
|
2603
|
+
* @returns The lazy loading flag.
|
|
2604
|
+
*/
|
|
2605
|
+
get lazy() {
|
|
2252
2606
|
return this._lazy;
|
|
2253
2607
|
}
|
|
2608
|
+
/**
|
|
2609
|
+
* Sets the texture's magnification filter, used when the texture is displayed larger than its
|
|
2610
|
+
* source size. Applies to `texture` and `textureatlas` assets, both when the texture is
|
|
2611
|
+
* created and after it has loaded.
|
|
2612
|
+
* @param value - The filter, or `null` to use the engine default of 'linear'.
|
|
2613
|
+
*/
|
|
2614
|
+
set magFilter(value) {
|
|
2615
|
+
this._magFilter = value;
|
|
2616
|
+
const json = value === null ? null : toTextureJson(value);
|
|
2617
|
+
const constant = value === null ? null : (magFilterModes.get(value) ?? playcanvas.FILTER_LINEAR);
|
|
2618
|
+
this._applyTextureOption('magfilter', 'magFilter', json, constant);
|
|
2619
|
+
}
|
|
2620
|
+
/**
|
|
2621
|
+
* Gets the texture's magnification filter.
|
|
2622
|
+
* @returns The filter, or `null` when unset.
|
|
2623
|
+
*/
|
|
2624
|
+
get magFilter() {
|
|
2625
|
+
return this._magFilter;
|
|
2626
|
+
}
|
|
2627
|
+
/**
|
|
2628
|
+
* Sets the texture's minification filter, used when the texture is displayed smaller than its
|
|
2629
|
+
* source size. The mip variants blend within (and, for the second `linear`, between) mipmap
|
|
2630
|
+
* levels. Applies to `texture` and `textureatlas` assets, both when the texture is created
|
|
2631
|
+
* and after it has loaded.
|
|
2632
|
+
* @param value - The filter, or `null` to use the engine default of 'linear-mip-linear'.
|
|
2633
|
+
*/
|
|
2634
|
+
set minFilter(value) {
|
|
2635
|
+
this._minFilter = value;
|
|
2636
|
+
const json = value === null ? null : toTextureJson(value);
|
|
2637
|
+
const constant = value === null ? null : (minFilterModes.get(value) ?? playcanvas.FILTER_LINEAR_MIPMAP_LINEAR);
|
|
2638
|
+
this._applyTextureOption('minfilter', 'minFilter', json, constant);
|
|
2639
|
+
}
|
|
2640
|
+
/**
|
|
2641
|
+
* Gets the texture's minification filter.
|
|
2642
|
+
* @returns The filter, or `null` when unset.
|
|
2643
|
+
*/
|
|
2644
|
+
get minFilter() {
|
|
2645
|
+
return this._minFilter;
|
|
2646
|
+
}
|
|
2647
|
+
/**
|
|
2648
|
+
* Sets whether the texture generates and uses mipmaps. Changing this on a loaded texture
|
|
2649
|
+
* recreates the underlying GPU resource, so prefer declaring it up front. Applies to
|
|
2650
|
+
* `texture` and `textureatlas` assets.
|
|
2651
|
+
* @param value - The mipmaps flag, or `null` to use the engine default of `true`.
|
|
2652
|
+
*/
|
|
2653
|
+
set mipmaps(value) {
|
|
2654
|
+
this._mipmaps = value;
|
|
2655
|
+
this._applyTextureOption('mipmaps', 'mipmaps', value, value);
|
|
2656
|
+
}
|
|
2657
|
+
/**
|
|
2658
|
+
* Gets whether the texture generates and uses mipmaps.
|
|
2659
|
+
* @returns The mipmaps flag, or `null` when unset.
|
|
2660
|
+
*/
|
|
2661
|
+
get mipmaps() {
|
|
2662
|
+
return this._mipmaps;
|
|
2663
|
+
}
|
|
2664
|
+
/**
|
|
2665
|
+
* Sets whether the texture holds sRGB (gamma-encoded) color data, enabling hardware gamma
|
|
2666
|
+
* decode. Free when set before the texture loads; changing it on a loaded texture recreates
|
|
2667
|
+
* the underlying GPU resource. Applies to `texture` and `textureatlas` assets.
|
|
2668
|
+
* @param value - The sRGB flag, or `null` to use the engine default of `false`.
|
|
2669
|
+
*/
|
|
2670
|
+
set srgb(value) {
|
|
2671
|
+
this._srgb = value;
|
|
2672
|
+
this._applyTextureOption('srgb', 'srgb', value, value);
|
|
2673
|
+
}
|
|
2674
|
+
/**
|
|
2675
|
+
* Gets whether the texture holds sRGB (gamma-encoded) color data.
|
|
2676
|
+
* @returns The sRGB flag, or `null` when unset.
|
|
2677
|
+
*/
|
|
2678
|
+
get srgb() {
|
|
2679
|
+
return this._srgb;
|
|
2680
|
+
}
|
|
2254
2681
|
/**
|
|
2255
2682
|
* Returns the {@link Asset} created by the `<pc-asset>` element with the given `id`, or
|
|
2256
2683
|
* `undefined` if there is no such element or its asset has not been created yet.
|
|
@@ -2263,11 +2690,91 @@
|
|
|
2263
2690
|
return assetElement?.asset;
|
|
2264
2691
|
}
|
|
2265
2692
|
static get observedAttributes() {
|
|
2266
|
-
return [
|
|
2693
|
+
return [
|
|
2694
|
+
'address-u',
|
|
2695
|
+
'address-v',
|
|
2696
|
+
'anisotropy',
|
|
2697
|
+
'flip-y',
|
|
2698
|
+
'lazy',
|
|
2699
|
+
'mag-filter',
|
|
2700
|
+
'min-filter',
|
|
2701
|
+
'mipmaps',
|
|
2702
|
+
'srgb'
|
|
2703
|
+
];
|
|
2267
2704
|
}
|
|
2268
2705
|
attributeChangedCallback(name, _oldValue, newValue) {
|
|
2269
|
-
|
|
2270
|
-
|
|
2706
|
+
// Each texture option keeps its parse* call as the branch's first assignment (the CEM
|
|
2707
|
+
// manifest derives the attribute's type and default from it - a ternary would degrade
|
|
2708
|
+
// both to plain string) and treats a removed attribute (null) as a reset to unset,
|
|
2709
|
+
// which restores the engine default on a loaded texture.
|
|
2710
|
+
switch (name) {
|
|
2711
|
+
case 'address-u':
|
|
2712
|
+
if (newValue !== null) {
|
|
2713
|
+
this.addressU = parseEnum(newValue, addressModes, 'repeat', name);
|
|
2714
|
+
}
|
|
2715
|
+
else {
|
|
2716
|
+
this.addressU = null;
|
|
2717
|
+
}
|
|
2718
|
+
break;
|
|
2719
|
+
case 'address-v':
|
|
2720
|
+
if (newValue !== null) {
|
|
2721
|
+
this.addressV = parseEnum(newValue, addressModes, 'repeat', name);
|
|
2722
|
+
}
|
|
2723
|
+
else {
|
|
2724
|
+
this.addressV = null;
|
|
2725
|
+
}
|
|
2726
|
+
break;
|
|
2727
|
+
case 'anisotropy':
|
|
2728
|
+
if (newValue !== null) {
|
|
2729
|
+
this.anisotropy = parseNumber(newValue, 1, name);
|
|
2730
|
+
}
|
|
2731
|
+
else {
|
|
2732
|
+
this.anisotropy = null;
|
|
2733
|
+
}
|
|
2734
|
+
break;
|
|
2735
|
+
case 'flip-y':
|
|
2736
|
+
if (newValue !== null) {
|
|
2737
|
+
this.flipY = parseBool(newValue, false);
|
|
2738
|
+
}
|
|
2739
|
+
else {
|
|
2740
|
+
this.flipY = null;
|
|
2741
|
+
}
|
|
2742
|
+
break;
|
|
2743
|
+
case 'lazy':
|
|
2744
|
+
this.lazy = parseBool(newValue, false);
|
|
2745
|
+
break;
|
|
2746
|
+
case 'mag-filter':
|
|
2747
|
+
if (newValue !== null) {
|
|
2748
|
+
this.magFilter = parseEnum(newValue, magFilterModes, 'linear', name);
|
|
2749
|
+
}
|
|
2750
|
+
else {
|
|
2751
|
+
this.magFilter = null;
|
|
2752
|
+
}
|
|
2753
|
+
break;
|
|
2754
|
+
case 'min-filter':
|
|
2755
|
+
if (newValue !== null) {
|
|
2756
|
+
this.minFilter = parseEnum(newValue, minFilterModes, 'linear-mip-linear', name);
|
|
2757
|
+
}
|
|
2758
|
+
else {
|
|
2759
|
+
this.minFilter = null;
|
|
2760
|
+
}
|
|
2761
|
+
break;
|
|
2762
|
+
case 'mipmaps':
|
|
2763
|
+
if (newValue !== null) {
|
|
2764
|
+
this.mipmaps = parseBool(newValue, true);
|
|
2765
|
+
}
|
|
2766
|
+
else {
|
|
2767
|
+
this.mipmaps = null;
|
|
2768
|
+
}
|
|
2769
|
+
break;
|
|
2770
|
+
case 'srgb':
|
|
2771
|
+
if (newValue !== null) {
|
|
2772
|
+
this.srgb = parseBool(newValue, false);
|
|
2773
|
+
}
|
|
2774
|
+
else {
|
|
2775
|
+
this.srgb = null;
|
|
2776
|
+
}
|
|
2777
|
+
break;
|
|
2271
2778
|
}
|
|
2272
2779
|
}
|
|
2273
2780
|
}
|
|
@@ -2283,6 +2790,16 @@
|
|
|
2283
2790
|
_enabled = true;
|
|
2284
2791
|
_component = null;
|
|
2285
2792
|
_appElement = null;
|
|
2793
|
+
/**
|
|
2794
|
+
* The element hosting this component, held so the host's readiness cycles can be observed
|
|
2795
|
+
* even after `closestEntity` would no longer resolve (during teardown).
|
|
2796
|
+
*/
|
|
2797
|
+
_hostElement = null;
|
|
2798
|
+
/**
|
|
2799
|
+
* The listener re-applying this component when the host's readiness cycles. Held for
|
|
2800
|
+
* removal on disconnect.
|
|
2801
|
+
*/
|
|
2802
|
+
_hostReadyListener = null;
|
|
2286
2803
|
/**
|
|
2287
2804
|
* Incremented on every connect and disconnect. connectedCallback captures the value on entry
|
|
2288
2805
|
* and abandons itself wherever it resumes from an await if the value has moved on — so a
|
|
@@ -2310,6 +2827,37 @@
|
|
|
2310
2827
|
getInitialComponentData() {
|
|
2311
2828
|
return {};
|
|
2312
2829
|
}
|
|
2830
|
+
/**
|
|
2831
|
+
* Creates the component on the host's current entity, removing it first from a previous
|
|
2832
|
+
* entity that is still alive (a retargeted `<pc-node>` moves its decorations with it). When
|
|
2833
|
+
* the entity already has a component of this type — a glTF node arriving with its authored
|
|
2834
|
+
* `render` component, say — warns and leaves `component` null. The element-level warning is
|
|
2835
|
+
* load-bearing: the engine's own duplicate-addComponent warning is Debug-stripped from
|
|
2836
|
+
* production builds, which would otherwise leave a silent null.
|
|
2837
|
+
*/
|
|
2838
|
+
_applyComponent() {
|
|
2839
|
+
const entity = this._hostElement?.entity ?? null;
|
|
2840
|
+
if (this._component && this._component.entity === entity) {
|
|
2841
|
+
return;
|
|
2842
|
+
}
|
|
2843
|
+
// A retarget leaves the previous component on a still-live entity - remove it so the
|
|
2844
|
+
// decoration follows the element, or vanishes with a dissolved binding. A destroyed
|
|
2845
|
+
// entity took its components with it.
|
|
2846
|
+
const previous = this._component;
|
|
2847
|
+
if (previous?.entity && previous.entity.c[this._componentName] === previous) {
|
|
2848
|
+
previous.entity.removeComponent(this._componentName);
|
|
2849
|
+
}
|
|
2850
|
+
this._component = null;
|
|
2851
|
+
if (!entity) {
|
|
2852
|
+
return;
|
|
2853
|
+
}
|
|
2854
|
+
if (entity.c[this._componentName]) {
|
|
2855
|
+
const label = this.id ? ` '${this.id}'` : '';
|
|
2856
|
+
console.warn(`${this.tagName.toLowerCase()}${label} - '${entity.name}' already has a '${this._componentName}' component - component not added`);
|
|
2857
|
+
return;
|
|
2858
|
+
}
|
|
2859
|
+
this._component = entity.addComponent(this._componentName, this.getInitialComponentData());
|
|
2860
|
+
}
|
|
2313
2861
|
async _addComponent() {
|
|
2314
2862
|
const generation = this._connectionGeneration;
|
|
2315
2863
|
const entityElement = this.closestEntity;
|
|
@@ -2326,9 +2874,40 @@
|
|
|
2326
2874
|
if (generation !== this._connectionGeneration) {
|
|
2327
2875
|
return;
|
|
2328
2876
|
}
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2877
|
+
this._hostElement = entityElement;
|
|
2878
|
+
this._applyComponent();
|
|
2879
|
+
// Re-apply when the host's readiness cycles without this element disconnecting: a
|
|
2880
|
+
// `<pc-node>` rebinding after its model reloads or retargets, or a re-created entity.
|
|
2881
|
+
// The 'ready' event bubbles, so events from descendants pass through this host - only
|
|
2882
|
+
// the host's own cycles count. Readiness is cycled here too, so decorations one level
|
|
2883
|
+
// down re-apply the same way.
|
|
2884
|
+
this._hostReadyListener = (event) => {
|
|
2885
|
+
if (event.target !== this._hostElement) {
|
|
2886
|
+
return;
|
|
2887
|
+
}
|
|
2888
|
+
if (generation !== this._connectionGeneration) {
|
|
2889
|
+
return;
|
|
2890
|
+
}
|
|
2891
|
+
this._hostCycled();
|
|
2892
|
+
};
|
|
2893
|
+
entityElement.addEventListener('ready', this._hostReadyListener);
|
|
2894
|
+
}
|
|
2895
|
+
/**
|
|
2896
|
+
* Re-evaluates this component against the host's current entity: applied to a new entity,
|
|
2897
|
+
* moved from a still-live old one, or removed when the host no longer fronts an entity at
|
|
2898
|
+
* all. Readiness follows - it cycles with a re-application and stays unresolved while the
|
|
2899
|
+
* host is unbound. Called by the host-ready listener, and directly by a `<pc-node>`
|
|
2900
|
+
* dissolving its binding: the one transition that fires no ready event to ride.
|
|
2901
|
+
*
|
|
2902
|
+
* @internal
|
|
2903
|
+
*/
|
|
2904
|
+
_hostCycled() {
|
|
2905
|
+
this._resetReady();
|
|
2906
|
+
this._applyComponent();
|
|
2907
|
+
if (this._hostElement?.entity) {
|
|
2908
|
+
this.initComponent();
|
|
2909
|
+
this._onReady();
|
|
2910
|
+
}
|
|
2332
2911
|
}
|
|
2333
2912
|
/**
|
|
2334
2913
|
* Configures the newly added component. Overridden by subclasses whose setup goes beyond
|
|
@@ -2357,6 +2936,11 @@
|
|
|
2357
2936
|
disconnectedCallback() {
|
|
2358
2937
|
// Invalidate any connectedCallback still suspended on an await
|
|
2359
2938
|
this._connectionGeneration++;
|
|
2939
|
+
if (this._hostElement && this._hostReadyListener) {
|
|
2940
|
+
this._hostElement.removeEventListener('ready', this._hostReadyListener);
|
|
2941
|
+
}
|
|
2942
|
+
this._hostElement = null;
|
|
2943
|
+
this._hostReadyListener = null;
|
|
2360
2944
|
// Remove the component when the element is disconnected. Skip this when the owning
|
|
2361
2945
|
// application has already been destroyed — removing a <pc-app> disconnects it before
|
|
2362
2946
|
// its children, taking the component systems with it.
|
|
@@ -3316,6 +3900,12 @@
|
|
|
3316
3900
|
* The CollisionComponentElement interface also inherits the properties and methods of the
|
|
3317
3901
|
* {@link HTMLElement} interface.
|
|
3318
3902
|
*
|
|
3903
|
+
* For `type="mesh"`, the collision geometry defaults to the host entity's own render component
|
|
3904
|
+
* (its render asset) — a collider matching the visible mesh, which is what a mesh collider on a
|
|
3905
|
+
* glTF node means. The default resolves each time the component applies, so a `pc-node` that
|
|
3906
|
+
* retargets or rebinds picks up the new node's geometry. An entity with no asset-backed render
|
|
3907
|
+
* component warns, and the collider has no shape.
|
|
3908
|
+
*
|
|
3319
3909
|
* @category Components
|
|
3320
3910
|
*/
|
|
3321
3911
|
class CollisionComponentElement extends ComponentElement {
|
|
@@ -3343,6 +3933,29 @@
|
|
|
3343
3933
|
type: this._type
|
|
3344
3934
|
};
|
|
3345
3935
|
}
|
|
3936
|
+
initComponent() {
|
|
3937
|
+
this._applyMeshGeometryDefault();
|
|
3938
|
+
}
|
|
3939
|
+
/**
|
|
3940
|
+
* Defaults a mesh collider's geometry to the host entity's own render component. The
|
|
3941
|
+
* engine's mesh collider only works with explicitly supplied geometry, and the element has
|
|
3942
|
+
* no attribute to supply it - so the host's visible geometry, the meaning a mesh collider
|
|
3943
|
+
* on a glTF node carries, fills the gap. Runs on every application (so a rebound `pc-node`
|
|
3944
|
+
* recomputes it) and on a runtime switch to `type="mesh"`; an explicitly assigned
|
|
3945
|
+
* `renderAsset` is never overwritten.
|
|
3946
|
+
*/
|
|
3947
|
+
_applyMeshGeometryDefault() {
|
|
3948
|
+
const component = this.component;
|
|
3949
|
+
if (!component || this._type !== 'mesh' || component.renderAsset !== null) {
|
|
3950
|
+
return;
|
|
3951
|
+
}
|
|
3952
|
+
const asset = component.entity.render?.asset ?? null;
|
|
3953
|
+
if (asset === null) {
|
|
3954
|
+
console.warn(`pc-collision type="mesh" on '${component.entity.name}' found no asset-backed render component to take geometry from - collider has no shape`);
|
|
3955
|
+
return;
|
|
3956
|
+
}
|
|
3957
|
+
component.renderAsset = asset;
|
|
3958
|
+
}
|
|
3346
3959
|
/**
|
|
3347
3960
|
* Gets the underlying PlayCanvas collision component.
|
|
3348
3961
|
* @returns The collision component.
|
|
@@ -3417,6 +4030,7 @@
|
|
|
3417
4030
|
this._type = value;
|
|
3418
4031
|
if (this.component) {
|
|
3419
4032
|
this.component.type = value;
|
|
4033
|
+
this._applyMeshGeometryDefault();
|
|
3420
4034
|
}
|
|
3421
4035
|
}
|
|
3422
4036
|
get type() {
|
|
@@ -5587,13 +6201,13 @@
|
|
|
5587
6201
|
}
|
|
5588
6202
|
/**
|
|
5589
6203
|
* @param slot - The material property to write.
|
|
5590
|
-
* @param texture - The loaded texture
|
|
6204
|
+
* @param texture - The loaded texture, applied with its sampler state untouched - anisotropy
|
|
6205
|
+
* and friends belong to the `pc-asset`'s texture options.
|
|
5591
6206
|
*/
|
|
5592
6207
|
_applyMap(slot, texture) {
|
|
5593
6208
|
if (!this.material)
|
|
5594
6209
|
return;
|
|
5595
6210
|
this.material[slot] = texture;
|
|
5596
|
-
texture.anisotropy = 4;
|
|
5597
6211
|
this._scheduleUpdate();
|
|
5598
6212
|
}
|
|
5599
6213
|
/**
|
|
@@ -9873,6 +10487,21 @@
|
|
|
9873
10487
|
* {@link https://developer.playcanvas.com/user-manual/web-components/tags/pc-model/ | `<pc-model>`} elements.
|
|
9874
10488
|
* The ModelElement interface also inherits the properties and methods of the
|
|
9875
10489
|
* {@link HTMLElement} interface.
|
|
10490
|
+
*
|
|
10491
|
+
* The element becomes ready once its container asset has loaded and the instantiated hierarchy has
|
|
10492
|
+
* been added to the scene — `entity` is non-null by then. A failed load also settles readiness,
|
|
10493
|
+
* with `entity` remaining `null`: readiness means the load settled, not that it succeeded — listen
|
|
10494
|
+
* for `error`, or check `entity`, to tell the outcomes apart. Changing `asset` re-arms readiness
|
|
10495
|
+
* and instantiates anew, so a `ready()` obtained after the change resolves against the new
|
|
10496
|
+
* hierarchy. A `pc-model` outside a `pc-app`, or referencing an unknown asset id, warns and never
|
|
10497
|
+
* becomes ready.
|
|
10498
|
+
*
|
|
10499
|
+
* @fires {Event} load - Fired each time a container asset finishes instantiating, including
|
|
10500
|
+
* re-instantiation after `asset` changes. Does not bubble — listen on this element, or use a
|
|
10501
|
+
* capture-phase listener on an ancestor.
|
|
10502
|
+
* @fires {ErrorEvent} error - Fired when the container asset fails to load, with the engine's
|
|
10503
|
+
* error in `message`. Does not bubble. The element still becomes ready — readiness means the load
|
|
10504
|
+
* settled, not that it succeeded.
|
|
9876
10505
|
*/
|
|
9877
10506
|
class ModelElement extends AsyncElement {
|
|
9878
10507
|
_asset = '';
|
|
@@ -9885,11 +10514,12 @@
|
|
|
9885
10514
|
*/
|
|
9886
10515
|
_loadGeneration = 0;
|
|
9887
10516
|
/**
|
|
9888
|
-
* The pending asset
|
|
9889
|
-
*
|
|
9890
|
-
*
|
|
10517
|
+
* The pending asset subscriptions of the current load, if it is waiting for its asset. Held
|
|
10518
|
+
* so that whatever supersedes the load can detach the handlers from the asset, rather than
|
|
10519
|
+
* leave them registered until the asset settles (or forever, if it never does).
|
|
9891
10520
|
*/
|
|
9892
10521
|
_loadHandle = null;
|
|
10522
|
+
_errorHandle = null;
|
|
9893
10523
|
/**
|
|
9894
10524
|
* The root entity of the instantiated model. `null` until the container asset has loaded
|
|
9895
10525
|
* and been instantiated, and again once the element has been removed from the document.
|
|
@@ -9899,18 +10529,36 @@
|
|
|
9899
10529
|
return this._entity;
|
|
9900
10530
|
}
|
|
9901
10531
|
connectedCallback() {
|
|
10532
|
+
// A model outside an application is inert and never becomes ready, so awaiting it hangs.
|
|
10533
|
+
// Warn rather than fail silently, naming the parent it requires, as every other misplaced
|
|
10534
|
+
// element does.
|
|
10535
|
+
if (!this.closestApp) {
|
|
10536
|
+
const label = this._asset ? ` '${this._asset}'` : '';
|
|
10537
|
+
console.warn(`pc-model${label} must be a descendant of pc-app - model not created`);
|
|
10538
|
+
return;
|
|
10539
|
+
}
|
|
9902
10540
|
this._loadModel();
|
|
9903
|
-
this._onReady();
|
|
9904
10541
|
}
|
|
9905
10542
|
disconnectedCallback() {
|
|
9906
10543
|
this._loadGeneration++;
|
|
9907
|
-
this.
|
|
10544
|
+
this._detachLoadHandlers();
|
|
9908
10545
|
this._unloadModel();
|
|
9909
10546
|
this._resetReady();
|
|
9910
10547
|
}
|
|
9911
|
-
|
|
10548
|
+
_detachLoadHandlers() {
|
|
9912
10549
|
this._loadHandle?.off();
|
|
9913
10550
|
this._loadHandle = null;
|
|
10551
|
+
this._errorHandle?.off();
|
|
10552
|
+
this._errorHandle = null;
|
|
10553
|
+
}
|
|
10554
|
+
/**
|
|
10555
|
+
* Resolves readiness and dispatches the `load` event. Called once the instantiated hierarchy
|
|
10556
|
+
* has been parented — readiness means "in the scene graph", matching `pc-entity`, so a ready
|
|
10557
|
+
* model's entity always has world transforms.
|
|
10558
|
+
*/
|
|
10559
|
+
_announceLoad() {
|
|
10560
|
+
this._onReady();
|
|
10561
|
+
this.dispatchEvent(new Event('load'));
|
|
9914
10562
|
}
|
|
9915
10563
|
_instantiate(container) {
|
|
9916
10564
|
const generation = this._loadGeneration;
|
|
@@ -9932,6 +10580,7 @@
|
|
|
9932
10580
|
return;
|
|
9933
10581
|
}
|
|
9934
10582
|
parentEntityElement.entity.addChild(entity);
|
|
10583
|
+
this._announceLoad();
|
|
9935
10584
|
});
|
|
9936
10585
|
}
|
|
9937
10586
|
else {
|
|
@@ -9942,6 +10591,7 @@
|
|
|
9942
10591
|
return;
|
|
9943
10592
|
}
|
|
9944
10593
|
appElement.app.root.addChild(entity);
|
|
10594
|
+
this._announceLoad();
|
|
9945
10595
|
});
|
|
9946
10596
|
}
|
|
9947
10597
|
}
|
|
@@ -9950,15 +10600,29 @@
|
|
|
9950
10600
|
this._unloadModel();
|
|
9951
10601
|
// Supersede any load already in flight - only the newest load may instantiate
|
|
9952
10602
|
const generation = ++this._loadGeneration;
|
|
9953
|
-
this.
|
|
9954
|
-
|
|
10603
|
+
this._detachLoadHandlers();
|
|
10604
|
+
// Re-arm readiness so a waiter obtained after an asset change resolves against the new
|
|
10605
|
+
// hierarchy. A no-op on first connection, where readiness is still pending.
|
|
10606
|
+
this._resetReady();
|
|
10607
|
+
const appElement = this.closestApp;
|
|
10608
|
+
if (!appElement) {
|
|
10609
|
+
// Outside pc-app; connectedCallback already warned. Reached through the asset setter.
|
|
10610
|
+
return;
|
|
10611
|
+
}
|
|
10612
|
+
await appElement.ready();
|
|
9955
10613
|
// The element may have been removed, or another load started, while we waited
|
|
9956
10614
|
if (generation !== this._loadGeneration) {
|
|
9957
10615
|
return;
|
|
9958
10616
|
}
|
|
9959
|
-
const app = appElement
|
|
10617
|
+
const app = appElement.app;
|
|
9960
10618
|
const asset = AssetElement.get(this._asset);
|
|
9961
10619
|
if (!asset) {
|
|
10620
|
+
// An empty id is a legitimate transient (the asset may be assigned later); a
|
|
10621
|
+
// non-empty one that resolves to nothing is a dead end - say so rather than staying
|
|
10622
|
+
// silently pending.
|
|
10623
|
+
if (this._asset) {
|
|
10624
|
+
console.warn(`pc-model could not find asset '${this._asset}' - model not created`);
|
|
10625
|
+
}
|
|
9962
10626
|
return;
|
|
9963
10627
|
}
|
|
9964
10628
|
if (asset.loaded) {
|
|
@@ -9967,14 +10631,26 @@
|
|
|
9967
10631
|
else {
|
|
9968
10632
|
// The generation is re-checked even though a superseded handler is detached: the
|
|
9969
10633
|
// detach relies on how the engine's event emitter treats removal, while the check
|
|
9970
|
-
// holds on its own.
|
|
10634
|
+
// holds on its own. Whichever of load/error fires first detaches the other.
|
|
9971
10635
|
this._loadHandle = asset.once('load', () => {
|
|
9972
|
-
this.
|
|
10636
|
+
this._detachLoadHandlers();
|
|
9973
10637
|
if (generation !== this._loadGeneration) {
|
|
9974
10638
|
return;
|
|
9975
10639
|
}
|
|
9976
10640
|
this._instantiate(asset.resource);
|
|
9977
10641
|
});
|
|
10642
|
+
this._errorHandle = asset.once('error', (err) => {
|
|
10643
|
+
this._detachLoadHandlers();
|
|
10644
|
+
if (generation !== this._loadGeneration) {
|
|
10645
|
+
return;
|
|
10646
|
+
}
|
|
10647
|
+
// A failed load settles readiness with a null entity, mirroring pc-asset:
|
|
10648
|
+
// readiness means the load settled, not that it succeeded.
|
|
10649
|
+
this.dispatchEvent(new ErrorEvent('error', {
|
|
10650
|
+
message: err instanceof Error ? err.message : String(err)
|
|
10651
|
+
}));
|
|
10652
|
+
this._onReady();
|
|
10653
|
+
});
|
|
9978
10654
|
app.assets.load(asset);
|
|
9979
10655
|
}
|
|
9980
10656
|
}
|
|
@@ -10012,6 +10688,625 @@
|
|
|
10012
10688
|
}
|
|
10013
10689
|
customElements.define('pc-model', ModelElement);
|
|
10014
10690
|
|
|
10691
|
+
/**
|
|
10692
|
+
* Computes the Levenshtein distance between two strings, for near-miss suggestions in the
|
|
10693
|
+
* resolution warnings.
|
|
10694
|
+
*
|
|
10695
|
+
* @param a - The first string.
|
|
10696
|
+
* @param b - The second string.
|
|
10697
|
+
* @returns The edit distance.
|
|
10698
|
+
*/
|
|
10699
|
+
const levenshtein = (a, b) => {
|
|
10700
|
+
const row = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
10701
|
+
for (let i = 1; i <= a.length; i++) {
|
|
10702
|
+
let previous = row[0];
|
|
10703
|
+
row[0] = i;
|
|
10704
|
+
for (let j = 1; j <= b.length; j++) {
|
|
10705
|
+
const current = row[j];
|
|
10706
|
+
row[j] = Math.min(row[j] + 1, row[j - 1] + 1, previous + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
10707
|
+
previous = current;
|
|
10708
|
+
}
|
|
10709
|
+
}
|
|
10710
|
+
return row[b.length];
|
|
10711
|
+
};
|
|
10712
|
+
/**
|
|
10713
|
+
* The NodeElement interface provides properties and methods for manipulating
|
|
10714
|
+
* {@link https://developer.playcanvas.com/user-manual/web-components/tags/pc-node/ | `<pc-node>`}
|
|
10715
|
+
* elements. The NodeElement interface also inherits the properties and methods of the
|
|
10716
|
+
* {@link HTMLElement} interface.
|
|
10717
|
+
*
|
|
10718
|
+
* A `pc-node` is an override element: where `pc-entity` creates an entity, `pc-node` binds to a
|
|
10719
|
+
* node a `pc-model` loaded and declares overrides against the authored asset — components to
|
|
10720
|
+
* add, properties to change, content to attach. Attributes present apply as overrides; attributes
|
|
10721
|
+
* absent leave authored values untouched, and removing an attribute (or assigning `null` to the
|
|
10722
|
+
* matching property) restores the authored value.
|
|
10723
|
+
*
|
|
10724
|
+
* `name` selects among the host model's nodes (first match in depth-first order), nesting a
|
|
10725
|
+
* `pc-node` inside another scopes the search to that subtree, and `index` picks among identically
|
|
10726
|
+
* named matches. When `name` matches more than one node and no `index` is given, the element
|
|
10727
|
+
* warns and binds nothing.
|
|
10728
|
+
*
|
|
10729
|
+
* The element becomes ready once bound, and never while unresolved — a missing or ambiguous
|
|
10730
|
+
* name warns and records the failure in `state`, readiness stays unresolved, and descendants
|
|
10731
|
+
* wait with it.
|
|
10732
|
+
*
|
|
10733
|
+
* The pointer events below are dispatched by the containing `<pc-app>` element when the pointer
|
|
10734
|
+
* intersects the bound node's geometry, exactly as for `<pc-entity>`.
|
|
10735
|
+
*
|
|
10736
|
+
* @attribute {string} name - The name of the node to bind, resolved within the nearest ancestor
|
|
10737
|
+
* `pc-model` (or `pc-node`) once it has instantiated.
|
|
10738
|
+
* @attribute {number} index - Which match to bind when `name` matches more than one node,
|
|
10739
|
+
* 0-based in depth-first order. Optional for a unique match; required for an ambiguous one.
|
|
10740
|
+
* @attribute {boolean} enabled - Overrides the node's enabled state.
|
|
10741
|
+
* @attribute {string} position - Overrides the node's local position, as an "x y z" triple.
|
|
10742
|
+
* @attribute {string} rotation - Overrides the node's local rotation (Euler angles), as an
|
|
10743
|
+
* "x y z" triple.
|
|
10744
|
+
* @attribute {string} scale - Overrides the node's local scale, as an "x y z" triple.
|
|
10745
|
+
* @attribute {string} tags - Overrides the node's tags, separated by spaces or commas.
|
|
10746
|
+
* @attribute {string} onpointerenter - Script to run when the pointer moves onto the node.
|
|
10747
|
+
* @attribute {string} onpointerleave - Script to run when the pointer moves off the node.
|
|
10748
|
+
* @attribute {string} onpointermove - Script to run when the pointer moves over the node.
|
|
10749
|
+
* @attribute {string} onpointerdown - Script to run when a pointer button is pressed over the
|
|
10750
|
+
* node.
|
|
10751
|
+
* @attribute {string} onpointerup - Script to run when a pointer button is released over the
|
|
10752
|
+
* node.
|
|
10753
|
+
* @fires {PointerEvent} pointerenter - Fired when the pointer moves onto the node.
|
|
10754
|
+
* @fires {PointerEvent} pointerleave - Fired when the pointer moves off the node.
|
|
10755
|
+
* @fires {PointerEvent} pointermove - Fired when the pointer moves over the node.
|
|
10756
|
+
* @fires {PointerEvent} pointerdown - Fired when a pointer button is pressed over the node.
|
|
10757
|
+
* @fires {PointerEvent} pointerup - Fired when a pointer button is released over the node.
|
|
10758
|
+
*/
|
|
10759
|
+
class NodeElement extends EntityBaseElement {
|
|
10760
|
+
_name = '';
|
|
10761
|
+
_index = null;
|
|
10762
|
+
_state = 'pending';
|
|
10763
|
+
_path = null;
|
|
10764
|
+
/**
|
|
10765
|
+
* The element whose entity roots this element's search: the nearest ancestor `pc-node`, or
|
|
10766
|
+
* failing that the nearest ancestor `pc-model`. Resolved on connection.
|
|
10767
|
+
*/
|
|
10768
|
+
_host = null;
|
|
10769
|
+
/**
|
|
10770
|
+
* The listener following the host's binding cycles. Both host kinds announce each cycle
|
|
10771
|
+
* with a `ready` event — `pc-model` on every instantiation, `pc-node` on every bind.
|
|
10772
|
+
*/
|
|
10773
|
+
_hostListener = null;
|
|
10774
|
+
/**
|
|
10775
|
+
* The subscription to the bound entity's destruction, detached on unbind so a retargeted
|
|
10776
|
+
* element cannot be reset by the eventual death of a node it no longer fronts.
|
|
10777
|
+
*/
|
|
10778
|
+
_destroyHandle = null;
|
|
10779
|
+
/** The authored values displaced by this element's overrides, captured per property. */
|
|
10780
|
+
_authored = {};
|
|
10781
|
+
// Override values. `null` means "no override": the authored value stays in force.
|
|
10782
|
+
_enabled = null;
|
|
10783
|
+
_position = null;
|
|
10784
|
+
_rotation = null;
|
|
10785
|
+
_scale = null;
|
|
10786
|
+
_tags = null;
|
|
10787
|
+
/**
|
|
10788
|
+
* The binding state: `pending` until the host instantiates and `name` resolves, `bound`
|
|
10789
|
+
* once decorated, `missing`/`ambiguous`/`duplicate` when resolution failed (each also
|
|
10790
|
+
* warns). Useful for asserting a document's bindings programmatically.
|
|
10791
|
+
* @returns The binding state.
|
|
10792
|
+
*/
|
|
10793
|
+
get state() {
|
|
10794
|
+
return this._state;
|
|
10795
|
+
}
|
|
10796
|
+
/**
|
|
10797
|
+
* The path of the bound node below the search root, `/`-separated, or `null` while not
|
|
10798
|
+
* bound.
|
|
10799
|
+
* @returns The bound node's path, or `null`.
|
|
10800
|
+
*/
|
|
10801
|
+
get path() {
|
|
10802
|
+
return this._path;
|
|
10803
|
+
}
|
|
10804
|
+
connectedCallback() {
|
|
10805
|
+
const host = (this.parentElement?.closest('pc-model, pc-node') ?? null);
|
|
10806
|
+
if (!host) {
|
|
10807
|
+
const label = this._name ? ` '${this._name}'` : '';
|
|
10808
|
+
console.warn(`pc-node${label} must be a descendant of pc-model - node not bound`);
|
|
10809
|
+
return;
|
|
10810
|
+
}
|
|
10811
|
+
this._host = host;
|
|
10812
|
+
// Follow the host's binding cycles. `ready` bubbles, so cycles of elements nested under
|
|
10813
|
+
// the host pass through it - only the host's own count.
|
|
10814
|
+
this._hostListener = (event) => {
|
|
10815
|
+
if (event.target !== this._host) {
|
|
10816
|
+
return;
|
|
10817
|
+
}
|
|
10818
|
+
this._rebind();
|
|
10819
|
+
};
|
|
10820
|
+
host.addEventListener('ready', this._hostListener);
|
|
10821
|
+
// The host may already be instantiated (an element inserted after load binds immediately)
|
|
10822
|
+
this._rebind();
|
|
10823
|
+
}
|
|
10824
|
+
disconnectedCallback() {
|
|
10825
|
+
if (this._host && this._hostListener) {
|
|
10826
|
+
this._host.removeEventListener('ready', this._hostListener);
|
|
10827
|
+
}
|
|
10828
|
+
this._host = null;
|
|
10829
|
+
this._hostListener = null;
|
|
10830
|
+
// Removal reverts: the model owns the node, so the entity is left as authored. Children
|
|
10831
|
+
// clean up through their own disconnect behavior.
|
|
10832
|
+
this._unbind();
|
|
10833
|
+
this._state = 'pending';
|
|
10834
|
+
}
|
|
10835
|
+
/**
|
|
10836
|
+
* Re-resolves the binding against the host's current hierarchy: on connection, on a `name`
|
|
10837
|
+
* or `index` change, and on every host cycle (a model [re]instantiating, an enclosing
|
|
10838
|
+
* `pc-node` [re]binding). When re-resolution yields the entity already bound, the binding
|
|
10839
|
+
* is retained untouched — a redundant edit must not flicker overrides through a revert.
|
|
10840
|
+
*/
|
|
10841
|
+
_rebind() {
|
|
10842
|
+
const hostEntity = this._host?.entity ?? null;
|
|
10843
|
+
if (!hostEntity || !this._name) {
|
|
10844
|
+
// Host not instantiated (or nothing to look up yet): return to pending. An assigned
|
|
10845
|
+
// name arriving later, or the host's next cycle, resolves it.
|
|
10846
|
+
this._unbind();
|
|
10847
|
+
this._state = 'pending';
|
|
10848
|
+
return;
|
|
10849
|
+
}
|
|
10850
|
+
const target = this._resolve(hostEntity);
|
|
10851
|
+
if (target && target === this._entity) {
|
|
10852
|
+
this._path = this._pathOf(target, hostEntity);
|
|
10853
|
+
return;
|
|
10854
|
+
}
|
|
10855
|
+
this._unbind();
|
|
10856
|
+
if (!target) {
|
|
10857
|
+
// _resolve warned and set the failure state
|
|
10858
|
+
return;
|
|
10859
|
+
}
|
|
10860
|
+
this._bind(target, hostEntity);
|
|
10861
|
+
}
|
|
10862
|
+
/**
|
|
10863
|
+
* Resolves `name` (and `index`) to an entity under `hostEntity`, warning and recording the
|
|
10864
|
+
* failure state when it cannot.
|
|
10865
|
+
*
|
|
10866
|
+
* @param hostEntity - The root of the search.
|
|
10867
|
+
* @returns The resolved entity, or `null`.
|
|
10868
|
+
*/
|
|
10869
|
+
_resolve(hostEntity) {
|
|
10870
|
+
const matches = hostEntity.find((node) => node.name === this._name);
|
|
10871
|
+
if (matches.length === 0) {
|
|
10872
|
+
const closest = this._closestName(hostEntity);
|
|
10873
|
+
const hint = closest ? ` - closest match: '${closest}'` : '';
|
|
10874
|
+
console.warn(`pc-node '${this._name}' not found in ${this._describeHost()}${hint}`);
|
|
10875
|
+
this._state = 'missing';
|
|
10876
|
+
return null;
|
|
10877
|
+
}
|
|
10878
|
+
let target;
|
|
10879
|
+
if (this._index !== null) {
|
|
10880
|
+
if (this._index >= matches.length) {
|
|
10881
|
+
console.warn(`pc-node '${this._name}' index ${this._index} is out of range - ${matches.length} match(es) in ${this._describeHost()}`);
|
|
10882
|
+
this._state = 'missing';
|
|
10883
|
+
return null;
|
|
10884
|
+
}
|
|
10885
|
+
target = matches[this._index];
|
|
10886
|
+
}
|
|
10887
|
+
else if (matches.length > 1) {
|
|
10888
|
+
// Ambiguity binds nothing: a fallback guess performs side effects on the wrong
|
|
10889
|
+
// scene node, and would go wrong silently when a re-export introduces a duplicate
|
|
10890
|
+
// name. The candidates tell the author exactly what to write.
|
|
10891
|
+
const candidates = matches.map((m, i) => `[${i}] ${this._pathOf(m, hostEntity)}`).join(', ');
|
|
10892
|
+
console.warn(`pc-node '${this._name}' is ambiguous in ${this._describeHost()} - specify index: ${candidates}`);
|
|
10893
|
+
this._state = 'ambiguous';
|
|
10894
|
+
return null;
|
|
10895
|
+
}
|
|
10896
|
+
else {
|
|
10897
|
+
target = matches[0];
|
|
10898
|
+
}
|
|
10899
|
+
const owner = this.closestApp?.elementFromEntity(target);
|
|
10900
|
+
if (owner && owner !== this) {
|
|
10901
|
+
console.warn(`pc-node '${this._name}' resolves to a node already bound by another element - element ignored`);
|
|
10902
|
+
this._state = 'duplicate';
|
|
10903
|
+
return null;
|
|
10904
|
+
}
|
|
10905
|
+
return target;
|
|
10906
|
+
}
|
|
10907
|
+
/**
|
|
10908
|
+
* Binds `target`: registers it (making it a pick target), hooks its destruction, applies
|
|
10909
|
+
* this element's overrides, announces readiness and builds the deferred child subtree.
|
|
10910
|
+
*
|
|
10911
|
+
* @param target - The entity to bind.
|
|
10912
|
+
* @param hostEntity - The search root, for the path.
|
|
10913
|
+
*/
|
|
10914
|
+
_bind(target, hostEntity) {
|
|
10915
|
+
this._entity = target;
|
|
10916
|
+
this._registerEntity(target);
|
|
10917
|
+
this._destroyHandle = target.once('destroy', this._onEntityDestroy, this);
|
|
10918
|
+
this._state = 'bound';
|
|
10919
|
+
this._path = this._pathOf(target, hostEntity);
|
|
10920
|
+
this._applyOverrides();
|
|
10921
|
+
this._onReady();
|
|
10922
|
+
this._buildChildren();
|
|
10923
|
+
}
|
|
10924
|
+
/**
|
|
10925
|
+
* Dissolves the current binding, restoring every authored value this element's overrides
|
|
10926
|
+
* displaced and removing the decorations this binding hosts: attachment entities are
|
|
10927
|
+
* destroyed (re-created against the next binding) and component decorations are removed
|
|
10928
|
+
* from the abandoned node. Both sweeps are scoped by `closestEntity`, so a still-bound
|
|
10929
|
+
* nested `pc-node` keeps its own decorations. Safe to call in any state.
|
|
10930
|
+
*/
|
|
10931
|
+
_unbind() {
|
|
10932
|
+
const entity = this._entity;
|
|
10933
|
+
if (!entity) {
|
|
10934
|
+
return;
|
|
10935
|
+
}
|
|
10936
|
+
this._revertOverrides();
|
|
10937
|
+
// Attachment points anchor to the bound node, so they cannot outlive the binding. Each
|
|
10938
|
+
// destroyed entity resets its element, which the next _buildChildren re-creates.
|
|
10939
|
+
this.querySelectorAll('pc-entity').forEach((child) => {
|
|
10940
|
+
if (child.closestEntity === this) {
|
|
10941
|
+
child.entity?.destroy();
|
|
10942
|
+
}
|
|
10943
|
+
});
|
|
10944
|
+
this._destroyHandle?.off();
|
|
10945
|
+
this._destroyHandle = null;
|
|
10946
|
+
this._unregisterEntity(entity);
|
|
10947
|
+
this._entity = null;
|
|
10948
|
+
this._path = null;
|
|
10949
|
+
this._authored = {};
|
|
10950
|
+
// Component decorations come off through the same hook the host-ready cycle uses. A
|
|
10951
|
+
// dissolve that never rebinds fires no ready event, so the sweep is explicit - after
|
|
10952
|
+
// `_entity` is cleared, so the hook sees a host without an entity.
|
|
10953
|
+
this.querySelectorAll('*').forEach((child) => {
|
|
10954
|
+
if (child instanceof ComponentElement && child.closestEntity === this) {
|
|
10955
|
+
child._hostCycled();
|
|
10956
|
+
}
|
|
10957
|
+
});
|
|
10958
|
+
this._resetReady();
|
|
10959
|
+
}
|
|
10960
|
+
/**
|
|
10961
|
+
* Handles the destruction of the bound entity - its model unloading, reloading, or a script
|
|
10962
|
+
* destroying it. There is nothing to revert on a destroyed entity; the element returns to
|
|
10963
|
+
* pending and the host's next cycle re-resolves it.
|
|
10964
|
+
*/
|
|
10965
|
+
_onEntityDestroy(entity) {
|
|
10966
|
+
this._destroyHandle = null;
|
|
10967
|
+
this._unregisterEntity(entity);
|
|
10968
|
+
this._entity = null;
|
|
10969
|
+
this._path = null;
|
|
10970
|
+
this._authored = {};
|
|
10971
|
+
this._state = 'pending';
|
|
10972
|
+
this._resetReady();
|
|
10973
|
+
}
|
|
10974
|
+
/**
|
|
10975
|
+
* Creates and parents the entities of child `pc-entity` elements - the attachment points.
|
|
10976
|
+
* Mirrors the runtime-insertion path in EntityElement.connectedCallback: children were
|
|
10977
|
+
* deferred while this host was unresolved (or reset when a previous binding dissolved), and
|
|
10978
|
+
* build here once it binds.
|
|
10979
|
+
*/
|
|
10980
|
+
_buildChildren() {
|
|
10981
|
+
const app = this.closestApp?.app;
|
|
10982
|
+
if (!app) {
|
|
10983
|
+
return;
|
|
10984
|
+
}
|
|
10985
|
+
const childEntities = this.querySelectorAll('pc-entity');
|
|
10986
|
+
childEntities.forEach((child) => {
|
|
10987
|
+
child._createEntity(app);
|
|
10988
|
+
});
|
|
10989
|
+
childEntities.forEach((child) => {
|
|
10990
|
+
child._buildHierarchy(app);
|
|
10991
|
+
});
|
|
10992
|
+
}
|
|
10993
|
+
/**
|
|
10994
|
+
* Applies every override that is explicitly set, capturing the authored value it displaces.
|
|
10995
|
+
*/
|
|
10996
|
+
_applyOverrides() {
|
|
10997
|
+
if (this._enabled !== null) {
|
|
10998
|
+
this.enabled = this._enabled;
|
|
10999
|
+
}
|
|
11000
|
+
if (this._position !== null) {
|
|
11001
|
+
this.position = this._position;
|
|
11002
|
+
}
|
|
11003
|
+
if (this._rotation !== null) {
|
|
11004
|
+
this.rotation = this._rotation;
|
|
11005
|
+
}
|
|
11006
|
+
if (this._scale !== null) {
|
|
11007
|
+
this.scale = this._scale;
|
|
11008
|
+
}
|
|
11009
|
+
if (this._tags !== null) {
|
|
11010
|
+
this.tags = this._tags;
|
|
11011
|
+
}
|
|
11012
|
+
}
|
|
11013
|
+
/**
|
|
11014
|
+
* Restores every authored value this element's overrides displaced. The override values
|
|
11015
|
+
* themselves are kept - they re-apply on the next binding.
|
|
11016
|
+
*/
|
|
11017
|
+
_revertOverrides() {
|
|
11018
|
+
const entity = this._entity;
|
|
11019
|
+
const authored = this._authored;
|
|
11020
|
+
if (authored.enabled !== undefined) {
|
|
11021
|
+
entity.enabled = authored.enabled;
|
|
11022
|
+
}
|
|
11023
|
+
if (authored.position) {
|
|
11024
|
+
entity.setLocalPosition(authored.position);
|
|
11025
|
+
}
|
|
11026
|
+
if (authored.rotation) {
|
|
11027
|
+
entity.setLocalRotation(authored.rotation);
|
|
11028
|
+
}
|
|
11029
|
+
if (authored.scale) {
|
|
11030
|
+
entity.setLocalScale(authored.scale);
|
|
11031
|
+
}
|
|
11032
|
+
if (authored.tags) {
|
|
11033
|
+
entity.tags.clear();
|
|
11034
|
+
entity.tags.add(authored.tags);
|
|
11035
|
+
}
|
|
11036
|
+
this._authored = {};
|
|
11037
|
+
}
|
|
11038
|
+
/**
|
|
11039
|
+
* Renders the path of `node` below `root`, for the `path` property and the resolution
|
|
11040
|
+
* warnings.
|
|
11041
|
+
*
|
|
11042
|
+
* @param node - The node to describe.
|
|
11043
|
+
* @param root - The search root.
|
|
11044
|
+
* @returns The `/`-separated path.
|
|
11045
|
+
*/
|
|
11046
|
+
_pathOf(node, root) {
|
|
11047
|
+
const parts = [];
|
|
11048
|
+
for (let current = node; current && current !== root; current = current.parent) {
|
|
11049
|
+
parts.unshift(current.name);
|
|
11050
|
+
}
|
|
11051
|
+
return parts.join('/') || node.name;
|
|
11052
|
+
}
|
|
11053
|
+
/**
|
|
11054
|
+
* Describes the search root for warnings: the model's asset id, or the enclosing node's
|
|
11055
|
+
* name.
|
|
11056
|
+
* @returns The description.
|
|
11057
|
+
*/
|
|
11058
|
+
_describeHost() {
|
|
11059
|
+
if (this._host instanceof ModelElement) {
|
|
11060
|
+
return `model '${this._host.asset}'`;
|
|
11061
|
+
}
|
|
11062
|
+
return `pc-node '${this._host?.name ?? ''}' subtree`;
|
|
11063
|
+
}
|
|
11064
|
+
/**
|
|
11065
|
+
* Finds the node name nearest to the missing `name`, for the miss warning. The names are
|
|
11066
|
+
* already in hand from resolution, so the suggestion is nearly free.
|
|
11067
|
+
*
|
|
11068
|
+
* @param hostEntity - The root of the search.
|
|
11069
|
+
* @returns The closest name within an edit distance of 2, or `null`.
|
|
11070
|
+
*/
|
|
11071
|
+
_closestName(hostEntity) {
|
|
11072
|
+
let best = null;
|
|
11073
|
+
let bestDistance = 3;
|
|
11074
|
+
hostEntity.find((node) => {
|
|
11075
|
+
const distance = levenshtein(this._name, node.name);
|
|
11076
|
+
if (distance < bestDistance) {
|
|
11077
|
+
bestDistance = distance;
|
|
11078
|
+
best = node.name;
|
|
11079
|
+
}
|
|
11080
|
+
return false;
|
|
11081
|
+
});
|
|
11082
|
+
return best;
|
|
11083
|
+
}
|
|
11084
|
+
/**
|
|
11085
|
+
* Sets the name of the node to bind. A change retargets: the current binding's overrides
|
|
11086
|
+
* revert and the new name resolves afresh. `name` on a `pc-node` is never a rename of the
|
|
11087
|
+
* authored node - it is only ever a reference.
|
|
11088
|
+
* @param value - The node name.
|
|
11089
|
+
*/
|
|
11090
|
+
set name(value) {
|
|
11091
|
+
this._name = value;
|
|
11092
|
+
if (this.isConnected && this._host) {
|
|
11093
|
+
this._rebind();
|
|
11094
|
+
}
|
|
11095
|
+
}
|
|
11096
|
+
/**
|
|
11097
|
+
* Gets the name of the node to bind.
|
|
11098
|
+
* @returns The node name.
|
|
11099
|
+
*/
|
|
11100
|
+
get name() {
|
|
11101
|
+
return this._name;
|
|
11102
|
+
}
|
|
11103
|
+
/**
|
|
11104
|
+
* Sets which match to bind when `name` matches more than one node, 0-based in depth-first
|
|
11105
|
+
* order. A change retargets, like `name`. `null` means unset - required when the name is
|
|
11106
|
+
* ambiguous, optional otherwise.
|
|
11107
|
+
* @param value - The match index, or `null`.
|
|
11108
|
+
*/
|
|
11109
|
+
set index(value) {
|
|
11110
|
+
this._index = value;
|
|
11111
|
+
if (this.isConnected && this._host) {
|
|
11112
|
+
this._rebind();
|
|
11113
|
+
}
|
|
11114
|
+
}
|
|
11115
|
+
/**
|
|
11116
|
+
* Gets which match to bind.
|
|
11117
|
+
* @returns The match index, or `null` when unset.
|
|
11118
|
+
*/
|
|
11119
|
+
get index() {
|
|
11120
|
+
return this._index;
|
|
11121
|
+
}
|
|
11122
|
+
/**
|
|
11123
|
+
* Sets the enabled override. `null` clears it, restoring the authored state.
|
|
11124
|
+
* @param value - The enabled state, or `null`.
|
|
11125
|
+
*/
|
|
11126
|
+
set enabled(value) {
|
|
11127
|
+
this._enabled = value;
|
|
11128
|
+
const entity = this._state === 'bound' ? this._entity : null;
|
|
11129
|
+
if (!entity) {
|
|
11130
|
+
return;
|
|
11131
|
+
}
|
|
11132
|
+
if (value !== null) {
|
|
11133
|
+
this._authored.enabled ??= entity.enabled;
|
|
11134
|
+
entity.enabled = value;
|
|
11135
|
+
}
|
|
11136
|
+
else if (this._authored.enabled !== undefined) {
|
|
11137
|
+
entity.enabled = this._authored.enabled;
|
|
11138
|
+
delete this._authored.enabled;
|
|
11139
|
+
}
|
|
11140
|
+
}
|
|
11141
|
+
/**
|
|
11142
|
+
* Gets the enabled override.
|
|
11143
|
+
* @returns The enabled state, or `null` while no override is set.
|
|
11144
|
+
*/
|
|
11145
|
+
get enabled() {
|
|
11146
|
+
return this._enabled;
|
|
11147
|
+
}
|
|
11148
|
+
/**
|
|
11149
|
+
* Sets the local position override. `null` clears it, restoring the authored position.
|
|
11150
|
+
* @param value - The position, or `null`.
|
|
11151
|
+
*/
|
|
11152
|
+
set position(value) {
|
|
11153
|
+
this._position = value;
|
|
11154
|
+
const entity = this._state === 'bound' ? this._entity : null;
|
|
11155
|
+
if (!entity) {
|
|
11156
|
+
return;
|
|
11157
|
+
}
|
|
11158
|
+
if (value !== null) {
|
|
11159
|
+
this._authored.position ??= entity.getLocalPosition().clone();
|
|
11160
|
+
entity.setLocalPosition(value);
|
|
11161
|
+
}
|
|
11162
|
+
else if (this._authored.position) {
|
|
11163
|
+
entity.setLocalPosition(this._authored.position);
|
|
11164
|
+
delete this._authored.position;
|
|
11165
|
+
}
|
|
11166
|
+
}
|
|
11167
|
+
/**
|
|
11168
|
+
* Gets the local position override.
|
|
11169
|
+
* @returns The position, or `null` while no override is set.
|
|
11170
|
+
*/
|
|
11171
|
+
get position() {
|
|
11172
|
+
return this._position;
|
|
11173
|
+
}
|
|
11174
|
+
/**
|
|
11175
|
+
* Sets the local rotation override, as Euler angles in degrees. `null` clears it, restoring
|
|
11176
|
+
* the authored rotation.
|
|
11177
|
+
* @param value - The rotation, or `null`.
|
|
11178
|
+
*/
|
|
11179
|
+
set rotation(value) {
|
|
11180
|
+
this._rotation = value;
|
|
11181
|
+
const entity = this._state === 'bound' ? this._entity : null;
|
|
11182
|
+
if (!entity) {
|
|
11183
|
+
return;
|
|
11184
|
+
}
|
|
11185
|
+
if (value !== null) {
|
|
11186
|
+
// The authored rotation is cached as a quaternion: it restores exactly, where a
|
|
11187
|
+
// round trip through Euler angles need not.
|
|
11188
|
+
this._authored.rotation ??= entity.getLocalRotation().clone();
|
|
11189
|
+
entity.setLocalEulerAngles(value);
|
|
11190
|
+
}
|
|
11191
|
+
else if (this._authored.rotation) {
|
|
11192
|
+
entity.setLocalRotation(this._authored.rotation);
|
|
11193
|
+
delete this._authored.rotation;
|
|
11194
|
+
}
|
|
11195
|
+
}
|
|
11196
|
+
/**
|
|
11197
|
+
* Gets the local rotation override.
|
|
11198
|
+
* @returns The rotation, or `null` while no override is set.
|
|
11199
|
+
*/
|
|
11200
|
+
get rotation() {
|
|
11201
|
+
return this._rotation;
|
|
11202
|
+
}
|
|
11203
|
+
/**
|
|
11204
|
+
* Sets the local scale override. `null` clears it, restoring the authored scale.
|
|
11205
|
+
* @param value - The scale, or `null`.
|
|
11206
|
+
*/
|
|
11207
|
+
set scale(value) {
|
|
11208
|
+
this._scale = value;
|
|
11209
|
+
const entity = this._state === 'bound' ? this._entity : null;
|
|
11210
|
+
if (!entity) {
|
|
11211
|
+
return;
|
|
11212
|
+
}
|
|
11213
|
+
if (value !== null) {
|
|
11214
|
+
this._authored.scale ??= entity.getLocalScale().clone();
|
|
11215
|
+
entity.setLocalScale(value);
|
|
11216
|
+
}
|
|
11217
|
+
else if (this._authored.scale) {
|
|
11218
|
+
entity.setLocalScale(this._authored.scale);
|
|
11219
|
+
delete this._authored.scale;
|
|
11220
|
+
}
|
|
11221
|
+
}
|
|
11222
|
+
/**
|
|
11223
|
+
* Gets the local scale override.
|
|
11224
|
+
* @returns The scale, or `null` while no override is set.
|
|
11225
|
+
*/
|
|
11226
|
+
get scale() {
|
|
11227
|
+
return this._scale;
|
|
11228
|
+
}
|
|
11229
|
+
/**
|
|
11230
|
+
* Sets the tags override. `null` clears it, restoring the authored tags.
|
|
11231
|
+
* @param value - The tags, or `null`.
|
|
11232
|
+
*/
|
|
11233
|
+
set tags(value) {
|
|
11234
|
+
this._tags = value;
|
|
11235
|
+
const entity = this._state === 'bound' ? this._entity : null;
|
|
11236
|
+
if (!entity) {
|
|
11237
|
+
return;
|
|
11238
|
+
}
|
|
11239
|
+
if (value !== null) {
|
|
11240
|
+
this._authored.tags ??= entity.tags.list().slice();
|
|
11241
|
+
entity.tags.clear();
|
|
11242
|
+
entity.tags.add(value);
|
|
11243
|
+
}
|
|
11244
|
+
else if (this._authored.tags) {
|
|
11245
|
+
entity.tags.clear();
|
|
11246
|
+
entity.tags.add(this._authored.tags);
|
|
11247
|
+
delete this._authored.tags;
|
|
11248
|
+
}
|
|
11249
|
+
}
|
|
11250
|
+
/**
|
|
11251
|
+
* Gets the tags override.
|
|
11252
|
+
* @returns The tags, or `null` while no override is set.
|
|
11253
|
+
*/
|
|
11254
|
+
get tags() {
|
|
11255
|
+
return this._tags;
|
|
11256
|
+
}
|
|
11257
|
+
static get observedAttributes() {
|
|
11258
|
+
return ['enabled', 'index', 'name', 'position', 'rotation', 'scale', 'tags', ...POINTER_ATTRIBUTES];
|
|
11259
|
+
}
|
|
11260
|
+
attributeChangedCallback(name, _oldValue, newValue) {
|
|
11261
|
+
switch (name) {
|
|
11262
|
+
case 'enabled':
|
|
11263
|
+
this.enabled = newValue === null ? null : parseBool(newValue, true);
|
|
11264
|
+
break;
|
|
11265
|
+
case 'index':
|
|
11266
|
+
if (newValue === null) {
|
|
11267
|
+
this.index = null;
|
|
11268
|
+
}
|
|
11269
|
+
else {
|
|
11270
|
+
// Number('') is 0, which would make index="" silently mean the first match
|
|
11271
|
+
const index = newValue.trim() === '' ? NaN : Number(newValue);
|
|
11272
|
+
if (!Number.isInteger(index) || index < 0) {
|
|
11273
|
+
// Invalid values are treated as absent: under ambiguity that means
|
|
11274
|
+
// unbound, the fail-safe direction.
|
|
11275
|
+
console.warn(`pc-node index '${newValue}' is not a non-negative integer - treated as absent`);
|
|
11276
|
+
this.index = null;
|
|
11277
|
+
}
|
|
11278
|
+
else {
|
|
11279
|
+
this.index = index;
|
|
11280
|
+
}
|
|
11281
|
+
}
|
|
11282
|
+
break;
|
|
11283
|
+
case 'name':
|
|
11284
|
+
this.name = newValue ?? '';
|
|
11285
|
+
break;
|
|
11286
|
+
case 'position':
|
|
11287
|
+
this.position = newValue === null ? null : parseVec3(newValue, playcanvas.Vec3.ZERO, name);
|
|
11288
|
+
break;
|
|
11289
|
+
case 'rotation':
|
|
11290
|
+
this.rotation = newValue === null ? null : parseVec3(newValue, playcanvas.Vec3.ZERO, name);
|
|
11291
|
+
break;
|
|
11292
|
+
case 'scale':
|
|
11293
|
+
this.scale = newValue === null ? null : parseVec3(newValue, playcanvas.Vec3.ONE, name);
|
|
11294
|
+
break;
|
|
11295
|
+
case 'tags':
|
|
11296
|
+
this.tags = newValue === null ? null : parseTags(newValue);
|
|
11297
|
+
break;
|
|
11298
|
+
case 'onpointerenter':
|
|
11299
|
+
case 'onpointerleave':
|
|
11300
|
+
case 'onpointerdown':
|
|
11301
|
+
case 'onpointerup':
|
|
11302
|
+
case 'onpointermove':
|
|
11303
|
+
this._updateInlineHandler(name, newValue);
|
|
11304
|
+
break;
|
|
11305
|
+
}
|
|
11306
|
+
}
|
|
11307
|
+
}
|
|
11308
|
+
customElements.define('pc-node', NodeElement);
|
|
11309
|
+
|
|
10015
11310
|
/**
|
|
10016
11311
|
* The SceneElement interface provides properties and methods for manipulating
|
|
10017
11312
|
* {@link https://developer.playcanvas.com/user-manual/web-components/tags/pc-scene/ | `<pc-scene>`} elements.
|
|
@@ -10284,7 +11579,6 @@
|
|
|
10284
11579
|
return;
|
|
10285
11580
|
const source = asset.resource;
|
|
10286
11581
|
const skybox = playcanvas.EnvLighting.generateSkyboxCubemap(source);
|
|
10287
|
-
skybox.anisotropy = 4;
|
|
10288
11582
|
// This element owns what it generated (see _unloadSkybox) - replacing a skybox from an
|
|
10289
11583
|
// earlier load must release it, not orphan it on the GPU
|
|
10290
11584
|
this._scene.skybox?.destroy();
|
|
@@ -10541,6 +11835,7 @@
|
|
|
10541
11835
|
exports.CollisionComponentElement = CollisionComponentElement;
|
|
10542
11836
|
exports.ComponentElement = ComponentElement;
|
|
10543
11837
|
exports.ElementComponentElement = ElementComponentElement;
|
|
11838
|
+
exports.EntityBaseElement = EntityBaseElement;
|
|
10544
11839
|
exports.EntityElement = EntityElement;
|
|
10545
11840
|
exports.GSplatComponentElement = GSplatComponentElement;
|
|
10546
11841
|
exports.LayoutChildComponentElement = LayoutChildComponentElement;
|
|
@@ -10550,6 +11845,7 @@
|
|
|
10550
11845
|
exports.MaterialElement = MaterialElement;
|
|
10551
11846
|
exports.ModelElement = ModelElement;
|
|
10552
11847
|
exports.ModuleElement = ModuleElement;
|
|
11848
|
+
exports.NodeElement = NodeElement;
|
|
10553
11849
|
exports.ParticleSystemComponentElement = ParticleSystemComponentElement;
|
|
10554
11850
|
exports.RenderComponentElement = RenderComponentElement;
|
|
10555
11851
|
exports.RigidBodyComponentElement = RigidBodyComponentElement;
|