webgl_materials_cars_camaro_crosseyed.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. #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. <script src="../build/Three.js"></script>
  35. <script src="js/Detector.js"></script>
  36. <script src="js/RequestAnimationFrame.js"></script>
  37. <script src="js/Stats.js"></script>
  38. <script>
  39. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  40. var SCREEN_WIDTH = window.innerWidth;
  41. var SCREEN_HEIGHT = window.innerHeight;
  42. var container, stats;
  43. var camera, scene, renderer;
  44. var lightMesh;
  45. var directionalLight, pointLight;
  46. var mouseX = 0, mouseY = 0;
  47. var windowHalfX = window.innerWidth / 2;
  48. var windowHalfY = window.innerHeight / 2;
  49. init();
  50. animate();
  51. function init() {
  52. container = document.createElement( 'div' );
  53. document.body.appendChild( container );
  54. camera = new THREE.PerspectiveCamera( 70, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 100000 );
  55. camera.position.z = 1000;
  56. camera.target = new THREE.Vector3( 0, 0, 0 );
  57. scene = new THREE.Scene();
  58. // LIGHTS
  59. var ambient = new THREE.AmbientLight( 0x555555 );
  60. scene.add( ambient );
  61. directionalLight = new THREE.DirectionalLight( 0xffffff );
  62. directionalLight.position.set( 1, 1, 0.5 ).normalize();
  63. scene.add( directionalLight );
  64. pointLight = new THREE.PointLight( 0xffaa00 );
  65. pointLight.position.set( 0, 0, 0 );
  66. scene.add( pointLight );
  67. sphere = new THREE.SphereGeometry( 100, 16, 8, 1 );
  68. lightMesh = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) );
  69. lightMesh.scale.x = lightMesh.scale.y = lightMesh.scale.z = 0.05;
  70. lightMesh.position = pointLight.position;
  71. scene.add( lightMesh );
  72. renderer = new THREE.CrosseyedWebGLRenderer( { separation: 20, antialias: true } );
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. renderer.setFaceCulling( 0 );
  75. container.appendChild( renderer.domElement );
  76. stats = new Stats();
  77. stats.domElement.style.position = 'absolute';
  78. stats.domElement.style.top = '0px';
  79. stats.domElement.style.zIndex = 100;
  80. container.appendChild( stats.domElement );
  81. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  82. document.addEventListener( 'keydown', onKeyDown, false );
  83. var r = "textures/cube/SwedishRoyalCastle/";
  84. var urls = [ r + "px.jpg", r + "nx.jpg",
  85. r + "py.jpg", r + "ny.jpg",
  86. r + "pz.jpg", r + "nz.jpg" ];
  87. var textureCube = THREE.ImageUtils.loadTextureCube( urls );
  88. var camaroMaterials = {
  89. body: [],
  90. chrome: new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: textureCube } ),
  91. darkchrome: new THREE.MeshLambertMaterial( { color: 0x444444, envMap: textureCube } ),
  92. glass: new THREE.MeshBasicMaterial( { color: 0x223344, envMap: textureCube, opacity: 0.25, combine: THREE.MixOperation, reflectivity: 0.25, transparent: true } ),
  93. tire: new THREE.MeshLambertMaterial( { color: 0x050505 } ),
  94. interior: new THREE.MeshPhongMaterial( { color: 0x050505, shininess: 20 } ),
  95. black: new THREE.MeshLambertMaterial( { color: 0x000000 } )
  96. }
  97. camaroMaterials.body.push( [ "Orange", new THREE.MeshLambertMaterial( { color: 0xff6600, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.3 } ) ] );
  98. camaroMaterials.body.push( [ "Blue", new THREE.MeshLambertMaterial( { color: 0x226699, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.3 } ) ] );
  99. camaroMaterials.body.push( [ "Red", new THREE.MeshLambertMaterial( { color: 0x660000, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.5 } ) ] );
  100. camaroMaterials.body.push( [ "Black", new THREE.MeshLambertMaterial( { color: 0x000000, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.5 } ) ] );
  101. camaroMaterials.body.push( [ "White", new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.5 } ) ] );
  102. camaroMaterials.body.push( [ "Carmine", new THREE.MeshPhongMaterial( { color: 0x770000, specular:0xffaaaa, envMap: textureCube, combine: THREE.MultiplyOperation } ) ] );
  103. camaroMaterials.body.push( [ "Gold", new THREE.MeshPhongMaterial( { color: 0xaa9944, specular:0xbbaa99, shininess:50, envMap: textureCube, combine: THREE.MultiplyOperation } ) ] );
  104. camaroMaterials.body.push( [ "Bronze", new THREE.MeshPhongMaterial( { color: 0x150505, specular:0xee6600, shininess:10, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.5 } ) ] );
  105. camaroMaterials.body.push( [ "Chrome", new THREE.MeshPhongMaterial( { color: 0xffffff, specular:0xffffff, envMap: textureCube, combine: THREE.MultiplyOperation } ) ] );
  106. var loader = new THREE.BinaryLoader();
  107. loader.load( { model: "obj/camaro/CamaroNoUv_bin.js", callback: function( geometry ) { createScene( geometry, camaroMaterials ) } } );
  108. }
  109. function $( id ) { return document.getElementById( id ) }
  110. function createButtons( materials, geometry ) {
  111. var i, src = "", parent = $( "buttons" );
  112. for( i = 0; i < materials.length; i++ ) {
  113. src += '<button id="m' + i + '">' + materials[ i ][ 0 ] + '</button>';
  114. }
  115. parent.innerHTML = src;
  116. for( i = 0; i < materials.length; i++ ) {
  117. $( "m" + i ).counter = i;
  118. $( "m" + i ).addEventListener( 'click', function() { geometry.materials[ 0 ][ 0 ] = materials[ this.counter ][ 1 ] }, false );
  119. }
  120. }
  121. function createScene( geometry, materials ) {
  122. var s = 75, m = new THREE.MeshFaceMaterial();
  123. geometry.materials[ 0 ][ 0 ] = materials.body[ 0 ][ 1 ]; // car body
  124. geometry.materials[ 1 ][ 0 ] = materials.chrome; // wheels chrome
  125. geometry.materials[ 2 ][ 0 ] = materials.chrome; // grille chrome
  126. geometry.materials[ 3 ][ 0 ] = materials.darkchrome; // door lines
  127. geometry.materials[ 4 ][ 0 ] = materials.glass; // windshield
  128. geometry.materials[ 5 ][ 0 ] = materials.interior; // interior
  129. geometry.materials[ 6 ][ 0 ] = materials.tire; // tire
  130. geometry.materials[ 7 ][ 0 ] = materials.black; // tireling
  131. geometry.materials[ 8 ][ 0 ] = materials.black; // behind grille
  132. var mesh = new THREE.Mesh( geometry, m );
  133. mesh.rotation.y = 1;
  134. mesh.scale.x = mesh.scale.y = mesh.scale.z = s;
  135. scene.add( mesh );
  136. createButtons( materials.body, geometry );
  137. }
  138. function onDocumentMouseMove( event ) {
  139. mouseX = ( event.clientX - windowHalfX );
  140. mouseY = ( event.clientY - windowHalfY );
  141. }
  142. function onKeyDown ( event ) {
  143. switch( event.keyCode ) {
  144. /* O */
  145. case 79: renderer.separation -= 0.5; break;
  146. /* P */
  147. case 80: renderer.separation += 0.5; break;
  148. }
  149. console.log( renderer.separation );
  150. };
  151. //
  152. function animate() {
  153. requestAnimationFrame( animate );
  154. render();
  155. stats.update();
  156. }
  157. function render() {
  158. var timer = - new Date().getTime() * 0.0002;
  159. camera.position.x += ( mouseX - camera.position.x ) * .05;
  160. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  161. camera.lookAt( camera.target );
  162. lightMesh.position.x = 1500 * Math.cos( timer );
  163. lightMesh.position.z = 1500 * Math.sin( timer );
  164. renderer.render( scene, camera );
  165. }
  166. </script>
  167. </body>
  168. </html>