@playcanvas/web-components 0.1.5 → 0.1.7

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/dist/pwc.js CHANGED
@@ -49,14 +49,23 @@
49
49
  const glue = this.getAttribute('glue');
50
50
  const wasm = this.getAttribute('wasm');
51
51
  const fallback = this.getAttribute('fallback');
52
- playcanvas.WasmModule.setConfig(name, {
53
- glueUrl: glue,
54
- wasmUrl: wasm,
55
- fallbackUrl: fallback
56
- });
57
- await new Promise((resolve) => {
58
- playcanvas.WasmModule.getInstance(name, () => resolve());
59
- });
52
+ if (name === 'Basis') {
53
+ playcanvas.basisInitialize({
54
+ glueUrl: glue,
55
+ wasmUrl: wasm,
56
+ fallbackUrl: fallback
57
+ });
58
+ }
59
+ else {
60
+ playcanvas.WasmModule.setConfig(name, {
61
+ glueUrl: glue,
62
+ wasmUrl: wasm,
63
+ fallbackUrl: fallback
64
+ });
65
+ await new Promise((resolve) => {
66
+ playcanvas.WasmModule.getInstance(name, () => resolve());
67
+ });
68
+ }
60
69
  }
61
70
  getLoadPromise() {
62
71
  return this.loadPromise;
@@ -77,6 +86,10 @@
77
86
  * The canvas element.
78
87
  */
79
88
  this._canvas = null;
89
+ this._alpha = true;
90
+ this._antialias = true;
91
+ this._depth = true;
92
+ this._stencil = true;
80
93
  this._highResolution = false;
81
94
  /**
82
95
  * The PlayCanvas application instance.
@@ -96,11 +109,15 @@
96
109
  // Initialize the PlayCanvas application
97
110
  this.app = new playcanvas.Application(this._canvas, {
98
111
  graphicsDeviceOptions: {
99
- devicePixelRatio: this._highResolution ? window.devicePixelRatio : 1
112
+ alpha: this._alpha,
113
+ antialias: this._antialias,
114
+ depth: this._depth,
115
+ stencil: this._stencil
100
116
  },
101
117
  keyboard: new playcanvas.Keyboard(window),
102
118
  mouse: new playcanvas.Mouse(this._canvas)
103
119
  });
120
+ this.app.graphicsDevice.maxPixelRatio = this._highResolution ? window.devicePixelRatio : 1;
104
121
  this.app.setCanvasFillMode(playcanvas.FILLMODE_FILL_WINDOW);
105
122
  this.app.setCanvasResolution(playcanvas.RESOLUTION_AUTO);
106
123
  // Get all pc-asset elements that are direct children of the pc-app element
@@ -145,6 +162,48 @@
145
162
  this.app.resizeCanvas();
146
163
  }
147
164
  }
165
+ /**
166
+ * Sets the alpha flag.
167
+ * @param value - The alpha flag.
168
+ */
169
+ set alpha(value) {
170
+ this._alpha = value;
171
+ }
172
+ /**
173
+ * Gets the alpha flag.
174
+ * @returns The alpha flag.
175
+ */
176
+ get alpha() {
177
+ return this._alpha;
178
+ }
179
+ /**
180
+ * Sets the antialias flag.
181
+ * @param value - The antialias flag.
182
+ */
183
+ set antialias(value) {
184
+ this._antialias = value;
185
+ }
186
+ /**
187
+ * Gets the antialias flag.
188
+ * @returns The antialias flag.
189
+ */
190
+ get antialias() {
191
+ return this._antialias;
192
+ }
193
+ /**
194
+ * Sets the depth flag.
195
+ * @param value - The depth flag.
196
+ */
197
+ set depth(value) {
198
+ this._depth = value;
199
+ }
200
+ /**
201
+ * Gets the depth flag.
202
+ * @returns The depth flag.
203
+ */
204
+ get depth() {
205
+ return this._depth;
206
+ }
148
207
  /**
149
208
  * Sets the high resolution flag. When true, the application will render at the device's
150
209
  * physical resolution. When false, the application will render at CSS resolution.
@@ -163,19 +222,196 @@
163
222
  get highResolution() {
164
223
  return this._highResolution;
165
224
  }
225
+ /**
226
+ * Sets the stencil flag.
227
+ * @param value - The stencil flag.
228
+ */
229
+ set stencil(value) {
230
+ this._stencil = value;
231
+ }
232
+ /**
233
+ * Gets the stencil flag.
234
+ * @returns The stencil flag.
235
+ */
236
+ get stencil() {
237
+ return this._stencil;
238
+ }
166
239
  static get observedAttributes() {
167
- return ['high-resolution'];
240
+ return ['alpha', 'antialias', 'depth', 'stencil', 'high-resolution'];
168
241
  }
169
- attributeChangedCallback(name, _oldValue, _newValue) {
242
+ attributeChangedCallback(name, _oldValue, newValue) {
170
243
  switch (name) {
244
+ case 'alpha':
245
+ this.alpha = newValue !== 'false';
246
+ break;
247
+ case 'antialias':
248
+ this.antialias = newValue !== 'false';
249
+ break;
250
+ case 'depth':
251
+ this.depth = newValue !== 'false';
252
+ break;
171
253
  case 'high-resolution':
172
- this.highResolution = this.hasAttribute(name);
254
+ this.highResolution = newValue !== 'false';
255
+ break;
256
+ case 'stencil':
257
+ this.stencil = newValue !== 'false';
173
258
  break;
174
259
  }
175
260
  }
176
261
  }
177
262
  customElements.define('pc-app', AppElement);
178
263
 
264
+ const CSS_COLORS = {
265
+ aliceblue: '#f0f8ff',
266
+ antiquewhite: '#faebd7',
267
+ aqua: '#00ffff',
268
+ aquamarine: '#7fffd4',
269
+ azure: '#f0ffff',
270
+ beige: '#f5f5dc',
271
+ bisque: '#ffe4c4',
272
+ black: '#000000',
273
+ blanchedalmond: '#ffebcd',
274
+ blue: '#0000ff',
275
+ blueviolet: '#8a2be2',
276
+ brown: '#a52a2a',
277
+ burlywood: '#deb887',
278
+ cadetblue: '#5f9ea0',
279
+ chartreuse: '#7fff00',
280
+ chocolate: '#d2691e',
281
+ coral: '#ff7f50',
282
+ cornflowerblue: '#6495ed',
283
+ cornsilk: '#fff8dc',
284
+ crimson: '#dc143c',
285
+ cyan: '#00ffff',
286
+ darkblue: '#00008b',
287
+ darkcyan: '#008b8b',
288
+ darkgoldenrod: '#b8860b',
289
+ darkgray: '#a9a9a9',
290
+ darkgreen: '#006400',
291
+ darkgrey: '#a9a9a9',
292
+ darkkhaki: '#bdb76b',
293
+ darkmagenta: '#8b008b',
294
+ darkolivegreen: '#556b2f',
295
+ darkorange: '#ff8c00',
296
+ darkorchid: '#9932cc',
297
+ darkred: '#8b0000',
298
+ darksalmon: '#e9967a',
299
+ darkseagreen: '#8fbc8f',
300
+ darkslateblue: '#483d8b',
301
+ darkslategray: '#2f4f4f',
302
+ darkslategrey: '#2f4f4f',
303
+ darkturquoise: '#00ced1',
304
+ darkviolet: '#9400d3',
305
+ deeppink: '#ff1493',
306
+ deepskyblue: '#00bfff',
307
+ dimgray: '#696969',
308
+ dimgrey: '#696969',
309
+ dodgerblue: '#1e90ff',
310
+ firebrick: '#b22222',
311
+ floralwhite: '#fffaf0',
312
+ forestgreen: '#228b22',
313
+ fuchsia: '#ff00ff',
314
+ gainsboro: '#dcdcdc',
315
+ ghostwhite: '#f8f8ff',
316
+ gold: '#ffd700',
317
+ goldenrod: '#daa520',
318
+ gray: '#808080',
319
+ green: '#008000',
320
+ greenyellow: '#adff2f',
321
+ grey: '#808080',
322
+ honeydew: '#f0fff0',
323
+ hotpink: '#ff69b4',
324
+ indianred: '#cd5c5c',
325
+ indigo: '#4b0082',
326
+ ivory: '#fffff0',
327
+ khaki: '#f0e68c',
328
+ lavender: '#e6e6fa',
329
+ lavenderblush: '#fff0f5',
330
+ lawngreen: '#7cfc00',
331
+ lemonchiffon: '#fffacd',
332
+ lightblue: '#add8e6',
333
+ lightcoral: '#f08080',
334
+ lightcyan: '#e0ffff',
335
+ lightgoldenrodyellow: '#fafad2',
336
+ lightgray: '#d3d3d3',
337
+ lightgreen: '#90ee90',
338
+ lightgrey: '#d3d3d3',
339
+ lightpink: '#ffb6c1',
340
+ lightsalmon: '#ffa07a',
341
+ lightseagreen: '#20b2aa',
342
+ lightskyblue: '#87cefa',
343
+ lightslategray: '#778899',
344
+ lightslategrey: '#778899',
345
+ lightsteelblue: '#b0c4de',
346
+ lightyellow: '#ffffe0',
347
+ lime: '#00ff00',
348
+ limegreen: '#32cd32',
349
+ linen: '#faf0e6',
350
+ magenta: '#ff00ff',
351
+ maroon: '#800000',
352
+ mediumaquamarine: '#66cdaa',
353
+ mediumblue: '#0000cd',
354
+ mediumorchid: '#ba55d3',
355
+ mediumpurple: '#9370db',
356
+ mediumseagreen: '#3cb371',
357
+ mediumslateblue: '#7b68ee',
358
+ mediumspringgreen: '#00fa9a',
359
+ mediumturquoise: '#48d1cc',
360
+ mediumvioletred: '#c71585',
361
+ midnightblue: '#191970',
362
+ mintcream: '#f5fffa',
363
+ mistyrose: '#ffe4e1',
364
+ moccasin: '#ffe4b5',
365
+ navajowhite: '#ffdead',
366
+ navy: '#000080',
367
+ oldlace: '#fdf5e6',
368
+ olive: '#808000',
369
+ olivedrab: '#6b8e23',
370
+ orange: '#ffa500',
371
+ orangered: '#ff4500',
372
+ orchid: '#da70d6',
373
+ palegoldenrod: '#eee8aa',
374
+ palegreen: '#98fb98',
375
+ paleturquoise: '#afeeee',
376
+ palevioletred: '#db7093',
377
+ papayawhip: '#ffefd5',
378
+ peachpuff: '#ffdab9',
379
+ peru: '#cd853f',
380
+ pink: '#ffc0cb',
381
+ plum: '#dda0dd',
382
+ powderblue: '#b0e0e6',
383
+ purple: '#800080',
384
+ rebeccapurple: '#663399',
385
+ red: '#ff0000',
386
+ rosybrown: '#bc8f8f',
387
+ royalblue: '#4169e1',
388
+ saddlebrown: '#8b4513',
389
+ salmon: '#fa8072',
390
+ sandybrown: '#f4a460',
391
+ seagreen: '#2e8b57',
392
+ seashell: '#fff5ee',
393
+ sienna: '#a0522d',
394
+ silver: '#c0c0c0',
395
+ skyblue: '#87ceeb',
396
+ slateblue: '#6a5acd',
397
+ slategray: '#708090',
398
+ slategrey: '#708090',
399
+ snow: '#fffafa',
400
+ springgreen: '#00ff7f',
401
+ steelblue: '#4682b4',
402
+ tan: '#d2b48c',
403
+ teal: '#008080',
404
+ thistle: '#d8bfd8',
405
+ tomato: '#ff6347',
406
+ turquoise: '#40e0d0',
407
+ violet: '#ee82ee',
408
+ wheat: '#f5deb3',
409
+ white: '#ffffff',
410
+ whitesmoke: '#f5f5f5',
411
+ yellow: '#ffff00',
412
+ yellowgreen: '#9acd32'
413
+ };
414
+
179
415
  /**
180
416
  * Parse a color string into a Color object. String can be in the format of '#rgb', '#rgba',
181
417
  * '#rrggbb', '#rrggbbaa', or a string of 3 or 4 comma-delimited numbers.
@@ -184,10 +420,15 @@
184
420
  * @returns The parsed Color object.
185
421
  */
186
422
  const parseColor = (value) => {
423
+ // Check if it's a CSS color name first
424
+ const hexColor = CSS_COLORS[value.toLowerCase()];
425
+ if (hexColor) {
426
+ return new playcanvas.Color().fromString(hexColor);
427
+ }
187
428
  if (value.startsWith('#')) {
188
429
  return new playcanvas.Color().fromString(value);
189
430
  }
190
- const components = value.split(',').map(Number);
431
+ const components = value.split(' ').map(Number);
191
432
  return new playcanvas.Color(components);
192
433
  };
193
434
  /**
@@ -197,7 +438,7 @@
197
438
  * @returns The parsed Quat object.
198
439
  */
199
440
  const parseQuat = (value) => {
200
- const [x, y, z] = value.split(',').map(Number);
441
+ const [x, y, z] = value.split(' ').map(Number);
201
442
  const q = new playcanvas.Quat();
202
443
  q.setFromEulerAngles(x, y, z);
203
444
  return q;
@@ -209,7 +450,7 @@
209
450
  * @returns The parsed Vec2 object.
210
451
  */
211
452
  const parseVec2 = (value) => {
212
- const components = value.split(',').map(Number);
453
+ const components = value.split(' ').map(Number);
213
454
  return new playcanvas.Vec2(components);
214
455
  };
215
456
  /**
@@ -219,7 +460,7 @@
219
460
  * @returns The parsed Vec3 object.
220
461
  */
221
462
  const parseVec3 = (value) => {
222
- const components = value.split(',').map(Number);
463
+ const components = value.split(' ').map(Number);
223
464
  return new playcanvas.Vec3(components);
224
465
  };
225
466
  /**
@@ -229,7 +470,7 @@
229
470
  * @returns The parsed Vec4 object.
230
471
  */
231
472
  const parseVec4 = (value) => {
232
- const components = value.split(',').map(Number);
473
+ const components = value.split(' ').map(Number);
233
474
  return new playcanvas.Vec4(components);
234
475
  };
235
476
 
@@ -2247,6 +2488,9 @@
2247
2488
  }
2248
2489
  customElements.define('pc-screen', ScreenComponentElement);
2249
2490
 
2491
+ const tmpV2 = new playcanvas.Vec2();
2492
+ const tmpV3 = new playcanvas.Vec3();
2493
+ const tmpV4 = new playcanvas.Vec4();
2250
2494
  /**
2251
2495
  * Represents a script component in the PlayCanvas engine.
2252
2496
  *
@@ -2276,9 +2520,39 @@
2276
2520
  }
2277
2521
  applyAttributes(script, attributes) {
2278
2522
  try {
2279
- // Parse the attributes string into an object and set them on the script
2280
2523
  const attributesObject = attributes ? JSON.parse(attributes) : {};
2281
- Object.assign(script, attributesObject);
2524
+ const applyValue = (target, key, value) => {
2525
+ // Handle vectors
2526
+ if (Array.isArray(value)) {
2527
+ if (target[key] instanceof playcanvas.Vec2) {
2528
+ target[key] = tmpV2.set(value[0], value[1]);
2529
+ return;
2530
+ }
2531
+ if (target[key] instanceof playcanvas.Vec3) {
2532
+ target[key] = tmpV3.set(value[0], value[1], value[2]);
2533
+ return;
2534
+ }
2535
+ if (target[key] instanceof playcanvas.Vec4) {
2536
+ target[key] = tmpV4.set(value[0], value[1], value[2], value[3]);
2537
+ return;
2538
+ }
2539
+ }
2540
+ // Handle nested objects
2541
+ if (value && typeof value === 'object' && !Array.isArray(value)) {
2542
+ if (!target[key] || typeof target[key] !== 'object') {
2543
+ target[key] = {};
2544
+ }
2545
+ for (const nestedKey in value) {
2546
+ applyValue(target[key], nestedKey, value[nestedKey]);
2547
+ }
2548
+ }
2549
+ else {
2550
+ target[key] = value;
2551
+ }
2552
+ };
2553
+ for (const key in attributesObject) {
2554
+ applyValue(script, key, attributesObject[key]);
2555
+ }
2282
2556
  }
2283
2557
  catch (error) {
2284
2558
  console.error(`Error parsing attributes JSON string ${attributes}:`, error);
@@ -2442,14 +2716,22 @@
2442
2716
  class SoundComponentElement extends ComponentElement {
2443
2717
  constructor() {
2444
2718
  super('sound');
2719
+ this._distanceModel = 'linear';
2720
+ this._maxDistance = 10000;
2445
2721
  this._pitch = 1;
2446
2722
  this._positional = false;
2723
+ this._refDistance = 1;
2724
+ this._rollOffFactor = 1;
2447
2725
  this._volume = 1;
2448
2726
  }
2449
2727
  getInitialComponentData() {
2450
2728
  return {
2729
+ distanceModel: this._distanceModel,
2730
+ maxDistance: this._maxDistance,
2451
2731
  pitch: this._pitch,
2452
2732
  positional: this._positional,
2733
+ refDistance: this._refDistance,
2734
+ rollOffFactor: this._rollOffFactor,
2453
2735
  volume: this._volume
2454
2736
  };
2455
2737
  }
@@ -2460,6 +2742,40 @@
2460
2742
  get component() {
2461
2743
  return super.component;
2462
2744
  }
2745
+ /**
2746
+ * Sets which algorithm to use to reduce the volume of the sound as it moves away from the listener.
2747
+ * @param value - The distance model.
2748
+ */
2749
+ set distanceModel(value) {
2750
+ this._distanceModel = value;
2751
+ if (this.component) {
2752
+ this.component.distanceModel = value;
2753
+ }
2754
+ }
2755
+ /**
2756
+ * Gets which algorithm to use to reduce the volume of the sound as it moves away from the listener.
2757
+ * @returns The distance model.
2758
+ */
2759
+ get distanceModel() {
2760
+ return this._distanceModel;
2761
+ }
2762
+ /**
2763
+ * Sets the maximum distance from the listener at which audio falloff stops.
2764
+ * @param value - The max distance.
2765
+ */
2766
+ set maxDistance(value) {
2767
+ this._maxDistance = value;
2768
+ if (this.component) {
2769
+ this.component.maxDistance = value;
2770
+ }
2771
+ }
2772
+ /**
2773
+ * Gets the maximum distance from the listener at which audio falloff stops.
2774
+ * @returns The max distance.
2775
+ */
2776
+ get maxDistance() {
2777
+ return this._maxDistance;
2778
+ }
2463
2779
  /**
2464
2780
  * Sets the pitch of the sound.
2465
2781
  * @param value - The pitch.
@@ -2494,6 +2810,40 @@
2494
2810
  get positional() {
2495
2811
  return this._positional;
2496
2812
  }
2813
+ /**
2814
+ * Sets the reference distance for reducing volume as the sound source moves further from the listener. Defaults to 1.
2815
+ * @param value - The ref distance.
2816
+ */
2817
+ set refDistance(value) {
2818
+ this._refDistance = value;
2819
+ if (this.component) {
2820
+ this.component.refDistance = value;
2821
+ }
2822
+ }
2823
+ /**
2824
+ * Gets the reference distance for reducing volume as the sound source moves further from the listener.
2825
+ * @returns The ref distance.
2826
+ */
2827
+ get refDistance() {
2828
+ return this._refDistance;
2829
+ }
2830
+ /**
2831
+ * Sets the factor used in the falloff equation. Defaults to 1.
2832
+ * @param value - The roll-off factor.
2833
+ */
2834
+ set rollOffFactor(value) {
2835
+ this._rollOffFactor = value;
2836
+ if (this.component) {
2837
+ this.component.rollOffFactor = value;
2838
+ }
2839
+ }
2840
+ /**
2841
+ * Gets the factor used in the falloff equation.
2842
+ * @returns The roll-off factor.
2843
+ */
2844
+ get rollOffFactor() {
2845
+ return this._rollOffFactor;
2846
+ }
2497
2847
  /**
2498
2848
  * Sets the volume of the sound.
2499
2849
  * @param value - The volume.
@@ -2512,17 +2862,38 @@
2512
2862
  return this._volume;
2513
2863
  }
2514
2864
  static get observedAttributes() {
2515
- return [...super.observedAttributes, 'pitch', 'positional', 'volume'];
2865
+ return [
2866
+ ...super.observedAttributes,
2867
+ 'distance-model',
2868
+ 'max-distance',
2869
+ 'pitch',
2870
+ 'positional',
2871
+ 'ref-distance',
2872
+ 'roll-off-factor',
2873
+ 'volume'
2874
+ ];
2516
2875
  }
2517
2876
  attributeChangedCallback(name, _oldValue, newValue) {
2518
2877
  super.attributeChangedCallback(name, _oldValue, newValue);
2519
2878
  switch (name) {
2879
+ case 'distance-model':
2880
+ this.distanceModel = newValue;
2881
+ break;
2882
+ case 'max-distance':
2883
+ this.maxDistance = parseFloat(newValue);
2884
+ break;
2520
2885
  case 'pitch':
2521
2886
  this.pitch = parseFloat(newValue);
2522
2887
  break;
2523
2888
  case 'positional':
2524
2889
  this.positional = this.hasAttribute('positional');
2525
2890
  break;
2891
+ case 'ref-distance':
2892
+ this.refDistance = parseFloat(newValue);
2893
+ break;
2894
+ case 'roll-off-factor':
2895
+ this.rollOffFactor = parseFloat(newValue);
2896
+ break;
2526
2897
  case 'volume':
2527
2898
  this.volume = parseFloat(newValue);
2528
2899
  break;
@@ -2779,48 +3150,71 @@
2779
3150
  constructor() {
2780
3151
  super(...arguments);
2781
3152
  this._asset = '';
3153
+ this._entity = null;
2782
3154
  }
2783
- async connectedCallback() {
2784
- var _a;
2785
- await ((_a = this.closestApp) === null || _a === void 0 ? void 0 : _a.ready());
2786
- const asset = this.getAttribute('asset');
2787
- if (asset) {
2788
- this.asset = asset;
2789
- }
3155
+ connectedCallback() {
3156
+ this._loadModel();
2790
3157
  this._onReady();
2791
3158
  }
2792
- _loadModel() {
2793
- const asset = AssetElement.get(this._asset);
2794
- if (!asset) {
2795
- return;
2796
- }
2797
- const entity = asset.resource.instantiateRenderEntity();
2798
- if (asset.resource.animations.length > 0) {
2799
- entity.addComponent('anim');
2800
- entity.anim.assignAnimation('animation', asset.resource.animations[0].resource);
3159
+ disconnectedCallback() {
3160
+ this._unloadModel();
3161
+ }
3162
+ _instantiate(container) {
3163
+ this._entity = container.instantiateRenderEntity();
3164
+ // @ts-ignore
3165
+ if (container.animations.length > 0) {
3166
+ this._entity.addComponent('anim');
3167
+ // @ts-ignore
3168
+ this._entity.anim.assignAnimation('animation', container.animations[0].resource);
2801
3169
  }
2802
3170
  const parentEntityElement = this.closestEntity;
2803
3171
  if (parentEntityElement) {
2804
3172
  parentEntityElement.ready().then(() => {
2805
- parentEntityElement.entity.addChild(entity);
3173
+ parentEntityElement.entity.addChild(this._entity);
2806
3174
  });
2807
3175
  }
2808
3176
  else {
2809
3177
  const appElement = this.closestApp;
2810
3178
  if (appElement) {
2811
3179
  appElement.ready().then(() => {
2812
- appElement.app.root.addChild(entity);
3180
+ appElement.app.root.addChild(this._entity);
2813
3181
  });
2814
3182
  }
2815
3183
  }
2816
3184
  }
3185
+ async _loadModel() {
3186
+ var _a;
3187
+ this._unloadModel();
3188
+ const appElement = await ((_a = this.closestApp) === null || _a === void 0 ? void 0 : _a.ready());
3189
+ const app = appElement === null || appElement === void 0 ? void 0 : appElement.app;
3190
+ const asset = AssetElement.get(this._asset);
3191
+ if (!asset) {
3192
+ return;
3193
+ }
3194
+ if (asset.loaded) {
3195
+ this._instantiate(asset.resource);
3196
+ }
3197
+ else {
3198
+ asset.once('load', () => {
3199
+ this._instantiate(asset.resource);
3200
+ });
3201
+ app.assets.load(asset);
3202
+ }
3203
+ }
3204
+ _unloadModel() {
3205
+ var _a;
3206
+ (_a = this._entity) === null || _a === void 0 ? void 0 : _a.destroy();
3207
+ this._entity = null;
3208
+ }
2817
3209
  /**
2818
3210
  * Sets the asset ID of the model.
2819
3211
  * @param value - The asset ID.
2820
3212
  */
2821
3213
  set asset(value) {
2822
3214
  this._asset = value;
2823
- this._loadModel();
3215
+ if (this.isConnected) {
3216
+ this._loadModel();
3217
+ }
2824
3218
  }
2825
3219
  /**
2826
3220
  * Gets the source URL of the model.
@@ -3012,48 +3406,76 @@
3012
3406
  this._intensity = 1;
3013
3407
  this._rotation = new playcanvas.Vec3();
3014
3408
  this._level = 0;
3409
+ this._lighting = false;
3015
3410
  this._scale = new playcanvas.Vec3(100, 100, 100);
3016
3411
  this._type = 'infinite';
3412
+ this._scene = null;
3017
3413
  }
3018
- async connectedCallback() {
3019
- var _a;
3020
- await ((_a = this.closestApp) === null || _a === void 0 ? void 0 : _a.ready());
3021
- this.asset = this.getAttribute('asset') || '';
3414
+ connectedCallback() {
3415
+ this._loadSkybox();
3022
3416
  this._onReady();
3023
3417
  }
3024
- getScene() {
3025
- const app = this.closestApp.app;
3026
- if (!app) {
3027
- return;
3028
- }
3029
- return app.scene;
3418
+ disconnectedCallback() {
3419
+ this._unloadSkybox();
3030
3420
  }
3031
- initSkybox(source) {
3421
+ _generateSkybox(asset) {
3422
+ if (!this._scene)
3423
+ return;
3424
+ const source = asset.resource;
3032
3425
  source.anisotropy = 4;
3033
3426
  const skybox = playcanvas.EnvLighting.generateSkyboxCubemap(source);
3034
- const lighting = playcanvas.EnvLighting.generateLightingSource(source);
3035
- const envAtlas = playcanvas.EnvLighting.generateAtlas(lighting);
3036
- const app = this.closestApp.app;
3037
- if (app) {
3038
- app.scene.envAtlas = envAtlas;
3039
- app.scene.skybox = skybox;
3040
- const layer = app.scene.layers.getLayerById(playcanvas.LAYERID_SKYBOX);
3041
- if (layer) {
3042
- layer.enabled = this._type !== 'none';
3043
- }
3044
- app.scene.sky.type = this._type;
3045
- app.scene.sky.node.setLocalScale(this._scale);
3046
- app.scene.sky.center = this._center;
3427
+ this._scene.skybox = skybox;
3428
+ if (this._lighting) {
3429
+ const lighting = playcanvas.EnvLighting.generateLightingSource(source);
3430
+ const envAtlas = playcanvas.EnvLighting.generateAtlas(lighting);
3431
+ this._scene.envAtlas = envAtlas;
3047
3432
  }
3433
+ const layer = this._scene.layers.getLayerById(playcanvas.LAYERID_SKYBOX);
3434
+ if (layer) {
3435
+ layer.enabled = this._type !== 'none';
3436
+ }
3437
+ this._scene.sky.type = this._type;
3438
+ this._scene.sky.node.setLocalScale(this._scale);
3439
+ this._scene.sky.center = this._center;
3440
+ }
3441
+ async _loadSkybox() {
3442
+ var _a;
3443
+ const appElement = await ((_a = this.closestApp) === null || _a === void 0 ? void 0 : _a.ready());
3444
+ const app = appElement === null || appElement === void 0 ? void 0 : appElement.app;
3445
+ if (!app) {
3446
+ return;
3447
+ }
3448
+ const asset = AssetElement.get(this._asset);
3449
+ if (!asset) {
3450
+ return;
3451
+ }
3452
+ this._scene = app.scene;
3453
+ if (asset.loaded) {
3454
+ this._generateSkybox(asset);
3455
+ }
3456
+ else {
3457
+ asset.once('load', () => {
3458
+ this._generateSkybox(asset);
3459
+ });
3460
+ app.assets.load(asset);
3461
+ }
3462
+ }
3463
+ _unloadSkybox() {
3464
+ var _a, _b;
3465
+ if (!this._scene)
3466
+ return;
3467
+ (_a = this._scene.skybox) === null || _a === void 0 ? void 0 : _a.destroy();
3468
+ // @ts-ignore
3469
+ this._scene.skybox = null;
3470
+ (_b = this._scene.envAtlas) === null || _b === void 0 ? void 0 : _b.destroy();
3471
+ // @ts-ignore
3472
+ this._scene.envAtlas = null;
3473
+ this._scene = null;
3048
3474
  }
3049
3475
  set asset(value) {
3050
3476
  this._asset = value;
3051
- const scene = this.getScene();
3052
- if (scene) {
3053
- const asset = AssetElement.get(value);
3054
- if (asset) {
3055
- this.initSkybox(asset.resource);
3056
- }
3477
+ if (this.isConnected) {
3478
+ this._loadSkybox();
3057
3479
  }
3058
3480
  }
3059
3481
  get asset() {
@@ -3061,9 +3483,8 @@
3061
3483
  }
3062
3484
  set center(value) {
3063
3485
  this._center = value;
3064
- const scene = this.getScene();
3065
- if (scene) {
3066
- scene.sky.center = this._center;
3486
+ if (this._scene) {
3487
+ this._scene.sky.center = this._center;
3067
3488
  }
3068
3489
  }
3069
3490
  get center() {
@@ -3071,19 +3492,32 @@
3071
3492
  }
3072
3493
  set intensity(value) {
3073
3494
  this._intensity = value;
3074
- const scene = this.getScene();
3075
- if (scene) {
3076
- scene.skyboxIntensity = this._intensity;
3495
+ if (this._scene) {
3496
+ this._scene.skyboxIntensity = this._intensity;
3077
3497
  }
3078
3498
  }
3079
3499
  get intensity() {
3080
3500
  return this._intensity;
3081
3501
  }
3502
+ set level(value) {
3503
+ this._level = value;
3504
+ if (this._scene) {
3505
+ this._scene.skyboxMip = this._level;
3506
+ }
3507
+ }
3508
+ get level() {
3509
+ return this._level;
3510
+ }
3511
+ set lighting(value) {
3512
+ this._lighting = value;
3513
+ }
3514
+ get lighting() {
3515
+ return this._lighting;
3516
+ }
3082
3517
  set rotation(value) {
3083
3518
  this._rotation = value;
3084
- const scene = this.getScene();
3085
- if (scene) {
3086
- scene.skyboxRotation = new playcanvas.Quat().setFromEulerAngles(value);
3519
+ if (this._scene) {
3520
+ this._scene.skyboxRotation = new playcanvas.Quat().setFromEulerAngles(value);
3087
3521
  }
3088
3522
  }
3089
3523
  get rotation() {
@@ -3091,30 +3525,18 @@
3091
3525
  }
3092
3526
  set scale(value) {
3093
3527
  this._scale = value;
3094
- const scene = this.getScene();
3095
- if (scene) {
3096
- scene.sky.node.setLocalScale(this._scale);
3528
+ if (this._scene) {
3529
+ this._scene.sky.node.setLocalScale(this._scale);
3097
3530
  }
3098
3531
  }
3099
3532
  get scale() {
3100
3533
  return this._scale;
3101
3534
  }
3102
- set level(value) {
3103
- this._level = value;
3104
- const scene = this.getScene();
3105
- if (scene) {
3106
- scene.skyboxMip = this._level;
3107
- }
3108
- }
3109
- get level() {
3110
- return this._level;
3111
- }
3112
3535
  set type(value) {
3113
3536
  this._type = value;
3114
- const scene = this.getScene();
3115
- if (scene) {
3116
- scene.sky.type = this._type;
3117
- const layer = scene.layers.getLayerById(playcanvas.LAYERID_SKYBOX);
3537
+ if (this._scene) {
3538
+ this._scene.sky.type = this._type;
3539
+ const layer = this._scene.layers.getLayerById(playcanvas.LAYERID_SKYBOX);
3118
3540
  if (layer) {
3119
3541
  layer.enabled = this._type !== 'none';
3120
3542
  }
@@ -3124,7 +3546,7 @@
3124
3546
  return this._type;
3125
3547
  }
3126
3548
  static get observedAttributes() {
3127
- return ['asset', 'center', 'intensity', 'level', 'rotation', 'scale', 'type'];
3549
+ return ['asset', 'center', 'intensity', 'level', 'lighting', 'rotation', 'scale', 'type'];
3128
3550
  }
3129
3551
  attributeChangedCallback(name, _oldValue, newValue) {
3130
3552
  switch (name) {
@@ -3137,12 +3559,15 @@
3137
3559
  case 'intensity':
3138
3560
  this.intensity = parseFloat(newValue);
3139
3561
  break;
3140
- case 'rotation':
3141
- this.rotation = parseVec3(newValue);
3142
- break;
3143
3562
  case 'level':
3144
3563
  this.level = parseInt(newValue, 10);
3145
3564
  break;
3565
+ case 'lighting':
3566
+ this.lighting = this.hasAttribute(name);
3567
+ break;
3568
+ case 'rotation':
3569
+ this.rotation = parseVec3(newValue);
3570
+ break;
3146
3571
  case 'scale':
3147
3572
  this.scale = parseVec3(newValue);
3148
3573
  break;