@archvisioninc/canvas 2.6.6 → 2.6.8

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.
@@ -153,7 +153,7 @@ export const updateMaterial = inboundData => {
153
153
  albedoHasAlpha,
154
154
  clearTextureChannels
155
155
  } = payload;
156
- const material = scene.getMaterialByName(id, true);
156
+ const material = scene?.getMaterialByName?.(id, true);
157
157
  if (material) {
158
158
  // Renaming material.
159
159
  if (newId) {
@@ -182,7 +182,7 @@ export const updateMaterial = inboundData => {
182
182
 
183
183
  // Reset supported texture channels, useful when manually importing a new material.
184
184
  if (clearTextureChannels) {
185
- const workingMaterial = scene.metadata.materials.find(mat => mat.materialId === id);
185
+ const workingMaterial = scene.metadata.materials.find(mat => mat.materialId === id) || {};
186
186
  Object.keys(workingMaterial)?.forEach(key => {
187
187
  if (key.endsWith('Texture')) {
188
188
  material[key] = null;
@@ -1,3 +1,4 @@
1
+ /* eslint-disable */
1
2
  import { GLTF2 } from 'babylonjs-loaders';
2
3
  import { GIZMOS, GUI, TRANSPARENCY_MODES } from '../constants';
3
4
  import { orthoOptions } from '../enums';
@@ -409,45 +410,50 @@ export const buildMeshPositionsArray = args => {
409
410
  export const getImageFileFromBuffer = args => {
410
411
  const {
411
412
  buffer,
412
- asBase64Data
413
+ asDataUrl
413
414
  } = args || {};
414
415
  if (_.isEmpty(buffer)) return '';
415
- try {
416
- const blob = new Blob([buffer], {
416
+ if (asDataUrl) {
417
+ const ascii = new Uint8Array(buffer);
418
+ const Uint8ToString = u8a => {
419
+ if (_.isEmpty(u8a)) return '';
420
+ const maxChunkSize = 8192;
421
+ const chunks = [];
422
+ for (let i = 0; i < u8a.length; i += maxChunkSize) {
423
+ const newChunk = String.fromCharCode.apply(null, u8a.subarray(i, i + maxChunkSize));
424
+ chunks.push(newChunk);
425
+ }
426
+ return chunks.join('');
427
+ };
428
+ const dataString = btoa(Uint8ToString(ascii));
429
+ const blob = new Blob([dataString], {
417
430
  type: 'image/png'
418
431
  });
419
- const url = URL.createObjectURL(blob);
420
- if (asBase64Data) {
421
- return new Promise((resolve, reject) => {
422
- const reader = new FileReader();
423
- reader.onloadend = () => {
424
- resolve(reader.result);
425
- };
426
- reader.onerror = error => {
427
- console.error('Error reading buffer: ', error);
428
- reject(error);
429
- };
430
- reader.readAsDataURL(blob);
431
- });
432
- }
433
- return Promise.resolve(url);
434
- } catch (error) {
435
- console.error('Unable to get image from buffer: ', error);
436
- return Promise.reject(error);
432
+ const blobUrl = URL.createObjectURL(blob);
433
+ console.log({
434
+ dataString,
435
+ blob,
436
+ blobUrl
437
+ });
438
+ return blobUrl;
437
439
  }
440
+ const blob = new Blob([buffer], {
441
+ type: 'image/png'
442
+ });
443
+ const url = URL.createObjectURL(blob);
444
+ return url;
438
445
  };
439
- export const getTextureUrl = async args => {
446
+ export const getTextureUrl = args => {
440
447
  const {
441
448
  texture,
442
- asBase64Data
449
+ asDataUrl
443
450
  } = args || {};
444
451
  const buffer = texture?._buffer;
445
452
  if (_.isEmpty(buffer)) return texture?.url || '';
446
- const url = await getImageFileFromBuffer({
453
+ return getImageFileFromBuffer({
447
454
  buffer,
448
- asBase64Data
455
+ asDataUrl
449
456
  });
450
- return url;
451
457
  };
452
458
  export const materialData = material => {
453
459
  const meshesWithMaterial = getUserMeshes().map(mesh => {
@@ -472,7 +478,7 @@ export const materialData = material => {
472
478
  name: material.albedoTexture?.name,
473
479
  url: getTextureUrl({
474
480
  texture: material.albedoTexture,
475
- asBase64Data: true
481
+ asDataUrl: true
476
482
  })
477
483
  },
478
484
  albedoHasAlpha: material.albedoTexture?.hasAlpha || false,
@@ -482,7 +488,7 @@ export const materialData = material => {
482
488
  name: material.metallicTexture?.name,
483
489
  url: getTextureUrl({
484
490
  texture: material.metallicTexture,
485
- asBase64Data: true
491
+ asDataUrl: true
486
492
  })
487
493
  },
488
494
  roughness: material.roughness,
@@ -490,7 +496,7 @@ export const materialData = material => {
490
496
  name: material.microSurfaceTexture?.name,
491
497
  url: getTextureUrl({
492
498
  texture: material.microSurfaceTexture,
493
- asBase64Data: true
499
+ asDataUrl: true
494
500
  })
495
501
  },
496
502
  emissiveColor: material.emissiveColor?.toHexString() || props?.theme?.colors?.[props.$selectedTheme]?.black,
@@ -499,7 +505,7 @@ export const materialData = material => {
499
505
  name: material.emissiveTexture?.name,
500
506
  url: getTextureUrl({
501
507
  texture: material.emissiveTexture,
502
- asBase64Data: true
508
+ asDataUrl: true
503
509
  })
504
510
  },
505
511
  bumpIntensity: material.bumpTexture?.level || 0.5,
@@ -507,7 +513,7 @@ export const materialData = material => {
507
513
  name: material.bumpTexture?.name,
508
514
  url: getTextureUrl({
509
515
  texture: material.bumpTexture,
510
- asBase64Data: true
516
+ asDataUrl: true
511
517
  })
512
518
  },
513
519
  // Advanced
@@ -520,7 +526,7 @@ export const materialData = material => {
520
526
  name: material.opacityTexture?.name,
521
527
  url: getTextureUrl({
522
528
  texture: material.opacityTexture,
523
- asBase64Data: true
529
+ asDataUrl: true
524
530
  })
525
531
  },
526
532
  transparencyMode: material.transparencyMode,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@archvisioninc/canvas",
3
- "version": "2.6.6",
3
+ "version": "2.6.8",
4
4
  "private": false,
5
5
  "main": "dist/Canvas.js",
6
6
  "module": "dist/Canvas.js",
@@ -190,7 +190,7 @@ export const updateMaterial = inboundData => {
190
190
  clearTextureChannels,
191
191
  } = payload;
192
192
 
193
- const material = scene.getMaterialByName(id, true);
193
+ const material = scene?.getMaterialByName?.(id, true);
194
194
 
195
195
  if (material) {
196
196
  // Renaming material.
@@ -220,7 +220,7 @@ export const updateMaterial = inboundData => {
220
220
 
221
221
  // Reset supported texture channels, useful when manually importing a new material.
222
222
  if (clearTextureChannels) {
223
- const workingMaterial = scene.metadata.materials.find(mat => mat.materialId === id);
223
+ const workingMaterial = scene.metadata.materials.find(mat => mat.materialId === id) || {};
224
224
 
225
225
  Object.keys(workingMaterial)?.forEach(key => {
226
226
  if (key.endsWith('Texture')) {
@@ -1,3 +1,4 @@
1
+ /* eslint-disable */
1
2
  import { GLTF2 } from 'babylonjs-loaders';
2
3
  import { GIZMOS, GUI, TRANSPARENCY_MODES } from '../constants';
3
4
  import { orthoOptions } from '../enums';
@@ -467,45 +468,47 @@ export const buildMeshPositionsArray = args => {
467
468
  };
468
469
 
469
470
  export const getImageFileFromBuffer = args => {
470
- const { buffer, asBase64Data } = args || {};
471
+ const { buffer, asDataUrl } = args || {};
471
472
 
472
473
  if (_.isEmpty(buffer)) return '';
473
474
 
474
- try {
475
- const blob = new Blob([ buffer ], { type: 'image/png' });
476
- const url = URL.createObjectURL(blob);
475
+ if (asDataUrl) {
476
+ const ascii = new Uint8Array(buffer);
477
477
 
478
- if (asBase64Data) {
479
- return new Promise((resolve, reject) => {
480
- const reader = new FileReader();
478
+ const Uint8ToString = u8a => {
479
+ if (_.isEmpty(u8a)) return '';
481
480
 
482
- reader.onloadend = () => {
483
- resolve(reader.result);
484
- };
481
+ const maxChunkSize = 8192;
482
+ const chunks = [];
485
483
 
486
- reader.onerror = error => {
487
- console.error('Error reading buffer: ', error);
488
- reject(error);
489
- };
484
+ for (let i = 0; i < u8a.length; i += maxChunkSize) {
485
+ const newChunk = String.fromCharCode.apply(null, u8a.subarray(i, i + maxChunkSize));
486
+ chunks.push(newChunk);
487
+ }
490
488
 
491
- reader.readAsDataURL(blob);
492
- });
493
- }
489
+ return chunks.join('');
490
+ };
494
491
 
495
- return Promise.resolve(url);
496
- } catch (error) {
497
- console.error('Unable to get image from buffer: ', error);
498
- return Promise.reject(error);
492
+ const dataString = btoa(Uint8ToString(ascii));
493
+ const blob = new Blob([ dataString ], { type: 'image/png' });
494
+ const blobUrl = URL.createObjectURL(blob);
495
+
496
+ console.log({ dataString, blob, blobUrl });
497
+ return blobUrl;
499
498
  }
499
+
500
+ const blob = new Blob([ buffer ], { type: 'image/png' });
501
+ const url = URL.createObjectURL(blob);
502
+
503
+ return url;
500
504
  };
501
505
 
502
- export const getTextureUrl = async args => {
503
- const { texture, asBase64Data } = args || {};
506
+ export const getTextureUrl = args => {
507
+ const { texture, asDataUrl } = args || {};
504
508
  const buffer = texture?._buffer;
505
509
 
506
510
  if (_.isEmpty(buffer)) return texture?.url || '';
507
- const url = await getImageFileFromBuffer({ buffer, asBase64Data });
508
- return url;
511
+ return getImageFileFromBuffer({ buffer, asDataUrl });
509
512
  };
510
513
 
511
514
  export const materialData = material => {
@@ -534,30 +537,30 @@ export const materialData = material => {
534
537
  albedoColor: material.albedoColor?.toHexString() || props?.theme?.colors?.[props.$selectedTheme]?.black,
535
538
  albedoTexture: {
536
539
  name: material.albedoTexture?.name,
537
- url: getTextureUrl({ texture: material.albedoTexture, asBase64Data: true }),
540
+ url: getTextureUrl({ texture: material.albedoTexture, asDataUrl: true }),
538
541
  },
539
542
  albedoHasAlpha: material.albedoTexture?.hasAlpha || false,
540
543
  environmentIntensity: material.environmentIntensity,
541
544
  metallic: material.metallic,
542
545
  metallicTexture: {
543
546
  name: material.metallicTexture?.name,
544
- url: getTextureUrl({ texture: material.metallicTexture, asBase64Data: true }),
547
+ url: getTextureUrl({ texture: material.metallicTexture, asDataUrl: true }),
545
548
  },
546
549
  roughness: material.roughness,
547
550
  microSurfaceTexture: {
548
551
  name: material.microSurfaceTexture?.name,
549
- url: getTextureUrl({ texture: material.microSurfaceTexture, asBase64Data: true }),
552
+ url: getTextureUrl({ texture: material.microSurfaceTexture, asDataUrl: true }),
550
553
  },
551
554
  emissiveColor: material.emissiveColor?.toHexString() || props?.theme?.colors?.[props.$selectedTheme]?.black,
552
555
  emissiveIntensity: material.emissiveIntensity,
553
556
  emissiveTexture: {
554
557
  name: material.emissiveTexture?.name,
555
- url: getTextureUrl({ texture: material.emissiveTexture, asBase64Data: true }),
558
+ url: getTextureUrl({ texture: material.emissiveTexture, asDataUrl: true }),
556
559
  },
557
560
  bumpIntensity: material.bumpTexture?.level || 0.5,
558
561
  bumpTexture: {
559
562
  name: material.bumpTexture?.name,
560
- url: getTextureUrl({ texture: material.bumpTexture, asBase64Data: true }),
563
+ url: getTextureUrl({ texture: material.bumpTexture, asDataUrl: true }),
561
564
  },
562
565
 
563
566
  // Advanced
@@ -568,7 +571,7 @@ export const materialData = material => {
568
571
  alpha: material.alpha,
569
572
  opacityTexture: {
570
573
  name: material.opacityTexture?.name,
571
- url: getTextureUrl({ texture: material.opacityTexture, asBase64Data: true }),
574
+ url: getTextureUrl({ texture: material.opacityTexture, asDataUrl: true }),
572
575
  },
573
576
  transparencyMode: material.transparencyMode,
574
577
  sssRefractionEnabled: material.subSurface?.isRefractionEnabled || false,