Browse Source

Added second cube mapping example.

Also refactored a bit texture cube code, to make it easier to reuse.
alteredq 14 years ago
parent
commit
1c6be1c70b

+ 13 - 7
examples/materials_cubemap.html

@@ -140,13 +140,7 @@
 				var cubeMaterial2 = new THREE.MeshLambertMaterial( { color: 0xffee00, env_map: new THREE.TextureCube( images ) } );
 				var cubeMaterial1 = new THREE.MeshLambertMaterial( { color: 0xffffff, env_map: new THREE.TextureCube( images ) } )
 				
-				var plane = new Plane( 100, 100 );
-				
-				addMesh( plane, 40,     0, 0,  -2000,  0,    0,    0,    new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[5] ) } )  );
-				addMesh( plane, 40, -2000, 0,      0,  0,    1.57, 0,    new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[0] ) } )  );
-				addMesh( plane, 40,  2000, 0,      0,  0,   -1.57, 0,    new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[1] ) } )  );
-				addMesh( plane, 40,     0, 2000,   0,  1.57, 0,    3.14, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[2] ) } )  );
-				addMesh( plane, 40,     0, -2000,  0, -1.57, 0,    3.14, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[3] ) } )  );
+				createCube( 4000, images );
 				
 				webglRenderer = new THREE.WebGLRenderer( scene );
 				webglRenderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
@@ -163,6 +157,18 @@
 
 			}
 
+			function createCube( size, images ) {
+			
+				var hsize = size/2, plane = new Plane( size, size ), pi2 = Math.PI/2, pi = Math.PI;
+				
+				addMesh( plane, 1,      0,     0,  -hsize,  0,      0,  0, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[5] ) } ) );
+				addMesh( plane, 1, -hsize,     0,       0,  0,    pi2,  0, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[0] ) } ) );
+				addMesh( plane, 1,  hsize,     0,       0,  0,   -pi2,  0, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[1] ) } ) );
+				addMesh( plane, 1,     0,  hsize,       0,  pi2,    0, pi, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[2] ) } ) );
+				addMesh( plane, 1,     0, -hsize,       0, -pi2,    0, pi, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[3] ) } ) );
+				
+			}
+
 			function createScene( geometry, m1, m2, m3 ) {
 				
 				var s = 15;

+ 235 - 0
examples/materials_cubemap_escher.html

@@ -0,0 +1,235 @@
+<!DOCTYPE HTML>
+<html lang="en">
+	<head>
+		<title>three.js - Escher cube map WebGL</title>
+		<meta charset="utf-8">
+		<style type="text/css">
+			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; }
+			
+			#log { position:absolute; top:50px; text-align:left; display:block; z-index:100 }
+			#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) }
+		</style>
+	</head>
+	
+	<body>
+		<div id="d">
+			<p><a href="http://github.com/mrdoob/three.js">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>
+		
+		<pre id="log"></pre>
+
+		<script type="text/javascript" src="../build/ThreeEscher.js"></script> 
+		
+		<script type="text/javascript" src="../src/extras/primitives/Sphere.js"></script>
+		<script type="text/javascript" src="../src/extras/primitives/Plane.js"></script>
+
+		<script type="text/javascript" src="js/Stats.js"></script>
+
+		<script type="text/javascript">
+
+			var SCREEN_WIDTH = window.innerWidth;
+			var SCREEN_HEIGHT = window.innerHeight;
+
+			var statsEnabled = false;
+			
+			var container;
+			var stats;
+
+			var camera;
+			var scene;
+			var webglRenderer;
+
+			var mesh, zmesh, lightMesh, geometry;
+			
+			var directionalLight, pointLight;
+			
+			var mouseX = 0;
+			var mouseY = 0;
+
+			var windowHalfX = window.innerWidth >> 1;
+			var windowHalfY = window.innerHeight >> 1;
+			
+			document.addEventListener('mousemove', onDocumentMouseMove, false);
+
+			init();
+			
+			loop();
+			
+			setInterval(loop, 1000/60);
+			
+			function addMesh( geometry, scale, x, y, z, rx, ry, rz, material ) {
+				
+				mesh = new THREE.Mesh( geometry, material );
+				mesh.scale.x = mesh.scale.y = mesh.scale.z = scale;
+				mesh.position.x = x;
+				mesh.position.y = y;
+				mesh.position.z = z;
+				mesh.rotation.x = rx;
+				mesh.rotation.y = ry;
+				mesh.rotation.z = rz;
+				mesh.overdraw = true;
+				mesh.updateMatrix();
+				scene.addObject(mesh);
+				
+			}
+			
+			function init() {
+
+				container = document.createElement('div');
+				document.body.appendChild(container);
+
+				camera = new THREE.Camera( 90, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 100000 );
+				camera.position.z = 3200;
+				camera.updateMatrix();
+
+				scene = new THREE.Scene();
+
+				// LIGHTS
+
+				var ambient = new THREE.AmbientLight( 0x101010 );
+				scene.addLight( ambient );
+
+				directionalLight = new THREE.DirectionalLight( 0xffffff );
+				directionalLight.position.x = 1;
+				directionalLight.position.y = 1;
+				directionalLight.position.z = 2;
+				directionalLight.position.normalize();
+				scene.addLight( directionalLight );
+
+				pointLight = new THREE.PointLight( 0xffffff );
+				pointLight.position.x = 0;
+				pointLight.position.y = 0;
+				pointLight.position.z = 0;
+				scene.addLight( pointLight );
+
+				// light representation
+				sphere = new Sphere( 100, 16, 8, 1 );
+				lightMesh = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color:0xffaa00 } ) );
+				lightMesh.scale.x = lightMesh.scale.y = lightMesh.scale.z = 0.05;
+				lightMesh.position = pointLight.position;
+				lightMesh.overdraw = true;
+				lightMesh.updateMatrix();
+				scene.addObject(lightMesh);
+
+				// material samples
+				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 images = loadImageArray( urls );
+				
+				var cubeMaterial = new THREE.MeshLambertMaterial( { color: 0xffffff, env_map: new THREE.TextureCube( images ) } )
+				var sphere = new Sphere( 100, 64, 32, true );
+				
+				createCube( 6000, images );
+				createScene( sphere, cubeMaterial, 16 );
+				
+				webglRenderer = new THREE.WebGLRenderer( scene );
+				webglRenderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
+				container.appendChild( webglRenderer.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 );
+					
+				}
+			
+				
+			}
+
+			function createCube( size, images ) {
+			
+				var hsize = size/2, plane = new Plane( size, size ), pi2 = Math.PI/2, pi = Math.PI;
+				
+				addMesh( plane, 1,      0,     0,  -hsize,  0,      0,  0, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[5] ) } ) );
+				addMesh( plane, 1, -hsize,     0,       0,  0,    pi2,  0, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[0] ) } ) );
+				addMesh( plane, 1,  hsize,     0,       0,  0,   -pi2,  0, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[1] ) } ) );
+				addMesh( plane, 1,     0,  hsize,       0,  pi2,    0, pi, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[2] ) } ) );
+				addMesh( plane, 1,     0, -hsize,       0, -pi2,    0, pi, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[3] ) } ) );
+				
+			}
+			
+			function createScene( geometry, m, scale ) {
+				
+				var mesh = new THREE.Mesh( geometry, m );
+				mesh.scale.x = mesh.scale.y = mesh.scale.z = scale;
+				mesh.updateMatrix();
+				scene.addObject(mesh);
+
+			}
+
+			function loadImageArray( urls ) {
+				
+				var i, images = [];
+				
+				images.loadCount = 0;
+				
+				for ( i = 0; i < urls.length; ++i ) {
+					
+					images[i] = new Image();
+					images[i].loaded = 0;
+					images[i].onload = function() { images.loadCount += 1; this.loaded = 1;/*log( images.loadCount );*/ }
+					images[i].src = urls[i];
+					
+				}
+
+				return images;
+				
+			}
+			
+			function onDocumentMouseMove(event) {
+
+				mouseX = ( event.clientX - windowHalfX );
+				mouseY = ( event.clientY - windowHalfY );
+
+			}
+
+			var r = 0;
+			
+			function loop() {
+
+				camera.position.x += ( mouseX - camera.position.x ) * .05;
+				camera.position.y += ( - mouseY - camera.position.y ) * .05;
+				camera.updateMatrix();
+							
+				lightMesh.position.x = 1500*Math.cos(r);
+				lightMesh.position.z = 1500*Math.sin(r);
+				lightMesh.updateMatrix();
+
+				r += 0.01;
+				
+				webglRenderer.render( scene, camera );
+
+				if ( statsEnabled ) stats.update();
+
+			}
+
+			function log(text) {
+			
+				var e = document.getElementById("log");
+				e.innerHTML = text + "<br/>" + e.innerHTML;
+				
+			}
+		</script>
+
+	</body>
+</html>

BIN
examples/textures/cube/Escher/nx.jpg


BIN
examples/textures/cube/Escher/ny.jpg


BIN
examples/textures/cube/Escher/nz.jpg


BIN
examples/textures/cube/Escher/px.jpg


BIN
examples/textures/cube/Escher/py.jpg


BIN
examples/textures/cube/Escher/pz.jpg