Skip to content

Instantly share code, notes, and snippets.

Created July 12, 2014 12:14
Show Gist options
  • Select an option

  • Save anonymous/4d067d72cbd4481a08a3 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/4d067d72cbd4481a08a3 to your computer and use it in GitHub Desktop.
<html>
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r67/three.min.js"></script>
</head>
<body>
<script>
var map = [
[
"2222222222222",
"2222222222222",
"2222222222222",
"2222222222222",
"2222222222222",
"2222222222222"],
[
"1111111111111",
"1000000000001",
"1000000000001",
"1000$00000001",
"1000000000001",
"1111111111111"],
[
"3333333333333",
"3333333333333",
"3333333333333",
"3333333333333",
"3333333333333",
"3333333333333"]];
var scene, camera, renderer;
var wallTexture = THREE.ImageUtils.loadTexture( "http://tcrf.net/images/3/35/W3d_finalredbrick1.png" );
var floorTexture = THREE.ImageUtils.loadTexture( "http://tcrf.net/images/0/08/W3d_finalbluewall1.png" );
var ceilingTexture = THREE.ImageUtils.loadTexture( "http://tcrf.net/images/d/d5/Wolf3D_Door1final.png" );
var wallMaterial = new THREE.MeshBasicMaterial({ map: wallTexture });
var floorMaterial = new THREE.MeshBasicMaterial({ map: floorTexture });
var ceilingMaterial = new THREE.MeshBasicMaterial({ map: ceilingTexture });
window.onload = function() {
init();
initMap( map );
animate();
}
function init() {
var width = 800,
height = 600;
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 75, width / height, 1, 10000 );
renderer = new THREE.CanvasRenderer();
renderer.setSize( width, height );
document.body.appendChild( renderer.domElement );
}
function animate() {
requestAnimationFrame( animate );
renderer.render( scene, camera );
}
function initMap( map ) {
for ( var y in map ) {
for ( var x in map[y] ) {
for ( var z in map[y][x] ) {
if ( map[y][x][z] == '1' ) {
var box = new THREE.BoxGeometry( 200, 200, 200 );
var boxMesh = new THREE.Mesh( box, wallMaterial );
scene.add( boxMesh );
boxMesh.position = new THREE.Vector3( 200 * x, 200 * y, 200 * z );
}
if ( map[y][x][z] == '2' ) {
var box = new THREE.BoxGeometry( 200, 200, 200 );
var boxMesh = new THREE.Mesh( box, floorMaterial );
scene.add( boxMesh );
boxMesh.position = new THREE.Vector3( 200 * x, 200 * y, 200 * z );
}
if ( map[y][x][z] == '3' ) {
var box = new THREE.BoxGeometry( 200, 200, 200 );
var boxMesh = new THREE.Mesh( box, ceilingMaterial );
scene.add( boxMesh );
boxMesh.position = new THREE.Vector3( 200 * x, 200 * y, 200 * z );
}
else if ( map[y][x][z] == '$' ) {
camera.position.x = 200 * x;
camera.position.y = 200 * y;
camera.position.z = 200 * z;
}
}
}
}
};
document.addEventListener('keydown', function(event) {
var moveSpeed = 5,
rotateSpeed = 0.04;
switch( event.keyCode ) {
case "W".charCodeAt( 0 ):
camera.translateZ( -moveSpeed );
break;
case "S".charCodeAt( 0 ):
camera.translateZ( +moveSpeed );
break;
case "A".charCodeAt( 0 ):
camera.rotateY( rotateSpeed );
break;
case "D".charCodeAt( 0 ):
camera.rotateY( -rotateSpeed );
break;
}
}, false);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment