<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - materials - cube reflection [Escher]</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				background:#fff;
				color:#fff;
				padding:0;
				margin:0;
				overflow:hidden;
				font-family:georgia;
				text-align:center;
			}
			a {	color: #ff0080;	text-decoration: none; }
			a:hover { color: #0080ff; }

			#d { text-align:center; margin:1em auto -9.0em; z-index:1000; position:relative; display:block;
				 background:rgba(0,0,0,0.75); padding:0.25em; width:300px; border-radius:10px; -webkit-box-shadow: 0px 0px 10px rgba(0,0,0,0.5) }
			#oldie { margin-top:15em !important }
		</style>
	</head>

	<body>
		<div id="d">
			<p><a href="http://threejs.org">Three.js</a> cube mapping demo
			<p>Original artwork by <a href="http://en.wikipedia.org/wiki/Hand_with_Reflecting_Sphere" target="_blank">M. C. Escher</a>
			<p>Texture by <a href="http://brainwagon.org/2002/12/05/fun-with-environment-maps/" target="_blank">Mark VandeWettering</a>
		</div>

		<script src="../build/three.min.js"></script>
		<script src="js/loaders/DDSLoader.js"></script>

		<script src="js/Detector.js"></script>
		<script src="js/libs/stats.min.js"></script>

		<script>

			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			var statsEnabled = false;

			var container, stats;

			var camera, scene, renderer;

			var mesh, zmesh, lightMesh, geometry;

			var directionalLight, pointLight;

			var mouseX = 0;
			var mouseY = 0;

			var windowHalfX = window.innerWidth / 2;
			var windowHalfY = window.innerHeight / 2;

			document.addEventListener( 'mousemove', onDocumentMouseMove, false );

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 100000 );
				camera.position.z = 3200;

				scene = new THREE.Scene();

				/*
				var r = "textures/cube/Escher/";

				var urls = [ r + "px.jpg", r + "nx.jpg",
							 r + "py.jpg", r + "ny.jpg",
							 r + "pz.jpg", r + "nz.jpg" ];

				var textureCube = THREE.ImageUtils.loadTextureCube( urls );
				*/

				var r = "textures/cube/Escher/dds/";

				var urls = [ r + "px.dds", r + "nx.dds",
							 r + "py.dds", r + "ny.dds",
							 r + "pz.dds", r + "nz.dds" ];

				var loader = new THREE.DDSLoader();
				
				var textureCube = loader.load( urls );
				var material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube } )
				var geometry = new THREE.SphereGeometry( 100, 96, 64 );

				var mesh = new THREE.Mesh( geometry, material );
				mesh.scale.x = mesh.scale.y = mesh.scale.z = 16;
				scene.add( mesh );

				// Skybox

				var shader = THREE.ShaderLib[ "cube" ];
				shader.uniforms[ "tCube" ].value = textureCube;

				var material = new THREE.ShaderMaterial( {

					fragmentShader: shader.fragmentShader,
					vertexShader: shader.vertexShader,
					uniforms: shader.uniforms,
					side: THREE.BackSide

				} ),

				mesh = new THREE.Mesh( new THREE.BoxGeometry( 6000, 6000, 6000 ), material );
				scene.add( mesh );

				//

				renderer = new THREE.WebGLRenderer();
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				container.appendChild( renderer.domElement );

				if ( statsEnabled ) {

					stats = new Stats();
					stats.domElement.style.position = 'absolute';
					stats.domElement.style.top = '0px';
					stats.domElement.style.zIndex = 100;
					container.appendChild( stats.domElement );

				}

				//

				window.addEventListener( 'resize', onWindowResize, false );

			}

			function onWindowResize() {

				windowHalfX = window.innerWidth / 2;
				windowHalfY = window.innerHeight / 2;

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function onDocumentMouseMove(event) {

				mouseX = ( event.clientX - windowHalfX );
				mouseY = ( event.clientY - windowHalfY );

			}

			//

			function animate() {

				requestAnimationFrame( animate );

				render();
				if ( statsEnabled ) stats.update();

			}

			function render() {

				camera.position.x += ( mouseX - camera.position.x ) * .05;
				camera.position.y += ( - mouseY - camera.position.y ) * .05;

				camera.lookAt( scene.position );

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>