@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.js CHANGED
@@ -80,6 +80,11 @@
80
80
  this.app.assets.add(asset);
81
81
  }
82
82
  });
83
+ // Get all pc-material elements that are direct children of the pc-app element
84
+ const materialElements = this.querySelectorAll(':scope > pc-material');
85
+ Array.from(materialElements).forEach((materialElement) => {
86
+ materialElement.createMaterial();
87
+ });
83
88
  // Load assets before starting the application
84
89
  this.app.preload(() => {
85
90
  // Start the application
@@ -224,7 +229,7 @@
224
229
  const app = await appElement.getApplication();
225
230
  // Create a new entity
226
231
  this.entity = new playcanvas.Entity(this._name, app);
227
- if (this.parentElement && this.parentElement.tagName === 'pc-entity' && this.parentElement.entity) {
232
+ if (this.parentElement && this.parentElement.tagName.toLowerCase() === 'pc-entity' && this.parentElement.entity) {
228
233
  this.parentElement.entity.addChild(this.entity);
229
234
  }
230
235
  else {
@@ -459,6 +464,10 @@
459
464
  get preload() {
460
465
  return this._preload;
461
466
  }
467
+ static get(id) {
468
+ const assetElement = document.querySelector(`pc-asset[id="${id}"]`);
469
+ return assetElement === null || assetElement === void 0 ? void 0 : assetElement.asset;
470
+ }
462
471
  static get observedAttributes() {
463
472
  return ['preload'];
464
473
  }
@@ -580,21 +589,39 @@
580
589
  */
581
590
  constructor() {
582
591
  super('camera');
583
- this._clearColor = new playcanvas.Color(1, 1, 1, 1);
592
+ this._clearColor = new playcanvas.Color(0.75, 0.75, 0.75, 1);
593
+ this._clearColorBuffer = true;
594
+ this._clearDepthBuffer = true;
595
+ this._clearStencilBuffer = false;
596
+ this._cullFaces = true;
584
597
  this._farClip = 1000;
598
+ this._flipFaces = false;
585
599
  this._fov = 45;
600
+ this._frustumCulling = true;
586
601
  this._nearClip = 0.1;
587
602
  this._orthographic = false;
588
603
  this._orthoHeight = 10;
604
+ this._priority = 0;
605
+ this._rect = new playcanvas.Vec4(0, 0, 1, 1);
606
+ this._scissorRect = new playcanvas.Vec4(0, 0, 1, 1);
589
607
  }
590
608
  getInitialComponentData() {
591
609
  return {
592
610
  clearColor: this._clearColor,
611
+ clearColorBuffer: this._clearColorBuffer,
612
+ clearDepthBuffer: this._clearDepthBuffer,
613
+ clearStencilBuffer: this._clearStencilBuffer,
614
+ cullFaces: this._cullFaces,
593
615
  farClip: this._farClip,
616
+ flipFaces: this._flipFaces,
594
617
  fov: this._fov,
618
+ frustumCulling: this._frustumCulling,
595
619
  nearClip: this._nearClip,
596
- projection: this._orthographic ? playcanvas.PROJECTION_ORTHOGRAPHIC : playcanvas.PROJECTION_PERSPECTIVE,
597
- orthoHeight: this._orthoHeight
620
+ orthographic: this._orthographic,
621
+ orthoHeight: this._orthoHeight,
622
+ priority: this._priority,
623
+ rect: this._rect,
624
+ scissorRect: this._scissorRect
598
625
  };
599
626
  }
600
627
  /**
@@ -621,6 +648,74 @@
621
648
  get clearColor() {
622
649
  return this._clearColor;
623
650
  }
651
+ /**
652
+ * Sets the clear color buffer of the camera.
653
+ * @param value - The clear color buffer.
654
+ */
655
+ set clearColorBuffer(value) {
656
+ this._clearColorBuffer = value;
657
+ if (this.component) {
658
+ this.component.clearColorBuffer = value;
659
+ }
660
+ }
661
+ /**
662
+ * Gets the clear color buffer of the camera.
663
+ * @returns The clear color buffer.
664
+ */
665
+ get clearColorBuffer() {
666
+ return this._clearColorBuffer;
667
+ }
668
+ /**
669
+ * Sets the clear depth buffer of the camera.
670
+ * @param value - The clear depth buffer.
671
+ */
672
+ set clearDepthBuffer(value) {
673
+ this._clearDepthBuffer = value;
674
+ if (this.component) {
675
+ this.component.clearDepthBuffer = value;
676
+ }
677
+ }
678
+ /**
679
+ * Gets the clear depth buffer of the camera.
680
+ * @returns The clear depth buffer.
681
+ */
682
+ get clearDepthBuffer() {
683
+ return this._clearDepthBuffer;
684
+ }
685
+ /**
686
+ * Sets the clear stencil buffer of the camera.
687
+ * @param value - The clear stencil buffer.
688
+ */
689
+ set clearStencilBuffer(value) {
690
+ this._clearStencilBuffer = value;
691
+ if (this.component) {
692
+ this.component.clearStencilBuffer = value;
693
+ }
694
+ }
695
+ /**
696
+ * Gets the clear stencil buffer of the camera.
697
+ * @returns The clear stencil buffer.
698
+ */
699
+ get clearStencilBuffer() {
700
+ return this._clearStencilBuffer;
701
+ }
702
+ /**
703
+ * Sets the cull faces of the camera.
704
+ * @param value - The cull faces.
705
+ */
706
+ set cullFaces(value) {
707
+ this._cullFaces = value;
708
+ if (this.component) {
709
+ this.component.cullFaces = value;
710
+ }
711
+ }
712
+ /**
713
+ * Gets the cull faces of the camera.
714
+ * @returns The cull faces.
715
+ */
716
+ get cullFaces() {
717
+ return this._cullFaces;
718
+ }
624
719
  /**
625
720
  * Sets the far clip distance of the camera.
626
721
  * @param value - The far clip distance.
@@ -638,6 +733,23 @@
638
733
  get farClip() {
639
734
  return this._farClip;
640
735
  }
736
+ /**
737
+ * Sets the flip faces of the camera.
738
+ * @param value - The flip faces.
739
+ */
740
+ set flipFaces(value) {
741
+ this._flipFaces = value;
742
+ if (this.component) {
743
+ this.component.flipFaces = value;
744
+ }
745
+ }
746
+ /**
747
+ * Gets the flip faces of the camera.
748
+ * @returns The flip faces.
749
+ */
750
+ get flipFaces() {
751
+ return this._flipFaces;
752
+ }
641
753
  /**
642
754
  * Sets the field of view of the camera.
643
755
  * @param value - The field of view.
@@ -655,6 +767,23 @@
655
767
  get fov() {
656
768
  return this._fov;
657
769
  }
770
+ /**
771
+ * Sets the frustum culling of the camera.
772
+ * @param value - The frustum culling.
773
+ */
774
+ set frustumCulling(value) {
775
+ this._frustumCulling = value;
776
+ if (this.component) {
777
+ this.component.frustumCulling = value;
778
+ }
779
+ }
780
+ /**
781
+ * Gets the frustum culling of the camera.
782
+ * @returns The frustum culling.
783
+ */
784
+ get frustumCulling() {
785
+ return this._frustumCulling;
786
+ }
658
787
  /**
659
788
  * Sets the near clip distance of the camera.
660
789
  * @param value - The near clip distance.
@@ -706,8 +835,76 @@
706
835
  get orthoHeight() {
707
836
  return this._orthoHeight;
708
837
  }
838
+ /**
839
+ * Sets the priority of the camera.
840
+ * @param value - The priority.
841
+ */
842
+ set priority(value) {
843
+ this._priority = value;
844
+ if (this.component) {
845
+ this.component.priority = value;
846
+ }
847
+ }
848
+ /**
849
+ * Gets the priority of the camera.
850
+ * @returns The priority.
851
+ */
852
+ get priority() {
853
+ return this._priority;
854
+ }
855
+ /**
856
+ * Sets the rect of the camera.
857
+ * @param value - The rect.
858
+ */
859
+ set rect(value) {
860
+ this._rect = value;
861
+ if (this.component) {
862
+ this.component.rect = value;
863
+ }
864
+ }
865
+ /**
866
+ * Gets the rect of the camera.
867
+ * @returns The rect.
868
+ */
869
+ get rect() {
870
+ return this._rect;
871
+ }
872
+ /**
873
+ * Sets the scissor rect of the camera.
874
+ * @param value - The scissor rect.
875
+ */
876
+ set scissorRect(value) {
877
+ this._scissorRect = value;
878
+ if (this.component) {
879
+ this.component.scissorRect = value;
880
+ }
881
+ }
882
+ /**
883
+ * Gets the scissor rect of the camera.
884
+ * @returns The scissor rect.
885
+ */
886
+ get scissorRect() {
887
+ return this._scissorRect;
888
+ }
709
889
  static get observedAttributes() {
710
- return [...super.observedAttributes, 'clear-color', 'near-clip', 'far-clip', 'fov', 'orthographic', 'ortho-height'];
890
+ return [
891
+ ...super.observedAttributes,
892
+ 'clear-color',
893
+ 'clear-color-buffer',
894
+ 'clear-depth-buffer',
895
+ 'clear-stencil-buffer',
896
+ 'cull-faces',
897
+ 'far-clip',
898
+ 'flip-faces',
899
+ 'fov',
900
+ 'frustum-culling',
901
+ 'near-clip',
902
+ 'orthographic',
903
+ 'ortho-height',
904
+ 'priority',
905
+ 'rect',
906
+ 'scissor-rect'
907
+ ];
711
908
  }
712
909
  attributeChangedCallback(name, _oldValue, newValue) {
713
910
  super.attributeChangedCallback(name, _oldValue, newValue);
@@ -715,12 +912,30 @@
715
912
  case 'clear-color':
716
913
  this.clearColor = parseColor(newValue);
717
914
  break;
915
+ case 'clear-color-buffer':
916
+ this.clearColorBuffer = newValue !== 'false';
917
+ break;
918
+ case 'clear-depth-buffer':
919
+ this.clearDepthBuffer = newValue !== 'false';
920
+ break;
921
+ case 'clear-stencil-buffer':
922
+ this.clearStencilBuffer = newValue !== 'false';
923
+ break;
924
+ case 'cull-faces':
925
+ this.cullFaces = newValue !== 'false';
926
+ break;
718
927
  case 'far-clip':
719
928
  this.farClip = parseFloat(newValue);
720
929
  break;
930
+ case 'flip-faces':
931
+ this.flipFaces = newValue !== 'true';
932
+ break;
721
933
  case 'fov':
722
934
  this.fov = parseFloat(newValue);
723
935
  break;
936
+ case 'frustum-culling':
937
+ this.frustumCulling = newValue !== 'false';
938
+ break;
724
939
  case 'near-clip':
725
940
  this.nearClip = parseFloat(newValue);
726
941
  break;
@@ -730,6 +945,15 @@
730
945
  case 'ortho-height':
731
946
  this.orthoHeight = parseFloat(newValue);
732
947
  break;
948
+ case 'priority':
949
+ this.priority = parseFloat(newValue);
950
+ break;
951
+ case 'rect':
952
+ this.rect = parseVec4(newValue);
953
+ break;
954
+ case 'scissor-rect':
955
+ this.scissorRect = parseVec4(newValue);
956
+ break;
733
957
  }
734
958
  }
735
959
  }
@@ -877,16 +1101,12 @@
877
1101
  await super.connectedCallback();
878
1102
  this.component._text._material.useFog = true;
879
1103
  }
880
- getAsset() {
881
- const assetElement = document.querySelector(`pc-asset[id="${this._asset}"]`);
882
- return assetElement.asset;
883
- }
884
1104
  getInitialComponentData() {
885
1105
  return {
886
1106
  anchor: this._anchor,
887
1107
  autoWidth: this._autoWidth,
888
1108
  color: this._color,
889
- fontAsset: this.getAsset().id,
1109
+ fontAsset: AssetElement.get(this._asset).id,
890
1110
  fontSize: this._fontSize,
891
1111
  lineHeight: this._lineHeight,
892
1112
  pivot: this._pivot,
@@ -914,7 +1134,7 @@
914
1134
  }
915
1135
  set asset(value) {
916
1136
  this._asset = value;
917
- const asset = this.getAsset();
1137
+ const asset = AssetElement.get(value);
918
1138
  if (this.component && asset) {
919
1139
  this.component.fontAsset = asset.id;
920
1140
  }
@@ -1065,13 +1285,9 @@
1065
1285
  super('gsplat');
1066
1286
  this._asset = '';
1067
1287
  }
1068
- getAsset() {
1069
- const assetElement = document.querySelector(`pc-asset[id="${this._asset}"]`);
1070
- return assetElement.asset;
1071
- }
1072
1288
  getInitialComponentData() {
1073
1289
  return {
1074
- asset: this.getAsset()
1290
+ asset: AssetElement.get(this._asset)
1075
1291
  };
1076
1292
  }
1077
1293
  /**
@@ -1083,7 +1299,7 @@
1083
1299
  }
1084
1300
  set asset(value) {
1085
1301
  this._asset = value;
1086
- const asset = this.getAsset();
1302
+ const asset = AssetElement.get(value);
1087
1303
  if (this.component && asset) {
1088
1304
  this.component.asset = asset;
1089
1305
  }
@@ -1375,6 +1591,120 @@
1375
1591
  }
1376
1592
  customElements.define('pc-light', LightComponentElement);
1377
1593
 
1594
+ /**
1595
+ * Represents a material in the PlayCanvas engine.
1596
+ */
1597
+ class MaterialElement extends HTMLElement {
1598
+ constructor() {
1599
+ super(...arguments);
1600
+ this._diffuse = new playcanvas.Color(1, 1, 1);
1601
+ this._diffuseMap = '';
1602
+ this._metalnessMap = '';
1603
+ this._normalMap = '';
1604
+ this._roughnessMap = '';
1605
+ this.material = null;
1606
+ }
1607
+ createMaterial() {
1608
+ this.material = new playcanvas.StandardMaterial();
1609
+ this.material.glossInvert = true;
1610
+ this.material.useMetalness = true;
1611
+ this.material.diffuse = this._diffuse;
1612
+ this.diffuseMap = this._diffuseMap;
1613
+ this.metalnessMap = this._metalnessMap;
1614
+ this.normalMap = this._normalMap;
1615
+ this.roughnessMap = this._roughnessMap;
1616
+ this.material.update();
1617
+ }
1618
+ disconnectedCallback() {
1619
+ if (this.material) {
1620
+ this.material.destroy();
1621
+ this.material = null;
1622
+ }
1623
+ }
1624
+ setMap(map, property) {
1625
+ if (this.material) {
1626
+ const asset = AssetElement.get(map);
1627
+ if (asset) {
1628
+ if (asset.loaded) {
1629
+ this.material[property] = asset.resource;
1630
+ this.material[property].anisotropy = 4;
1631
+ }
1632
+ else {
1633
+ asset.once('load', () => {
1634
+ this.material[property] = asset.resource;
1635
+ this.material[property].anisotropy = 4;
1636
+ this.material.update();
1637
+ });
1638
+ }
1639
+ }
1640
+ }
1641
+ }
1642
+ set diffuse(value) {
1643
+ this._diffuse = value;
1644
+ if (this.material) {
1645
+ this.material.diffuse = value;
1646
+ }
1647
+ }
1648
+ get diffuse() {
1649
+ return this._diffuse;
1650
+ }
1651
+ set diffuseMap(value) {
1652
+ this._diffuseMap = value;
1653
+ this.setMap(value, 'diffuseMap');
1654
+ }
1655
+ get diffuseMap() {
1656
+ return this._diffuseMap;
1657
+ }
1658
+ set metalnessMap(value) {
1659
+ this._metalnessMap = value;
1660
+ this.setMap(value, 'metalnessMap');
1661
+ }
1662
+ get metalnessMap() {
1663
+ return this._metalnessMap;
1664
+ }
1665
+ set normalMap(value) {
1666
+ this._normalMap = value;
1667
+ this.setMap(value, 'normalMap');
1668
+ }
1669
+ get normalMap() {
1670
+ return this._normalMap;
1671
+ }
1672
+ set roughnessMap(value) {
1673
+ this._roughnessMap = value;
1674
+ this.setMap(value, 'glossMap');
1675
+ }
1676
+ get roughnessMap() {
1677
+ return this._roughnessMap;
1678
+ }
1679
+ static get(id) {
1680
+ const materialElement = document.querySelector(`pc-material[id="${id}"]`);
1681
+ return materialElement === null || materialElement === void 0 ? void 0 : materialElement.material;
1682
+ }
1683
+ static get observedAttributes() {
1684
+ return ['diffuse', 'diffuse-map', 'metalness-map', 'normal-map', 'roughness-map'];
1685
+ }
1686
+ attributeChangedCallback(name, _oldValue, newValue) {
1687
+ switch (name) {
1688
+ case 'diffuse':
1689
+ this.diffuse = parseColor(newValue);
1690
+ break;
1691
+ case 'diffuse-map':
1692
+ this.diffuseMap = newValue;
1693
+ break;
1694
+ case 'metalness-map':
1695
+ this.metalnessMap = newValue;
1696
+ break;
1697
+ case 'normal-map':
1698
+ this.normalMap = newValue;
1699
+ break;
1700
+ case 'roughness-map':
1701
+ this.roughnessMap = newValue;
1702
+ break;
1703
+ }
1704
+ }
1705
+ }
1706
+ customElements.define('pc-material', MaterialElement);
1707
+
1378
1708
  /**
1379
1709
  * Represents a render component in the PlayCanvas engine.
1380
1710
  *
@@ -1383,9 +1713,14 @@
1383
1713
  class RenderComponentElement extends ComponentElement {
1384
1714
  constructor() {
1385
1715
  super('render');
1386
- this._type = 'asset';
1387
1716
  this._castShadows = true;
1717
+ this._material = '';
1388
1718
  this._receiveShadows = true;
1719
+ this._type = 'asset';
1720
+ }
1721
+ async connectedCallback() {
1722
+ await super.connectedCallback();
1723
+ this.material = this._material;
1389
1724
  }
1390
1725
  getInitialComponentData() {
1391
1726
  return {
@@ -1435,6 +1770,23 @@
1435
1770
  get castShadows() {
1436
1771
  return this._castShadows;
1437
1772
  }
1773
+ /**
1774
+ * Sets the material of the render component.
1775
+ * @param value - The id of the material asset to use.
1776
+ */
1777
+ set material(value) {
1778
+ this._material = value;
1779
+ if (this.component) {
1780
+ this.component.material = MaterialElement.get(value);
1781
+ }
1782
+ }
1783
+ /**
1784
+ * Gets the id of the material asset used by the render component.
1785
+ * @returns The id of the material asset.
1786
+ */
1787
+ get material() {
1788
+ return this._material;
1789
+ }
1438
1790
  /**
1439
1791
  * Sets the receive shadows flag of the render component.
1440
1792
  * @param value - The receive shadows flag.
@@ -1453,20 +1805,23 @@
1453
1805
  return this._receiveShadows;
1454
1806
  }
1455
1807
  static get observedAttributes() {
1456
- return [...super.observedAttributes, 'cast-shadows', 'receive-shadows', 'type'];
1808
+ return [...super.observedAttributes, 'cast-shadows', 'material', 'receive-shadows', 'type'];
1457
1809
  }
1458
1810
  attributeChangedCallback(name, _oldValue, newValue) {
1459
1811
  super.attributeChangedCallback(name, _oldValue, newValue);
1460
1812
  switch (name) {
1461
- case 'type':
1462
- this.type = newValue;
1463
- break;
1464
1813
  case 'cast-shadows':
1465
1814
  this.castShadows = newValue !== 'false';
1466
1815
  break;
1816
+ case 'material':
1817
+ this.material = newValue;
1818
+ break;
1467
1819
  case 'receive-shadows':
1468
1820
  this.receiveShadows = newValue !== 'false';
1469
1821
  break;
1822
+ case 'type':
1823
+ this.type = newValue;
1824
+ break;
1470
1825
  }
1471
1826
  }
1472
1827
  }
@@ -1660,91 +2015,275 @@
1660
2015
  customElements.define('pc-rigidbody', RigidBodyComponentElement);
1661
2016
 
1662
2017
  /**
1663
- * Represents a script component in the PlayCanvas engine.
2018
+ * Represents a screen component in the PlayCanvas engine.
1664
2019
  *
1665
2020
  * @category Components
1666
2021
  */
1667
- class ScriptComponentElement extends ComponentElement {
2022
+ class ScreenComponentElement extends ComponentElement {
1668
2023
  constructor() {
1669
- super('script');
2024
+ super('screen');
2025
+ this._screenSpace = false;
2026
+ this._resolution = new playcanvas.Vec2(640, 320);
2027
+ this._referenceResolution = new playcanvas.Vec2(640, 320);
2028
+ this._priority = 0;
2029
+ this._blend = false;
2030
+ this._scaleBlend = 0.5;
2031
+ }
2032
+ getInitialComponentData() {
2033
+ return {
2034
+ priority: this._priority,
2035
+ referenceResolution: this._referenceResolution,
2036
+ resolution: this._resolution,
2037
+ scaleBlend: this._scaleBlend,
2038
+ scaleMode: this._blend ? playcanvas.SCALEMODE_BLEND : playcanvas.SCALEMODE_NONE,
2039
+ screenSpace: this._screenSpace
2040
+ };
1670
2041
  }
1671
2042
  /**
1672
- * Gets the script component.
1673
- * @returns The script component.
2043
+ * Gets the screen component.
2044
+ * @returns The screen component.
1674
2045
  */
1675
2046
  get component() {
1676
2047
  return super.component;
1677
2048
  }
2049
+ set priority(value) {
2050
+ this._priority = value;
2051
+ if (this.component) {
2052
+ this.component.priority = this._priority;
2053
+ }
2054
+ }
2055
+ get priority() {
2056
+ return this._priority;
2057
+ }
2058
+ set referenceResolution(value) {
2059
+ this._referenceResolution = value;
2060
+ if (this.component) {
2061
+ this.component.referenceResolution = this._referenceResolution;
2062
+ }
2063
+ }
2064
+ get referenceResolution() {
2065
+ return this._referenceResolution;
2066
+ }
2067
+ set resolution(value) {
2068
+ this._resolution = value;
2069
+ if (this.component) {
2070
+ this.component.resolution = this._resolution;
2071
+ }
2072
+ }
2073
+ get resolution() {
2074
+ return this._resolution;
2075
+ }
2076
+ set scaleBlend(value) {
2077
+ this._scaleBlend = value;
2078
+ if (this.component) {
2079
+ this.component.scaleBlend = this._scaleBlend;
2080
+ }
2081
+ }
2082
+ get scaleBlend() {
2083
+ return this._scaleBlend;
2084
+ }
2085
+ set blend(value) {
2086
+ this._blend = value;
2087
+ if (this.component) {
2088
+ this.component.scaleMode = this._blend ? playcanvas.SCALEMODE_BLEND : playcanvas.SCALEMODE_NONE;
2089
+ }
2090
+ }
2091
+ get blend() {
2092
+ return this._blend;
2093
+ }
2094
+ set screenSpace(value) {
2095
+ this._screenSpace = value;
2096
+ if (this.component) {
2097
+ this.component.screenSpace = this._screenSpace;
2098
+ }
2099
+ }
2100
+ get screenSpace() {
2101
+ return this._screenSpace;
2102
+ }
2103
+ static get observedAttributes() {
2104
+ return [
2105
+ ...super.observedAttributes,
2106
+ 'blend',
2107
+ 'screen-space',
2108
+ 'resolution',
2109
+ 'reference-resolution',
2110
+ 'priority',
2111
+ 'scale-blend'
2112
+ ];
2113
+ }
2114
+ attributeChangedCallback(name, _oldValue, newValue) {
2115
+ super.attributeChangedCallback(name, _oldValue, newValue);
2116
+ switch (name) {
2117
+ case 'priority':
2118
+ this.priority = parseInt(newValue);
2119
+ break;
2120
+ case 'reference-resolution':
2121
+ this.referenceResolution = parseVec2(newValue);
2122
+ break;
2123
+ case 'resolution':
2124
+ this.resolution = parseVec2(newValue);
2125
+ break;
2126
+ case 'scale-blend':
2127
+ this.scaleBlend = parseFloat(newValue);
2128
+ break;
2129
+ case 'blend':
2130
+ this.blend = this.hasAttribute('blend');
2131
+ break;
2132
+ case 'screen-space':
2133
+ this.screenSpace = this.hasAttribute('screen-space');
2134
+ break;
2135
+ }
2136
+ }
1678
2137
  }
1679
- customElements.define('pc-scripts', ScriptComponentElement);
2138
+ customElements.define('pc-screen', ScreenComponentElement);
1680
2139
 
1681
2140
  /**
1682
- * Represents a script in the PlayCanvas engine.
2141
+ * Represents a script component in the PlayCanvas engine.
2142
+ *
2143
+ * @category Components
1683
2144
  */
1684
- class ScriptElement extends HTMLElement {
2145
+ class ScriptComponentElement extends ComponentElement {
1685
2146
  constructor() {
1686
- super(...arguments);
1687
- this._attributes = {};
1688
- this._enabled = true;
1689
- this._name = '';
1690
- this._script = null;
2147
+ super('script');
2148
+ // Create mutation observer to watch for child script elements
2149
+ this.observer = new MutationObserver(this.handleMutations.bind(this));
2150
+ this.observer.observe(this, {
2151
+ childList: true
2152
+ });
2153
+ // Listen for script attribute and enable changes
2154
+ this.addEventListener('scriptattributeschange', this.handleScriptAttributesChange.bind(this));
2155
+ this.addEventListener('scriptenablechange', this.handleScriptEnableChange.bind(this));
1691
2156
  }
1692
2157
  async connectedCallback() {
1693
- var _a, _b, _c;
1694
- const appElement = this.closest('pc-app');
1695
- if (!appElement) {
1696
- console.error(`${this.tagName.toLowerCase()} should be a descendant of pc-app`);
2158
+ await super.connectedCallback();
2159
+ // Handle initial script elements
2160
+ this.querySelectorAll(':scope > pc-script').forEach((scriptElement) => {
2161
+ const scriptName = scriptElement.getAttribute('name');
2162
+ const attributes = scriptElement.getAttribute('attributes');
2163
+ if (scriptName) {
2164
+ this.createScript(scriptName, attributes);
2165
+ }
2166
+ });
2167
+ }
2168
+ applyAttributes(script, attributes) {
2169
+ try {
2170
+ // Parse the attributes string into an object and set them on the script
2171
+ const attributesObject = attributes ? JSON.parse(attributes) : {};
2172
+ Object.assign(script, attributesObject);
2173
+ }
2174
+ catch (error) {
2175
+ console.error(`Error parsing attributes JSON string ${attributes}:`, error);
2176
+ }
2177
+ }
2178
+ handleScriptAttributesChange(event) {
2179
+ const scriptElement = event.target;
2180
+ const scriptName = scriptElement.getAttribute('name');
2181
+ if (!scriptName || !this.component)
1697
2182
  return;
2183
+ const script = this.component.get(scriptName);
2184
+ if (script) {
2185
+ this.applyAttributes(script, event.detail.attributes);
1698
2186
  }
1699
- await appElement.getApplication();
1700
- const scriptAttributes = this.getAttribute('attributes');
1701
- if (scriptAttributes) {
1702
- try {
1703
- this._attributes = JSON.parse(scriptAttributes);
1704
- }
1705
- catch (e) {
1706
- console.error('Failed to parse script attributes:', e);
1707
- }
2187
+ }
2188
+ handleScriptEnableChange(event) {
2189
+ const scriptElement = event.target;
2190
+ const scriptName = scriptElement.getAttribute('name');
2191
+ if (!scriptName || !this.component)
2192
+ return;
2193
+ const script = this.component.get(scriptName);
2194
+ if (script) {
2195
+ script.enabled = event.detail.enabled;
1708
2196
  }
1709
- // When the script is created, initialize it with the necessary attributes
1710
- (_a = this.scriptsElement) === null || _a === void 0 ? void 0 : _a.component.on(`create:${this._name}`, (scriptInstance) => {
1711
- Object.assign(scriptInstance, this._attributes);
1712
- });
1713
- 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;
1714
2197
  }
1715
- disconnectedCallback() {
1716
- var _a;
1717
- (_a = this.scriptsElement) === null || _a === void 0 ? void 0 : _a.component.destroy(this._name);
2198
+ createScript(name, attributes) {
2199
+ if (!this.component)
2200
+ return null;
2201
+ this.component.on(`create:${name}`, (script) => {
2202
+ this.applyAttributes(script, attributes);
2203
+ });
2204
+ return this.component.create(name);
1718
2205
  }
1719
- refreshAttributes() {
1720
- if (this._script) {
1721
- Object.entries(this._attributes).forEach(([name, value]) => {
1722
- if (this._script && name in this._script) {
1723
- this._script[name] = value;
2206
+ destroyScript(name) {
2207
+ if (!this.component)
2208
+ return;
2209
+ this.component.destroy(name);
2210
+ }
2211
+ handleMutations(mutations) {
2212
+ for (const mutation of mutations) {
2213
+ // Handle added nodes
2214
+ mutation.addedNodes.forEach((node) => {
2215
+ if (node instanceof HTMLElement && node.tagName.toLowerCase() === 'pc-script') {
2216
+ const scriptName = node.getAttribute('name');
2217
+ const attributes = node.getAttribute('attributes');
2218
+ if (scriptName) {
2219
+ this.createScript(scriptName, attributes);
2220
+ }
2221
+ }
2222
+ });
2223
+ // Handle removed nodes
2224
+ mutation.removedNodes.forEach((node) => {
2225
+ if (node instanceof HTMLElement && node.tagName.toLowerCase() === 'pc-script') {
2226
+ const scriptName = node.getAttribute('name');
2227
+ if (scriptName) {
2228
+ this.destroyScript(scriptName);
2229
+ }
1724
2230
  }
1725
2231
  });
1726
2232
  }
1727
2233
  }
1728
- get scriptsElement() {
1729
- const scriptsElement = this.parentElement;
1730
- if (!(scriptsElement instanceof ScriptComponentElement)) {
1731
- console.warn('pc-script must be a direct child of a pc-scripts element');
1732
- return null;
1733
- }
1734
- return scriptsElement;
2234
+ disconnectedCallback() {
2235
+ var _a;
2236
+ this.observer.disconnect();
2237
+ (_a = super.disconnectedCallback) === null || _a === void 0 ? void 0 : _a.call(this);
1735
2238
  }
2239
+ /**
2240
+ * Gets the script component.
2241
+ * @returns The script component.
2242
+ */
2243
+ get component() {
2244
+ return super.component;
2245
+ }
2246
+ }
2247
+ customElements.define('pc-scripts', ScriptComponentElement);
2248
+
2249
+ /**
2250
+ * Represents a script in the PlayCanvas engine.
2251
+ */
2252
+ class ScriptElement extends HTMLElement {
2253
+ constructor() {
2254
+ super(...arguments);
2255
+ this._attributes = '{}';
2256
+ this._enabled = true;
2257
+ this._name = '';
2258
+ }
2259
+ /**
2260
+ * Sets the attributes of the script.
2261
+ * @param value - The attributes of the script.
2262
+ */
1736
2263
  set scriptAttributes(value) {
1737
- this._attributes = JSON.parse(value);
1738
- this.refreshAttributes();
2264
+ this._attributes = value;
2265
+ this.dispatchEvent(new CustomEvent('scriptattributeschange', {
2266
+ detail: { attributes: value },
2267
+ bubbles: true
2268
+ }));
1739
2269
  }
2270
+ /**
2271
+ * Gets the attributes of the script.
2272
+ * @returns The attributes of the script.
2273
+ */
1740
2274
  get scriptAttributes() {
1741
- return JSON.stringify(this._attributes);
2275
+ return this._attributes;
1742
2276
  }
2277
+ /**
2278
+ * Sets the enabled state of the script.
2279
+ * @param value - The enabled state of the script.
2280
+ */
1743
2281
  set enabled(value) {
1744
2282
  this._enabled = value;
1745
- if (this._script) {
1746
- this._script.enabled = value;
1747
- }
2283
+ this.dispatchEvent(new CustomEvent('scriptenablechange', {
2284
+ detail: { enabled: value },
2285
+ bubbles: true
2286
+ }));
1748
2287
  }
1749
2288
  /**
1750
2289
  * Gets the enabled state of the script.
@@ -1881,7 +2420,7 @@
1881
2420
  }
1882
2421
  }
1883
2422
  }
1884
- customElements.define('pc-sound', SoundComponentElement);
2423
+ customElements.define('pc-sounds', SoundComponentElement);
1885
2424
 
1886
2425
  /**
1887
2426
  * Represents a sound slot in the PlayCanvas engine.
@@ -1903,10 +2442,6 @@
1903
2442
  */
1904
2443
  this.soundSlot = null;
1905
2444
  }
1906
- getAsset() {
1907
- const assetElement = document.querySelector(`pc-asset[id="${this._asset}"]`);
1908
- return assetElement.asset;
1909
- }
1910
2445
  async connectedCallback() {
1911
2446
  const appElement = this.closest('pc-app');
1912
2447
  if (!appElement) {
@@ -1948,7 +2483,7 @@
1948
2483
  var _a;
1949
2484
  this._asset = value;
1950
2485
  if (this.soundSlot) {
1951
- const id = (_a = this.getAsset()) === null || _a === void 0 ? void 0 : _a.id;
2486
+ const id = (_a = AssetElement.get(value)) === null || _a === void 0 ? void 0 : _a.id;
1952
2487
  if (id) {
1953
2488
  this.soundSlot.asset = id;
1954
2489
  }
@@ -2129,7 +2664,7 @@
2129
2664
  }
2130
2665
  }
2131
2666
  }
2132
- customElements.define('pc-sound-slot', SoundSlotElement);
2667
+ customElements.define('pc-sound', SoundSlotElement);
2133
2668
 
2134
2669
  /**
2135
2670
  * Represents a model in the PlayCanvas engine.
@@ -2152,12 +2687,8 @@
2152
2687
  this.asset = asset;
2153
2688
  }
2154
2689
  }
2155
- getAsset() {
2156
- const assetElement = document.querySelector(`pc-asset[id="${this._asset}"]`);
2157
- return assetElement.asset;
2158
- }
2159
2690
  _loadModel() {
2160
- const asset = this.getAsset();
2691
+ const asset = AssetElement.get(this._asset);
2161
2692
  if (!asset) {
2162
2693
  return;
2163
2694
  }
@@ -2385,10 +2916,6 @@
2385
2916
  this.asset = this.getAttribute('asset') || '';
2386
2917
  this.solidColor = this.hasAttribute('solid-color');
2387
2918
  }
2388
- getAsset() {
2389
- const assetElement = document.querySelector(`pc-asset[id="${this._asset}"]`);
2390
- return assetElement.asset;
2391
- }
2392
2919
  getScene() {
2393
2920
  const appElement = this.closest('pc-app');
2394
2921
  if (!appElement) {
@@ -2404,7 +2931,7 @@
2404
2931
  this._asset = value;
2405
2932
  const scene = this.getScene();
2406
2933
  if (scene) {
2407
- const asset = this.getAsset();
2934
+ const asset = AssetElement.get(value);
2408
2935
  if (asset) {
2409
2936
  if (asset.resource) {
2410
2937
  scene.envAtlas = asset.resource;
@@ -2498,11 +3025,13 @@
2498
3025
  exports.GSplatComponentElement = GSplatComponentElement;
2499
3026
  exports.LightComponentElement = LightComponentElement;
2500
3027
  exports.ListenerComponentElement = ListenerComponentElement;
3028
+ exports.MaterialElement = MaterialElement;
2501
3029
  exports.ModelElement = ModelElement;
2502
3030
  exports.ModuleElement = ModuleElement;
2503
3031
  exports.RenderComponentElement = RenderComponentElement;
2504
3032
  exports.RigidBodyComponentElement = RigidBodyComponentElement;
2505
3033
  exports.SceneElement = SceneElement;
3034
+ exports.ScreenComponentElement = ScreenComponentElement;
2506
3035
  exports.ScriptComponentElement = ScriptComponentElement;
2507
3036
  exports.ScriptElement = ScriptElement;
2508
3037
  exports.SkyElement = SkyElement;