gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:33:28 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com