简体   繁体   English

规范化three.js中的网格

[英]normalizing mesh in three.js

I'm loading a mesh from an obj file, then trying to normalize it. 我正在从obj文件加载一个网格,然后尝试将其标准化。 However, I'm getting strange results. 但是,我得到了奇怪的结果。 Here is the code for loading and centering the mesh: 以下是加载和居中网格的代码:

var manager = new THREE.LoadingManager();
var loader = new THREE.OBJLoader(manager);
loader.load('http://jamesdedge.com/threejs/bunny.obj', function(object) {
  object.traverse(function(child) {
    if(child instanceof THREE.Mesh)
    {
      var geometry = child.geometry;
      var verts = geometry.vertices;
      var ctr = new THREE.Vector3(0.0, 0.0, 0.0);


      for(i = 0; i < verts.length; ++i)
        ctr.add(verts[i]);

      ctr.divideScalar(verts.length);

      for(i = 0; i < verts.length; ++i)
        verts[i].sub(ctr);
    }
  });

  scene.add(object);
});

This code should just center the mesh on the average of the vertex positions, but it seems to be causing a strange effect. 此代码应该将网格中心放在顶点位置的平均值上,但它似乎会导致奇怪的效果。 You can see it on my website here: http://jamesdedge.com/threejs/tjs_demo.html 你可以在我的网站上看到它: http//jamesdedge.com/threejs/tjs_demo.html

I don't see what is causing this, the ctr variable is giving me a valid vector and subtracting a vector from all the vertices will only reposition it. 我没有看到导致这种情况的原因,ctr变量给我一个有效的向量,从所有顶点减去一个向量只会重新定位它。

Many of the vertices in this model are duplicates (ie the same javascript object is contained in the object multiple times), so ctr gets detracted from those vertices multiple times. 此模型中的许多顶点都是重复的(即,对象中多次包含相同的javascript对象),因此ctr会多次从这些顶点中删除。

One way to address this is issue is to merge the vertices at the start of your traversal function, ie 解决这个问题的一种方法是在遍历函数的开头合并顶点,即

var geometry = child.geometry;
geometry.mergeVertices();

This will also make your code run a bit faster as it has to process a lot less vertices. 这也将使您的代码运行得更快,因为它必须处理更少的顶点。

You can use GeometryUtils.center() to center your whole mesh conveniently in the middle of the scene at (0,0,0). 您可以使用GeometryUtils.center()将整个网格方便地置于场景中间的(0,0,0)。 Anyway, just look at the source code of that function https://github.com/mrdoob/three.js/blob/master/src/extras/GeometryUtils.js This should give you a good idea on how to conquer your problem. 无论如何,只要看看该函数的源代码https://github.com/mrdoob/three.js/blob/master/src/extras/GeometryUtils.js这应该会让你对如何克服你的问题有一个很好的了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM