Prefetching working now

This commit is contained in:
Thomas FORGIONE 2015-11-17 10:41:05 +01:00
parent 844982e552
commit f4e06c9c28
5 changed files with 302 additions and 288 deletions

View File

@ -1,105 +0,0 @@
#!/usr/bin/node
var lib = require('./lib.js');
var r = require('./initScene.js');
var reco = [
r.createPeachRecommendations(),
r.createBobombRecommendations(),
r.createMountainRecommendations(),
r.createWhompRecommendations()
];
function distanceBetweenPoints(pt1, pt2) {
return (
Math.sqrt(
(pt2.x - pt1.x) * (pt2.x - pt1.x) +
(pt2.y - pt1.y) * (pt2.y - pt1.y) +
(pt2.z - pt1.z) * (pt2.z - pt1.z)
)
);
}
function main(path) {
var db = lib.loadFromFile(path);
var groups = lib.makeGroups(db);
// Erase groups that are not usable
var invalid = 0;
groups = groups.filter(function(elt) {
// An elt is valid if it contains at least 2 exp, BaseRecommendation included
if (elt.length > 1 && elt.find(function(e) { return e.recommendation_style[4] === 'B'; }) !== undefined) {
return true
} else {
invalid++;
return false;
}
});
var percentSum = 0;
var eltNum = 0;
for (var i = 0; i < db.experiments.length; i++) {
var exp = db.experiments[i];
if (exp.coinCombination.scene_id === 1 || exp.recommendation_style[4] === 'B' || !exp.finished || lib.coinsGot(exp) < 6) {
continue;
}
eltNum++;
var distance = 0;
var distanceWithReco = 0;
var j = 0;
while (exp.elements.events[j].position === undefined) { j++; };
var startPosition = exp.elements.events[j].position;
j++;
while (j < exp.elements.events.length) {
var nextPosition, evt = exp.elements.events[j];
if (evt.position === undefined && evt.type !== 'arrow') { j++; continue; }
if (evt.type === 'arrow') {
nextPosition = reco[exp.coinCombination.scene_id - 1][evt.id].position;
} else {
nextPosition = evt.position;
}
var tmp = distanceBetweenPoints(startPosition, nextPosition);
if (evt.type === 'arrow') {
distanceWithReco += tmp;
}
distance += tmp;
startPosition = nextPosition;
j++;
}
percentSum += 100 * distanceWithReco / distance;
console.log(exp.id + ' -> ' + Math.floor(100 * distanceWithReco / distance) + '%');
}
console.log('Mean : ' + percentSum / eltNum);
}
if (process.argv.length !== 3) {
process.stderr.write('Error : please give me a JSON file to work on\n');
process.exit(-1);
}
main(process.argv[2])

View File

@ -181,6 +181,8 @@ L3D.PointerCamera = function() {
* @param {Object}
*/
this.resetElements = {position: new THREE.Vector3(0,1,1), target: new THREE.Vector3()};
this.recommendationClicked = null;
};
L3D.PointerCamera.prototype = Object.create(THREE.PerspectiveCamera.prototype);
L3D.PointerCamera.prototype.constructor = L3D.PointerCamera;
@ -323,6 +325,7 @@ L3D.PointerCamera.prototype.hermiteMotion = function(time) {
if (this.t > 1) {
this.movingHermite = false;
this.recommendationClicked = null;
this.anglesFromVectors();
}
};
@ -491,6 +494,8 @@ L3D.PointerCamera.prototype.moveHermite = function(recommendation, toSave) {
var otherCamera = recommendation.camera || recommendation;
this.recommendationClicked = otherCamera;
this.moving = false;
this.movingHermite = true;
this.t = 0;
@ -762,7 +767,9 @@ L3D.PointerCamera.prototype.toList = function() {
var ret =
[[this.position.x, this.position.y, this.position.z],
[this.target.x, this.target.y, this.target.z]];
[this.target.x, this.target.y, this.target.z],
this.recommendationClicked !== null
];
for (var i = 0; i < frustum.planes.length; i++) {

View File

@ -1,5 +1,15 @@
L3D.ProgressiveLoader = (function() {
if (typeof process === 'undefined') {
process = {
stderr : {
write: function(str) {
console.log(str);
}
}
};
}
/**
* Parse a list as it is sent by the server and gives a slightly more comprehensible result
* @param Array array corresponding to the line of the mesh file
@ -88,7 +98,7 @@ var _parseList = function(arr) {
* @constructor
* @memberOf L3D
*/
var ProgressiveLoader = function(path, scene, camera, callback, log, laggy) {
var ProgressiveLoader = function(path, scene, camera, callback, log, laggy, prefetch) {
var self = this;
@ -186,12 +196,24 @@ var ProgressiveLoader = function(path, scene, camera, callback, log, laggy) {
*/
this.camera = camera;
if (this.camera instanceof L3D.ReplayCamera) {
this.camera._moveReco = this.camera.moveReco;
this.camera.moveReco = function(param) {
self.socket.emit('reco', param);
self.camera._moveReco.apply(self.camera, arguments);
};
} else if (this.camera instanceof L3D.PointerCamera) {
// Only good for sponza model
this.camera._moveHermite = this.camera.moveHermite;
this.camera.moveHermite = function(param) {
var toSend = param.position.x > 0 ? 0 : 1;
self.socket.emit('reco', toSend);
self.camera._moveHermite.apply(self.camera, arguments);
};
}
/**
* Number of total elements for loading
@ -220,6 +242,9 @@ var ProgressiveLoader = function(path, scene, camera, callback, log, laggy) {
this.mapFace = {};
this.prefetch = prefetch === undefined ? true : (!!prefetch);
console.log(this.prefetch);
};
ProgressiveLoader.prototype.hasFace = function(face) {
@ -277,6 +302,8 @@ ProgressiveLoader.prototype.initIOCallbacks = function() {
this.socket.on('elements', function(arr) {
process.stderr.write('Received ' + arr.length + '\n');
for (var i = 0; i < arr.length; i++) {
if (typeof self.log === 'function' && self.numberOfFacesReceived % self.modulus === 0) {
@ -360,6 +387,8 @@ ProgressiveLoader.prototype.initIOCallbacks = function() {
self.numberOfFacesReceived++;
self.mapFace[elt.a + '-' + elt.b + '-' + elt.c] = true;
if (!self.meshes[elt.mesh].added) {
self.meshes[elt.mesh].added = true;
@ -392,7 +421,6 @@ ProgressiveLoader.prototype.initIOCallbacks = function() {
}
self.mapFace[elt.a + '-' + elt.b + '-' + elt.c] = true;
} else if (elt.type === 'global') {
@ -436,7 +464,7 @@ ProgressiveLoader.prototype.initIOCallbacks = function() {
* Starts the communication with the server
*/
ProgressiveLoader.prototype.start = function() {
this.socket.emit('request', this.objPath, this.laggy);
this.socket.emit('request', this.objPath, this.laggy, this.prefetch);
};
return ProgressiveLoader;

View File

@ -728,6 +728,10 @@ L3D.createSponzaRecommendations = function(width, height) {
createRecommendation(
new THREE.Vector3(97.36225946503932,10.925697484337014,12.852363038244272),
new THREE.Vector3(133.315101552449,18.576354168001703,-2.9229530646577633)
),
createRecommendation(
new THREE.Vector3(-110.4869853758238,17.692671522169423,14.022902297589127),
new THREE.Vector3(-147.9067343700736,16.890814116754584,-0.08806541935797796)
)
];
};

View File

@ -5,9 +5,12 @@ var L3D = require('../../static/js/l3d.min.js');
try
{
var predictionTables = [
JSON.parse(fs.readFileSync('./mat1.json')),
JSON.parse(fs.readFileSync('./mat2.json')),
JSON.parse(fs.readFileSync('./mat3.json'))
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]]
];
} catch (e) {
process.stderr.write('No prefetching will be done !');
@ -156,7 +159,7 @@ geo.MeshStreamer = function(path) {
* Number of element to send by packet
* @type {Number}
*/
this.chunk = 500;
this.chunk = 5000;
this.previousReco = 0;
@ -278,12 +281,14 @@ geo.MeshStreamer.prototype.start = function(socket) {
var self = this;
socket.on('request', function(path, laggy) {
socket.on('request', function(path, laggy, prefetch) {
if (laggy === true) {
self.chunk = 1;
}
self.prefetch = prefetch;
self.mesh = geo.availableMeshes[path];
switch (path) {
@ -299,6 +304,8 @@ geo.MeshStreamer.prototype.start = function(socket) {
case '/static/data/whomp/Whomps Fortress_sub.obj':
self.predictionTable = predictionTables[2];
break;
default:
self.predictionTable = predictionTables[3];
};
if (self.mesh === undefined) {
@ -358,7 +365,7 @@ geo.MeshStreamer.prototype.start = function(socket) {
planes: []
};
var shouldPrefetch = _camera[2];
var fullFrustum = _camera[2];
for (i = 3; i < _camera.length; i++) {
@ -375,26 +382,45 @@ geo.MeshStreamer.prototype.start = function(socket) {
}
// Create config for proportions of chunks
var config;
if (self.prefetch) {
config = [{ frustum: cameraFrustum, proportion : 0.5}];
// Find best recommendation
var bestReco;
var bestScore = -Infinity;
var bestIndex = null;
if (self.predictionTable !== undefined) {
var sum = 0;
for (var i = 0; i < self.mesh.recommendations.length; i++) {
if (self.predictionTable[self.previousReco][i+1] > bestScore) {
sum += self.predictionTable[self.previousReco][i];
bestReco = self.mesh.recommendations[i];
bestScore = self.predictionTable[self.previousReco][i+1];
bestIndex = i;
}
for (var i = 0; i < self.mesh.recommendations.length; i++) {
if (self.predictionTable[self.previousReco][i] > 0) {
config.push({
proportion : self.predictionTable[self.previousReco][i] / (2 * sum),
frustum : self.mesh.recommendations[i]
});
}
}
// console.log(config.map(function(o) { return o.proportion; }));
} else {
// For sponza
@ -402,33 +428,76 @@ geo.MeshStreamer.prototype.start = function(socket) {
}
// Create config for proportions of chunks
var config = [];
if (!fullFrustum) {
if (shouldPrefetch) {
// console.log('Frustum and prefetch : ' + (cameraFrustum !== undefined) + ' ' + (bestReco !== undefined));
// Camera cull
config.push({
proportion: 0.5,
} else {
// console.log('Full frustum fetching (reco clicked)');
config = [{
proportion: 1,
frustum: cameraFrustum
});
}];
config.push({
proportion: 0.5,
frustum: bestReco
});
}
} else {
config = [{frustum: cameraFrustum, proportion: 1}];
}
// Send next elements
var oldTime = Date.now();
var next = self.nextElements(config);
// console.log(next.configSizes);
// console.log('Time to generate chunk : ' + (Date.now() - oldTime) + 'ms');
if (next.data.length === 0) {
// If nothing, just serve stuff
var tmp = self.nextElements();
var tmp = self.nextElements([
// {
// proportion: 1,
// frustum: cameraFrustum
// }
]);
next.data = tmp.data;
} else if (!self.prefetch && next.size < this.chunk) {
// Recompute config
var newConfig = [];
var sum = 0;
for (var i = 0; i < config.length; i++) {
// Check if config was full
if (next.configSizes[i] < this.chunk * config[i].proportion) {
newConfig.push(config[i]);
sum += config[i].proportion;
}
}
for (var i = 0; i < newConfig.length; i++) {
newConfig[i].proportion /= sum;
}
// Normalize config probabilities
var newData = self.nextElements(newConfig, this.chunk - next.size);
next.data = next.data.push.apply(next.data, newData.data);
}
socket.emit('elements', next.data);
@ -479,33 +548,37 @@ 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(config) {
geo.MeshStreamer.prototype.nextElements = function(config, chunk) {
if (chunk === undefined)
chunk = this.chunk;
var i;
var data = [];
var configSizes = [];
var mightBeCompletetlyFinished = true;
// BOOM
// if (camera != null)
// this.mesh.faces.sort(this.faceComparator(camera));
if (config === undefined) { config = [] }
configloop:
for (var configIndex = 0; configIndex < config.length + 1; configIndex++) {
var sent = 0;
// config[0] should always be cameraFrustum, with eventually 0 proportion
var currentConfig = configIndex !== config.length ? config[configIndex] : config[0];
if (currentConfig === undefined) {
currentConfig = {
proportion : Infinity,
}
if (config.length === 0) {
config.push({
proportion: 1
});
}
totalSize = 0;
for (var configIndex = 0; configIndex < config.length; configIndex++) {
configSizes[configIndex] = 0;
}
faceloop:
for (var faceIndex = 0; faceIndex < this.mesh.faces.length; faceIndex++) {
var currentFace = this.mesh.faces[faceIndex];
@ -522,29 +595,25 @@ geo.MeshStreamer.prototype.nextElements = function(config) {
var vertex2 = this.mesh.vertices[currentFace.b];
var vertex3 = this.mesh.vertices[currentFace.c];
if (currentConfig.frustum !== undefined) {
for (var configIndex = 0; configIndex < config.length; configIndex++) {
var currentConfig = config[configIndex];
if ( configSizes[configIndex] < chunk * currentConfig.proportion) {
var display = false;
var exitToContinue = false;
threeVertices = [vertex1, vertex2, vertex3];
var threeVertices = [vertex1, vertex2, vertex3];
// Frustum culling
if (!isInFrustum(threeVertices, currentConfig.frustum.planes)) {
continue;
}
// Backface culling
if (this.isBackFace(currentConfig.frustum, currentFace)) {
continue;
}
}
if (currentConfig.frustum === undefined || (isInFrustum(threeVertices, currentConfig.frustum.planes) && !this.isBackFace(currentConfig.frustum, currentFace))) {
// Send face
if (!this.vertices[currentFace.a]) {
data.push(vertex1.toList());
this.vertices[currentFace.a] = true;
sent++;
configSizes[configIndex]++;
}
@ -552,7 +621,7 @@ geo.MeshStreamer.prototype.nextElements = function(config) {
data.push(vertex2.toList());
this.vertices[currentFace.b] = true;
sent++;
configSizes[configIndex]++;
}
@ -560,7 +629,7 @@ geo.MeshStreamer.prototype.nextElements = function(config) {
data.push(vertex3.toList());
this.vertices[currentFace.c] = true;
sent++;
configSizes[configIndex]++;
}
@ -572,7 +641,8 @@ geo.MeshStreamer.prototype.nextElements = function(config) {
data.push(normal1.toList());
this.normals[currentFace.aNormal] = true;
sent++;
configSizes[configIndex]++;
totalSize++;
}
@ -580,7 +650,8 @@ geo.MeshStreamer.prototype.nextElements = function(config) {
data.push(normal2.toList());
this.normals[currentFace.bNormal] = true;
sent++;
configSizes[configIndex]++;
totalSize++;
}
@ -588,7 +659,8 @@ geo.MeshStreamer.prototype.nextElements = function(config) {
data.push(normal3.toList());
this.normals[currentFace.cNormal] = true;
sent++;
configSizes[configIndex]++;
totalSize++;
}
@ -600,7 +672,8 @@ geo.MeshStreamer.prototype.nextElements = function(config) {
data.push(tex1.toList());
this.texCoords[currentFace.aTexture] = true;
sent++;
configSizes[configIndex]++;
totalSize++;
}
@ -608,7 +681,8 @@ geo.MeshStreamer.prototype.nextElements = function(config) {
data.push(tex2.toList());
this.texCoords[currentFace.bTexture] = true;
sent++;
configSizes[configIndex]++;
totalSize++;
}
@ -616,38 +690,44 @@ geo.MeshStreamer.prototype.nextElements = function(config) {
data.push(tex3.toList());
this.texCoords[currentFace.cTexture] = true;
sent++;
configSizes[configIndex]++;
totalSize++;
}
data.push(currentFace.toList());
// this.meshFaces[meshIndex] = this.meshFaces[meshIndex] || [];
this.faces[currentFace.index] = true;
configSizes[configIndex]+=3;
totalSize+=3;
// this.meshFaces[meshIndex].counter++;
// currentMesh.faceIndex++;
sent++;
// if (totalSize > chunk) {
if (data.length > this.chunk) {
// // console.log(configIndex, sent/(chunk * currentConfig.proportion));
// return {data: data, finsihed:false, configSizes: configSizes, size: totalSize};
// console.log(configIndex, sent/(this.chunk * currentConfig.proportion));
return {data: data, finsihed:false};
// }
// Loop on next face
continue faceloop;
}
}
if (sent > this.chunk * currentConfig.proportion) {
if (totalSize > chunk) {
// console.log(configIndex, sent/(this.chunk * currentConfig.proportion));
continue configloop;
// console.log(configIndex, sent/(chunk * currentConfig.proportion));
return {data: data, finsihed:false, configSizes: configSizes, size: totalSize};
}
}
return {data: data, finished: mightBeCompletetlyFinished};
}
return {data: data, finished: mightBeCompletetlyFinished};
return {data: data, finished: mightBeCompletetlyFinished, configSizes: configSizes};
};