webgl_materials_variations_phong.html 6.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - materials</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 {
				color: #fff;
				font-family:Monospace;
				font-size:13px;
				text-align:center;

				background-color: #fff;
				margin: 0px;
				overflow: hidden;
			}

			#info {
				position: absolute;
				top: 0px; width: 100%;
				padding: 5px;
			}
		</style>
	</head>
	<body>

		<div id="container"></div>
		<div id="info"><a href="http://threejs.org" target="_blank">three.js</a> - Phong Material Variantions by <a href="http://clara.io/" target="_blank">Ben Houston</a>.</div>


		<script src="../build/three.min.js"></script>
		<script src="js/controls/OrbitControls.js"></script>
		<script src="js/geometries/TextGeometry.js"></script>
		<script src="js/utils/FontUtils.js"></script>
		<script src="fonts/gentilis_regular.typeface.js"></script>

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

		<script>

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

			var container, stats;

			var camera, scene, renderer, controls, objects = [];
			var particleLight;

			init();
			animate();

			function init() {

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

				camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
				camera.position.set( 0.0, 400, 400 * 3.5 );

				scene = new THREE.Scene();

				// Materials

				var imgTexture = THREE.ImageUtils.loadTexture( "textures/planets/moon_1024.jpg" );
				imgTexture.wrapS = imgTexture.wrapT = THREE.RepeatWrapping;
				imgTexture.anisotropy = 16;
				imgTexture = null;

				var shininess = 50, specular = 0x333333, bumpScale = 1, shading = THREE.SmoothShading;

				var materials = [];

				var path = "textures/cube/SwedishRoyalCastle/";
				var format = '.jpg';
				var urls = [
						path + 'px' + format, path + 'nx' + format,
						path + 'py' + format, path + 'ny' + format,
						path + 'pz' + format, path + 'nz' + format
					];

				var reflectionCube = THREE.ImageUtils.loadTextureCube( urls );
				reflectionCube.format = THREE.RGBFormat;
84
		
85 86 87 88 89 90 91 92
				var cubeWidth = 400;
				var numberOfSphersPerSide = 5;
				var sphereRadius = ( cubeWidth / numberOfSphersPerSide ) * 0.8 * 0.5;
				var stepSize = 1.0 / numberOfSphersPerSide;

				var geometry = new THREE.SphereBufferGeometry( sphereRadius, 32, 16 );

	
93 94 95 96 97 98 99 100 101 102
				var localReflectionCube;
				
				for( var alpha = 0, alphaIndex = 0; alpha <= 1.0; alpha += stepSize, alphaIndex ++ ) {

					if( alphaIndex % 2 === 0 ) {
						localReflectionCube = null;
					}
					else {
						localReflectionCube = reflectionCube;
					}
103 104 105 106 107 108

					var specularShininess = Math.pow( 2, alpha * 10 );

					for( var beta = 0; beta <= 1.0; beta += stepSize ) {

						var specularColor = new THREE.Color( beta * 0.2, beta * 0.2, beta * 0.2 );
109
						var reflectivity = beta;
110 111 112 113 114 115

						for( var gamma = 0; gamma <= 1.0; gamma += stepSize ) {

							// basic monochromatic energy preservation
							var diffuseColor = new THREE.Color( 0, 0, gamma ).multiplyScalar( 1 - beta * 0.2 );

116
							var material = new THREE.MeshPhongMaterial( { map: imgTexture, bumpMap: imgTexture, bumpScale: bumpScale, color: diffuseColor, specular: specularColor, reflectivity: reflectivity, shininess: specularShininess, shading: THREE.SmoothShading, envMap: localReflectionCube  } )
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

							var mesh = new THREE.Mesh( geometry, material );

							mesh.position.x = alpha * 400 - 200;
							mesh.position.y = beta * 400 - 200;
							mesh.position.z = gamma * 400 - 200;

							objects.push( mesh );

							scene.add( mesh );
						}
					}
				}
	
	
				function addLabel( name, location ) {
					var textGeo = new THREE.TextGeometry( name, {

						size: 20,
						height: 5,
						curveSegments: 10,

						font: 'gentilis',
						weight: 'normal',
						style: 'normal',

						material: 0,
						extrudeMaterial: 1

					});

					var textMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff } );
					var textMesh = new THREE.Mesh( textGeo, textMaterial );
					textMesh.position.copy( location );
					scene.add( textMesh );
				}

				addLabel( "-shininess", new THREE.Vector3( -350, 0, 0 ) );
				addLabel( "+shininess", new THREE.Vector3( 350, 0, 0 ) );

157 158
				addLabel( "-specular, -reflectivity", new THREE.Vector3( 0, -300, 0 ) );
				addLabel( "+specular, +reflectivity", new THREE.Vector3( 0, 300, 0 ) );
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

				addLabel( "-diffuse", new THREE.Vector3( 0, 0, -300 ) );
				addLabel( "+diffuse", new THREE.Vector3( 0, 0, 300 ) );

				particleLight = new THREE.Mesh( new THREE.SphereBufferGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
				scene.add( particleLight );

				// Lights

				scene.add( new THREE.AmbientLight( 0x222222 ) );

				var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
				directionalLight.position.set( 1, 1, 1 ).normalize();
				scene.add( directionalLight );

				var pointLight = new THREE.PointLight( 0xffffff, 2, 800 );
				particleLight.add( pointLight );

				//

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setClearColor( 0x0a0a0a );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.sortObjects = true;

				container.appendChild( renderer.domElement );

				renderer.gammaInput = true;
				renderer.gammaOutput = true;

				//

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

				container.appendChild( stats.domElement );

				controls = new THREE.OrbitControls( camera );
				controls.target.set( 0, 0, 0 );
				controls.update();

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

			}

			function onWindowResize() {

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

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

			}

			//

			function animate() {

				requestAnimationFrame( animate );

				render();
				stats.update();

			}

			function render() {

				var timer = Date.now() * 0.00025;

				//camera.position.x = Math.cos( timer ) * 800;
				//camera.position.z = Math.sin( timer ) * 800;

				camera.lookAt( scene.position );

				for ( var i = 0, l = objects.length; i < l; i ++ ) {

					var object = objects[ i ];

					object.rotation.y += 0.005;

				}

				particleLight.position.x = Math.sin( timer * 7 ) * 300;
				particleLight.position.y = Math.cos( timer * 5 ) * 400;
				particleLight.position.z = Math.cos( timer * 3 ) * 300;

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>