It seems that the ProgressiveLoader is working

This commit is contained in:
Thomas FORGIONE 2015-06-12 19:19:38 +02:00
parent a5a761de75
commit 9f6f3dccac
4 changed files with 99 additions and 73 deletions

View File

@ -42,6 +42,10 @@ geo.MeshStreamer.prototype.loadFromFile = function(path, callback) {
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
@ -56,6 +60,11 @@ geo.MeshStreamer.prototype.loadFromFile = function(path, callback) {
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
@ -139,18 +148,10 @@ geo.MeshStreamer.prototype.merge = function(callback) {
geo.Vertex = function() {
if (typeof arguments[0] === 'string' || arguments[0] instanceof String) {
var split = arguments[0].split(' ');
var split = arguments[0].replace(/\s+/g, " ").split(' ');
this.x = parseFloat(split[1]);
this.y = parseFloat(split[2]);
this.z = parseFloat(split[3]);
} else if (arguments.length === 3) {
this.x = arguments[0];
this.y = arguments[1];
this.z = arguments[2];
} else {
this.x = 0;
this.y = 0;
this.z = 0;
}
this.sent = false;
}
@ -165,15 +166,9 @@ geo.Vertex.prototype.toString = function() {
geo.TextureCoord = function() {
if (typeof arguments[0] === 'string' || arguments[0] instanceof String) {
var split = arguments[0].split(' ');
var split = arguments[0].replace(/\s+/g, " ").split(' ');
this.x = parseFloat(split[1]);
this.y = parseFloat(split[2]);
} else if (arguments.length === 3) {
this.x = arguments[0];
this.y = arguments[1];
} else {
this.x = 0;
this.y = 0;
}
this.sent = false;
}
@ -190,7 +185,7 @@ geo.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"
var split = arguments[0].split(' ');
var split = arguments[0].replace(/\s+/g, ' ').split(' ');
this.a = parseInt(split[1]) - 1;
this.b = parseInt(split[2]) - 1;
this.c = parseInt(split[3]) - 1;
@ -200,7 +195,7 @@ geo.Face = function() {
} else {
// There might be textures coords
var split = arguments[0].split(' ');
var split = arguments[0].replace(/\s+/g, ' ').trim().split(' ');
// Split elements
var split1 = split[1].split('/');
@ -208,7 +203,8 @@ geo.Face = function() {
var split3 = split[3].split('/');
var vIndex = 0;
var tIndex = split1.length === 2 ? 1 : 2;
// var tIndex = split1.length === 2 ? 1 : 2;
var tIndex = 1;
this.a = parseInt(split1[vIndex]) - 1; this.aTexture = parseInt(split1[tIndex]) - 1;
this.b = parseInt(split2[vIndex]) - 1; this.bTexture = parseInt(split2[tIndex]) - 1;
@ -216,18 +212,12 @@ geo.Face = function() {
if (split.length === 5) {
var split4 = split[4].split('/');
this.d = parseInt(split4[vIndex]) - 1; this.cTexture = parseInt(split4[tIndex]) - 1;
this.d = parseInt(split4[vIndex]) - 1; this.dTexture = parseInt(split4[tIndex]) - 1;
}
}
} else if (arguments.length === 3) {
this.a = arguments[0] - 1;
this.b = arguments[1] - 1;
this.c = arguments[2] - 1;
if (arguments.length === 4)
this.d = arguments[3] - 1;
}
this.sent = false;
}
@ -239,6 +229,14 @@ geo.Face.prototype.max = function() {
}
}
geo.Face.prototype.maxTexture = function() {
if (this.dTexture) {
return Math.max(this.aTexture, this.bTexture, this.cTexture, this.dTexture);
} else {
return Math.max(this.aTexture, this.bTexture, this.cTexture);
}
}
geo.Face.prototype.toList = function() {
var l = ['f', this.a, this.b, this.c];
if (this.d)
@ -251,7 +249,7 @@ geo.Face.prototype.toList = function() {
}
if (this.dTexture)
l.push(this.aTexture);
l.push(this.dTexture);
return l;
}
@ -261,7 +259,7 @@ geo.Face.prototype.toString = function() {
}
geo.Usemtl = function() {
var split = arguments[0].split(' ');
var split = arguments[0].replace(/\s+/g, ' ').split(' ');
this.name = split[1];
}

View File

@ -54,8 +54,6 @@ var ProgressiveLoader = function(path, scene, materialCreator, transparentElemen
elts[3]
));
verticesNeedUpdate = true;
} else if (elts[0] === 'vt') {
textCoords.push(new THREE.Vector2(
@ -83,7 +81,7 @@ var ProgressiveLoader = function(path, scene, materialCreator, transparentElemen
}
// Add texture
if (elts.length === 7 || elts.length === 9) {
if (elts.length === 7) {
uvs.push([
textCoords[elts[4]],
textCoords[elts[5]],
@ -91,6 +89,14 @@ var ProgressiveLoader = function(path, scene, materialCreator, transparentElemen
]);
}
if (elts.length === 9) {
uvs.push([
textCoords[elts[5]],
textCoords[elts[6]],
textCoords[elts[7]]
]);
}
if (elts.length === 9) {
uvs.push([
textCoords[elts[5]],
@ -118,6 +124,7 @@ var ProgressiveLoader = function(path, scene, materialCreator, transparentElemen
} else {
material = materialCreator.materials[currentMaterial.trim()];
material.side = THREE.DoubleSide;
if (material.map)
material.map.wrapS = material.map.wrapT = THREE.RepeatWrapping;
currentMaterial = null;
@ -130,8 +137,8 @@ var ProgressiveLoader = function(path, scene, materialCreator, transparentElemen
currentMesh.raycastable = true;
}
}
currentMesh.geometry.computeFaceNormals();
}
obj.add(currentMesh);
} else if (elts[0] === 'u') {

View File

@ -781,45 +781,66 @@ function initMountain(camera, scene, static_path, coins) {
}
function initSponzaScene(scene, collidableObjects, loader, static_path) {
var onProgress = function ( xhr ) {
if ( xhr.lengthComputable ) {
var percentComplete = xhr.loaded / xhr.total * 100;
console.log( Math.round(percentComplete, 2) + '% downloaded' );
}
};
loader.load(
static_path + './data/sponza/sponza.json',
function (geometry, materials) {
console.log("OK");
geometry.mergeVertices();
var material = new THREE.MeshFaceMaterial(materials);
var object = new THREE.Mesh(geometry, material);
object.scale.set(0.01,0.01,0.01);
object.raycastable = true;
collidableObjects.push(object);
// object.rotation.x = -Math.PI/2;
// object.rotation.z = Math.PI/2;
// collidableObjects.push(object);
// scene.add(object);
object.traverse(function (obj) {
if (obj instanceof THREE.Mesh) {
for (var i in obj.material.materials) {
var m = obj.material.materials[i].map;
if (m)
m.wrapS = m.wrapT = THREE.RepeatWrapping;
}
// obj.material.map.wrapS = obj.material.map.wrapT = THREE.RepeatWrapping;
}
// obj.geometry.mergeVertices();
// obj.geometry.computeVertexNormals();
// obj.material.side = THREE.DoubleSide;
// obj.raycastable = true;
// }
var loader = new THREE.MTLLoader('/static/data/sponza/');
loader.load('/static/data/sponza/sponza.mtl', function(materialCreator) {
materialCreator.preload();
console.log("Starting loading...");
var mesh = ProgressiveLoader('static/data/sponza/sponza.obj', scene, materialCreator, [
'chain',
'leaf'
]);
// object.position.z -= 10.9;
// object.position.y += 0.555;
// object.position.x += 3.23;
mesh.scale.set(0.01,0.01,0.01);
mesh.raycastable = true;
collidableObjects.push(mesh);
});
scene.add(object);
}
, onProgress, function(xhr) { console.log("error");});
// var onProgress = function ( xhr ) {
// if ( xhr.lengthComputable ) {
// var percentComplete = xhr.loaded / xhr.total * 100;
// console.log( Math.round(percentComplete, 2) + '% downloaded' );
// }
// };
// loader.load(
// static_path + './data/sponza/sponza.json',
// function (geometry, materials) {
// console.log("OK");
// geometry.mergeVertices();
// var material = new THREE.MeshFaceMaterial(materials);
// var object = new THREE.Mesh(geometry, material);
// object.scale.set(0.01,0.01,0.01);
// object.raycastable = true;
// collidableObjects.push(object);
// // object.rotation.x = -Math.PI/2;
// // object.rotation.z = Math.PI/2;
// // collidableObjects.push(object);
// // scene.add(object);
// object.traverse(function (obj) {
// if (obj instanceof THREE.Mesh) {
// for (var i in obj.material.materials) {
// var m = obj.material.materials[i].map;
// if (m)
// m.wrapS = m.wrapT = THREE.RepeatWrapping;
// }
// // obj.material.map.wrapS = obj.material.map.wrapT = THREE.RepeatWrapping;
// }
// // obj.geometry.mergeVertices();
// // obj.geometry.computeVertexNormals();
// // obj.material.side = THREE.DoubleSide;
// // obj.raycastable = true;
// // }
// });
// scene.add(object);
// }
// , onProgress, function(xhr) { console.log("error");});
}
function createSponzaCoins() {

View File

@ -48,7 +48,7 @@ module.exports = function(io) {
socket.on('next', function() {
var bound = 100;
var bound = 1000;
var toSend = [];
var elt;