2017-04-05 62 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; 
}