webgl_materials_cars_camaro.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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://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. <script src="../build/Three.js"></script>
  32. <script src="js/Detector.js"></script>
  33. <script src="js/RequestAnimationFrame.js"></script>
  34. <script src="js/Stats.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 = 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( 0x555555 );
  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. pointLight.position.set( 0, 0, 0 );
  62. scene.add( pointLight );
  63. sphere = new THREE.SphereGeometry( 100, 16, 8 );
  64. lightMesh = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) );
  65. lightMesh.scale.set( 0.05, 0.05, 0.05 );
  66. lightMesh.position = pointLight.position;
  67. scene.add( lightMesh );
  68. renderer = new THREE.WebGLRenderer();
  69. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  70. renderer.setFaceCulling( 0 );
  71. container.appendChild( renderer.domElement );
  72. stats = new Stats();
  73. stats.domElement.style.position = 'absolute';
  74. stats.domElement.style.top = '0px';
  75. stats.domElement.style.zIndex = 100;
  76. container.appendChild( stats.domElement );
  77. document.addEventListener('mousemove', onDocumentMouseMove, false);
  78. var r = "textures/cube/SwedishRoyalCastle/";
  79. var urls = [ r + "px.jpg", r + "nx.jpg",
  80. r + "py.jpg", r + "ny.jpg",
  81. r + "pz.jpg", r + "nz.jpg" ];
  82. var textureCube = THREE.ImageUtils.loadTextureCube( urls );
  83. var camaroMaterials = {
  84. body: [],
  85. chrome: new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: textureCube } ),
  86. darkchrome: new THREE.MeshLambertMaterial( { color: 0x444444, envMap: textureCube } ),
  87. glass: new THREE.MeshBasicMaterial( { color: 0x223344, envMap: textureCube, opacity: 0.25, combine: THREE.MixOperation, reflectivity: 0.25, transparent: true } ),
  88. tire: new THREE.MeshLambertMaterial( { color: 0x050505 } ),
  89. interior: new THREE.MeshPhongMaterial( { color: 0x050505, shininess: 20 } ),
  90. black: new THREE.MeshLambertMaterial( { color: 0x000000 } )
  91. }
  92. camaroMaterials.body.push( [ "Orange", new THREE.MeshLambertMaterial( { color: 0xff6600, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.3 } ) ] );
  93. camaroMaterials.body.push( [ "Blue", new THREE.MeshLambertMaterial( { color: 0x226699, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.3 } ) ] );
  94. camaroMaterials.body.push( [ "Red", new THREE.MeshLambertMaterial( { color: 0x660000, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.5 } ) ] );
  95. camaroMaterials.body.push( [ "Black", new THREE.MeshLambertMaterial( { color: 0x000000, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.5 } ) ] );
  96. camaroMaterials.body.push( [ "White", new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.5 } ) ] );
  97. camaroMaterials.body.push( [ "Carmine", new THREE.MeshPhongMaterial( { color: 0x770000, specular:0xffaaaa, envMap: textureCube, combine: THREE.MultiplyOperation } ) ] );
  98. camaroMaterials.body.push( [ "Gold", new THREE.MeshPhongMaterial( { color: 0xaa9944, specular:0xbbaa99, shininess:50, envMap: textureCube, combine: THREE.MultiplyOperation } ) ] );
  99. camaroMaterials.body.push( [ "Bronze", new THREE.MeshPhongMaterial( { color: 0x150505, specular:0xee6600, shininess:10, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.5 } ) ] );
  100. camaroMaterials.body.push( [ "Chrome", new THREE.MeshPhongMaterial( { color: 0xffffff, specular:0xffffff, envMap: textureCube, combine: THREE.MultiplyOperation } ) ] );
  101. var loader = new THREE.BinaryLoader();
  102. loader.load( "obj/camaro/CamaroNoUv_bin.js", function( geometry ) { createScene( geometry, camaroMaterials ) } );
  103. }
  104. function $( id ) { return document.getElementById( id ) }
  105. function createButtons( materials, geometry ) {
  106. var i, src = "", parent = $( "buttons" );
  107. for( i = 0; i < materials.length; i ++ ) {
  108. src += '<button id="m' + i + '">' + materials[ i ][ 0 ] + '</button>';
  109. }
  110. parent.innerHTML = src;
  111. for( i = 0; i < materials.length; i ++ ) {
  112. $( "m" + i ).counter = i;
  113. $( "m" + i ).addEventListener( 'click', function() { geometry.materials[ 0 ] = materials[ this.counter ][ 1 ] }, false );
  114. }
  115. }
  116. function createScene( geometry, materials ) {
  117. var s = 75, m = new THREE.MeshFaceMaterial();
  118. geometry.materials[ 0 ] = materials.body[ 0 ][ 1 ]; // car body
  119. geometry.materials[ 1 ] = materials.chrome; // wheels chrome
  120. geometry.materials[ 2 ] = materials.chrome; // grille chrome
  121. geometry.materials[ 3 ] = materials.darkchrome; // door lines
  122. geometry.materials[ 4 ] = materials.glass; // windshield
  123. geometry.materials[ 5 ] = materials.interior; // interior
  124. geometry.materials[ 6 ] = materials.tire; // tire
  125. geometry.materials[ 7 ] = materials.black; // tireling
  126. geometry.materials[ 8 ] = materials.black; // behind grille
  127. var mesh = new THREE.Mesh( geometry, m );
  128. mesh.rotation.y = 1;
  129. mesh.scale.set( s, s, s );
  130. scene.add( mesh );
  131. createButtons( materials.body, geometry );
  132. }
  133. function onDocumentMouseMove(event) {
  134. mouseX = ( event.clientX - windowHalfX );
  135. mouseY = ( event.clientY - windowHalfY );
  136. }
  137. //
  138. function animate() {
  139. requestAnimationFrame( animate );
  140. render();
  141. stats.update();
  142. }
  143. function render() {
  144. var timer = -0.0002 * Date.now();
  145. camera.position.x += ( mouseX - camera.position.x ) * .05;
  146. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  147. camera.lookAt( scene.position );
  148. lightMesh.position.x = 1500 * Math.cos( timer );
  149. lightMesh.position.z = 1500 * Math.sin( timer );
  150. renderer.render( scene, camera );
  151. }
  152. </script>
  153. </body>
  154. </html>