Progressive by buffer

This commit is contained in:
Thomas FORGIONE 2015-06-10 11:50:47 +02:00
parent c28de16a87
commit 595301bc4d
2 changed files with 83 additions and 40 deletions

View File

@ -18,6 +18,75 @@ var ProgressiveLoader = function(res, scene) {
socket.emit('next');
});
socket.on('elements', function(arr) {
for (var i = 0; i < arr.length; i++) {
var line = arr[i];
// console.log(line);
if (line[0] === 'v') {
var elts = line.split(' ');
mesh.geometry.vertices.push(new THREE.Vector3(
parseFloat(elts[1]),
parseFloat(elts[2]),
parseFloat(elts[3])
));
mesh.geometry.verticesNeedUpdate = true;
} else if (line[0] === 'f') {
var elts = line.split(' ');
elts[1] = parseInt(elts[1]) - 1;
elts[2] = parseInt(elts[2]) - 1;
elts[3] = parseInt(elts[3]) - 1;
if (elts[4])
elts[4] = parseInt(elts[4]) - 1;
mesh.geometry.faces.push(new THREE.Face3(
parseInt(elts[1]),
parseInt(elts[2]),
parseInt(elts[3])
));
if (elts[4]) {
mesh.geometry.faces.push(new THREE.Face3(
parseInt(elts[1]),
parseInt(elts[3]),
parseInt(elts[4])
));
}
if (!added) {
scene.add(mesh);
added = true;
}
}
}
mesh.geometry.computeFaceNormals();
mesh.geometry.groupsNeedUpdate = true;
mesh.geometry.elementsNeedUpdate = true;
mesh.geometry.normalsNeedUpdate = true;
if (!finished) {
socket.emit('next');
} else {
console.log("Finished");
}
});
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]);

View File

@ -29,51 +29,25 @@ module.exports = function(io) {
fs.readFile(path, function(err, data) {
var lines = data.toString('utf-8').split("\n");
var line = lines[index];
var toSend = [];
/// while (line && line.length === 0) {
/// line = lines[++index];
/// }
if (!line) {
socket.emit('finished');
return;
}
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++;
for (var i = 0; i < 50; i++) {
while (line && line[0] !== 'f') {
toSend.push(line);
line = lines[++index];
}
socket.emit('face', arr);
index++;
return;
if (line && line[0] === 'f') {
toSend.push(line);
line = lines[++index];
}
}
if (!line) {
socket.emit('finished');
}
socket.emit('none');
// socket.disconnect();
socket.emit('elements', toSend);
});
});
});