2017-04-05 161 views
0

是否有任何方法使用给定的步长/厚度来补偿THREE.JS中的网格几何?THREE.JS中的偏移网格

offseting

这不是一个简单的缩放。我建议算法应该基于法线,它给出缩放边缘的方向。

+0

可以想像,THREE.FaceNormalsHelper或THREE.VertexNormalsHelper。 – VVK

回答

0

这是一些代码,我只是很快写下来,并没有testet。但这将是我如何做到的方法。也许这是一开始。

var geo = mesh.geometry; // assuming this Geometry and not BufferGeometry 
var verts = geo.vertices; 
var faces = geo.faces; 

// vertices are used in multiple faces with different normals 
// we want to translate the vertex along the average normal 

// first, I collect all normals for each vertex 
// (it's a bit quick and dirty as I add a normals property to the Vector3 object) 
for(var i=0; i<faces.length; i++) { 
    // vertex a 
    var va = verts[faces[i].a]; 
    if(va.normals) va.normals.push(faces[i].vertexNormals[0]); // add normal to collection 
    else va.normals = [ faces[i].vertexNormals[0] ];    // otherwise create array with first element 
    // vertex b 
    var vb = verts[faces[i].b]; 
    if(vb.normals) vb.normals.push(faces[i].vertexNormals[1]); 
    else vb.normals = [ faces[i].vertexNormals[1] ]; 
    // vertex c 
    var vc = verts[faces[i].c]; 
    if(vc.normals) vc.normals.push(faces[i].vertexNormals[2]); 
    else vc.normals = [ faces[i].vertexNormals[2] ]; 
} 

// then iterate over vertices, compute average normal, and translate vertex 
for(i=0; i<verts.length; i++) { 
    var v = verts[i]; 
    var normal = computeAverageNormal(v.normals); 
    normal.multiplyScalar(offsetAmount); 
    v.add(normal); 
} 

geo.verticesNeedUpdate = true; 

function computeAverageNormal(normals) { 
    var n = new THREE.Vector3(); 
    for(var i=0; i<normals.length; i++) { 
    n.add(normals[i]); 
    } 
    n.normalize(); 
    return n; 
}