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.texCoords = [];
this.normals = [];
this.faceIndex = -1;
this.faceIndex = 0;
this.material = null;
this.started = false;
this.finished = false;
}
mesh.Mesh.prototype.hasNormals = function() {
@ -71,7 +73,7 @@ mesh.Mesh.prototype.addNormal = function(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");
console.error("Can only add normal from mesh.Normal of string");
return;
}
@ -79,7 +81,19 @@ mesh.Mesh.prototype.addNormal = function(normal) {
}
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
@ -223,8 +237,8 @@ mesh.Face.prototype.maxTexture = function() {
}
}
mesh.Face.prototype.toList = function(parameter) {
var l = ['f', this.index, parameter,
mesh.Face.prototype.toList = function() {
var l = ['f', this.index, this.meshIndex,
[this.a, this.b, this.c ],
isNaN(this.aTexture) ? [] : [this.aTexture, this.bTexture, this.cTexture],
isNaN(this.aNormal ) ? [] : [this.aNormal, this.bNormal, this.cNormal ]

View File

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

View File

@ -126,10 +126,22 @@ ProgressiveLoaderGeometry.prototype.initIOCallbacks = function() {
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 !");
for (var i = 0; i < arr.length; i++) {
var elt = _parseList2(arr[i]);
// console.log(arr[i]);
// console.log(elts);
if (elt.type === 'vertex') {