Improved progressive renderer... still bugged

This commit is contained in:
Thomas FORGIONE 2015-06-18 11:46:24 +02:00
parent bfca8ceab1
commit b94725ab7c
3 changed files with 198 additions and 113 deletions

View File

@ -6,8 +6,10 @@ mesh.Mesh = function() {
this.faces = []; this.faces = [];
this.texCoords = []; this.texCoords = [];
this.normals = []; this.normals = [];
this.faceIndex = -1; this.faceIndex = 0;
this.material = null; this.material = null;
this.started = false;
this.finished = false;
} }
mesh.Mesh.prototype.hasNormals = function() { mesh.Mesh.prototype.hasNormals = function() {
@ -71,7 +73,7 @@ mesh.Mesh.prototype.addNormal = function(normal) {
} else if (typeof normal === 'string' || normal instanceof String) { } else if (typeof normal === 'string' || normal instanceof String) {
this.normals.push(new mesh.Normal(normal)); this.normals.push(new mesh.Normal(normal));
} else { } else {
console.error("Cann only add normal from mesh.Normal of string"); console.error("Can only add normal from mesh.Normal of string");
return; return;
} }
@ -79,7 +81,19 @@ mesh.Mesh.prototype.addNormal = function(normal) {
} }
mesh.Mesh.prototype.isFinished = function() { mesh.Mesh.prototype.isFinished = function() {
return this.faces.length < this.faceIndex; // return this.faceIndex === this.faces.length;
for (var i = 0; i < this.faces.length; i++) {
if (!(this.faces[i].sent)) {
return false;
}
}
return true;
} }
// Vertex // Vertex
@ -223,8 +237,8 @@ mesh.Face.prototype.maxTexture = function() {
} }
} }
mesh.Face.prototype.toList = function(parameter) { mesh.Face.prototype.toList = function() {
var l = ['f', this.index, parameter, var l = ['f', this.index, this.meshIndex,
[this.a, this.b, this.c ], [this.a, this.b, this.c ],
isNaN(this.aTexture) ? [] : [this.aTexture, this.bTexture, this.cTexture], isNaN(this.aTexture) ? [] : [this.aTexture, this.bTexture, this.cTexture],
isNaN(this.aNormal ) ? [] : [this.aNormal, this.bNormal, this.cNormal ] isNaN(this.aNormal ) ? [] : [this.aNormal, this.bNormal, this.cNormal ]

View File

@ -19,7 +19,6 @@ geo.MeshStreamer = function(path, callback) {
if (path !== undefined) { if (path !== undefined) {
var self = this; var self = this;
this.loadFromFile(path, function() { this.loadFromFile(path, function() {
if (typeof callback === 'function') if (typeof callback === 'function')
callback(); callback();
@ -31,6 +30,8 @@ geo.MeshStreamer.prototype.loadFromFile = function(path, callback) {
var self = this; var self = this;
fs.readFile(path, function(err, data) { fs.readFile(path, function(err, data) {
var oldTime = Date.now();
var currentMesh; var currentMesh;
// Get lines from file // Get lines from file
@ -84,12 +85,14 @@ geo.MeshStreamer.prototype.loadFromFile = function(path, callback) {
// Create faces (two if Face4) // Create faces (two if Face4)
var faces = currentMesh.addFaces(line); var faces = currentMesh.addFaces(line);
faces[0].index = self.faces.length; faces[0].index = currentMesh.faces.length - (faces.length === 2 ? 1 : 2);
faces[0].meshIndex = self.meshes.length - 1;
self.faces.push(faces[0]); self.faces.push(faces[0]);
if (faces.length === 2) { if (faces.length === 2) {
faces[1].index = self.faces.length; faces[1].index = currentMesh.faces.length - 1;
faces[1].meshIndex = self.meshes.length - 1;
self.faces.push(faces[1]); self.faces.push(faces[1]);
} }
@ -113,12 +116,15 @@ geo.MeshStreamer.prototype.loadFromFile = function(path, callback) {
callback(); callback();
} }
console.log(Date.now() - oldTime);
}); });
} }
geo.MeshStreamer.prototype.start = function(socket) { geo.MeshStreamer.prototype.start = function(socket) {
this.meshIndex = 0; this.meshIndex = 0;
this.socket = socket;
var self = this; var self = this;
@ -139,8 +145,30 @@ geo.MeshStreamer.prototype.start = function(socket) {
}); });
socket.on('next', function(_camera) { socket.on('next', function(camera) {
// Send next elements
var next = self.nextElements(camera);
// console.log(self.meshIndex);
// console.log(data);
// Emit self.chunk faces (and the corresponding vertices if not emitted)
socket.emit('elements', next.data);
if (next.finished) {
socket.disconnect();
}
});
}
geo.MeshStreamer.prototype.nextElements = function(_camera) {
// Prepare camera (and scale to model)
var camera = { var camera = {
position: { position: {
x: _camera[0]*10, x: _camera[0]*10,
@ -154,77 +182,61 @@ geo.MeshStreamer.prototype.start = function(socket) {
} }
} }
// Compute camera direction
var direction = { var direction = {
x: camera.target.x - camera.position.x, x: camera.target.x - camera.position.x,
y: camera.target.y - camera.position.y, y: camera.target.y - camera.position.y,
z: camera.target.z - camera.position.z z: camera.target.z - camera.position.z
} }
var norm = direction.x * direction.x + direction.y * direction.y + direction.z * direction.z; var sent = 0;;
direction.x /= norm;
direction.y /= norm;
direction.z /= norm;
// Send next elements
var currentMesh = self.meshes[self.meshIndex];
var currentFace;
var data = []; var data = [];
var sent = 0; var mightBeCompletetlyFinished = true;
while (sent < 100) { for (var meshIndex = 0; meshIndex < this.meshes.length; meshIndex++) {
var currentMesh = this.meshes[meshIndex];
if (currentMesh.faceIndex === -1) { if (currentMesh.isFinished()) {
// Must give usemtl
data.push(['u', currentMesh.material, currentMesh.vertices.length, currentMesh.faces.length, self.texCoords.length > 0, self.normals.length > 0]);
sent++;
currentFace = currentMesh.faces[++currentMesh.faceIndex];
} else if (currentMesh.isFinished()) {
// Must switch to next mesh
currentMesh = self.meshes[++self.meshIndex];
// If not other mesh, we finished
if (currentMesh === undefined) {
socket.emit('elements', data);
socket.disconnect();
return;
}
data.push(['u', currentMesh.material, currentMesh.vertices.length, currentMesh.faces.length, self.texCoords.length > 0, self.normals.length > 0]);
currentMesh.faceIndex++;
sent++;
continue; continue;
} else { } else {
currentFace = currentMesh.faces[++currentMesh.faceIndex]; mightBeCompletetlyFinished = false;
if (currentFace === undefined) {
currentMesh = self.meshes[++self.meshIndex];
if (currentMesh === undefined) {
socket.emit('elements', data);
socket.disconnect();
return;
} }
for (var faceIndex = 0; faceIndex < currentMesh.faces.length; faceIndex++) {
var currentFace = currentMesh.faces[faceIndex];
if (currentFace.sent) {
continue; continue;
} }
if (!currentMesh.started) {
// Send usemtl
data.push([
'u',
currentMesh.material,
currentMesh.vertices.length,
currentMesh.faces.length,
this.texCoords.length > 0,
this.normals.length > 0
]);
sent++;
currentMesh.started = true;
} }
var vertex1 = self.vertices[currentFace.a]; var vertex1 = this.vertices[currentFace.a];
var vertex2 = self.vertices[currentFace.b]; var vertex2 = this.vertices[currentFace.b];
var vertex3 = self.vertices[currentFace.c]; var vertex3 = this.vertices[currentFace.c];
var v1 = { var v1 = {
x: vertex1.x - camera.position.x, x: vertex1.x - camera.position.x,
@ -244,66 +256,113 @@ geo.MeshStreamer.prototype.start = function(socket) {
z: vertex3.z - camera.position.z z: vertex3.z - camera.position.z
}; };
norm = v1.x * v1.x + v1.y * v1.y + v1.z * v1.z;
v1.x /= norm;
v1.y /= norm;
v1.z /= norm;
norm = v2.x * v2.x + v2.y * v2.y + v2.z * v2.z;
v2.x /= norm;
v2.y /= norm;
v2.z /= norm;
norm = v3.x * v3.x + v3.y * v3.y + v3.z * v3.z;
v3.x /= norm;
v3.y /= norm;
v3.z /= norm;
if ( if (
direction.x * v1.x + direction.y * v1.y + direction.z * v1.z < 0 && direction.x * v1.x + direction.y * v1.y + direction.z * v1.z < 0 &&
direction.x * v2.x + direction.y * v2.y + direction.z * v2.z < 0 && direction.x * v2.x + direction.y * v2.y + direction.z * v2.z < 0 &&
direction.x * v3.x + direction.y * v3.y + direction.z * v3.z < 0 direction.x * v3.x + direction.y * v3.y + direction.z * v3.z < 0
) { ) {
currentFace.sent = false;
continue; continue;
} }
if (!vertex1.sent) { data.push(vertex1.toList()); vertex1.sent = true; sent++;} if (!vertex1.sent) {
if (!vertex2.sent) { data.push(vertex2.toList()); vertex2.sent = true; sent++;}
if (!vertex3.sent) { data.push(vertex3.toList()); vertex3.sent = true; sent++;}
var normal1 = self.normals[currentFace.aNormal]; data.push(vertex1.toList());
var normal2 = self.normals[currentFace.bNormal]; vertex1.sent = true;
var normal3 = self.normals[currentFace.cNormal];
if (normal1 !== undefined && !normal1.sent) { data.push(normal1.toList()); normal1.sent = true; sent++;}
if (normal2 !== undefined && !normal2.sent) { data.push(normal2.toList()); normal2.sent = true; sent++;}
if (normal3 !== undefined && !normal3.sent) { data.push(normal3.toList()); normal3.sent = true; sent++;}
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; sent++;}
if (tex2 !== undefined && !tex2.sent) { data.push(tex2.toList()); tex2.sent = true; sent++;}
if (tex3 !== undefined && !tex3.sent) { data.push(tex3.toList()); tex3.sent = true; sent++;}
currentFace.index = currentMesh.faceIndex;
data.push(currentFace.toList(self.meshIndex)); currentFace.sent = true;
sent++; sent++;
} }
// console.log(self.meshIndex);
// console.log(data);
// Emit self.chunk faces (and the corresponding vertices if not emitted) if (!vertex2.sent) {
socket.emit('elements', data);
data.push(vertex2.toList());
vertex2.sent = true;
sent++;
}
if (!vertex3.sent) {
data.push(vertex3.toList());
vertex3.sent = true;
sent++;
}
var normal1 = this.normals[currentFace.aNormal];
var normal2 = this.normals[currentFace.bNormal];
var normal3 = this.normals[currentFace.cNormal];
if (normal1 !== undefined && !normal1.sent) {
data.push(normal1.toList());
normal1.sent = true;
sent++;
}
if (normal2 !== undefined && !normal2.sent) {
data.push(normal2.toList());
normal2.sent = true;
sent++;
}
if (normal3 !== undefined && !normal3.sent) {
data.push(normal3.toList());
normal3.sent = true;
sent++;
}
var tex1 = this.texCoords[currentFace.aTexture];
var tex2 = this.texCoords[currentFace.bTexture];
var tex3 = this.texCoords[currentFace.cTexture];
if (tex1 !== undefined && !tex1.sent) {
data.push(tex1.toList());
tex1.sent = true;
sent++;
}
if (tex2 !== undefined && !tex2.sent) {
data.push(tex2.toList());
tex2.sent = true;
sent++;
}
if (tex3 !== undefined && !tex3.sent) {
data.push(tex3.toList());
tex3.sent = true;
sent++;
}
data.push(currentFace.toList());
currentFace.sent = true;
currentMesh.faceIndex++;
sent++;
if (sent > 500) {
return {data: data, finished: false};
}
}
}
return {data: data, finished: mightBeCompletetlyFinished};
});
} }
module.exports = geo; module.exports = geo;

View File

@ -126,10 +126,22 @@ ProgressiveLoaderGeometry.prototype.initIOCallbacks = function() {
this.socket.on('elements', function(arr) { this.socket.on('elements', function(arr) {
if (arr.length === 0) {
console.log("Empty array");
} else {
console.log("Stuff received");
}
// console.log("Received elements for the " + (++self.counter) + "th time !"); // console.log("Received elements for the " + (++self.counter) + "th time !");
for (var i = 0; i < arr.length; i++) { for (var i = 0; i < arr.length; i++) {
var elt = _parseList2(arr[i]); var elt = _parseList2(arr[i]);
// console.log(arr[i]);
// console.log(elts); // console.log(elts);
if (elt.type === 'vertex') { if (elt.type === 'vertex') {