Improved server-side mesh streaming

This commit is contained in:
Thomas FORGIONE 2015-06-11 10:12:50 +02:00
parent 613fd0edd2
commit e5e8f7d590
3 changed files with 155 additions and 28 deletions

134
geo/Mesh.js Normal file
View File

@ -0,0 +1,134 @@
var fs = require('fs');
var geo = geo || {};
geo.MeshStreamer = function(path, callback) {
this.vertices = [];
this.faces = [];
this.orderedElements = [];
this.index = 0;
if (path !== undefined) {
var self = this;
this.loadFromFile(path, callback);
}
}
geo.MeshStreamer.prototype.loadFromFile = function(path, callback) {
var self = this;
fs.readFile(path, function(err, data) {
var lines = data.toString('utf-8').split("\n");
var vertexCounter = 0;
var faceCounter = 0;
//
var vertex_face = [];
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (line[0] === 'v') {
var vertex = self.vertices[self.vertices.push(new geo.Vertex(line)) - 1];
vertex_face.push([]);
vertexCounter ++;
} else if (line[0] === 'f') {
var face = self.faces[self.faces.push(new geo.Face(line)) - 1];
vertex_face[face.a].push(faceCounter);
vertex_face[face.b].push(faceCounter);
vertex_face[face.c].push(faceCounter);
if (vertex_face[face.d]) {
vertex_face[face.d].push(faceCounter);
}
faceCounter ++;
}
}
for (var vertex = 0; vertex < self.vertices.length; vertex++) {
self.orderedElements.push(self.vertices[vertex]);
for (var face = 0; face < vertex_face[vertex].length; face++) {
var faceToAdd = self.faces[vertex_face[vertex][face]];
if (faceToAdd.max() <= vertex) {
self.orderedElements.push(faceToAdd);
}
}
}
if (typeof callback === 'function') {
callback();
}
});
}
geo.Vertex = function() {
if (typeof arguments[0] === 'string' || arguments[0] instanceof String) {
var split = arguments[0].split(' ');
this.x = parseFloat(split[1]);
this.y = parseFloat(split[2]);
this.z = parseFloat(split[3]);
} else if (arguments.length === 3) {
this.x = arguments[0];
this.y = arguments[1];
this.z = arguments[2];
} else {
this.x = 0;
this.y = 0;
this.z = 0;
}
this.sent = false;
}
geo.Vertex.prototype.toList = function() {
return ['v', this.x, this.y, this.z];
}
geo.Vertex.prototype.toString = function() {
return 'v ' + this.x + ' ' + this.y + ' ' + this.z;
}
geo.Face = function() {
if (typeof arguments[0] === 'string' || arguments[0] instanceof String) {
var split = arguments[0].split(' ');
this.a = parseInt(split[1]) - 1;
this.b = parseInt(split[2]) - 1;
this.c = parseInt(split[3]) - 1;
if (split.length === 5)
this.d = parseInt(split[4]) - 1;
} else if (arguments.length === 3) {
this.a = arguments[0] - 1;
this.b = arguments[1] - 1;
this.c = arguments[2] - 1;
if (arguments.length === 4)
this.d = arguments[3] - 1;
}
this.sent = false;
}
geo.Face.prototype.max = function() {
if (this.d) {
return Math.max(this.a, this.b, this.c, this.d);
} else {
return Math.max(this.a, this.b, this.c);
}
}
geo.Face.prototype.toList = function() {
var l = ['f', this.a, this.b, this.c];
if (this.d)
l.push(this.d);
return l;
}
geo.Face.prototype.toString = function() {
return 'f ' + this.a + ' ' + this.b + ' ' + this.c + (this.d ? ' ' + this.d : '');
}
module.exports = geo;

View File

@ -82,13 +82,11 @@ var ProgressiveLoader = function(res, scene) {
} }
mesh.geometry.computeFaceNormals(); mesh.geometry.computeFaceNormals();
mesh.geometry.groupsNeedUpdate = true; mesh.geometry.groupsNeedUpdate = true;
mesh.geometry.elementsNeedUpdate = true; mesh.geometry.elementsNeedUpdate = true;
mesh.geometry.normalsNeedUpdate = true; mesh.geometry.normalsNeedUpdate = true;
},0); },0);
}); });

View File

@ -1,5 +1,6 @@
var fs = require('fs'); var fs = require('fs');
var sleep = require('sleep'); var sleep = require('sleep');
var geo = require('./geo/Mesh.js');
function parseLine(line) { function parseLine(line) {
var elts = line.split(' '); var elts = line.split(' ');
@ -33,9 +34,9 @@ module.exports = function(io) {
io.on('connection', function(socket) { io.on('connection', function(socket) {
var index = 0; var index = 0;
var path;
var vIndex = 0; var vIndex = 0;
var fIndex = 0; var fIndex = 0;
var mesh;
// console.log(socket.conn.remoteAddress + " connected !"); // console.log(socket.conn.remoteAddress + " connected !");
@ -46,40 +47,34 @@ module.exports = function(io) {
socket.on("request", function(res) { socket.on("request", function(res) {
// console.log('Asking for static/data/spheres/' + res + '.obj'); // console.log('Asking for static/data/spheres/' + res + '.obj');
path = 'static/data/spheres/' + res + '.obj.obj'; var path = 'static/data/spheres/' + res + '.obj';
socket.emit('ok');
mesh = new geo.MeshStreamer(path, function() {
socket.emit('ok');
});
}); });
socket.on('next', function() { socket.on('next', function() {
var toSend = [];
var elt;
for (var i = mesh.index, limit = mesh.index + 200; i < limit; i++) {
elt = mesh.orderedElements[i];
fs.readFile(path, function(err, data) { if (elt) {
var lines = data.toString('utf-8').split("\n"); toSend.push(elt.toList());
var line = lines[index]; mesh.index ++;
var toSend = []; } else {
break;
for (var i = 0; i < 50; i++) {
while (line && line[0] !== 'f') {
toSend.push(parseLine(line));
line = lines[++index];
}
if (line && line[0] === 'f') {
toSend.push(parseLine(line));
line = lines[++index];
}
} }
}
socket.emit('elements', toSend); console.log(toSend.length);
socket.emit('elements', toSend);
if (!line) { if (!elt) {
// socket.emit('finished'); socket.disconnect();
socket.disconnect(); }
}
});
}); });
}); });
} }