I'm too good, progressive streaming with only frustum sent, and if

nothing, just stream
This commit is contained in:
Thomas FORGIONE 2015-06-26 09:22:32 +02:00
parent 741f23e03d
commit 5ec0a152f5
4 changed files with 142 additions and 41 deletions

View File

@ -328,6 +328,14 @@ geo.MeshStreamer.prototype.start = function(socket) {
// Send next elements // Send next elements
var next = self.nextElements(camera); 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); socket.emit('elements', next.data);
if (next.finished) { if (next.finished) {
@ -373,28 +381,49 @@ geo.MeshStreamer.prototype.nextMaterials = function() {
* only interesting parts according to the camera * only interesting parts according to the camera
* @returns {array} an array of elements ready to send * @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) // Prepare camera (and scale to model)
var camera = null; var camera = null;
var planes = [];
var direction;
if (_camera !== null) { if (_camera !== null) {
var camera = { camera = {
position: { position: {
x: _camera[0], x: _camera[0][0],
y: _camera[1], y: _camera[0][1],
z: _camera[2] z: _camera[0][2]
}, },
target: { target: {
x: _camera[3], x: _camera[1][0],
y: _camera[4], y: _camera[1][1],
z: _camera[5] 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 // Compute camera direction
var direction = { direction = {
x: camera.target.x - camera.position.x, x: camera.target.x - camera.position.x,
y: camera.target.y - camera.position.y, y: camera.target.y - camera.position.y,
z: camera.target.z - camera.position.z z: camera.target.z - camera.position.z
@ -436,36 +465,73 @@ geo.MeshStreamer.prototype.nextElements = function(_camera) {
var vertex2 = this.vertices[currentFace.b]; var vertex2 = this.vertices[currentFace.b];
var vertex3 = this.vertices[currentFace.c]; var vertex3 = this.vertices[currentFace.c];
if (camera !== null) { // if (camera !== null) {
var v1 = { // var v1 = {
x: vertex1.x - camera.position.x, // x: vertex1.x - camera.position.x,
y: vertex1.y - camera.position.y, // y: vertex1.y - camera.position.y,
z: vertex1.z - camera.position.z // z: vertex1.z - camera.position.z
}; // };
var v2 = { // var v2 = {
x: vertex2.x - camera.position.x, // x: vertex2.x - camera.position.x,
y: vertex2.y - camera.position.y, // y: vertex2.y - camera.position.y,
z: vertex2.z - camera.position.z // z: vertex2.z - camera.position.z
}; // };
var v3 = { // var v3 = {
x: vertex3.x - camera.position.x, // x: vertex3.x - camera.position.x,
y: vertex3.y - camera.position.y, // y: vertex3.y - camera.position.y,
z: vertex3.z - camera.position.z // z: vertex3.z - camera.position.z
}; // };
if ( // if (
direction.x * v1.x + direction.y * v1.y + direction.z * v1.z < 0 && // 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 * v2.x + direction.y * v2.y + direction.z * v2.z < 0 &&
direction.x * v3.x + direction.y * v3.y + direction.z * v3.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) { if (!vertex1.sent) {

View File

@ -714,3 +714,30 @@ PointerCamera.prototype.redoable = function() {
return this.history.redoable(); 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;
}

View File

@ -202,8 +202,7 @@ ProgressiveLoaderGeometry.prototype.getCamera = function() {
if (this.camera === null) if (this.camera === null)
return null; return null;
return [this.camera.position.x, this.camera.position.y, this.camera.position.z, return this.toList();
this.camera.target.x, this.camera.target.y, this.camera.target.z];
} }
/** /**

View File

@ -637,14 +637,23 @@ function initSponzaScene(scene, collidableObjects, loader, camera) {
loader.getCamera = function() { loader.getCamera = function() {
return [ var ret = loader.camera.toList();
loader.camera.position.x * 10, ret[0][0] *= 10;
loader.camera.position.y * 10, ret[0][1] *= 10;
loader.camera.position.z * 10, ret[0][2] *= 10;
loader.camera.target.x * 10,
loader.camera.target.y * 10, ret[1][0] *= 10;
loader.camera.target.z * 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); loader.obj.scale.set(0.1,0.1,0.1);