Server-side threejs rendering

This commit is contained in:
Thomas FORGIONE 2015-11-18 16:13:01 +01:00
parent f4e06c9c28
commit c0d8d6292c
3 changed files with 2128 additions and 17 deletions

View File

@ -0,0 +1,44 @@
"use strict";
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');
let width = Math.floor(1134);
let height = Math.floor(768);
let renderer = new THREE.CanvasRenderer();
renderer.domElement.style = renderer.domElement;
renderer.setSize(width,height);
renderer.setClearColor(0x000000);
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;
for (let i = 0; i < 1; i += 0.005) {
mesh.rotation.x = i * 2 * Math.PI;
mesh.rotation.y = i * Math.PI;
camera.lookAt(new THREE.Vector3());
renderer.render(scene, camera);
console.log(i);
fs.writeFileSync(__dirname + '/' + pad(counter++, 4) + '.png', renderer.domElement.toBuffer());
}

File diff suppressed because it is too large Load Diff

View File

@ -457,18 +457,7 @@ geo.MeshStreamer.prototype.start = function(socket) {
// console.log('Time to generate chunk : ' + (Date.now() - oldTime) + 'ms'); // console.log('Time to generate chunk : ' + (Date.now() - oldTime) + 'ms');
if (next.data.length === 0) { if (self.prefetch && next.size < self.chunk) {
// If nothing, just serve stuff
var tmp = self.nextElements([
// {
// proportion: 1,
// frustum: cameraFrustum
// }
]);
next.data = tmp.data;
} else if (!self.prefetch && next.size < this.chunk) {
// Recompute config // Recompute config
var newConfig = []; var newConfig = [];
@ -477,7 +466,7 @@ geo.MeshStreamer.prototype.start = function(socket) {
for (var i = 0; i < config.length; i++) { for (var i = 0; i < config.length; i++) {
// Check if config was full // Check if config was full
if (next.configSizes[i] < this.chunk * config[i].proportion) { if (next.configSizes[i] >= self.chunk * config[i].proportion) {
newConfig.push(config[i]); newConfig.push(config[i]);
sum += config[i].proportion; sum += config[i].proportion;
@ -494,12 +483,32 @@ geo.MeshStreamer.prototype.start = function(socket) {
// Normalize config probabilities // Normalize config probabilities
var newData = self.nextElements(newConfig, this.chunk - next.size); var newData = self.nextElements(newConfig, self.chunk - next.size);
next.data = next.data.push.apply(next.data, newData.data); next.data.push.apply(next.data, newData.data);
// console.log('Adding ' + newData.size + ' for newConfig : ' + JSON.stringify(newConfig.map(function(o) { return o.proportion})));
next.size = next.size + newData.size;
} }
if (next.data.length === 0) {
// If nothing, just serve stuff
var tmp = self.nextElements([
// {
// proportion: 1,
// frustum: cameraFrustum
// }
]);
next.data = tmp.data;
next.size = tmp.size;
}
// console.log('Chunk of size ' + next.size);
socket.emit('elements', next.data); socket.emit('elements', next.data);
if (next.finished) { if (next.finished) {
@ -614,6 +623,7 @@ geo.MeshStreamer.prototype.nextElements = function(config, chunk) {
data.push(vertex1.toList()); data.push(vertex1.toList());
this.vertices[currentFace.a] = true; this.vertices[currentFace.a] = true;
configSizes[configIndex]++; configSizes[configIndex]++;
totalSize++;
} }
@ -622,6 +632,7 @@ geo.MeshStreamer.prototype.nextElements = function(config, chunk) {
data.push(vertex2.toList()); data.push(vertex2.toList());
this.vertices[currentFace.b] = true; this.vertices[currentFace.b] = true;
configSizes[configIndex]++; configSizes[configIndex]++;
totalSize++;
} }
@ -630,6 +641,7 @@ geo.MeshStreamer.prototype.nextElements = function(config, chunk) {
data.push(vertex3.toList()); data.push(vertex3.toList());
this.vertices[currentFace.c] = true; this.vertices[currentFace.c] = true;
configSizes[configIndex]++; configSizes[configIndex]++;
totalSize++;
} }
@ -714,6 +726,7 @@ geo.MeshStreamer.prototype.nextElements = function(config, chunk) {
continue faceloop; continue faceloop;
} }
} }
if (totalSize > chunk) { if (totalSize > chunk) {
@ -727,7 +740,7 @@ geo.MeshStreamer.prototype.nextElements = function(config, chunk) {
} }
return {data: data, finished: mightBeCompletetlyFinished, configSizes: configSizes}; return {data: data, finished: mightBeCompletetlyFinished, configSizes: configSizes, size:totalSize};
}; };