当前位置:网站首页>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
}
边栏推荐
- 自动化测试-Selenium
- Dynamic: capturing network dynamics using dynamic graph representation learning
- Authing biweekly news: authing forum launched (4.25-5.8)
- What problems are exposed when all Sohu employees are cheated?
- 拜登下令强制推行零信任架构
- Mathematical basis of information security Chapter 1 - Division
- What is the minimum change price of PTA futures? How can PTA futures be safe?
- Read and understand the development plan for software and information technology service industry during the "14th five year plan"
- 6-8 有结构文件的读写1
- vscode保存代碼時自動eslint格式化
猜你喜欢

活动 | Authing 首次渠道合作活动圆满落幕

为什么udp流设置1316字节

Axi protocol Basics

Bentley uses authing to quickly integrate application system and identity

10 times faster than 5g. Are you ready for 10 Gigabit communication?

ffmpeg CBR精准码流控制三个步骤

删除链表的倒数第N个节点---2022/02/22

Qlineedit set input mask

vscode保存代码时自动eslint格式化
![[MySQL] detailed explanation of redo log, undo log and binlog (4)](/img/67/6e646040c1b941c270b3efff74e94d.png)
[MySQL] detailed explanation of redo log, undo log and binlog (4)
随机推荐
Vscode configures eslint to automatically format an error "auto fix is enabled by default. use the single string form“
tidb-cdc日志tables are not eligible to replicate
Automated testing selenium
6-3 读文章(*)
Authing biweekly news: authing forum launched (4.25-5.8)
信息安全数学基础 Chapter 3——有限域(二)
你还不懂线程池的设计及原理吗?掰开揉碎了教你设计线程池
tidb-lightning配置数据还原路由
JPA循环保存多个实体失败
05_特征工程—降维
Derivation of child numbering formula for nodes numbered I in full k-ary tree
Talk about the interview questions of the collection
【题解】Codeforces Round #798 (Div. 2)
av_read_frame返回值为-5 Input/output error
6-2 多个整数的逆序输出-递归
Authing biweekly news: online application market (5.10-5.22)
【Mysql】redo log,undo log 和binlog详解(四)
Vscode automatic eslint formatting when saving code
Docker installs mysql5.7 (enable binlog function and modify characters)
Bentley 使用 Authing 快速实现应用系统与身份的集成