|
@@ -0,0 +1,244 @@
|
|
|
+<!DOCTYPE HTML>
|
|
|
+<html lang="en">
|
|
|
+ <head>
|
|
|
+ <title>three.js - materials - cube reflection [camaro] - webgl</title>
|
|
|
+ <meta charset="utf-8">
|
|
|
+ <style type="text/css">
|
|
|
+ body {
|
|
|
+ background:#000;
|
|
|
+ color:#fff;
|
|
|
+ padding:0;
|
|
|
+ margin:0;
|
|
|
+ overflow:hidden;
|
|
|
+ font-family:georgia;
|
|
|
+ text-align:center;
|
|
|
+ }
|
|
|
+ h1 { }
|
|
|
+ a { color:skyblue }
|
|
|
+ canvas { pointer-events:none; z-index:10; position:relative; }
|
|
|
+ #log { position:absolute; top:50px; text-align:left; display:block; z-index:100 }
|
|
|
+ #d { text-align:center; margin:1em 0 -2.5em 0; z-index:0; position:relative; display:block }
|
|
|
+ #buttons { margin:0.5em 0 0 0 }
|
|
|
+ button { font-family:georgia; border:0; background:#222; color:#fff; padding:0.2em 0.5em; cursor:pointer; border-radius:3px }
|
|
|
+ button:hover { background:#333 }
|
|
|
+ </style>
|
|
|
+ </head>
|
|
|
+
|
|
|
+ <body>
|
|
|
+ <div id="d">
|
|
|
+ <div id="info"><a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl cube reflection demo. chevrolet camaro by <a href="http://www.turbosquid.com/3d-models/blender-camaro/411348" target="_blank">dskfnwn</a></div>
|
|
|
+
|
|
|
+ <div id="buttons"></div>
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <div id="log"></div>
|
|
|
+
|
|
|
+ <script type="text/javascript" src="../build/Three.js"></script>
|
|
|
+
|
|
|
+ <script type="text/javascript" src="../src/extras/primitives/Sphere.js"></script>
|
|
|
+ <script type="text/javascript" src="../src/extras/ImageUtils.js"></script>
|
|
|
+ <script type="text/javascript" src="../src/extras/SceneUtils.js"></script>
|
|
|
+ <script type="text/javascript" src="../src/extras/io/Loader.js"></script>
|
|
|
+ <script type="text/javascript" src="../src/extras/Detector.js"></script>
|
|
|
+
|
|
|
+ <script type="text/javascript" src="js/Stats.js"></script>
|
|
|
+
|
|
|
+ <script type="text/javascript">
|
|
|
+
|
|
|
+ if ( ! THREE.Detector.webgl ) THREE.Detector.addGetWebGLMessage();
|
|
|
+
|
|
|
+ var SCREEN_WIDTH = window.innerWidth;
|
|
|
+ var SCREEN_HEIGHT = window.innerHeight;
|
|
|
+
|
|
|
+ var container;
|
|
|
+ var stats;
|
|
|
+
|
|
|
+ var camera;
|
|
|
+ var scene;
|
|
|
+ var webglRenderer;
|
|
|
+
|
|
|
+ var lightMesh;
|
|
|
+
|
|
|
+ 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 init() {
|
|
|
+
|
|
|
+ container = document.createElement('div');
|
|
|
+ document.body.appendChild(container);
|
|
|
+
|
|
|
+ camera = new THREE.Camera( 70, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 100000 );
|
|
|
+ camera.position.z = 1000;
|
|
|
+ camera.updateMatrix();
|
|
|
+
|
|
|
+ scene = new THREE.Scene();
|
|
|
+
|
|
|
+ // LIGHTS
|
|
|
+
|
|
|
+ var ambient = new THREE.AmbientLight( 0x555555 );
|
|
|
+ scene.addLight( ambient );
|
|
|
+
|
|
|
+ directionalLight = new THREE.DirectionalLight( 0xffffff );
|
|
|
+ directionalLight.position.x = 1;
|
|
|
+ directionalLight.position.y = 1;
|
|
|
+ directionalLight.position.z = 0.5;
|
|
|
+ directionalLight.position.normalize();
|
|
|
+ scene.addLight( directionalLight );
|
|
|
+
|
|
|
+ pointLight = new THREE.PointLight( 0xffaa00 );
|
|
|
+ pointLight.position.x = 0;
|
|
|
+ pointLight.position.y = 0;
|
|
|
+ pointLight.position.z = 0;
|
|
|
+ scene.addLight( pointLight );
|
|
|
+
|
|
|
+
|
|
|
+ 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);
|
|
|
+
|
|
|
+
|
|
|
+ webglRenderer = new THREE.WebGLRenderer();
|
|
|
+ webglRenderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
|
|
|
+ webglRenderer.setFaceCulling( 0 );
|
|
|
+ container.appendChild( webglRenderer.domElement );
|
|
|
+
|
|
|
+ stats = new Stats();
|
|
|
+ stats.domElement.style.position = 'absolute';
|
|
|
+ stats.domElement.style.top = '0px';
|
|
|
+ stats.domElement.style.zIndex = 100;
|
|
|
+ container.appendChild( stats.domElement );
|
|
|
+
|
|
|
+ var r = "textures/cube/SwedishRoyalCastle/";
|
|
|
+ var urls = [ r + "px.jpg", r + "nx.jpg",
|
|
|
+ r + "py.jpg", r + "ny.jpg",
|
|
|
+ r + "pz.jpg", r + "nz.jpg" ];
|
|
|
+
|
|
|
+ var images = ImageUtils.loadArray( urls );
|
|
|
+ var textureCube = new THREE.Texture( images );
|
|
|
+
|
|
|
+ var camaroMaterials = {
|
|
|
+
|
|
|
+ body: [],
|
|
|
+ chrome: new THREE.MeshLambertMaterial( { color: 0xffffff, env_map: textureCube } ),
|
|
|
+ darkchrome: new THREE.MeshLambertMaterial( { color: 0x444444, env_map: textureCube } ),
|
|
|
+ glass: new THREE.MeshBasicMaterial( { color: 0x223344, env_map: textureCube, opacity: 0.25, combine: THREE.MixOperation, reflectivity: 0.25 } ),
|
|
|
+ tire: new THREE.MeshLambertMaterial( { color: 0x050505 } ),
|
|
|
+ interior: new THREE.MeshPhongMaterial( { color: 0x050505, shininess: 20 } ),
|
|
|
+ black: new THREE.MeshLambertMaterial( { color: 0x000000 } )
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ camaroMaterials.body.push( [ "Orange", new THREE.MeshLambertMaterial( { color: 0xff6600, env_map: textureCube, combine: THREE.MixOperation, reflectivity: 0.3 } ) ] );
|
|
|
+ camaroMaterials.body.push( [ "Blue", new THREE.MeshLambertMaterial( { color: 0x226699, env_map: textureCube, combine: THREE.MixOperation, reflectivity: 0.3 } ) ] );
|
|
|
+ camaroMaterials.body.push( [ "Red", new THREE.MeshLambertMaterial( { color: 0x660000, env_map: textureCube, combine: THREE.MixOperation, reflectivity: 0.5 } ) ] );
|
|
|
+ camaroMaterials.body.push( [ "Black", new THREE.MeshLambertMaterial( { color: 0x000000, env_map: textureCube, combine: THREE.MixOperation, reflectivity: 0.5 } ) ] );
|
|
|
+ camaroMaterials.body.push( [ "White", new THREE.MeshLambertMaterial( { color: 0xffffff, env_map: textureCube, combine: THREE.MixOperation, reflectivity: 0.5 } ) ] );
|
|
|
+
|
|
|
+ camaroMaterials.body.push( [ "Carmine", new THREE.MeshPhongMaterial( { color: 0x770000, specular:0xffaaaa, env_map: textureCube, combine: THREE.MultiplyOperation } ) ] );
|
|
|
+ camaroMaterials.body.push( [ "Gold", new THREE.MeshPhongMaterial( { color: 0xaa9944, specular:0xbbaa99, shininess:50, env_map: textureCube, combine: THREE.MultiplyOperation } ) ] );
|
|
|
+ camaroMaterials.body.push( [ "Bronze", new THREE.MeshPhongMaterial( { color: 0x150505, specular:0xee6600, shininess:10, env_map: textureCube, combine: THREE.MixOperation, reflectivity: 0.5 } ) ] );
|
|
|
+ camaroMaterials.body.push( [ "Chrome", new THREE.MeshPhongMaterial( { color: 0xffffff, specular:0xffffff, env_map: textureCube, combine: THREE.MultiplyOperation } ) ] );
|
|
|
+
|
|
|
+ var loader = new THREE.Loader();
|
|
|
+ loader.loadBinary( { model: "obj/camaro/CamaroNoUv_bin.js", callback: function( geometry ) { createScene( geometry, camaroMaterials ) } } );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function $( id ) { return document.getElementById( id ) }
|
|
|
+
|
|
|
+
|
|
|
+ function createButtons( materials, geometry ) {
|
|
|
+
|
|
|
+ var i, src = "", parent = $( "buttons" );
|
|
|
+
|
|
|
+ for( i = 0; i < materials.length; i++ ) {
|
|
|
+
|
|
|
+ src += '<button id="m' + i + '">' + materials[ i ][ 0 ] + '</button>';
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ parent.innerHTML = src;
|
|
|
+
|
|
|
+ for( i = 0; i < materials.length; i++ ) {
|
|
|
+
|
|
|
+ $( "m" + i ).counter = i;
|
|
|
+ $( "m" + i ).addEventListener( 'click', function() { geometry.materials[ 0 ][ 0 ] = materials[ this.counter ][ 1 ] }, false );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function createScene( geometry, materials ) {
|
|
|
+
|
|
|
+ var s = 75, m = new THREE.MeshFaceMaterial();
|
|
|
+
|
|
|
+ geometry.materials[ 0 ][ 0 ] = materials.body[ 0 ][ 1 ]; // car body
|
|
|
+ geometry.materials[ 1 ][ 0 ] = materials.chrome; // wheels chrome
|
|
|
+ geometry.materials[ 2 ][ 0 ] = materials.chrome; // grille chrome
|
|
|
+ geometry.materials[ 3 ][ 0 ] = materials.darkchrome; // door lines
|
|
|
+ geometry.materials[ 4 ][ 0 ] = materials.glass; // windshield
|
|
|
+ geometry.materials[ 5 ][ 0 ] = materials.interior; // interior
|
|
|
+ geometry.materials[ 6 ][ 0 ] = materials.tire; // tire
|
|
|
+ geometry.materials[ 7 ][ 0 ] = materials.black; // tireling
|
|
|
+ geometry.materials[ 8 ][ 0 ] = materials.black; // behind grille
|
|
|
+
|
|
|
+ SceneUtils.addMesh( scene, geometry, s, 0, 0, 0, 0.0, 1.0, 0.0, m );
|
|
|
+
|
|
|
+ createButtons( materials.body, geometry );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ 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;
|
|
|
+
|
|
|
+ lightMesh.position.x = 1500 * Math.cos( r );
|
|
|
+ lightMesh.position.z = 1500 * Math.sin( r );
|
|
|
+
|
|
|
+ r += 0.01;
|
|
|
+
|
|
|
+ webglRenderer.render( scene, camera );
|
|
|
+
|
|
|
+ stats.update();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function log(text) {
|
|
|
+
|
|
|
+ var e = document.getElementById("log");
|
|
|
+ e.innerHTML = text + "<br/>" + e.innerHTML;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ </script>
|
|
|
+
|
|
|
+ </body>
|
|
|
+</html>
|