Added normals to the spheres, and improved ProgressiveLoader

This commit is contained in:
Thomas FORGIONE
2015-06-16 12:05:29 +02:00
parent f89e52dc5a
commit c6062e6a50
30 changed files with 90117 additions and 45613 deletions

View File

@@ -5,10 +5,19 @@ mesh.Mesh = function() {
this.vertices = [];
this.faces = [];
this.texCoords = [];
this.normals = [];
this.faceIndex = 0;
this.material = null;
}
mesh.Mesh.prototype.hasNormals = function() {
return this.normals.length > 0;
}
mesh.Mesh.prototype.hasTexCoords = function() {
return this.texCoords.length > 0;
}
mesh.Mesh.prototype.addVertex = function(vertex) {
if (vertex instanceof mesh.Vertex) {
@@ -24,8 +33,6 @@ mesh.Mesh.prototype.addVertex = function(vertex) {
}
mesh.Mesh.prototype.addFaces = function(face) {
this.index = this.faces.length;
var faces;
if (face instanceof mesh.Face) {
@@ -46,8 +53,6 @@ mesh.Mesh.prototype.addFaces = function(face) {
}
mesh.Mesh.prototype.addTexCoord = function(texCoord) {
this.index = this.texCoords.length;
if (texCoord instanceof mesh.TexCoord) {
this.texCoords.push(texCoord);
} else if (typeof texCoord === 'string' || texCoord instanceof String) {
@@ -60,6 +65,19 @@ mesh.Mesh.prototype.addTexCoord = function(texCoord) {
return this.texCoords[this.texCoords.length - 1];
}
mesh.Mesh.prototype.addNormal = function(normal) {
if (normal instanceof mesh.Normal) {
this.normals.push(normal);
} else if (typeof normal === 'string' || normal instanceof String) {
this.normals.push(new mesh.Normal(normal));
} else {
console.error("Cann only add normal from mesh.Normal of string");
return;
}
return this.normals[this.normals.length - 1];
}
mesh.Mesh.prototype.isFinished = function() {
return this.faces.length === this.faceIndex;
}
@@ -83,6 +101,25 @@ mesh.Vertex.prototype.toString = function() {
return 'v ' + this.x + ' ' + this.y + ' ' + this.z;
}
// Normal is the same as a vertex, except for toList and toString
mesh.Normal = function() {
mesh.Vertex.apply(this, arguments);
}
mesh.Normal.prototype = Object.create(mesh.Vertex.prototype);
mesh.Normal.prototype.constructor = mesh.Normal;
mesh.Normal.prototype.toList = function() {
var superObject = mesh.Vertex.prototype.toList.call(this);
superObject[0] = 'vn';
return superObject;
}
mesh.Normal.toString = function() {
var superObject = mesh.Vertex.prototype.toString.call(this);
superObject.replace('v', 'vn');
return superObject;
}
// TexCoord : texture coordinates
mesh.TexCoord = function() {
@@ -123,12 +160,20 @@ mesh.Face = function() {
var split3 = split[3].split('/');
var vIndex = 0;
// var tIndex = split1.length === 2 ? 1 : 2;
var tIndex = 1;
var nIndex = 2;
this.a = parseInt(split1[vIndex]) - 1; this.aTexture = parseInt(split1[tIndex]) - 1;
this.b = parseInt(split2[vIndex]) - 1; this.bTexture = parseInt(split2[tIndex]) - 1;
this.c = parseInt(split3[vIndex]) - 1; this.cTexture = parseInt(split3[tIndex]) - 1;
this.a = parseInt(split1[vIndex]) - 1;
this.b = parseInt(split2[vIndex]) - 1;
this.c = parseInt(split3[vIndex]) - 1;
this.aTexture = parseInt(split1[tIndex]) - 1;
this.bTexture = parseInt(split2[tIndex]) - 1;
this.cTexture = parseInt(split3[tIndex]) - 1;
this.aNormal = parseInt(split1[nIndex]) - 1;
this.bNormal = parseInt(split2[nIndex]) - 1;
this.cNormal = parseInt(split3[nIndex]) - 1;
}
}
@@ -137,9 +182,9 @@ mesh.Face = function() {
}
var parseFace = function(arg) {
var split = arg.split(' ');
var split = arg.trim().split(' ');
var ret = [];
// Face3
@@ -147,6 +192,7 @@ var parseFace = function(arg) {
ret.push(new mesh.Face(arg));
}
// Face3 == 2 * Face3
if (split.length >= 5) {
ret.push(new mesh.Face(
[
@@ -178,7 +224,11 @@ mesh.Face.prototype.maxTexture = function() {
}
mesh.Face.prototype.toList = function() {
var l = ['f', this.index, this.a, this.b, this.c];
var l = ['f', this.index,
[this.a, this.b, this.c ],
isNaN(this.aTexture) ? [] : [this.aTexture, this.bTexture, this.cTexture],
isNaN(this.aNormal ) ? [] : [this.aNormal, this.bNormal, this.cNormal ]
];
// if (this.d !== undefined)
// l.push(this.d);
@@ -201,8 +251,9 @@ mesh.Face.prototype.toString = function() {
// Material
mesh.Material = function() {
var split = arguments[0].replace(/\s+/g, ' ').split(' ');
var split = arguments[0].replace(/\s+/g, ' ').trim().split(' ');
this.name = split[1];
console.log(this.name);
}
mesh.Material.prototype.toString = function() {

View File

@@ -10,6 +10,7 @@ geo.MeshStreamer = function(path, callback) {
// In meshes, vertices and texture coords are shared
this.vertices = [];
this.faces = [];
this.normals = [];
this.texCoords = [];
// Chunk size
@@ -45,21 +46,28 @@ geo.MeshStreamer.prototype.loadFromFile = function(path, callback) {
if (line[1] === 't') {
// Texture coord
self.texCoords.push(new mesh.TexCoord(line));
var texCoord = new mesh.TexCoord(line);
texCoord.index = self.texCoords.length;
self.texCoords.push(texCoord);
} else if (line[1] === 'n') {
// Ignore normals
var normal = new mesh.Normal(line);
normal.index = self.normals.length;
self.normals.push(normal);
} else {
// Just a simple vertex
if (currentMesh === undefined) {
// Chances are that we won't use any material in this case
currentMesh = new mesh.Mesh();
self.meshes.push(currentMesh);
}
var vertex = currentMesh.addVertex(line);
// if (currentMesh === undefined) {
// // Chances are that we won't use any material in this case
// currentMesh = new mesh.Mesh();
// self.meshes.push(currentMesh);
// }
var vertex = new mesh.Vertex(line);
vertex.index = self.vertices.length;
self.vertices.push(vertex);
@@ -67,25 +75,35 @@ geo.MeshStreamer.prototype.loadFromFile = function(path, callback) {
} else if (line[0] === 'f') {
// Create mesh if it doesn't exist
if (currentMesh === undefined) {
currentMesh = new mesh.Mesh();
self.meshes.push(currentMesh);
}
// Create faces (two if Face4)
var faces = currentMesh.addFaces(line);
faces[0].index = self.faces.length;
self.faces.push(faces[0]);
if (faces.length === 2) {
faces[1].index = self.faces.length;
self.faces.push(faces[1]);
}
} else if (line[0] === 'u') {
// usemtl
// If a current mesh exists, finish it
// Create a new mesh
currentMesh = new mesh.Mesh();
self.meshes.push(currentMesh);
currentMesh.material = mesh.Material(line);
self.orderedElements.push(new mesh.Usemtl(line));
currentMesh.material = (new mesh.Material(line)).name;
// console.log(currentMesh.material);
}
@@ -139,7 +157,8 @@ geo.MeshStreamer.prototype.start = function(socket) {
currentMesh.material,
currentMesh.vertices.length,
currentMesh.faces.length,
currentMesh.texCoords.length
self.texCoords.length > 0,
self.normals.length > 0
);
} else {
@@ -160,6 +179,24 @@ geo.MeshStreamer.prototype.start = function(socket) {
if (!vertex2.sent) { data.push(vertex2.toList()); vertex2.sent = true;}
if (!vertex3.sent) { data.push(vertex3.toList()); vertex3.sent = true;}
var normal1 = self.normals[currentFace.aNormal];
var normal2 = self.normals[currentFace.bNormal];
var normal3 = self.normals[currentFace.cNormal];
if (normal1 !== undefined && !normal1.sent) { data.push(normal1.toList()); normal1.sent = true;}
if (normal2 !== undefined && !normal2.sent) { data.push(normal2.toList()); normal2.sent = true;}
if (normal3 !== undefined && !normal3.sent) { data.push(normal3.toList()); normal3.sent = true;}
var tex1 = self.texCoords[currentFace.aTexture];
var tex2 = self.texCoords[currentFace.bTexture];
var tex3 = self.texCoords[currentFace.cTexture];
if (tex1 !== undefined && !tex1.sent) { data.push(tex1.toList()); tex1.sent = true;}
if (tex2 !== undefined && !tex2.sent) { data.push(tex2.toList()); tex2.sent = true;}
if (tex3 !== undefined && !tex3.sent) { data.push(tex3.toList()); tex3.sent = true;}
currentFace.index = currentMesh.faceIndex;
data.push(currentFace.toList()); currentFace.sent = true;
}