2015-05-21 14:42:18 +02:00
|
|
|
Math.clamp = Math.clamp || function(number, min, max) {
|
|
|
|
return Math.max(Math.min(number, max), min);
|
|
|
|
}
|
|
|
|
|
2015-06-02 11:01:28 +02:00
|
|
|
var Previewer = function(renderer, scene) {
|
2015-05-06 14:54:02 +02:00
|
|
|
this.domElement = document.createElement('canvas');
|
|
|
|
this.ctx = this.domElement.getContext('2d');
|
|
|
|
this.renderer = renderer;
|
2015-05-18 09:28:51 +02:00
|
|
|
this.fixed = false;
|
2015-06-02 11:01:28 +02:00
|
|
|
this.scene = scene;
|
2015-06-04 15:08:23 +02:00
|
|
|
this.drawn = false;
|
|
|
|
this.drawnBefore = false;
|
2015-05-06 14:54:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Previewer.prototype.render = function(prev, container_width, container_height) {
|
2015-05-18 09:28:51 +02:00
|
|
|
var width, height, left, bottom;
|
2015-05-06 14:54:02 +02:00
|
|
|
|
|
|
|
if (prev.go) {
|
|
|
|
width = Math.floor(container_width / 5);
|
|
|
|
height = Math.floor(container_height / 5);
|
2015-05-18 09:28:51 +02:00
|
|
|
if (!this.fixed) {
|
|
|
|
left = Math.floor(prev.x - width/2);
|
|
|
|
bottom = Math.floor(this.renderer.domElement.height - prev.y + height/5);
|
2015-05-21 14:42:18 +02:00
|
|
|
|
|
|
|
// Translate box if too high
|
2015-05-21 14:31:18 +02:00
|
|
|
if (bottom + height > this.renderer.domElement.height) {
|
|
|
|
bottom -= 7 * height / 5;
|
|
|
|
}
|
2015-05-21 14:42:18 +02:00
|
|
|
|
|
|
|
// Translate box if too on the side
|
|
|
|
left = Math.clamp(left, width / 5, this.renderer.domElement.width - 6 * width / 5);
|
|
|
|
|
2015-05-18 09:28:51 +02:00
|
|
|
} else {
|
|
|
|
left = 0;
|
|
|
|
bottom = 0;
|
|
|
|
}
|
2015-05-06 14:54:02 +02:00
|
|
|
|
|
|
|
// Draw border
|
|
|
|
var can_bottom = container_height - bottom - height ;
|
|
|
|
this.ctx.strokeStyle = "#ffffff";
|
|
|
|
this.ctx.beginPath();
|
|
|
|
this.ctx.moveTo(left-1, can_bottom);
|
|
|
|
this.ctx.lineTo(left-1, can_bottom + height);
|
|
|
|
this.ctx.lineTo(left + width-1, can_bottom + height);
|
|
|
|
this.ctx.lineTo(left + width-1, can_bottom);
|
|
|
|
this.ctx.closePath();
|
|
|
|
this.ctx.stroke();
|
|
|
|
|
|
|
|
this.ctx.strokeStyle = "#000000";
|
|
|
|
this.ctx.beginPath();
|
|
|
|
this.ctx.moveTo(left, can_bottom + 1);
|
|
|
|
this.ctx.lineTo(left, can_bottom + height - 1);
|
|
|
|
this.ctx.lineTo(left + width - 2 , can_bottom + height-1);
|
|
|
|
this.ctx.lineTo(left + width - 2, can_bottom+1);
|
|
|
|
this.ctx.closePath();
|
|
|
|
this.ctx.stroke();
|
|
|
|
|
|
|
|
// Do render in previsualization
|
|
|
|
prev.camera.look();
|
2015-05-11 08:45:13 +02:00
|
|
|
this.renderer.setScissor(left, bottom, width, height);
|
2015-05-18 09:28:51 +02:00
|
|
|
this.renderer.enableScissorTest(true);
|
2015-05-11 08:45:13 +02:00
|
|
|
this.renderer.setViewport(left, bottom, width, height);
|
2015-06-02 11:01:28 +02:00
|
|
|
this.renderer.render(this.scene, prev.camera);
|
2015-05-06 14:54:02 +02:00
|
|
|
|
2015-06-04 15:08:23 +02:00
|
|
|
this.update(true);
|
2015-06-10 10:46:00 +02:00
|
|
|
|
|
|
|
if (this.prevCamera !== prev.camera) {
|
|
|
|
this.clearNeeded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.prevCamera = prev.camera;
|
2015-06-04 15:08:23 +02:00
|
|
|
} else {
|
|
|
|
this.update(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.drawnBefore && !this.drawn) {
|
2015-05-13 09:05:03 +02:00
|
|
|
this.clearNeeded = true;
|
|
|
|
}
|
2015-06-10 10:46:00 +02:00
|
|
|
|
2015-05-06 14:54:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Previewer.prototype.clear = function() {
|
2015-05-13 09:05:03 +02:00
|
|
|
if (this.clearNeeded) {
|
|
|
|
this.domElement.width = this.domElement.width;
|
|
|
|
this.clearNeeded = false;
|
|
|
|
}
|
2015-05-06 14:54:02 +02:00
|
|
|
}
|
2015-05-18 09:28:51 +02:00
|
|
|
|
|
|
|
Previewer.prototype.fixedRecommendation = function(bool) {
|
|
|
|
this.fixed = bool;
|
|
|
|
}
|
2015-06-04 15:08:23 +02:00
|
|
|
|
|
|
|
Previewer.prototype.update = function(arg) {
|
|
|
|
this.drawnBefore = this.drawn;
|
|
|
|
this.drawn = arg;
|
|
|
|
}
|