@playcanvas/web-components 0.2.12 → 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.
Files changed (41) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +84 -84
  3. package/dist/components/element-component.d.ts +12 -0
  4. package/dist/components/light-component.d.ts +48 -0
  5. package/dist/components/splat-component.d.ts +0 -13
  6. package/dist/pwc.cjs +298 -207
  7. package/dist/pwc.cjs.map +1 -1
  8. package/dist/pwc.js +298 -207
  9. package/dist/pwc.js.map +1 -1
  10. package/dist/pwc.min.js +1 -1
  11. package/dist/pwc.min.js.map +1 -1
  12. package/dist/pwc.mjs +299 -208
  13. package/dist/pwc.mjs.map +1 -1
  14. package/package.json +76 -64
  15. package/src/app.ts +612 -606
  16. package/src/asset.ts +159 -159
  17. package/src/async-element.ts +46 -46
  18. package/src/colors.ts +150 -150
  19. package/src/components/camera-component.ts +557 -557
  20. package/src/components/collision-component.ts +183 -183
  21. package/src/components/component.ts +97 -97
  22. package/src/components/element-component.ts +367 -341
  23. package/src/components/light-component.ts +570 -466
  24. package/src/components/listener-component.ts +30 -30
  25. package/src/components/particlesystem-component.ts +155 -155
  26. package/src/components/render-component.ts +147 -147
  27. package/src/components/rigidbody-component.ts +227 -227
  28. package/src/components/screen-component.ts +157 -157
  29. package/src/components/script-component.ts +270 -270
  30. package/src/components/script.ts +90 -90
  31. package/src/components/sound-component.ts +230 -230
  32. package/src/components/sound-slot.ts +288 -288
  33. package/src/components/splat-component.ts +102 -133
  34. package/src/entity.ts +360 -360
  35. package/src/index.ts +63 -63
  36. package/src/material.ts +141 -141
  37. package/src/model.ts +111 -111
  38. package/src/module.ts +43 -43
  39. package/src/scene.ts +217 -217
  40. package/src/sky.ts +293 -293
  41. package/src/utils.ts +71 -71
package/src/asset.ts CHANGED
@@ -1,159 +1,159 @@
1
- import { Asset } from 'playcanvas';
2
-
3
- import { MeshoptDecoder } from '../lib/meshopt_decoder.module.js';
4
-
5
- const extToType = new Map([
6
- ['bin', 'binary'],
7
- ['css', 'css'],
8
- ['frag', 'shader'],
9
- ['glb', 'container'],
10
- ['glsl', 'shader'],
11
- ['hdr', 'texture'],
12
- ['html', 'html'],
13
- ['jpg', 'texture'],
14
- ['js', 'script'],
15
- ['json', 'json'],
16
- ['mp3', 'audio'],
17
- ['mjs', 'script'],
18
- ['ply', 'gsplat'],
19
- ['png', 'texture'],
20
- ['sog', 'gsplat'],
21
- ['txt', 'text'],
22
- ['vert', 'shader'],
23
- ['webp', 'texture']
24
- ]);
25
-
26
-
27
- // provide buffer view callback so we can handle models compressed with MeshOptimizer
28
- // https://github.com/zeux/meshoptimizer
29
- const processBufferView = (
30
- gltfBuffer: any,
31
- buffers: Array<any>,
32
- continuation: (err: string | null, result: any) => void
33
- ) => {
34
- if (gltfBuffer.extensions && gltfBuffer.extensions.EXT_meshopt_compression) {
35
- const extensionDef = gltfBuffer.extensions.EXT_meshopt_compression;
36
-
37
- Promise.all([MeshoptDecoder.ready, buffers[extensionDef.buffer]]).then((promiseResult) => {
38
- const buffer = promiseResult[1];
39
-
40
- const byteOffset = extensionDef.byteOffset || 0;
41
- const byteLength = extensionDef.byteLength || 0;
42
-
43
- const count = extensionDef.count;
44
- const stride = extensionDef.byteStride;
45
-
46
- const result = new Uint8Array(count * stride);
47
- const source = new Uint8Array(buffer.buffer, buffer.byteOffset + byteOffset, byteLength);
48
-
49
- MeshoptDecoder.decodeGltfBuffer(
50
- result,
51
- count,
52
- stride,
53
- source,
54
- extensionDef.mode,
55
- extensionDef.filter
56
- );
57
-
58
- continuation(null, result);
59
- });
60
- } else {
61
- continuation(null, null);
62
- }
63
- };
64
-
65
- /**
66
- * The AssetElement interface provides properties and methods for manipulating
67
- * {@link https://developer.playcanvas.com/user-manual/web-components/tags/pc-asset/ | `<pc-asset>`} elements.
68
- * The AssetElement interface also inherits the properties and methods of the
69
- * {@link HTMLElement} interface.
70
- */
71
- class AssetElement extends HTMLElement {
72
- private _lazy: boolean = false;
73
-
74
- /**
75
- * The asset that is loaded.
76
- */
77
- asset: Asset | null = null;
78
-
79
- disconnectedCallback() {
80
- this.destroyAsset();
81
- }
82
-
83
- createAsset() {
84
- const id = this.getAttribute('id') || '';
85
- const src = this.getAttribute('src') || '';
86
- let type = this.getAttribute('type');
87
-
88
- // If no type is specified, try to infer it from the file extension.
89
- if (!type) {
90
- const ext = src.split('.').pop();
91
- type = extToType.get(ext || '') ?? null;
92
- }
93
-
94
- if (!type) {
95
- console.warn(`Unsupported asset type: ${src}`);
96
- return;
97
- }
98
-
99
- if (type === 'container') {
100
- this.asset = new Asset(id, type, { url: src }, undefined, {
101
- // @ts-ignore TODO no definition in pc
102
- bufferView: {
103
- processAsync: processBufferView.bind(this)
104
- }
105
- });
106
- } else {
107
- // @ts-ignore
108
- this.asset = new Asset(id, type, { url: src });
109
- }
110
-
111
- this.asset.preload = !this._lazy;
112
- }
113
-
114
-
115
- destroyAsset() {
116
- if (this.asset) {
117
- this.asset.unload();
118
- this.asset = null;
119
- }
120
- }
121
-
122
- /**
123
- * Sets whether the asset should be loaded lazily.
124
- * @param value - The lazy loading flag.
125
- */
126
- set lazy(value: boolean) {
127
- this._lazy = value;
128
- if (this.asset) {
129
- this.asset.preload = !value;
130
- }
131
- }
132
-
133
- /**
134
- * Gets whether the asset should be loaded lazily.
135
- * @returns The lazy loading flag.
136
- */
137
- get lazy() {
138
- return this._lazy;
139
- }
140
-
141
- static get(id: string) {
142
- const assetElement = document.querySelector<AssetElement>(`pc-asset[id="${id}"]`);
143
- return assetElement?.asset;
144
- }
145
-
146
- static get observedAttributes() {
147
- return ['lazy'];
148
- }
149
-
150
- attributeChangedCallback(name: string, _oldValue: string, _newValue: string) {
151
- if (name === 'lazy') {
152
- this.lazy = this.hasAttribute('lazy');
153
- }
154
- }
155
- }
156
-
157
- customElements.define('pc-asset', AssetElement);
158
-
159
- export { AssetElement };
1
+ import { Asset } from 'playcanvas';
2
+
3
+ import { MeshoptDecoder } from '../lib/meshopt_decoder.module.js';
4
+
5
+ const extToType = new Map([
6
+ ['bin', 'binary'],
7
+ ['css', 'css'],
8
+ ['frag', 'shader'],
9
+ ['glb', 'container'],
10
+ ['glsl', 'shader'],
11
+ ['hdr', 'texture'],
12
+ ['html', 'html'],
13
+ ['jpg', 'texture'],
14
+ ['js', 'script'],
15
+ ['json', 'json'],
16
+ ['mp3', 'audio'],
17
+ ['mjs', 'script'],
18
+ ['ply', 'gsplat'],
19
+ ['png', 'texture'],
20
+ ['sog', 'gsplat'],
21
+ ['txt', 'text'],
22
+ ['vert', 'shader'],
23
+ ['webp', 'texture']
24
+ ]);
25
+
26
+
27
+ // provide buffer view callback so we can handle models compressed with MeshOptimizer
28
+ // https://github.com/zeux/meshoptimizer
29
+ const processBufferView = (
30
+ gltfBuffer: any,
31
+ buffers: Array<any>,
32
+ continuation: (err: string | null, result: any) => void
33
+ ) => {
34
+ if (gltfBuffer.extensions && gltfBuffer.extensions.EXT_meshopt_compression) {
35
+ const extensionDef = gltfBuffer.extensions.EXT_meshopt_compression;
36
+
37
+ Promise.all([MeshoptDecoder.ready, buffers[extensionDef.buffer]]).then((promiseResult) => {
38
+ const buffer = promiseResult[1];
39
+
40
+ const byteOffset = extensionDef.byteOffset || 0;
41
+ const byteLength = extensionDef.byteLength || 0;
42
+
43
+ const count = extensionDef.count;
44
+ const stride = extensionDef.byteStride;
45
+
46
+ const result = new Uint8Array(count * stride);
47
+ const source = new Uint8Array(buffer.buffer, buffer.byteOffset + byteOffset, byteLength);
48
+
49
+ MeshoptDecoder.decodeGltfBuffer(
50
+ result,
51
+ count,
52
+ stride,
53
+ source,
54
+ extensionDef.mode,
55
+ extensionDef.filter
56
+ );
57
+
58
+ continuation(null, result);
59
+ });
60
+ } else {
61
+ continuation(null, null);
62
+ }
63
+ };
64
+
65
+ /**
66
+ * The AssetElement interface provides properties and methods for manipulating
67
+ * {@link https://developer.playcanvas.com/user-manual/web-components/tags/pc-asset/ | `<pc-asset>`} elements.
68
+ * The AssetElement interface also inherits the properties and methods of the
69
+ * {@link HTMLElement} interface.
70
+ */
71
+ class AssetElement extends HTMLElement {
72
+ private _lazy: boolean = false;
73
+
74
+ /**
75
+ * The asset that is loaded.
76
+ */
77
+ asset: Asset | null = null;
78
+
79
+ disconnectedCallback() {
80
+ this.destroyAsset();
81
+ }
82
+
83
+ createAsset() {
84
+ const id = this.getAttribute('id') || '';
85
+ const src = this.getAttribute('src') || '';
86
+ let type = this.getAttribute('type');
87
+
88
+ // If no type is specified, try to infer it from the file extension.
89
+ if (!type) {
90
+ const ext = src.split('.').pop();
91
+ type = extToType.get(ext || '') ?? null;
92
+ }
93
+
94
+ if (!type) {
95
+ console.warn(`Unsupported asset type: ${src}`);
96
+ return;
97
+ }
98
+
99
+ if (type === 'container') {
100
+ this.asset = new Asset(id, type, { url: src }, undefined, {
101
+ // @ts-ignore TODO no definition in pc
102
+ bufferView: {
103
+ processAsync: processBufferView.bind(this)
104
+ }
105
+ });
106
+ } else {
107
+ // @ts-ignore
108
+ this.asset = new Asset(id, type, { url: src });
109
+ }
110
+
111
+ this.asset.preload = !this._lazy;
112
+ }
113
+
114
+
115
+ destroyAsset() {
116
+ if (this.asset) {
117
+ this.asset.unload();
118
+ this.asset = null;
119
+ }
120
+ }
121
+
122
+ /**
123
+ * Sets whether the asset should be loaded lazily.
124
+ * @param value - The lazy loading flag.
125
+ */
126
+ set lazy(value: boolean) {
127
+ this._lazy = value;
128
+ if (this.asset) {
129
+ this.asset.preload = !value;
130
+ }
131
+ }
132
+
133
+ /**
134
+ * Gets whether the asset should be loaded lazily.
135
+ * @returns The lazy loading flag.
136
+ */
137
+ get lazy() {
138
+ return this._lazy;
139
+ }
140
+
141
+ static get(id: string) {
142
+ const assetElement = document.querySelector<AssetElement>(`pc-asset[id="${id}"]`);
143
+ return assetElement?.asset;
144
+ }
145
+
146
+ static get observedAttributes() {
147
+ return ['lazy'];
148
+ }
149
+
150
+ attributeChangedCallback(name: string, _oldValue: string, _newValue: string) {
151
+ if (name === 'lazy') {
152
+ this.lazy = this.hasAttribute('lazy');
153
+ }
154
+ }
155
+ }
156
+
157
+ customElements.define('pc-asset', AssetElement);
158
+
159
+ export { AssetElement };
@@ -1,46 +1,46 @@
1
- import { AppElement } from './app';
2
- import { EntityElement } from './entity';
3
-
4
- /**
5
- * Base class for all PlayCanvas Web Components that initialize asynchronously.
6
- */
7
- class AsyncElement extends HTMLElement {
8
- private _readyPromise: Promise<void>;
9
-
10
- private _readyResolve!: () => void;
11
-
12
- /** @ignore */
13
- constructor() {
14
- super();
15
- this._readyPromise = new Promise<void>((resolve) => {
16
- this._readyResolve = resolve;
17
- });
18
- }
19
-
20
- get closestApp(): AppElement {
21
- return this.parentElement?.closest('pc-app') as AppElement;
22
- }
23
-
24
- get closestEntity(): EntityElement {
25
- return this.parentElement?.closest('pc-entity') as EntityElement;
26
- }
27
-
28
- /**
29
- * Called when the element is fully initialized and ready.
30
- * Subclasses should call this when they're ready.
31
- */
32
- protected _onReady() {
33
- this._readyResolve();
34
- this.dispatchEvent(new CustomEvent('ready'));
35
- }
36
-
37
- /**
38
- * Returns a promise that resolves with this element when it's ready.
39
- * @returns A promise that resolves with this element when it's ready.
40
- */
41
- ready(): Promise<this> {
42
- return this._readyPromise.then(() => this);
43
- }
44
- }
45
-
46
- export { AsyncElement };
1
+ import { AppElement } from './app';
2
+ import { EntityElement } from './entity';
3
+
4
+ /**
5
+ * Base class for all PlayCanvas Web Components that initialize asynchronously.
6
+ */
7
+ class AsyncElement extends HTMLElement {
8
+ private _readyPromise: Promise<void>;
9
+
10
+ private _readyResolve!: () => void;
11
+
12
+ /** @ignore */
13
+ constructor() {
14
+ super();
15
+ this._readyPromise = new Promise<void>((resolve) => {
16
+ this._readyResolve = resolve;
17
+ });
18
+ }
19
+
20
+ get closestApp(): AppElement {
21
+ return this.parentElement?.closest('pc-app') as AppElement;
22
+ }
23
+
24
+ get closestEntity(): EntityElement {
25
+ return this.parentElement?.closest('pc-entity') as EntityElement;
26
+ }
27
+
28
+ /**
29
+ * Called when the element is fully initialized and ready.
30
+ * Subclasses should call this when they're ready.
31
+ */
32
+ protected _onReady() {
33
+ this._readyResolve();
34
+ this.dispatchEvent(new CustomEvent('ready'));
35
+ }
36
+
37
+ /**
38
+ * Returns a promise that resolves with this element when it's ready.
39
+ * @returns A promise that resolves with this element when it's ready.
40
+ */
41
+ ready(): Promise<this> {
42
+ return this._readyPromise.then(() => this);
43
+ }
44
+ }
45
+
46
+ export { AsyncElement };