From fc77511d9488f099875ff1d108eb219c53d55fff Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Thu, 18 Jun 2015 16:30:37 +0200 Subject: [PATCH] Changed stuff --- geo/MeshStreamer.js | 109 ++++++++++++++++++++++++++++++-- js/ProgressiveLoaderGeometry.js | 8 ++- js/prototype/initScene.js | 28 ++++---- 3 files changed, 123 insertions(+), 22 deletions(-) diff --git a/geo/MeshStreamer.js b/geo/MeshStreamer.js index 746b63a..5ee6ee9 100644 --- a/geo/MeshStreamer.js +++ b/geo/MeshStreamer.js @@ -3,6 +3,38 @@ var mesh = require('./Mesh.js'); var geo = {}; +function bisect(items, x, lo, hi) { + var mid; + if (typeof(lo) == 'undefined') lo = 0; + if (typeof(hi) == 'undefined') hi = items.length; + while (lo < hi) { + mid = Math.floor((lo + hi) / 2); + if (x < items[mid]) hi = mid; + else lo = mid + 1; + } + return lo; +} + +function insort(items, x) { + items.splice(bisect(items, x), 0, x); +} + +function partialSort(items, k, comparator) { + var smallest = items.slice(0, k).sort(), + max = smallest[k-1]; + + for (var i = k, len = items.length; i < len; ++i) { + var item = items[i]; + var cond = comparator === undefined ? item < max : comparator(item, max) < 0; + if (cond) { + insort(smallest, item); + smallest.length = k; + max = smallest[k-1]; + } + } + return smallest; +} + geo.MeshStreamer = function(path, callback) { // Different parts of a obj (a mesh per material) this.meshes = []; @@ -12,6 +44,7 @@ geo.MeshStreamer = function(path, callback) { this.faces = []; this.normals = []; this.texCoords = []; + this.orderedFaces = []; // Chunk size this.chunk = 1000; @@ -26,6 +59,76 @@ geo.MeshStreamer = function(path, callback) { } } +// Returns the function that compares two faces +geo.MeshStreamer.prototype.faceComparator = function(camera) { + + var direction = { + x: camera.target.x - camera.position.x, + y: camera.target.y - camera.position.y, + z: camera.target.z - camera.position.z + }; + + var norm = Math.sqrt(direction.x * direction.x + direction.y * direction.y + direction.z * direction.z); + + direction.x /= norm; + direction.y /= norm; + direction.z /= norm; + + return function(face1, face2) { + + var center1 = { + x: (self.vertices[face1.a].x + self.vertices[face1.b].x + self.vertices[face1.b].x) / 3, + y: (self.vertices[face1.a].y + self.vertices[face1.b].y + self.vertices[face1.b].y) / 3, + z: (self.vertices[face1.a].z + self.vertices[face1.b].z + self.vertices[face1.b].z) / 3 + + }; + + var dir1 = { + x: center1.x - camera.position.x, + y: center1.y - camera.position.y, + z: center1.z - camera.position.z + }; + + var norm1 = Math.sqrt(dir1.x * dir1.x + dir1.y * dir1.y + dir1.z + dir1.z); + + dir1.x /= norm1; + dir1.y /= norm1; + dir1.z /= norm1; + + var dot1 = direction.x * dir1.x + direction.y * dir1.y + direction.z * dir1.z; + + var center2 = { + x: (self.vertices[face2.a].x + self.vertices[face2.b].x + self.vertices[face2.b].x) / 3, + y: (self.vertices[face2.a].y + self.vertices[face2.b].y + self.vertices[face2.b].y) / 3, + z: (self.vertices[face2.a].z + self.vertices[face2.b].z + self.vertices[face2.b].z) / 3 + } + + var dir2 = { + x: center2.x - camera.position.x, + y: center2.y - camera.position.y, + z: center2.z - camera.position.z + }; + + var norm2 = Math.sqrt(dir2.x * dir2.x + dir2.y * dir2.y + dir2.z + dir2.z); + + dir2.x /= norm2; + dir2.y /= norm2; + dir2.z /= norm2; + + var dot2 = direction.x * dir2.x + direction.y * dir2.y + direction.z * dir2.z; + + // Decreasing order + if (dot1 > dot2) { + return -1; + } + if (dot1 < dot2) { + return 1; + } + return 0; + + } +} + geo.MeshStreamer.prototype.loadFromFile = function(path, callback) { var self = this; fs.readFile(path, function(err, data) { @@ -88,12 +191,14 @@ geo.MeshStreamer.prototype.loadFromFile = function(path, callback) { faces[0].index = self.faces.length; faces[0].meshIndex = self.meshes.length - 1; self.faces.push(faces[0]); + self.orderedFaces.push(faces[0]); if (faces.length === 2) { faces[1].index = self.faces.length; faces[1].meshIndex = self.meshes.length - 1; self.faces.push(faces[1]); + self.orderedFaces.push(faces[1]); } @@ -151,10 +256,6 @@ geo.MeshStreamer.prototype.start = function(socket) { // Send next elements var next = self.nextElements(camera); - // console.log(self.meshIndex); - // console.log(data); - - // Emit self.chunk faces (and the corresponding vertices if not emitted) socket.emit('elements', next.data); if (next.finished) { diff --git a/js/ProgressiveLoaderGeometry.js b/js/ProgressiveLoaderGeometry.js index 6fe746d..9c52b73 100644 --- a/js/ProgressiveLoaderGeometry.js +++ b/js/ProgressiveLoaderGeometry.js @@ -111,9 +111,11 @@ ProgressiveLoaderGeometry.prototype.load = function() { } ProgressiveLoaderGeometry.prototype.getCamera = function() { - - return [this.camera.position.x, this.camera.position.y, this.camera.position.z, - this.camera.target.x, this.camera.target.y, this.camera.target.z]; + if (this.camera !== null) + return [this.camera.position.x, this.camera.position.y, this.camera.position.z, + this.camera.target.x, this.camera.target.y, this.camera.target.z]; + else + return null; } diff --git a/js/prototype/initScene.js b/js/prototype/initScene.js index 8cd5354..1ca0bb3 100644 --- a/js/prototype/initScene.js +++ b/js/prototype/initScene.js @@ -16,7 +16,7 @@ function initPeachCastle(scene, collidableObjects, loader, camera) { var loader = new ProgressiveLoaderGeometry( '/static/data/castle/princess peaches castle (outside).obj', scene, - camera, + null, function(object) { object.raycastable = true; if (object.material.name === 'Material.103_princess_peaches_cast') { @@ -38,10 +38,8 @@ 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(1.4172594473140818,0.7422266244449804,-6.745110400697304), - target: new THREE.Vector3(1.217322777130804,-0.25766921214350846,-46.732111209008615) + position: new THREE.Vector3(0.24120226734236713,0.2009624547018851,-0.5998422840047036), + target: new THREE.Vector3(0.24120226734232672,0.20096245470190008,-40.5998422840047) }; } @@ -337,16 +335,16 @@ function initWhompScene(scene, collidableObjects, loader, camera) { loader.obj.rotation.z = Math.PI/2; loader.obj.scale.set(0.1,0.1,0.1); - loader.getCamera = function() { - return [ - loader.camera.position.z * 10, - loader.camera.position.x * 10, - loader.camera.position.y * 10, - loader.camera.target.z * 10, - loader.camera.target.x * 10, - loader.camera.target.y * 10 - ]; - } + // loader.getCamera = function() { + // return [ + // loader.camera.position.z * 10, + // loader.camera.position.x * 10, + // loader.camera.position.y * 10, + // loader.camera.target.z * 10, + // loader.camera.target.x * 10, + // loader.camera.target.y * 10 + // ]; + // } collidableObjects.push(loader.obj); loader.obj.raycastable = true;