From e5e8f7d590d276b43dbc12f2de75fb10bd91fa44 Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Thu, 11 Jun 2015 10:12:50 +0200 Subject: [PATCH] Improved server-side mesh streaming --- geo/Mesh.js | 134 ++++++++++++++++++++++++++++++++++++++++ js/ProgressiveLoader.js | 2 - socket.js | 47 +++++++------- 3 files changed, 155 insertions(+), 28 deletions(-) create mode 100644 geo/Mesh.js diff --git a/geo/Mesh.js b/geo/Mesh.js new file mode 100644 index 0000000..d8c1bfd --- /dev/null +++ b/geo/Mesh.js @@ -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; diff --git a/js/ProgressiveLoader.js b/js/ProgressiveLoader.js index a0fa8b0..ef748f7 100644 --- a/js/ProgressiveLoader.js +++ b/js/ProgressiveLoader.js @@ -82,13 +82,11 @@ var ProgressiveLoader = function(res, scene) { } - mesh.geometry.computeFaceNormals(); mesh.geometry.groupsNeedUpdate = true; mesh.geometry.elementsNeedUpdate = true; mesh.geometry.normalsNeedUpdate = true; - },0); }); diff --git a/socket.js b/socket.js index 9be8776..5a0151a 100644 --- a/socket.js +++ b/socket.js @@ -1,5 +1,6 @@ var fs = require('fs'); var sleep = require('sleep'); +var geo = require('./geo/Mesh.js'); function parseLine(line) { var elts = line.split(' '); @@ -33,9 +34,9 @@ module.exports = function(io) { io.on('connection', function(socket) { var index = 0; - var path; var vIndex = 0; var fIndex = 0; + var mesh; // console.log(socket.conn.remoteAddress + " connected !"); @@ -46,40 +47,34 @@ module.exports = function(io) { socket.on("request", function(res) { // console.log('Asking for static/data/spheres/' + res + '.obj'); - path = 'static/data/spheres/' + res + '.obj.obj'; - - socket.emit('ok'); + var path = 'static/data/spheres/' + res + '.obj'; + mesh = new geo.MeshStreamer(path, function() { + socket.emit('ok'); + }); }); 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) { - var lines = data.toString('utf-8').split("\n"); - var line = lines[index]; - var toSend = []; - - 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]; - } + if (elt) { + toSend.push(elt.toList()); + mesh.index ++; + } else { + break; } + } - socket.emit('elements', toSend); + console.log(toSend.length); + socket.emit('elements', toSend); - if (!line) { - // socket.emit('finished'); - socket.disconnect(); - } + if (!elt) { + socket.disconnect(); + } - - }); }); }); }