webgl_materials_cars_camaro.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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"><a href="http://threejs.org" 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. <script src="../build/three.min.js"></script>
  32. <script src="js/loaders/BinaryLoader.js"></script>
  33. <script src="js/Detector.js"></script>
  34. <script src="js/libs/stats.min.js"></script>
  35. <script>
  36. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  37. var SCREEN_WIDTH = window.innerWidth;
  38. var SCREEN_HEIGHT = window.innerHeight;
  39. var container, stats;
  40. var camera, scene, renderer;
  41. var directionalLight, pointLight;
  42. var mouseX = 0, mouseY = 0;
  43. var windowHalfX = window.innerWidth / 2;
  44. var windowHalfY = window.innerHeight / 2;
  45. init();
  46. animate();
  47. function init() {
  48. container = document.createElement( 'div' );
  49. document.body.appendChild( container );
  50. // CAMERA
  51. camera = new THREE.PerspectiveCamera( 70, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
  52. camera.position.z = 1000;
  53. scene = new THREE.Scene();
  54. // LIGHTS
  55. var ambient = new THREE.AmbientLight( 0x020202 );
  56. scene.add( ambient );
  57. directionalLight = new THREE.DirectionalLight( 0xffffff );
  58. directionalLight.position.set( 1, 1, 0.5 ).normalize();
  59. scene.add( directionalLight );
  60. pointLight = new THREE.PointLight( 0xffaa00 );
  61. scene.add( pointLight );
  62. var sphere = new THREE.SphereGeometry( 100, 16, 8 );
  63. var mesh = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) );
  64. mesh.scale.set( 0.05, 0.05, 0.05 );
  65. pointLight.add( mesh );
  66. renderer = new THREE.WebGLRenderer();
  67. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  68. renderer.setFaceCulling( THREE.CullFaceNone );
  69. container.appendChild( renderer.domElement );
  70. stats = new Stats();
  71. stats.domElement.style.position = 'absolute';
  72. stats.domElement.style.top = '0px';
  73. stats.domElement.style.zIndex = 100;
  74. container.appendChild( stats.domElement );
  75. document.addEventListener('mousemove', onDocumentMouseMove, false);
  76. var r = "textures/cube/SwedishRoyalCastle/";
  77. var urls = [ r + "px.jpg", r + "nx.jpg",
  78. r + "py.jpg", r + "ny.jpg",
  79. r + "pz.jpg", r + "nz.jpg" ];
  80. var textureCube = THREE.ImageUtils.loadTextureCube( urls );
  81. var camaroMaterials = {
  82. body: {
  83. Orange: new THREE.MeshLambertMaterial( {
  84. color: 0xff6600,
  85. envMap: textureCube,
  86. combine: THREE.MixOperation,
  87. reflectivity: 0.3
  88. } ),
  89. Blue: new THREE.MeshLambertMaterial( {
  90. color: 0x226699,
  91. envMap: textureCube,
  92. combine: THREE.MixOperation,
  93. reflectivity: 0.3
  94. } ),
  95. Red: new THREE.MeshLambertMaterial( {
  96. color: 0x660000,
  97. envMap: textureCube,
  98. combine: THREE.MixOperation,
  99. reflectivity: 0.5
  100. } ),
  101. Black: new THREE.MeshLambertMaterial( {
  102. color: 0x000000,
  103. envMap: textureCube,
  104. combine: THREE.MixOperation,
  105. reflectivity: 0.5
  106. } ),
  107. White: new THREE.MeshLambertMaterial( {
  108. color: 0xffffff,
  109. envMap: textureCube,
  110. combine: THREE.MixOperation,
  111. reflectivity: 0.5
  112. } ),
  113. Carmine: new THREE.MeshPhongMaterial( {
  114. color: 0x770000,
  115. specular: 0xffaaaa,
  116. envMap: textureCube,
  117. combine: THREE.MultiplyOperation
  118. } ),
  119. Gold: new THREE.MeshPhongMaterial( {
  120. color: 0xaa9944,
  121. specular: 0xbbaa99,
  122. shininess: 50,
  123. envMap: textureCube,
  124. combine: THREE.MultiplyOperation
  125. } ),
  126. Bronze: new THREE.MeshPhongMaterial( {
  127. color: 0x150505,
  128. specular: 0xee6600,
  129. shininess: 10,
  130. envMap: textureCube,
  131. combine: THREE.MixOperation,
  132. reflectivity: 0.5
  133. } ),
  134. Chrome: new THREE.MeshPhongMaterial( {
  135. color: 0xffffff,
  136. specular:0xffffff,
  137. envMap: textureCube,
  138. combine: THREE.MultiplyOperation
  139. } )
  140. },
  141. chrome: new THREE.MeshLambertMaterial( {
  142. color: 0xffffff,
  143. envMap: textureCube
  144. } ),
  145. darkchrome: new THREE.MeshLambertMaterial( {
  146. color: 0x444444,
  147. envMap: textureCube
  148. } ),
  149. glass: new THREE.MeshBasicMaterial( {
  150. color: 0x223344,
  151. envMap: textureCube,
  152. opacity: 0.25,
  153. combine: THREE.MixOperation,
  154. reflectivity: 0.25,
  155. transparent: true
  156. } ),
  157. tire: new THREE.MeshLambertMaterial( {
  158. color: 0x050505
  159. } ),
  160. interior: new THREE.MeshPhongMaterial( {
  161. color: 0x050505,
  162. shininess: 20
  163. } ),
  164. black: new THREE.MeshLambertMaterial( {
  165. color: 0x000000
  166. } )
  167. };
  168. var loader = new THREE.BinaryLoader();
  169. loader.load( "obj/camaro/CamaroNoUv_bin.js", function( geometry ) { createScene( geometry, camaroMaterials ) } );
  170. //
  171. window.addEventListener( 'resize', onWindowResize, false );
  172. }
  173. function onWindowResize() {
  174. windowHalfX = window.innerWidth / 2;
  175. windowHalfY = window.innerHeight / 2;
  176. camera.aspect = window.innerWidth / window.innerHeight;
  177. camera.updateProjectionMatrix();
  178. renderer.setSize( window.innerWidth, window.innerHeight );
  179. }
  180. function createButtons( materials, faceMaterial ) {
  181. var buttons = document.getElementById( "buttons" );
  182. for ( var key in materials ) {
  183. var button = document.createElement( 'button' );
  184. button.textContent = key;
  185. button.addEventListener( 'click', function ( event ) {
  186. faceMaterial.materials[ 0 ] = materials[ this.textContent ];
  187. }, false );
  188. buttons.appendChild( button );
  189. }
  190. }
  191. function createScene( geometry, materials ) {
  192. var s = 75, m = new THREE.MeshFaceMaterial();
  193. m.materials[ 0 ] = materials.body[ "Orange" ]; // car body
  194. m.materials[ 1 ] = materials.chrome; // wheels chrome
  195. m.materials[ 2 ] = materials.chrome; // grille chrome
  196. m.materials[ 3 ] = materials.darkchrome; // door lines
  197. m.materials[ 4 ] = materials.glass; // windshield
  198. m.materials[ 5 ] = materials.interior; // interior
  199. m.materials[ 6 ] = materials.tire; // tire
  200. m.materials[ 7 ] = materials.black; // tireling
  201. m.materials[ 8 ] = materials.black; // behind grille
  202. var mesh = new THREE.Mesh( geometry, m );
  203. mesh.rotation.y = 1;
  204. mesh.scale.set( s, s, s );
  205. scene.add( mesh );
  206. createButtons( materials.body, m );
  207. }
  208. function onDocumentMouseMove(event) {
  209. mouseX = ( event.clientX - windowHalfX );
  210. mouseY = ( event.clientY - windowHalfY );
  211. }
  212. //
  213. function animate() {
  214. requestAnimationFrame( animate );
  215. render();
  216. stats.update();
  217. }
  218. function render() {
  219. var timer = -0.0002 * Date.now();
  220. camera.position.x += ( mouseX - camera.position.x ) * .05;
  221. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  222. camera.lookAt( scene.position );
  223. pointLight.position.x = 1500 * Math.cos( timer );
  224. pointLight.position.z = 1500 * Math.sin( timer );
  225. renderer.render( scene, camera );
  226. }
  227. </script>
  228. </body>
  229. </html>