webgl_materials_cars_camaro_crosseyed.html 8.6 KB

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