webgl_materials_cars_camaro.html 7.9 KB

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