@playcanvas/web-components 0.1.13 → 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/dist/pwc.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { basisInitialize, WasmModule, Application, Keyboard, Mouse, FILLMODE_FILL_WINDOW, RESOLUTION_AUTO, Picker, Color, Quat, Vec2, Vec3, Vec4, Entity, Asset, GAMMA_SRGB, GAMMA_NONE, XRTYPE_VR, PROJECTION_ORTHOGRAPHIC, PROJECTION_PERSPECTIVE, TONEMAP_NONE, TONEMAP_LINEAR, TONEMAP_FILMIC, TONEMAP_HEJL, TONEMAP_ACES, TONEMAP_ACES2, TONEMAP_NEUTRAL, SHADOW_PCF3_32F, SHADOW_PCF1_16F, SHADOW_PCF1_32F, SHADOW_PCF3_16F, SHADOW_PCF5_16F, SHADOW_PCF5_32F, SHADOW_VSM_16F, SHADOW_VSM_32F, SHADOW_PCSS_32F, StandardMaterial, SCALEMODE_BLEND, SCALEMODE_NONE, EnvLighting, LAYERID_SKYBOX } from 'playcanvas';
1
+ import { basisInitialize, WasmModule, Application, Mouse, Keyboard, FILLMODE_FILL_WINDOW, RESOLUTION_AUTO, Picker, Vec3, Vec4, Color, Quat, Vec2, Entity, Asset, GAMMA_SRGB, GAMMA_NONE, XRTYPE_VR, PROJECTION_ORTHOGRAPHIC, PROJECTION_PERSPECTIVE, TONEMAP_NONE, TONEMAP_LINEAR, TONEMAP_FILMIC, TONEMAP_HEJL, TONEMAP_ACES, TONEMAP_ACES2, TONEMAP_NEUTRAL, SHADOW_PCF3_32F, SHADOW_PCF1_16F, SHADOW_PCF1_32F, SHADOW_PCF3_16F, SHADOW_PCF5_16F, SHADOW_PCF5_32F, SHADOW_VSM_16F, SHADOW_VSM_32F, SHADOW_PCSS_32F, StandardMaterial, SCALEMODE_BLEND, SCALEMODE_NONE, EnvLighting, LAYERID_SKYBOX } from 'playcanvas';
2
2
 
3
3
  /**
4
4
  * Base class for all PlayCanvas Web Components that initialize asynchronously.
@@ -1233,6 +1233,11 @@ class CameraComponentElement extends ComponentElement {
1233
1233
  const xrManager = (_a = this.component) === null || _a === undefined ? undefined : _a.system.app.xr;
1234
1234
  return xrManager && xrManager.supported && xrManager.isAvailable(XRTYPE_VR);
1235
1235
  }
1236
+ /**
1237
+ * Starts the camera in XR mode.
1238
+ * @param type - The type of XR mode to start.
1239
+ * @param space - The space to start the camera in.
1240
+ */
1236
1241
  startXr(type, space) {
1237
1242
  if (this.component && this.xrAvailable) {
1238
1243
  this.component.startXr(type, space, {
@@ -1243,6 +1248,9 @@ class CameraComponentElement extends ComponentElement {
1243
1248
  });
1244
1249
  }
1245
1250
  }
1251
+ /**
1252
+ * Ends the camera's XR mode.
1253
+ */
1246
1254
  endXr() {
1247
1255
  if (this.component) {
1248
1256
  this.component.endXr();
@@ -3118,62 +3126,116 @@ class ScriptComponentElement extends ComponentElement {
3118
3126
  }
3119
3127
  });
3120
3128
  }
3121
- applyAttributes(script, attributes) {
3122
- try {
3123
- const attributesObject = attributes ? JSON.parse(attributes) : {};
3124
- const applyValue = (target, key, value) => {
3125
- // Handle asset references
3126
- if (typeof value === 'string' && value.startsWith('asset:')) {
3127
- const assetId = value.slice(6);
3128
- const assetElement = document.querySelector(`pc-asset#${assetId}`);
3129
- if (assetElement) {
3130
- target[key] = assetElement.asset;
3131
- return;
3132
- }
3129
+ /**
3130
+ * Recursively converts raw attribute data into proper PlayCanvas types. Supported conversions:
3131
+ * - "asset:assetId" resolves to an Asset instance
3132
+ * - "entity:entityId" resolves to an Entity instance
3133
+ * - "vec2:1,2" → new Vec2(1,2)
3134
+ * - "vec3:1,2,3" new Vec3(1,2,3)
3135
+ * - "vec4:1,2,3,4" → new Vec4(1,2,3,4)
3136
+ * - "color:1,0.5,0.5,1" → new Color(1,0.5,0.5,1)
3137
+ * @param item - The item to convert.
3138
+ * @returns The converted item.
3139
+ */
3140
+ convertAttributes(item) {
3141
+ if (typeof item === 'string') {
3142
+ if (item.startsWith('asset:')) {
3143
+ const assetId = item.slice(6);
3144
+ const assetElement = document.querySelector(`pc-asset#${assetId}`);
3145
+ if (assetElement) {
3146
+ return assetElement.asset;
3133
3147
  }
3134
- // Handle arrays
3135
- if (value && typeof value === 'object' && Array.isArray(value)) {
3136
- // If it's an array of objects, recursively apply to each object
3137
- if (value.length > 0 && typeof value[0] === 'object') {
3138
- target[key] = value.map((item) => {
3139
- const obj = {};
3140
- for (const itemKey in item) {
3141
- applyValue(obj, itemKey, item[itemKey]);
3142
- }
3143
- return obj;
3144
- });
3145
- return;
3146
- }
3147
- // Handle vectors
3148
- if (value.length === 2 && typeof value[0] === 'number') {
3149
- target[key] = new Vec2(value[0], value[1]);
3150
- return;
3151
- }
3152
- if (value.length === 3 && typeof value[0] === 'number') {
3153
- target[key] = new Vec3(value[0], value[1], value[2]);
3154
- return;
3155
- }
3156
- if (value.length === 4 && typeof value[0] === 'number') {
3157
- target[key] = new Vec4(value[0], value[1], value[2], value[3]);
3158
- return;
3159
- }
3148
+ }
3149
+ if (item.startsWith('entity:')) {
3150
+ const entityId = item.slice(7);
3151
+ const entityElement = document.querySelector(`pc-entity[name="${entityId}"]`);
3152
+ if (entityElement) {
3153
+ return entityElement.entity;
3160
3154
  }
3161
- // Handle nested objects (non-array)
3162
- if (value && typeof value === 'object' && !Array.isArray(value)) {
3163
- if (!target[key] || typeof target[key] !== 'object') {
3164
- target[key] = {};
3165
- }
3166
- for (const nestedKey in value) {
3167
- applyValue(target[key], nestedKey, value[nestedKey]);
3168
- }
3155
+ }
3156
+ if (item.startsWith('vec2:')) {
3157
+ const parts = item.slice(5).split(',').map(Number);
3158
+ if (parts.length === 2 && parts.every(v => !isNaN(v))) {
3159
+ return new Vec2(parts[0], parts[1]);
3169
3160
  }
3170
- else {
3171
- target[key] = value;
3161
+ }
3162
+ if (item.startsWith('vec3:')) {
3163
+ const parts = item.slice(5).split(',').map(Number);
3164
+ if (parts.length === 3 && parts.every(v => !isNaN(v))) {
3165
+ return new Vec3(parts[0], parts[1], parts[2]);
3166
+ }
3167
+ }
3168
+ if (item.startsWith('vec4:')) {
3169
+ const parts = item.slice(5).split(',').map(Number);
3170
+ if (parts.length === 4 && parts.every(v => !isNaN(v))) {
3171
+ return new Vec4(parts[0], parts[1], parts[2], parts[3]);
3172
+ }
3173
+ }
3174
+ if (item.startsWith('color:')) {
3175
+ const parts = item.slice(6).split(',').map(Number);
3176
+ if (parts.length === 4 && parts.every(v => !isNaN(v))) {
3177
+ return new Color(parts[0], parts[1], parts[2], parts[3]);
3172
3178
  }
3173
- };
3174
- for (const key in attributesObject) {
3175
- applyValue(script, key, attributesObject[key]);
3176
3179
  }
3180
+ return item;
3181
+ }
3182
+ if (Array.isArray(item)) {
3183
+ // If it's an array of objects, convert each element individually.
3184
+ if (item.length > 0 && typeof item[0] === 'object') {
3185
+ return item.map((el) => this.convertAttributes(el));
3186
+ }
3187
+ // Otherwise, leave the numeric array unchanged but process each element.
3188
+ return item.map((el) => this.convertAttributes(el));
3189
+ }
3190
+ if (item && typeof item === 'object') {
3191
+ const result = {};
3192
+ for (const key in item) {
3193
+ result[key] = this.convertAttributes(item[key]);
3194
+ }
3195
+ return result;
3196
+ }
3197
+ return item;
3198
+ }
3199
+ /**
3200
+ * Preprocess the attributes object by converting its values.
3201
+ * @param attrs - The attributes object to preprocess.
3202
+ * @returns The preprocessed attributes object.
3203
+ */
3204
+ preprocessAttributes(attrs) {
3205
+ return this.convertAttributes(attrs);
3206
+ }
3207
+ /**
3208
+ * Recursively merge properties from source into target.
3209
+ * @param target - The target object to merge into.
3210
+ * @param source - The source object to merge from.
3211
+ * @returns The merged object.
3212
+ */
3213
+ mergeDeep(target, source) {
3214
+ for (const key in source) {
3215
+ if (source[key] &&
3216
+ typeof source[key] === 'object' &&
3217
+ !Array.isArray(source[key])) {
3218
+ if (!target[key] || typeof target[key] !== 'object') {
3219
+ target[key] = {};
3220
+ }
3221
+ this.mergeDeep(target[key], source[key]);
3222
+ }
3223
+ else {
3224
+ target[key] = source[key];
3225
+ }
3226
+ }
3227
+ return target;
3228
+ }
3229
+ /**
3230
+ * Update script attributes by merging preprocessed values into the script.
3231
+ * @param script - The script to update.
3232
+ * @param attributes - The attributes to merge into the script.
3233
+ */
3234
+ applyAttributes(script, attributes) {
3235
+ try {
3236
+ const attributesObject = attributes ? JSON.parse(attributes) : {};
3237
+ const converted = this.convertAttributes(attributesObject);
3238
+ this.mergeDeep(script, converted);
3177
3239
  }
3178
3240
  catch (error) {
3179
3241
  console.error(`Error parsing attributes JSON string ${attributes}:`, error);
@@ -3202,10 +3264,20 @@ class ScriptComponentElement extends ComponentElement {
3202
3264
  createScript(name, attributes) {
3203
3265
  if (!this.component)
3204
3266
  return null;
3205
- this.component.on(`create:${name}`, (script) => {
3206
- this.applyAttributes(script, attributes);
3267
+ let attributesObject = {};
3268
+ if (attributes) {
3269
+ try {
3270
+ attributesObject = JSON.parse(attributes);
3271
+ // Preprocess attributes: convert arrays or strings into vectors, colors, asset references, etc.
3272
+ attributesObject = this.preprocessAttributes(attributesObject);
3273
+ }
3274
+ catch (error) {
3275
+ console.error(`Error parsing attributes JSON string ${attributes}:`, error);
3276
+ }
3277
+ }
3278
+ return this.component.create(name, {
3279
+ properties: attributesObject
3207
3280
  });
3208
- return this.component.create(name);
3209
3281
  }
3210
3282
  destroyScript(name) {
3211
3283
  if (!this.component)