webgl_materials_bumpmap.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - bump map [Lee Perry-Smith]</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. font-weight: bold;
  14. overflow:hidden;
  15. }
  16. a { color: #ffffff; }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. color: #ffffff;
  21. padding: 5px;
  22. font-family:Monospace;
  23. font-size:13px;
  24. text-align:center;
  25. z-index:1000;
  26. }
  27. #oldie {
  28. background:rgb(200,100,0) !important;
  29. color:#fff;
  30. }
  31. #vt { display:none }
  32. #vt, #vt a { color:orange; }
  33. .code { }
  34. </style>
  35. </head>
  36. <body>
  37. <div id="info">
  38. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl bump mapping without tangents using <a href="http://mmikkelsen3d.blogspot.sk/2011/07/derivative-maps.html">Morten Mikkelsen's</a> method -
  39. <a href="http://www.ir-ltd.net/infinite-3d-head-scan-released/" target="_blank">Lee Perry-Smith</a> head
  40. </div>
  41. <script src="../build/Three.js"></script>
  42. <script src="js/Detector.js"></script>
  43. <script src="js/Stats.js"></script>
  44. <script>
  45. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  46. var statsEnabled = true;
  47. var container, stats, loader;
  48. var camera, scene, renderer;
  49. var mesh;
  50. var directionalLight, directionalLight2, pointLight, ambientLight, spotLight;
  51. var mouseX = 0;
  52. var mouseY = 0;
  53. var targetX = 0;
  54. var targetY = 0;
  55. var windowHalfX = window.innerWidth / 2;
  56. var windowHalfY = window.innerHeight / 2;
  57. init();
  58. animate();
  59. function init() {
  60. container = document.createElement( 'div' );
  61. document.body.appendChild( container );
  62. //
  63. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
  64. camera.position.z = 1200;
  65. scene = new THREE.Scene();
  66. // LIGHTS
  67. ambientLight = new THREE.AmbientLight( 0x444444 );
  68. scene.add( ambientLight );
  69. //
  70. pointLight = new THREE.PointLight( 0xffffff, 1.5, 1000 );
  71. pointLight.color.setHSV( 0.05, 0.05, 1 );
  72. pointLight.position.set( 0, 0, 600 );
  73. scene.add( pointLight );
  74. // shadow for PointLight
  75. spotLight = new THREE.SpotLight( 0xffffff, 1.5 );
  76. spotLight.position.set( 0.05, 0.05, 1 );
  77. spotLight.color.setHSV( 0.6, 0.05, 1 );
  78. scene.add( spotLight );
  79. spotLight.position.multiplyScalar( 700 );
  80. spotLight.castShadow = true;
  81. spotLight.onlyShadow = true;
  82. //spotLight.shadowCameraVisible = true;
  83. spotLight.shadowMapWidth = 2048;
  84. spotLight.shadowMapHeight = 2048;
  85. spotLight.shadowCameraNear = 200;
  86. spotLight.shadowCameraFar = 1500;
  87. spotLight.shadowCameraFov = 40;
  88. spotLight.shadowBias = -0.005;
  89. spotLight.shadowDarkness = 0.35;
  90. //
  91. directionalLight = new THREE.DirectionalLight( 0xffffff, 1.5 );
  92. directionalLight.position.set( 1, -0.5, 1 );
  93. directionalLight.color.setHSV( 0.6, 0.05, 1 );
  94. scene.add( directionalLight );
  95. directionalLight.position.multiplyScalar( 500 );
  96. directionalLight.castShadow = true;
  97. //directionalLight.shadowCameraVisible = true;
  98. directionalLight.shadowMapWidth = 2048;
  99. directionalLight.shadowMapHeight = 2048;
  100. directionalLight.shadowCameraNear = 200;
  101. directionalLight.shadowCameraFar = 1500;
  102. directionalLight.shadowCameraLeft = -500;
  103. directionalLight.shadowCameraRight = 500;
  104. directionalLight.shadowCameraTop = 500;
  105. directionalLight.shadowCameraBottom = -500;
  106. directionalLight.shadowBias = -0.005;
  107. directionalLight.shadowDarkness = 0.35;
  108. //
  109. directionalLight2 = new THREE.DirectionalLight( 0xffffff, 1.2 );
  110. directionalLight2.position.set( 1, -0.5, -1 );
  111. directionalLight2.color.setHSV( 0.08, 0.35, 1 );
  112. scene.add( directionalLight2 );
  113. var mapHeight = THREE.ImageUtils.loadTexture( "obj/leeperrysmith/Infinite-Level_02_Disp_NoSmoothUV-4096.jpg" );
  114. mapHeight.anisotropy = 16;
  115. mapHeight.repeat.set( 0.998, 0.998 );
  116. mapHeight.offset.set( 0.001, 0.001 )
  117. mapHeight.wrapS = mapHeight.wrapT = THREE.RepeatWrapping;
  118. var material = new THREE.MeshPhongMaterial( { ambient: 0x552811, color: 0x552811, specular: 0x333333, shininess: 25, perPixel: true, bumpMap: mapHeight, bumpScale: 19, metal: false } );
  119. loader = new THREE.JSONLoader( true );
  120. document.body.appendChild( loader.statusDomElement );
  121. loader.load( "obj/leeperrysmith/LeePerrySmith.js", function( geometry ) { createScene( geometry, 100, material ) } );
  122. renderer = new THREE.WebGLRenderer( { antialias: true, clearColor: 0x060708, clearAlpha: 1 } );
  123. renderer.setSize( window.innerWidth, window.innerHeight );
  124. container.appendChild( renderer.domElement );
  125. renderer.shadowMapEnabled = true;
  126. renderer.shadowMapCullFrontFaces = false;
  127. //
  128. renderer.gammaInput = true;
  129. renderer.gammaOutput = true;
  130. renderer.physicallyBasedShading = true;
  131. //
  132. if ( statsEnabled ) {
  133. stats = new Stats();
  134. stats.domElement.style.position = 'absolute';
  135. stats.domElement.style.top = '0px';
  136. stats.domElement.style.zIndex = 100;
  137. stats.domElement.children[ 0 ].children[ 0 ].style.color = "#aaa";
  138. stats.domElement.children[ 0 ].style.background = "transparent";
  139. stats.domElement.children[ 0 ].children[ 1 ].style.display = "none";
  140. container.appendChild( stats.domElement );
  141. }
  142. // EVENTS
  143. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  144. window.addEventListener( 'resize', onWindowResize, false );
  145. }
  146. function createScene( geometry, scale, material ) {
  147. mesh = new THREE.Mesh( geometry, material );
  148. mesh.position.y = - 50;
  149. mesh.scale.set( scale, scale, scale );
  150. mesh.castShadow = true;
  151. mesh.receiveShadow = true;
  152. scene.add( mesh );
  153. loader.statusDomElement.style.display = "none";
  154. }
  155. //
  156. function onWindowResize( event ) {
  157. SCREEN_WIDTH = window.innerWidth;
  158. SCREEN_HEIGHT = window.innerHeight;
  159. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  160. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  161. camera.updateProjectionMatrix();
  162. }
  163. function onDocumentMouseMove( event ) {
  164. mouseX = ( event.clientX - windowHalfX );
  165. mouseY = ( event.clientY - windowHalfY );
  166. }
  167. //
  168. function animate() {
  169. requestAnimationFrame( animate );
  170. render();
  171. if ( statsEnabled ) stats.update();
  172. }
  173. function render() {
  174. targetX = mouseX * .001;
  175. targetY = mouseY * .001;
  176. if ( mesh ) {
  177. mesh.rotation.y += 0.05 * ( targetX - mesh.rotation.y );
  178. mesh.rotation.x += 0.05 * ( targetY - mesh.rotation.x );
  179. }
  180. renderer.render( scene, camera );
  181. }
  182. </script>
  183. </body>
  184. </html>