Skip to content

Instantly share code, notes, and snippets.

@mathdoodle
Last active August 29, 2015 14:25
Show Gist options
  • Save mathdoodle/8c8869a8d69d7e5450e4 to your computer and use it in GitHub Desktop.
Save mathdoodle/8c8869a8d69d7e5450e4 to your computer and use it in GitHub Desktop.
VIEWPORT
{
"uuid": "a4431166-2d08-4b6a-a2d0-4ff682a3592f",
"description": "VIEWPORT",
"dependencies": {
"DomReady": "latest",
"davinci-eight": "latest",
"davinci-threejs": "0.71.4"
},
"operatorOverloading": true
}
<!doctype html>
<html>
<head>
<style>
/* STYLE-MARKER */
</style>
<!-- SCRIPTS-MARKER -->
<script>
// CODE-MARKER
</script>
</head>
<body>
<canvas id='canvas' width='600' height='600'>
Your browser does not support the HTML5 canvas element.
</canvas>
</body>
</html>
DomReady.ready(function() {
try {
main();
}
catch(e) {
alert(e);
}
});
function main() {
var world = EIGHT.world();
var viewL = EIGHT.perspective();
viewL.aspect = (window.innerWidth / 2) / window.innerHeight;
viewL.eye.x = -1;
viewL.eye.y = 1;
viewL.eye.z = 3;
var viewR = EIGHT.perspective();
viewR.aspect = (window.innerWidth / 2) / window.innerHeight;
viewR.eye.x = 1;
viewR.eye.y = 1;
viewR.eye.z = 3;
var box = EIGHT.box(viewL)
world.add(box);
var canvas = <HTMLCanvasElement>document.getElementById('canvas');
var viewportL = EIGHT.viewport({canvas: canvas});
viewportL.clearColor(1,0,0,1);
viewportL.width = canvas.width / 2;
var viewportR = EIGHT.viewport({canvas: canvas});
viewportR.clearColor(0,0,1,1);
viewportR.x = canvas.width / 2;
viewportR.width = canvas.width / 2;
var runner = EIGHT.animationRunner(tick, terminate, setUp, tearDown, window);
var monitor = EIGHT.contextMonitor(canvas);
runner.start();
function tick(time: number): void {
var frequency = 0.2;
var omega = 2 * Math.PI * frequency;
var theta = omega * time;
box.model.position.y = 1.2 * Math.sin(theta);
box.model.attitude = {yz:0, zx:0, xy:Math.sin(theta/2),w: Math.cos(theta/2)};
viewportL.render(world, [viewL]);
viewportR.render(world, [viewR]);
}
function setUp(): void {
monitor.addContextUser(world);
monitor.addContextUser(viewportL)
monitor.addContextUser(viewportR)
monitor.start();
}
function tearDown(e: Error): void {
monitor.stop();
}
function terminate(time: number): boolean {
return false;
}
}
body {
background-color: white;
margin: 0;
}
canvas {
background-color: black;
position: absolute;
width: 100%;
height: 100%;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment