如何在 THREE.JS 中预初始化纹理

How to pre initialize textures in THREE.JS

我在开始渲染和设置对象动画之前预加载和创建材质纹理。
但是只有在相机中显示对象时,三个 JS 才会将纹理上传到 GPU。
因此,当新对象出现在屏幕上时,由于纹理在 GPU 上发送,动画会出现抖动。 问题是如何在创建纹理期间将纹理发送到 GPU 以避免在运行时发生? 将图像加载到 GPU 需要很多时间。

我的猜测是遍历您的对象树,将每个对象的 frustumCulled 标志设置为 false,调用 renderer.render(scene, ...) 一次,然后将标志恢复为 true(或其他)。

function setAllCulled(obj, culled) {
  obj.frustumCulled = culled;
  obj.children.forEach(child => setAllCulled(child, culled));
}

setAllCulled(scene, false);
renderer.render(scene, camera);
setAllCulled(scene, true);

您也可以调用renderer.setTexture2D(texture, 0)来强制初始化纹理。