【发布时间】:2020-08-28 15:43:53
【问题描述】:
我正在将大约 10MB 的 GLTF 模型加载到 React 中的 three.js 场景中。当用户浏览网站时,他们点击了 3D 模型“场景”,我的 GLTF 模型被加载。当他们完成这个场景时,我会在卸载 react 组件之前进行一些 threejs 清理工作。但是,如果用户重新访问该场景 - 它会再次经历整个加载过程。
有没有办法以某种方式将加载的对象保存在浏览器缓存中,以便即使在卸载 react 组件之后,加载的模型数据也可供后续访问 / 组件安装(即使它是仅在同一浏览器会话中,刷新之前等)?
这是模型加载代码:
loadGLB = loader => {
if (sceneConfig.modelFormat === 'glb') {
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath('three/examples/js/libs/draco/');
loader.setDRACOLoader(dracoLoader);
}
};
const loader = new GLTFLoader().setPath(models.modelPath);
this.loadGLB(loader);
loader.load(`${models.modelToLoad}.${sceneConfig.modelFormat}`, glb => {
this.object = glb.scene;
...
});
和清理:
componentWillUnmount() {
cancelAnimationFrame(this.frameId);
window.removeEventListener('resize', this.handleWindowResize);
this.container.removeChild(this.renderer.domElement);
this.renderer.forceContextLoss();
}
【问题讨论】:
标签: reactjs caching three.js 3d browser-cache