MaterialXLab API  0.0.1
APIs For MaterialXLab Libraries
Loading...
Searching...
No Matches
JsMaterialXZipUtils.js
Go to the documentation of this file.
1// JsMaterialXZipUtils.js
2//
3
9class MxZipUtils {
10
16 // Check if fflate is available
17 if (typeof fflate === 'undefined') {
18 console.error("fflate is not available. Please include fflate in your HTML file.");
19 }
20 }
21
27 static async unzipMaterialXData(zipData) {
28 let documents = [];
29 let textures = [];
30
31 // Unzip the file using fflate
32 fflate.unzip(zipData, (err, files) => {
33 if (err) {
34 console.error("Error unzipping file:", err);
35 return;
36 }
37
38 // Iterate through files in the ZIP
39 for (let fileName in files) {
40 const fileType = fileName.split('.').pop().toLowerCase();
41
42 if (['mtlx'].includes(fileType)) {
43
44 // Handle text files using TextDecoder
45 const fileContent = new TextDecoder("utf-8").decode(files[fileName]);
46 //console.log(`File: ${fileName} (${fileContent.length} )`);
47 documents.push({ name: fileName, content: fileContent });
48
49 } else {
50 // Get mime type based on extension
51 const standardMimeTypes = {
52 'png': 'image/png',
53 'jpg': 'image/jpeg',
54 'jpeg': 'image/jpeg',
55 'gif': 'image/gif',
56 'bmp': 'image/bmp',
57 'svg': 'image/svg+xml',
58 'webp': 'image/webp',
59 'exr': 'image/exr',
60 'hdr': 'image/vnd.radiance'
61 };
62
63 let mimeType = 'image/' + fileType;
64 if (fileType in standardMimeTypes) {
65 mimeType = standardMimeTypes[fileType];
66
67 const blob = new Blob([files[fileName]], { type: mimeType });
68 const url = URL.createObjectURL(blob);
69
70 textures.push({ name: fileName, url: url });
71 //console.log(`Texture: ${fileName} (${url})`);
72
73 }
74 else {
75 // Check if it's a folder
76 if (files[fileName].length === 0) {
77 console.log(`Folder: ${fileName}`);
78 } else {
79 console.log(`Unsupported file type: ${fileName}`);
80 }
81 }
82 }
83 }
84 });
85
86 return [documents, textures];
87 }
88
95 static async unzipMaterialXUrl(zipUrl) {
96 const response = await fetch(zipUrl);
97 const data = await response.arrayBuffer();
98 const zipData = new Uint8Array(data); // Convert to Uint8Array
99 const result = await MxZipUtils.unzipMaterialXData(zipData);
100 return result;
101 }
102}
Utility class for unzipping ZIP which contain MaterialX files and dependent images .
constructor()
Constructor.
static async unzipMaterialXUrl(zipUrl)
Fetch the ZIP file on initial load.
static async unzipMaterialXData(zipData)
Unzips the given ZIP data and returns the MaterialX documents and textures.