上一章使用mars3D模型库 遗留一个问题 部分资源不完整
 如果模型没有其他依赖文件会正常加载 若有其他依赖就会报错
正常获取到的
缺少文件的

 经过观察在gltf文件中发现缺失的是这几个文件
 
还是通过脚本下载
脚本实例
const fs = require('fs');
const path = require('path');
const http = require('http');
const https = require('https');
const jsonfile = require('jsonfile');
// 设置文件夹路径
const directoryPath = path.join(__dirname, '你的文件夹路径');
// 处理下载文件的最大重试次数
const maxRetries = 3;
const retryDelay = 5000; // 5 秒延迟
// 下载文件函数
const downloadFile = (url, outputPath, retries = 0) => {
    return new Promise((resolve, reject) => {
        console.log(`Downloading: ${url}`);  // 打印 URL 进行调试
        const protocol = url.startsWith('https') ? https : http;
        http.get(url, (response) => {
            if (response.statusCode === 429 && retries < maxRetries) {
                console.log(`遇到 429 错误,重试 ${retries + 1} 次...`);
                setTimeout(() => {
                    downloadFile(url, outputPath, retries + 1)
                        .then(resolve)
                        .catch(reject);
                }, retryDelay);
                return;
            }
            if (response.statusCode !== 200) {
                return reject(new Error(`下载失败,状态码: ${response.statusCode}`));
            }
            const fileStream = fs.createWriteStream(outputPath);
            response.pipe(fileStream);
            fileStream.on('finish', () => {
                fileStream.close(resolve);
            });
            fileStream.on('error', (err) => {
                fs.unlink(outputPath, () => reject(err));
            });
        }).on('error', (err) => {
            reject(err);
        });
    });
};
// 处理所有下载任务的队列
const downloadQueue = [];
// 递归函数来读取目录
const readDirectoryRecursively = (dir) => {
    fs.readdir(dir, { withFileTypes: true }, (err, entries) => {
        if (err) {
            return console.error('读取目录失败:', err);
        }
        entries.forEach(entry => {
            const fullPath = path.join(dir, entry.name);
            if (entry.isDirectory()) {
                // 递归处理子目录
                readDirectoryRecursively(fullPath);
            } else if (path.extname(entry.name) === '.gltf') {
                // 处理 .gltf 文件
                jsonfile.readFile(fullPath, (err, json) => {
                    if (err) {
                        return console.error('读取文件失败:', err);
                    }
                    const baseUrl = 'http://data.mars3d.cn/gltf/imap/';
                    // 处理 images 属性
                    if (json.images && Array.isArray(json.images)) {
                        json.images.forEach((image) => {
                            if (image.uri) {
                                const imageUrl = new URL(image.uri, baseUrl + path.relative(directoryPath, dir) + '/').href;
                                const imageOutputPath = path.join(dir, image.uri);
                                downloadQueue.push(() => downloadFile(imageUrl, imageOutputPath));
                            }
                        });
                    }
                    // 处理 buffers 属性
                    if (json.buffers && Array.isArray(json.buffers)) {
                        json.buffers.forEach((buffer) => {
                            if (buffer.uri) {
                                const bufferUrl = new URL(buffer.uri, baseUrl + path.relative(directoryPath, dir) + '/').href;
                                const bufferOutputPath = path.join(dir, buffer.uri);
                                downloadQueue.push(() => downloadFile(bufferUrl, bufferOutputPath));
                            }
                        });
                    }
                    // 开始处理下载队列
                    processDownloadQueue();
                });
            }
        });
    });
};
// 处理下载队列的函数
const processDownloadQueue = () => {
    if (downloadQueue.length === 0) {
        console.log('所有文件下载完成');
        return;
    }
    const downloadTask = downloadQueue.shift();
    downloadTask()
        .then(() => {
            console.log('文件下载完成,开始下载下一个文件');
            processDownloadQueue(); // 下载完成后继续下载下一个文件
        })
        .catch((error) => {
            console.error('下载过程中出现错误:', error);
            processDownloadQueue(); // 错误处理后继续下载下一个文件
        });
};
// 启动递归读取
readDirectoryRecursively(directoryPath);
 

 




















