2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Parse a list as it is sent by the server and gives a slightly more comprehensible result
|
|
|
|
* @private
|
|
|
|
*/
|
2015-06-17 17:11:23 +02:00
|
|
|
var _parseList2 = function(arr) {
|
2015-06-17 11:09:15 +02:00
|
|
|
|
|
|
|
var ret = {};
|
|
|
|
ret.index = arr[1];
|
|
|
|
|
|
|
|
if (arr[0] === 'v') {
|
|
|
|
ret.type = 'vertex';
|
|
|
|
ret.x = arr[2];
|
|
|
|
ret.y = arr[3];
|
|
|
|
ret.z = arr[4];
|
|
|
|
} else if (arr[0] === 'vt') {
|
|
|
|
ret.type = 'texCoord';
|
|
|
|
ret.x = arr[2];
|
|
|
|
ret.y = arr[3];
|
|
|
|
} else if (arr[0] === 'f') {
|
|
|
|
ret.type = 'face';
|
2015-06-17 17:11:23 +02:00
|
|
|
ret.mesh = arr[2];
|
2015-06-17 11:09:15 +02:00
|
|
|
|
|
|
|
// Only Face3 are allowed
|
2015-06-17 17:11:23 +02:00
|
|
|
vertexIndices = arr[3];
|
|
|
|
textureIndices = arr[4];
|
|
|
|
normalIndices = arr[5];
|
2015-06-17 11:09:15 +02:00
|
|
|
|
|
|
|
// Vertex indices
|
|
|
|
ret.a = vertexIndices[0];
|
|
|
|
ret.b = vertexIndices[1];
|
|
|
|
ret.c = vertexIndices[2];
|
|
|
|
|
|
|
|
// Texutre indices (if they exist)
|
|
|
|
if (textureIndices.length > 0) {
|
|
|
|
ret.aTexture = textureIndices[0];
|
|
|
|
ret.bTexture = textureIndices[1];
|
|
|
|
ret.cTexture = textureIndices[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normal indices (if they exist)
|
|
|
|
if (normalIndices.length > 0) {
|
|
|
|
ret.aNormal = normalIndices[0];
|
|
|
|
ret.bNormal = normalIndices[1];
|
|
|
|
ret.cNormal = normalIndices[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (arr[0] === 'vn') {
|
|
|
|
|
|
|
|
// Normal
|
|
|
|
ret.type = "normal";
|
|
|
|
ret.x = arr[2];
|
|
|
|
ret.y = arr[3];
|
|
|
|
ret.z = arr[4];
|
|
|
|
|
|
|
|
} else if (arr[0] === 'u') {
|
|
|
|
|
|
|
|
// usemtl
|
|
|
|
ret.index = -1;
|
|
|
|
ret.type = 'usemtl';
|
|
|
|
ret.materialName = arr[1];
|
|
|
|
ret.vLength = arr[2];
|
|
|
|
ret.fLength = arr[3];
|
|
|
|
ret.texCoordsExist = arr[4];
|
|
|
|
ret.normalsExist = arr[5];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Loads a mesh from socket.io
|
|
|
|
* @param {string} path path to the .obj file
|
|
|
|
* @param {THREE.Scene} scene to add the object
|
|
|
|
* @param {PointerCamera} camera the camera that will be sent to server for smart
|
|
|
|
* streaming (can be null, then the server will stream the mesh in the .obj
|
|
|
|
* order)
|
|
|
|
* @param {function} callback callback to call on the objects when they're created
|
|
|
|
* @constructor
|
|
|
|
*/
|
2015-06-17 17:11:23 +02:00
|
|
|
var ProgressiveLoaderGeometry = function(path, scene, camera, callback) {
|
2015-06-18 14:46:09 +02:00
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Path to the .obj file
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2015-06-23 14:05:40 +02:00
|
|
|
this.objPath = path;
|
2015-06-23 15:31:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Path to the folder where the textures are
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2015-06-17 11:09:15 +02:00
|
|
|
this.texturesPath = path.substring(0, path.lastIndexOf('/')) + '/';
|
2015-06-23 15:31:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Path to the .mtl file
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2015-06-17 11:09:15 +02:00
|
|
|
this.mtlPath = path.replace('.obj', '.mtl');
|
2015-06-23 15:31:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to the scene in which the object should be added
|
|
|
|
*/
|
2015-06-17 11:09:15 +02:00
|
|
|
this.scene = scene;
|
2015-06-23 15:31:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback to call on the object when they're created
|
|
|
|
*/
|
2015-06-17 11:09:15 +02:00
|
|
|
this.callback = callback;
|
2015-06-23 15:31:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Counter (not used)
|
|
|
|
* @private
|
|
|
|
*/
|
2015-06-17 11:09:15 +02:00
|
|
|
this.counter = 0;
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Group where the sub-objects will be added
|
|
|
|
* @type {THREE.Object3D}
|
|
|
|
*/
|
2015-06-17 11:09:15 +02:00
|
|
|
this.obj = new THREE.Object3D();
|
|
|
|
|
|
|
|
scene.add(this.obj);
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Array of the vertices of the mesh
|
|
|
|
* @type {THREE.Vector3[]}
|
|
|
|
*/
|
2015-06-17 11:09:15 +02:00
|
|
|
this.vertices = [];
|
2015-06-23 15:31:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of the texture coordinates of the mesh
|
|
|
|
* @type {THREE.Vector2[]}
|
|
|
|
*/
|
2015-06-17 11:09:15 +02:00
|
|
|
this.texCoords = [];
|
2015-06-23 15:31:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of the normal of the mesh
|
|
|
|
* @type {THREE.Vector3[]}
|
|
|
|
*/
|
2015-06-17 11:09:15 +02:00
|
|
|
this.normals = [];
|
2015-06-23 15:31:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of the UV mapping
|
|
|
|
* @description Each element is an array of 3 elements that are the indices
|
|
|
|
* of the element in <code>this.texCoords</code> that should be
|
|
|
|
* used as texture coordinates for the current vertex of the face
|
|
|
|
* @type {Number[][]}
|
|
|
|
*/
|
2015-06-17 11:09:15 +02:00
|
|
|
this.uvs = [];
|
2015-06-23 15:31:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of all the meshes that will be added to the main object
|
|
|
|
* @type {THREE.Mesh[]}
|
|
|
|
*/
|
2015-06-18 14:46:09 +02:00
|
|
|
this.meshes = [];
|
2015-06-17 11:09:15 +02:00
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Loader for the material file
|
|
|
|
* @type {THREE.MTLLoader}
|
|
|
|
*/
|
2015-06-17 11:09:15 +02:00
|
|
|
this.loader = new THREE.MTLLoader(this.texturesPath);
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Socket to connect to get the mesh
|
|
|
|
* @type {socket}
|
|
|
|
*/
|
2015-06-17 11:09:15 +02:00
|
|
|
this.socket = io();
|
2015-06-23 15:31:23 +02:00
|
|
|
|
2015-06-17 11:09:15 +02:00
|
|
|
this.initIOCallbacks();
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Reference to the camera
|
|
|
|
* @type {PointerCamera}
|
|
|
|
*/
|
2015-06-17 17:11:23 +02:00
|
|
|
this.camera = camera;
|
|
|
|
|
2015-06-17 11:09:15 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Starts the loading of the mesh
|
|
|
|
*/
|
2015-06-17 11:09:15 +02:00
|
|
|
ProgressiveLoaderGeometry.prototype.load = function() {
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.loader.load(self.mtlPath, function(materialCreator) {
|
|
|
|
|
|
|
|
self.materialCreator = materialCreator;
|
|
|
|
|
|
|
|
materialCreator.preload();
|
|
|
|
|
|
|
|
self.start();
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Will return a list representation of the camera (to be sent to the server)
|
|
|
|
*/
|
2015-06-17 17:11:23 +02:00
|
|
|
ProgressiveLoaderGeometry.prototype.getCamera = function() {
|
2015-06-19 11:24:37 +02:00
|
|
|
if (this.camera === null)
|
|
|
|
return null;
|
|
|
|
|
2015-06-26 09:22:32 +02:00
|
|
|
return this.toList();
|
2015-06-17 17:11:23 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Initializes the socket.io functions so that it can discuss with the server
|
|
|
|
*/
|
2015-06-17 11:09:15 +02:00
|
|
|
ProgressiveLoaderGeometry.prototype.initIOCallbacks = function() {
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.socket.on('ok', function() {
|
|
|
|
console.log('ok');
|
2015-06-18 16:58:07 +02:00
|
|
|
self.socket.emit('materials');
|
2015-06-17 11:09:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.socket.on('elements', function(arr) {
|
|
|
|
|
2015-06-18 11:46:24 +02:00
|
|
|
if (arr.length === 0) {
|
|
|
|
|
|
|
|
console.log("Empty array");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.log("Stuff received");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-17 11:09:15 +02:00
|
|
|
// console.log("Received elements for the " + (++self.counter) + "th time !");
|
|
|
|
for (var i = 0; i < arr.length; i++) {
|
|
|
|
|
2015-06-17 17:11:23 +02:00
|
|
|
var elt = _parseList2(arr[i]);
|
2015-06-17 11:09:15 +02:00
|
|
|
|
|
|
|
// console.log(elts);
|
|
|
|
if (elt.type === 'vertex') {
|
|
|
|
|
|
|
|
// New vertex arrived
|
2015-06-30 09:39:20 +02:00
|
|
|
|
|
|
|
// Fill the array of vertices with null vector (to avoid undefined)
|
2015-06-17 11:09:15 +02:00
|
|
|
while (elt.index > self.vertices.length) {
|
|
|
|
|
|
|
|
self.vertices.push(new THREE.Vector3());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
self.vertices[elt.index] = new THREE.Vector3(elt.x, elt.y, elt.z);
|
|
|
|
self.currentMesh.geometry.verticesNeedUpdate = true;
|
|
|
|
|
|
|
|
} else if (elt.type === 'texCoord') {
|
|
|
|
|
|
|
|
// New texCoord arrived
|
|
|
|
self.texCoords[elt.index] = new THREE.Vector2(elt.x, elt.y);
|
|
|
|
self.currentMesh.geometry.uvsNeedUpdate = true;
|
|
|
|
|
|
|
|
} else if (elt.type === 'normal') {
|
|
|
|
|
|
|
|
// New normal arrived
|
|
|
|
self.normals[elt.index] = new THREE.Vector3(elt.x, elt.y, elt.z);
|
|
|
|
|
|
|
|
} else if (elt.type === 'usemtl') {
|
|
|
|
|
|
|
|
if (self.currentMesh !== undefined) {
|
|
|
|
|
|
|
|
// if (self.currentMesh.geometry.attributes.normal === undefined) {
|
|
|
|
|
|
|
|
// self.currentMesh.geometry.computeVertexNormals();
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must create new mesh
|
|
|
|
// console.log("New mesh arrived : " + elt.materialName);
|
|
|
|
|
|
|
|
// Create mesh material
|
|
|
|
var material;
|
|
|
|
|
|
|
|
if (elt.materialName === null) {
|
|
|
|
|
|
|
|
// If no material, create a default material
|
|
|
|
material = new THREE.MeshLambertMaterial({color: 'red'});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// If material name exists, load if from material, and do a couple of settings
|
|
|
|
material = self.materialCreator.materials[elt.materialName.trim()];
|
|
|
|
|
|
|
|
material.side = THREE.DoubleSide;
|
|
|
|
|
|
|
|
if (material.map)
|
|
|
|
material.map.wrapS = material.map.wrapT = THREE.RepeatWrapping;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create mesh geometry
|
|
|
|
self.uvs = [];
|
|
|
|
var geometry = new THREE.Geometry();
|
|
|
|
geometry.vertices = self.vertices;
|
|
|
|
geometry.faces = [];
|
|
|
|
|
|
|
|
// If texture coords, init faceVertexUvs attribute
|
|
|
|
if (elt.texCoordsExist) {
|
|
|
|
geometry.faceVertexUvs = [self.uvs];
|
|
|
|
}
|
|
|
|
|
|
|
|
geometry.dynamic = true;
|
|
|
|
|
|
|
|
// Create mesh
|
|
|
|
var mesh = new THREE.Mesh(geometry, material);
|
2015-06-30 09:39:20 +02:00
|
|
|
mesh.faceNumber = elt.fLength;
|
2015-06-18 14:46:09 +02:00
|
|
|
self.meshes.push(mesh);
|
2015-06-17 11:09:15 +02:00
|
|
|
|
|
|
|
self.currentMesh = mesh;
|
|
|
|
|
|
|
|
if (typeof self.callback === 'function') {
|
|
|
|
self.callback(mesh);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (elt.type === 'face') {
|
|
|
|
|
2015-06-18 14:46:09 +02:00
|
|
|
if (!self.meshes[elt.mesh].added) {
|
|
|
|
|
|
|
|
self.meshes[elt.mesh].added = true;
|
|
|
|
self.obj.add(self.meshes[elt.mesh]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elt.aNormal !== undefined) {
|
|
|
|
self.meshes[elt.mesh].geometry.faces.push(new THREE.Face3(elt.a, elt.b, elt.c, [self.normals[elt.aNormal], self.normals[elt.bNormal], self.normals[elt.cNormal]]));
|
|
|
|
} else {
|
|
|
|
self.meshes[elt.mesh].geometry.faces.push(new THREE.Face3(elt.a, elt.b, elt.c));
|
|
|
|
self.meshes[elt.mesh].geometry.computeFaceNormals();
|
|
|
|
self.meshes[elt.mesh].geometry.computeVertexNormals();
|
|
|
|
}
|
2015-06-17 11:09:15 +02:00
|
|
|
|
|
|
|
if (elt.aTexture !== undefined) {
|
|
|
|
|
2015-06-18 14:46:09 +02:00
|
|
|
self.meshes[elt.mesh].geometry.faceVertexUvs[0].push([self.texCoords[elt.aTexture], self.texCoords[elt.bTexture], self.texCoords[elt.cTexture]]);
|
2015-06-17 11:09:15 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-18 14:46:09 +02:00
|
|
|
self.meshes[elt.mesh].geometry.verticesNeedUpdate = true;
|
|
|
|
self.meshes[elt.mesh].geometry.uvsNeedUpdate = true;
|
|
|
|
self.meshes[elt.mesh].geometry.normalsNeedUpdate = true;
|
|
|
|
self.meshes[elt.mesh].geometry.groupsNeedUpdate = true;
|
2015-06-17 11:09:15 +02:00
|
|
|
|
2015-06-30 09:39:20 +02:00
|
|
|
if (self.meshes[elt.mesh].faceNumber === self.meshes[elt.mesh].geometry.faces.length) {
|
|
|
|
|
|
|
|
self.meshes[elt.mesh].geometry.computeBoundingSphere();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-17 11:09:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask for next elements
|
2015-06-30 10:12:50 +02:00
|
|
|
console.log("Asking next");
|
2015-06-17 17:11:23 +02:00
|
|
|
self.socket.emit('next', self.getCamera());
|
2015-06-17 11:09:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.socket.on('disconnect', function() {
|
|
|
|
console.log('Finished !');
|
|
|
|
self.finished = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Starts the communication with the server
|
|
|
|
*/
|
2015-06-17 11:09:15 +02:00
|
|
|
ProgressiveLoaderGeometry.prototype.start = function() {
|
|
|
|
this.socket.emit('request', this.objPath);
|
|
|
|
}
|
|
|
|
|