我通過向新的THREE.Geometry()添加頂點和麪以創建自定義網格,然後在其上運行computeFaceNormals()和computeVertexNormals()以平滑渲染(I使用MeshPhongMaterial)。如果沒有computevertexnormals,我的網格部分會出現條紋。問題是r69中包含的computeVertexNormals()忽略了尖銳的邊緣。這是一個優雅的函數,通過平均周圍的面來構建每個頂點的法線。但是它平均了我需要保持邊緣清晰的邊緣的法線。另外一些有希望的評論question with the same topic但是沒有發佈代碼來解決邊緣尖銳問題。three.js在平滑着色時保持摺痕定製幾何
我試圖修改computeVertexNormals()來添加邊緣檢測,但沒有運氣。我的嘗試是基於檢測相鄰面之間的角度,並且只在給定閾值內時纔將它們的法線添加到平均值。這裏是我的代碼:
function computeVertexNormals(object, angle_threshold, areaWeighted) { //will compute normals if faces diverge less than given angle (in degrees)
var v, vl, f, fl, face, vertices;
angle = angle_threshold * 0.0174532925; //degrees to radians
vertices = new Array(object.vertices.length);
for (v = 0, vl = object.vertices.length; v < vl; v ++) {
vertices[ v ] = new THREE.Vector3();
}
if (areaWeighted && areaWeighted == true) {
// vertex normals weighted by triangle areas
// http://www.iquilezles.org/www/articles/normals/normals.htm
var vA, vB, vC, vD;
var cb = new THREE.Vector3(), ab = new THREE.Vector3(),
db = new THREE.Vector3(), dc = new THREE.Vector3(), bc = new THREE.Vector3();
for (f = 0, fl = object.faces.length; f < fl; f ++) {
face = object.faces[ f ];
vA = object.vertices[ face.a ];
vB = object.vertices[ face.b ];
vC = object.vertices[ face.c ];
cb.subVectors(vC, vB);
ab.subVectors(vA, vB);
cb.cross(ab);
vertices[ face.a ].add(cb);
vertices[ face.b ].add(cb);
vertices[ face.c ].add(cb);
}
} else {
for (f = 0, fl = object.faces.length; f < fl; f ++) {
face = object.faces[ f ];
vertices[ face.a ].add(face.normal);
vertices[ face.b ].add(face.normal);
vertices[ face.c ].add(face.normal);
}
}
for (v = 0, vl = object.vertices.length; v < vl; v ++) {
vertices[ v ].normalize();
}
for (f = 0, fl = object.faces.length; f < fl; f ++) {
face = object.faces[ f ];
//**********my modifications are all in this last section*************
if(face.normal && face.normal != undefined){
if(vertices[ face.a ].angleTo(face.normal) < angle_threshold){
face.vertexNormals[ 0 ] = vertices[ face.a ].clone();
}else{
face.vertexNormals[ 0 ] = face.normal.clone();
}
if(vertices[ face.b ].angleTo(face.normal) < angle_threshold){
face.vertexNormals[ 1 ] = vertices[ face.b ].clone();
}else{
face.vertexNormals[ 1 ] = face.normal.clone();
}
if(vertices[ face.c ].angleTo(face.normal) < angle_threshold){
face.vertexNormals[ 2 ] = vertices[ face.c ].clone();
}else{
face.vertexNormals[ 2 ] = face.normal.clone();
}
}
}
}
大家能否請提供摺痕檢測的策略,所以我可以有平滑的形狀與一些鋒利的邊緣? 在此先感謝!
相反,考慮在構建幾何時複製硬邊上的頂點。 – WestLangley 2014-11-21 09:48:14