webgl_materials_cars_camaro.html 8.1 KB

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