1 line
22 KiB
Plaintext
1 line
22 KiB
Plaintext
|
|
{"version":3,"file":"USDZExporter.cjs","sources":["../../src/exporters/USDZExporter.ts"],"sourcesContent":["import { zipSync, strToU8, Zippable } from 'fflate'\nimport {\n BufferGeometry,\n Color,\n Matrix4,\n Mesh,\n MeshPhysicalMaterial,\n MeshStandardMaterial,\n Object3D,\n Texture,\n Vector2,\n} from 'three'\nimport { Nullable } from '../types/utils'\n\ntype MaterialRepresentaion = MeshStandardMaterial | MeshPhysicalMaterial\n\nclass USDZExporter {\n private readonly PRECISION = 7\n\n private materials: { [key: string]: MaterialRepresentaion }\n private textures: { [key: string]: Texture }\n\n private files: Nullable<Zippable>\n\n constructor() {\n this.materials = {}\n this.textures = {}\n\n this.files = {}\n }\n\n public async parse(scene: Object3D): Promise<Uint8Array> {\n const modelFileName = 'model.usda'\n\n // model file should be first in USDZ archive so we init it here\n this.files[modelFileName] = null\n\n let output: string | null = this.buildHeader()\n\n scene.traverseVisible((object) => {\n if (object instanceof Mesh && object.isMesh && object.material.isMeshStandardMaterial) {\n const geometry: BufferGeometry = object.geometry\n const material: MaterialRepresentaion = object.material\n\n const geometryFileName = 'geometries/Geometry_' + geometry.id + '.usd'\n\n if (!(geometryFileName in this.files)) {\n const meshObject = this.buildMeshObject(geometry)\n this.files[geometryFileName] = this.buildUSDFileAsString(meshObject)\n }\n\n if (!(material.uuid in this.materials)) {\n this.materials[material.uuid] = material\n }\n\n output += this.buildXform(object, geometry, material)\n }\n })\n\n output += this.buildMaterials(this.materials)\n\n this.files[modelFileName] = strToU8(output)\n output = null\n\n for (const id in this.textures) {\n const texture = this.textures[id]\n const color = id.split('_')[1]\n const isRGBA = texture.format === 1023\n\n const canvas = this.imageToCanvas(texture.image, color)\n const blob = await new Promise<Blob | null>((resolve) =>\n canvas?.toBlob(resolve, isRGBA ? 'image/png' : 'image/jpeg', 1),\n )\n\n if (blob) {\n this.files[`textures/Texture_${id}.${isRGBA ? 'png' : 'jpg'}`] = new Uint8Array(await blob.arrayBuffer())\n }\n }\n\n // 64 byte alignment\n // https://github.com/101arrowz/fflate/issues/39#issuecomment-777263109\n\n let offset = 0\n\n for (const filename in this.files) {\n const file = this.files[filename]\n const headerSize = 34 + filename.length\n\n offset += headerSize\n\n const offsetMod64 = offset & 63\n\n if (offsetMod64 !== 4 && file !== null && file instanceof Uint8Array) {\n const padLength = 64 - offsetMod64\n const padding = new Uint8Array(padLength)\n\n this.files[filename] = [file, { extra: { 12345: padding } }]\n }\n\n if (file && typeof file.length === 'number') {\n offset = file.length\n }\n }\n\n return zipSync(this.files as Zippable, { level: 0 })\n }\n\n private imageToCanvas(\n image: HTMLImageElement | HTMLCanvasElement | OffscreenCanvas | ImageBitmap,\n color: string,\n ): HTMLCanvasElement | undefined {\n if (\n (typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement) ||\n (typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement) ||\n (typeof OffscreenCanvas !== 'undefined' && image instanceof OffscreenCanvas) ||\n (typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap)\n ) {\n const scale = 1024 / Math.max(image.width, image.height)\n\n const canvas = document.createElement('canvas')\n canvas.width = image.width * Math.min(1, scale)\n canvas.height = image.height * Math.min(1, scale)\n\n const context = canvas.getContext('2d')\n context?.drawImage(image, 0, 0, canvas.width, canvas.height)\n\n if (color !== undefined) {\n
|