Skip to content

Instantly share code, notes, and snippets.

@bmidgley
Last active August 29, 2015 13:57
Show Gist options
  • Save bmidgley/9627611 to your computer and use it in GitHub Desktop.
Save bmidgley/9627611 to your computer and use it in GitHub Desktop.
3d ghosts! place inside gamingjs.com/ice for easy modification
<body></body>
<script src="http://gamingJS.com/Three.js"></script>
<script src="http://gamingJS.com/ChromeFixes.js"></script>
<script>
var requestFullScreen = function(element) {
var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;
if (requestMethod) {
requestMethod.call(element);
}
}
var Ghost = function() {
var shape, cover;
shape = new THREE.CylinderGeometry(50,60,100);
cover = new THREE.MeshPhongMaterial();
cover.emissive.setRGB(0.5, 0.5, 1);
this.body = new THREE.Mesh(shape, cover);
this.body.castShadow = true;
shape = new THREE.SphereGeometry(50,10,10);
cover = new THREE.MeshPhongMaterial();
cover.emissive.setRGB(0.5, 0.5, 1);
this.head = new THREE.Mesh(shape, cover);
this.body.add(this.head);
this.head.castShadow = true;
shape = new THREE.TorusGeometry(100,15,10,25,2.14);
cover = new THREE.MeshPhongMaterial();
cover.emissive.setRGB(0.5, 0.5, 1);
this.arms = new THREE.Mesh(shape, cover);
this.body.add(this.arms);
this.arms.castShadow = true;
}
var scene = new THREE.Scene();
var separation = 10; // positive for 3d tv viewing
var in3d = true;
//in3d = false; // comment this for side-by-side stereoscopic rendering
var cameras = new THREE.Object3D();
var lookingAtPoint = new THREE.Vector3(0,0,-1000);
scene.add(cameras);
cameras.position.set(0, 0, 700);
cameraLeft = new THREE.PerspectiveCamera(45, 1, 1, 4000);
cameras.add(cameraLeft);
cameraLeft.lookAt(lookingAtPoint);
cameraLeft.position.set(-separation, 0, 0);
cameraRight = new THREE.PerspectiveCamera(45, 1, 1, 4000);
cameras.add(cameraRight);
cameraRight.lookAt(lookingAtPoint);
cameraRight.position.set(separation, 0, 0);
var renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.shadowMapEnabled = true;
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
shape = new THREE.PlaneGeometry(600, 400);
cover = new THREE.MeshBasicMaterial();
cover.color.setRGB(0.5, 0.5, 0.5);
var wall = new THREE.Mesh(shape, cover);
scene.add(wall);
wall.rotation.set(0, 0.5, 0);
wall.receiveShadow = true;
var ghost = new Ghost();
scene.add(ghost.body);
ghost.body.castShadow = true;
var candle = new THREE.DirectionalLight();
candle.intensity = 0.5;
candle.position.set(300, 0, 500);
scene.add(candle);
candle.castShadow = true;
//requestFullScreen(document.body);
var ghost_orientation = 0, ghost_distance = 0;
document.addEventListener('keydown', function(event) {
var key = event.keyCode;
if(key == 37)
ghost_orientation += 0.1;
if(key == 38)
ghost_distance += 10;
if(key == 39)
ghost_orientation -= 0.1;
if(key == 40)
ghost_distance -= 10;
});
var clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
var t = clock.getElapsedTime();
ghost.head.position.set(0, 50 + 5*Math.sin(10*t), 0);
ghost.arms.position.set(0, - 50 - 5*Math.sin(10*t), 0);
ghost.arms.rotation.set(0, 0, 0.5);
ghost.body.position.set(100*Math.sin(t), 90*Math.cos(t),200*Math.sin(t) + ghost_distance);
ghost.body.rotation.set(0, 0, ghost_orientation);
var height = window.innerHeight;
var width;
if(in3d) {
width = Math.round(window.innerWidth/2);
cameraLeft.aspect = width * 2 / height;
} else {
width = window.innerWidth;
cameraLeft.aspect = width / height;
}
renderer.setViewport(0, 0, width, height);
renderer.setScissor(0, 0, width, height);
renderer.enableScissorTest (true);
cameraLeft.updateProjectionMatrix();
renderer.render(scene, cameraLeft);
if(in3d) {
renderer.setViewport(width, 0, width, height);
renderer.setScissor(width, 0, width, height);
renderer.enableScissorTest ( true );
cameraRight.aspect = width * 2 / height;
cameraRight.updateProjectionMatrix();
renderer.render( scene, cameraRight );
}
}
animate();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment