140 async
downloadMaterialAsset(assetId, imageFormat =
'PNG', imageResolution =
'1', downloadAttributeKey =
'downloadAttribute', downloadLinkKey =
'downloadLink') {
155 let downloadAttribute =
'';
157 items.forEach(item => {
158 downloadAttribute = item[downloadAttributeKey];
159 if (downloadAttribute === target) {
160 url = item[downloadLinkKey];
161 this.logger.info(`Found Asset: ${assetId}. Download Attribute: ${downloadAttribute} -> ${url}`);
166 this.logger.error(`No download link found
for asset: ${assetId}, attribute: ${target}`);
170 this.downloadMaterialFileName = url.split(
'file=')[1];
171 console.log(
'>>>> URL:', url,
'Filename:', this.downloadMaterialFileName);
174 const response = await fetch(url);
176 throw new Error(`HTTP error! Status: ${response.status}`);
180 const arrayBuffer = await response.arrayBuffer();
183 this.downloadMaterial = Buffer.from(arrayBuffer);
184 this.logger.info(`Material file downloaded: ${this.downloadMaterialFileName}`);
187 this.downloadMaterialFileName =
'';
188 this.logger.error(`Error occurred
while downloading the file: ${error}`);
191 return this.downloadMaterialFileName;
223 const MATERIALS_CACHE_FILE =
'ambientcg_materials.json';
225 if (fs.existsSync(MATERIALS_CACHE_FILE)) {
227 const data = fs.readFileSync(MATERIALS_CACHE_FILE,
'utf8');
228 this.materials = JSON.parse(data);
229 this.logger.info(`Loaded AmbientCG materials from cache: ${MATERIALS_CACHE_FILE}`);
230 return this.materials;
232 this.logger.warn(`Failed to load AmbientCG materials cache: ${e.message}`);
237 const headers = { Accept:
'application/csv' };
238 const url =
new URL(
'https://ambientCG.com/api/v2/downloads_csv');
239 url.searchParams.append(
'method',
'PBRPhotogrammetry');
240 url.searchParams.append(
'type',
'Material');
241 url.searchParams.append(
'sort',
'Alphabet');
243 this.logger.info(
'Downloading materials CSV list from network...');
245 const response = await fetch(url, { headers });
246 if (response.status === 200) {
247 const csvContent = await response.text();
248 this.csvMaterials = csvContent;
249 this.materials = parse(csvContent, { columns:
true });
250 this.logger.info(
'Downloaded CSV material list as JSON.');
253 fs.writeFileSync(MATERIALS_CACHE_FILE, JSON.stringify(
this.materials,
null, 2));
254 this.logger.info(`Saved AmbientCG materials to cache: ${MATERIALS_CACHE_FILE}`);
256 this.logger.warn(`Failed to write AmbientCG materials cache: ${e.message}`);
259 this.materials =
null;
260 this.logger.warning(`Failed to fetch the CSV material content. HTTP status code: ${response.status}`);
263 this.materials =
null;
264 this.logger.error(`Error downloading materials list: ${error}`);
267 return this.materials;
294 const url =
'https://ambientcg.com/api/v2/full_json';
295 const headers = { Accept:
'application/json' };
297 method:
'PBRPhotogrammetry',
303 const response = await axios.get(url, { headers, params });
304 if (response.status === 200) {
305 this.database = response.data;
306 this.assets = this.database.foundAssets;
308 this.logger.error(`Status: ${response.status}, ${response.data}`);
311 this.logger.error(`Error downloading asset database: ${error}`);
314 return this.database;