如何优化 Three.js 中多个 sphereGeometry 的渲染?

2024-05-24

我想优化 Three.js 中 sphereGeometry 的渲染,因为它成为我的程序的瓶颈。 javascript程序如下所示:

var sphereThree = [];
for(var idSphere = 0; idSphere < numSphere; idSphere++){
    var sphereGeometry = new THREE.SphereGeometry(1.0);
    var sphereMaterial = new THREE.MeshLambertMaterial({color: 'rgb(255, 0, 0)'});

    sphereThree[idSphere] = new THREE.Mesh(sphereGeometry, sphereMaterial);

    sphereThree[idSphere].position.x = x[idSphere];
    sphereThree[idSphere].position.y = y[idSphere];
    sphereThree[idSphere].position.z = z[idSphere];

    scene.add(sphereThree[idSphere]);
}

正如以下链接中提到的:
- 使用 Three.js 为一百万个字母制作动画 http://www.html5rocks.com/en/tutorials/webgl/million_letters/
- 优化 Three.js 性能:模拟数以万计的独立移动物体 http://www.ianww.com/blog/2012/11/04/optimizing-three-dot-js-performance-simulating-tens-of-thousands-of-independent-moving-objects/
他们指出,我们不应该单独添加对象,最好同时添加同类对象,以进行优化。然而,由于我是这个领域的新手,所以我不知道在使用 SphereGeometry 时如何编写此优化代码。

如果有人知道如何使用 SphereGeometry 进行编码,或者有人知道渲染许多(10,000 个或更多)球体的有效方法,您会教我们吗?

Thanks!


您可以像这样重复使用几何图形和材质:

var sphereGeometry = new THREE.SphereGeometry(1.0);
var sphereMaterial = new THREE.MeshLambertMaterial({color: 'rgb(255, 0, 0)'});

var sphereThree = [];
for(var idSphere = 0; idSphere < numSphere; idSphere++){
    sphereThree[idSphere] = new THREE.Mesh(sphereGeometry, sphereMaterial);

    sphereThree[idSphere].position.x = x[idSphere];
    sphereThree[idSphere].position.y = y[idSphere];
    sphereThree[idSphere].position.z = z[idSphere];

    scene.add(sphereThree[idSphere]);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何优化 Three.js 中多个 sphereGeometry 的渲染? 的相关文章

随机推荐