@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.js CHANGED
@@ -261,6 +261,157 @@
261
261
  }
262
262
  customElements.define('pc-app', AppElement);
263
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
+
264
415
  /**
265
416
  * Parse a color string into a Color object. String can be in the format of '#rgb', '#rgba',
266
417
  * '#rrggbb', '#rrggbbaa', or a string of 3 or 4 comma-delimited numbers.
@@ -269,10 +420,15 @@
269
420
  * @returns The parsed Color object.
270
421
  */
271
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
+ }
272
428
  if (value.startsWith('#')) {
273
429
  return new playcanvas.Color().fromString(value);
274
430
  }
275
- const components = value.split(',').map(Number);
431
+ const components = value.split(' ').map(Number);
276
432
  return new playcanvas.Color(components);
277
433
  };
278
434
  /**
@@ -282,7 +438,7 @@
282
438
  * @returns The parsed Quat object.
283
439
  */
284
440
  const parseQuat = (value) => {
285
- const [x, y, z] = value.split(',').map(Number);
441
+ const [x, y, z] = value.split(' ').map(Number);
286
442
  const q = new playcanvas.Quat();
287
443
  q.setFromEulerAngles(x, y, z);
288
444
  return q;
@@ -294,7 +450,7 @@
294
450
  * @returns The parsed Vec2 object.
295
451
  */
296
452
  const parseVec2 = (value) => {
297
- const components = value.split(',').map(Number);
453
+ const components = value.split(' ').map(Number);
298
454
  return new playcanvas.Vec2(components);
299
455
  };
300
456
  /**
@@ -304,7 +460,7 @@
304
460
  * @returns The parsed Vec3 object.
305
461
  */
306
462
  const parseVec3 = (value) => {
307
- const components = value.split(',').map(Number);
463
+ const components = value.split(' ').map(Number);
308
464
  return new playcanvas.Vec3(components);
309
465
  };
310
466
  /**
@@ -314,7 +470,7 @@
314
470
  * @returns The parsed Vec4 object.
315
471
  */
316
472
  const parseVec4 = (value) => {
317
- const components = value.split(',').map(Number);
473
+ const components = value.split(' ').map(Number);
318
474
  return new playcanvas.Vec4(components);
319
475
  };
320
476
 
@@ -2364,23 +2520,38 @@
2364
2520
  }
2365
2521
  applyAttributes(script, attributes) {
2366
2522
  try {
2367
- // Parse the attributes string into an object and set them on the script
2368
2523
  const attributesObject = attributes ? JSON.parse(attributes) : {};
2369
- for (const key in attributesObject) {
2370
- const value = attributesObject[key];
2371
- if (Array.isArray(value) && script[key] instanceof playcanvas.Vec2) {
2372
- script[key] = tmpV2.set(value[0], value[1]);
2373
- continue;
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
+ }
2374
2539
  }
2375
- if (Array.isArray(value) && script[key] instanceof playcanvas.Vec3) {
2376
- script[key] = tmpV3.set(value[0], value[1], value[2]);
2377
- continue;
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
+ }
2378
2548
  }
2379
- if (Array.isArray(value) && script[key] instanceof playcanvas.Vec4) {
2380
- script[key] = tmpV4.set(value[0], value[1], value[2], value[3]);
2381
- continue;
2549
+ else {
2550
+ target[key] = value;
2382
2551
  }
2383
- script[key] = value;
2552
+ };
2553
+ for (const key in attributesObject) {
2554
+ applyValue(script, key, attributesObject[key]);
2384
2555
  }
2385
2556
  }
2386
2557
  catch (error) {
@@ -2979,48 +3150,71 @@
2979
3150
  constructor() {
2980
3151
  super(...arguments);
2981
3152
  this._asset = '';
3153
+ this._entity = null;
2982
3154
  }
2983
- async connectedCallback() {
2984
- var _a;
2985
- await ((_a = this.closestApp) === null || _a === void 0 ? void 0 : _a.ready());
2986
- const asset = this.getAttribute('asset');
2987
- if (asset) {
2988
- this.asset = asset;
2989
- }
3155
+ connectedCallback() {
3156
+ this._loadModel();
2990
3157
  this._onReady();
2991
3158
  }
2992
- _loadModel() {
2993
- const asset = AssetElement.get(this._asset);
2994
- if (!asset) {
2995
- return;
2996
- }
2997
- const entity = asset.resource.instantiateRenderEntity();
2998
- if (asset.resource.animations.length > 0) {
2999
- entity.addComponent('anim');
3000
- 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);
3001
3169
  }
3002
3170
  const parentEntityElement = this.closestEntity;
3003
3171
  if (parentEntityElement) {
3004
3172
  parentEntityElement.ready().then(() => {
3005
- parentEntityElement.entity.addChild(entity);
3173
+ parentEntityElement.entity.addChild(this._entity);
3006
3174
  });
3007
3175
  }
3008
3176
  else {
3009
3177
  const appElement = this.closestApp;
3010
3178
  if (appElement) {
3011
3179
  appElement.ready().then(() => {
3012
- appElement.app.root.addChild(entity);
3180
+ appElement.app.root.addChild(this._entity);
3013
3181
  });
3014
3182
  }
3015
3183
  }
3016
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
+ }
3017
3209
  /**
3018
3210
  * Sets the asset ID of the model.
3019
3211
  * @param value - The asset ID.
3020
3212
  */
3021
3213
  set asset(value) {
3022
3214
  this._asset = value;
3023
- this._loadModel();
3215
+ if (this.isConnected) {
3216
+ this._loadModel();
3217
+ }
3024
3218
  }
3025
3219
  /**
3026
3220
  * Gets the source URL of the model.
@@ -3212,48 +3406,76 @@
3212
3406
  this._intensity = 1;
3213
3407
  this._rotation = new playcanvas.Vec3();
3214
3408
  this._level = 0;
3409
+ this._lighting = false;
3215
3410
  this._scale = new playcanvas.Vec3(100, 100, 100);
3216
3411
  this._type = 'infinite';
3412
+ this._scene = null;
3217
3413
  }
3218
- async connectedCallback() {
3219
- var _a;
3220
- await ((_a = this.closestApp) === null || _a === void 0 ? void 0 : _a.ready());
3221
- this.asset = this.getAttribute('asset') || '';
3414
+ connectedCallback() {
3415
+ this._loadSkybox();
3222
3416
  this._onReady();
3223
3417
  }
3224
- getScene() {
3225
- const app = this.closestApp.app;
3226
- if (!app) {
3227
- return;
3228
- }
3229
- return app.scene;
3418
+ disconnectedCallback() {
3419
+ this._unloadSkybox();
3230
3420
  }
3231
- initSkybox(source) {
3421
+ _generateSkybox(asset) {
3422
+ if (!this._scene)
3423
+ return;
3424
+ const source = asset.resource;
3232
3425
  source.anisotropy = 4;
3233
3426
  const skybox = playcanvas.EnvLighting.generateSkyboxCubemap(source);
3234
- const lighting = playcanvas.EnvLighting.generateLightingSource(source);
3235
- const envAtlas = playcanvas.EnvLighting.generateAtlas(lighting);
3236
- const app = this.closestApp.app;
3237
- if (app) {
3238
- app.scene.envAtlas = envAtlas;
3239
- app.scene.skybox = skybox;
3240
- const layer = app.scene.layers.getLayerById(playcanvas.LAYERID_SKYBOX);
3241
- if (layer) {
3242
- layer.enabled = this._type !== 'none';
3243
- }
3244
- app.scene.sky.type = this._type;
3245
- app.scene.sky.node.setLocalScale(this._scale);
3246
- 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;
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);
3247
3461
  }
3248
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;
3474
+ }
3249
3475
  set asset(value) {
3250
3476
  this._asset = value;
3251
- const scene = this.getScene();
3252
- if (scene) {
3253
- const asset = AssetElement.get(value);
3254
- if (asset) {
3255
- this.initSkybox(asset.resource);
3256
- }
3477
+ if (this.isConnected) {
3478
+ this._loadSkybox();
3257
3479
  }
3258
3480
  }
3259
3481
  get asset() {
@@ -3261,9 +3483,8 @@
3261
3483
  }
3262
3484
  set center(value) {
3263
3485
  this._center = value;
3264
- const scene = this.getScene();
3265
- if (scene) {
3266
- scene.sky.center = this._center;
3486
+ if (this._scene) {
3487
+ this._scene.sky.center = this._center;
3267
3488
  }
3268
3489
  }
3269
3490
  get center() {
@@ -3271,19 +3492,32 @@
3271
3492
  }
3272
3493
  set intensity(value) {
3273
3494
  this._intensity = value;
3274
- const scene = this.getScene();
3275
- if (scene) {
3276
- scene.skyboxIntensity = this._intensity;
3495
+ if (this._scene) {
3496
+ this._scene.skyboxIntensity = this._intensity;
3277
3497
  }
3278
3498
  }
3279
3499
  get intensity() {
3280
3500
  return this._intensity;
3281
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
+ }
3282
3517
  set rotation(value) {
3283
3518
  this._rotation = value;
3284
- const scene = this.getScene();
3285
- if (scene) {
3286
- scene.skyboxRotation = new playcanvas.Quat().setFromEulerAngles(value);
3519
+ if (this._scene) {
3520
+ this._scene.skyboxRotation = new playcanvas.Quat().setFromEulerAngles(value);
3287
3521
  }
3288
3522
  }
3289
3523
  get rotation() {
@@ -3291,30 +3525,18 @@
3291
3525
  }
3292
3526
  set scale(value) {
3293
3527
  this._scale = value;
3294
- const scene = this.getScene();
3295
- if (scene) {
3296
- scene.sky.node.setLocalScale(this._scale);
3528
+ if (this._scene) {
3529
+ this._scene.sky.node.setLocalScale(this._scale);
3297
3530
  }
3298
3531
  }
3299
3532
  get scale() {
3300
3533
  return this._scale;
3301
3534
  }
3302
- set level(value) {
3303
- this._level = value;
3304
- const scene = this.getScene();
3305
- if (scene) {
3306
- scene.skyboxMip = this._level;
3307
- }
3308
- }
3309
- get level() {
3310
- return this._level;
3311
- }
3312
3535
  set type(value) {
3313
3536
  this._type = value;
3314
- const scene = this.getScene();
3315
- if (scene) {
3316
- scene.sky.type = this._type;
3317
- 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);
3318
3540
  if (layer) {
3319
3541
  layer.enabled = this._type !== 'none';
3320
3542
  }
@@ -3324,7 +3546,7 @@
3324
3546
  return this._type;
3325
3547
  }
3326
3548
  static get observedAttributes() {
3327
- return ['asset', 'center', 'intensity', 'level', 'rotation', 'scale', 'type'];
3549
+ return ['asset', 'center', 'intensity', 'level', 'lighting', 'rotation', 'scale', 'type'];
3328
3550
  }
3329
3551
  attributeChangedCallback(name, _oldValue, newValue) {
3330
3552
  switch (name) {
@@ -3337,12 +3559,15 @@
3337
3559
  case 'intensity':
3338
3560
  this.intensity = parseFloat(newValue);
3339
3561
  break;
3340
- case 'rotation':
3341
- this.rotation = parseVec3(newValue);
3342
- break;
3343
3562
  case 'level':
3344
3563
  this.level = parseInt(newValue, 10);
3345
3564
  break;
3565
+ case 'lighting':
3566
+ this.lighting = this.hasAttribute(name);
3567
+ break;
3568
+ case 'rotation':
3569
+ this.rotation = parseVec3(newValue);
3570
+ break;
3346
3571
  case 'scale':
3347
3572
  this.scale = parseVec3(newValue);
3348
3573
  break;