javascript - 带有实例化的 Three.js - 没有 FrustumCulling = false 就无法让它工作

标签 javascript three.js

我正在使用 three.js 和实例化(如 this example ),但我遇到了其他人报告的相同问题:对象被随机剪裁并不断从相机中消失

建议的解决方法是设置

my_instanced_object.frustumCulled = false;

但这意味着每帧渲染每个对象,如果有很多对象,这会扼杀我的帧速率。

我有什么替代方案?如果我使用实例化,我怎样才能进行适当的视锥体剔除?

我正在添加我正在使用的代码以防有人想看

var geometry = new THREE.InstancedBufferGeometry();
geometry.maxInstancedCount = all_meshes_data.length;

geometry.addAttribute( 'position', mesh.geometry.attributes.position );
geometry.addAttribute( 'normal', mesh.geometry.attributes.normal );
geometry.addAttribute( 'uv', mesh.geometry.attributes.uv );

var offsets = new THREE.InstancedBufferAttribute( new Float32Array( all_meshes_data.length * 3 ), 3, 1 );

for ( var i = 0, ul = all_meshes_data.length; i < ul; i++ ) { // Populate all instancing positions (where to spawn instances)
    offsets.setXYZ( i, all_meshes_data[i].x, all_meshes_data[i].y, all_meshes_data[i].z );
}

geometry.addAttribute( 'offset', offsets );

var instanceMaterial = new THREE.RawShaderMaterial( {
    vertexShader: document.getElementById( 'vertexShader' ).textContent,
    fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
    transparent: true
} );

geometry.computeVertexNormals();
geometry.boundingSphere = new THREE.Sphere( new THREE.Vector3(), 50 ); // Not working, it works just for a 0;0;0 world positioned mesh that is the 'base' of all of the instanced ones

var instanced_mesh = new THREE.Mesh( geometry, instanceMaterial );

//instanced_mesh.frustumCulled = false; // Works, but the scene becomes very slow (rendering everything even if not in sight)

scene.add( instanced_mesh );

最佳答案

如果您使用实例化,有两种方法可以处理视锥体剔除。

一种是关闭对象的截锥体剔除:

object.frustumCulled = false;

另一种选择是手动设置几何体的边界球——如果您知道或可以估计的话:

geometry.boundingSphere = new THREE.Sphere( new THREE.Vector3(), radius );

three.js r.86

关于javascript - 带有实例化的 Three.js - 没有 FrustumCulling = false 就无法让它工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45378920/

相关文章:

android - 如何将 Three.js 代码实现到 Android 移动应用程序中?

javascript - 使用 javascript 显示 CAD 图像

javascript - AngularJS 中页面加载的 Switch 语句

javascript - 我只需要在 Google Charts 表格上显示两行

javascript - 在 <div> 中使用 JavaScript 添加新行

javascript - 如何计算哪个顶点最接近 3D 点?

javascript - 在我的输入文本区域评论中想要 3 行而不是一行

javascript - 如何找到图像元素的xpath

Three.js SceneUtils 没有 traverseHierarchy!!!它在哪里?

three.js - 使用Three.js翻转(镜像)任何对象