三.js - 使用 mergeVertices() 平滑法线; [英] three.js - Smoothing normals using mergeVertices();
问题描述
我正在尝试平滑使用 THREE.OBJLoader 加载的网格
I'm trying to smoothen a mesh I loaded using THREE.OBJLoader
正如您在此原始图像中看到的,所有多边形都是多面的.我试过其他加载器/格式,但得到相同的结果.在扎根之后,我发现一个可能的解决方案是在计算法线之前合并顶点.当我尝试这个时,我在控制台中得到 TypeError: geometry.mergeVertices is not a function".这是代码,突出显示了我插入 mergeVertices() 函数的位置.
As you can see in this original image all polygons are faceted. I've tried other loaders/formats but get the same result. After rooting around I see that a possible solution is to merge the vertices before computing the normals. When I try this i get "TypeError: geometry.mergeVertices is not a function" in my console. Here is the code, highlighting where I've inserted the mergeVertices() function.
var loader = new THREE.OBJLoader();
loader.load('../assets/models/nos2.obj', function (nos) {
var material = new THREE.MeshLambertMaterial({color: 0xffffff, side:THREE.DoubleSide});
nos.children.forEach(function (child) {
child.material = material;
child.geometry.mergeVertices(); /* ADDED MERGE WHICH GIVES ERROR */
child.geometry.computeFaceNormals();
child.geometry.computeVertexNormals();
});
nos.scale.set(300, 300, 300);
nos.rotation.x = -0.3;
scene.add(nos);}
我做错了什么?
推荐答案
转换为常规几何图形,应用您需要的任何内容并转换回缓冲区几何图形.我正在使用 ES6 模块加载器,所以没有三个.前缀
Convert to regular geometry, apply whatever you need and convert back to buffer geometry. I'm using ES6 module loaders so no THREE. prefix
const tempGeo = new Geometry().fromBufferGeometry(child.geometry);
tempGeo.mergeVertices();
// after only mergeVertices my textrues were turning black so this fixed normals issues
tempGeo.computeVertexNormals();
tempGeo.computeFaceNormals();
child.geometry = new BufferGeometry().fromGeometry(tempGeo);
这篇关于三.js - 使用 mergeVertices() 平滑法线;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!