I am in the process of creating my own model format and attempting to generate custom geometry. Although I can successfully import the geometry, the face normals do not render even after being added to the geometry.
Below is the input file:
# Coordinates
0e+0 0e+0 0e+0
1e+0 0e+0 0e+0
1e+0 0e+0 1e+0
0e+0 0e+0 1e+0
0e+0 1e+0 0e+0
1e+0 1e+0 0e+0
1e+0 1e+0 1e+0
0e+0 1e+0 1e+0
# Normals
0e+0 0e+0 -1e+0
0e+0 -1e+0 0e+0
0e+0 0e+0 1e+0
0e+0 1e+0 0e+0
1e+0 0e+0 0e+0
-1e+0 0e+0 0e+0
# Connectivity List
1 2 6 5
1 2 3 4
3 4 8 7
6 5 8 7
2 6 7 3
1 5 8 4
This is how I am importing it:
var geometry = new THREE.Geometry();
//Add all positions to geometry
for (var g=0;g<coordinates.length;g++){
geometry.vertices.push(coordinates[g]);
}
for(var l=0;l<connectivity.length;l++){
//sml file have rectangular faces but three js uses triangular faces (THREE.Face4 is deprecated) so converting 4 vertex faces to 3 verex faces
var index0= connectivity[l][3]-1;
var index1= connectivity[l][4]-1;
var index2= connectivity[l][5]-1;
var index3= connectivity[l][6]-1;
//If normals is exist thenaddthem to face array too
if(normals.length==connectivity.length){
console.log("Normals are exist");
var face0= new THREE.Face3(index0,index1,index2);
face0.normal.set(normals[l]);
geometry.faces.push(face0);
var face1= new THREE.Face3(index2,index3,index0);
face1.normal.set(normals[l]);
geometry.faces.push(face1);
} else{
console.log("Normals are not exist");
var face0= new THREE.Face3(index0,index1,index2);
geometry.faces.push(face0);
var face1= new THREE.Face3(index2,index3,index0);
geometry.faces.push(face1);
}
}
geometry.computeBoundingBox();
// geometry.compteVertexNormals();
geometry.computeFaceNormals();
In the code, I am converting quads to triangles in the face (connectivity list) array as FACE4 is deprecated in Three.js. Additionally, I assign the same normal to both triangles that share the same quad.
Here is a visualization of this box:
https://i.sstatic.net/NK622.jpg
Do you see any missing components or potential issues?