webgl_materials_bumpmap.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. #stats { position: absolute; top:0; left: 0 }
  34. #stats #fps { background: transparent !important }
  35. #stats #fps #fpsText { color: #aaa !important }
  36. #stats #fps #fpsGraph { display: none }
  37. </style>
  38. </head>
  39. <body>
  40. <div id="info">
  41. <a href="http://threejs.org" 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 -
  42. <a href="http://www.ir-ltd.net/infinite-3d-head-scan-released/" target="_blank">Lee Perry-Smith</a> head
  43. </div>
  44. <script src="../build/three.min.js"></script>
  45. <script src="js/Detector.js"></script>
  46. <script src="js/libs/stats.min.js"></script>
  47. <script>
  48. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  49. var statsEnabled = true;
  50. var container, stats, loader;
  51. var camera, scene, renderer;
  52. var mesh;
  53. var directionalLight, directionalLight2, pointLight, ambientLight, spotLight;
  54. var mouseX = 0;
  55. var mouseY = 0;
  56. var targetX = 0;
  57. var targetY = 0;
  58. var windowHalfX = window.innerWidth / 2;
  59. var windowHalfY = window.innerHeight / 2;
  60. init();
  61. animate();
  62. function init() {
  63. container = document.createElement( 'div' );
  64. document.body.appendChild( container );
  65. //
  66. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
  67. camera.position.z = 1200;
  68. scene = new THREE.Scene();
  69. // LIGHTS
  70. ambientLight = new THREE.AmbientLight( 0x444444 );
  71. scene.add( ambientLight );
  72. //
  73. pointLight = new THREE.PointLight( 0xffffff, 1.5, 1000 );
  74. pointLight.color.setHSL( 0.05, 1, 0.95 );
  75. pointLight.position.set( 0, 0, 600 );
  76. scene.add( pointLight );
  77. // shadow for PointLight
  78. spotLight = new THREE.SpotLight( 0xffffff, 1.5 );
  79. spotLight.position.set( 0.05, 0.05, 1 );
  80. spotLight.color.setHSL( 0.6, 1, 0.95 );
  81. scene.add( spotLight );
  82. spotLight.position.multiplyScalar( 700 );
  83. spotLight.castShadow = true;
  84. spotLight.onlyShadow = true;
  85. // spotLight.shadowCameraVisible = true;
  86. spotLight.shadowMapWidth = 2048;
  87. spotLight.shadowMapHeight = 2048;
  88. spotLight.shadowCameraNear = 200;
  89. spotLight.shadowCameraFar = 1500;
  90. spotLight.shadowCameraFov = 40;
  91. spotLight.shadowBias = -0.005;
  92. spotLight.shadowDarkness = 0.35;
  93. //
  94. directionalLight = new THREE.DirectionalLight( 0xffffff, 1.5 );
  95. directionalLight.position.set( 1, -0.5, 1 );
  96. directionalLight.color.setHSL( 0.6, 1, 0.95 );
  97. scene.add( directionalLight );
  98. directionalLight.position.multiplyScalar( 500 );
  99. directionalLight.castShadow = true;
  100. // directionalLight.shadowCameraVisible = true;
  101. directionalLight.shadowMapWidth = 2048;
  102. directionalLight.shadowMapHeight = 2048;
  103. directionalLight.shadowCameraNear = 200;
  104. directionalLight.shadowCameraFar = 1500;
  105. directionalLight.shadowCameraLeft = -500;
  106. directionalLight.shadowCameraRight = 500;
  107. directionalLight.shadowCameraTop = 500;
  108. directionalLight.shadowCameraBottom = -500;
  109. directionalLight.shadowBias = -0.005;
  110. directionalLight.shadowDarkness = 0.35;
  111. //
  112. directionalLight2 = new THREE.DirectionalLight( 0xffffff, 1.2 );
  113. directionalLight2.position.set( 1, -0.5, -1 );
  114. directionalLight2.color.setHSL( 0.08, 1, 0.825 );
  115. scene.add( directionalLight2 );
  116. var mapHeight = THREE.ImageUtils.loadTexture( "obj/leeperrysmith/Infinite-Level_02_Disp_NoSmoothUV-4096.jpg" );
  117. mapHeight.anisotropy = 4;
  118. mapHeight.repeat.set( 0.998, 0.998 );
  119. mapHeight.offset.set( 0.001, 0.001 );
  120. mapHeight.wrapS = mapHeight.wrapT = THREE.RepeatWrapping;
  121. mapHeight.format = THREE.RGBFormat;
  122. var material = new THREE.MeshPhongMaterial( { color: 0x552811, specular: 0x333333, shininess: 25, bumpMap: mapHeight, bumpScale: 19, metal: false } );
  123. loader = new THREE.JSONLoader( true );
  124. document.body.appendChild( loader.statusDomElement );
  125. loader.load( "obj/leeperrysmith/LeePerrySmith.js", function( geometry ) { createScene( geometry, 100, material ) } );
  126. renderer = new THREE.WebGLRenderer( { antialias: false } );
  127. renderer.setClearColor( 0x060708 );
  128. renderer.setPixelRatio( window.devicePixelRatio );
  129. renderer.setSize( window.innerWidth, window.innerHeight );
  130. container.appendChild( renderer.domElement );
  131. renderer.shadowMapEnabled = true;
  132. renderer.shadowMapCullFace = THREE.CullFaceBack;
  133. //
  134. renderer.gammaInput = true;
  135. renderer.gammaOutput = true;
  136. //
  137. if ( statsEnabled ) {
  138. stats = new Stats();
  139. container.appendChild( stats.domElement );
  140. }
  141. // EVENTS
  142. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  143. window.addEventListener( 'resize', onWindowResize, false );
  144. }
  145. function createScene( geometry, scale, material ) {
  146. mesh = new THREE.Mesh( geometry, material );
  147. mesh.position.y = - 50;
  148. mesh.scale.set( scale, scale, scale );
  149. mesh.castShadow = true;
  150. mesh.receiveShadow = true;
  151. scene.add( mesh );
  152. loader.statusDomElement.style.display = "none";
  153. }
  154. //
  155. function onWindowResize( event ) {
  156. SCREEN_WIDTH = window.innerWidth;
  157. SCREEN_HEIGHT = window.innerHeight;
  158. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  159. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  160. camera.updateProjectionMatrix();
  161. }
  162. function onDocumentMouseMove( event ) {
  163. mouseX = ( event.clientX - windowHalfX );
  164. mouseY = ( event.clientY - windowHalfY );
  165. }
  166. //
  167. function animate() {
  168. requestAnimationFrame( animate );
  169. render();
  170. if ( statsEnabled ) stats.update();
  171. }
  172. function render() {
  173. targetX = mouseX * .001;
  174. targetY = mouseY * .001;
  175. if ( mesh ) {
  176. mesh.rotation.y += 0.05 * ( targetX - mesh.rotation.y );
  177. mesh.rotation.x += 0.05 * ( targetY - mesh.rotation.x );
  178. }
  179. renderer.render( scene, camera );
  180. }
  181. </script>
  182. </body>
  183. </html>