three.js - 合并的几何体不会渲染更新

时间:2016-04-10 05:56:07

标签: javascript three.js

我在已添加到场景中的Mesh中有一个合并的几何体。我更新网格中的合并几何体并将其添加到场景中。这是一个间隔。

scene.remove(that.graph[i]);
that.graph[i].geometry.dispose();

        var planeMaterial = new THREE.MeshLambertMaterial({
          color: 0xffffff,
          vertexColors: THREE.VertexColors
        });
        var boxGeometry = new THREE.BoxGeometry(50, 50, Math.random() * 10000, 1, 1);
      var cube = new THREE.Mesh(boxGeometry, planeMaterial);

      cube.material.color = new THREE.Color(1,0,0);

      cube.updateMatrix();
      that.graph[i].geometry.dynamic = true;
      that.graph[i].geometry.merge(cube.geometry, cube.matrix);
      that.graph[i].geometry.mergeVertices();
      that.graph[i].geometry.verticesNeedUpdate = true;
      that.graph[i].geometry.elementsNeedUpdate = true;
      that.graph[i].geometry.morphTargetsNeedUpdate = true;
      that.graph[i].geometry.uvsNeedUpdate = true;
      that.graph[i].geometry.normalsNeedUpdate = true;
      that.graph[i].geometry.colorsNeedUpdate = true;
      that.graph[i].geometry.tangentsNeedUpdate = true;
      that.graph[i].geometry.groupsNeedUpdate = true;

      scene.add(that.graph[i]);

我知道大多数更新标志都不需要,但我想表明我添加了所有内容。

我确定几何体已更新,但在初始渲染后它不会渲染。

到目前为止,唯一可以让它渲染的方法是在网格添加到场景之前添加以下内容。

that.graph[i].geometry = that.graph[i].geometry.clone();

但这会导致标签在大约5次迭代后崩溃。

为什么我的网格没有使用新几何体进行更新?

1 个答案:

答案 0 :(得分:0)

据我了解geometry.dispose(),它会从内存中删除几何对象,因此像merge()这样的方法将不再起作用。尽量不要合并新几何体,而是将其分配给that.graph[i].geometry

that.graph[i].geometry.dispose();

that.graph[i].geometry = new THREE.BoxGeometry(50, 50, Math.random() * 10000, 1, 1);

// I don't see, where you are setting up a matrix, and there is no need to create a mesh anymore
// but if you still need to apply a matrix do
that.graph[i].geometry.applyMatrix(matrix);

// if you want to change the material, e.g. you could do
that.graph[i].material.color.setRGB(1,0,0);

顺便说一句:我认为,scene.remove()scene.add()是不必要的。

我希望,它有效:)