webgl_materials_cars_camaro_crosseyed.html 8.1 KB

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