@playcanvas/web-components 0.1.1 → 0.1.3

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.
Files changed (39) hide show
  1. package/README.md +260 -1
  2. package/dist/asset.d.ts +1 -0
  3. package/dist/components/camera-component.d.ts +110 -2
  4. package/dist/components/element-component.d.ts +0 -1
  5. package/dist/components/gsplat-component.d.ts +1 -2
  6. package/dist/components/render-component.d.ts +13 -1
  7. package/dist/components/screen-component.d.ts +44 -0
  8. package/dist/components/script-component.d.ts +25 -0
  9. package/dist/components/script.d.ts +12 -6
  10. package/dist/components/sound-slot.d.ts +0 -1
  11. package/dist/index.d.ts +3 -1
  12. package/dist/material.d.ts +29 -0
  13. package/dist/model.d.ts +0 -1
  14. package/dist/pwc.cjs +620 -91
  15. package/dist/pwc.cjs.map +1 -1
  16. package/dist/pwc.js +620 -91
  17. package/dist/pwc.js.map +1 -1
  18. package/dist/pwc.min.js +1 -1
  19. package/dist/pwc.min.js.map +1 -1
  20. package/dist/pwc.mjs +620 -93
  21. package/dist/pwc.mjs.map +1 -1
  22. package/dist/sky.d.ts +0 -1
  23. package/package.json +4 -4
  24. package/src/app.ts +7 -0
  25. package/src/asset.ts +5 -0
  26. package/src/components/camera-component.ts +248 -6
  27. package/src/components/element-component.ts +2 -7
  28. package/src/components/gsplat-component.ts +2 -7
  29. package/src/components/render-component.ts +38 -7
  30. package/src/components/screen-component.ts +153 -0
  31. package/src/components/script-component.ts +120 -1
  32. package/src/components/script.ts +23 -65
  33. package/src/components/sound-component.ts +1 -1
  34. package/src/components/sound-slot.ts +2 -7
  35. package/src/entity.ts +1 -1
  36. package/src/index.ts +4 -0
  37. package/src/material.ts +138 -0
  38. package/src/model.ts +1 -6
  39. package/src/sky.ts +1 -6
package/dist/pwc.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { WasmModule, Application, Keyboard, Mouse, FILLMODE_FILL_WINDOW, RESOLUTION_AUTO, Color, Vec2, Vec3, Vec4, Entity, Asset, PROJECTION_ORTHOGRAPHIC, PROJECTION_PERSPECTIVE, Quat, LAYERID_SKYBOX } from 'playcanvas';
1
+ import { WasmModule, Application, Keyboard, Mouse, FILLMODE_FILL_WINDOW, RESOLUTION_AUTO, Color, Vec2, Vec3, Vec4, Entity, Asset, PROJECTION_ORTHOGRAPHIC, PROJECTION_PERSPECTIVE, StandardMaterial, SCALEMODE_BLEND, SCALEMODE_NONE, Quat, LAYERID_SKYBOX } from 'playcanvas';
2
2
 
3
3
  class ModuleElement extends HTMLElement {
4
4
  constructor() {
@@ -76,6 +76,11 @@ class AppElement extends HTMLElement {
76
76
  this.app.assets.add(asset);
77
77
  }
78
78
  });
79
+ // Get all pc-material elements that are direct children of the pc-app element
80
+ const materialElements = this.querySelectorAll(':scope > pc-material');
81
+ Array.from(materialElements).forEach((materialElement) => {
82
+ materialElement.createMaterial();
83
+ });
79
84
  // Load assets before starting the application
80
85
  this.app.preload(() => {
81
86
  // Start the application
@@ -220,7 +225,7 @@ class EntityElement extends HTMLElement {
220
225
  const app = await appElement.getApplication();
221
226
  // Create a new entity
222
227
  this.entity = new Entity(this._name, app);
223
- if (this.parentElement && this.parentElement.tagName === 'pc-entity' && this.parentElement.entity) {
228
+ if (this.parentElement && this.parentElement.tagName.toLowerCase() === 'pc-entity' && this.parentElement.entity) {
224
229
  this.parentElement.entity.addChild(this.entity);
225
230
  }
226
231
  else {
@@ -455,6 +460,10 @@ class AssetElement extends HTMLElement {
455
460
  get preload() {
456
461
  return this._preload;
457
462
  }
463
+ static get(id) {
464
+ const assetElement = document.querySelector(`pc-asset[id="${id}"]`);
465
+ return assetElement === null || assetElement === void 0 ? void 0 : assetElement.asset;
466
+ }
458
467
  static get observedAttributes() {
459
468
  return ['preload'];
460
469
  }
@@ -576,21 +585,39 @@ class CameraComponentElement extends ComponentElement {
576
585
  */
577
586
  constructor() {
578
587
  super('camera');
579
- this._clearColor = new Color(1, 1, 1, 1);
588
+ this._clearColor = new Color(0.75, 0.75, 0.75, 1);
589
+ this._clearColorBuffer = true;
590
+ this._clearDepthBuffer = true;
591
+ this._clearStencilBuffer = false;
592
+ this._cullFaces = true;
580
593
  this._farClip = 1000;
594
+ this._flipFaces = false;
581
595
  this._fov = 45;
596
+ this._frustumCulling = true;
582
597
  this._nearClip = 0.1;
583
598
  this._orthographic = false;
584
599
  this._orthoHeight = 10;
600
+ this._priority = 0;
601
+ this._rect = new Vec4(0, 0, 1, 1);
602
+ this._scissorRect = new Vec4(0, 0, 1, 1);
585
603
  }
586
604
  getInitialComponentData() {
587
605
  return {
588
606
  clearColor: this._clearColor,
607
+ clearColorBuffer: this._clearColorBuffer,
608
+ clearDepthBuffer: this._clearDepthBuffer,
609
+ clearStencilBuffer: this._clearStencilBuffer,
610
+ cullFaces: this._cullFaces,
589
611
  farClip: this._farClip,
612
+ flipFaces: this._flipFaces,
590
613
  fov: this._fov,
614
+ frustumCulling: this._frustumCulling,
591
615
  nearClip: this._nearClip,
592
- projection: this._orthographic ? PROJECTION_ORTHOGRAPHIC : PROJECTION_PERSPECTIVE,
593
- orthoHeight: this._orthoHeight
616
+ orthographic: this._orthographic,
617
+ orthoHeight: this._orthoHeight,
618
+ priority: this._priority,
619
+ rect: this._rect,
620
+ scissorRect: this._scissorRect
594
621
  };
595
622
  }
596
623
  /**
@@ -617,6 +644,74 @@ class CameraComponentElement extends ComponentElement {
617
644
  get clearColor() {
618
645
  return this._clearColor;
619
646
  }
647
+ /**
648
+ * Sets the clear color buffer of the camera.
649
+ * @param value - The clear color buffer.
650
+ */
651
+ set clearColorBuffer(value) {
652
+ this._clearColorBuffer = value;
653
+ if (this.component) {
654
+ this.component.clearColorBuffer = value;
655
+ }
656
+ }
657
+ /**
658
+ * Gets the clear color buffer of the camera.
659
+ * @returns The clear color buffer.
660
+ */
661
+ get clearColorBuffer() {
662
+ return this._clearColorBuffer;
663
+ }
664
+ /**
665
+ * Sets the clear depth buffer of the camera.
666
+ * @param value - The clear depth buffer.
667
+ */
668
+ set clearDepthBuffer(value) {
669
+ this._clearDepthBuffer = value;
670
+ if (this.component) {
671
+ this.component.clearDepthBuffer = value;
672
+ }
673
+ }
674
+ /**
675
+ * Gets the clear depth buffer of the camera.
676
+ * @returns The clear depth buffer.
677
+ */
678
+ get clearDepthBuffer() {
679
+ return this._clearDepthBuffer;
680
+ }
681
+ /**
682
+ * Sets the clear stencil buffer of the camera.
683
+ * @param value - The clear stencil buffer.
684
+ */
685
+ set clearStencilBuffer(value) {
686
+ this._clearStencilBuffer = value;
687
+ if (this.component) {
688
+ this.component.clearStencilBuffer = value;
689
+ }
690
+ }
691
+ /**
692
+ * Gets the clear stencil buffer of the camera.
693
+ * @returns The clear stencil buffer.
694
+ */
695
+ get clearStencilBuffer() {
696
+ return this._clearStencilBuffer;
697
+ }
698
+ /**
699
+ * Sets the cull faces of the camera.
700
+ * @param value - The cull faces.
701
+ */
702
+ set cullFaces(value) {
703
+ this._cullFaces = value;
704
+ if (this.component) {
705
+ this.component.cullFaces = value;
706
+ }
707
+ }
708
+ /**
709
+ * Gets the cull faces of the camera.
710
+ * @returns The cull faces.
711
+ */
712
+ get cullFaces() {
713
+ return this._cullFaces;
714
+ }
620
715
  /**
621
716
  * Sets the far clip distance of the camera.
622
717
  * @param value - The far clip distance.
@@ -634,6 +729,23 @@ class CameraComponentElement extends ComponentElement {
634
729
  get farClip() {
635
730
  return this._farClip;
636
731
  }
732
+ /**
733
+ * Sets the flip faces of the camera.
734
+ * @param value - The flip faces.
735
+ */
736
+ set flipFaces(value) {
737
+ this._flipFaces = value;
738
+ if (this.component) {
739
+ this.component.flipFaces = value;
740
+ }
741
+ }
742
+ /**
743
+ * Gets the flip faces of the camera.
744
+ * @returns The flip faces.
745
+ */
746
+ get flipFaces() {
747
+ return this._flipFaces;
748
+ }
637
749
  /**
638
750
  * Sets the field of view of the camera.
639
751
  * @param value - The field of view.
@@ -651,6 +763,23 @@ class CameraComponentElement extends ComponentElement {
651
763
  get fov() {
652
764
  return this._fov;
653
765
  }
766
+ /**
767
+ * Sets the frustum culling of the camera.
768
+ * @param value - The frustum culling.
769
+ */
770
+ set frustumCulling(value) {
771
+ this._frustumCulling = value;
772
+ if (this.component) {
773
+ this.component.frustumCulling = value;
774
+ }
775
+ }
776
+ /**
777
+ * Gets the frustum culling of the camera.
778
+ * @returns The frustum culling.
779
+ */
780
+ get frustumCulling() {
781
+ return this._frustumCulling;
782
+ }
654
783
  /**
655
784
  * Sets the near clip distance of the camera.
656
785
  * @param value - The near clip distance.
@@ -702,8 +831,76 @@ class CameraComponentElement extends ComponentElement {
702
831
  get orthoHeight() {
703
832
  return this._orthoHeight;
704
833
  }
834
+ /**
835
+ * Sets the priority of the camera.
836
+ * @param value - The priority.
837
+ */
838
+ set priority(value) {
839
+ this._priority = value;
840
+ if (this.component) {
841
+ this.component.priority = value;
842
+ }
843
+ }
844
+ /**
845
+ * Gets the priority of the camera.
846
+ * @returns The priority.
847
+ */
848
+ get priority() {
849
+ return this._priority;
850
+ }
851
+ /**
852
+ * Sets the rect of the camera.
853
+ * @param value - The rect.
854
+ */
855
+ set rect(value) {
856
+ this._rect = value;
857
+ if (this.component) {
858
+ this.component.rect = value;
859
+ }
860
+ }
861
+ /**
862
+ * Gets the rect of the camera.
863
+ * @returns The rect.
864
+ */
865
+ get rect() {
866
+ return this._rect;
867
+ }
868
+ /**
869
+ * Sets the scissor rect of the camera.
870
+ * @param value - The scissor rect.
871
+ */
872
+ set scissorRect(value) {
873
+ this._scissorRect = value;
874
+ if (this.component) {
875
+ this.component.scissorRect = value;
876
+ }
877
+ }
878
+ /**
879
+ * Gets the scissor rect of the camera.
880
+ * @returns The scissor rect.
881
+ */
882
+ get scissorRect() {
883
+ return this._scissorRect;
884
+ }
705
885
  static get observedAttributes() {
706
- return [...super.observedAttributes, 'clear-color', 'near-clip', 'far-clip', 'fov', 'orthographic', 'ortho-height'];
886
+ return [
887
+ ...super.observedAttributes,
888
+ 'clear-color',
889
+ 'clear-color-buffer',
890
+ 'clear-depth-buffer',
891
+ 'clear-stencil-buffer',
892
+ 'cull-faces',
893
+ 'far-clip',
894
+ 'flip-faces',
895
+ 'fov',
896
+ 'frustum-culling',
897
+ 'near-clip',
898
+ 'orthographic',
899
+ 'ortho-height',
900
+ 'priority',
901
+ 'rect',
902
+ 'scissor-rect'
903
+ ];
707
904
  }
708
905
  attributeChangedCallback(name, _oldValue, newValue) {
709
906
  super.attributeChangedCallback(name, _oldValue, newValue);
@@ -711,12 +908,30 @@ class CameraComponentElement extends ComponentElement {
711
908
  case 'clear-color':
712
909
  this.clearColor = parseColor(newValue);
713
910
  break;
911
+ case 'clear-color-buffer':
912
+ this.clearColorBuffer = newValue !== 'false';
913
+ break;
914
+ case 'clear-depth-buffer':
915
+ this.clearDepthBuffer = newValue !== 'false';
916
+ break;
917
+ case 'clear-stencil-buffer':
918
+ this.clearStencilBuffer = newValue !== 'false';
919
+ break;
920
+ case 'cull-faces':
921
+ this.cullFaces = newValue !== 'false';
922
+ break;
714
923
  case 'far-clip':
715
924
  this.farClip = parseFloat(newValue);
716
925
  break;
926
+ case 'flip-faces':
927
+ this.flipFaces = newValue !== 'true';
928
+ break;
717
929
  case 'fov':
718
930
  this.fov = parseFloat(newValue);
719
931
  break;
932
+ case 'frustum-culling':
933
+ this.frustumCulling = newValue !== 'false';
934
+ break;
720
935
  case 'near-clip':
721
936
  this.nearClip = parseFloat(newValue);
722
937
  break;
@@ -726,6 +941,15 @@ class CameraComponentElement extends ComponentElement {
726
941
  case 'ortho-height':
727
942
  this.orthoHeight = parseFloat(newValue);
728
943
  break;
944
+ case 'priority':
945
+ this.priority = parseFloat(newValue);
946
+ break;
947
+ case 'rect':
948
+ this.rect = parseVec4(newValue);
949
+ break;
950
+ case 'scissor-rect':
951
+ this.scissorRect = parseVec4(newValue);
952
+ break;
729
953
  }
730
954
  }
731
955
  }
@@ -873,16 +1097,12 @@ class ElementComponentElement extends ComponentElement {
873
1097
  await super.connectedCallback();
874
1098
  this.component._text._material.useFog = true;
875
1099
  }
876
- getAsset() {
877
- const assetElement = document.querySelector(`pc-asset[id="${this._asset}"]`);
878
- return assetElement.asset;
879
- }
880
1100
  getInitialComponentData() {
881
1101
  return {
882
1102
  anchor: this._anchor,
883
1103
  autoWidth: this._autoWidth,
884
1104
  color: this._color,
885
- fontAsset: this.getAsset().id,
1105
+ fontAsset: AssetElement.get(this._asset).id,
886
1106
  fontSize: this._fontSize,
887
1107
  lineHeight: this._lineHeight,
888
1108
  pivot: this._pivot,
@@ -910,7 +1130,7 @@ class ElementComponentElement extends ComponentElement {
910
1130
  }
911
1131
  set asset(value) {
912
1132
  this._asset = value;
913
- const asset = this.getAsset();
1133
+ const asset = AssetElement.get(value);
914
1134
  if (this.component && asset) {
915
1135
  this.component.fontAsset = asset.id;
916
1136
  }
@@ -1061,13 +1281,9 @@ class GSplatComponentElement extends ComponentElement {
1061
1281
  super('gsplat');
1062
1282
  this._asset = '';
1063
1283
  }
1064
- getAsset() {
1065
- const assetElement = document.querySelector(`pc-asset[id="${this._asset}"]`);
1066
- return assetElement.asset;
1067
- }
1068
1284
  getInitialComponentData() {
1069
1285
  return {
1070
- asset: this.getAsset()
1286
+ asset: AssetElement.get(this._asset)
1071
1287
  };
1072
1288
  }
1073
1289
  /**
@@ -1079,7 +1295,7 @@ class GSplatComponentElement extends ComponentElement {
1079
1295
  }
1080
1296
  set asset(value) {
1081
1297
  this._asset = value;
1082
- const asset = this.getAsset();
1298
+ const asset = AssetElement.get(value);
1083
1299
  if (this.component && asset) {
1084
1300
  this.component.asset = asset;
1085
1301
  }
@@ -1371,6 +1587,120 @@ class LightComponentElement extends ComponentElement {
1371
1587
  }
1372
1588
  customElements.define('pc-light', LightComponentElement);
1373
1589
 
1590
+ /**
1591
+ * Represents a material in the PlayCanvas engine.
1592
+ */
1593
+ class MaterialElement extends HTMLElement {
1594
+ constructor() {
1595
+ super(...arguments);
1596
+ this._diffuse = new Color(1, 1, 1);
1597
+ this._diffuseMap = '';
1598
+ this._metalnessMap = '';
1599
+ this._normalMap = '';
1600
+ this._roughnessMap = '';
1601
+ this.material = null;
1602
+ }
1603
+ createMaterial() {
1604
+ this.material = new StandardMaterial();
1605
+ this.material.glossInvert = true;
1606
+ this.material.useMetalness = true;
1607
+ this.material.diffuse = this._diffuse;
1608
+ this.diffuseMap = this._diffuseMap;
1609
+ this.metalnessMap = this._metalnessMap;
1610
+ this.normalMap = this._normalMap;
1611
+ this.roughnessMap = this._roughnessMap;
1612
+ this.material.update();
1613
+ }
1614
+ disconnectedCallback() {
1615
+ if (this.material) {
1616
+ this.material.destroy();
1617
+ this.material = null;
1618
+ }
1619
+ }
1620
+ setMap(map, property) {
1621
+ if (this.material) {
1622
+ const asset = AssetElement.get(map);
1623
+ if (asset) {
1624
+ if (asset.loaded) {
1625
+ this.material[property] = asset.resource;
1626
+ this.material[property].anisotropy = 4;
1627
+ }
1628
+ else {
1629
+ asset.once('load', () => {
1630
+ this.material[property] = asset.resource;
1631
+ this.material[property].anisotropy = 4;
1632
+ this.material.update();
1633
+ });
1634
+ }
1635
+ }
1636
+ }
1637
+ }
1638
+ set diffuse(value) {
1639
+ this._diffuse = value;
1640
+ if (this.material) {
1641
+ this.material.diffuse = value;
1642
+ }
1643
+ }
1644
+ get diffuse() {
1645
+ return this._diffuse;
1646
+ }
1647
+ set diffuseMap(value) {
1648
+ this._diffuseMap = value;
1649
+ this.setMap(value, 'diffuseMap');
1650
+ }
1651
+ get diffuseMap() {
1652
+ return this._diffuseMap;
1653
+ }
1654
+ set metalnessMap(value) {
1655
+ this._metalnessMap = value;
1656
+ this.setMap(value, 'metalnessMap');
1657
+ }
1658
+ get metalnessMap() {
1659
+ return this._metalnessMap;
1660
+ }
1661
+ set normalMap(value) {
1662
+ this._normalMap = value;
1663
+ this.setMap(value, 'normalMap');
1664
+ }
1665
+ get normalMap() {
1666
+ return this._normalMap;
1667
+ }
1668
+ set roughnessMap(value) {
1669
+ this._roughnessMap = value;
1670
+ this.setMap(value, 'glossMap');
1671
+ }
1672
+ get roughnessMap() {
1673
+ return this._roughnessMap;
1674
+ }
1675
+ static get(id) {
1676
+ const materialElement = document.querySelector(`pc-material[id="${id}"]`);
1677
+ return materialElement === null || materialElement === void 0 ? void 0 : materialElement.material;
1678
+ }
1679
+ static get observedAttributes() {
1680
+ return ['diffuse', 'diffuse-map', 'metalness-map', 'normal-map', 'roughness-map'];
1681
+ }
1682
+ attributeChangedCallback(name, _oldValue, newValue) {
1683
+ switch (name) {
1684
+ case 'diffuse':
1685
+ this.diffuse = parseColor(newValue);
1686
+ break;
1687
+ case 'diffuse-map':
1688
+ this.diffuseMap = newValue;
1689
+ break;
1690
+ case 'metalness-map':
1691
+ this.metalnessMap = newValue;
1692
+ break;
1693
+ case 'normal-map':
1694
+ this.normalMap = newValue;
1695
+ break;
1696
+ case 'roughness-map':
1697
+ this.roughnessMap = newValue;
1698
+ break;
1699
+ }
1700
+ }
1701
+ }
1702
+ customElements.define('pc-material', MaterialElement);
1703
+
1374
1704
  /**
1375
1705
  * Represents a render component in the PlayCanvas engine.
1376
1706
  *
@@ -1379,9 +1709,14 @@ customElements.define('pc-light', LightComponentElement);
1379
1709
  class RenderComponentElement extends ComponentElement {
1380
1710
  constructor() {
1381
1711
  super('render');
1382
- this._type = 'asset';
1383
1712
  this._castShadows = true;
1713
+ this._material = '';
1384
1714
  this._receiveShadows = true;
1715
+ this._type = 'asset';
1716
+ }
1717
+ async connectedCallback() {
1718
+ await super.connectedCallback();
1719
+ this.material = this._material;
1385
1720
  }
1386
1721
  getInitialComponentData() {
1387
1722
  return {
@@ -1431,6 +1766,23 @@ class RenderComponentElement extends ComponentElement {
1431
1766
  get castShadows() {
1432
1767
  return this._castShadows;
1433
1768
  }
1769
+ /**
1770
+ * Sets the material of the render component.
1771
+ * @param value - The id of the material asset to use.
1772
+ */
1773
+ set material(value) {
1774
+ this._material = value;
1775
+ if (this.component) {
1776
+ this.component.material = MaterialElement.get(value);
1777
+ }
1778
+ }
1779
+ /**
1780
+ * Gets the id of the material asset used by the render component.
1781
+ * @returns The id of the material asset.
1782
+ */
1783
+ get material() {
1784
+ return this._material;
1785
+ }
1434
1786
  /**
1435
1787
  * Sets the receive shadows flag of the render component.
1436
1788
  * @param value - The receive shadows flag.
@@ -1449,20 +1801,23 @@ class RenderComponentElement extends ComponentElement {
1449
1801
  return this._receiveShadows;
1450
1802
  }
1451
1803
  static get observedAttributes() {
1452
- return [...super.observedAttributes, 'cast-shadows', 'receive-shadows', 'type'];
1804
+ return [...super.observedAttributes, 'cast-shadows', 'material', 'receive-shadows', 'type'];
1453
1805
  }
1454
1806
  attributeChangedCallback(name, _oldValue, newValue) {
1455
1807
  super.attributeChangedCallback(name, _oldValue, newValue);
1456
1808
  switch (name) {
1457
- case 'type':
1458
- this.type = newValue;
1459
- break;
1460
1809
  case 'cast-shadows':
1461
1810
  this.castShadows = newValue !== 'false';
1462
1811
  break;
1812
+ case 'material':
1813
+ this.material = newValue;
1814
+ break;
1463
1815
  case 'receive-shadows':
1464
1816
  this.receiveShadows = newValue !== 'false';
1465
1817
  break;
1818
+ case 'type':
1819
+ this.type = newValue;
1820
+ break;
1466
1821
  }
1467
1822
  }
1468
1823
  }
@@ -1656,91 +2011,275 @@ class RigidBodyComponentElement extends ComponentElement {
1656
2011
  customElements.define('pc-rigidbody', RigidBodyComponentElement);
1657
2012
 
1658
2013
  /**
1659
- * Represents a script component in the PlayCanvas engine.
2014
+ * Represents a screen component in the PlayCanvas engine.
1660
2015
  *
1661
2016
  * @category Components
1662
2017
  */
1663
- class ScriptComponentElement extends ComponentElement {
2018
+ class ScreenComponentElement extends ComponentElement {
1664
2019
  constructor() {
1665
- super('script');
2020
+ super('screen');
2021
+ this._screenSpace = false;
2022
+ this._resolution = new Vec2(640, 320);
2023
+ this._referenceResolution = new Vec2(640, 320);
2024
+ this._priority = 0;
2025
+ this._blend = false;
2026
+ this._scaleBlend = 0.5;
2027
+ }
2028
+ getInitialComponentData() {
2029
+ return {
2030
+ priority: this._priority,
2031
+ referenceResolution: this._referenceResolution,
2032
+ resolution: this._resolution,
2033
+ scaleBlend: this._scaleBlend,
2034
+ scaleMode: this._blend ? SCALEMODE_BLEND : SCALEMODE_NONE,
2035
+ screenSpace: this._screenSpace
2036
+ };
1666
2037
  }
1667
2038
  /**
1668
- * Gets the script component.
1669
- * @returns The script component.
2039
+ * Gets the screen component.
2040
+ * @returns The screen component.
1670
2041
  */
1671
2042
  get component() {
1672
2043
  return super.component;
1673
2044
  }
2045
+ set priority(value) {
2046
+ this._priority = value;
2047
+ if (this.component) {
2048
+ this.component.priority = this._priority;
2049
+ }
2050
+ }
2051
+ get priority() {
2052
+ return this._priority;
2053
+ }
2054
+ set referenceResolution(value) {
2055
+ this._referenceResolution = value;
2056
+ if (this.component) {
2057
+ this.component.referenceResolution = this._referenceResolution;
2058
+ }
2059
+ }
2060
+ get referenceResolution() {
2061
+ return this._referenceResolution;
2062
+ }
2063
+ set resolution(value) {
2064
+ this._resolution = value;
2065
+ if (this.component) {
2066
+ this.component.resolution = this._resolution;
2067
+ }
2068
+ }
2069
+ get resolution() {
2070
+ return this._resolution;
2071
+ }
2072
+ set scaleBlend(value) {
2073
+ this._scaleBlend = value;
2074
+ if (this.component) {
2075
+ this.component.scaleBlend = this._scaleBlend;
2076
+ }
2077
+ }
2078
+ get scaleBlend() {
2079
+ return this._scaleBlend;
2080
+ }
2081
+ set blend(value) {
2082
+ this._blend = value;
2083
+ if (this.component) {
2084
+ this.component.scaleMode = this._blend ? SCALEMODE_BLEND : SCALEMODE_NONE;
2085
+ }
2086
+ }
2087
+ get blend() {
2088
+ return this._blend;
2089
+ }
2090
+ set screenSpace(value) {
2091
+ this._screenSpace = value;
2092
+ if (this.component) {
2093
+ this.component.screenSpace = this._screenSpace;
2094
+ }
2095
+ }
2096
+ get screenSpace() {
2097
+ return this._screenSpace;
2098
+ }
2099
+ static get observedAttributes() {
2100
+ return [
2101
+ ...super.observedAttributes,
2102
+ 'blend',
2103
+ 'screen-space',
2104
+ 'resolution',
2105
+ 'reference-resolution',
2106
+ 'priority',
2107
+ 'scale-blend'
2108
+ ];
2109
+ }
2110
+ attributeChangedCallback(name, _oldValue, newValue) {
2111
+ super.attributeChangedCallback(name, _oldValue, newValue);
2112
+ switch (name) {
2113
+ case 'priority':
2114
+ this.priority = parseInt(newValue);
2115
+ break;
2116
+ case 'reference-resolution':
2117
+ this.referenceResolution = parseVec2(newValue);
2118
+ break;
2119
+ case 'resolution':
2120
+ this.resolution = parseVec2(newValue);
2121
+ break;
2122
+ case 'scale-blend':
2123
+ this.scaleBlend = parseFloat(newValue);
2124
+ break;
2125
+ case 'blend':
2126
+ this.blend = this.hasAttribute('blend');
2127
+ break;
2128
+ case 'screen-space':
2129
+ this.screenSpace = this.hasAttribute('screen-space');
2130
+ break;
2131
+ }
2132
+ }
1674
2133
  }
1675
- customElements.define('pc-scripts', ScriptComponentElement);
2134
+ customElements.define('pc-screen', ScreenComponentElement);
1676
2135
 
1677
2136
  /**
1678
- * Represents a script in the PlayCanvas engine.
2137
+ * Represents a script component in the PlayCanvas engine.
2138
+ *
2139
+ * @category Components
1679
2140
  */
1680
- class ScriptElement extends HTMLElement {
2141
+ class ScriptComponentElement extends ComponentElement {
1681
2142
  constructor() {
1682
- super(...arguments);
1683
- this._attributes = {};
1684
- this._enabled = true;
1685
- this._name = '';
1686
- this._script = null;
2143
+ super('script');
2144
+ // Create mutation observer to watch for child script elements
2145
+ this.observer = new MutationObserver(this.handleMutations.bind(this));
2146
+ this.observer.observe(this, {
2147
+ childList: true
2148
+ });
2149
+ // Listen for script attribute and enable changes
2150
+ this.addEventListener('scriptattributeschange', this.handleScriptAttributesChange.bind(this));
2151
+ this.addEventListener('scriptenablechange', this.handleScriptEnableChange.bind(this));
1687
2152
  }
1688
2153
  async connectedCallback() {
1689
- var _a, _b, _c;
1690
- const appElement = this.closest('pc-app');
1691
- if (!appElement) {
1692
- console.error(`${this.tagName.toLowerCase()} should be a descendant of pc-app`);
2154
+ await super.connectedCallback();
2155
+ // Handle initial script elements
2156
+ this.querySelectorAll(':scope > pc-script').forEach((scriptElement) => {
2157
+ const scriptName = scriptElement.getAttribute('name');
2158
+ const attributes = scriptElement.getAttribute('attributes');
2159
+ if (scriptName) {
2160
+ this.createScript(scriptName, attributes);
2161
+ }
2162
+ });
2163
+ }
2164
+ applyAttributes(script, attributes) {
2165
+ try {
2166
+ // Parse the attributes string into an object and set them on the script
2167
+ const attributesObject = attributes ? JSON.parse(attributes) : {};
2168
+ Object.assign(script, attributesObject);
2169
+ }
2170
+ catch (error) {
2171
+ console.error(`Error parsing attributes JSON string ${attributes}:`, error);
2172
+ }
2173
+ }
2174
+ handleScriptAttributesChange(event) {
2175
+ const scriptElement = event.target;
2176
+ const scriptName = scriptElement.getAttribute('name');
2177
+ if (!scriptName || !this.component)
1693
2178
  return;
2179
+ const script = this.component.get(scriptName);
2180
+ if (script) {
2181
+ this.applyAttributes(script, event.detail.attributes);
1694
2182
  }
1695
- await appElement.getApplication();
1696
- const scriptAttributes = this.getAttribute('attributes');
1697
- if (scriptAttributes) {
1698
- try {
1699
- this._attributes = JSON.parse(scriptAttributes);
1700
- }
1701
- catch (e) {
1702
- console.error('Failed to parse script attributes:', e);
1703
- }
2183
+ }
2184
+ handleScriptEnableChange(event) {
2185
+ const scriptElement = event.target;
2186
+ const scriptName = scriptElement.getAttribute('name');
2187
+ if (!scriptName || !this.component)
2188
+ return;
2189
+ const script = this.component.get(scriptName);
2190
+ if (script) {
2191
+ script.enabled = event.detail.enabled;
1704
2192
  }
1705
- // When the script is created, initialize it with the necessary attributes
1706
- (_a = this.scriptsElement) === null || _a === void 0 ? void 0 : _a.component.on(`create:${this._name}`, (scriptInstance) => {
1707
- Object.assign(scriptInstance, this._attributes);
1708
- });
1709
- this._script = (_c = (_b = this.scriptsElement) === null || _b === void 0 ? void 0 : _b.component.create(this._name, { preloading: false })) !== null && _c !== void 0 ? _c : null;
1710
2193
  }
1711
- disconnectedCallback() {
1712
- var _a;
1713
- (_a = this.scriptsElement) === null || _a === void 0 ? void 0 : _a.component.destroy(this._name);
2194
+ createScript(name, attributes) {
2195
+ if (!this.component)
2196
+ return null;
2197
+ this.component.on(`create:${name}`, (script) => {
2198
+ this.applyAttributes(script, attributes);
2199
+ });
2200
+ return this.component.create(name);
1714
2201
  }
1715
- refreshAttributes() {
1716
- if (this._script) {
1717
- Object.entries(this._attributes).forEach(([name, value]) => {
1718
- if (this._script && name in this._script) {
1719
- this._script[name] = value;
2202
+ destroyScript(name) {
2203
+ if (!this.component)
2204
+ return;
2205
+ this.component.destroy(name);
2206
+ }
2207
+ handleMutations(mutations) {
2208
+ for (const mutation of mutations) {
2209
+ // Handle added nodes
2210
+ mutation.addedNodes.forEach((node) => {
2211
+ if (node instanceof HTMLElement && node.tagName.toLowerCase() === 'pc-script') {
2212
+ const scriptName = node.getAttribute('name');
2213
+ const attributes = node.getAttribute('attributes');
2214
+ if (scriptName) {
2215
+ this.createScript(scriptName, attributes);
2216
+ }
2217
+ }
2218
+ });
2219
+ // Handle removed nodes
2220
+ mutation.removedNodes.forEach((node) => {
2221
+ if (node instanceof HTMLElement && node.tagName.toLowerCase() === 'pc-script') {
2222
+ const scriptName = node.getAttribute('name');
2223
+ if (scriptName) {
2224
+ this.destroyScript(scriptName);
2225
+ }
1720
2226
  }
1721
2227
  });
1722
2228
  }
1723
2229
  }
1724
- get scriptsElement() {
1725
- const scriptsElement = this.parentElement;
1726
- if (!(scriptsElement instanceof ScriptComponentElement)) {
1727
- console.warn('pc-script must be a direct child of a pc-scripts element');
1728
- return null;
1729
- }
1730
- return scriptsElement;
2230
+ disconnectedCallback() {
2231
+ var _a;
2232
+ this.observer.disconnect();
2233
+ (_a = super.disconnectedCallback) === null || _a === void 0 ? void 0 : _a.call(this);
1731
2234
  }
2235
+ /**
2236
+ * Gets the script component.
2237
+ * @returns The script component.
2238
+ */
2239
+ get component() {
2240
+ return super.component;
2241
+ }
2242
+ }
2243
+ customElements.define('pc-scripts', ScriptComponentElement);
2244
+
2245
+ /**
2246
+ * Represents a script in the PlayCanvas engine.
2247
+ */
2248
+ class ScriptElement extends HTMLElement {
2249
+ constructor() {
2250
+ super(...arguments);
2251
+ this._attributes = '{}';
2252
+ this._enabled = true;
2253
+ this._name = '';
2254
+ }
2255
+ /**
2256
+ * Sets the attributes of the script.
2257
+ * @param value - The attributes of the script.
2258
+ */
1732
2259
  set scriptAttributes(value) {
1733
- this._attributes = JSON.parse(value);
1734
- this.refreshAttributes();
2260
+ this._attributes = value;
2261
+ this.dispatchEvent(new CustomEvent('scriptattributeschange', {
2262
+ detail: { attributes: value },
2263
+ bubbles: true
2264
+ }));
1735
2265
  }
2266
+ /**
2267
+ * Gets the attributes of the script.
2268
+ * @returns The attributes of the script.
2269
+ */
1736
2270
  get scriptAttributes() {
1737
- return JSON.stringify(this._attributes);
2271
+ return this._attributes;
1738
2272
  }
2273
+ /**
2274
+ * Sets the enabled state of the script.
2275
+ * @param value - The enabled state of the script.
2276
+ */
1739
2277
  set enabled(value) {
1740
2278
  this._enabled = value;
1741
- if (this._script) {
1742
- this._script.enabled = value;
1743
- }
2279
+ this.dispatchEvent(new CustomEvent('scriptenablechange', {
2280
+ detail: { enabled: value },
2281
+ bubbles: true
2282
+ }));
1744
2283
  }
1745
2284
  /**
1746
2285
  * Gets the enabled state of the script.
@@ -1877,7 +2416,7 @@ class SoundComponentElement extends ComponentElement {
1877
2416
  }
1878
2417
  }
1879
2418
  }
1880
- customElements.define('pc-sound', SoundComponentElement);
2419
+ customElements.define('pc-sounds', SoundComponentElement);
1881
2420
 
1882
2421
  /**
1883
2422
  * Represents a sound slot in the PlayCanvas engine.
@@ -1899,10 +2438,6 @@ class SoundSlotElement extends HTMLElement {
1899
2438
  */
1900
2439
  this.soundSlot = null;
1901
2440
  }
1902
- getAsset() {
1903
- const assetElement = document.querySelector(`pc-asset[id="${this._asset}"]`);
1904
- return assetElement.asset;
1905
- }
1906
2441
  async connectedCallback() {
1907
2442
  const appElement = this.closest('pc-app');
1908
2443
  if (!appElement) {
@@ -1944,7 +2479,7 @@ class SoundSlotElement extends HTMLElement {
1944
2479
  var _a;
1945
2480
  this._asset = value;
1946
2481
  if (this.soundSlot) {
1947
- const id = (_a = this.getAsset()) === null || _a === void 0 ? void 0 : _a.id;
2482
+ const id = (_a = AssetElement.get(value)) === null || _a === void 0 ? void 0 : _a.id;
1948
2483
  if (id) {
1949
2484
  this.soundSlot.asset = id;
1950
2485
  }
@@ -2125,7 +2660,7 @@ class SoundSlotElement extends HTMLElement {
2125
2660
  }
2126
2661
  }
2127
2662
  }
2128
- customElements.define('pc-sound-slot', SoundSlotElement);
2663
+ customElements.define('pc-sound', SoundSlotElement);
2129
2664
 
2130
2665
  /**
2131
2666
  * Represents a model in the PlayCanvas engine.
@@ -2148,12 +2683,8 @@ class ModelElement extends HTMLElement {
2148
2683
  this.asset = asset;
2149
2684
  }
2150
2685
  }
2151
- getAsset() {
2152
- const assetElement = document.querySelector(`pc-asset[id="${this._asset}"]`);
2153
- return assetElement.asset;
2154
- }
2155
2686
  _loadModel() {
2156
- const asset = this.getAsset();
2687
+ const asset = AssetElement.get(this._asset);
2157
2688
  if (!asset) {
2158
2689
  return;
2159
2690
  }
@@ -2381,10 +2912,6 @@ class SkyElement extends HTMLElement {
2381
2912
  this.asset = this.getAttribute('asset') || '';
2382
2913
  this.solidColor = this.hasAttribute('solid-color');
2383
2914
  }
2384
- getAsset() {
2385
- const assetElement = document.querySelector(`pc-asset[id="${this._asset}"]`);
2386
- return assetElement.asset;
2387
- }
2388
2915
  getScene() {
2389
2916
  const appElement = this.closest('pc-app');
2390
2917
  if (!appElement) {
@@ -2400,7 +2927,7 @@ class SkyElement extends HTMLElement {
2400
2927
  this._asset = value;
2401
2928
  const scene = this.getScene();
2402
2929
  if (scene) {
2403
- const asset = this.getAsset();
2930
+ const asset = AssetElement.get(value);
2404
2931
  if (asset) {
2405
2932
  if (asset.resource) {
2406
2933
  scene.envAtlas = asset.resource;
@@ -2484,5 +3011,5 @@ class SkyElement extends HTMLElement {
2484
3011
  }
2485
3012
  customElements.define('pc-sky', SkyElement);
2486
3013
 
2487
- export { AppElement, AssetElement, CameraComponentElement, CollisionComponentElement, ComponentElement, ElementComponentElement, EntityElement, GSplatComponentElement, LightComponentElement, ListenerComponentElement, ModelElement, ModuleElement, RenderComponentElement, RigidBodyComponentElement, SceneElement, ScriptComponentElement, ScriptElement, SkyElement, SoundComponentElement, SoundSlotElement };
3014
+ export { AppElement, AssetElement, CameraComponentElement, CollisionComponentElement, ComponentElement, ElementComponentElement, EntityElement, GSplatComponentElement, LightComponentElement, ListenerComponentElement, MaterialElement, ModelElement, ModuleElement, RenderComponentElement, RigidBodyComponentElement, SceneElement, ScreenComponentElement, ScriptComponentElement, ScriptElement, SkyElement, SoundComponentElement, SoundSlotElement };
2488
3015
  //# sourceMappingURL=pwc.mjs.map