Corrected raycaster's problem
This commit is contained in:
parent
c760c404a4
commit
8ed2b18d49
|
@ -9,7 +9,7 @@ let L3D = require('../../static/js/l3d.min.js');
|
|||
let math = require('mathjs');
|
||||
let fs = require('fs');
|
||||
|
||||
let scene = new THREE.Object3D();
|
||||
let scene = new THREE.Scene();
|
||||
let geometry = new THREE.Geometry();
|
||||
|
||||
let peach = new THREE.Object3D();
|
||||
|
@ -19,7 +19,8 @@ let mountain = new THREE.Object3D();
|
|||
|
||||
let loader = new L3D.ProgressiveLoader(
|
||||
'/static/data/castle/princess peaches castle (outside).obj',
|
||||
peach,
|
||||
// '/static/data/bobomb/bobomb battlefeild.obj',
|
||||
scene,
|
||||
null
|
||||
);
|
||||
|
||||
|
@ -36,6 +37,14 @@ for (let i = 0; i < 856; i++) {
|
|||
]);
|
||||
}
|
||||
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
function main() {
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open("GET", "http://localhost:4000/prototype/replay-info/" + id, true);
|
||||
|
@ -57,7 +66,10 @@ var finished = false;
|
|||
|
||||
function init(data) {
|
||||
camera = new L3D.ReplayCamera(50, width / height, 0.01, 100000, [], data, () => finished = true);
|
||||
scene.add(camera);
|
||||
camera.resetElements = L3D.resetPeachElements();
|
||||
camera.reset();
|
||||
camera.speed = 0.001;
|
||||
camera.start();
|
||||
setTimeout(loop, 0);
|
||||
}
|
||||
|
@ -68,12 +80,29 @@ var finished = false;
|
|||
let frame = 0;
|
||||
let raycaster = new THREE.Raycaster();
|
||||
|
||||
var total = 0;
|
||||
function loop() {
|
||||
|
||||
for (let i = 0; i < 10; i++)
|
||||
camera.update(20);
|
||||
let old;
|
||||
|
||||
old = {
|
||||
position: new THREE.Vector3().copy(camera.position),
|
||||
target: new THREE.Vector3().copy(camera.target)
|
||||
};
|
||||
|
||||
do {
|
||||
|
||||
for (let i = 0; i < 10; i++)
|
||||
camera.update(20);
|
||||
|
||||
total++;
|
||||
console.log(total);
|
||||
|
||||
} while (!testDistance(old, camera) && !finished);
|
||||
|
||||
camera.look();
|
||||
camera.updateMatrixWorld(true);
|
||||
|
||||
// printVector(camera.position);
|
||||
|
||||
let buf = [];
|
||||
|
@ -97,7 +126,7 @@ function loop() {
|
|||
|
||||
raycaster.setFromCamera({x:x, y:y}, camera);
|
||||
|
||||
let intersects = raycaster.intersectObjects(peach.children, true);
|
||||
let intersects = raycaster.intersectObjects(scene.children, true);
|
||||
|
||||
let grey = 0;
|
||||
try {
|
||||
|
@ -120,32 +149,33 @@ function loop() {
|
|||
// colorsOccurence[i+1] += colorsOccurence[i];
|
||||
// }
|
||||
|
||||
var buffer = '';
|
||||
buffer += ('P3\n');
|
||||
buffer += (width+'\n');
|
||||
buffer += (height+'\n');
|
||||
buffer += (255+'\n');
|
||||
var buffer = [];
|
||||
buffer.push('P3\n');
|
||||
buffer.push(width+'\n');
|
||||
buffer.push(height+'\n');
|
||||
buffer.push(255+'\n');
|
||||
|
||||
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)
|
||||
buffer += '0 0 0\n';
|
||||
buffer.push('0 0 0\n');
|
||||
else {
|
||||
buffer += (colors[grey][0] + ' ' + colors[grey][1] + ' ' + colors[grey][2] + '\n');
|
||||
buffer.push(colors[grey][0] + ' ' + colors[grey][1] + ' ' + colors[grey][2] + '\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fs.writeFileSync(`img/${frame}.ppm`, buffer);
|
||||
let frameName = "" + frame;
|
||||
var pad = "00000";
|
||||
frameName = pad.substring(0, pad.length - frameName.length) + frameName;
|
||||
fs.writeFileSync(`img/${frameName}.ppm`, buffer.join(''));
|
||||
|
||||
|
||||
// Write image buffer to disk
|
||||
frame++;
|
||||
|
||||
console.log(frame);
|
||||
|
||||
if (!finished)
|
||||
setTimeout(loop, 0);
|
||||
loop();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue