Improved progressive sphere

This commit is contained in:
Thomas FORGIONE 2015-06-10 11:24:59 +02:00
parent 7993c02109
commit 73371aa42f
2 changed files with 81 additions and 43 deletions

View File

@ -8,25 +8,33 @@ var ProgressiveLoader = function(res, scene) {
var mesh = new THREE.Mesh(geometry, material);
mesh.up = new THREE.Vector3(0,0,1);
var added = false;
var finished = false;
var socket = io();
socket.emit('request', res);
socket.on('ok', function() {
socket.emit('next');
});
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) {
// if (arr[0] >= mesh.geometry.vertices.length
// || arr[1] >= mesh.geometry.vertices.length
// || arr[2] >= mesh.geometry.vertices.length) {
console.log("Error");
}
// console.log("Error");
// }
if (arr[3])
mesh.geometry.faces.push(new THREE.Face3(arr[0], arr[2], arr[3]));
@ -38,18 +46,21 @@ var ProgressiveLoader = function(res, scene) {
// Compute the normal
mesh.geometry.computeFaceNormals();
mesh.geometry.verticesNeedUpdate = true;
mesh.geometry.groupsNeedUpdate = true;
mesh.geometry.elementsNeedUpdate = true;
mesh.geometry.normalsNeedUpdate = true;
if (mesh.faceNumber && mesh.faceNumber === mesh.geometry.faces.length) {
if (finished) {
console.log("Finished");
} else {
socket.emit('next');
}
mesh.geometry.computeFaceNormals();
});
socket.on('finished', function(arg) {
finished = true;
mesh.faceNumber = arg;
//mesh.geometry.computeFaceNormals();
mesh.geometry.verticesNeedUpdate = true;
@ -64,5 +75,9 @@ var ProgressiveLoader = function(res, scene) {
// scene.add(mesh);
});
socket.on('none', function() {
socket.emit('next');
});
return mesh;
}

View File

@ -3,53 +3,76 @@ var sleep = require('sleep');
module.exports = function(io) {
io.on('connection', function(socket) {
console.log(socket + " connected !");
var index = 0;
var path;
var vIndex = 0;
var fIndex = 0;
console.log(socket.conn.remoteAddress + " connected !");
socket.on('disconnect', function() {
console.log(socket + " disconnected !");
console.log(socket.conn.remoteAddress + " disconnected !");
});
socket.on("request", function(res) {
console.log('Asking for static/data/spheres/' + res + '.obj');
fs.readFile('static/data/spheres/' + res + '.obj.obj', function(err, data) {
path = 'static/data/spheres/' + res + '.obj.obj';
socket.emit('ok');
});
socket.on('next', function() {
fs.readFile(path, function(err, data) {
var lines = data.toString('utf-8').split("\n");
var vIndex = 0;
var fIndex = 0;
for (i = 0, iMax = lines.length; i < iMax; i++ ) {
if (lines[i][0] === 'v') {
var arr = lines[i].split(" ");
arr[0] = vIndex++;
arr[1] = parseFloat(arr[1]);
arr[2] = parseFloat(arr[2]);
arr[3] = parseFloat(arr[3]);
(function (arr) {
setTimeout(function() {
socket.emit('vertex', arr);
}, i*100);
})(arr);
} else if (lines[i][0] === 'f') {
fIndex++;
var arr = lines[i].split(" ");
arr.shift();
arr[0]--;
arr[1]--;
arr[2]--;
var line = lines[index];
if (arr[3]) {
arr[3]--;
fIndex++;
}
/// while (line && line.length === 0) {
/// line = lines[++index];
/// }
(function (arr) {
setTimeout(function() {
socket.emit('face', arr);
},i*100);
})(arr);
}
if (!line) {
socket.emit('finished');
return;
}
socket.emit('finished', fIndex);
if (line[0] === 'v') {
var arr = line.split(" ");
arr[0] = vIndex++;
arr[1] = parseFloat(arr[1]);
arr[2] = parseFloat(arr[2]);
arr[3] = parseFloat(arr[3]);
socket.emit('vertex', arr);
index++;
return;
} else if (line[0] === 'f') {
fIndex++;
var arr = line.split(" ");
arr.shift();
arr[0]--;
arr[1]--;
arr[2]--;
if (arr[3]) {
arr[3]--;
fIndex++;
}
socket.emit('face', arr);
index++;
return;
}
socket.emit('none');
// socket.disconnect();
});
});