3d-interface/js/ProgressiveLoader.js

287 lines
9.4 KiB
JavaScript
Raw Normal View History

2015-06-15 17:04:19 +02:00
var _parseList = function(arr) {
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';
// Only Face3 are allowed
vertexIndices = arr[2];
textureIndices = arr[3];
normalIndices = arr[4];
// 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];
}
2015-06-15 17:04:19 +02:00
// Normal indices (if they exist)
if (normalIndices.length > 0) {
ret.aNormal = normalIndices[0];
ret.bNormal = normalIndices[1];
ret.cNormal = normalIndices[2];
}
2015-06-15 17:04:19 +02:00
} else if (arr[0] === 'vn') {
2015-06-15 17:04:19 +02:00
// Normal
ret.type = "normal";
ret.x = arr[2];
ret.y = arr[3];
ret.z = arr[4];
2015-06-15 17:04:19 +02:00
2015-06-16 16:35:43 +02:00
} 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];
2015-06-15 17:04:19 +02:00
}
return ret;
};
2015-06-15 17:04:19 +02:00
var ProgressiveLoader = function(path, scene, camera, callback) {
2015-06-15 17:04:19 +02:00
// Init attributes
2015-06-16 15:14:43 +02:00
this.objPath = path.substring(1, path.length);
this.texturesPath = path.substring(0, path.lastIndexOf('/')) + '/';
this.mtlPath = path.replace('.obj', '.mtl');
2015-06-15 17:04:19 +02:00
this.scene = scene;
this.callback = callback;
this.counter = 0;
this.obj = new THREE.Object3D();
scene.add(this.obj);
this.vertices = [];
this.texCoords = [];
this.normals = [];
2015-06-15 17:04:19 +02:00
// Init MTLLoader
this.loader = new THREE.MTLLoader(this.texturesPath);
// Init io stuff
this.socket = io();
this.initIOCallbacks();
this.camera = camera;
};
2015-06-15 17:04:19 +02:00
ProgressiveLoader.prototype.load = function() {
var self = this;
this.loader.load(self.mtlPath, function(materialCreator) {
self.materialCreator = materialCreator;
2015-06-15 09:40:30 +02:00
materialCreator.preload();
2015-06-10 11:59:02 +02:00
2015-06-15 17:04:19 +02:00
self.start();
});
};
2015-06-15 17:04:19 +02:00
ProgressiveLoader.prototype.initIOCallbacks = function() {
var self = this;
2015-06-16 16:35:43 +02:00
this.socket.on('ok', function() {
console.log('ok');
self.socket.emit('next');
});
2015-06-16 16:35:43 +02:00
this.socket.on('elements', function(arr) {
2015-06-16 15:14:43 +02:00
2015-06-16 16:35:43 +02:00
// console.log("Received elements for the " + (++self.counter) + "th time !");
for (var i = 0; i < arr.length; i++) {
2015-06-16 15:14:43 +02:00
2015-06-16 16:35:43 +02:00
var elt = _parseList(arr[i]);
2015-06-15 17:04:19 +02:00
2015-06-16 16:35:43 +02:00
// console.log(elts);
if (elt.type === 'vertex') {
2015-06-15 17:04:19 +02:00
2015-06-16 16:35:43 +02:00
// New vertex arrived
self.vertices[elt.index] = [elt.x, elt.y, elt.z];
2015-06-15 17:04:19 +02:00
2015-06-16 16:35:43 +02:00
} else if (elt.type === 'texCoord') {
2015-06-15 17:04:19 +02:00
2015-06-16 16:35:43 +02:00
// New texCoord arrived
self.texCoords[elt.index] = [elt.x, elt.y];
2015-06-15 17:04:19 +02:00
2015-06-16 16:35:43 +02:00
} else if (elt.type === 'normal') {
2015-06-15 17:04:19 +02:00
2015-06-16 16:35:43 +02:00
// New normal arrived
self.normals[elt.index] = [elt.x, elt.y, elt.z];
2015-06-15 17:04:19 +02:00
2015-06-16 16:35:43 +02:00
} else if (elt.type === 'usemtl') {
2015-06-15 17:04:19 +02:00
2015-06-16 16:35:43 +02:00
if (self.currentMesh !== undefined) {
2015-06-15 17:04:19 +02:00
2015-06-16 16:35:43 +02:00
self.currentMesh.geometry.computeBoundingSphere();
2015-06-15 17:04:19 +02:00
2015-06-16 16:35:43 +02:00
if (self.currentMesh.geometry.attributes.normal === undefined) {
2015-06-15 17:04:19 +02:00
2015-06-16 16:35:43 +02:00
self.currentMesh.geometry.computeVertexNormals();
2015-06-16 16:35:43 +02:00
}
2015-06-16 16:35:43 +02:00
}
2015-06-16 16:35:43 +02:00
// Must create new mesh
// console.log("New mesh arrived : " + elt.materialName);
2015-06-16 16:35:43 +02:00
// Create mesh material
var material;
2015-06-16 16:35:43 +02:00
if (elt.materialName === null) {
2015-06-16 16:35:43 +02:00
// If no material, create a default material
material = new THREE.MeshLambertMaterial({color: 'red'});
2015-06-16 16:35:43 +02:00
} else {
2015-06-16 16:35:43 +02:00
// If material name exists, load if from material, and do a couple of settings
material = self.materialCreator.materials[elt.materialName.trim()];
2015-06-16 16:35:43 +02:00
material.side = THREE.DoubleSide;
2015-06-15 17:04:19 +02:00
2015-06-16 16:35:43 +02:00
if (material.map)
material.map.wrapS = material.map.wrapT = THREE.RepeatWrapping;
}
2015-06-10 11:59:02 +02:00
2015-06-16 16:35:43 +02:00
// Create mesh geometry
var geometry = new THREE.BufferGeometry();
geometry.dynamic = true;
2015-06-16 16:35:43 +02:00
var positionArray = new Float32Array(elt.fLength * 3 * 3);
var positionAttribute = new THREE.BufferAttribute(positionArray, 3);
2015-06-15 17:04:19 +02:00
2015-06-16 16:35:43 +02:00
geometry.addAttribute('position', positionAttribute);
2015-06-15 17:04:19 +02:00
2015-06-16 16:35:43 +02:00
// Add other attributes if necessary
if (elt.texCoordsExist) {
2015-06-15 17:04:19 +02:00
2015-06-16 16:35:43 +02:00
// console.log("Mesh with textures");
2015-06-15 17:04:19 +02:00
2015-06-16 16:35:43 +02:00
var uvArray = new Float32Array(elt.fLength * 3 * 2);
var uvAttribute = new THREE.BufferAttribute(uvArray, 2);
2015-06-15 17:04:19 +02:00
2015-06-16 16:35:43 +02:00
geometry.addAttribute('uv', uvAttribute);
}
2015-06-09 17:36:21 +02:00
2015-06-16 16:35:43 +02:00
if (elt.normalsExist) {
2015-06-09 17:36:21 +02:00
2015-06-16 16:35:43 +02:00
// console.log("Mesh with normals");
2015-06-16 16:35:43 +02:00
var normalArray = new Float32Array(elt.fLength * 3 * 3);
var normalAttribute = new THREE.BufferAttribute(normalArray, 3);
2015-06-09 17:36:21 +02:00
2015-06-16 16:35:43 +02:00
geometry.addAttribute('normal', normalAttribute);
2015-06-16 16:35:43 +02:00
}
2015-06-16 16:35:43 +02:00
// Create mesh
var mesh = new THREE.Mesh(geometry, material);
self.currentMesh = mesh;
self.obj.add(mesh);
if (typeof self.callback === 'function') {
self.callback(mesh);
}
2015-06-10 11:50:47 +02:00
2015-06-15 17:04:19 +02:00
} else if (elt.type === 'face') {
2015-06-15 09:40:30 +02:00
2015-06-15 17:04:19 +02:00
// New face arrived : add it into current mesh
self.currentMesh.geometry.attributes.position.array[elt.index * 9 ] = self.vertices[elt.a][0];
self.currentMesh.geometry.attributes.position.array[elt.index * 9 + 1] = self.vertices[elt.a][1];
self.currentMesh.geometry.attributes.position.array[elt.index * 9 + 2] = self.vertices[elt.a][2];
2015-06-10 14:48:11 +02:00
2015-06-15 17:04:19 +02:00
self.currentMesh.geometry.attributes.position.array[elt.index * 9 + 3] = self.vertices[elt.b][0];
self.currentMesh.geometry.attributes.position.array[elt.index * 9 + 4] = self.vertices[elt.b][1];
self.currentMesh.geometry.attributes.position.array[elt.index * 9 + 5] = self.vertices[elt.b][2];
self.currentMesh.geometry.attributes.position.array[elt.index * 9 + 6] = self.vertices[elt.c][0];
self.currentMesh.geometry.attributes.position.array[elt.index * 9 + 7] = self.vertices[elt.c][1];
self.currentMesh.geometry.attributes.position.array[elt.index * 9 + 8] = self.vertices[elt.c][2];
self.currentMesh.geometry.attributes.position.needsUpdate = true;
2015-06-10 11:50:47 +02:00
// If normals
if (elt.aNormal !== undefined) {
self.currentMesh.geometry.attributes.normal.array[elt.index * 9 ] = self.normals[elt.aNormal][0];
self.currentMesh.geometry.attributes.normal.array[elt.index * 9 + 1] = self.normals[elt.aNormal][1];
self.currentMesh.geometry.attributes.normal.array[elt.index * 9 + 2] = self.normals[elt.aNormal][2];
self.currentMesh.geometry.attributes.normal.array[elt.index * 9 + 3] = self.normals[elt.bNormal][0];
self.currentMesh.geometry.attributes.normal.array[elt.index * 9 + 4] = self.normals[elt.bNormal][1];
self.currentMesh.geometry.attributes.normal.array[elt.index * 9 + 5] = self.normals[elt.bNormal][2];
self.currentMesh.geometry.attributes.normal.array[elt.index * 9 + 6] = self.normals[elt.cNormal][0];
self.currentMesh.geometry.attributes.normal.array[elt.index * 9 + 7] = self.normals[elt.cNormal][1];
self.currentMesh.geometry.attributes.normal.array[elt.index * 9 + 8] = self.normals[elt.cNormal][2];
self.currentMesh.geometry.attributes.normal.needsUpdate = true;
}
if (elt.aTexture !== undefined) {
self.currentMesh.geometry.attributes.uv.array[elt.index * 6 ] = self.texCoords[elt.aTexture][0];
self.currentMesh.geometry.attributes.uv.array[elt.index * 6 + 1] = self.texCoords[elt.aTexture][1];
self.currentMesh.geometry.attributes.uv.array[elt.index * 6 + 2] = self.texCoords[elt.bTexture][0];
self.currentMesh.geometry.attributes.uv.array[elt.index * 6 + 3] = self.texCoords[elt.bTexture][1];
self.currentMesh.geometry.attributes.uv.array[elt.index * 6 + 4] = self.texCoords[elt.cTexture][0];
self.currentMesh.geometry.attributes.uv.array[elt.index * 6 + 5] = self.texCoords[elt.cTexture][1];
self.currentMesh.geometry.attributes.uv.needsUpdate = true;
}
2015-06-15 09:40:30 +02:00
}
2015-06-15 17:04:19 +02:00
}
2015-06-10 11:50:47 +02:00
2015-06-15 17:04:19 +02:00
// Ask for next elements
self.socket.emit('next');
});
2015-06-10 11:50:47 +02:00
2015-06-15 17:04:19 +02:00
this.socket.on('disconnect', function() {
console.log('Finished !');
self.finished = true;
2015-06-09 17:36:21 +02:00
});
};
2015-06-15 17:04:19 +02:00
ProgressiveLoader.prototype.start = function() {
this.socket.emit('request', this.objPath);
};
2015-06-15 17:04:19 +02:00