3d-interface/analysis/server-replay/main.js

232 lines
7.7 KiB
JavaScript
Raw Normal View History

2015-11-18 16:13:01 +01:00
"use strict";
2015-11-24 09:26:12 +01:00
require('app-module-path').addPath(__dirname + '/../../server/lib/');
2015-11-18 16:13:01 +01:00
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
let fs = require('fs');
let THREE = require('./three.js');
2015-11-24 09:26:12 +01:00
let L3D = require('../../static/js/l3d.min.js');
let Serial = require('Serial');
let XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
2015-11-18 16:13:01 +01:00
2015-11-24 09:26:12 +01:00
// Start
2015-11-18 16:13:01 +01:00
let width = Math.floor(1134);
let height = Math.floor(768);
2015-11-24 09:26:12 +01:00
let imageNumber = 0;
let info = {};
let progLoader;
let modelMap;
let smallMap;
let smallModel;
let triangleMeshes = [];
2015-11-27 10:20:13 +01:00
let colorToFace = [];
2015-11-18 16:13:01 +01:00
let renderer = new THREE.CanvasRenderer();
renderer.domElement.style = renderer.domElement;
renderer.setSize(width,height);
renderer.setClearColor(0x000000);
2015-11-24 09:26:12 +01:00
let id = process.argv[2] === undefined ? 56 : parseInt(process.argv[2]);
2015-11-18 16:13:01 +01:00
let scene = new THREE.Scene();
let camera = new THREE.PerspectiveCamera(75, width / height, 1, 10000);
camera.position.z = 1000;
let geometry = new THREE.BoxGeometry(200, 200, 200);
let material = new THREE.MeshBasicMaterial({color: 0xff0000});
let mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
scene.add(camera);
let counter = 0;
2015-11-27 10:20:13 +01:00
let forceFinished = false;
2015-11-18 16:13:01 +01:00
2015-11-24 09:26:12 +01:00
init()
function init() {
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) {
let 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);
let path = initElements(camera, data.sceneInfo, data.redCoins);
if (process.argv[3] === 'null') {
info.camera = null;
info.sort = false;
} else if (process.argv[3] === 'cull') {
info.camera = camera;
info.sort = false;
} else if (process.argv[3] === 'reco') {
info.camera = camera;
info.sort = true;
}
progLoader = new L3D.ProgressiveLoader(
2015-11-27 10:20:13 +01:00
buildPathBigObj(path), new THREE.Object3D(), info.camera, null, function(a,b) { /* process.stderr.write((100*a/b) + '%\n'); */ }, false, info.sort
2015-11-24 09:26:12 +01:00
);
// Init variables
modelMap = JSON.parse(fs.readFileSync(buildPathMap(path), 'utf-8'));
let bigModel = Serial.loadFromFile(buildPathBigObjStatic(path));
smallModel = Serial.loadFromFile(buildPathSmallObjStatic(path));
// Build triangleMeshes
let material = new THREE.MeshFaceMaterial();
// For each small triangle
let counter = 0;
for (let key in modelMap) {
let geometry = new THREE.Geometry();
geometry.vertices = bigModel.children[0].geometry.vertices;
for (let i = 0; i < modelMap[key].length; i++) {
let face = modelMap[key][i];
let split = face.split('-').map(function(o) { return parseInt(o,10) - 1; });
let face3 = new THREE.Face3(split[0], split[1], split[2]);
face3.materialIndex = counter;
material.materials.push(new THREE.MeshBasicMaterial({color: counter, overdraw: true}));
2015-11-27 10:20:13 +01:00
colorToFace[counter] = face3;
2015-11-24 09:26:12 +01:00
geometry.faces.push(face3);
counter++;
}
let name = key.split('-').map(function(o) { return parseInt(o,10)-1; }).join('-');
triangleMeshes[name] = new THREE.Mesh(geometry, material);
scene.add(triangleMeshes[name]);
}
2015-11-18 16:13:01 +01:00
2015-11-27 10:20:13 +01:00
process.stderr.write('--> Loading complete.\n');
2015-11-18 16:13:01 +01:00
2015-11-24 09:26:12 +01:00
progLoader.onBeforeEmit = loop;
progLoader.load(function() {
2015-11-27 10:20:13 +01:00
process.stderr.write("--> Loading complete\n");
2015-11-24 09:26:12 +01:00
forceFinished = true;
});
scene.add(camera);
camera.reset();
camera.speed = 0.001;
camera.start();
}
};
xhr.send();
}
function buildPathSmallObj(path) { return '/static/data/' + path.folder + '/' + path.name + '.obj'; }
function buildPathBigObj(path) { return '/static/data/' + path.folder + '/' + path.name + '_sub' + '.obj'; }
function buildPathSmallObjStatic(path) { return './models/' + path.name + '.json'; }
function buildPathBigObjStatic(path) { return './models/' + path.name + '_sub' + '.json'; }
function buildPathMap(path) { return './maps/' + path.name + '.json'; }
function loop() {
2015-11-27 10:20:13 +01:00
process.stderr.write('--> ' + imageNumber + '\n');
2015-11-24 09:26:12 +01:00
2015-11-27 10:20:13 +01:00
for (let i = 0; i < 10; i++)
camera.update(20);
2015-11-24 09:26:12 +01:00
camera.look();
2015-11-27 10:20:13 +01:00
process.stderr.write('--> Rendering...\n');
2015-11-24 09:26:12 +01:00
let lastTime = Date.now();
2015-11-18 16:13:01 +01:00
renderer.render(scene, camera);
2015-11-27 10:20:13 +01:00
process.stderr.write('--> Renderered in ' + (Date.now() - lastTime) + '\n');
2015-11-24 09:26:12 +01:00
2015-11-27 10:20:13 +01:00
fs.writeFileSync(__dirname + '/img' + (info.sort ? '1' : '') + '/' + pad(imageNumber++, 5) + '.png', renderer.domElement.toBuffer());
2015-11-18 16:13:01 +01:00
2015-11-27 10:20:13 +01:00
let score = 0;
let denom = 0;
// Traverse image to find faces
lastTime = Date.now();
let pixelData = renderer.getContext()
.getImageData(0, 0, renderer.domElement.width, renderer.domElement.height).data;
for (let i = 0; i < pixelData.length; i += 4) {
let pixelNumber = (pixelData[i] << 16) + (pixelData[i+1] << 8) + (pixelData[i+2]);
if (pixelNumber !== 0) {
denom++;
try {
if (progLoader.hasFace(colorToFace[pixelNumber])) {
score++;
}
} catch(e) {
process.stderr.write('--> ' + pixelNumber + '\n');
}
}
}
process.stderr.write('--> Computed pixels in ' + (Date.now() - lastTime) + 'ms\n');
score /= denom;
process.stderr.write('--> Score : ' + score + '\n');
console.log(score);
2015-11-18 16:13:01 +01:00
}
2015-11-24 09:26:12 +01:00
function initElements(camera, sceneInfo, redCoins) {
switch (sceneInfo.sceneId) {
case 1:
camera.resetElements = L3D.resetPeachElements();
camera.cameras = L3D.createPeachRecommendations(width, height);
camera.speed = 0.001;
camera.coins = L3D.generateCoins(L3D.createPeachCoins(), redCoins);
return '/static/data/castle/princess peaches castle (outside)_sub.obj';
case 2:
camera.resetElements = L3D.resetBobombElements();
camera.cameras = L3D.createBobombRecommendations(width, height);
camera.speed = 0.005;
// camera.coins = L3D.generateCoins(L3D.createBobombCoins(), redCoins);
return {name: 'bobomb battlefeild', folder:'bobomb'};
case 3:
camera.resetElements = L3D.resetMountainElements();
camera.cameras = L3D.createMountainRecommendations(width, height);
camera.speed = 0.005;
// camera.coins = L3D.generateCoins(L3D.createMountainCoins(), redCoins);
return {name :'coocoolmountain', folder:'mountain'};
case 4:
camera.resetElements = L3D.resetWhompElements();
camera.cameras = L3D.createWhompRecommendations(width, height);
camera.speed = 0.002;
// camera.coins = L3D.generateCoins(L3D.createWhompCoins(), redCoins);
return {name:'Whomps Fortress', folder:'whomp'};
default:
2015-11-27 10:20:13 +01:00
process.stderr.write('--> This sceneId doesn\'t exist\n');
2015-11-24 09:26:12 +01:00
process.exit(-1);
}
}