使用three.js显示多次相同的3D对象

display several times the same 3D object with three.js

本文关键字:3D 对象 three js 显示 使用      更新时间:2023-09-26

我使用的是Three.js javascript库。为了测试它,我从这里下载了一个例子。

我试图使用for循环显示几次相同的元素。有两个问题相关(1,2)但这不是我想要的。我的问题是,如果我在循环中创建元素,它将只显示迭代的最后一个元素。在本例中,位置为(12,12)的元素

但是,如果我执行像alert这样的操作,它将显示所有元素。如果我有任何其他函数延迟执行。

我看到一些例子运行,如mrdoob的例子,但我希望这段代码运行,因为我需要加载几个网格,而不是生成原始图形。

// Set up the scene, camera, and renderer as global variables.
var scene, camera, renderer;
var group;
// Call functions
init();
animate();
// Sets up the scene.
function init() {
  // Iterator
  var i, j;
  // Create the scene and set the scene size.
  scene = new THREE.Scene();
  var WIDTH = window.innerWidth,
      HEIGHT = window.innerHeight;
  // Create a renderer and add it to the DOM.
  renderer = new THREE.WebGLRenderer({antialias:true});
  renderer.setSize(WIDTH, HEIGHT);
  document.body.appendChild(renderer.domElement);
  // Create a camera, zoom it out from the model a bit, and add it to the scene.
  camera = new THREE.PerspectiveCamera(45, WIDTH / HEIGHT, 0.1, 20000);
  camera.position.set(0,20,20);
  scene.add(camera);
  // Create an event listener that resizes the renderer with the browser window.
  window.addEventListener('resize', function() {
    var WIDTH = window.innerWidth,
        HEIGHT = window.innerHeight;
    renderer.setSize(WIDTH, HEIGHT);
    camera.aspect = WIDTH / HEIGHT;
    camera.updateProjectionMatrix();
  });
  // Set the background color of the scene.
  renderer.setClearColor(0x333F47, 1);
  // Create a light, set its position, and add it to the scene.
  var light = new THREE.PointLight(0xffffff);
  light.position.set(-100,200,100);
  scene.add(light);

  group = new THREE.Object3D();
  for(i=0; i < 15; i+=3) {
    for(j=0; j < 15; j+=3) {
      var loader = new THREE.JSONLoader();
      loader.load( "models/treehouse_logo.js", function(geometry){
        var material = new THREE.MeshLambertMaterial({color: 0x55B663});
        var mesh = new THREE.Mesh(geometry, material);
        mesh.position.set(i,0,j);
        group.add(mesh);
      });
    //alert("iteration"+i+" "+j);
    }
  }
  scene.add( group );
  // Add OrbitControls so that we can pan around with the mouse.
  controls = new THREE.OrbitControls(camera, renderer.domElement);
}
// Renders the scene and updates the render as needed.
function animate() {
  // Read more about requestAnimationFrame at http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
  requestAnimationFrame(animate);
  // Render the scene.
  renderer.render(scene, camera);
  controls.update();
}

你在这里所做的是令人难以置信的低效:

  for(i=0; i < 15; i+=3) {
    for(j=0; j < 15; j+=3) {
      var loader = new THREE.JSONLoader();
      loader.load( "models/treehouse_logo.js", function(geometry){
        var material = new THREE.MeshLambertMaterial({color: 0x55B663});
        var mesh = new THREE.Mesh(geometry, material);
        mesh.position.set(i,0,j);
        group.add(mesh);
      });
    //alert("iteration"+i+" "+j);
    }
  }

这样做会更好(未测试):

  var loader = new THREE.JSONLoader();
  loader.load( "models/treehouse_logo.js", function( geometry ){
    var material, mesh, i, j, instance;
    material = new THREE.MeshLambertMaterial({ color: 0x55B663 });
    mesh = new THREE.Mesh( geometry, material );
    for ( i = 0; i < 15; i += 3 ) {
      for ( j = 0; j < 15; j += 3 ) {
        instance = mesh.clone();
        instance.position.set( i, 0, j );
        group.add( instance );
      }
    }
  });

你需要为每个独特的网格重复这个模式。

当前方法存在的问题是:

  • 每个相同的网格需要更多的GPU内存
  • 浏览器需要更多内存来记住每个相同的网格
  • GPU需要更多的处理能力,因为需要处理更多的内存
  • 每次调用加载器时,都指示浏览器执行一个请求。你的案子里有25个相同的请求。它们应该来自缓存,但仍然会很慢。
  • 你可能有变量范围问题也给问题的加载器回调,但我不完全确定。
顺便说一下,

alert()是一个非常糟糕的调试工具,因为它改变了浏览器的反应方式:当警报打开时,它停止执行JavaScript,这会影响加载器和类似的东西。您最好使用控制台日志方法。

我会说这是因为你在循环的每次迭代中设置了加载器变量,它将覆盖上次迭代的加载器。

为什么实际的加载是在循环中完成的?为什么不加载一次并克隆它呢?
如。

  group = new THREE.Object3D();
      var loader = new THREE.JSONLoader();
      loader.load( "models/treehouse_logo.js", function(geometry){
        var material = new THREE.MeshLambertMaterial({color: 0x55B663});
        for(i=0; i < 15; i+=3) {
          for(j=0; j < 15; j+=3) {
            var mesh = new THREE.Mesh(geometry.clone(), material);
            mesh.position.set(i,0,j);
            group.add(mesh);
          }
        }
      });
  scene.add( group );

上面的代码是未经测试的