@playcanvas/web-components 0.1.0 → 0.1.1
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.
- package/README.md +3 -3
- package/dist/pwc.cjs +1 -1
- package/dist/pwc.cjs.map +1 -1
- package/dist/pwc.js +1 -1
- package/dist/pwc.js.map +1 -1
- package/dist/pwc.min.js +1 -1
- package/dist/pwc.min.js.map +1 -1
- package/dist/pwc.mjs +1 -1
- package/dist/pwc.mjs.map +1 -1
- package/package.json +3 -2
- package/src/app.ts +138 -0
- package/src/asset.ts +99 -0
- package/src/components/camera-component.ts +196 -0
- package/src/components/collision-component.ts +148 -0
- package/src/components/component.ts +106 -0
- package/src/components/element-component.ts +252 -0
- package/src/components/gsplat-component.ts +66 -0
- package/src/components/light-component.ts +313 -0
- package/src/components/listener-component.ts +26 -0
- package/src/components/render-component.ts +117 -0
- package/src/components/rigidbody-component.ts +226 -0
- package/src/components/script-component.ts +26 -0
- package/src/components/script.ts +130 -0
- package/src/components/sound-component.ts +117 -0
- package/src/components/sound-slot.ts +289 -0
- package/src/entity.ts +230 -0
- package/src/index.ts +46 -0
- package/src/model.ts +80 -0
- package/src/module.ts +35 -0
- package/src/scene.ts +188 -0
- package/src/sky.ts +150 -0
- package/src/utils.ts +50 -0
package/src/scene.ts
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { Color, Scene } from 'playcanvas';
|
|
2
|
+
|
|
3
|
+
import { AppElement } from './app';
|
|
4
|
+
import { parseColor } from './utils';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Represents a scene in the PlayCanvas engine.
|
|
8
|
+
*/
|
|
9
|
+
class SceneElement extends HTMLElement {
|
|
10
|
+
/**
|
|
11
|
+
* The fog type of the scene.
|
|
12
|
+
*/
|
|
13
|
+
private _fog = 'none'; // possible values: 'none', 'linear', 'exp', 'exp2'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The color of the fog.
|
|
17
|
+
*/
|
|
18
|
+
private _fogColor = new Color(1, 1, 1);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The density of the fog.
|
|
22
|
+
*/
|
|
23
|
+
private _fogDensity = 0;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The start distance of the fog.
|
|
27
|
+
*/
|
|
28
|
+
private _fogStart = 0;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The end distance of the fog.
|
|
32
|
+
*/
|
|
33
|
+
private _fogEnd = 1000;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The PlayCanvas scene instance.
|
|
37
|
+
*/
|
|
38
|
+
scene: Scene | null = null;
|
|
39
|
+
|
|
40
|
+
async connectedCallback() {
|
|
41
|
+
// Get the application
|
|
42
|
+
const appElement = this.closest('pc-app') as AppElement;
|
|
43
|
+
if (!appElement) {
|
|
44
|
+
console.warn(`${this.tagName} must be a child of pc-app`);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const app = await appElement.getApplication();
|
|
49
|
+
|
|
50
|
+
this.scene = app.scene;
|
|
51
|
+
this.updateSceneSettings();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
updateSceneSettings() {
|
|
55
|
+
if (this.scene) {
|
|
56
|
+
this.scene.rendering.fog = this._fog;
|
|
57
|
+
this.scene.rendering.fogColor = this._fogColor;
|
|
58
|
+
this.scene.rendering.fogDensity = this._fogDensity;
|
|
59
|
+
this.scene.rendering.fogStart = this._fogStart;
|
|
60
|
+
this.scene.rendering.fogEnd = this._fogEnd;
|
|
61
|
+
// ... set other properties on the scene as well
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Sets the fog type of the scene.
|
|
67
|
+
* @param value - The fog type.
|
|
68
|
+
*/
|
|
69
|
+
set fog(value) {
|
|
70
|
+
this._fog = value;
|
|
71
|
+
if (this.scene) {
|
|
72
|
+
this.scene.rendering.fog = value;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Gets the fog type of the scene.
|
|
78
|
+
* @returns The fog type.
|
|
79
|
+
*/
|
|
80
|
+
get fog() {
|
|
81
|
+
return this._fog;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Sets the fog color of the scene.
|
|
86
|
+
* @param value - The fog color.
|
|
87
|
+
*/
|
|
88
|
+
set fogColor(value: Color) {
|
|
89
|
+
this._fogColor = value;
|
|
90
|
+
if (this.scene) {
|
|
91
|
+
this.scene.rendering.fogColor = value;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Gets the fog color of the scene.
|
|
97
|
+
* @returns The fog color.
|
|
98
|
+
*/
|
|
99
|
+
get fogColor() {
|
|
100
|
+
return this._fogColor;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Sets the fog density of the scene.
|
|
105
|
+
* @param value - The fog density.
|
|
106
|
+
*/
|
|
107
|
+
set fogDensity(value: number) {
|
|
108
|
+
this._fogDensity = value;
|
|
109
|
+
if (this.scene) {
|
|
110
|
+
this.scene.rendering.fogDensity = value;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Gets the fog density of the scene.
|
|
116
|
+
* @returns The fog density.
|
|
117
|
+
*/
|
|
118
|
+
get fogDensity() {
|
|
119
|
+
return this._fogDensity;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Sets the fog start distance of the scene.
|
|
124
|
+
* @param value - The fog start distance.
|
|
125
|
+
*/
|
|
126
|
+
set fogStart(value: number) {
|
|
127
|
+
this._fogStart = value;
|
|
128
|
+
if (this.scene) {
|
|
129
|
+
this.scene.rendering.fogStart = value;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Gets the fog start distance of the scene.
|
|
135
|
+
* @returns The fog start distance.
|
|
136
|
+
*/
|
|
137
|
+
get fogStart() {
|
|
138
|
+
return this._fogStart;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Sets the fog end distance of the scene.
|
|
143
|
+
* @param value - The fog end distance.
|
|
144
|
+
*/
|
|
145
|
+
set fogEnd(value: number) {
|
|
146
|
+
this._fogEnd = value;
|
|
147
|
+
if (this.scene) {
|
|
148
|
+
this.scene.rendering.fogEnd = value;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Gets the fog end distance of the scene.
|
|
154
|
+
* @returns The fog end distance.
|
|
155
|
+
*/
|
|
156
|
+
get fogEnd() {
|
|
157
|
+
return this._fogEnd;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
static get observedAttributes() {
|
|
161
|
+
return ['fog', 'fog-color', 'fog-density', 'fog-start', 'fog-end'];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
attributeChangedCallback(name: string, _oldValue: string, newValue: string) {
|
|
165
|
+
switch (name) {
|
|
166
|
+
case 'fog':
|
|
167
|
+
this.fog = newValue;
|
|
168
|
+
break;
|
|
169
|
+
case 'fog-color':
|
|
170
|
+
this.fogColor = parseColor(newValue);
|
|
171
|
+
break;
|
|
172
|
+
case 'fog-density':
|
|
173
|
+
this.fogDensity = parseFloat(newValue);
|
|
174
|
+
break;
|
|
175
|
+
case 'fog-start':
|
|
176
|
+
this.fogStart = parseFloat(newValue);
|
|
177
|
+
break;
|
|
178
|
+
case 'fog-end':
|
|
179
|
+
this.fogEnd = parseFloat(newValue);
|
|
180
|
+
break;
|
|
181
|
+
// ... handle other attributes as well
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
customElements.define('pc-scene', SceneElement);
|
|
187
|
+
|
|
188
|
+
export { SceneElement };
|
package/src/sky.ts
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { LAYERID_SKYBOX, Quat } from 'playcanvas';
|
|
2
|
+
|
|
3
|
+
import { AppElement } from './app';
|
|
4
|
+
import { AssetElement } from './asset';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Represents a sky in the PlayCanvas engine.
|
|
8
|
+
*/
|
|
9
|
+
class SkyElement extends HTMLElement {
|
|
10
|
+
private _asset = '';
|
|
11
|
+
|
|
12
|
+
private _intensity = 1;
|
|
13
|
+
|
|
14
|
+
private _rotation = [0, 0, 0];
|
|
15
|
+
|
|
16
|
+
private _level = 0;
|
|
17
|
+
|
|
18
|
+
private _solidColor = false;
|
|
19
|
+
|
|
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
|
+
}
|
|
27
|
+
|
|
28
|
+
await appElement.getApplication();
|
|
29
|
+
|
|
30
|
+
this.asset = this.getAttribute('asset') || '';
|
|
31
|
+
this.solidColor = this.hasAttribute('solid-color');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
getAsset() {
|
|
35
|
+
const assetElement = document.querySelector(`pc-asset[id="${this._asset}"]`) as AssetElement;
|
|
36
|
+
return assetElement!.asset;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
getScene() {
|
|
40
|
+
const appElement = this.closest('pc-app') as AppElement | null;
|
|
41
|
+
if (!appElement) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const app = appElement.app;
|
|
45
|
+
if (!app) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
return app.scene;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
set asset(value: string) {
|
|
52
|
+
this._asset = value;
|
|
53
|
+
const scene = this.getScene();
|
|
54
|
+
if (scene) {
|
|
55
|
+
const asset = this.getAsset();
|
|
56
|
+
if (asset) {
|
|
57
|
+
if (asset.resource) {
|
|
58
|
+
scene.envAtlas = asset.resource;
|
|
59
|
+
} else {
|
|
60
|
+
asset.once('load', () => {
|
|
61
|
+
scene.envAtlas = asset.resource;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get asset() {
|
|
69
|
+
return this._asset;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
set intensity(value: number) {
|
|
73
|
+
this._intensity = value;
|
|
74
|
+
const scene = this.getScene();
|
|
75
|
+
if (scene) {
|
|
76
|
+
scene.skyboxIntensity = this._intensity;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
get intensity() {
|
|
81
|
+
return this._intensity;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
set rotation(value: number[]) {
|
|
85
|
+
this._rotation = value;
|
|
86
|
+
const scene = this.getScene();
|
|
87
|
+
if (scene) {
|
|
88
|
+
scene.skyboxRotation = new Quat().setFromEulerAngles(this._rotation[0], this._rotation[1], this._rotation[2]);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
get rotation() {
|
|
93
|
+
return this._rotation;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
set solidColor(value: boolean) {
|
|
97
|
+
this._solidColor = value;
|
|
98
|
+
const scene = this.getScene();
|
|
99
|
+
if (scene) {
|
|
100
|
+
const layer = scene.layers.getLayerById(LAYERID_SKYBOX);
|
|
101
|
+
if (layer) {
|
|
102
|
+
layer.enabled = !this._solidColor;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
get solidColor() {
|
|
108
|
+
return this._solidColor;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
set level(value: number) {
|
|
112
|
+
this._level = value;
|
|
113
|
+
const scene = this.getScene();
|
|
114
|
+
if (scene) {
|
|
115
|
+
scene.skyboxMip = this._level;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
get level() {
|
|
120
|
+
return this._level;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
static get observedAttributes() {
|
|
124
|
+
return ['asset', 'intensity', 'level', 'rotation', 'solid-color'];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
attributeChangedCallback(name: string, _oldValue: string, newValue: string) {
|
|
128
|
+
switch (name) {
|
|
129
|
+
case 'asset':
|
|
130
|
+
this.asset = newValue;
|
|
131
|
+
break;
|
|
132
|
+
case 'intensity':
|
|
133
|
+
this.intensity = parseFloat(newValue);
|
|
134
|
+
break;
|
|
135
|
+
case 'rotation':
|
|
136
|
+
this.rotation = newValue.split(',').map(Number);
|
|
137
|
+
break;
|
|
138
|
+
case 'level':
|
|
139
|
+
this.level = parseInt(newValue, 10);
|
|
140
|
+
break;
|
|
141
|
+
case 'solid-color':
|
|
142
|
+
this.solidColor = this.hasAttribute('solid-color');
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
customElements.define('pc-sky', SkyElement);
|
|
149
|
+
|
|
150
|
+
export { SkyElement };
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Color, Vec2, Vec3, Vec4 } from 'playcanvas';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Parse a color string into a Color object. String can be in the format of '#rgb', '#rgba',
|
|
5
|
+
* '#rrggbb', '#rrggbbaa', or a string of 3 or 4 comma-delimited numbers.
|
|
6
|
+
*
|
|
7
|
+
* @param value - The color string to parse.
|
|
8
|
+
* @returns The parsed Color object.
|
|
9
|
+
*/
|
|
10
|
+
export const parseColor = (value: string): Color => {
|
|
11
|
+
if (value.startsWith('#')) {
|
|
12
|
+
return new Color().fromString(value);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const components = value.split(',').map(Number);
|
|
16
|
+
return new Color(components);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Parse a Vec2 string into a Vec2 object. String can be in the format of 'x,y'.
|
|
21
|
+
*
|
|
22
|
+
* @param value - The Vec2 string to parse.
|
|
23
|
+
* @returns The parsed Vec2 object.
|
|
24
|
+
*/
|
|
25
|
+
export const parseVec2 = (value: string): Vec2 => {
|
|
26
|
+
const components = value.split(',').map(Number);
|
|
27
|
+
return new Vec2(components);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Parse a Vec3 string into a Vec3 object. String can be in the format of 'x,y,z'.
|
|
32
|
+
*
|
|
33
|
+
* @param value - The Vec3 string to parse.
|
|
34
|
+
* @returns The parsed Vec3 object.
|
|
35
|
+
*/
|
|
36
|
+
export const parseVec3 = (value: string): Vec3 => {
|
|
37
|
+
const components = value.split(',').map(Number);
|
|
38
|
+
return new Vec3(components);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Parse a Vec4 string into a Vec4 object. String can be in the format of 'x,y,z,w'.
|
|
43
|
+
*
|
|
44
|
+
* @param value - The Vec4 string to parse.
|
|
45
|
+
* @returns The parsed Vec4 object.
|
|
46
|
+
*/
|
|
47
|
+
export const parseVec4 = (value: string): Vec4 => {
|
|
48
|
+
const components = value.split(',').map(Number);
|
|
49
|
+
return new Vec4(components);
|
|
50
|
+
};
|