@playcanvas/web-components 0.1.2 → 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.
@@ -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;