@playcanvas/web-components 0.3.0 → 0.5.0
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/LICENSE +21 -21
- package/README.md +84 -84
- package/dist/components/light-component.d.ts +48 -0
- package/dist/components/splat-component.d.ts +0 -13
- package/dist/pwc.cjs +275 -207
- package/dist/pwc.cjs.map +1 -1
- package/dist/pwc.js +275 -207
- 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 +276 -208
- package/dist/pwc.mjs.map +1 -1
- package/package.json +76 -66
- package/src/app.ts +612 -606
- package/src/asset.ts +159 -159
- package/src/async-element.ts +46 -46
- package/src/colors.ts +150 -150
- package/src/components/camera-component.ts +557 -557
- package/src/components/collision-component.ts +183 -183
- package/src/components/component.ts +97 -97
- package/src/components/element-component.ts +367 -367
- package/src/components/light-component.ts +570 -466
- package/src/components/listener-component.ts +30 -30
- package/src/components/particlesystem-component.ts +155 -155
- package/src/components/render-component.ts +147 -147
- package/src/components/rigidbody-component.ts +227 -227
- package/src/components/screen-component.ts +157 -157
- package/src/components/script-component.ts +270 -270
- package/src/components/script.ts +90 -90
- package/src/components/sound-component.ts +230 -230
- package/src/components/sound-slot.ts +288 -288
- package/src/components/splat-component.ts +102 -133
- package/src/entity.ts +360 -360
- package/src/index.ts +63 -63
- package/src/material.ts +141 -141
- package/src/model.ts +111 -111
- package/src/module.ts +43 -43
- package/src/scene.ts +217 -217
- package/src/sky.ts +293 -293
- package/src/utils.ts +71 -71
package/src/index.ts
CHANGED
|
@@ -1,63 +1,63 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The Engine Web Components module provides a set of Web Components for the PlayCanvas Engine.
|
|
3
|
-
* While these components are normally instantiated in a declarative fashion using HTML, this
|
|
4
|
-
* reference covers the TypeScript/JavaScript API that allows these components to be created
|
|
5
|
-
* programmatically.
|
|
6
|
-
*
|
|
7
|
-
* @module EngineWebComponents
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/* eslint-disable import/order */
|
|
11
|
-
|
|
12
|
-
// Note that order matters here (e.g. pc-entity must be defined before components)
|
|
13
|
-
import { AsyncElement } from './async-element';
|
|
14
|
-
import { ModuleElement } from './module';
|
|
15
|
-
import { AppElement } from './app';
|
|
16
|
-
import { EntityElement } from './entity';
|
|
17
|
-
import { AssetElement } from './asset';
|
|
18
|
-
import { ListenerComponentElement } from './components/listener-component';
|
|
19
|
-
import { CameraComponentElement } from './components/camera-component';
|
|
20
|
-
import { CollisionComponentElement } from './components/collision-component';
|
|
21
|
-
import { ComponentElement } from './components/component';
|
|
22
|
-
import { ElementComponentElement } from './components/element-component';
|
|
23
|
-
import { LightComponentElement } from './components/light-component';
|
|
24
|
-
import { ParticleSystemComponentElement } from './components/particlesystem-component';
|
|
25
|
-
import { RenderComponentElement } from './components/render-component';
|
|
26
|
-
import { RigidBodyComponentElement } from './components/rigidbody-component';
|
|
27
|
-
import { ScreenComponentElement } from './components/screen-component';
|
|
28
|
-
import { ScriptComponentElement } from './components/script-component';
|
|
29
|
-
import { ScriptElement } from './components/script';
|
|
30
|
-
import { SoundComponentElement } from './components/sound-component';
|
|
31
|
-
import { SoundSlotElement } from './components/sound-slot';
|
|
32
|
-
import { SplatComponentElement } from './components/splat-component';
|
|
33
|
-
import { MaterialElement } from './material';
|
|
34
|
-
import { ModelElement } from './model';
|
|
35
|
-
import { SceneElement } from './scene';
|
|
36
|
-
import { SkyElement } from './sky';
|
|
37
|
-
|
|
38
|
-
export {
|
|
39
|
-
AsyncElement,
|
|
40
|
-
ModuleElement,
|
|
41
|
-
AppElement,
|
|
42
|
-
EntityElement,
|
|
43
|
-
AssetElement,
|
|
44
|
-
CameraComponentElement,
|
|
45
|
-
CollisionComponentElement,
|
|
46
|
-
ComponentElement,
|
|
47
|
-
ElementComponentElement,
|
|
48
|
-
ParticleSystemComponentElement,
|
|
49
|
-
LightComponentElement,
|
|
50
|
-
ListenerComponentElement,
|
|
51
|
-
RenderComponentElement,
|
|
52
|
-
RigidBodyComponentElement,
|
|
53
|
-
ScreenComponentElement,
|
|
54
|
-
ScriptComponentElement,
|
|
55
|
-
ScriptElement,
|
|
56
|
-
SoundComponentElement,
|
|
57
|
-
SoundSlotElement,
|
|
58
|
-
SplatComponentElement,
|
|
59
|
-
MaterialElement,
|
|
60
|
-
ModelElement,
|
|
61
|
-
SceneElement,
|
|
62
|
-
SkyElement
|
|
63
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* The Engine Web Components module provides a set of Web Components for the PlayCanvas Engine.
|
|
3
|
+
* While these components are normally instantiated in a declarative fashion using HTML, this
|
|
4
|
+
* reference covers the TypeScript/JavaScript API that allows these components to be created
|
|
5
|
+
* programmatically.
|
|
6
|
+
*
|
|
7
|
+
* @module EngineWebComponents
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/* eslint-disable import/order */
|
|
11
|
+
|
|
12
|
+
// Note that order matters here (e.g. pc-entity must be defined before components)
|
|
13
|
+
import { AsyncElement } from './async-element';
|
|
14
|
+
import { ModuleElement } from './module';
|
|
15
|
+
import { AppElement } from './app';
|
|
16
|
+
import { EntityElement } from './entity';
|
|
17
|
+
import { AssetElement } from './asset';
|
|
18
|
+
import { ListenerComponentElement } from './components/listener-component';
|
|
19
|
+
import { CameraComponentElement } from './components/camera-component';
|
|
20
|
+
import { CollisionComponentElement } from './components/collision-component';
|
|
21
|
+
import { ComponentElement } from './components/component';
|
|
22
|
+
import { ElementComponentElement } from './components/element-component';
|
|
23
|
+
import { LightComponentElement } from './components/light-component';
|
|
24
|
+
import { ParticleSystemComponentElement } from './components/particlesystem-component';
|
|
25
|
+
import { RenderComponentElement } from './components/render-component';
|
|
26
|
+
import { RigidBodyComponentElement } from './components/rigidbody-component';
|
|
27
|
+
import { ScreenComponentElement } from './components/screen-component';
|
|
28
|
+
import { ScriptComponentElement } from './components/script-component';
|
|
29
|
+
import { ScriptElement } from './components/script';
|
|
30
|
+
import { SoundComponentElement } from './components/sound-component';
|
|
31
|
+
import { SoundSlotElement } from './components/sound-slot';
|
|
32
|
+
import { SplatComponentElement } from './components/splat-component';
|
|
33
|
+
import { MaterialElement } from './material';
|
|
34
|
+
import { ModelElement } from './model';
|
|
35
|
+
import { SceneElement } from './scene';
|
|
36
|
+
import { SkyElement } from './sky';
|
|
37
|
+
|
|
38
|
+
export {
|
|
39
|
+
AsyncElement,
|
|
40
|
+
ModuleElement,
|
|
41
|
+
AppElement,
|
|
42
|
+
EntityElement,
|
|
43
|
+
AssetElement,
|
|
44
|
+
CameraComponentElement,
|
|
45
|
+
CollisionComponentElement,
|
|
46
|
+
ComponentElement,
|
|
47
|
+
ElementComponentElement,
|
|
48
|
+
ParticleSystemComponentElement,
|
|
49
|
+
LightComponentElement,
|
|
50
|
+
ListenerComponentElement,
|
|
51
|
+
RenderComponentElement,
|
|
52
|
+
RigidBodyComponentElement,
|
|
53
|
+
ScreenComponentElement,
|
|
54
|
+
ScriptComponentElement,
|
|
55
|
+
ScriptElement,
|
|
56
|
+
SoundComponentElement,
|
|
57
|
+
SoundSlotElement,
|
|
58
|
+
SplatComponentElement,
|
|
59
|
+
MaterialElement,
|
|
60
|
+
ModelElement,
|
|
61
|
+
SceneElement,
|
|
62
|
+
SkyElement
|
|
63
|
+
};
|
package/src/material.ts
CHANGED
|
@@ -1,141 +1,141 @@
|
|
|
1
|
-
import { Color, StandardMaterial, Texture } from 'playcanvas';
|
|
2
|
-
|
|
3
|
-
import { AssetElement } from './asset';
|
|
4
|
-
import { parseColor } from './utils';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* The MaterialElement interface provides properties and methods for manipulating
|
|
8
|
-
* {@link https://developer.playcanvas.com/user-manual/web-components/tags/pc-material/ | `<pc-material>`} elements.
|
|
9
|
-
* The MaterialElement interface also inherits the properties and methods of the
|
|
10
|
-
* {@link HTMLElement} interface.
|
|
11
|
-
*/
|
|
12
|
-
class MaterialElement extends HTMLElement {
|
|
13
|
-
private _diffuse = new Color(1, 1, 1);
|
|
14
|
-
|
|
15
|
-
private _diffuseMap = '';
|
|
16
|
-
|
|
17
|
-
private _metalnessMap = '';
|
|
18
|
-
|
|
19
|
-
private _normalMap = '';
|
|
20
|
-
|
|
21
|
-
private _roughnessMap = '';
|
|
22
|
-
|
|
23
|
-
material: StandardMaterial | null = null;
|
|
24
|
-
|
|
25
|
-
createMaterial() {
|
|
26
|
-
this.material = new StandardMaterial();
|
|
27
|
-
this.material.glossInvert = false;
|
|
28
|
-
this.material.useMetalness = false;
|
|
29
|
-
this.material.diffuse = this._diffuse;
|
|
30
|
-
this.diffuseMap = this._diffuseMap;
|
|
31
|
-
this.metalnessMap = this._metalnessMap;
|
|
32
|
-
this.normalMap = this._normalMap;
|
|
33
|
-
this.roughnessMap = this._roughnessMap;
|
|
34
|
-
this.material.update();
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
disconnectedCallback() {
|
|
38
|
-
if (this.material) {
|
|
39
|
-
this.material.destroy();
|
|
40
|
-
this.material = null;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
setMap(map: string, property: 'diffuseMap' | 'metalnessMap' | 'normalMap' | 'glossMap') {
|
|
45
|
-
if (this.material) {
|
|
46
|
-
const asset = AssetElement.get(map);
|
|
47
|
-
if (asset) {
|
|
48
|
-
if (asset.loaded) {
|
|
49
|
-
this.material[property] = asset.resource as Texture;
|
|
50
|
-
this.material[property]!.anisotropy = 4;
|
|
51
|
-
} else {
|
|
52
|
-
asset.once('load', () => {
|
|
53
|
-
this.material![property] = asset.resource as Texture;
|
|
54
|
-
this.material![property]!.anisotropy = 4;
|
|
55
|
-
this.material!.update();
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
set diffuse(value: Color) {
|
|
63
|
-
this._diffuse = value;
|
|
64
|
-
if (this.material) {
|
|
65
|
-
this.material.diffuse = value;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
get diffuse(): Color {
|
|
70
|
-
return this._diffuse;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
set diffuseMap(value: string) {
|
|
74
|
-
this._diffuseMap = value;
|
|
75
|
-
this.setMap(value, 'diffuseMap');
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
get diffuseMap() {
|
|
79
|
-
return this._diffuseMap;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
set metalnessMap(value: string) {
|
|
83
|
-
this._metalnessMap = value;
|
|
84
|
-
this.setMap(value, 'metalnessMap');
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
get metalnessMap() {
|
|
88
|
-
return this._metalnessMap;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
set normalMap(value: string) {
|
|
92
|
-
this._normalMap = value;
|
|
93
|
-
this.setMap(value, 'normalMap');
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
get normalMap() {
|
|
97
|
-
return this._normalMap;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
set roughnessMap(value: string) {
|
|
101
|
-
this._roughnessMap = value;
|
|
102
|
-
this.setMap(value, 'glossMap');
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
get roughnessMap() {
|
|
106
|
-
return this._roughnessMap;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
static get(id: string) {
|
|
110
|
-
const materialElement = document.querySelector<MaterialElement>(`pc-material[id="${id}"]`);
|
|
111
|
-
return materialElement?.material;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
static get observedAttributes() {
|
|
115
|
-
return ['diffuse', 'diffuse-map', 'metalness-map', 'normal-map', 'roughness-map'];
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
attributeChangedCallback(name: string, _oldValue: string, newValue: string) {
|
|
119
|
-
switch (name) {
|
|
120
|
-
case 'diffuse':
|
|
121
|
-
this.diffuse = parseColor(newValue);
|
|
122
|
-
break;
|
|
123
|
-
case 'diffuse-map':
|
|
124
|
-
this.diffuseMap = newValue;
|
|
125
|
-
break;
|
|
126
|
-
case 'metalness-map':
|
|
127
|
-
this.metalnessMap = newValue;
|
|
128
|
-
break;
|
|
129
|
-
case 'normal-map':
|
|
130
|
-
this.normalMap = newValue;
|
|
131
|
-
break;
|
|
132
|
-
case 'roughness-map':
|
|
133
|
-
this.roughnessMap = newValue;
|
|
134
|
-
break;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
customElements.define('pc-material', MaterialElement);
|
|
140
|
-
|
|
141
|
-
export { MaterialElement };
|
|
1
|
+
import { Color, StandardMaterial, Texture } from 'playcanvas';
|
|
2
|
+
|
|
3
|
+
import { AssetElement } from './asset';
|
|
4
|
+
import { parseColor } from './utils';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The MaterialElement interface provides properties and methods for manipulating
|
|
8
|
+
* {@link https://developer.playcanvas.com/user-manual/web-components/tags/pc-material/ | `<pc-material>`} elements.
|
|
9
|
+
* The MaterialElement interface also inherits the properties and methods of the
|
|
10
|
+
* {@link HTMLElement} interface.
|
|
11
|
+
*/
|
|
12
|
+
class MaterialElement extends HTMLElement {
|
|
13
|
+
private _diffuse = new Color(1, 1, 1);
|
|
14
|
+
|
|
15
|
+
private _diffuseMap = '';
|
|
16
|
+
|
|
17
|
+
private _metalnessMap = '';
|
|
18
|
+
|
|
19
|
+
private _normalMap = '';
|
|
20
|
+
|
|
21
|
+
private _roughnessMap = '';
|
|
22
|
+
|
|
23
|
+
material: StandardMaterial | null = null;
|
|
24
|
+
|
|
25
|
+
createMaterial() {
|
|
26
|
+
this.material = new StandardMaterial();
|
|
27
|
+
this.material.glossInvert = false;
|
|
28
|
+
this.material.useMetalness = false;
|
|
29
|
+
this.material.diffuse = this._diffuse;
|
|
30
|
+
this.diffuseMap = this._diffuseMap;
|
|
31
|
+
this.metalnessMap = this._metalnessMap;
|
|
32
|
+
this.normalMap = this._normalMap;
|
|
33
|
+
this.roughnessMap = this._roughnessMap;
|
|
34
|
+
this.material.update();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
disconnectedCallback() {
|
|
38
|
+
if (this.material) {
|
|
39
|
+
this.material.destroy();
|
|
40
|
+
this.material = null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
setMap(map: string, property: 'diffuseMap' | 'metalnessMap' | 'normalMap' | 'glossMap') {
|
|
45
|
+
if (this.material) {
|
|
46
|
+
const asset = AssetElement.get(map);
|
|
47
|
+
if (asset) {
|
|
48
|
+
if (asset.loaded) {
|
|
49
|
+
this.material[property] = asset.resource as Texture;
|
|
50
|
+
this.material[property]!.anisotropy = 4;
|
|
51
|
+
} else {
|
|
52
|
+
asset.once('load', () => {
|
|
53
|
+
this.material![property] = asset.resource as Texture;
|
|
54
|
+
this.material![property]!.anisotropy = 4;
|
|
55
|
+
this.material!.update();
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
set diffuse(value: Color) {
|
|
63
|
+
this._diffuse = value;
|
|
64
|
+
if (this.material) {
|
|
65
|
+
this.material.diffuse = value;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
get diffuse(): Color {
|
|
70
|
+
return this._diffuse;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
set diffuseMap(value: string) {
|
|
74
|
+
this._diffuseMap = value;
|
|
75
|
+
this.setMap(value, 'diffuseMap');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
get diffuseMap() {
|
|
79
|
+
return this._diffuseMap;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
set metalnessMap(value: string) {
|
|
83
|
+
this._metalnessMap = value;
|
|
84
|
+
this.setMap(value, 'metalnessMap');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
get metalnessMap() {
|
|
88
|
+
return this._metalnessMap;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
set normalMap(value: string) {
|
|
92
|
+
this._normalMap = value;
|
|
93
|
+
this.setMap(value, 'normalMap');
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
get normalMap() {
|
|
97
|
+
return this._normalMap;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
set roughnessMap(value: string) {
|
|
101
|
+
this._roughnessMap = value;
|
|
102
|
+
this.setMap(value, 'glossMap');
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
get roughnessMap() {
|
|
106
|
+
return this._roughnessMap;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
static get(id: string) {
|
|
110
|
+
const materialElement = document.querySelector<MaterialElement>(`pc-material[id="${id}"]`);
|
|
111
|
+
return materialElement?.material;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
static get observedAttributes() {
|
|
115
|
+
return ['diffuse', 'diffuse-map', 'metalness-map', 'normal-map', 'roughness-map'];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
attributeChangedCallback(name: string, _oldValue: string, newValue: string) {
|
|
119
|
+
switch (name) {
|
|
120
|
+
case 'diffuse':
|
|
121
|
+
this.diffuse = parseColor(newValue);
|
|
122
|
+
break;
|
|
123
|
+
case 'diffuse-map':
|
|
124
|
+
this.diffuseMap = newValue;
|
|
125
|
+
break;
|
|
126
|
+
case 'metalness-map':
|
|
127
|
+
this.metalnessMap = newValue;
|
|
128
|
+
break;
|
|
129
|
+
case 'normal-map':
|
|
130
|
+
this.normalMap = newValue;
|
|
131
|
+
break;
|
|
132
|
+
case 'roughness-map':
|
|
133
|
+
this.roughnessMap = newValue;
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
customElements.define('pc-material', MaterialElement);
|
|
140
|
+
|
|
141
|
+
export { MaterialElement };
|
package/src/model.ts
CHANGED
|
@@ -1,111 +1,111 @@
|
|
|
1
|
-
import { ContainerResource, Entity } from 'playcanvas';
|
|
2
|
-
|
|
3
|
-
import { AssetElement } from './asset';
|
|
4
|
-
import { AsyncElement } from './async-element';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* The ModelElement interface provides properties and methods for manipulating
|
|
8
|
-
* {@link https://developer.playcanvas.com/user-manual/web-components/tags/pc-model/ | `<pc-model>`} elements.
|
|
9
|
-
* The ModelElement interface also inherits the properties and methods of the
|
|
10
|
-
* {@link HTMLElement} interface.
|
|
11
|
-
*/
|
|
12
|
-
class ModelElement extends AsyncElement {
|
|
13
|
-
private _asset: string = '';
|
|
14
|
-
|
|
15
|
-
private _entity: Entity | null = null;
|
|
16
|
-
|
|
17
|
-
connectedCallback() {
|
|
18
|
-
this._loadModel();
|
|
19
|
-
this._onReady();
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
disconnectedCallback() {
|
|
23
|
-
this._unloadModel();
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
private _instantiate(container: ContainerResource) {
|
|
27
|
-
this._entity = container.instantiateRenderEntity();
|
|
28
|
-
|
|
29
|
-
// @ts-ignore
|
|
30
|
-
if (container.animations.length > 0) {
|
|
31
|
-
this._entity.addComponent('anim');
|
|
32
|
-
// @ts-ignore
|
|
33
|
-
this._entity.anim.assignAnimation('animation', container.animations[0].resource);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const parentEntityElement = this.closestEntity;
|
|
37
|
-
if (parentEntityElement) {
|
|
38
|
-
parentEntityElement.ready().then(() => {
|
|
39
|
-
parentEntityElement.entity!.addChild(this._entity!);
|
|
40
|
-
});
|
|
41
|
-
} else {
|
|
42
|
-
const appElement = this.closestApp;
|
|
43
|
-
if (appElement) {
|
|
44
|
-
appElement.ready().then(() => {
|
|
45
|
-
appElement.app!.root.addChild(this._entity!);
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
private async _loadModel() {
|
|
52
|
-
this._unloadModel();
|
|
53
|
-
|
|
54
|
-
const appElement = await this.closestApp?.ready();
|
|
55
|
-
const app = appElement?.app;
|
|
56
|
-
|
|
57
|
-
const asset = AssetElement.get(this._asset);
|
|
58
|
-
if (!asset) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (asset.loaded) {
|
|
63
|
-
this._instantiate(asset.resource as ContainerResource);
|
|
64
|
-
} else {
|
|
65
|
-
asset.once('load', () => {
|
|
66
|
-
this._instantiate(asset.resource as ContainerResource);
|
|
67
|
-
});
|
|
68
|
-
app!.assets.load(asset);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
private _unloadModel() {
|
|
73
|
-
this._entity?.destroy();
|
|
74
|
-
this._entity = null;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Sets the id of the `pc-asset` to use for the model.
|
|
79
|
-
* @param value - The asset ID.
|
|
80
|
-
*/
|
|
81
|
-
set asset(value: string) {
|
|
82
|
-
this._asset = value;
|
|
83
|
-
if (this.isConnected) {
|
|
84
|
-
this._loadModel();
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Gets the id of the `pc-asset` to use for the model.
|
|
90
|
-
* @returns The asset ID.
|
|
91
|
-
*/
|
|
92
|
-
get asset(): string {
|
|
93
|
-
return this._asset;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
static get observedAttributes() {
|
|
97
|
-
return ['asset'];
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
attributeChangedCallback(name: string, _oldValue: string, newValue: string) {
|
|
101
|
-
switch (name) {
|
|
102
|
-
case 'asset':
|
|
103
|
-
this.asset = newValue;
|
|
104
|
-
break;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
customElements.define('pc-model', ModelElement);
|
|
110
|
-
|
|
111
|
-
export { ModelElement };
|
|
1
|
+
import { ContainerResource, Entity } from 'playcanvas';
|
|
2
|
+
|
|
3
|
+
import { AssetElement } from './asset';
|
|
4
|
+
import { AsyncElement } from './async-element';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The ModelElement interface provides properties and methods for manipulating
|
|
8
|
+
* {@link https://developer.playcanvas.com/user-manual/web-components/tags/pc-model/ | `<pc-model>`} elements.
|
|
9
|
+
* The ModelElement interface also inherits the properties and methods of the
|
|
10
|
+
* {@link HTMLElement} interface.
|
|
11
|
+
*/
|
|
12
|
+
class ModelElement extends AsyncElement {
|
|
13
|
+
private _asset: string = '';
|
|
14
|
+
|
|
15
|
+
private _entity: Entity | null = null;
|
|
16
|
+
|
|
17
|
+
connectedCallback() {
|
|
18
|
+
this._loadModel();
|
|
19
|
+
this._onReady();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
disconnectedCallback() {
|
|
23
|
+
this._unloadModel();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
private _instantiate(container: ContainerResource) {
|
|
27
|
+
this._entity = container.instantiateRenderEntity();
|
|
28
|
+
|
|
29
|
+
// @ts-ignore
|
|
30
|
+
if (container.animations.length > 0) {
|
|
31
|
+
this._entity.addComponent('anim');
|
|
32
|
+
// @ts-ignore
|
|
33
|
+
this._entity.anim.assignAnimation('animation', container.animations[0].resource);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const parentEntityElement = this.closestEntity;
|
|
37
|
+
if (parentEntityElement) {
|
|
38
|
+
parentEntityElement.ready().then(() => {
|
|
39
|
+
parentEntityElement.entity!.addChild(this._entity!);
|
|
40
|
+
});
|
|
41
|
+
} else {
|
|
42
|
+
const appElement = this.closestApp;
|
|
43
|
+
if (appElement) {
|
|
44
|
+
appElement.ready().then(() => {
|
|
45
|
+
appElement.app!.root.addChild(this._entity!);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private async _loadModel() {
|
|
52
|
+
this._unloadModel();
|
|
53
|
+
|
|
54
|
+
const appElement = await this.closestApp?.ready();
|
|
55
|
+
const app = appElement?.app;
|
|
56
|
+
|
|
57
|
+
const asset = AssetElement.get(this._asset);
|
|
58
|
+
if (!asset) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (asset.loaded) {
|
|
63
|
+
this._instantiate(asset.resource as ContainerResource);
|
|
64
|
+
} else {
|
|
65
|
+
asset.once('load', () => {
|
|
66
|
+
this._instantiate(asset.resource as ContainerResource);
|
|
67
|
+
});
|
|
68
|
+
app!.assets.load(asset);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private _unloadModel() {
|
|
73
|
+
this._entity?.destroy();
|
|
74
|
+
this._entity = null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Sets the id of the `pc-asset` to use for the model.
|
|
79
|
+
* @param value - The asset ID.
|
|
80
|
+
*/
|
|
81
|
+
set asset(value: string) {
|
|
82
|
+
this._asset = value;
|
|
83
|
+
if (this.isConnected) {
|
|
84
|
+
this._loadModel();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Gets the id of the `pc-asset` to use for the model.
|
|
90
|
+
* @returns The asset ID.
|
|
91
|
+
*/
|
|
92
|
+
get asset(): string {
|
|
93
|
+
return this._asset;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
static get observedAttributes() {
|
|
97
|
+
return ['asset'];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
attributeChangedCallback(name: string, _oldValue: string, newValue: string) {
|
|
101
|
+
switch (name) {
|
|
102
|
+
case 'asset':
|
|
103
|
+
this.asset = newValue;
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
customElements.define('pc-model', ModelElement);
|
|
110
|
+
|
|
111
|
+
export { ModelElement };
|