当前位置:网站首页>Threejs uses indexeddb cache to load GLB model
Threejs uses indexeddb cache to load GLB model
2022-06-11 17:33:00 【Karaoke, king of】
1、 Load tool class indexeddb.js
import axios from "axios";
const DB_NAME = 'railway-indexeddb-epublications_5';
const DB_VERSION = 4; // Use a long long for this value (don't use a float) const DB_STORE_NAME = 'model_glb'; // Load database function initDb() { if (!window.indexedDB) { console.log("Your browser doesn't support a stable version of IndexedDB.") return; } let request = indexedDB.open(DB_NAME, DB_VERSION); return new Promise((resolve, reject) => { request.onerror = function () { console.log("error: create db error"); reject() }; request.onupgradeneeded = function (evt) { evt.currentTarget.result.createObjectStore( DB_STORE_NAME, {keyPath: 'ssn'}); }; request.onsuccess = function (evt) { console.log("onsuccess: create db success "); resolve(evt.target.result) }; }) } // Get the model async function getModel(url, id) { let db = await initDb(); let getRequest = db .transaction([DB_STORE_NAME], "readwrite").objectStore(DB_STORE_NAME).get(id); return new Promise((resolve, reject) => { getRequest.onsuccess = function (event) { let modelFile = event.target.result; // If you already have a cache Use cache directly if (modelFile) { resolve(modelFile.blob) } else { // If there is no cache Request new model deposit inputModel(url, id).then((blob) => { resolve(blob) }).catch(() => { reject() }); } }; getRequest.onerror = function (event) { console.log('error', event) reject() } }) } // Deposit model async function inputModel(url, id) { let db = await initDb(); let modelResult = await axios.get(url, { responseType: 'blob', onDownloadProgress: (e) => { if (id == 1) { window.dispatchEvent(new CustomEvent('loadingProgress', { detail: (e.loaded * 100 / e.total).toFixed(2) })) } } }); if (modelResult.status == 200) { let obj = { ssn: id } obj.blob = new Blob([modelResult.data]) let inputRequest = db .transaction([DB_STORE_NAME], "readwrite") .objectStore(DB_STORE_NAME) .add(obj); return new Promise((resolve, reject) => { inputRequest.onsuccess = function () { console.log("glb Data added successfully "); resolve(obj.blob) }; inputRequest.onerror = function (evt) { console.log("glb Data addition failed ", evt);
reject()
};
})
}
}
export {
getModel,
inputModel
}


2、 Load model
let url = URL.createObjectURL(new Blob([blob])) utilize URL.createObjectURL hold blob Turn into load Loadable url
import * as THREE from 'three/build/three.module.js';
import {
GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
import {
getModel} from "./indexeddb";
let model = new THREE.Group();
getModel('/droc_model/changsharelease.glb',1).then((blob) => { console.log('getModel To be successful ', blob); let loader = new GLTFLoader(); let url = URL.createObjectURL(new Blob([blob]));
loader.load(url, function (gltf) {
model.add(gltf.scene);
}, function ( xhr ) {
console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
})
})
export {
model
}
边栏推荐
- tidb-ddl的速度的调整
- tidb-cdc创建任务报错 Unknown or incorrect time zone
- Connect the server with springboard / fortress through xshell
- require和ES6 import的区别
- TiDB-unsafe recover(tikv宕机数大于等于一半副本数)
- vscode配置eslint自动格式化报错“The setting is deprecated. Use editor.codeActionsOnSave instead with a source“
- 04_ Feature engineering feature selection
- Derivation of child numbering formula for nodes numbered I in full k-ary tree
- 05_特征工程—降维
- Kubernetes deploys elk and collects container logs using filebeat
猜你喜欢

Vscode configures eslint to automatically format an error "auto fix is enabled by default. use the single string form“

Authing 双周动态:Authing 论坛上线(4.25-5.8)

Axi protocol Basics

What problems are exposed when all Sohu employees are cheated?

From manufacturing to "intelligent manufacturing", explore the way for manufacturing enterprises to break the situation

vscode配置eslint自动格式化报错“Auto Fix is enabled by default. Use the single string form“

Semaphore PV operation of process interaction and its code implementation

子类继承了什么、多态、 向上转型

ffmpeg硬编解码 Inter QSV

Use of forcescan in SQL server and precautions
随机推荐
tidb-cdc日志tables are not eligible to replicate
6-3 批量求和(*)
Vscode configures eslint to automatically format with an error "the setting is deprecated. use editor.codeactionsonsave instead with a source“
Docker安装mysql5.7(开启binlog功能、修改字符)
R语言 mice包 Error in terms.formula(tmp, simplify = TRUE) : ExtractVars里的模型公式不对
Read and understand the development plan for software and information technology service industry during the "14th five year plan"
Derivation of child numbering formula for nodes numbered I in full k-ary tree
Use exe4j to convert The jar file is packaged as Exe file
6-6 批量求和(*)
Cs0006 C failed to find metadata file "c:\users\... Problem
ffmpeg奇偶场帧Interlace progressive命令和代码处理
vscode保存代码时自动eslint格式化
通过Xshell连接有跳板机/堡垒机的服务器
Qlineedit set input mask
Authing biweekly news: online application market (5.10-5.22)
信息安全数学基础 Chapter 2——同余
开源项目那么多,这次带你了解个版本的区别,明白alpha版、beta版、rc版是什么意思
How does Sister Feng change to ice?
QLineEdit 设置输入掩码
你还不懂线程池的设计及原理吗?掰开揉碎了教你设计线程池