@playcanvas/web-components 0.1.3 → 0.1.5

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 (43) hide show
  1. package/README.md +12 -24
  2. package/dist/app.d.ts +11 -4
  3. package/dist/asset.d.ts +0 -1
  4. package/dist/async-element.d.ts +23 -0
  5. package/dist/components/camera-component.d.ts +3 -0
  6. package/dist/components/collision-component.d.ts +9 -1
  7. package/dist/components/component.d.ts +4 -2
  8. package/dist/components/element-component.d.ts +1 -1
  9. package/dist/components/render-component.d.ts +2 -2
  10. package/dist/components/script-component.d.ts +1 -1
  11. package/dist/components/sound-slot.d.ts +2 -1
  12. package/dist/entity.d.ts +2 -1
  13. package/dist/index.d.ts +10 -1
  14. package/dist/model.d.ts +2 -1
  15. package/dist/pwc.cjs +259 -119
  16. package/dist/pwc.cjs.map +1 -1
  17. package/dist/pwc.js +259 -119
  18. package/dist/pwc.js.map +1 -1
  19. package/dist/pwc.min.js +1 -1
  20. package/dist/pwc.min.js.map +1 -1
  21. package/dist/pwc.mjs +260 -121
  22. package/dist/pwc.mjs.map +1 -1
  23. package/dist/scene.d.ts +2 -1
  24. package/dist/sky.d.ts +15 -6
  25. package/dist/utils.d.ts +8 -1
  26. package/package.json +13 -12
  27. package/src/app.ts +13 -16
  28. package/src/asset.ts +1 -3
  29. package/src/async-element.ts +45 -0
  30. package/src/components/camera-component.ts +23 -2
  31. package/src/components/collision-component.ts +37 -3
  32. package/src/components/component.ts +15 -26
  33. package/src/components/element-component.ts +15 -4
  34. package/src/components/render-component.ts +1 -6
  35. package/src/components/screen-component.ts +1 -1
  36. package/src/components/script-component.ts +1 -3
  37. package/src/components/sound-slot.ts +7 -10
  38. package/src/entity.ts +25 -15
  39. package/src/index.ts +11 -0
  40. package/src/model.ts +20 -15
  41. package/src/scene.ts +6 -11
  42. package/src/sky.ts +82 -40
  43. package/src/utils.ts +14 -1
package/src/sky.ts CHANGED
@@ -1,61 +1,72 @@
1
- import { LAYERID_SKYBOX, Quat } from 'playcanvas';
1
+ import { EnvLighting, LAYERID_SKYBOX, Quat, Texture, Vec3 } from 'playcanvas';
2
2
 
3
- import { AppElement } from './app';
4
3
  import { AssetElement } from './asset';
4
+ import { AsyncElement } from './async-element';
5
+ import { parseVec3 } from './utils';
5
6
 
6
7
  /**
7
8
  * Represents a sky in the PlayCanvas engine.
8
9
  */
9
- class SkyElement extends HTMLElement {
10
+ class SkyElement extends AsyncElement {
10
11
  private _asset = '';
11
12
 
13
+ private _center = new Vec3(0, 0.01, 0);
14
+
12
15
  private _intensity = 1;
13
16
 
14
- private _rotation = [0, 0, 0];
17
+ private _rotation = new Vec3();
15
18
 
16
19
  private _level = 0;
17
20
 
18
- private _solidColor = false;
21
+ private _scale = new Vec3(100, 100, 100);
19
22
 
20
- async connectedCallback() {
21
- // Get the application
22
- const appElement = this.closest('pc-app') as AppElement;
23
- if (!appElement) {
24
- console.warn(`${this.tagName} must be a child of pc-app`);
25
- return;
26
- }
23
+ private _type: 'box' | 'dome' | 'infinite' | 'none' = 'infinite';
27
24
 
28
- await appElement.getApplication();
25
+ async connectedCallback() {
26
+ await this.closestApp?.ready();
29
27
 
30
28
  this.asset = this.getAttribute('asset') || '';
31
- this.solidColor = this.hasAttribute('solid-color');
29
+
30
+ this._onReady();
32
31
  }
33
32
 
34
33
  getScene() {
35
- const appElement = this.closest('pc-app') as AppElement | null;
36
- if (!appElement) {
37
- return;
38
- }
39
- const app = appElement.app;
34
+ const app = this.closestApp!.app;
40
35
  if (!app) {
41
36
  return;
42
37
  }
43
38
  return app.scene;
44
39
  }
45
40
 
41
+ private initSkybox(source: Texture) {
42
+ source.anisotropy = 4;
43
+
44
+ const skybox = EnvLighting.generateSkyboxCubemap(source);
45
+ const lighting = EnvLighting.generateLightingSource(source);
46
+ const envAtlas = EnvLighting.generateAtlas(lighting);
47
+ const app = this.closestApp!.app;
48
+ if (app) {
49
+ app.scene.envAtlas = envAtlas;
50
+ app.scene.skybox = skybox;
51
+
52
+ const layer = app.scene.layers.getLayerById(LAYERID_SKYBOX);
53
+ if (layer) {
54
+ layer.enabled = this._type !== 'none';
55
+ }
56
+
57
+ app.scene.sky.type = this._type;
58
+ app.scene.sky.node.setLocalScale(this._scale);
59
+ app.scene.sky.center = this._center;
60
+ }
61
+ }
62
+
46
63
  set asset(value: string) {
47
64
  this._asset = value;
48
65
  const scene = this.getScene();
49
66
  if (scene) {
50
67
  const asset = AssetElement.get(value);
51
68
  if (asset) {
52
- if (asset.resource) {
53
- scene.envAtlas = asset.resource;
54
- } else {
55
- asset.once('load', () => {
56
- scene.envAtlas = asset.resource;
57
- });
58
- }
69
+ this.initSkybox(asset.resource);
59
70
  }
60
71
  }
61
72
  }
@@ -64,6 +75,18 @@ class SkyElement extends HTMLElement {
64
75
  return this._asset;
65
76
  }
66
77
 
78
+ set center(value: Vec3) {
79
+ this._center = value;
80
+ const scene = this.getScene();
81
+ if (scene) {
82
+ scene.sky.center = this._center;
83
+ }
84
+ }
85
+
86
+ get center() {
87
+ return this._center;
88
+ }
89
+
67
90
  set intensity(value: number) {
68
91
  this._intensity = value;
69
92
  const scene = this.getScene();
@@ -76,11 +99,11 @@ class SkyElement extends HTMLElement {
76
99
  return this._intensity;
77
100
  }
78
101
 
79
- set rotation(value: number[]) {
102
+ set rotation(value: Vec3) {
80
103
  this._rotation = value;
81
104
  const scene = this.getScene();
82
105
  if (scene) {
83
- scene.skyboxRotation = new Quat().setFromEulerAngles(this._rotation[0], this._rotation[1], this._rotation[2]);
106
+ scene.skyboxRotation = new Quat().setFromEulerAngles(value);
84
107
  }
85
108
  }
86
109
 
@@ -88,19 +111,16 @@ class SkyElement extends HTMLElement {
88
111
  return this._rotation;
89
112
  }
90
113
 
91
- set solidColor(value: boolean) {
92
- this._solidColor = value;
114
+ set scale(value: Vec3) {
115
+ this._scale = value;
93
116
  const scene = this.getScene();
94
117
  if (scene) {
95
- const layer = scene.layers.getLayerById(LAYERID_SKYBOX);
96
- if (layer) {
97
- layer.enabled = !this._solidColor;
98
- }
118
+ scene.sky.node.setLocalScale(this._scale);
99
119
  }
100
120
  }
101
121
 
102
- get solidColor() {
103
- return this._solidColor;
122
+ get scale() {
123
+ return this._scale;
104
124
  }
105
125
 
106
126
  set level(value: number) {
@@ -115,8 +135,24 @@ class SkyElement extends HTMLElement {
115
135
  return this._level;
116
136
  }
117
137
 
138
+ set type(value: 'box' | 'dome' | 'infinite' | 'none') {
139
+ this._type = value;
140
+ const scene = this.getScene();
141
+ if (scene) {
142
+ scene.sky.type = this._type;
143
+ const layer = scene.layers.getLayerById(LAYERID_SKYBOX);
144
+ if (layer) {
145
+ layer.enabled = this._type !== 'none';
146
+ }
147
+ }
148
+ }
149
+
150
+ get type() {
151
+ return this._type;
152
+ }
153
+
118
154
  static get observedAttributes() {
119
- return ['asset', 'intensity', 'level', 'rotation', 'solid-color'];
155
+ return ['asset', 'center', 'intensity', 'level', 'rotation', 'scale', 'type'];
120
156
  }
121
157
 
122
158
  attributeChangedCallback(name: string, _oldValue: string, newValue: string) {
@@ -124,17 +160,23 @@ class SkyElement extends HTMLElement {
124
160
  case 'asset':
125
161
  this.asset = newValue;
126
162
  break;
163
+ case 'center':
164
+ this.center = parseVec3(newValue);
165
+ break;
127
166
  case 'intensity':
128
167
  this.intensity = parseFloat(newValue);
129
168
  break;
130
169
  case 'rotation':
131
- this.rotation = newValue.split(',').map(Number);
170
+ this.rotation = parseVec3(newValue);
132
171
  break;
133
172
  case 'level':
134
173
  this.level = parseInt(newValue, 10);
135
174
  break;
136
- case 'solid-color':
137
- this.solidColor = this.hasAttribute('solid-color');
175
+ case 'scale':
176
+ this.scale = parseVec3(newValue);
177
+ break;
178
+ case 'type':
179
+ this.type = newValue as 'box' | 'dome' | 'infinite' | 'none';
138
180
  break;
139
181
  }
140
182
  }
package/src/utils.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Color, Vec2, Vec3, Vec4 } from 'playcanvas';
1
+ import { Color, Quat, Vec2, Vec3, Vec4 } from 'playcanvas';
2
2
 
3
3
  /**
4
4
  * Parse a color string into a Color object. String can be in the format of '#rgb', '#rgba',
@@ -16,6 +16,19 @@ export const parseColor = (value: string): Color => {
16
16
  return new Color(components);
17
17
  };
18
18
 
19
+ /**
20
+ * Parse an Euler angles string into a Quat object. String can be in the format of 'x,y,z'.
21
+ *
22
+ * @param value - The Euler angles string to parse.
23
+ * @returns The parsed Quat object.
24
+ */
25
+ export const parseQuat = (value: string): Quat => {
26
+ const [x, y, z] = value.split(',').map(Number);
27
+ const q = new Quat();
28
+ q.setFromEulerAngles(x, y, z);
29
+ return q;
30
+ };
31
+
19
32
  /**
20
33
  * Parse a Vec2 string into a Vec2 object. String can be in the format of 'x,y'.
21
34
  *