A lot of work, cleaning streaming
This commit is contained in:
278
geo/Mesh.js
278
geo/Mesh.js
@@ -1,152 +1,71 @@
|
||||
var fs = require('fs');
|
||||
var mesh = {};
|
||||
|
||||
var geo = {};
|
||||
|
||||
geo.MeshStreamer = function(path, callback) {
|
||||
// Mesh
|
||||
mesh.Mesh = function() {
|
||||
this.vertices = [];
|
||||
this.textureCoords = [];
|
||||
this.faces = [];
|
||||
this.orderedElements = [];
|
||||
this.index = 0;
|
||||
|
||||
if (path !== undefined) {
|
||||
var self = this;
|
||||
this.loadFromFile(path, function() {
|
||||
|
||||
self.orderedElements = self.tryMerge();
|
||||
|
||||
if (typeof callback === 'function')
|
||||
callback();
|
||||
|
||||
});
|
||||
}
|
||||
this.texCoords = [];
|
||||
this.faceIndex = 0;
|
||||
this.material = null;
|
||||
}
|
||||
|
||||
geo.MeshStreamer.prototype.loadFromFile = function(path, callback) {
|
||||
var self = this;
|
||||
fs.readFile(path, function(err, data) {
|
||||
mesh.Mesh.prototype.addVertex = function(vertex) {
|
||||
|
||||
// Get lines from file
|
||||
var lines = data.toString('utf-8').split("\n");
|
||||
|
||||
// For each line
|
||||
for (var i = 0; i < lines.length; i++) {
|
||||
|
||||
var line = lines[i];
|
||||
|
||||
if (line[0] === 'v') {
|
||||
|
||||
if (line[1] === 't') {
|
||||
|
||||
// Texture coord
|
||||
var texCoord = self.textureCoords[self.textureCoords.push(new geo.TextureCoord(line)) - 1];
|
||||
self.orderedElements.push(texCoord);
|
||||
|
||||
} else if (line[1] === 'n') {
|
||||
|
||||
// Ignore normals
|
||||
|
||||
} else {
|
||||
|
||||
// Just a simple vertex
|
||||
var vertex = self.vertices[self.vertices.push(new geo.Vertex(line)) - 1];
|
||||
self.orderedElements.push(vertex);
|
||||
|
||||
}
|
||||
|
||||
} else if (line[0] === 'f') {
|
||||
|
||||
// Create face
|
||||
var face = self.faces[self.faces.push(new geo.Face(line)) - 1];
|
||||
self.orderedElements.push(face);
|
||||
|
||||
// Check
|
||||
// if (face.max() >= self.vertices.length || face.maxTexture() >= self.textureCoords.length) {
|
||||
// console.log("Error");
|
||||
// }
|
||||
|
||||
} else if (line[0] === 'u') {
|
||||
|
||||
// usemtl
|
||||
self.orderedElements.push(new geo.Usemtl(line));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (typeof callback === 'function') {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
geo.MeshStreamer.prototype.tryMerge = function() {
|
||||
if (this.faces[0].aTexture !== undefined) {
|
||||
return this.orderedElements;
|
||||
if (vertex instanceof mesh.Vertex) {
|
||||
this.vertices.push(vertex);
|
||||
} else if (typeof vertex === 'string' || vertex instanceof String) {
|
||||
this.vertices.push(new mesh.Vertex(vertex));
|
||||
} else {
|
||||
return this.merge();
|
||||
console.error("Can only add vertex from mesh.Vertex or string");
|
||||
return;
|
||||
}
|
||||
|
||||
return this.vertices[this.vertices.length - 1];
|
||||
}
|
||||
|
||||
mesh.Mesh.prototype.addFaces = function(face) {
|
||||
this.index = this.faces.length;
|
||||
|
||||
var faces;
|
||||
|
||||
if (face instanceof mesh.Face) {
|
||||
this.faces.push(face);
|
||||
} else if (typeof face === 'string' || face instanceof String) {
|
||||
faces = parseFace(face);
|
||||
this.faces = this.faces.concat(faces);
|
||||
} else {
|
||||
console.error("Can only add face from mesh.Face or string");
|
||||
return;
|
||||
}
|
||||
|
||||
if (faces === undefined) {
|
||||
return this.faces[this.faces.length - 1];
|
||||
} else {
|
||||
return faces;
|
||||
}
|
||||
}
|
||||
|
||||
geo.MeshStreamer.prototype.merge = function(callback) {
|
||||
// Gives for each vertex the indices of the faces in which it is present
|
||||
var vertexFace = [];
|
||||
|
||||
// Result variable
|
||||
var orderedElements = [];
|
||||
|
||||
// For each vertex
|
||||
for (var i = 0; i < this.vertices.length; i++) {
|
||||
|
||||
// Init the list of faces where this vertex is
|
||||
vertexFace.push([]);
|
||||
mesh.Mesh.prototype.addTexCoord = function(texCoord) {
|
||||
this.index = this.texCoords.length;
|
||||
|
||||
if (texCoord instanceof mesh.TexCoord) {
|
||||
this.texCoords.push(texCoord);
|
||||
} else if (typeof texCoord === 'string' || texCoord instanceof String) {
|
||||
this.texCoords.push(new mesh.TexCoord(texCoord));
|
||||
} else {
|
||||
console.error("Can only add texCoord from mesh.TexCoord or string");
|
||||
return;
|
||||
}
|
||||
|
||||
// For each face
|
||||
for (var i = 0; i < this.faces.length; i++) {
|
||||
|
||||
var face = this.faces[i];
|
||||
|
||||
// For each vertex of the face, and the face to the list of the correspondant vertex
|
||||
vertexFace[face.a].push(i);
|
||||
vertexFace[face.b].push(i);
|
||||
vertexFace[face.c].push(i);
|
||||
|
||||
if (vertexFace[face.d]) {
|
||||
vertexFace[face.d].push(i);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// For each vertex
|
||||
for (var vertex = 0; vertex < this.vertices.length; vertex++) {
|
||||
|
||||
// Print the vertex
|
||||
orderedElements.push(this.vertices[vertex]);
|
||||
|
||||
// For each face that contains this vertex
|
||||
for (var face = 0; face < vertexFace[vertex].length; face++) {
|
||||
|
||||
var faceToAdd = this.faces[vertexFace[vertex][face]];
|
||||
|
||||
// If the face can be given (that means that all vertices have already been pushed)
|
||||
if (faceToAdd.max() <= vertex) {
|
||||
|
||||
// Add it now
|
||||
orderedElements.push(faceToAdd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof callback === 'function')
|
||||
callback();
|
||||
|
||||
return orderedElements;
|
||||
|
||||
return this.texCoords[this.texCoords.length - 1];
|
||||
}
|
||||
|
||||
geo.Vertex = function() {
|
||||
mesh.Mesh.prototype.isFinished = function() {
|
||||
return this.faces.length === this.faceIndex;
|
||||
}
|
||||
|
||||
// Vertex
|
||||
mesh.Vertex = function() {
|
||||
if (typeof arguments[0] === 'string' || arguments[0] instanceof String) {
|
||||
var split = arguments[0].replace(/\s+/g, " ").split(' ');
|
||||
this.x = parseFloat(split[1]);
|
||||
@@ -156,15 +75,17 @@ geo.Vertex = function() {
|
||||
this.sent = false;
|
||||
}
|
||||
|
||||
geo.Vertex.prototype.toList = function() {
|
||||
return ['v', this.x, this.y, this.z];
|
||||
mesh.Vertex.prototype.toList = function() {
|
||||
return ['v', this.index, this.x, this.y, this.z];
|
||||
}
|
||||
|
||||
geo.Vertex.prototype.toString = function() {
|
||||
mesh.Vertex.prototype.toString = function() {
|
||||
return 'v ' + this.x + ' ' + this.y + ' ' + this.z;
|
||||
}
|
||||
|
||||
geo.TextureCoord = function() {
|
||||
|
||||
// TexCoord : texture coordinates
|
||||
mesh.TexCoord = function() {
|
||||
if (typeof arguments[0] === 'string' || arguments[0] instanceof String) {
|
||||
var split = arguments[0].replace(/\s+/g, " ").split(' ');
|
||||
this.x = parseFloat(split[1]);
|
||||
@@ -173,15 +94,17 @@ geo.TextureCoord = function() {
|
||||
this.sent = false;
|
||||
}
|
||||
|
||||
geo.TextureCoord.prototype.toList = function() {
|
||||
return ['vt', this.x, this.y];
|
||||
mesh.TexCoord.prototype.toList = function() {
|
||||
return ['vt', this.index, this.x, this.y];
|
||||
}
|
||||
|
||||
geo.TextureCoord.prototype.toString = function() {
|
||||
mesh.TexCoord.prototype.toString = function() {
|
||||
return 'vt ' + this.x + ' ' + this.y;
|
||||
}
|
||||
|
||||
geo.Face = function() {
|
||||
|
||||
// Face
|
||||
mesh.Face = function() {
|
||||
if (typeof arguments[0] === 'string' || arguments[0] instanceof String) {
|
||||
if (arguments[0].indexOf('/') === -1) {
|
||||
// No / : easy win : "f 1 2 3" or "f 1 2 3 4"
|
||||
@@ -190,9 +113,6 @@ geo.Face = function() {
|
||||
this.b = parseInt(split[2]) - 1;
|
||||
this.c = parseInt(split[3]) - 1;
|
||||
|
||||
if (split.length === 5)
|
||||
this.d = parseInt(split[4]) - 1;
|
||||
|
||||
} else {
|
||||
// There might be textures coords
|
||||
var split = arguments[0].replace(/\s+/g, ' ').trim().split(' ');
|
||||
@@ -210,18 +130,38 @@ geo.Face = function() {
|
||||
this.b = parseInt(split2[vIndex]) - 1; this.bTexture = parseInt(split2[tIndex]) - 1;
|
||||
this.c = parseInt(split3[vIndex]) - 1; this.cTexture = parseInt(split3[tIndex]) - 1;
|
||||
|
||||
if (split.length === 5) {
|
||||
var split4 = split[4].split('/');
|
||||
this.d = parseInt(split4[vIndex]) - 1; this.dTexture = parseInt(split4[tIndex]) - 1;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this.sent = false;
|
||||
|
||||
}
|
||||
|
||||
geo.Face.prototype.max = function() {
|
||||
|
||||
var parseFace = function(arg) {
|
||||
var split = arg.split(' ');
|
||||
var ret = [];
|
||||
|
||||
// Face3
|
||||
if (split.length >= 4) {
|
||||
ret.push(new mesh.Face(arg));
|
||||
}
|
||||
|
||||
if (split.length >= 5) {
|
||||
ret.push(new mesh.Face(
|
||||
[
|
||||
split[0],
|
||||
split[1],
|
||||
split[3],
|
||||
split[4]
|
||||
].join(' ')
|
||||
));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
mesh.Face.prototype.max = function() {
|
||||
if (this.d !== undefined) {
|
||||
return Math.max(this.a, this.b, this.c, this.d);
|
||||
} else {
|
||||
@@ -229,7 +169,7 @@ geo.Face.prototype.max = function() {
|
||||
}
|
||||
}
|
||||
|
||||
geo.Face.prototype.maxTexture = function() {
|
||||
mesh.Face.prototype.maxTexture = function() {
|
||||
if (this.dTexture) {
|
||||
return Math.max(this.aTexture, this.bTexture, this.cTexture, this.dTexture);
|
||||
} else {
|
||||
@@ -237,38 +177,40 @@ geo.Face.prototype.maxTexture = function() {
|
||||
}
|
||||
}
|
||||
|
||||
geo.Face.prototype.toList = function() {
|
||||
var l = ['f', this.a, this.b, this.c];
|
||||
if (this.d !== undefined)
|
||||
l.push(this.d);
|
||||
mesh.Face.prototype.toList = function() {
|
||||
var l = ['f', this.index, this.a, this.b, this.c];
|
||||
|
||||
if (this.aTexture !== undefined) {
|
||||
l.push(this.aTexture);
|
||||
l.push(this.bTexture);
|
||||
l.push(this.cTexture);
|
||||
}
|
||||
// if (this.d !== undefined)
|
||||
// l.push(this.d);
|
||||
|
||||
if (this.dTexture !== undefined)
|
||||
l.push(this.dTexture);
|
||||
// if (this.aTexture !== undefined) {
|
||||
// l.push(this.aTexture);
|
||||
// l.push(this.bTexture);
|
||||
// l.push(this.cTexture);
|
||||
// }
|
||||
|
||||
// if (this.dTexture !== undefined)
|
||||
// l.push(this.dTexture);
|
||||
|
||||
return l;
|
||||
}
|
||||
|
||||
geo.Face.prototype.toString = function() {
|
||||
mesh.Face.prototype.toString = function() {
|
||||
return 'f ' + this.a + ' ' + this.b + ' ' + this.c + (this.d !== undefined ? ' ' + this.d : '');
|
||||
}
|
||||
|
||||
geo.Usemtl = function() {
|
||||
// Material
|
||||
mesh.Material = function() {
|
||||
var split = arguments[0].replace(/\s+/g, ' ').split(' ');
|
||||
this.name = split[1];
|
||||
}
|
||||
|
||||
geo.Usemtl.prototype.toString = function() {
|
||||
mesh.Material.prototype.toString = function() {
|
||||
return 'usemtl ' + this.name;
|
||||
}
|
||||
|
||||
geo.Usemtl.prototype.toList = function() {
|
||||
mesh.Material.prototype.toList = function() {
|
||||
return ['u', this.name];
|
||||
}
|
||||
|
||||
module.exports = geo;
|
||||
module.exports = mesh;
|
||||
|
||||
173
geo/MeshStreamer.js
Normal file
173
geo/MeshStreamer.js
Normal file
@@ -0,0 +1,173 @@
|
||||
var fs = require('fs');
|
||||
var mesh = require('./Mesh.js');
|
||||
|
||||
var geo = {};
|
||||
|
||||
geo.MeshStreamer = function(path, callback) {
|
||||
// Different parts of a obj (a mesh per material)
|
||||
this.meshes = [];
|
||||
|
||||
// In meshes, vertices and texture coords are shared
|
||||
this.vertices = [];
|
||||
this.faces = [];
|
||||
this.texCoords = [];
|
||||
|
||||
// Chunk size
|
||||
this.chunk = 1000;
|
||||
|
||||
if (path !== undefined) {
|
||||
var self = this;
|
||||
this.loadFromFile(path, function() {
|
||||
|
||||
if (typeof callback === 'function')
|
||||
callback();
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
geo.MeshStreamer.prototype.loadFromFile = function(path, callback) {
|
||||
var self = this;
|
||||
fs.readFile(path, function(err, data) {
|
||||
|
||||
var currentMesh;
|
||||
|
||||
// Get lines from file
|
||||
var lines = data.toString('utf-8').split("\n");
|
||||
|
||||
// For each line
|
||||
for (var i = 0; i < lines.length; i++) {
|
||||
|
||||
var line = lines[i];
|
||||
|
||||
if (line[0] === 'v') {
|
||||
|
||||
if (line[1] === 't') {
|
||||
|
||||
// Texture coord
|
||||
self.texCoords.push(new mesh.TexCoord(line));
|
||||
|
||||
} else if (line[1] === 'n') {
|
||||
|
||||
// Ignore normals
|
||||
|
||||
} else {
|
||||
|
||||
// Just a simple vertex
|
||||
if (currentMesh === undefined) {
|
||||
// Chances are that we won't use any material in this case
|
||||
currentMesh = new mesh.Mesh();
|
||||
self.meshes.push(currentMesh);
|
||||
}
|
||||
var vertex = currentMesh.addVertex(line);
|
||||
vertex.index = self.vertices.length;
|
||||
self.vertices.push(vertex);
|
||||
|
||||
}
|
||||
|
||||
} else if (line[0] === 'f') {
|
||||
|
||||
// Create faces (two if Face4)
|
||||
var faces = currentMesh.addFaces(line);
|
||||
|
||||
faces[0].index = self.faces.length;
|
||||
self.faces.push(faces[0]);
|
||||
if (faces.length === 2) {
|
||||
faces[1].index = self.faces.length;
|
||||
self.faces.push(faces[1]);
|
||||
}
|
||||
|
||||
} else if (line[0] === 'u') {
|
||||
|
||||
// usemtl
|
||||
|
||||
// Create a new mesh
|
||||
currentMesh = new mesh.Mesh();
|
||||
self.meshes.push(currentMesh);
|
||||
currentMesh.material = mesh.Material(line);
|
||||
self.orderedElements.push(new mesh.Usemtl(line));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (typeof callback === 'function') {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
geo.MeshStreamer.prototype.start = function(socket) {
|
||||
|
||||
this.meshIndex = -1;
|
||||
|
||||
var self = this;
|
||||
|
||||
socket.on('request', function(path) {
|
||||
console.log('Asking for ' + path);
|
||||
|
||||
var regex = /.*\.\..*/;
|
||||
|
||||
if (regex.test(path)) {
|
||||
socket.emit('refused');
|
||||
socket.disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
self.loadFromFile(path, function() {
|
||||
socket.emit('ok');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
socket.on('next', function() {
|
||||
|
||||
// Send next elements
|
||||
var currentMesh = self.meshes[self.meshIndex];
|
||||
|
||||
if (currentMesh === undefined || currentMesh.isFinished()) {
|
||||
currentMesh = self.meshes[++self.meshIndex];
|
||||
|
||||
if (currentMesh === undefined) {
|
||||
socket.emit('finished');
|
||||
socket.disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
socket.emit(
|
||||
'usemtl',
|
||||
currentMesh.material,
|
||||
currentMesh.vertices.length,
|
||||
currentMesh.faces.length,
|
||||
currentMesh.texCoords.length
|
||||
);
|
||||
|
||||
} else {
|
||||
|
||||
var data = [];
|
||||
|
||||
for (var limit = Math.min(currentMesh.faceIndex + self.chunk, currentMesh.faces.length);
|
||||
currentMesh.faceIndex < limit;
|
||||
currentMesh.faceIndex++)
|
||||
{
|
||||
|
||||
var currentFace = currentMesh.faces[currentMesh.faceIndex];
|
||||
var vertex1 = self.vertices[currentFace.a];
|
||||
var vertex2 = self.vertices[currentFace.b];
|
||||
var vertex3 = self.vertices[currentFace.c];
|
||||
|
||||
if (!vertex1.sent) { data.push(vertex1.toList()); vertex1.sent = true;}
|
||||
if (!vertex2.sent) { data.push(vertex2.toList()); vertex2.sent = true;}
|
||||
if (!vertex3.sent) { data.push(vertex3.toList()); vertex3.sent = true;}
|
||||
|
||||
data.push(currentFace.toList()); currentFace.sent = true;
|
||||
}
|
||||
|
||||
// Emit self.chunk faces (and the corresponding vertices if not emitted)
|
||||
socket.emit('elements', data);
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = geo;
|
||||
Reference in New Issue
Block a user