2015-10-21 15:23:33 +02:00
|
|
|
"use strict";
|
|
|
|
|
2015-10-26 15:56:18 +01:00
|
|
|
// REQUIRES AND INITS
|
2015-10-21 15:23:33 +02:00
|
|
|
let width = Math.floor(1134/10);
|
|
|
|
let height = Math.floor(768 /10);
|
|
|
|
|
|
|
|
let XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
|
|
|
|
let THREE = require('three');
|
|
|
|
let L3D = require('../../static/js/l3d.min.js');
|
|
|
|
let math = require('mathjs');
|
|
|
|
let fs = require('fs');
|
|
|
|
|
2015-10-22 15:37:18 +02:00
|
|
|
let scene = new THREE.Scene();
|
2015-10-21 15:23:33 +02:00
|
|
|
let geometry = new THREE.Geometry();
|
|
|
|
|
|
|
|
let peach = new THREE.Object3D();
|
|
|
|
let bobomb = new THREE.Object3D();
|
|
|
|
let whomp = new THREE.Object3D();
|
|
|
|
let mountain = new THREE.Object3D();
|
|
|
|
|
2015-10-26 15:56:18 +01:00
|
|
|
var finished = false;
|
|
|
|
let frame = 0;
|
|
|
|
let raycaster = new THREE.Raycaster();
|
2015-10-21 15:23:33 +02:00
|
|
|
|
2015-10-26 15:56:18 +01:00
|
|
|
var total = 0;
|
|
|
|
|
|
|
|
let loader, progLoader;
|
2015-10-21 15:23:33 +02:00
|
|
|
|
2015-10-26 15:56:18 +01:00
|
|
|
let id = 56;
|
|
|
|
|
|
|
|
|
|
|
|
// INIT COLORS
|
2015-10-21 15:23:33 +02:00
|
|
|
var colors = [[0,0,0]];
|
|
|
|
|
|
|
|
for (let i = 0; i < 856; i++) {
|
|
|
|
colors.push([
|
|
|
|
Math.floor(255*Math.random()),
|
|
|
|
Math.floor(255*Math.random()),
|
|
|
|
Math.floor(255*Math.random())
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-10-26 15:56:18 +01:00
|
|
|
var camera; var finished = false;
|
|
|
|
var Recommendation = L3D.BaseRecommendation;
|
|
|
|
var forceFinished = false;
|
|
|
|
main();
|
2015-10-22 15:37:18 +02:00
|
|
|
|
2015-10-26 15:56:18 +01:00
|
|
|
// FUNCTIONS
|
2015-10-21 15:23:33 +02:00
|
|
|
function main() {
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
xhr.open("GET", "http://localhost:4000/prototype/replay-info/" + id, true);
|
|
|
|
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (xhr.readyState == 4 && xhr.status == 200) {
|
2015-10-26 15:56:18 +01:00
|
|
|
|
|
|
|
var data = JSON.parse(xhr.responseText);
|
|
|
|
|
|
|
|
console.log('%sceneId = ' + data.sceneInfo.sceneId);
|
|
|
|
console.log('%recoStyle = ' + data.sceneInfo.recommendationStyle);
|
|
|
|
console.log('%expId = ' + id);
|
|
|
|
console.log();
|
|
|
|
console.log('M = [');
|
|
|
|
|
|
|
|
camera = new L3D.ReplayCamera(50, width / height, 0.01, 100000, [], data, () => finished = true);
|
|
|
|
var path = initElements(camera, data.sceneInfo);
|
|
|
|
|
|
|
|
loader = new L3D.ProgressiveLoader(
|
|
|
|
path, new THREE.Object3D(), null
|
|
|
|
);
|
|
|
|
|
|
|
|
progLoader = new L3D.ProgressiveLoader(
|
|
|
|
path, scene, camera, null, null, true
|
|
|
|
);
|
|
|
|
|
2015-10-21 15:23:33 +02:00
|
|
|
loader.load(function(){
|
2015-10-26 15:56:18 +01:00
|
|
|
process.stderr.write('Loading complete.\n');
|
|
|
|
progLoader.load(function() {
|
|
|
|
process.stderr.write("Loading complete\n");
|
|
|
|
forceFinished = true;
|
|
|
|
});
|
|
|
|
init(data);
|
2015-10-21 15:23:33 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.send();
|
|
|
|
}
|
|
|
|
|
2015-10-26 15:56:18 +01:00
|
|
|
function testDistance(old, newP) {
|
|
|
|
|
|
|
|
return (
|
|
|
|
L3D.Tools.norm2(L3D.Tools.diff(old.position, newP.position)) +
|
|
|
|
L3D.Tools.norm2(L3D.Tools.diff(old.target, newP.target)) > 0.1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-10-21 15:23:33 +02:00
|
|
|
|
2015-10-26 15:56:18 +01:00
|
|
|
function initElements(camera, sceneInfo) {
|
|
|
|
switch (sceneInfo.sceneId) {
|
|
|
|
case 1:
|
|
|
|
camera.resetElements = L3D.resetPeachElements();
|
|
|
|
camera.cameras = L3D.createPeachRecommendations(width, height);
|
|
|
|
camera.speed = 0.001;
|
|
|
|
return '/static/data/castle/princess peaches castle (outside).obj';
|
|
|
|
case 2:
|
|
|
|
camera.resetElements = L3D.resetBobombElements();
|
|
|
|
camera.cameras = L3D.createBobombRecommendations(width, height);
|
|
|
|
camera.speed = 0.005;
|
|
|
|
return '/static/data/bobomb/bobomb battlefeild.obj';
|
|
|
|
case 3:
|
|
|
|
camera.resetElements = L3D.resetMountainElements();
|
|
|
|
camera.cameras = L3D.createMountainRecommendations(width, height);
|
|
|
|
camera.speed = 0.005;
|
|
|
|
return '/static/data/mountain/coocoolmountain.obj';
|
|
|
|
case 4:
|
|
|
|
camera.resetElements = L3D.resetWhompElements();
|
|
|
|
camera.cameras = L3D.createWhompRecommendations(width, height);
|
|
|
|
camera.speed = 0.002;
|
|
|
|
return '/static/data/whomp/Whomps Fortress.obj';
|
|
|
|
default:
|
|
|
|
console.err('This sceneId doesn\'t exist');
|
|
|
|
process.exit(-1);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2015-10-21 15:23:33 +02:00
|
|
|
|
|
|
|
function init(data) {
|
2015-10-22 15:37:18 +02:00
|
|
|
scene.add(camera);
|
2015-10-26 15:56:18 +01:00
|
|
|
|
2015-10-22 15:37:18 +02:00
|
|
|
camera.reset();
|
|
|
|
camera.speed = 0.001;
|
2015-10-21 15:23:33 +02:00
|
|
|
camera.start();
|
|
|
|
setTimeout(loop, 0);
|
|
|
|
}
|
|
|
|
|
2015-10-26 15:56:18 +01:00
|
|
|
function equalFaces(face1, face2) { return face1.a === face2.a && face1.b === face2.b && face1.c === face2.c; };
|
|
|
|
|
2015-10-21 15:23:33 +02:00
|
|
|
function printVector(vec) { console.log(`(${vec.x},${vec.y},${vec.z})`); }
|
|
|
|
|
2015-10-26 15:56:18 +01:00
|
|
|
let old;
|
2015-10-21 15:23:33 +02:00
|
|
|
|
|
|
|
function loop() {
|
|
|
|
|
2015-10-22 15:37:18 +02:00
|
|
|
old = {
|
|
|
|
position: new THREE.Vector3().copy(camera.position),
|
|
|
|
target: new THREE.Vector3().copy(camera.target)
|
|
|
|
};
|
|
|
|
|
2015-10-26 15:56:18 +01:00
|
|
|
for (let i = 0; i < 10; i++)
|
|
|
|
finished = camera.update(20);
|
2015-10-22 15:37:18 +02:00
|
|
|
|
2015-10-26 15:56:18 +01:00
|
|
|
total++;
|
2015-10-22 15:37:18 +02:00
|
|
|
|
2015-10-26 15:56:18 +01:00
|
|
|
if (!testDistance(old, camera) && !finished && !forceFinished) {
|
|
|
|
process.nextTick(loop);
|
|
|
|
return;
|
|
|
|
}
|
2015-10-22 15:37:18 +02:00
|
|
|
|
2015-10-21 15:23:33 +02:00
|
|
|
|
|
|
|
camera.look();
|
2015-10-22 15:37:18 +02:00
|
|
|
camera.updateMatrixWorld(true);
|
|
|
|
|
2015-10-21 15:23:33 +02:00
|
|
|
// printVector(camera.position);
|
|
|
|
|
|
|
|
let buf = [];
|
2015-10-26 15:56:18 +01:00
|
|
|
// let buf2 = [];
|
2015-10-21 15:23:33 +02:00
|
|
|
|
2015-10-26 15:56:18 +01:00
|
|
|
let score = 0;
|
2015-10-21 15:23:33 +02:00
|
|
|
|
2015-10-27 08:10:33 +01:00
|
|
|
var totalPixel = 0;
|
2015-10-21 15:23:33 +02:00
|
|
|
for (let i = 0; i < width; i++) {
|
|
|
|
|
|
|
|
buf[i] = [];
|
|
|
|
|
|
|
|
let x = (i / width) * 2 - 1;
|
|
|
|
|
|
|
|
// process.stderr.write('\b\r' + Math.floor(100*(i / width)) + '%\n');
|
|
|
|
|
|
|
|
for (let j = 0; j < height; j++) {
|
|
|
|
|
|
|
|
let y = (j / height) * 2 - 1;
|
|
|
|
|
|
|
|
raycaster.setFromCamera({x:x, y:y}, camera);
|
|
|
|
|
2015-10-26 15:56:18 +01:00
|
|
|
let intersectsProg = raycaster.intersectObject(progLoader.obj, true);
|
|
|
|
let intersects = raycaster.intersectObject(loader.obj, true);
|
2015-10-21 15:23:33 +02:00
|
|
|
|
2015-10-26 15:56:18 +01:00
|
|
|
if (intersectsProg.length === 0 && intersects.length === 0) {
|
2015-10-27 08:10:33 +01:00
|
|
|
// Not good
|
2015-10-26 15:56:18 +01:00
|
|
|
} else if (intersectsProg.length !== intersects.length) {
|
|
|
|
// Not good
|
2015-10-27 08:10:33 +01:00
|
|
|
totalPixel++;
|
2015-10-26 15:56:18 +01:00
|
|
|
} else if (equalFaces(intersectsProg[0].face, intersects[0].face)) {
|
|
|
|
score++;
|
2015-10-27 08:10:33 +01:00
|
|
|
totalPixel++;
|
|
|
|
} else {
|
|
|
|
totalPixel++;
|
2015-10-26 15:56:18 +01:00
|
|
|
}
|
2015-10-21 15:23:33 +02:00
|
|
|
|
2015-10-26 15:56:18 +01:00
|
|
|
if (intersectsProg.length > 0) {
|
|
|
|
buf[i][j] = intersectsProg[0].faceIndex;
|
2015-10-21 15:23:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-27 08:10:33 +01:00
|
|
|
score /= totalPixel;
|
2015-10-26 15:56:18 +01:00
|
|
|
|
2015-10-21 15:23:33 +02:00
|
|
|
// for (let i = 0; i < 255; i++) {
|
|
|
|
// colorsOccurence[i+1] += colorsOccurence[i];
|
|
|
|
// }
|
|
|
|
|
2015-10-22 15:37:18 +02:00
|
|
|
var buffer = [];
|
|
|
|
buffer.push('P3\n');
|
|
|
|
buffer.push(width+'\n');
|
|
|
|
buffer.push(height+'\n');
|
|
|
|
buffer.push(255+'\n');
|
2015-10-21 15:23:33 +02:00
|
|
|
|
|
|
|
for (let i = 0; i < height; i++) {
|
|
|
|
for (let j = 0; j < width; j++) {
|
|
|
|
var grey = buf[j][height - i - 1];
|
|
|
|
if (colors[grey] === undefined)
|
2015-10-22 15:37:18 +02:00
|
|
|
buffer.push('0 0 0\n');
|
2015-10-21 15:23:33 +02:00
|
|
|
else {
|
2015-10-22 15:37:18 +02:00
|
|
|
buffer.push(colors[grey][0] + ' ' + colors[grey][1] + ' ' + colors[grey][2] + '\n');
|
2015-10-21 15:23:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 15:37:18 +02:00
|
|
|
let frameName = "" + frame;
|
|
|
|
var pad = "00000";
|
|
|
|
frameName = pad.substring(0, pad.length - frameName.length) + frameName;
|
|
|
|
fs.writeFileSync(`img/${frameName}.ppm`, buffer.join(''));
|
2015-10-21 15:23:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Write image buffer to disk
|
2015-10-26 15:56:18 +01:00
|
|
|
console.log(score);
|
|
|
|
process.stderr.write('Frame : ' + frame++ + ', score = ' + score + '\n');
|
2015-10-21 15:23:33 +02:00
|
|
|
|
2015-10-26 15:56:18 +01:00
|
|
|
if (!finished && !forceFinished)
|
|
|
|
setTimeout(loop,50);
|
2015-10-21 15:23:33 +02:00
|
|
|
}
|