From 547162e9bb4143ab83c517262c076eb068fb1dba Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Thu, 18 Jun 2015 14:46:09 +0200 Subject: [PATCH] It seems to work now --- geo/Mesh.js | 14 +----------- geo/MeshStreamer.js | 7 +++--- js/ProgressiveLoaderGeometry.js | 38 +++++++++++++++++++++------------ js/prototype/initScene.js | 6 ++++-- 4 files changed, 33 insertions(+), 32 deletions(-) diff --git a/geo/Mesh.js b/geo/Mesh.js index 242213c..56f0ee1 100644 --- a/geo/Mesh.js +++ b/geo/Mesh.js @@ -81,19 +81,7 @@ mesh.Mesh.prototype.addNormal = function(normal) { } mesh.Mesh.prototype.isFinished = function() { - // return this.faceIndex === this.faces.length; - - for (var i = 0; i < this.faces.length; i++) { - - if (!(this.faces[i].sent)) { - - return false; - - } - - } - - return true; + return this.faceIndex === this.faces.length; } // Vertex diff --git a/geo/MeshStreamer.js b/geo/MeshStreamer.js index f7a34c5..2dbdc4c 100644 --- a/geo/MeshStreamer.js +++ b/geo/MeshStreamer.js @@ -85,13 +85,13 @@ geo.MeshStreamer.prototype.loadFromFile = function(path, callback) { // Create faces (two if Face4) var faces = currentMesh.addFaces(line); - faces[0].index = currentMesh.faces.length - (faces.length === 2 ? 1 : 2); + faces[0].index = self.faces.length; faces[0].meshIndex = self.meshes.length - 1; self.faces.push(faces[0]); if (faces.length === 2) { - faces[1].index = currentMesh.faces.length - 1; + faces[1].index = self.faces.length; faces[1].meshIndex = self.meshes.length - 1; self.faces.push(faces[1]); @@ -189,9 +189,10 @@ geo.MeshStreamer.prototype.nextElements = function(_camera) { z: camera.target.z - camera.position.z } - var sent = 0;; + var sent = 0; var data = []; + var mightBeCompletetlyFinished = true; for (var meshIndex = 0; meshIndex < this.meshes.length; meshIndex++) { diff --git a/js/ProgressiveLoaderGeometry.js b/js/ProgressiveLoaderGeometry.js index 97bed89..6fe746d 100644 --- a/js/ProgressiveLoaderGeometry.js +++ b/js/ProgressiveLoaderGeometry.js @@ -65,6 +65,7 @@ var _parseList2 = function(arr) { } var ProgressiveLoaderGeometry = function(path, scene, camera, callback) { + // Init attributes this.objPath = path.substring(1, path.length); this.texturesPath = path.substring(0, path.lastIndexOf('/')) + '/'; @@ -81,6 +82,7 @@ var ProgressiveLoaderGeometry = function(path, scene, camera, callback) { this.texCoords = []; this.normals = []; this.uvs = []; + this.meshes = []; // Init MTLLoader this.loader = new THREE.MTLLoader(this.texturesPath); @@ -141,7 +143,6 @@ ProgressiveLoaderGeometry.prototype.initIOCallbacks = function() { for (var i = 0; i < arr.length; i++) { var elt = _parseList2(arr[i]); - // console.log(arr[i]); // console.log(elts); if (elt.type === 'vertex') { @@ -216,9 +217,9 @@ ProgressiveLoaderGeometry.prototype.initIOCallbacks = function() { // Create mesh var mesh = new THREE.Mesh(geometry, material); + self.meshes.push(mesh); self.currentMesh = mesh; - self.obj.add(mesh); if (typeof self.callback === 'function') { self.callback(mesh); @@ -226,18 +227,31 @@ ProgressiveLoaderGeometry.prototype.initIOCallbacks = function() { } else if (elt.type === 'face') { - self.obj.children[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]])); + if (!self.meshes[elt.mesh].added) { - if (elt.aTexture !== undefined) { - - self.obj.children[elt.mesh].geometry.faceVertexUvs[0].push([self.texCoords[elt.aTexture], self.texCoords[elt.bTexture], self.texCoords[elt.cTexture]]); + self.meshes[elt.mesh].added = true; + self.obj.add(self.meshes[elt.mesh]); } - self.obj.children[elt.mesh].geometry.verticesNeedUpdate = true; - self.obj.children[elt.mesh].geometry.uvsNeedUpdate = true; - self.obj.children[elt.mesh].geometry.normalsNeedUpdate = true; - self.obj.children[elt.mesh].geometry.groupsNeedUpdate = true; + 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(); + } + + if (elt.aTexture !== undefined) { + + self.meshes[elt.mesh].geometry.faceVertexUvs[0].push([self.texCoords[elt.aTexture], self.texCoords[elt.bTexture], self.texCoords[elt.cTexture]]); + + } + + 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; } @@ -249,10 +263,6 @@ ProgressiveLoaderGeometry.prototype.initIOCallbacks = function() { this.socket.on('disconnect', function() { console.log('Finished !'); - self.currentMesh.geometry.computeBoundingSphere(); - // if (self.currentMesh.geometry.attributes.normal === undefined) { - // self.currentMesh.geometry.computeVertexNormals(); - // } self.finished = true; }); } diff --git a/js/prototype/initScene.js b/js/prototype/initScene.js index 1836f51..94e0227 100644 --- a/js/prototype/initScene.js +++ b/js/prototype/initScene.js @@ -38,8 +38,10 @@ function initPeachCastle(scene, collidableObjects, loader, camera) { function resetPeachElements() { return { - position: new THREE.Vector3(0.24120226734236713,0.2009624547018851,-0.5998422840047036), - target: new THREE.Vector3(0.24120226734232672,0.20096245470190008,-40.5998422840047) + // position: new THREE.Vector3(0.24120226734236713,0.2009624547018851,-0.5998422840047036), + // target: new THREE.Vector3(0.24120226734232672,0.20096245470190008,-40.5998422840047) + position: new THREE.Vector3(1.4172594473140818,0.7422266244449804,-6.745110400697304), + target: new THREE.Vector3(1.217322777130804,-0.25766921214350846,-46.732111209008615) }; }