webgl_materials_cars_camaro_crosseyed.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - cube reflection [camaro]</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. background:#000;
  9. color:#fff;
  10. padding:0;
  11. margin:0;
  12. overflow:hidden;
  13. font-family:georgia;
  14. text-align:center;
  15. }
  16. h1 { }
  17. a { color:skyblue }
  18. canvas { pointer-events:none; z-index:10; position:relative; }
  19. #log { position:absolute; top:50px; text-align:left; display:block; z-index:100 }
  20. #d { text-align:center; margin:1em 0 -2.5em 0; z-index:0; position:relative; display:block }
  21. #buttons { margin:0.5em 0 0 0 }
  22. button { font-family:georgia; border:0; background:#222; color:#fff; padding:0.2em 0.5em; cursor:pointer; border-radius:3px }
  23. button:hover { background:#333 }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="d">
  28. <div id="info">
  29. <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>
  30. - O, P : change eye separation
  31. </div>
  32. <div id="buttons"></div>
  33. </div>
  34. <div id="log"></div>
  35. <script type="text/javascript" src="../build/Three.js"></script>
  36. <script type="text/javascript" src="js/Detector.js"></script>
  37. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  38. <script type="text/javascript" src="js/Stats.js"></script>
  39. <script type="text/javascript">
  40. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  41. var SCREEN_WIDTH = window.innerWidth;
  42. var SCREEN_HEIGHT = window.innerHeight;
  43. var container, stats;
  44. var camera, scene, webglRenderer;
  45. var lightMesh;
  46. var directionalLight, pointLight;
  47. var mouseX = 0, mouseY = 0;
  48. var windowHalfX = window.innerWidth / 2;
  49. var windowHalfY = window.innerHeight / 2;
  50. init();
  51. animate();
  52. function init() {
  53. container = document.createElement( 'div' );
  54. document.body.appendChild( container );
  55. camera = new THREE.Camera( 70, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 100000 );
  56. camera.position.z = 1000;
  57. camera.updateMatrix();
  58. scene = new THREE.Scene();
  59. // LIGHTS
  60. var ambient = new THREE.AmbientLight( 0x555555 );
  61. scene.add( ambient );
  62. directionalLight = new THREE.DirectionalLight( 0xffffff );
  63. directionalLight.position.x = 1;
  64. directionalLight.position.y = 1;
  65. directionalLight.position.z = 0.5;
  66. directionalLight.position.normalize();
  67. scene.add( directionalLight );
  68. pointLight = new THREE.PointLight( 0xffaa00 );
  69. pointLight.position.x = 0;
  70. pointLight.position.y = 0;
  71. pointLight.position.z = 0;
  72. scene.add( pointLight );
  73. sphere = new THREE.SphereGeometry( 100, 16, 8, 1 );
  74. lightMesh = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) );
  75. lightMesh.scale.x = lightMesh.scale.y = lightMesh.scale.z = 0.05;
  76. lightMesh.position = pointLight.position;
  77. lightMesh.overdraw = true;
  78. lightMesh.updateMatrix();
  79. scene.add(lightMesh);
  80. webglRenderer = new THREE.CrosseyedWebGLRenderer( { separation: 20, antialias: true } );
  81. webglRenderer.setSize( window.innerWidth, window.innerHeight );
  82. container.appendChild( webglRenderer.domElement );
  83. webglRenderer.setFaceCulling( 0 );
  84. stats = new Stats();
  85. stats.domElement.style.position = 'absolute';
  86. stats.domElement.style.top = '0px';
  87. stats.domElement.style.zIndex = 100;
  88. container.appendChild( stats.domElement );
  89. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  90. document.addEventListener( 'keydown', onKeyDown, false );
  91. var r = "textures/cube/SwedishRoyalCastle/";
  92. var urls = [ r + "px.jpg", r + "nx.jpg",
  93. r + "py.jpg", r + "ny.jpg",
  94. r + "pz.jpg", r + "nz.jpg" ];
  95. var textureCube = THREE.ImageUtils.loadTextureCube( urls );
  96. var camaroMaterials = {
  97. body: [],
  98. chrome: new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: textureCube } ),
  99. darkchrome: new THREE.MeshLambertMaterial( { color: 0x444444, envMap: textureCube } ),
  100. glass: new THREE.MeshBasicMaterial( { color: 0x223344, envMap: textureCube, opacity: 0.25, combine: THREE.MixOperation, reflectivity: 0.25, transparent: true } ),
  101. tire: new THREE.MeshLambertMaterial( { color: 0x050505 } ),
  102. interior: new THREE.MeshPhongMaterial( { color: 0x050505, shininess: 20 } ),
  103. black: new THREE.MeshLambertMaterial( { color: 0x000000 } )
  104. }
  105. camaroMaterials.body.push( [ "Orange", new THREE.MeshLambertMaterial( { color: 0xff6600, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.3 } ) ] );
  106. camaroMaterials.body.push( [ "Blue", new THREE.MeshLambertMaterial( { color: 0x226699, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.3 } ) ] );
  107. camaroMaterials.body.push( [ "Red", new THREE.MeshLambertMaterial( { color: 0x660000, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.5 } ) ] );
  108. camaroMaterials.body.push( [ "Black", new THREE.MeshLambertMaterial( { color: 0x000000, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.5 } ) ] );
  109. camaroMaterials.body.push( [ "White", new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.5 } ) ] );
  110. camaroMaterials.body.push( [ "Carmine", new THREE.MeshPhongMaterial( { color: 0x770000, specular:0xffaaaa, envMap: textureCube, combine: THREE.MultiplyOperation } ) ] );
  111. camaroMaterials.body.push( [ "Gold", new THREE.MeshPhongMaterial( { color: 0xaa9944, specular:0xbbaa99, shininess:50, envMap: textureCube, combine: THREE.MultiplyOperation } ) ] );
  112. camaroMaterials.body.push( [ "Bronze", new THREE.MeshPhongMaterial( { color: 0x150505, specular:0xee6600, shininess:10, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.5 } ) ] );
  113. camaroMaterials.body.push( [ "Chrome", new THREE.MeshPhongMaterial( { color: 0xffffff, specular:0xffffff, envMap: textureCube, combine: THREE.MultiplyOperation } ) ] );
  114. var loader = new THREE.BinaryLoader();
  115. loader.load( { model: "obj/camaro/CamaroNoUv_bin.js", callback: function( geometry ) { createScene( geometry, camaroMaterials ) } } );
  116. }
  117. function $( id ) { return document.getElementById( id ) }
  118. function createButtons( materials, geometry ) {
  119. var i, src = "", parent = $( "buttons" );
  120. for( i = 0; i < materials.length; i++ ) {
  121. src += '<button id="m' + i + '">' + materials[ i ][ 0 ] + '</button>';
  122. }
  123. parent.innerHTML = src;
  124. for( i = 0; i < materials.length; i++ ) {
  125. $( "m" + i ).counter = i;
  126. $( "m" + i ).addEventListener( 'click', function() { geometry.materials[ 0 ][ 0 ] = materials[ this.counter ][ 1 ] }, false );
  127. }
  128. }
  129. function createScene( geometry, materials ) {
  130. var s = 75, m = new THREE.MeshFaceMaterial();
  131. geometry.materials[ 0 ][ 0 ] = materials.body[ 0 ][ 1 ]; // car body
  132. geometry.materials[ 1 ][ 0 ] = materials.chrome; // wheels chrome
  133. geometry.materials[ 2 ][ 0 ] = materials.chrome; // grille chrome
  134. geometry.materials[ 3 ][ 0 ] = materials.darkchrome; // door lines
  135. geometry.materials[ 4 ][ 0 ] = materials.glass; // windshield
  136. geometry.materials[ 5 ][ 0 ] = materials.interior; // interior
  137. geometry.materials[ 6 ][ 0 ] = materials.tire; // tire
  138. geometry.materials[ 7 ][ 0 ] = materials.black; // tireling
  139. geometry.materials[ 8 ][ 0 ] = materials.black; // behind grille
  140. var mesh = new THREE.Mesh( geometry, m );
  141. mesh.rotation.y = 1;
  142. mesh.scale.x = mesh.scale.y = mesh.scale.z = s;
  143. scene.add( mesh );
  144. createButtons( materials.body, geometry );
  145. }
  146. function onDocumentMouseMove( event ) {
  147. mouseX = ( event.clientX - windowHalfX );
  148. mouseY = ( event.clientY - windowHalfY );
  149. }
  150. function onKeyDown ( event ) {
  151. switch( event.keyCode ) {
  152. /* O */
  153. case 79: webglRenderer.separation -= 0.5; break;
  154. /* P */
  155. case 80: webglRenderer.separation += 0.5; break;
  156. }
  157. console.log( webglRenderer.separation );
  158. };
  159. //
  160. function animate() {
  161. requestAnimationFrame( animate );
  162. render();
  163. stats.update();
  164. }
  165. function render() {
  166. var timer = - new Date().getTime() * 0.0002;
  167. camera.position.x += ( mouseX - camera.position.x ) * .05;
  168. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  169. lightMesh.position.x = 1500 * Math.cos( timer );
  170. lightMesh.position.z = 1500 * Math.sin( timer );
  171. webglRenderer.render( scene, camera );
  172. }
  173. function log(text) {
  174. var e = document.getElementById("log");
  175. e.innerHTML = text + "<br/>" + e.innerHTML;
  176. }
  177. </script>
  178. </body>
  179. </html>