webgl_materials_cars_camaro.html 7.9 KB

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