This commit is contained in:
Thomas FORGIONE 2015-06-10 11:59:02 +02:00
parent 2e4a30d93d
commit 821ca9ef10
2 changed files with 14 additions and 51 deletions

View File

@ -2,24 +2,31 @@ var ProgressiveLoader = function(res, scene) {
// Create mesh // Create mesh
var geometry = new THREE.Geometry(); var geometry = new THREE.Geometry();
geometry.dynamic = true; geometry.dynamic = true;
var material = new THREE.MeshLambertMaterial(); var material = new THREE.MeshLambertMaterial();
material.color.setRGB(1,0,0); material.color.setRGB(1,0,0);
material.side = THREE.DoubleSide; material.side = THREE.DoubleSide;
var mesh = new THREE.Mesh(geometry, material); var mesh = new THREE.Mesh(geometry, material);
mesh.up = new THREE.Vector3(0,0,1); mesh.up = new THREE.Vector3(0,0,1);
var added = false; var added = false;
var finished = false; var finished = false;
var socket = io(); var socket = io();
// Init streaming with socket
socket.emit('request', res); socket.emit('request', res);
// When server's ready, start asking for the mesh
socket.on('ok', function() { socket.on('ok', function() {
socket.emit('next'); socket.emit('next');
}); });
// When receiving elements
socket.on('elements', function(arr) { socket.on('elements', function(arr) {
// We'll receive an array of string (obj)
for (var i = 0; i < arr.length; i++) { for (var i = 0; i < arr.length; i++) {
var line = arr[i]; var line = arr[i];
@ -55,6 +62,7 @@ var ProgressiveLoader = function(res, scene) {
parseInt(elts[3]) parseInt(elts[3])
)); ));
// If the face has 4 vertices, create second triangle
if (elts[4]) { if (elts[4]) {
mesh.geometry.faces.push(new THREE.Face3( mesh.geometry.faces.push(new THREE.Face3(
@ -65,6 +73,7 @@ var ProgressiveLoader = function(res, scene) {
} }
// Add mesh to scene one there are a few faces in it
if (!added) { if (!added) {
scene.add(mesh); scene.add(mesh);
added = true; added = true;
@ -72,7 +81,6 @@ var ProgressiveLoader = function(res, scene) {
} }
} }
mesh.geometry.computeFaceNormals(); mesh.geometry.computeFaceNormals();
@ -87,47 +95,6 @@ var ProgressiveLoader = function(res, scene) {
} }
}); });
socket.on('vertex', function(arr) {
// console.log('v(', arr[0], ')', arr[1], arr[2], arr[3]);
mesh.geometry.vertices[arr[0]] = new THREE.Vector3(arr[1], arr[2], arr[3]);
mesh.geometry.verticesNeedUpdate = true;
socket.emit('next');
});
socket.on('face', function(arr) {
// console.log('f', arr[0], arr[1], arr[2]);
mesh.geometry.faces.push(new THREE.Face3(arr[0], arr[1], arr[2]));
// if (arr[0] >= mesh.geometry.vertices.length
// || arr[1] >= mesh.geometry.vertices.length
// || arr[2] >= mesh.geometry.vertices.length) {
// console.log("Error");
// }
if (arr[3])
mesh.geometry.faces.push(new THREE.Face3(arr[0], arr[2], arr[3]));
if (!added) {
scene.add(mesh);
added = true;
}
// Compute the normal
mesh.geometry.verticesNeedUpdate = true;
mesh.geometry.groupsNeedUpdate = true;
mesh.geometry.elementsNeedUpdate = true;
mesh.geometry.normalsNeedUpdate = true;
if (finished) {
console.log("Finished");
} else {
socket.emit('next');
}
mesh.geometry.computeFaceNormals();
});
socket.on('finished', function(arg) { socket.on('finished', function(arg) {
finished = true; finished = true;
mesh.faceNumber = arg; mesh.faceNumber = arg;
@ -144,9 +111,5 @@ var ProgressiveLoader = function(res, scene) {
// scene.add(mesh); // scene.add(mesh);
}); });
socket.on('none', function() {
socket.emit('next');
});
return mesh; return mesh;
} }

View File

@ -9,14 +9,14 @@ module.exports = function(io) {
var vIndex = 0; var vIndex = 0;
var fIndex = 0; var fIndex = 0;
console.log(socket.conn.remoteAddress + " connected !"); // console.log(socket.conn.remoteAddress + " connected !");
socket.on('disconnect', function() { // socket.on('disconnect', function() {
console.log(socket.conn.remoteAddress + " disconnected !"); // console.log(socket.conn.remoteAddress + " disconnected !");
}); // });
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'; path = 'static/data/spheres/' + res + '.obj.obj';