MaterialXMaterials 0.0.1
Utilities for retrieving materials from remote servers
Loading...
Searching...
No Matches
JsGPUOpenLoader.js
1
13 if (JsGPUOpenMaterialLoader.instance) {
14 return JsGPUOpenMaterialLoader.instance;
15 }
16
17 this.rootUrl = 'https://api.matlib.gpuopen.com/api';
18 this.url = `${this.rootUrl}/materials`;
19 this.packageUrl = `${this.rootUrl}/packages`;
20 this.materials = null;
21 this.materialNames = null;
22
23 this.logger = console;
24
25 // Cache the instance
26 JsGPUOpenMaterialLoader.instance = this;
27 }
28
34 return this.materials;
35 }
36
42 return this.materialNames;
43 }
44
50 async getMaterials(batchSize = 50) {
51
52 /*
53 * Get the materials returned from the GPUOpen material database.
54 * Will loop based on the linked-list of materials stored in the database.
55 * Currently, the batch size requested is 100 materials per batch.
56 * @param batchSize: Number of materials to fetch per batch
57 * @return: List of material lists
58 */
59
60 this.materials = [];
61 this.materialNames = [];
62
63 let url = this.url;
64
65 // Get batches of materials. Start with the first N.
66 let params = new URLSearchParams({
67 limit: batchSize,
68 offset: 0
69 });
70
71 // Append & parameters to url using params
72 if (params) {
73 url += '?' + params.toString();
74 }
75
76 let haveMoreMaterials = true;
77 while (haveMoreMaterials)
78 {
79 try {
80
81 console.log('Fetch materials from url:', url)
82
83 const response = await fetch(url);
84
85 if (!response.ok) {
86 throw new Error(`HTTP error! status: ${response.status}`);
87 }
88
89 const jsonData = await response.json();
90 this.materials.push(jsonData)
91
92 for (const material of jsonData.results) {
93 this.materialNames.push(material['title']);
94 }
95
96 //console.log("Number of materials fetched:", jsonData.results.length)
97
98 let nextURL = jsonData.next
99 if (nextURL) {
100 //console.log('Next URL: ', jsonData.next)
101 url = nextURL;
102 haveMoreMaterials = true
103 }
104 else
105 {
106 console.log('Finished fetching materials')
107 haveMoreMaterials = false;
108 break;
109 }
110
111 } catch (error) {
112 this.logger.info(`Error: ${error.message}`);
113 haveMoreMaterials = true;
114 }
115 }
116
117 return this.materials;
118 }
119
127 async downloadPackage(listNumber, materialNumber, packageId = 0) {
128 if (this.materials === null || this.materials.length === 0) {
129 return [null, null];
130 }
131
132 const jsonData = this.materials[listNumber];
133 if (!jsonData) {
134 return [null, null];
135 }
136
137 let jsonResults = null;
138 let jsonResult = null;
139 if ('results' in jsonData) {
140 jsonResults = jsonData['results'];
141 if (jsonResults.length <= materialNumber) {
142 return [null, null];
143 } else {
144 jsonResult = jsonResults[materialNumber];
145 }
146 }
147
148 if (!jsonResult) {
149 return [null, null];
150 }
151
152 let jsonPackages = null;
153 if ('packages' in jsonResult) {
154 jsonPackages = jsonResult['packages'];
155 }
156 if (!jsonPackages) {
157 return [null, null];
158 }
159
160 if (jsonPackages.length <= packageId) {
161 return [null, null];
162 }
163 const packageIdValue = jsonPackages[packageId];
164
165 if (!packageIdValue) {
166 return [null, null];
167 }
168
169 const url = `${this.packageUrl}/${packageIdValue}/download`;
170 const response = await fetch(url);
171 const data = await response.arrayBuffer();
172 const title = jsonResult['title'];
173
174 console.log(`- Loader: Downloaded package: ${title} from ${url}`);
175 return [data, title];
176 }
177
184 findMaterialsByName(materialName)
185 {
186 if (!this.materials) {
187 return [];
188 }
189
190 const materialsList = [];
191 let listNumber = 0;
192
193 // Create a RegExp object for case-insensitive matching
194 const regex = new RegExp(materialName, 'i');
195
196 this.materials.forEach((materialList, materialIndex) => {
197 let materialNumber = 0;
198
199 materialList['results'].forEach((material) => {
200
201 //console.log('testing material:', material['title'], ' with regex:', regex)
202 if (regex.test(material['title'])) {
203 materialsList.push({
204 listNumber: listNumber,
205 materialNumber: materialNumber,
206 title: material['title'],
207 });
208 }
209 materialNumber += 1;
210 });
211
212 listNumber += 1;
213 });
214
215 return materialsList;
216 }
217
226 async downloadPackageByExpression(searchExpr, packageId = 0) {
227 const downloadList = [];
228
229 const foundList = this.findMaterialsByName(searchExpr);
230 if (foundList.length > 0) {
231 for (const found of foundList) {
232 const listNumber = found['listNumber'];
233 const materialNumber = found['materialNumber'];
234 const matName = found['title'];
235 this.logger.info(`> Download material: ${matName} List: ${listNumber}. Index: ${materialNumber}`);
236 const [data, title] = await this.downloadPackage(listNumber, materialNumber, packageId);
237 downloadList.push([data, title]);
238 }
239 }
240 return downloadList;
241 }
242}
243
244// Export a singleton instance of the class
245module.exports = new JsGPUOpenMaterialLoader();
246
Class to download MaterialX materials from the GPUOpen material database.
async getMaterials(batchSize=50)
Get lists of materials from the GPUOpen material database.
async downloadPackage(listNumber, materialNumber, packageId=0)
Download a material package from the GPUOpen material database.
findMaterialsByName(materialName)
Find materials by name.
getMaterialList()
Return downloaded material list.
getMaterialNames()
Return downloaded material names.
constructor()
Constructor for the JsGPUOpenMaterialLoader class.
async downloadPackageByExpression(searchExpr, packageId=0)
Download a material package by string expression.