webgl_materials_cars_camaro_crosseyed.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.min.js"></script>
  35. <script src="js/effects/CrosseyedEffect.js"></script>
  36. <script src="js/Detector.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, effect;
  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
  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 = new THREE.Scene();
  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.WebGLRenderer( { antialias: true } );
  74. renderer.setFaceCulling( 0 );
  75. container.appendChild( renderer.domElement );
  76. effect = new THREE.CrosseyedEffect( renderer );
  77. effect.setSize( window.innerWidth, window.innerHeight );
  78. effect.separation = 20;
  79. stats = new Stats();
  80. stats.domElement.style.position = 'absolute';
  81. stats.domElement.style.top = '0px';
  82. stats.domElement.style.zIndex = 100;
  83. container.appendChild( stats.domElement );
  84. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  85. document.addEventListener( 'keydown', onKeyDown, false );
  86. var r = "textures/cube/SwedishRoyalCastle/";
  87. var urls = [ r + "px.jpg", r + "nx.jpg",
  88. r + "py.jpg", r + "ny.jpg",
  89. r + "pz.jpg", r + "nz.jpg" ];
  90. var textureCube = THREE.ImageUtils.loadTextureCube( urls );
  91. var camaroMaterials = {
  92. body: [],
  93. chrome: new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: textureCube } ),
  94. darkchrome: new THREE.MeshLambertMaterial( { color: 0x444444, envMap: textureCube } ),
  95. glass: new THREE.MeshBasicMaterial( { color: 0x223344, envMap: textureCube, opacity: 0.25, combine: THREE.MixOperation, reflectivity: 0.25, transparent: true } ),
  96. tire: new THREE.MeshLambertMaterial( { color: 0x050505 } ),
  97. interior: new THREE.MeshPhongMaterial( { color: 0x050505, shininess: 20 } ),
  98. black: new THREE.MeshLambertMaterial( { color: 0x000000 } )
  99. }
  100. camaroMaterials.body.push( [ "Orange", new THREE.MeshLambertMaterial( { color: 0xff6600, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.3 } ) ] );
  101. camaroMaterials.body.push( [ "Blue", new THREE.MeshLambertMaterial( { color: 0x226699, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.3 } ) ] );
  102. camaroMaterials.body.push( [ "Red", new THREE.MeshLambertMaterial( { color: 0x660000, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.5 } ) ] );
  103. camaroMaterials.body.push( [ "Black", new THREE.MeshLambertMaterial( { color: 0x000000, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.5 } ) ] );
  104. camaroMaterials.body.push( [ "White", new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.5 } ) ] );
  105. camaroMaterials.body.push( [ "Carmine", new THREE.MeshPhongMaterial( { color: 0x770000, specular:0xffaaaa, envMap: textureCube, combine: THREE.MultiplyOperation } ) ] );
  106. camaroMaterials.body.push( [ "Gold", new THREE.MeshPhongMaterial( { color: 0xaa9944, specular:0xbbaa99, shininess:50, envMap: textureCube, combine: THREE.MultiplyOperation } ) ] );
  107. camaroMaterials.body.push( [ "Bronze", new THREE.MeshPhongMaterial( { color: 0x150505, specular:0xee6600, shininess:10, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.5 } ) ] );
  108. camaroMaterials.body.push( [ "Chrome", new THREE.MeshPhongMaterial( { color: 0xffffff, specular:0xffffff, envMap: textureCube, combine: THREE.MultiplyOperation } ) ] );
  109. var loader = new THREE.BinaryLoader();
  110. loader.load( "obj/camaro/CamaroNoUv_bin.js", function( geometry ) { createScene( geometry, camaroMaterials ) } );
  111. //
  112. window.addEventListener( 'resize', onWindowResize, false );
  113. }
  114. function onWindowResize() {
  115. windowHalfX = window.innerWidth / 2;
  116. windowHalfY = window.innerHeight / 2;
  117. camera.aspect = window.innerWidth / window.innerHeight;
  118. camera.updateProjectionMatrix();
  119. effect.setSize( window.innerWidth, window.innerHeight );
  120. }
  121. function $( id ) { return document.getElementById( id ) }
  122. function createButtons( materials, geometry ) {
  123. var i, src = "", parent = $( "buttons" );
  124. for( i = 0; i < materials.length; i ++ ) {
  125. src += '<button id="m' + i + '">' + materials[ i ][ 0 ] + '</button>';
  126. }
  127. parent.innerHTML = src;
  128. for( i = 0; i < materials.length; i ++ ) {
  129. $( "m" + i ).counter = i;
  130. $( "m" + i ).addEventListener( 'click', function() { geometry.materials[ 0 ] = materials[ this.counter ][ 1 ] }, false );
  131. }
  132. }
  133. function createScene( geometry, materials ) {
  134. var s = 75, m = new THREE.MeshFaceMaterial();
  135. geometry.materials[ 0 ] = materials.body[ 0 ][ 1 ]; // car body
  136. geometry.materials[ 1 ] = materials.chrome; // wheels chrome
  137. geometry.materials[ 2 ] = materials.chrome; // grille chrome
  138. geometry.materials[ 3 ] = materials.darkchrome; // door lines
  139. geometry.materials[ 4 ] = materials.glass; // windshield
  140. geometry.materials[ 5 ] = materials.interior; // interior
  141. geometry.materials[ 6 ] = materials.tire; // tire
  142. geometry.materials[ 7 ] = materials.black; // tireling
  143. geometry.materials[ 8 ] = materials.black; // behind grille
  144. var mesh = new THREE.Mesh( geometry, m );
  145. mesh.rotation.y = 1;
  146. mesh.scale.x = mesh.scale.y = mesh.scale.z = s;
  147. scene.add( mesh );
  148. createButtons( materials.body, geometry );
  149. }
  150. function onDocumentMouseMove( event ) {
  151. mouseX = ( event.clientX - windowHalfX );
  152. mouseY = ( event.clientY - windowHalfY );
  153. }
  154. function onKeyDown ( event ) {
  155. switch( event.keyCode ) {
  156. /* O */
  157. case 79: effect.separation -= 0.5; break;
  158. /* P */
  159. case 80: effect.separation += 0.5; break;
  160. }
  161. console.log( effect.separation );
  162. };
  163. //
  164. function animate() {
  165. requestAnimationFrame( animate );
  166. render();
  167. stats.update();
  168. }
  169. function render() {
  170. var timer = -0.0002 * Date.now();
  171. camera.position.x += ( mouseX - camera.position.x ) * .05;
  172. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  173. camera.lookAt( camera.target );
  174. lightMesh.position.x = 1500 * Math.cos( timer );
  175. lightMesh.position.z = 1500 * Math.sin( timer );
  176. effect.render( scene, camera );
  177. }
  178. </script>
  179. </body>
  180. </html>