@playcanvas/web-components 0.1.12 → 0.2.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/README.md +1 -1
- package/dist/asset.d.ts +7 -7
- package/dist/components/camera-component.d.ts +8 -0
- package/dist/components/script-component.d.ts +30 -0
- package/dist/pwc.cjs +140 -68
- package/dist/pwc.cjs.map +1 -1
- package/dist/pwc.js +140 -68
- 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 +141 -69
- package/dist/pwc.mjs.map +1 -1
- package/package.json +9 -8
- package/src/asset.ts +14 -14
- package/src/components/camera-component.ts +8 -0
- package/src/components/script-component.ts +126 -59
package/dist/pwc.js
CHANGED
|
@@ -1005,7 +1005,7 @@
|
|
|
1005
1005
|
class AssetElement extends HTMLElement {
|
|
1006
1006
|
constructor() {
|
|
1007
1007
|
super(...arguments);
|
|
1008
|
-
this.
|
|
1008
|
+
this._lazy = false;
|
|
1009
1009
|
/**
|
|
1010
1010
|
* The asset that is loaded.
|
|
1011
1011
|
*/
|
|
@@ -1029,7 +1029,7 @@
|
|
|
1029
1029
|
return;
|
|
1030
1030
|
}
|
|
1031
1031
|
this.asset = new playcanvas.Asset(id, type, { url: src });
|
|
1032
|
-
this.asset.preload = this.
|
|
1032
|
+
this.asset.preload = !this._lazy;
|
|
1033
1033
|
}
|
|
1034
1034
|
destroyAsset() {
|
|
1035
1035
|
if (this.asset) {
|
|
@@ -1038,32 +1038,32 @@
|
|
|
1038
1038
|
}
|
|
1039
1039
|
}
|
|
1040
1040
|
/**
|
|
1041
|
-
* Sets the
|
|
1042
|
-
* @param value - The
|
|
1041
|
+
* Sets whether the asset should be loaded lazily.
|
|
1042
|
+
* @param value - The lazy loading flag.
|
|
1043
1043
|
*/
|
|
1044
|
-
set
|
|
1045
|
-
this.
|
|
1044
|
+
set lazy(value) {
|
|
1045
|
+
this._lazy = value;
|
|
1046
1046
|
if (this.asset) {
|
|
1047
|
-
this.asset.preload = value;
|
|
1047
|
+
this.asset.preload = !value;
|
|
1048
1048
|
}
|
|
1049
1049
|
}
|
|
1050
1050
|
/**
|
|
1051
|
-
* Gets the
|
|
1052
|
-
* @returns The
|
|
1051
|
+
* Gets whether the asset should be loaded lazily.
|
|
1052
|
+
* @returns The lazy loading flag.
|
|
1053
1053
|
*/
|
|
1054
|
-
get
|
|
1055
|
-
return this.
|
|
1054
|
+
get lazy() {
|
|
1055
|
+
return this._lazy;
|
|
1056
1056
|
}
|
|
1057
1057
|
static get(id) {
|
|
1058
1058
|
const assetElement = document.querySelector(`pc-asset[id="${id}"]`);
|
|
1059
1059
|
return assetElement === null || assetElement === undefined ? undefined : assetElement.asset;
|
|
1060
1060
|
}
|
|
1061
1061
|
static get observedAttributes() {
|
|
1062
|
-
return ['
|
|
1062
|
+
return ['lazy'];
|
|
1063
1063
|
}
|
|
1064
1064
|
attributeChangedCallback(name, _oldValue, _newValue) {
|
|
1065
|
-
if (name === '
|
|
1066
|
-
this.
|
|
1065
|
+
if (name === 'lazy') {
|
|
1066
|
+
this.lazy = this.hasAttribute('lazy');
|
|
1067
1067
|
}
|
|
1068
1068
|
}
|
|
1069
1069
|
}
|
|
@@ -1237,6 +1237,11 @@
|
|
|
1237
1237
|
const xrManager = (_a = this.component) === null || _a === undefined ? undefined : _a.system.app.xr;
|
|
1238
1238
|
return xrManager && xrManager.supported && xrManager.isAvailable(playcanvas.XRTYPE_VR);
|
|
1239
1239
|
}
|
|
1240
|
+
/**
|
|
1241
|
+
* Starts the camera in XR mode.
|
|
1242
|
+
* @param type - The type of XR mode to start.
|
|
1243
|
+
* @param space - The space to start the camera in.
|
|
1244
|
+
*/
|
|
1240
1245
|
startXr(type, space) {
|
|
1241
1246
|
if (this.component && this.xrAvailable) {
|
|
1242
1247
|
this.component.startXr(type, space, {
|
|
@@ -1247,6 +1252,9 @@
|
|
|
1247
1252
|
});
|
|
1248
1253
|
}
|
|
1249
1254
|
}
|
|
1255
|
+
/**
|
|
1256
|
+
* Ends the camera's XR mode.
|
|
1257
|
+
*/
|
|
1250
1258
|
endXr() {
|
|
1251
1259
|
if (this.component) {
|
|
1252
1260
|
this.component.endXr();
|
|
@@ -3122,62 +3130,116 @@
|
|
|
3122
3130
|
}
|
|
3123
3131
|
});
|
|
3124
3132
|
}
|
|
3125
|
-
|
|
3126
|
-
|
|
3127
|
-
|
|
3128
|
-
|
|
3129
|
-
|
|
3130
|
-
|
|
3131
|
-
|
|
3132
|
-
|
|
3133
|
-
|
|
3134
|
-
|
|
3135
|
-
|
|
3136
|
-
|
|
3133
|
+
/**
|
|
3134
|
+
* Recursively converts raw attribute data into proper PlayCanvas types. Supported conversions:
|
|
3135
|
+
* - "asset:assetId" → resolves to an Asset instance
|
|
3136
|
+
* - "entity:entityId" → resolves to an Entity instance
|
|
3137
|
+
* - "vec2:1,2" → new Vec2(1,2)
|
|
3138
|
+
* - "vec3:1,2,3" → new Vec3(1,2,3)
|
|
3139
|
+
* - "vec4:1,2,3,4" → new Vec4(1,2,3,4)
|
|
3140
|
+
* - "color:1,0.5,0.5,1" → new Color(1,0.5,0.5,1)
|
|
3141
|
+
* @param item - The item to convert.
|
|
3142
|
+
* @returns The converted item.
|
|
3143
|
+
*/
|
|
3144
|
+
convertAttributes(item) {
|
|
3145
|
+
if (typeof item === 'string') {
|
|
3146
|
+
if (item.startsWith('asset:')) {
|
|
3147
|
+
const assetId = item.slice(6);
|
|
3148
|
+
const assetElement = document.querySelector(`pc-asset#${assetId}`);
|
|
3149
|
+
if (assetElement) {
|
|
3150
|
+
return assetElement.asset;
|
|
3137
3151
|
}
|
|
3138
|
-
|
|
3139
|
-
|
|
3140
|
-
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
|
|
3144
|
-
for (const itemKey in item) {
|
|
3145
|
-
applyValue(obj, itemKey, item[itemKey]);
|
|
3146
|
-
}
|
|
3147
|
-
return obj;
|
|
3148
|
-
});
|
|
3149
|
-
return;
|
|
3150
|
-
}
|
|
3151
|
-
// Handle vectors
|
|
3152
|
-
if (value.length === 2 && typeof value[0] === 'number') {
|
|
3153
|
-
target[key] = new playcanvas.Vec2(value[0], value[1]);
|
|
3154
|
-
return;
|
|
3155
|
-
}
|
|
3156
|
-
if (value.length === 3 && typeof value[0] === 'number') {
|
|
3157
|
-
target[key] = new playcanvas.Vec3(value[0], value[1], value[2]);
|
|
3158
|
-
return;
|
|
3159
|
-
}
|
|
3160
|
-
if (value.length === 4 && typeof value[0] === 'number') {
|
|
3161
|
-
target[key] = new playcanvas.Vec4(value[0], value[1], value[2], value[3]);
|
|
3162
|
-
return;
|
|
3163
|
-
}
|
|
3152
|
+
}
|
|
3153
|
+
if (item.startsWith('entity:')) {
|
|
3154
|
+
const entityId = item.slice(7);
|
|
3155
|
+
const entityElement = document.querySelector(`pc-entity[name="${entityId}"]`);
|
|
3156
|
+
if (entityElement) {
|
|
3157
|
+
return entityElement.entity;
|
|
3164
3158
|
}
|
|
3165
|
-
|
|
3166
|
-
|
|
3167
|
-
|
|
3168
|
-
|
|
3169
|
-
|
|
3170
|
-
for (const nestedKey in value) {
|
|
3171
|
-
applyValue(target[key], nestedKey, value[nestedKey]);
|
|
3172
|
-
}
|
|
3159
|
+
}
|
|
3160
|
+
if (item.startsWith('vec2:')) {
|
|
3161
|
+
const parts = item.slice(5).split(',').map(Number);
|
|
3162
|
+
if (parts.length === 2 && parts.every(v => !isNaN(v))) {
|
|
3163
|
+
return new playcanvas.Vec2(parts[0], parts[1]);
|
|
3173
3164
|
}
|
|
3174
|
-
|
|
3175
|
-
|
|
3165
|
+
}
|
|
3166
|
+
if (item.startsWith('vec3:')) {
|
|
3167
|
+
const parts = item.slice(5).split(',').map(Number);
|
|
3168
|
+
if (parts.length === 3 && parts.every(v => !isNaN(v))) {
|
|
3169
|
+
return new playcanvas.Vec3(parts[0], parts[1], parts[2]);
|
|
3170
|
+
}
|
|
3171
|
+
}
|
|
3172
|
+
if (item.startsWith('vec4:')) {
|
|
3173
|
+
const parts = item.slice(5).split(',').map(Number);
|
|
3174
|
+
if (parts.length === 4 && parts.every(v => !isNaN(v))) {
|
|
3175
|
+
return new playcanvas.Vec4(parts[0], parts[1], parts[2], parts[3]);
|
|
3176
|
+
}
|
|
3177
|
+
}
|
|
3178
|
+
if (item.startsWith('color:')) {
|
|
3179
|
+
const parts = item.slice(6).split(',').map(Number);
|
|
3180
|
+
if (parts.length === 4 && parts.every(v => !isNaN(v))) {
|
|
3181
|
+
return new playcanvas.Color(parts[0], parts[1], parts[2], parts[3]);
|
|
3176
3182
|
}
|
|
3177
|
-
};
|
|
3178
|
-
for (const key in attributesObject) {
|
|
3179
|
-
applyValue(script, key, attributesObject[key]);
|
|
3180
3183
|
}
|
|
3184
|
+
return item;
|
|
3185
|
+
}
|
|
3186
|
+
if (Array.isArray(item)) {
|
|
3187
|
+
// If it's an array of objects, convert each element individually.
|
|
3188
|
+
if (item.length > 0 && typeof item[0] === 'object') {
|
|
3189
|
+
return item.map((el) => this.convertAttributes(el));
|
|
3190
|
+
}
|
|
3191
|
+
// Otherwise, leave the numeric array unchanged but process each element.
|
|
3192
|
+
return item.map((el) => this.convertAttributes(el));
|
|
3193
|
+
}
|
|
3194
|
+
if (item && typeof item === 'object') {
|
|
3195
|
+
const result = {};
|
|
3196
|
+
for (const key in item) {
|
|
3197
|
+
result[key] = this.convertAttributes(item[key]);
|
|
3198
|
+
}
|
|
3199
|
+
return result;
|
|
3200
|
+
}
|
|
3201
|
+
return item;
|
|
3202
|
+
}
|
|
3203
|
+
/**
|
|
3204
|
+
* Preprocess the attributes object by converting its values.
|
|
3205
|
+
* @param attrs - The attributes object to preprocess.
|
|
3206
|
+
* @returns The preprocessed attributes object.
|
|
3207
|
+
*/
|
|
3208
|
+
preprocessAttributes(attrs) {
|
|
3209
|
+
return this.convertAttributes(attrs);
|
|
3210
|
+
}
|
|
3211
|
+
/**
|
|
3212
|
+
* Recursively merge properties from source into target.
|
|
3213
|
+
* @param target - The target object to merge into.
|
|
3214
|
+
* @param source - The source object to merge from.
|
|
3215
|
+
* @returns The merged object.
|
|
3216
|
+
*/
|
|
3217
|
+
mergeDeep(target, source) {
|
|
3218
|
+
for (const key in source) {
|
|
3219
|
+
if (source[key] &&
|
|
3220
|
+
typeof source[key] === 'object' &&
|
|
3221
|
+
!Array.isArray(source[key])) {
|
|
3222
|
+
if (!target[key] || typeof target[key] !== 'object') {
|
|
3223
|
+
target[key] = {};
|
|
3224
|
+
}
|
|
3225
|
+
this.mergeDeep(target[key], source[key]);
|
|
3226
|
+
}
|
|
3227
|
+
else {
|
|
3228
|
+
target[key] = source[key];
|
|
3229
|
+
}
|
|
3230
|
+
}
|
|
3231
|
+
return target;
|
|
3232
|
+
}
|
|
3233
|
+
/**
|
|
3234
|
+
* Update script attributes by merging preprocessed values into the script.
|
|
3235
|
+
* @param script - The script to update.
|
|
3236
|
+
* @param attributes - The attributes to merge into the script.
|
|
3237
|
+
*/
|
|
3238
|
+
applyAttributes(script, attributes) {
|
|
3239
|
+
try {
|
|
3240
|
+
const attributesObject = attributes ? JSON.parse(attributes) : {};
|
|
3241
|
+
const converted = this.convertAttributes(attributesObject);
|
|
3242
|
+
this.mergeDeep(script, converted);
|
|
3181
3243
|
}
|
|
3182
3244
|
catch (error) {
|
|
3183
3245
|
console.error(`Error parsing attributes JSON string ${attributes}:`, error);
|
|
@@ -3206,10 +3268,20 @@
|
|
|
3206
3268
|
createScript(name, attributes) {
|
|
3207
3269
|
if (!this.component)
|
|
3208
3270
|
return null;
|
|
3209
|
-
|
|
3210
|
-
|
|
3271
|
+
let attributesObject = {};
|
|
3272
|
+
if (attributes) {
|
|
3273
|
+
try {
|
|
3274
|
+
attributesObject = JSON.parse(attributes);
|
|
3275
|
+
// Preprocess attributes: convert arrays or strings into vectors, colors, asset references, etc.
|
|
3276
|
+
attributesObject = this.preprocessAttributes(attributesObject);
|
|
3277
|
+
}
|
|
3278
|
+
catch (error) {
|
|
3279
|
+
console.error(`Error parsing attributes JSON string ${attributes}:`, error);
|
|
3280
|
+
}
|
|
3281
|
+
}
|
|
3282
|
+
return this.component.create(name, {
|
|
3283
|
+
properties: attributesObject
|
|
3211
3284
|
});
|
|
3212
|
-
return this.component.create(name);
|
|
3213
3285
|
}
|
|
3214
3286
|
destroyScript(name) {
|
|
3215
3287
|
if (!this.component)
|