@playcanvas/web-components 0.1.6 → 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.cjs CHANGED
@@ -259,6 +259,157 @@ class AppElement extends AsyncElement {
259
259
  }
260
260
  customElements.define('pc-app', AppElement);
261
261
 
262
+ const CSS_COLORS = {
263
+ aliceblue: '#f0f8ff',
264
+ antiquewhite: '#faebd7',
265
+ aqua: '#00ffff',
266
+ aquamarine: '#7fffd4',
267
+ azure: '#f0ffff',
268
+ beige: '#f5f5dc',
269
+ bisque: '#ffe4c4',
270
+ black: '#000000',
271
+ blanchedalmond: '#ffebcd',
272
+ blue: '#0000ff',
273
+ blueviolet: '#8a2be2',
274
+ brown: '#a52a2a',
275
+ burlywood: '#deb887',
276
+ cadetblue: '#5f9ea0',
277
+ chartreuse: '#7fff00',
278
+ chocolate: '#d2691e',
279
+ coral: '#ff7f50',
280
+ cornflowerblue: '#6495ed',
281
+ cornsilk: '#fff8dc',
282
+ crimson: '#dc143c',
283
+ cyan: '#00ffff',
284
+ darkblue: '#00008b',
285
+ darkcyan: '#008b8b',
286
+ darkgoldenrod: '#b8860b',
287
+ darkgray: '#a9a9a9',
288
+ darkgreen: '#006400',
289
+ darkgrey: '#a9a9a9',
290
+ darkkhaki: '#bdb76b',
291
+ darkmagenta: '#8b008b',
292
+ darkolivegreen: '#556b2f',
293
+ darkorange: '#ff8c00',
294
+ darkorchid: '#9932cc',
295
+ darkred: '#8b0000',
296
+ darksalmon: '#e9967a',
297
+ darkseagreen: '#8fbc8f',
298
+ darkslateblue: '#483d8b',
299
+ darkslategray: '#2f4f4f',
300
+ darkslategrey: '#2f4f4f',
301
+ darkturquoise: '#00ced1',
302
+ darkviolet: '#9400d3',
303
+ deeppink: '#ff1493',
304
+ deepskyblue: '#00bfff',
305
+ dimgray: '#696969',
306
+ dimgrey: '#696969',
307
+ dodgerblue: '#1e90ff',
308
+ firebrick: '#b22222',
309
+ floralwhite: '#fffaf0',
310
+ forestgreen: '#228b22',
311
+ fuchsia: '#ff00ff',
312
+ gainsboro: '#dcdcdc',
313
+ ghostwhite: '#f8f8ff',
314
+ gold: '#ffd700',
315
+ goldenrod: '#daa520',
316
+ gray: '#808080',
317
+ green: '#008000',
318
+ greenyellow: '#adff2f',
319
+ grey: '#808080',
320
+ honeydew: '#f0fff0',
321
+ hotpink: '#ff69b4',
322
+ indianred: '#cd5c5c',
323
+ indigo: '#4b0082',
324
+ ivory: '#fffff0',
325
+ khaki: '#f0e68c',
326
+ lavender: '#e6e6fa',
327
+ lavenderblush: '#fff0f5',
328
+ lawngreen: '#7cfc00',
329
+ lemonchiffon: '#fffacd',
330
+ lightblue: '#add8e6',
331
+ lightcoral: '#f08080',
332
+ lightcyan: '#e0ffff',
333
+ lightgoldenrodyellow: '#fafad2',
334
+ lightgray: '#d3d3d3',
335
+ lightgreen: '#90ee90',
336
+ lightgrey: '#d3d3d3',
337
+ lightpink: '#ffb6c1',
338
+ lightsalmon: '#ffa07a',
339
+ lightseagreen: '#20b2aa',
340
+ lightskyblue: '#87cefa',
341
+ lightslategray: '#778899',
342
+ lightslategrey: '#778899',
343
+ lightsteelblue: '#b0c4de',
344
+ lightyellow: '#ffffe0',
345
+ lime: '#00ff00',
346
+ limegreen: '#32cd32',
347
+ linen: '#faf0e6',
348
+ magenta: '#ff00ff',
349
+ maroon: '#800000',
350
+ mediumaquamarine: '#66cdaa',
351
+ mediumblue: '#0000cd',
352
+ mediumorchid: '#ba55d3',
353
+ mediumpurple: '#9370db',
354
+ mediumseagreen: '#3cb371',
355
+ mediumslateblue: '#7b68ee',
356
+ mediumspringgreen: '#00fa9a',
357
+ mediumturquoise: '#48d1cc',
358
+ mediumvioletred: '#c71585',
359
+ midnightblue: '#191970',
360
+ mintcream: '#f5fffa',
361
+ mistyrose: '#ffe4e1',
362
+ moccasin: '#ffe4b5',
363
+ navajowhite: '#ffdead',
364
+ navy: '#000080',
365
+ oldlace: '#fdf5e6',
366
+ olive: '#808000',
367
+ olivedrab: '#6b8e23',
368
+ orange: '#ffa500',
369
+ orangered: '#ff4500',
370
+ orchid: '#da70d6',
371
+ palegoldenrod: '#eee8aa',
372
+ palegreen: '#98fb98',
373
+ paleturquoise: '#afeeee',
374
+ palevioletred: '#db7093',
375
+ papayawhip: '#ffefd5',
376
+ peachpuff: '#ffdab9',
377
+ peru: '#cd853f',
378
+ pink: '#ffc0cb',
379
+ plum: '#dda0dd',
380
+ powderblue: '#b0e0e6',
381
+ purple: '#800080',
382
+ rebeccapurple: '#663399',
383
+ red: '#ff0000',
384
+ rosybrown: '#bc8f8f',
385
+ royalblue: '#4169e1',
386
+ saddlebrown: '#8b4513',
387
+ salmon: '#fa8072',
388
+ sandybrown: '#f4a460',
389
+ seagreen: '#2e8b57',
390
+ seashell: '#fff5ee',
391
+ sienna: '#a0522d',
392
+ silver: '#c0c0c0',
393
+ skyblue: '#87ceeb',
394
+ slateblue: '#6a5acd',
395
+ slategray: '#708090',
396
+ slategrey: '#708090',
397
+ snow: '#fffafa',
398
+ springgreen: '#00ff7f',
399
+ steelblue: '#4682b4',
400
+ tan: '#d2b48c',
401
+ teal: '#008080',
402
+ thistle: '#d8bfd8',
403
+ tomato: '#ff6347',
404
+ turquoise: '#40e0d0',
405
+ violet: '#ee82ee',
406
+ wheat: '#f5deb3',
407
+ white: '#ffffff',
408
+ whitesmoke: '#f5f5f5',
409
+ yellow: '#ffff00',
410
+ yellowgreen: '#9acd32'
411
+ };
412
+
262
413
  /**
263
414
  * Parse a color string into a Color object. String can be in the format of '#rgb', '#rgba',
264
415
  * '#rrggbb', '#rrggbbaa', or a string of 3 or 4 comma-delimited numbers.
@@ -267,10 +418,15 @@ customElements.define('pc-app', AppElement);
267
418
  * @returns The parsed Color object.
268
419
  */
269
420
  const parseColor = (value) => {
421
+ // Check if it's a CSS color name first
422
+ const hexColor = CSS_COLORS[value.toLowerCase()];
423
+ if (hexColor) {
424
+ return new playcanvas.Color().fromString(hexColor);
425
+ }
270
426
  if (value.startsWith('#')) {
271
427
  return new playcanvas.Color().fromString(value);
272
428
  }
273
- const components = value.split(',').map(Number);
429
+ const components = value.split(' ').map(Number);
274
430
  return new playcanvas.Color(components);
275
431
  };
276
432
  /**
@@ -280,7 +436,7 @@ const parseColor = (value) => {
280
436
  * @returns The parsed Quat object.
281
437
  */
282
438
  const parseQuat = (value) => {
283
- const [x, y, z] = value.split(',').map(Number);
439
+ const [x, y, z] = value.split(' ').map(Number);
284
440
  const q = new playcanvas.Quat();
285
441
  q.setFromEulerAngles(x, y, z);
286
442
  return q;
@@ -292,7 +448,7 @@ const parseQuat = (value) => {
292
448
  * @returns The parsed Vec2 object.
293
449
  */
294
450
  const parseVec2 = (value) => {
295
- const components = value.split(',').map(Number);
451
+ const components = value.split(' ').map(Number);
296
452
  return new playcanvas.Vec2(components);
297
453
  };
298
454
  /**
@@ -302,7 +458,7 @@ const parseVec2 = (value) => {
302
458
  * @returns The parsed Vec3 object.
303
459
  */
304
460
  const parseVec3 = (value) => {
305
- const components = value.split(',').map(Number);
461
+ const components = value.split(' ').map(Number);
306
462
  return new playcanvas.Vec3(components);
307
463
  };
308
464
  /**
@@ -312,7 +468,7 @@ const parseVec3 = (value) => {
312
468
  * @returns The parsed Vec4 object.
313
469
  */
314
470
  const parseVec4 = (value) => {
315
- const components = value.split(',').map(Number);
471
+ const components = value.split(' ').map(Number);
316
472
  return new playcanvas.Vec4(components);
317
473
  };
318
474
 
@@ -2362,23 +2518,38 @@ class ScriptComponentElement extends ComponentElement {
2362
2518
  }
2363
2519
  applyAttributes(script, attributes) {
2364
2520
  try {
2365
- // Parse the attributes string into an object and set them on the script
2366
2521
  const attributesObject = attributes ? JSON.parse(attributes) : {};
2367
- for (const key in attributesObject) {
2368
- const value = attributesObject[key];
2369
- if (Array.isArray(value) && script[key] instanceof playcanvas.Vec2) {
2370
- script[key] = tmpV2.set(value[0], value[1]);
2371
- continue;
2522
+ const applyValue = (target, key, value) => {
2523
+ // Handle vectors
2524
+ if (Array.isArray(value)) {
2525
+ if (target[key] instanceof playcanvas.Vec2) {
2526
+ target[key] = tmpV2.set(value[0], value[1]);
2527
+ return;
2528
+ }
2529
+ if (target[key] instanceof playcanvas.Vec3) {
2530
+ target[key] = tmpV3.set(value[0], value[1], value[2]);
2531
+ return;
2532
+ }
2533
+ if (target[key] instanceof playcanvas.Vec4) {
2534
+ target[key] = tmpV4.set(value[0], value[1], value[2], value[3]);
2535
+ return;
2536
+ }
2372
2537
  }
2373
- if (Array.isArray(value) && script[key] instanceof playcanvas.Vec3) {
2374
- script[key] = tmpV3.set(value[0], value[1], value[2]);
2375
- continue;
2538
+ // Handle nested objects
2539
+ if (value && typeof value === 'object' && !Array.isArray(value)) {
2540
+ if (!target[key] || typeof target[key] !== 'object') {
2541
+ target[key] = {};
2542
+ }
2543
+ for (const nestedKey in value) {
2544
+ applyValue(target[key], nestedKey, value[nestedKey]);
2545
+ }
2376
2546
  }
2377
- if (Array.isArray(value) && script[key] instanceof playcanvas.Vec4) {
2378
- script[key] = tmpV4.set(value[0], value[1], value[2], value[3]);
2379
- continue;
2547
+ else {
2548
+ target[key] = value;
2380
2549
  }
2381
- script[key] = value;
2550
+ };
2551
+ for (const key in attributesObject) {
2552
+ applyValue(script, key, attributesObject[key]);
2382
2553
  }
2383
2554
  }
2384
2555
  catch (error) {
@@ -2977,48 +3148,71 @@ class ModelElement extends AsyncElement {
2977
3148
  constructor() {
2978
3149
  super(...arguments);
2979
3150
  this._asset = '';
3151
+ this._entity = null;
2980
3152
  }
2981
- async connectedCallback() {
2982
- var _a;
2983
- await ((_a = this.closestApp) === null || _a === void 0 ? void 0 : _a.ready());
2984
- const asset = this.getAttribute('asset');
2985
- if (asset) {
2986
- this.asset = asset;
2987
- }
3153
+ connectedCallback() {
3154
+ this._loadModel();
2988
3155
  this._onReady();
2989
3156
  }
2990
- _loadModel() {
2991
- const asset = AssetElement.get(this._asset);
2992
- if (!asset) {
2993
- return;
2994
- }
2995
- const entity = asset.resource.instantiateRenderEntity();
2996
- if (asset.resource.animations.length > 0) {
2997
- entity.addComponent('anim');
2998
- entity.anim.assignAnimation('animation', asset.resource.animations[0].resource);
3157
+ disconnectedCallback() {
3158
+ this._unloadModel();
3159
+ }
3160
+ _instantiate(container) {
3161
+ this._entity = container.instantiateRenderEntity();
3162
+ // @ts-ignore
3163
+ if (container.animations.length > 0) {
3164
+ this._entity.addComponent('anim');
3165
+ // @ts-ignore
3166
+ this._entity.anim.assignAnimation('animation', container.animations[0].resource);
2999
3167
  }
3000
3168
  const parentEntityElement = this.closestEntity;
3001
3169
  if (parentEntityElement) {
3002
3170
  parentEntityElement.ready().then(() => {
3003
- parentEntityElement.entity.addChild(entity);
3171
+ parentEntityElement.entity.addChild(this._entity);
3004
3172
  });
3005
3173
  }
3006
3174
  else {
3007
3175
  const appElement = this.closestApp;
3008
3176
  if (appElement) {
3009
3177
  appElement.ready().then(() => {
3010
- appElement.app.root.addChild(entity);
3178
+ appElement.app.root.addChild(this._entity);
3011
3179
  });
3012
3180
  }
3013
3181
  }
3014
3182
  }
3183
+ async _loadModel() {
3184
+ var _a;
3185
+ this._unloadModel();
3186
+ const appElement = await ((_a = this.closestApp) === null || _a === void 0 ? void 0 : _a.ready());
3187
+ const app = appElement === null || appElement === void 0 ? void 0 : appElement.app;
3188
+ const asset = AssetElement.get(this._asset);
3189
+ if (!asset) {
3190
+ return;
3191
+ }
3192
+ if (asset.loaded) {
3193
+ this._instantiate(asset.resource);
3194
+ }
3195
+ else {
3196
+ asset.once('load', () => {
3197
+ this._instantiate(asset.resource);
3198
+ });
3199
+ app.assets.load(asset);
3200
+ }
3201
+ }
3202
+ _unloadModel() {
3203
+ var _a;
3204
+ (_a = this._entity) === null || _a === void 0 ? void 0 : _a.destroy();
3205
+ this._entity = null;
3206
+ }
3015
3207
  /**
3016
3208
  * Sets the asset ID of the model.
3017
3209
  * @param value - The asset ID.
3018
3210
  */
3019
3211
  set asset(value) {
3020
3212
  this._asset = value;
3021
- this._loadModel();
3213
+ if (this.isConnected) {
3214
+ this._loadModel();
3215
+ }
3022
3216
  }
3023
3217
  /**
3024
3218
  * Gets the source URL of the model.
@@ -3210,48 +3404,76 @@ class SkyElement extends AsyncElement {
3210
3404
  this._intensity = 1;
3211
3405
  this._rotation = new playcanvas.Vec3();
3212
3406
  this._level = 0;
3407
+ this._lighting = false;
3213
3408
  this._scale = new playcanvas.Vec3(100, 100, 100);
3214
3409
  this._type = 'infinite';
3410
+ this._scene = null;
3215
3411
  }
3216
- async connectedCallback() {
3217
- var _a;
3218
- await ((_a = this.closestApp) === null || _a === void 0 ? void 0 : _a.ready());
3219
- this.asset = this.getAttribute('asset') || '';
3412
+ connectedCallback() {
3413
+ this._loadSkybox();
3220
3414
  this._onReady();
3221
3415
  }
3222
- getScene() {
3223
- const app = this.closestApp.app;
3224
- if (!app) {
3225
- return;
3226
- }
3227
- return app.scene;
3416
+ disconnectedCallback() {
3417
+ this._unloadSkybox();
3228
3418
  }
3229
- initSkybox(source) {
3419
+ _generateSkybox(asset) {
3420
+ if (!this._scene)
3421
+ return;
3422
+ const source = asset.resource;
3230
3423
  source.anisotropy = 4;
3231
3424
  const skybox = playcanvas.EnvLighting.generateSkyboxCubemap(source);
3232
- const lighting = playcanvas.EnvLighting.generateLightingSource(source);
3233
- const envAtlas = playcanvas.EnvLighting.generateAtlas(lighting);
3234
- const app = this.closestApp.app;
3235
- if (app) {
3236
- app.scene.envAtlas = envAtlas;
3237
- app.scene.skybox = skybox;
3238
- const layer = app.scene.layers.getLayerById(playcanvas.LAYERID_SKYBOX);
3239
- if (layer) {
3240
- layer.enabled = this._type !== 'none';
3241
- }
3242
- app.scene.sky.type = this._type;
3243
- app.scene.sky.node.setLocalScale(this._scale);
3244
- app.scene.sky.center = this._center;
3425
+ this._scene.skybox = skybox;
3426
+ if (this._lighting) {
3427
+ const lighting = playcanvas.EnvLighting.generateLightingSource(source);
3428
+ const envAtlas = playcanvas.EnvLighting.generateAtlas(lighting);
3429
+ this._scene.envAtlas = envAtlas;
3430
+ }
3431
+ const layer = this._scene.layers.getLayerById(playcanvas.LAYERID_SKYBOX);
3432
+ if (layer) {
3433
+ layer.enabled = this._type !== 'none';
3434
+ }
3435
+ this._scene.sky.type = this._type;
3436
+ this._scene.sky.node.setLocalScale(this._scale);
3437
+ this._scene.sky.center = this._center;
3438
+ }
3439
+ async _loadSkybox() {
3440
+ var _a;
3441
+ const appElement = await ((_a = this.closestApp) === null || _a === void 0 ? void 0 : _a.ready());
3442
+ const app = appElement === null || appElement === void 0 ? void 0 : appElement.app;
3443
+ if (!app) {
3444
+ return;
3445
+ }
3446
+ const asset = AssetElement.get(this._asset);
3447
+ if (!asset) {
3448
+ return;
3449
+ }
3450
+ this._scene = app.scene;
3451
+ if (asset.loaded) {
3452
+ this._generateSkybox(asset);
3453
+ }
3454
+ else {
3455
+ asset.once('load', () => {
3456
+ this._generateSkybox(asset);
3457
+ });
3458
+ app.assets.load(asset);
3245
3459
  }
3246
3460
  }
3461
+ _unloadSkybox() {
3462
+ var _a, _b;
3463
+ if (!this._scene)
3464
+ return;
3465
+ (_a = this._scene.skybox) === null || _a === void 0 ? void 0 : _a.destroy();
3466
+ // @ts-ignore
3467
+ this._scene.skybox = null;
3468
+ (_b = this._scene.envAtlas) === null || _b === void 0 ? void 0 : _b.destroy();
3469
+ // @ts-ignore
3470
+ this._scene.envAtlas = null;
3471
+ this._scene = null;
3472
+ }
3247
3473
  set asset(value) {
3248
3474
  this._asset = value;
3249
- const scene = this.getScene();
3250
- if (scene) {
3251
- const asset = AssetElement.get(value);
3252
- if (asset) {
3253
- this.initSkybox(asset.resource);
3254
- }
3475
+ if (this.isConnected) {
3476
+ this._loadSkybox();
3255
3477
  }
3256
3478
  }
3257
3479
  get asset() {
@@ -3259,9 +3481,8 @@ class SkyElement extends AsyncElement {
3259
3481
  }
3260
3482
  set center(value) {
3261
3483
  this._center = value;
3262
- const scene = this.getScene();
3263
- if (scene) {
3264
- scene.sky.center = this._center;
3484
+ if (this._scene) {
3485
+ this._scene.sky.center = this._center;
3265
3486
  }
3266
3487
  }
3267
3488
  get center() {
@@ -3269,19 +3490,32 @@ class SkyElement extends AsyncElement {
3269
3490
  }
3270
3491
  set intensity(value) {
3271
3492
  this._intensity = value;
3272
- const scene = this.getScene();
3273
- if (scene) {
3274
- scene.skyboxIntensity = this._intensity;
3493
+ if (this._scene) {
3494
+ this._scene.skyboxIntensity = this._intensity;
3275
3495
  }
3276
3496
  }
3277
3497
  get intensity() {
3278
3498
  return this._intensity;
3279
3499
  }
3500
+ set level(value) {
3501
+ this._level = value;
3502
+ if (this._scene) {
3503
+ this._scene.skyboxMip = this._level;
3504
+ }
3505
+ }
3506
+ get level() {
3507
+ return this._level;
3508
+ }
3509
+ set lighting(value) {
3510
+ this._lighting = value;
3511
+ }
3512
+ get lighting() {
3513
+ return this._lighting;
3514
+ }
3280
3515
  set rotation(value) {
3281
3516
  this._rotation = value;
3282
- const scene = this.getScene();
3283
- if (scene) {
3284
- scene.skyboxRotation = new playcanvas.Quat().setFromEulerAngles(value);
3517
+ if (this._scene) {
3518
+ this._scene.skyboxRotation = new playcanvas.Quat().setFromEulerAngles(value);
3285
3519
  }
3286
3520
  }
3287
3521
  get rotation() {
@@ -3289,30 +3523,18 @@ class SkyElement extends AsyncElement {
3289
3523
  }
3290
3524
  set scale(value) {
3291
3525
  this._scale = value;
3292
- const scene = this.getScene();
3293
- if (scene) {
3294
- scene.sky.node.setLocalScale(this._scale);
3526
+ if (this._scene) {
3527
+ this._scene.sky.node.setLocalScale(this._scale);
3295
3528
  }
3296
3529
  }
3297
3530
  get scale() {
3298
3531
  return this._scale;
3299
3532
  }
3300
- set level(value) {
3301
- this._level = value;
3302
- const scene = this.getScene();
3303
- if (scene) {
3304
- scene.skyboxMip = this._level;
3305
- }
3306
- }
3307
- get level() {
3308
- return this._level;
3309
- }
3310
3533
  set type(value) {
3311
3534
  this._type = value;
3312
- const scene = this.getScene();
3313
- if (scene) {
3314
- scene.sky.type = this._type;
3315
- const layer = scene.layers.getLayerById(playcanvas.LAYERID_SKYBOX);
3535
+ if (this._scene) {
3536
+ this._scene.sky.type = this._type;
3537
+ const layer = this._scene.layers.getLayerById(playcanvas.LAYERID_SKYBOX);
3316
3538
  if (layer) {
3317
3539
  layer.enabled = this._type !== 'none';
3318
3540
  }
@@ -3322,7 +3544,7 @@ class SkyElement extends AsyncElement {
3322
3544
  return this._type;
3323
3545
  }
3324
3546
  static get observedAttributes() {
3325
- return ['asset', 'center', 'intensity', 'level', 'rotation', 'scale', 'type'];
3547
+ return ['asset', 'center', 'intensity', 'level', 'lighting', 'rotation', 'scale', 'type'];
3326
3548
  }
3327
3549
  attributeChangedCallback(name, _oldValue, newValue) {
3328
3550
  switch (name) {
@@ -3335,12 +3557,15 @@ class SkyElement extends AsyncElement {
3335
3557
  case 'intensity':
3336
3558
  this.intensity = parseFloat(newValue);
3337
3559
  break;
3338
- case 'rotation':
3339
- this.rotation = parseVec3(newValue);
3340
- break;
3341
3560
  case 'level':
3342
3561
  this.level = parseInt(newValue, 10);
3343
3562
  break;
3563
+ case 'lighting':
3564
+ this.lighting = this.hasAttribute(name);
3565
+ break;
3566
+ case 'rotation':
3567
+ this.rotation = parseVec3(newValue);
3568
+ break;
3344
3569
  case 'scale':
3345
3570
  this.scale = parseVec3(newValue);
3346
3571
  break;