2015-07-03 13:27:05 +02:00
|
|
|
var Log = require('../lib/NodeLog.js');
|
|
|
|
|
2015-06-29 09:50:26 +02:00
|
|
|
/**
|
2015-06-29 15:41:18 +02:00
|
|
|
* Represents a mesh. All meshes are loaded once in geo.availableMesh to avoid
|
2015-06-29 09:50:26 +02:00
|
|
|
* loading at each mesh request
|
|
|
|
* @constructor
|
2015-06-29 15:41:18 +02:00
|
|
|
* @memberOf geo
|
2015-06-29 09:50:26 +02:00
|
|
|
*/
|
2015-07-02 15:05:59 +02:00
|
|
|
geo.MeshContainer = function(path, callback) {
|
2015-06-29 09:50:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* array of each part of the mesh
|
2015-06-29 15:41:18 +02:00
|
|
|
* @type {geo.Mesh[]}
|
2015-06-29 09:50:26 +02:00
|
|
|
*/
|
|
|
|
this.meshes = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* array of the vertices of the meshes (all merged)
|
2015-06-29 15:41:18 +02:00
|
|
|
* @type {geo.Vertex[]}
|
2015-06-29 09:50:26 +02:00
|
|
|
*/
|
|
|
|
this.vertices = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* array of the faces of the meshes (all merged)
|
2015-06-29 15:41:18 +02:00
|
|
|
* @type {geo.Face[]}
|
2015-06-29 09:50:26 +02:00
|
|
|
*/
|
|
|
|
this.faces = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* array of the normals of the meshes (all merged)
|
2015-06-29 15:41:18 +02:00
|
|
|
* @type {geo.Normal[]}
|
2015-06-29 09:50:26 +02:00
|
|
|
*/
|
|
|
|
this.normals = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* array of the texture coordinates (all merged)
|
2015-06-29 15:41:18 +02:00
|
|
|
* @type {geo.TexCoord[]}
|
2015-06-29 09:50:26 +02:00
|
|
|
*/
|
|
|
|
this.texCoords = [];
|
|
|
|
|
2015-07-07 11:47:21 +02:00
|
|
|
/**
|
|
|
|
* Number of elements to stream in the mesh
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
2015-07-07 14:40:32 +02:00
|
|
|
this.numberOfFaces = 0;
|
2015-07-07 11:47:21 +02:00
|
|
|
|
2015-07-02 15:05:59 +02:00
|
|
|
this.callback = callback;
|
|
|
|
|
2015-06-29 09:50:26 +02:00
|
|
|
if (path !== undefined) {
|
|
|
|
|
|
|
|
this.loadFromFile(path);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-29 09:50:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a obj file
|
|
|
|
* @param {string} path the path to the file
|
|
|
|
*/
|
2015-06-29 15:41:18 +02:00
|
|
|
geo.MeshContainer.prototype.loadFromFile = function(path) {
|
2015-06-29 09:50:26 +02:00
|
|
|
var self = this;
|
|
|
|
|
2015-07-02 15:05:59 +02:00
|
|
|
fs.readFile(path, {encoding: 'utf-8'}, function(err, data) {
|
2015-06-29 09:50:26 +02:00
|
|
|
|
|
|
|
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
|
2015-06-29 15:41:18 +02:00
|
|
|
var texCoord = new geo.TexCoord(line);
|
2015-06-29 09:50:26 +02:00
|
|
|
texCoord.index = self.texCoords.length;
|
|
|
|
self.texCoords.push(texCoord);
|
|
|
|
|
|
|
|
} else if (line[1] === 'n') {
|
|
|
|
|
2015-06-29 15:41:18 +02:00
|
|
|
var normal = new geo.Normal(line);
|
2015-06-29 09:50:26 +02:00
|
|
|
normal.index = self.normals.length;
|
|
|
|
self.normals.push(normal);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Just a simple vertex
|
2015-06-29 15:41:18 +02:00
|
|
|
var vertex = new geo.Vertex(line);
|
2015-06-29 09:50:26 +02:00
|
|
|
vertex.index = self.vertices.length;
|
|
|
|
self.vertices.push(vertex);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (line[0] === 'f') {
|
|
|
|
|
2015-07-07 14:40:32 +02:00
|
|
|
self.numberOfFaces++;
|
2015-07-07 11:47:21 +02:00
|
|
|
|
2015-06-29 09:50:26 +02:00
|
|
|
// Create mesh if it doesn't exist
|
|
|
|
if (currentMesh === undefined) {
|
2015-06-29 15:41:18 +02:00
|
|
|
currentMesh = new geo.Mesh();
|
2015-06-29 09:50:26 +02:00
|
|
|
self.meshes.push(currentMesh);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create faces (two if Face4)
|
|
|
|
var faces = currentMesh.addFaces(line);
|
|
|
|
|
|
|
|
faces[0].index = self.faces.length;
|
|
|
|
faces[0].meshIndex = self.meshes.length - 1;
|
|
|
|
self.faces.push(faces[0]);
|
|
|
|
|
|
|
|
if (faces.length === 2) {
|
|
|
|
|
2015-07-07 14:40:32 +02:00
|
|
|
self.numberOfFaces++;
|
2015-06-29 09:50:26 +02:00
|
|
|
faces[1].index = self.faces.length;
|
|
|
|
faces[1].meshIndex = self.meshes.length - 1;
|
|
|
|
self.faces.push(faces[1]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (line[0] === 'u') {
|
|
|
|
|
|
|
|
// usemtl
|
|
|
|
// If a current mesh exists, finish it
|
|
|
|
|
|
|
|
// Create a new mesh
|
2015-06-29 15:41:18 +02:00
|
|
|
currentMesh = new geo.Mesh();
|
2015-06-29 09:50:26 +02:00
|
|
|
self.meshes.push(currentMesh);
|
2015-06-29 15:41:18 +02:00
|
|
|
currentMesh.material = (new geo.Material(line)).name;
|
2015-06-29 09:50:26 +02:00
|
|
|
// console.log(currentMesh.material);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-02 15:05:59 +02:00
|
|
|
|
|
|
|
if (typeof self.callback === 'function') {
|
|
|
|
|
|
|
|
self.callback();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-29 09:50:26 +02:00
|
|
|
|
2015-07-02 15:05:59 +02:00
|
|
|
function trySetLoaded() {
|
|
|
|
for (var name in availableMeshNames) {
|
|
|
|
|
|
|
|
if (availableMeshNames[name] === false) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-03 13:27:05 +02:00
|
|
|
Log.ready("All meshes are ready");
|
2015-07-02 15:05:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var availableMeshNames = {
|
|
|
|
'/static/data/castle/princess peaches castle (outside).obj':false,
|
|
|
|
'/static/data/mountain/coocoolmountain.obj':false,
|
|
|
|
'/static/data/whomp/Whomps Fortress.obj':false,
|
|
|
|
'/static/data/bobomb/bobomb battlefeild.obj':false,
|
|
|
|
'/static/data/sponza/sponza.obj':false
|
|
|
|
};
|
2015-06-29 09:50:26 +02:00
|
|
|
|
|
|
|
for (var i = 1; i < 26; i++) {
|
|
|
|
|
2015-07-02 15:05:59 +02:00
|
|
|
availableMeshNames['/static/data/spheres/' + i + '.obj'] = false;
|
2015-06-29 09:50:26 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-29 15:41:18 +02:00
|
|
|
geo.availableMeshes = {};
|
2015-06-29 09:50:26 +02:00
|
|
|
|
2015-07-06 11:26:19 +02:00
|
|
|
function pushMesh(name) {
|
|
|
|
|
|
|
|
geo.availableMeshes[name] = new geo.MeshContainer(name.substring(1, name.length), function() {
|
|
|
|
availableMeshNames[name] = true;
|
|
|
|
trySetLoaded();
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-02 15:05:59 +02:00
|
|
|
for (var name in availableMeshNames) {
|
2015-06-29 09:50:26 +02:00
|
|
|
|
2015-07-06 11:26:19 +02:00
|
|
|
pushMesh(name);
|
2015-06-29 09:50:26 +02:00
|
|
|
|
|
|
|
}
|