@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
@@ -1,6 +1,24 @@
1
- import { ScriptComponent } from 'playcanvas';
1
+ import { ScriptComponent, ScriptType } from 'playcanvas';
2
2
 
3
3
  import { ComponentElement } from './component';
4
+ import { ScriptElement } from './script';
5
+
6
+ // Add these interfaces at the top of the file, after the imports
7
+ interface ScriptAttributesChangeEvent extends CustomEvent {
8
+ detail: { attributes: any };
9
+ }
10
+
11
+ interface ScriptEnableChangeEvent extends CustomEvent {
12
+ detail: { enabled: boolean };
13
+ }
14
+
15
+ // Add this interface before the ScriptComponentElement class
16
+ declare global {
17
+ interface HTMLElementEventMap {
18
+ 'scriptattributeschange': ScriptAttributesChangeEvent;
19
+ 'scriptenablechange': ScriptEnableChangeEvent;
20
+ }
21
+ }
4
22
 
5
23
  /**
6
24
  * Represents a script component in the PlayCanvas engine.
@@ -8,8 +26,109 @@ import { ComponentElement } from './component';
8
26
  * @category Components
9
27
  */
10
28
  class ScriptComponentElement extends ComponentElement {
29
+ private observer: MutationObserver;
30
+
11
31
  constructor() {
12
32
  super('script');
33
+
34
+ // Create mutation observer to watch for child script elements
35
+ this.observer = new MutationObserver(this.handleMutations.bind(this));
36
+ this.observer.observe(this, {
37
+ childList: true
38
+ });
39
+
40
+ // Listen for script attribute and enable changes
41
+ this.addEventListener('scriptattributeschange', this.handleScriptAttributesChange.bind(this));
42
+ this.addEventListener('scriptenablechange', this.handleScriptEnableChange.bind(this));
43
+ }
44
+
45
+ async connectedCallback() {
46
+ await super.connectedCallback();
47
+
48
+ // Handle initial script elements
49
+ this.querySelectorAll<ScriptElement>(':scope > pc-script').forEach((scriptElement) => {
50
+ const scriptName = scriptElement.getAttribute('name');
51
+ const attributes = scriptElement.getAttribute('attributes');
52
+ if (scriptName) {
53
+ this.createScript(scriptName, attributes);
54
+ }
55
+ });
56
+ }
57
+
58
+ private applyAttributes(script: ScriptType, attributes: string | null) {
59
+ try {
60
+ // Parse the attributes string into an object and set them on the script
61
+ const attributesObject = attributes ? JSON.parse(attributes) : {};
62
+ Object.assign(script, attributesObject);
63
+ } catch (error) {
64
+ console.error(`Error parsing attributes JSON string ${attributes}:`, error);
65
+ }
66
+ }
67
+
68
+ private handleScriptAttributesChange(event: ScriptAttributesChangeEvent) {
69
+ const scriptElement = event.target as ScriptElement;
70
+ const scriptName = scriptElement.getAttribute('name');
71
+ if (!scriptName || !this.component) return;
72
+
73
+ const script = this.component.get(scriptName);
74
+ if (script) {
75
+ this.applyAttributes(script, event.detail.attributes);
76
+ }
77
+ }
78
+
79
+ private handleScriptEnableChange(event: ScriptEnableChangeEvent) {
80
+ const scriptElement = event.target as ScriptElement;
81
+ const scriptName = scriptElement.getAttribute('name');
82
+ if (!scriptName || !this.component) return;
83
+
84
+ const script = this.component.get(scriptName);
85
+ if (script) {
86
+ script.enabled = event.detail.enabled;
87
+ }
88
+ }
89
+
90
+ private createScript(name: string, attributes: string | null): ScriptType | null {
91
+ if (!this.component) return null;
92
+
93
+ this.component.on(`create:${name}`, (script) => {
94
+ this.applyAttributes(script, attributes);
95
+ });
96
+ return this.component.create(name);
97
+ }
98
+
99
+ private destroyScript(name: string): void {
100
+ if (!this.component) return;
101
+ this.component.destroy(name);
102
+ }
103
+
104
+ private handleMutations(mutations: MutationRecord[]) {
105
+ for (const mutation of mutations) {
106
+ // Handle added nodes
107
+ mutation.addedNodes.forEach((node) => {
108
+ if (node instanceof HTMLElement && node.tagName.toLowerCase() === 'pc-script') {
109
+ const scriptName = node.getAttribute('name');
110
+ const attributes = node.getAttribute('attributes');
111
+ if (scriptName) {
112
+ this.createScript(scriptName, attributes);
113
+ }
114
+ }
115
+ });
116
+
117
+ // Handle removed nodes
118
+ mutation.removedNodes.forEach((node) => {
119
+ if (node instanceof HTMLElement && node.tagName.toLowerCase() === 'pc-script') {
120
+ const scriptName = node.getAttribute('name');
121
+ if (scriptName) {
122
+ this.destroyScript(scriptName);
123
+ }
124
+ }
125
+ });
126
+ }
127
+ }
128
+
129
+ disconnectedCallback() {
130
+ this.observer.disconnect();
131
+ super.disconnectedCallback?.();
13
132
  }
14
133
 
15
134
  /**
@@ -1,85 +1,43 @@
1
- import { ScriptType } from 'playcanvas';
2
-
3
- import { AppElement } from '../app';
4
- import { ScriptComponentElement } from './script-component';
5
-
6
1
  /**
7
2
  * Represents a script in the PlayCanvas engine.
8
3
  */
9
4
  class ScriptElement extends HTMLElement {
10
- private _attributes: Record<string, any> = {};
5
+ private _attributes: string = '{}';
11
6
 
12
7
  private _enabled: boolean = true;
13
8
 
14
9
  private _name: string = '';
15
10
 
16
- private _script: ScriptType | null = null;
17
-
18
- async connectedCallback() {
19
- const appElement = this.closest('pc-app') as AppElement | null;
20
- if (!appElement) {
21
- console.error(`${this.tagName.toLowerCase()} should be a descendant of pc-app`);
22
- return;
23
- }
24
-
25
- await appElement.getApplication();
26
-
27
- const scriptAttributes = this.getAttribute('attributes');
28
- if (scriptAttributes) {
29
- try {
30
- this._attributes = JSON.parse(scriptAttributes);
31
- } catch (e) {
32
- console.error('Failed to parse script attributes:', e);
33
- }
34
- }
35
-
36
- // When the script is created, initialize it with the necessary attributes
37
- this.scriptsElement?.component!.on(`create:${this._name}`, (scriptInstance) => {
38
- Object.assign(scriptInstance, this._attributes);
39
- });
40
-
41
- this._script = this.scriptsElement?.component!.create(this._name, { preloading: false }) ?? null;
42
- }
43
-
44
- disconnectedCallback() {
45
- this.scriptsElement?.component!.destroy(this._name);
46
- }
47
-
48
- refreshAttributes() {
49
- if (this._script) {
50
- Object.entries(this._attributes).forEach(([name, value]) => {
51
- if (this._script && name in this._script) {
52
- (this._script as any)[name] = value;
53
- }
54
- });
55
- }
56
- }
57
-
58
- protected get scriptsElement(): ScriptComponentElement | null {
59
- const scriptsElement = this.parentElement as ScriptComponentElement;
60
-
61
- if (!(scriptsElement instanceof ScriptComponentElement)) {
62
- console.warn('pc-script must be a direct child of a pc-scripts element');
63
- return null;
64
- }
65
-
66
- return scriptsElement;
67
- }
68
-
11
+ /**
12
+ * Sets the attributes of the script.
13
+ * @param value - The attributes of the script.
14
+ */
69
15
  set scriptAttributes(value: string) {
70
- this._attributes = JSON.parse(value);
71
- this.refreshAttributes();
16
+ this._attributes = value;
17
+ this.dispatchEvent(new CustomEvent('scriptattributeschange', {
18
+ detail: { attributes: value },
19
+ bubbles: true
20
+ }));
72
21
  }
73
22
 
23
+ /**
24
+ * Gets the attributes of the script.
25
+ * @returns The attributes of the script.
26
+ */
74
27
  get scriptAttributes() {
75
- return JSON.stringify(this._attributes);
28
+ return this._attributes;
76
29
  }
77
30
 
31
+ /**
32
+ * Sets the enabled state of the script.
33
+ * @param value - The enabled state of the script.
34
+ */
78
35
  set enabled(value: boolean) {
79
36
  this._enabled = value;
80
- if (this._script) {
81
- this._script.enabled = value;
82
- }
37
+ this.dispatchEvent(new CustomEvent('scriptenablechange', {
38
+ detail: { enabled: value },
39
+ bubbles: true
40
+ }));
83
41
  }
84
42
 
85
43
  /**
@@ -112,6 +112,6 @@ class SoundComponentElement extends ComponentElement {
112
112
  }
113
113
  }
114
114
 
115
- customElements.define('pc-sound', SoundComponentElement);
115
+ customElements.define('pc-sounds', SoundComponentElement);
116
116
 
117
117
  export { SoundComponentElement };
@@ -31,11 +31,6 @@ class SoundSlotElement extends HTMLElement {
31
31
  */
32
32
  soundSlot: SoundSlot | null = null;
33
33
 
34
- getAsset() {
35
- const assetElement = document.querySelector(`pc-asset[id="${this._asset}"]`) as AssetElement;
36
- return assetElement!.asset;
37
- }
38
-
39
34
  async connectedCallback() {
40
35
  const appElement = this.closest('pc-app') as AppElement | null;
41
36
  if (!appElement) {
@@ -83,7 +78,7 @@ class SoundSlotElement extends HTMLElement {
83
78
  set asset(value: string) {
84
79
  this._asset = value;
85
80
  if (this.soundSlot) {
86
- const id = this.getAsset()?.id;
81
+ const id = AssetElement.get(value)?.id;
87
82
  if (id) {
88
83
  this.soundSlot.asset = id;
89
84
  }
@@ -284,6 +279,6 @@ class SoundSlotElement extends HTMLElement {
284
279
  }
285
280
  }
286
281
 
287
- customElements.define('pc-sound-slot', SoundSlotElement);
282
+ customElements.define('pc-sound', SoundSlotElement);
288
283
 
289
284
  export { SoundSlotElement };
package/src/entity.ts CHANGED
@@ -55,7 +55,7 @@ class EntityElement extends HTMLElement {
55
55
  // Create a new entity
56
56
  this.entity = new Entity(this._name, app);
57
57
 
58
- if (this.parentElement && this.parentElement.tagName === 'pc-entity' && (this.parentElement as EntityElement).entity) {
58
+ if (this.parentElement && this.parentElement.tagName.toLowerCase() === 'pc-entity' && (this.parentElement as EntityElement).entity) {
59
59
  (this.parentElement as EntityElement).entity!.addChild(this.entity);
60
60
  } else {
61
61
  app.root.addChild(this.entity);
package/src/index.ts CHANGED
@@ -14,10 +14,12 @@ import { GSplatComponentElement } from './components/gsplat-component';
14
14
  import { LightComponentElement } from './components/light-component';
15
15
  import { RenderComponentElement } from './components/render-component';
16
16
  import { RigidBodyComponentElement } from './components/rigidbody-component';
17
+ import { ScreenComponentElement } from './components/screen-component';
17
18
  import { ScriptComponentElement } from './components/script-component';
18
19
  import { ScriptElement } from './components/script';
19
20
  import { SoundComponentElement } from './components/sound-component';
20
21
  import { SoundSlotElement } from './components/sound-slot';
22
+ import { MaterialElement } from './material';
21
23
  import { ModelElement } from './model';
22
24
  import { SceneElement } from './scene';
23
25
  import { SkyElement } from './sky';
@@ -36,10 +38,12 @@ export {
36
38
  ListenerComponentElement,
37
39
  RenderComponentElement,
38
40
  RigidBodyComponentElement,
41
+ ScreenComponentElement,
39
42
  ScriptComponentElement,
40
43
  ScriptElement,
41
44
  SoundComponentElement,
42
45
  SoundSlotElement,
46
+ MaterialElement,
43
47
  ModelElement,
44
48
  SceneElement,
45
49
  SkyElement
@@ -0,0 +1,138 @@
1
+ import { Color, StandardMaterial } from 'playcanvas';
2
+
3
+ import { AssetElement } from './asset';
4
+ import { parseColor } from './utils';
5
+
6
+ /**
7
+ * Represents a material in the PlayCanvas engine.
8
+ */
9
+ class MaterialElement extends HTMLElement {
10
+ private _diffuse = new Color(1, 1, 1);
11
+
12
+ private _diffuseMap = '';
13
+
14
+ private _metalnessMap = '';
15
+
16
+ private _normalMap = '';
17
+
18
+ private _roughnessMap = '';
19
+
20
+ material: StandardMaterial | null = null;
21
+
22
+ createMaterial() {
23
+ this.material = new StandardMaterial();
24
+ this.material.glossInvert = true;
25
+ this.material.useMetalness = true;
26
+ this.material.diffuse = this._diffuse;
27
+ this.diffuseMap = this._diffuseMap;
28
+ this.metalnessMap = this._metalnessMap;
29
+ this.normalMap = this._normalMap;
30
+ this.roughnessMap = this._roughnessMap;
31
+ this.material.update();
32
+ }
33
+
34
+ disconnectedCallback() {
35
+ if (this.material) {
36
+ this.material.destroy();
37
+ this.material = null;
38
+ }
39
+ }
40
+
41
+ setMap(map: string, property: 'diffuseMap' | 'metalnessMap' | 'normalMap' | 'glossMap') {
42
+ if (this.material) {
43
+ const asset = AssetElement.get(map);
44
+ if (asset) {
45
+ if (asset.loaded) {
46
+ this.material[property] = asset.resource;
47
+ this.material[property]!.anisotropy = 4;
48
+ } else {
49
+ asset.once('load', () => {
50
+ this.material![property] = asset.resource;
51
+ this.material![property]!.anisotropy = 4;
52
+ this.material!.update();
53
+ });
54
+ }
55
+ }
56
+ }
57
+ }
58
+
59
+ set diffuse(value: Color) {
60
+ this._diffuse = value;
61
+ if (this.material) {
62
+ this.material.diffuse = value;
63
+ }
64
+ }
65
+
66
+ get diffuse(): Color {
67
+ return this._diffuse;
68
+ }
69
+
70
+ set diffuseMap(value: string) {
71
+ this._diffuseMap = value;
72
+ this.setMap(value, 'diffuseMap');
73
+ }
74
+
75
+ get diffuseMap() {
76
+ return this._diffuseMap;
77
+ }
78
+
79
+ set metalnessMap(value: string) {
80
+ this._metalnessMap = value;
81
+ this.setMap(value, 'metalnessMap');
82
+ }
83
+
84
+ get metalnessMap() {
85
+ return this._metalnessMap;
86
+ }
87
+
88
+ set normalMap(value: string) {
89
+ this._normalMap = value;
90
+ this.setMap(value, 'normalMap');
91
+ }
92
+
93
+ get normalMap() {
94
+ return this._normalMap;
95
+ }
96
+
97
+ set roughnessMap(value: string) {
98
+ this._roughnessMap = value;
99
+ this.setMap(value, 'glossMap');
100
+ }
101
+
102
+ get roughnessMap() {
103
+ return this._roughnessMap;
104
+ }
105
+
106
+ static get(id: string) {
107
+ const materialElement = document.querySelector<MaterialElement>(`pc-material[id="${id}"]`);
108
+ return materialElement?.material;
109
+ }
110
+
111
+ static get observedAttributes() {
112
+ return ['diffuse', 'diffuse-map', 'metalness-map', 'normal-map', 'roughness-map'];
113
+ }
114
+
115
+ attributeChangedCallback(name: string, _oldValue: string, newValue: string) {
116
+ switch (name) {
117
+ case 'diffuse':
118
+ this.diffuse = parseColor(newValue);
119
+ break;
120
+ case 'diffuse-map':
121
+ this.diffuseMap = newValue;
122
+ break;
123
+ case 'metalness-map':
124
+ this.metalnessMap = newValue;
125
+ break;
126
+ case 'normal-map':
127
+ this.normalMap = newValue;
128
+ break;
129
+ case 'roughness-map':
130
+ this.roughnessMap = newValue;
131
+ break;
132
+ }
133
+ }
134
+ }
135
+
136
+ customElements.define('pc-material', MaterialElement);
137
+
138
+ export { MaterialElement };
package/src/model.ts CHANGED
@@ -24,13 +24,8 @@ class ModelElement extends HTMLElement {
24
24
  }
25
25
  }
26
26
 
27
- getAsset() {
28
- const assetElement = document.querySelector(`pc-asset[id="${this._asset}"]`) as AssetElement;
29
- return assetElement!.asset;
30
- }
31
-
32
27
  _loadModel() {
33
- const asset = this.getAsset();
28
+ const asset = AssetElement.get(this._asset);
34
29
  if (!asset) {
35
30
  return;
36
31
  }
package/src/sky.ts CHANGED
@@ -31,11 +31,6 @@ class SkyElement extends HTMLElement {
31
31
  this.solidColor = this.hasAttribute('solid-color');
32
32
  }
33
33
 
34
- getAsset() {
35
- const assetElement = document.querySelector(`pc-asset[id="${this._asset}"]`) as AssetElement;
36
- return assetElement!.asset;
37
- }
38
-
39
34
  getScene() {
40
35
  const appElement = this.closest('pc-app') as AppElement | null;
41
36
  if (!appElement) {
@@ -52,7 +47,7 @@ class SkyElement extends HTMLElement {
52
47
  this._asset = value;
53
48
  const scene = this.getScene();
54
49
  if (scene) {
55
- const asset = this.getAsset();
50
+ const asset = AssetElement.get(value);
56
51
  if (asset) {
57
52
  if (asset.resource) {
58
53
  scene.envAtlas = asset.resource;