From 5ec0a152f5f1ce084bd6e3d15487b41a8de3e7a3 Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Fri, 26 Jun 2015 09:22:32 +0200 Subject: [PATCH] I'm too good, progressive streaming with only frustum sent, and if nothing, just stream --- geo/MeshStreamer.js | 128 ++++++++++++++++++++++++-------- js/PointerCamera.js | 27 +++++++ js/ProgressiveLoaderGeometry.js | 3 +- js/prototype/initScene.js | 25 +++++-- 4 files changed, 142 insertions(+), 41 deletions(-) diff --git a/geo/MeshStreamer.js b/geo/MeshStreamer.js index 590f00c..803b8e9 100644 --- a/geo/MeshStreamer.js +++ b/geo/MeshStreamer.js @@ -328,6 +328,14 @@ geo.MeshStreamer.prototype.start = function(socket) { // Send next elements var next = self.nextElements(camera); + if (next.data.length === 0) { + + // If nothing, just serve stuff + var tmp = self.nextElements(camera, true); + next.data = tmp.data; + + } + socket.emit('elements', next.data); if (next.finished) { @@ -373,28 +381,49 @@ geo.MeshStreamer.prototype.nextMaterials = function() { * only interesting parts according to the camera * @returns {array} an array of elements ready to send */ -geo.MeshStreamer.prototype.nextElements = function(_camera) { +geo.MeshStreamer.prototype.nextElements = function(_camera, force) { + + if (force === undefined) { + + force = false; + + } // Prepare camera (and scale to model) var camera = null; + var planes = []; + var direction; if (_camera !== null) { - var camera = { + camera = { position: { - x: _camera[0], - y: _camera[1], - z: _camera[2] + x: _camera[0][0], + y: _camera[0][1], + z: _camera[0][2] }, target: { - x: _camera[3], - y: _camera[4], - z: _camera[5] + x: _camera[1][0], + y: _camera[1][1], + z: _camera[1][2] } } + for (var i = 2; i < _camera.length; i++) { + + planes.push({ + normal: { + x: _camera[i][0], + y: _camera[i][1], + z: _camera[i][2] + }, + constant: _camera[i][3] + }); + + } + // Compute camera direction - var direction = { + direction = { x: camera.target.x - camera.position.x, y: camera.target.y - camera.position.y, z: camera.target.z - camera.position.z @@ -436,36 +465,73 @@ geo.MeshStreamer.prototype.nextElements = function(_camera) { var vertex2 = this.vertices[currentFace.b]; var vertex3 = this.vertices[currentFace.c]; - if (camera !== null) { + // if (camera !== null) { - var v1 = { - x: vertex1.x - camera.position.x, - y: vertex1.y - camera.position.y, - z: vertex1.z - camera.position.z - }; + // var v1 = { + // x: vertex1.x - camera.position.x, + // y: vertex1.y - camera.position.y, + // z: vertex1.z - camera.position.z + // }; - var v2 = { - x: vertex2.x - camera.position.x, - y: vertex2.y - camera.position.y, - z: vertex2.z - camera.position.z - }; + // var v2 = { + // x: vertex2.x - camera.position.x, + // y: vertex2.y - camera.position.y, + // z: vertex2.z - camera.position.z + // }; - var v3 = { - x: vertex3.x - camera.position.x, - y: vertex3.y - camera.position.y, - z: vertex3.z - camera.position.z - }; + // var v3 = { + // x: vertex3.x - camera.position.x, + // y: vertex3.y - camera.position.y, + // z: vertex3.z - camera.position.z + // }; - if ( - direction.x * v1.x + direction.y * v1.y + direction.z * v1.z < 0 && - direction.x * v2.x + direction.y * v2.y + direction.z * v2.z < 0 && - direction.x * v3.x + direction.y * v3.y + direction.z * v3.z < 0 - ) { + // if ( + // direction.x * v1.x + direction.y * v1.y + direction.z * v1.z < 0 && + // direction.x * v2.x + direction.y * v2.y + direction.z * v2.z < 0 && + // direction.x * v3.x + direction.y * v3.y + direction.z * v3.z < 0 + // ) { - continue; + // continue; + + // } + + // } + + if (!force) { + + var exitToContinue = false; + threeVertices = [vertex1, vertex2, vertex3]; + + for (var i = 0; i < threeVertices.length; i++) { + + var vertex = threeVertices[i]; + + for (var j = 0; j < planes.length; j++) { + + var plane = planes[j]; + + distance = + plane.normal.x * vertex.x + + plane.normal.y * vertex.y + + plane.normal.z * vertex.z + + plane.constant; + + if (distance < 0) + { + exitToContinue = true; + break; + } + + } + + if (exitToContinue) + break; } + if (exitToContinue) + continue; + } if (!vertex1.sent) { diff --git a/js/PointerCamera.js b/js/PointerCamera.js index e4ea4bb..198ba51 100644 --- a/js/PointerCamera.js +++ b/js/PointerCamera.js @@ -714,3 +714,30 @@ PointerCamera.prototype.redoable = function() { return this.history.redoable(); } +PointerCamera.prototype.toList = function() { + this.updateMatrix(); + this.updateMatrixWorld(); + + var frustum = new THREE.Frustum(); + var projScreenMatrix = new THREE.Matrix4(); + projScreenMatrix.multiplyMatrices(this.projectionMatrix, this.matrixWorldInverse); + + frustum.setFromMatrix(new THREE.Matrix4().multiplyMatrices(this.projectionMatrix, this.matrixWorldInverse)); + + var ret = + [[this.position.x, this.position.y, this.position.z], + [this.target.x, this.target.y, this.target.z]]; + + for (var i = 0; i < frustum.planes.length; i++) { + + var p = frustum.planes[i]; + + ret.push([ + p.normal.x, p.normal.y, p.normal.z, p.constant + ]); + + } + + return ret; +} + diff --git a/js/ProgressiveLoaderGeometry.js b/js/ProgressiveLoaderGeometry.js index 8cb0ab1..c912473 100644 --- a/js/ProgressiveLoaderGeometry.js +++ b/js/ProgressiveLoaderGeometry.js @@ -202,8 +202,7 @@ ProgressiveLoaderGeometry.prototype.getCamera = function() { if (this.camera === null) return 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]; + return this.toList(); } /** diff --git a/js/prototype/initScene.js b/js/prototype/initScene.js index 990d856..59c503a 100644 --- a/js/prototype/initScene.js +++ b/js/prototype/initScene.js @@ -637,14 +637,23 @@ function initSponzaScene(scene, collidableObjects, loader, camera) { loader.getCamera = function() { - return [ - loader.camera.position.x * 10, - loader.camera.position.y * 10, - loader.camera.position.z * 10, - loader.camera.target.x * 10, - loader.camera.target.y * 10, - loader.camera.target.z * 10 - ]; + var ret = loader.camera.toList(); + ret[0][0] *= 10; + ret[0][1] *= 10; + ret[0][2] *= 10; + + ret[1][0] *= 10; + ret[1][1] *= 10; + ret[1][2] *= 10; + + // Planes + for (var i = 2; i < ret.length; i++) { + + ret[i][3] *= 10; + + } + + return ret; } loader.obj.scale.set(0.1,0.1,0.1);