2015-11-16 09:55:09 +01:00
|
|
|
var fs = require('fs');
|
2015-11-13 10:36:54 +01:00
|
|
|
var THREE = require('three');
|
|
|
|
var L3D = require('../../static/js/l3d.min.js');
|
|
|
|
|
2015-11-16 09:55:09 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var predictionTables = [
|
2015-11-17 10:41:05 +01:00
|
|
|
JSON.parse(fs.readFileSync('./geo/mat1.json')),
|
|
|
|
JSON.parse(fs.readFileSync('./geo/mat2.json')),
|
|
|
|
JSON.parse(fs.readFileSync('./geo/mat3.json')),
|
|
|
|
[[1,1,0],
|
|
|
|
[1,2,0],
|
|
|
|
[2,1,0]]
|
2015-11-16 09:55:09 +01:00
|
|
|
];
|
|
|
|
} catch (e) {
|
|
|
|
process.stderr.write('No prefetching will be done !');
|
|
|
|
predictionTables = [];
|
|
|
|
}
|
|
|
|
|
2015-11-13 10:36:54 +01:00
|
|
|
function isInFrustum(element, planes) {
|
|
|
|
|
|
|
|
if (element instanceof Array) {
|
|
|
|
|
|
|
|
var outcodes = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < element.length; i++) {
|
|
|
|
|
|
|
|
var vertex = element[i];
|
|
|
|
var currentOutcode = "";
|
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
for (var j = 0; j < planes.length; j++) {
|
2015-11-13 10:36:54 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
// }
|
|
|
|
|
|
|
|
currentOutcode += distance > 0 ? '0' : '1';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
outcodes.push(parseInt(currentOutcode,2));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// http://vterrain.org/LOD/culling.html
|
|
|
|
// I have no idea what i'm doing
|
|
|
|
// http://i.kinja-img.com/gawker-media/image/upload/japbcvpavbzau9dbuaxf.jpg
|
|
|
|
// But it seems to work
|
|
|
|
// EDIT : Not, this should be ok http://www.cs.unc.edu/~blloyd/comp770/Lecture07.pdf
|
|
|
|
|
|
|
|
if ((outcodes[0] | outcodes[1] | outcodes[2]) === 0) {
|
|
|
|
return true;
|
|
|
|
} else if ((outcodes[0] & outcodes[1] & outcodes[2]) !== 0) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// part of the triangle is inside the viewing volume
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2015-06-18 16:30:37 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2015-06-18 16:30:37 +02:00
|
|
|
function insort(items, x) {
|
|
|
|
items.splice(bisect(items, x), 0, x);
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2015-06-18 16:30:37 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* A class that streams easily a mesh via socket.io
|
|
|
|
* @memberOf geo
|
|
|
|
* @constructor
|
|
|
|
* @param {string} path to the mesh
|
|
|
|
*/
|
2015-06-29 09:50:26 +02:00
|
|
|
geo.MeshStreamer = function(path) {
|
2015-06-15 17:04:19 +02:00
|
|
|
|
2015-06-29 10:43:29 +02:00
|
|
|
/**
|
|
|
|
* array of array telling if the jth face of the ith mesh has already been sent
|
2015-06-29 15:41:18 +02:00
|
|
|
*
|
|
|
|
* For each mesh, there is an object containing
|
|
|
|
* <ul>
|
|
|
|
* <li>`counter` : the number of faces currently sent</li>
|
|
|
|
* <li>`array` : an array boolean telling if the ith face has already been sent</li>
|
|
|
|
* </ul>
|
|
|
|
* @type {Object[]}
|
2015-06-29 10:43:29 +02:00
|
|
|
*/
|
|
|
|
this.meshFaces = [];
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
2015-06-29 09:50:26 +02:00
|
|
|
* array of booleans telling if the ith vertex has already been sent
|
|
|
|
* @type {Boolean[]}
|
2015-06-23 15:31:23 +02:00
|
|
|
*/
|
2015-06-15 17:04:19 +02:00
|
|
|
this.vertices = [];
|
2015-06-23 15:31:23 +02:00
|
|
|
|
|
|
|
/**
|
2015-06-29 09:50:26 +02:00
|
|
|
* array of booleans telling if the ith face has already been sent
|
|
|
|
* @type {Boolean[]}
|
2015-06-23 15:31:23 +02:00
|
|
|
*/
|
2015-06-15 17:04:19 +02:00
|
|
|
this.faces = [];
|
2015-06-23 15:31:23 +02:00
|
|
|
|
|
|
|
/**
|
2015-06-29 09:50:26 +02:00
|
|
|
* array of booleans telling if the ith normal has already been sent
|
|
|
|
* @type {Boolean[]}
|
2015-06-23 15:31:23 +02:00
|
|
|
*/
|
2015-06-16 12:05:29 +02:00
|
|
|
this.normals = [];
|
2015-06-23 15:31:23 +02:00
|
|
|
|
|
|
|
/**
|
2015-06-29 09:50:26 +02:00
|
|
|
* array of booleans telling if the ith texCoord has already been sent
|
|
|
|
* @type {Boolean[]}
|
2015-06-23 15:31:23 +02:00
|
|
|
*/
|
2015-06-15 17:04:19 +02:00
|
|
|
this.texCoords = [];
|
2015-06-23 15:31:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of element to send by packet
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
2015-11-27 10:20:13 +01:00
|
|
|
this.chunk = 1250 / 2;
|
2015-11-16 09:55:09 +01:00
|
|
|
|
|
|
|
this.previousReco = 0;
|
2015-06-15 17:04:19 +02:00
|
|
|
|
|
|
|
if (path !== undefined) {
|
|
|
|
|
2015-06-29 15:41:18 +02:00
|
|
|
this.mesh = geo.availableMeshes[path];
|
2015-06-29 09:50:26 +02:00
|
|
|
|
2015-06-15 17:04:19 +02:00
|
|
|
}
|
2015-06-29 09:50:26 +02:00
|
|
|
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-15 17:04:19 +02:00
|
|
|
|
2015-11-13 10:36:54 +01:00
|
|
|
geo.MeshStreamer.prototype.isBackFace = function(camera, face) {
|
|
|
|
|
2015-11-16 09:55:09 +01:00
|
|
|
var directionCamera = L3D.Tools.diff(
|
|
|
|
L3D.Tools.mul(
|
|
|
|
L3D.Tools.sum(
|
|
|
|
L3D.Tools.sum(
|
|
|
|
this.mesh.vertices[face.a],
|
|
|
|
this.mesh.vertices[face.b]
|
|
|
|
),
|
|
|
|
this.mesh.vertices[face.c]
|
|
|
|
),
|
|
|
|
1/3),
|
|
|
|
camera.position
|
|
|
|
);
|
2015-11-13 10:36:54 +01:00
|
|
|
|
|
|
|
var v1 = L3D.Tools.diff(this.mesh.vertices[face.b], this.mesh.vertices[face.a]);
|
|
|
|
var v2 = L3D.Tools.diff(this.mesh.vertices[face.c], this.mesh.vertices[face.a]);
|
|
|
|
|
|
|
|
var normal = L3D.Tools.cross(v1, v2);
|
|
|
|
|
|
|
|
return L3D.Tools.dot(directionCamera, normal) > 0;
|
|
|
|
};
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Compute a function that can compare two faces
|
|
|
|
* @param {Camera} camera a camera seeing or not face
|
2015-10-13 16:32:36 +02:00
|
|
|
* @returns {function} the function that compares two faces : the higher face is the most interesting for the camera
|
2015-06-23 15:31:23 +02:00
|
|
|
*/
|
2015-06-18 16:30:37 +02:00
|
|
|
geo.MeshStreamer.prototype.faceComparator = function(camera) {
|
|
|
|
|
2015-06-18 16:58:07 +02:00
|
|
|
var self = this;
|
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
// var direction = {
|
|
|
|
// x: camera.target.x - camera.position.x,
|
|
|
|
// y: camera.target.y - camera.position.y,
|
|
|
|
// z: camera.target.z - camera.position.z
|
|
|
|
// };
|
2015-06-18 16:30:37 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
// var norm = Math.sqrt(direction.x * direction.x + direction.y * direction.y + direction.z * direction.z);
|
2015-06-18 16:30:37 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
// direction.x /= norm;
|
|
|
|
// direction.y /= norm;
|
|
|
|
// direction.z /= norm;
|
2015-06-18 16:30:37 +02:00
|
|
|
|
|
|
|
return function(face1, face2) {
|
|
|
|
|
|
|
|
var center1 = {
|
2015-11-24 09:26:12 +01:00
|
|
|
x: (self.mesh.vertices[face1.a].x + self.mesh.vertices[face1.b].x + self.mesh.vertices[face1.c].x) / 3,
|
|
|
|
y: (self.mesh.vertices[face1.a].y + self.mesh.vertices[face1.b].y + self.mesh.vertices[face1.c].y) / 3,
|
|
|
|
z: (self.mesh.vertices[face1.a].z + self.mesh.vertices[face1.b].z + self.mesh.vertices[face1.c].z) / 3
|
2015-06-18 16:30:37 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
var dir1 = {
|
|
|
|
x: center1.x - camera.position.x,
|
|
|
|
y: center1.y - camera.position.y,
|
|
|
|
z: center1.z - camera.position.z
|
|
|
|
};
|
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
// var norm1 = Math.sqrt(dir1.x * dir1.x + dir1.y * dir1.y + dir1.z + dir1.z);
|
2015-06-18 16:30:37 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
// dir1.x /= norm1;
|
|
|
|
// dir1.y /= norm1;
|
|
|
|
// dir1.z /= norm1;
|
2015-06-18 16:30:37 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
var dot1 = dir1.x * dir1.x + dir1.y * dir1.y + dir1.z * dir1.z;
|
2015-06-18 16:30:37 +02:00
|
|
|
|
|
|
|
var center2 = {
|
2015-11-24 09:26:12 +01:00
|
|
|
x: (self.mesh.vertices[face2.a].x + self.mesh.vertices[face2.b].x + self.mesh.vertices[face2.c].x) / 3,
|
|
|
|
y: (self.mesh.vertices[face2.a].y + self.mesh.vertices[face2.b].y + self.mesh.vertices[face2.c].y) / 3,
|
|
|
|
z: (self.mesh.vertices[face2.a].z + self.mesh.vertices[face2.b].z + self.mesh.vertices[face2.c].z) / 3
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-18 16:30:37 +02:00
|
|
|
|
|
|
|
var dir2 = {
|
|
|
|
x: center2.x - camera.position.x,
|
|
|
|
y: center2.y - camera.position.y,
|
|
|
|
z: center2.z - camera.position.z
|
|
|
|
};
|
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
// var norm2 = Math.sqrt(dir2.x * dir2.x + dir2.y * dir2.y + dir2.z + dir2.z);
|
2015-06-18 16:30:37 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
// dir2.x /= norm2;
|
|
|
|
// dir2.y /= norm2;
|
|
|
|
// dir2.z /= norm2;
|
2015-06-18 16:30:37 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
var dot2 = dir2.x * dir2.x + dir2.y * dir2.y + dir2.z * dir2.z;
|
2015-06-18 16:30:37 +02:00
|
|
|
|
|
|
|
// Decreasing order
|
2015-11-24 09:26:12 +01:00
|
|
|
if (dot1 < dot2) {
|
2015-06-18 16:30:37 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2015-11-24 09:26:12 +01:00
|
|
|
if (dot1 > dot2) {
|
2015-06-18 16:30:37 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
|
|
|
};
|
2015-06-18 16:30:37 +02:00
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Initialize the socket.io callback
|
|
|
|
* @param {socket} socket the socket to initialize
|
|
|
|
*/
|
2015-06-15 17:04:19 +02:00
|
|
|
geo.MeshStreamer.prototype.start = function(socket) {
|
|
|
|
|
2015-06-16 16:35:43 +02:00
|
|
|
this.meshIndex = 0;
|
2015-06-18 11:46:24 +02:00
|
|
|
this.socket = socket;
|
2015-06-15 17:04:19 +02:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
socket.on('request', function(path, laggy, prefetch) {
|
2015-10-26 15:56:18 +01:00
|
|
|
|
|
|
|
if (laggy === true) {
|
|
|
|
self.chunk = 1;
|
|
|
|
}
|
2015-06-15 17:04:19 +02:00
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
self.prefetch = prefetch;
|
|
|
|
|
2015-06-29 15:41:18 +02:00
|
|
|
self.mesh = geo.availableMeshes[path];
|
2015-06-29 09:50:26 +02:00
|
|
|
|
2015-11-16 09:55:09 +01:00
|
|
|
switch (path) {
|
|
|
|
case '/static/data/bobomb/bobomb battlefeild.obj':
|
|
|
|
case '/static/data/bobomb/bobomb battlefeild_sub.obj':
|
|
|
|
self.predictionTable = predictionTables[0];
|
|
|
|
break;
|
|
|
|
case '/static/data/mountain/coocoolmountain.obj':
|
|
|
|
case '/static/data/mountain/coocoolmountain_sub.obj':
|
|
|
|
self.predictionTable = predictionTables[1];
|
|
|
|
break;
|
|
|
|
case '/static/data/whomp/Whomps Fortress.obj':
|
|
|
|
case '/static/data/whomp/Whomps Fortress_sub.obj':
|
|
|
|
self.predictionTable = predictionTables[2];
|
|
|
|
break;
|
2015-11-17 10:41:05 +01:00
|
|
|
default:
|
|
|
|
self.predictionTable = predictionTables[3];
|
2015-11-16 09:55:09 +01:00
|
|
|
};
|
|
|
|
|
2015-11-13 10:36:54 +01:00
|
|
|
if (self.mesh === undefined) {
|
|
|
|
process.stderr.write('Wrong path for model : ' + path);
|
|
|
|
socket.emit('refused');
|
|
|
|
socket.disconnect();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-29 10:43:29 +02:00
|
|
|
self.meshFaces = new Array(self.mesh.meshes.length);
|
|
|
|
|
|
|
|
for (var i = 0; i < self.meshFaces.length; i++) {
|
|
|
|
|
|
|
|
self.meshFaces[i] = {
|
|
|
|
counter: 0,
|
|
|
|
array: new Array(self.mesh.meshes[i].faces.length)
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-29 09:50:26 +02:00
|
|
|
socket.emit('ok');
|
2015-06-15 17:04:19 +02:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-06-18 16:58:07 +02:00
|
|
|
socket.on('materials', function() {
|
|
|
|
|
|
|
|
var data = self.nextMaterials();
|
|
|
|
|
|
|
|
socket.emit('elements', data);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-11-16 09:55:09 +01:00
|
|
|
socket.on('reco', function(recoId) {
|
|
|
|
|
|
|
|
self.previousReco = recoId + 1;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-11-27 10:20:13 +01:00
|
|
|
socket.on('next', function(_camera, score) {
|
2015-11-16 09:55:09 +01:00
|
|
|
|
|
|
|
var cameraFrustum = {};
|
|
|
|
|
|
|
|
// Clean camera attribute
|
|
|
|
if (_camera !== null) {
|
|
|
|
|
|
|
|
cameraFrustum = {
|
|
|
|
position: {
|
|
|
|
x: _camera[0][0],
|
|
|
|
y: _camera[0][1],
|
|
|
|
z: _camera[0][2]
|
|
|
|
},
|
|
|
|
target: {
|
|
|
|
x: _camera[1][0],
|
|
|
|
y: _camera[1][1],
|
|
|
|
z: _camera[1][2]
|
|
|
|
},
|
|
|
|
planes: []
|
|
|
|
};
|
|
|
|
|
2015-11-27 10:20:13 +01:00
|
|
|
var recommendationClicked = _camera[2];
|
2015-11-16 09:55:09 +01:00
|
|
|
|
|
|
|
for (i = 3; i < _camera.length; i++) {
|
|
|
|
|
|
|
|
cameraFrustum.planes.push({
|
|
|
|
normal: {
|
|
|
|
x: _camera[i][0],
|
|
|
|
y: _camera[i][1],
|
|
|
|
z: _camera[i][2]
|
|
|
|
},
|
|
|
|
constant: _camera[i][3]
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
// Create config for proportions of chunks
|
|
|
|
var config;
|
2015-11-27 10:20:13 +01:00
|
|
|
var didPrefetch = false;
|
2015-11-17 10:41:05 +01:00
|
|
|
|
2015-11-27 10:20:13 +01:00
|
|
|
if (!self.prefetch || (recommendationClicked === null && score < 0.85)) {
|
2015-11-17 10:41:05 +01:00
|
|
|
|
2015-11-27 10:20:13 +01:00
|
|
|
console.log("Score not good enough, no prefetch");
|
|
|
|
config = [{ frustum: cameraFrustum, proportion: 1}];
|
|
|
|
|
|
|
|
} else if (recommendationClicked !== null) {
|
|
|
|
|
|
|
|
console.log("Recommendation is clicking : full for " + JSON.stringify(self.mesh.recommendations[recommendationClicked].position));
|
|
|
|
config = [{frustum: cameraFrustum, proportion:0.5}, {frustum : self.mesh.recommendations[recommendationClicked], proportion: 0.5}];
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.log("Good % (" + score + "), allow some prefetching");
|
|
|
|
|
|
|
|
didPrefetch = true;
|
2015-11-17 10:41:05 +01:00
|
|
|
config = [{ frustum: cameraFrustum, proportion : 0.5}];
|
2015-11-27 10:20:13 +01:00
|
|
|
// config = [];
|
2015-11-17 10:41:05 +01:00
|
|
|
|
|
|
|
// Find best recommendation
|
|
|
|
var bestReco;
|
|
|
|
var bestScore = -Infinity;
|
|
|
|
var bestIndex = null;
|
|
|
|
|
|
|
|
if (self.predictionTable !== undefined) {
|
|
|
|
|
|
|
|
var sum = 0;
|
|
|
|
|
2015-11-27 10:20:13 +01:00
|
|
|
for (var i = 1; i <= self.mesh.recommendations.length; i++) {
|
2015-11-16 09:55:09 +01:00
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
sum += self.predictionTable[self.previousReco][i];
|
2015-11-16 09:55:09 +01:00
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
}
|
|
|
|
|
2015-11-27 10:20:13 +01:00
|
|
|
for (var i = 1; i <= self.mesh.recommendations.length; i++) {
|
2015-11-17 10:41:05 +01:00
|
|
|
|
|
|
|
if (self.predictionTable[self.previousReco][i] > 0) {
|
|
|
|
|
|
|
|
config.push({
|
2015-06-17 17:11:23 +02:00
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
proportion : self.predictionTable[self.previousReco][i] / (2 * sum),
|
2015-11-27 10:20:13 +01:00
|
|
|
frustum : self.mesh.recommendations[i-1]
|
2015-11-16 09:55:09 +01:00
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
});
|
2015-11-16 09:55:09 +01:00
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
}
|
2015-11-16 09:55:09 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
// console.log(config.map(function(o) { return o.proportion; }));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// For sponza
|
|
|
|
bestReco = self.mesh.recommendations[0];
|
|
|
|
|
2015-11-16 09:55:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-06-15 17:04:19 +02:00
|
|
|
|
|
|
|
// Send next elements
|
2015-11-17 10:41:05 +01:00
|
|
|
var oldTime = Date.now();
|
2015-11-16 09:55:09 +01:00
|
|
|
var next = self.nextElements(config);
|
2015-06-15 17:04:19 +02:00
|
|
|
|
2015-11-27 10:20:13 +01:00
|
|
|
// console.log(
|
|
|
|
// 'Adding ' +
|
|
|
|
// next.size +
|
|
|
|
// ' for newConfig : '
|
|
|
|
// + JSON.stringify(config.map(function(o) { return o.proportion}))
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
|
|
console.log(next.configSizes);
|
2015-11-17 10:41:05 +01:00
|
|
|
|
|
|
|
// console.log('Time to generate chunk : ' + (Date.now() - oldTime) + 'ms');
|
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
if (self.prefetch && next.size < self.chunk) {
|
2015-11-17 10:41:05 +01:00
|
|
|
|
2015-11-27 10:20:13 +01:00
|
|
|
console.log("Chunk not full : prefetch reco");
|
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
// Recompute config
|
|
|
|
var newConfig = [];
|
|
|
|
var sum = 0;
|
|
|
|
|
2015-11-27 10:20:13 +01:00
|
|
|
if (!didPrefetch) {
|
|
|
|
|
|
|
|
if (self.predictionTable !== undefined) {
|
|
|
|
|
|
|
|
var sum = 0;
|
|
|
|
|
|
|
|
for (var i = 1; i <= self.mesh.recommendations.length; i++) {
|
|
|
|
|
|
|
|
sum += self.predictionTable[self.previousReco][i];
|
2015-11-17 10:41:05 +01:00
|
|
|
|
2015-11-27 10:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 1; i <= self.mesh.recommendations.length; i++) {
|
|
|
|
|
|
|
|
if (self.predictionTable[self.previousReco][i] > 0) {
|
|
|
|
|
|
|
|
newConfig.push({
|
|
|
|
|
|
|
|
proportion : self.predictionTable[self.previousReco][i] / (sum),
|
|
|
|
frustum : self.mesh.recommendations[i-1]
|
|
|
|
|
|
|
|
});
|
2015-11-17 10:41:05 +01:00
|
|
|
|
2015-11-27 10:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-11-17 10:41:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-27 10:20:13 +01:00
|
|
|
} else {
|
|
|
|
|
|
|
|
for (var i = 0; i < config.length; i++) {
|
|
|
|
|
|
|
|
// Check if config was full
|
|
|
|
if (next.configSizes[i] >= self.chunk * config[i].proportion) {
|
2015-11-17 10:41:05 +01:00
|
|
|
|
2015-11-27 10:20:13 +01:00
|
|
|
newConfig.push(config[i]);
|
|
|
|
sum += config[i].proportion;
|
2015-11-17 10:41:05 +01:00
|
|
|
|
2015-11-27 10:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize config probabilities
|
|
|
|
for (var i = 0; i < newConfig.length; i++) {
|
|
|
|
|
|
|
|
newConfig[i].proportion /= sum;
|
|
|
|
|
|
|
|
}
|
2015-11-17 10:41:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-18 16:13:01 +01:00
|
|
|
var newData = self.nextElements(newConfig, self.chunk - next.size);
|
|
|
|
|
|
|
|
next.data.push.apply(next.data, newData.data);
|
2015-11-17 10:41:05 +01:00
|
|
|
|
2015-11-27 10:20:13 +01:00
|
|
|
// console.log(
|
|
|
|
// 'Adding ' +
|
|
|
|
// newData.size +
|
|
|
|
// ' for newConfig : '
|
|
|
|
// + JSON.stringify(newConfig.map(function(o) { return o.proportion}))
|
|
|
|
// );
|
2015-11-18 16:13:01 +01:00
|
|
|
|
|
|
|
next.size = next.size + newData.size;
|
2015-11-17 10:41:05 +01:00
|
|
|
|
2015-06-26 09:22:32 +02:00
|
|
|
}
|
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
if (next.size < self.chunk) {
|
2015-11-18 16:13:01 +01:00
|
|
|
|
2015-11-27 10:20:13 +01:00
|
|
|
console.log("Chunk not full : fill linear");
|
|
|
|
|
2015-11-18 16:13:01 +01:00
|
|
|
// If nothing, just serve stuff
|
|
|
|
var tmp = self.nextElements([
|
|
|
|
// {
|
|
|
|
// proportion: 1,
|
|
|
|
// frustum: cameraFrustum
|
|
|
|
// }
|
2015-11-24 09:26:12 +01:00
|
|
|
], self.chunk - next.size);
|
|
|
|
|
|
|
|
next.data.push.apply(next.data, tmp.data);
|
|
|
|
next.size += tmp.size;
|
2015-11-18 16:13:01 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-27 10:20:13 +01:00
|
|
|
console.log('Chunk of size ' + next.size + ' (generated in ' + (Date.now() - oldTime) + 'ms)');
|
2015-11-24 09:26:12 +01:00
|
|
|
// console.log('Time to generate chunk : ' + (Date.now() - oldTime) + 'ms');
|
2015-11-18 16:13:01 +01:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
if (next.data.length === 0) {
|
2015-06-17 17:11:23 +02:00
|
|
|
|
2015-06-18 11:46:24 +02:00
|
|
|
socket.disconnect();
|
2015-06-16 16:35:43 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
} else {
|
|
|
|
|
|
|
|
socket.emit('elements', next.data);
|
|
|
|
|
2015-06-18 11:46:24 +02:00
|
|
|
}
|
2015-06-16 16:35:43 +02:00
|
|
|
|
2015-06-18 11:46:24 +02:00
|
|
|
});
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-15 17:04:19 +02:00
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Prepare the array of materials
|
|
|
|
* @return array the array to send with all materials of the current mesh
|
|
|
|
*/
|
2015-06-18 16:58:07 +02:00
|
|
|
geo.MeshStreamer.prototype.nextMaterials = function() {
|
|
|
|
|
|
|
|
var data = [];
|
|
|
|
|
2015-07-07 14:40:32 +02:00
|
|
|
data.push(['g', this.mesh.numberOfFaces]);
|
2015-07-07 11:47:21 +02:00
|
|
|
|
|
|
|
|
2015-06-29 09:50:26 +02:00
|
|
|
for (var i = 0; i < this.mesh.meshes.length; i++) {
|
2015-06-18 16:58:07 +02:00
|
|
|
|
2015-06-29 09:50:26 +02:00
|
|
|
var currentMesh = this.mesh.meshes[i];
|
2015-06-18 16:58:07 +02:00
|
|
|
|
|
|
|
// Send usemtl
|
|
|
|
data.push([
|
|
|
|
'u',
|
|
|
|
currentMesh.material,
|
|
|
|
currentMesh.vertices.length,
|
|
|
|
currentMesh.faces.length,
|
2015-06-29 09:50:26 +02:00
|
|
|
this.mesh.texCoords.length > 0,
|
|
|
|
this.mesh.normals.length > 0
|
2015-06-18 16:58:07 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-18 16:58:07 +02:00
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Prepare the next elements
|
|
|
|
* @param {camera} _camera a camera that can be usefull to do smart streaming (stream
|
|
|
|
* only interesting parts according to the camera
|
|
|
|
* @returns {array} an array of elements ready to send
|
|
|
|
*/
|
2015-11-17 10:41:05 +01:00
|
|
|
geo.MeshStreamer.prototype.nextElements = function(config, chunk) {
|
|
|
|
|
|
|
|
if (chunk === undefined)
|
|
|
|
chunk = this.chunk;
|
2015-06-26 09:22:32 +02:00
|
|
|
|
2015-07-01 10:14:15 +02:00
|
|
|
var i;
|
|
|
|
|
2015-11-16 09:55:09 +01:00
|
|
|
var data = [];
|
2015-06-26 09:22:32 +02:00
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
var configSizes = [];
|
2015-11-24 09:26:12 +01:00
|
|
|
var buffers = [];
|
2015-11-17 10:41:05 +01:00
|
|
|
|
2015-11-16 09:55:09 +01:00
|
|
|
var mightBeCompletetlyFinished = true;
|
2015-06-26 09:22:32 +02:00
|
|
|
|
2015-11-16 09:55:09 +01:00
|
|
|
// BOOM
|
|
|
|
// if (camera != null)
|
|
|
|
// this.mesh.faces.sort(this.faceComparator(camera));
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
if (config.length === 0) {
|
|
|
|
config.push({
|
|
|
|
proportion: 1
|
|
|
|
});
|
|
|
|
}
|
2015-06-18 15:03:06 +02:00
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
totalSize = 0;
|
|
|
|
for (var configIndex = 0; configIndex < config.length; configIndex++) {
|
2015-06-26 09:22:32 +02:00
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
configSizes[configIndex] = 0;
|
2015-11-24 09:26:12 +01:00
|
|
|
buffers[configIndex] = [];
|
2015-06-26 09:22:32 +02:00
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
}
|
2015-06-26 09:22:32 +02:00
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
faceloop:
|
|
|
|
for (var faceIndex = 0; faceIndex < this.mesh.faces.length; faceIndex++) {
|
2015-06-15 17:04:19 +02:00
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
var currentFace = this.mesh.faces[faceIndex];
|
2015-06-15 17:04:19 +02:00
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
if (this.faces[currentFace.index] === true) {
|
2015-06-15 17:04:19 +02:00
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
continue;
|
2015-06-15 17:04:19 +02:00
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
}
|
2015-06-15 17:04:19 +02:00
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
mightBeCompletetlyFinished = false;
|
2015-10-26 15:56:18 +01:00
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
var vertex1 = this.mesh.vertices[currentFace.a];
|
|
|
|
var vertex2 = this.mesh.vertices[currentFace.b];
|
|
|
|
var vertex3 = this.mesh.vertices[currentFace.c];
|
2015-06-16 12:05:29 +02:00
|
|
|
|
2015-11-17 10:41:05 +01:00
|
|
|
for (var configIndex = 0; configIndex < config.length; configIndex++) {
|
|
|
|
|
|
|
|
var currentConfig = config[configIndex];
|
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
var display = false;
|
|
|
|
var exitToContinue = false;
|
|
|
|
var threeVertices = [vertex1, vertex2, vertex3];
|
2015-06-16 12:05:29 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
// Frustum culling
|
|
|
|
if (currentConfig.frustum === undefined || (isInFrustum(threeVertices, currentConfig.frustum.planes) && !this.isBackFace(currentConfig.frustum, currentFace))) {
|
2015-06-17 17:11:23 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
buffers[configIndex].push(currentFace);
|
|
|
|
continue faceloop;
|
2015-06-16 12:05:29 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
}
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
}
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
}
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
var totalSize = 0;
|
|
|
|
var configSize = 0;
|
2015-11-13 10:36:54 +01:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
for (var configIndex = 0; configIndex < config.length; configIndex++) {
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
// Sort buffer
|
|
|
|
if (config[configIndex].frustum !== undefined) {
|
2015-11-27 10:20:13 +01:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
buffers[configIndex].sort(this.faceComparator(config[configIndex].frustum));
|
2015-11-27 10:20:13 +01:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
} else {
|
2015-11-27 10:20:13 +01:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
// console.log("Did not sort");
|
2015-11-27 10:20:13 +01:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
}
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
// Fill chunk
|
|
|
|
for(var i = 0; i < buffers[configIndex].length; i++) {
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
var size = this.pushFace(buffers[configIndex][i], data);
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
totalSize += size;
|
2015-11-27 10:20:13 +01:00
|
|
|
configSizes[configIndex] += size;
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-27 10:20:13 +01:00
|
|
|
if (configSizes[configIndex] > chunk * config[configIndex].proportion) {
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
break;
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
}
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
}
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
if (totalSize > chunk) {
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
// console.log(configIndex, sent/(chunk * currentConfig.proportion));
|
|
|
|
return {data: data, finsihed:false, configSizes: configSizes, size: totalSize};
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
return {data: data, finished: mightBeCompletetlyFinished, configSizes: configSizes, size:totalSize};
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
};
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
geo.MeshStreamer.prototype.pushFace = function(face, buffer) {
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
var totalSize = 0;
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
var vertex1 = this.mesh.vertices[face.a];
|
|
|
|
var vertex2 = this.mesh.vertices[face.b];
|
|
|
|
var vertex3 = this.mesh.vertices[face.c];
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
// Send face
|
|
|
|
if (!this.vertices[face.a]) {
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
buffer.push(vertex1.toList());
|
|
|
|
this.vertices[face.a] = true;
|
|
|
|
totalSize++;
|
2015-06-16 16:35:43 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
}
|
2015-06-16 16:35:43 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
if (!this.vertices[face.b]) {
|
2015-06-16 16:35:43 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
buffer.push(vertex2.toList());
|
|
|
|
this.vertices[face.b] = true;
|
|
|
|
totalSize++;
|
2015-06-16 16:35:43 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
}
|
2015-06-16 16:35:43 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
if (!this.vertices[face.c]) {
|
2015-06-19 11:24:37 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
buffer.push(vertex3.toList());
|
|
|
|
this.vertices[face.c] = true;
|
|
|
|
totalSize++;
|
2015-06-19 11:24:37 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
}
|
2015-06-19 11:24:37 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
var normal1 = this.mesh.normals[face.aNormal];
|
|
|
|
var normal2 = this.mesh.normals[face.bNormal];
|
|
|
|
var normal3 = this.mesh.normals[face.cNormal];
|
2015-06-16 16:35:43 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
if (normal1 !== undefined && !this.normals[face.aNormal]) {
|
2015-06-17 17:11:23 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
buffer.push(normal1.toList());
|
|
|
|
this.normals[face.aNormal] = true;
|
|
|
|
totalSize++;
|
2015-11-16 09:55:09 +01:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
}
|
2015-06-19 11:24:37 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
if (normal2 !== undefined && !this.normals[face.bNormal]) {
|
2015-11-18 16:13:01 +01:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
buffer.push(normal2.toList());
|
|
|
|
this.normals[face.bNormal] = true;
|
|
|
|
totalSize++;
|
2015-06-15 17:04:19 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
}
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
if (normal3 !== undefined && !this.normals[face.cNormal]) {
|
2015-06-18 11:46:24 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
buffer.push(normal3.toList());
|
|
|
|
this.normals[face.cNormal] = true;
|
|
|
|
totalSize++;
|
2015-06-19 11:24:37 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var tex1 = this.mesh.texCoords[face.aTexture];
|
|
|
|
var tex2 = this.mesh.texCoords[face.bTexture];
|
|
|
|
var tex3 = this.mesh.texCoords[face.cTexture];
|
|
|
|
|
|
|
|
if (tex1 !== undefined && !this.texCoords[face.aTexture]) {
|
|
|
|
|
|
|
|
buffer.push(tex1.toList());
|
|
|
|
this.texCoords[face.aTexture] = true;
|
|
|
|
totalSize++;
|
2015-11-16 09:55:09 +01:00
|
|
|
|
2015-06-18 11:46:24 +02:00
|
|
|
}
|
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
if (tex2 !== undefined && !this.texCoords[face.bTexture]) {
|
|
|
|
|
|
|
|
buffer.push(tex2.toList());
|
|
|
|
this.texCoords[face.bTexture] = true;
|
|
|
|
totalSize++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tex3 !== undefined && !this.texCoords[face.cTexture]) {
|
|
|
|
|
|
|
|
buffer.push(tex3.toList());
|
|
|
|
this.texCoords[face.cTexture] = true;
|
|
|
|
totalSize++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.push(face.toList());
|
|
|
|
// this.meshFaces[meshIndex] = this.meshFaces[meshIndex] || [];
|
|
|
|
this.faces[face.index] = true;
|
|
|
|
totalSize+=3;
|
2015-06-16 16:35:43 +02:00
|
|
|
|
2015-11-24 09:26:12 +01:00
|
|
|
return totalSize;
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|
2015-06-15 17:04:19 +02:00
|
|
|
|
2015-06-29 10:43:29 +02:00
|
|
|
geo.MeshStreamer.prototype.isFinished = function(i) {
|
|
|
|
|
|
|
|
return this.meshFaces[i].counter === this.meshFaces[i].array.length;
|
|
|
|
|
2015-07-01 10:14:15 +02:00
|
|
|
};
|