webgl_materials_displacementmap.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - displacement map</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. }
  26. #vt { display:none }
  27. #vt, #vt a { color:orange; }
  28. #stats { position: absolute; top:0; left: 0 }
  29. #stats #fps { background: transparent !important }
  30. #stats #fps #fpsText { color: #aaa !important }
  31. #stats #fps #fpsGraph { display: none }
  32. </style>
  33. </head>
  34. <body>
  35. <div id="info">
  36. <a href="http://threejs.org" target="_blank">three.js</a> - (<span id="description">normal + ao + displacement + environment</span>) map demo.<br />
  37. ninja head from <a href="http://developer.amd.com/tools-and-sdks/archive/legacy-cpu-gpu-tools/amd-gpu-meshmapper/" target="_blank">AMD GPU MeshMapper</a>
  38. <div id="vt">displacement mapping requires vertex textures</div>
  39. </div>
  40. <script src="../build/three.min.js"></script>
  41. <script src="js/controls/OrbitControls.js"></script>
  42. <script src="js/loaders/BinaryLoader.js"></script>
  43. <script src="js/Detector.js"></script>
  44. <script src="js/libs/stats.min.js"></script>
  45. <script src='js/libs/dat.gui.min.js'></script>
  46. <script>
  47. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  48. var stats, loader;
  49. var camera, scene, renderer, controls;
  50. var settings = {
  51. metalness: 1.0,
  52. roughness: 0.4,
  53. aoMapIntensity: 1.0,
  54. displacementScale: 1.0,
  55. normalScale: 1.0
  56. };
  57. var mesh, material;
  58. var pointLight;
  59. var mouseX = 0;
  60. var mouseY = 0;
  61. var windowHalfX = window.innerWidth / 2;
  62. var windowHalfY = window.innerHeight / 2;
  63. var height = 500; // of camera frustum
  64. var r = 0.0;
  65. init();
  66. animate();
  67. initGui();
  68. // Init gui
  69. function initGui() {
  70. var gui = new dat.GUI();
  71. //var gui = gui.addFolder( "Material" );
  72. gui.add( settings, "metalness" ).min( 0 ).max( 1 ).onChange( function( value ) {
  73. material.metalness = value;
  74. } );
  75. gui.add( settings, "roughness" ).min( 0 ).max( 1 ).onChange( function( value ) {
  76. material.roughness = value;
  77. } );
  78. gui.add( settings, "aoMapIntensity" ).min( 0 ).max( 1 ).onChange( function( value ) {
  79. material.aoMapIntensity = value;
  80. } );
  81. gui.add( settings, "displacementScale" ).min( 0 ).max( 3.0 ).onChange( function( value ) {
  82. material.displacementScale = value;
  83. } );
  84. gui.add( settings, "normalScale" ).min( - 1 ).max( 1 ).onChange( function( value ) {
  85. material.normalScale = new THREE.Vector2( 1, - 1 ).multiplyScalar( value );
  86. } );
  87. }
  88. function init() {
  89. var container = document.createElement( 'div' );
  90. document.body.appendChild( container );
  91. renderer = new THREE.WebGLRenderer();
  92. renderer.setPixelRatio( window.devicePixelRatio );
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. container.appendChild( renderer.domElement );
  95. renderer.gammaInput = true;
  96. renderer.gammaOutput = true;
  97. //
  98. scene = new THREE.Scene();
  99. var aspect = window.innerWidth / window.innerHeight;
  100. camera = new THREE.OrthographicCamera( - height * aspect, height * aspect, height, - height, 1, 10000 );
  101. camera.position.z = 1500;
  102. scene.add( camera );
  103. controls = new THREE.OrbitControls( camera, renderer.domElement );
  104. controls.enableZoom = false;
  105. controls.enableDamping = true;
  106. // lights
  107. var ambientLight = new THREE.AmbientLight( 0x111111 );
  108. scene.add( ambientLight );
  109. pointLight = new THREE.PointLight( 0xff0000, 0.5 );
  110. pointLight.position.z = 2500;
  111. scene.add( pointLight );
  112. var pointLight2 = new THREE.PointLight( 0xff6666, 1 );
  113. camera.add( pointLight2 );
  114. var pointLight3 = new THREE.PointLight( 0x0000ff, 0.5 );
  115. pointLight3.position.x = - 1000;
  116. pointLight3.position.z = 1000;
  117. scene.add( pointLight3 );
  118. // env map
  119. var path = "textures/cube/SwedishRoyalCastle/";
  120. var format = '.jpg';
  121. var urls = [
  122. path + 'px' + format, path + 'nx' + format,
  123. path + 'py' + format, path + 'ny' + format,
  124. path + 'pz' + format, path + 'nz' + format
  125. ];
  126. var reflectionCube = new THREE.CubeTextureLoader().load( urls );
  127. // textures
  128. var textureLoader = new THREE.TextureLoader();
  129. var normalMap = textureLoader.load( "textures/normal/ninja/normal.jpg" );
  130. var aoMap = textureLoader.load( "textures/normal/ninja/ao.jpg" );
  131. var displacementMap = textureLoader.load( "textures/normal/ninja/displacement.jpg" );
  132. // material
  133. material = new THREE.MeshStandardMaterial( {
  134. color: 0x888888,
  135. roughness: settings.roughness,
  136. metalness: settings.metalness,
  137. normalMap: normalMap,
  138. normalScale: new THREE.Vector2( 1, - 1 ), // why does the normal map require negation in this case?
  139. aoMap: aoMap,
  140. aoMapIntensity: 1,
  141. displacementMap: displacementMap,
  142. displacementScale: 2.436143,
  143. displacementBias: - 0.428408,
  144. envMap: reflectionCube,
  145. side: THREE.DoubleSide
  146. } );
  147. //
  148. loader = new THREE.BinaryLoader();
  149. loader.load( "obj/ninja/NinjaLo_bin.js", function( geometry ) {
  150. geometry.faceVertexUvs[ 1 ] = geometry.faceVertexUvs[ 0 ]; // 2nd set of UVs required for aoMap and displacementMap with MeshPhongMaterial
  151. mesh = new THREE.Mesh( geometry, material );
  152. mesh.scale.multiplyScalar( 25 );
  153. scene.add( mesh );
  154. } );
  155. //
  156. var description = "normal + ao" + ( renderer.supportsVertexTextures() ? " + displacement + environment" : " + <strike>displacement</strike>" );
  157. document.getElementById( "description" ).innerHTML = description;
  158. document.getElementById( "vt" ).style.display = renderer.supportsVertexTextures() ? "none" : "block";
  159. //
  160. stats = new Stats();
  161. container.appendChild( stats.domElement );
  162. //
  163. window.addEventListener( 'resize', onWindowResize, false );
  164. }
  165. function onWindowResize() {
  166. var aspect = window.innerWidth / window.innerHeight;
  167. camera.left = - height * aspect;
  168. camera.right = height * aspect;
  169. camera.top = height;
  170. camera.bottom = - height;
  171. camera.updateProjectionMatrix();
  172. renderer.setSize( window.innerWidth, window.innerHeight );
  173. }
  174. //
  175. function animate() {
  176. requestAnimationFrame( animate );
  177. controls.update();
  178. render();
  179. stats.update();
  180. }
  181. function render() {
  182. pointLight.position.x = 2500 * Math.cos( r );
  183. pointLight.position.z = 2500 * Math.sin( r );
  184. r += 0.01;
  185. renderer.render( scene, camera );
  186. }
  187. </script>
  188. </body>
  189. </html>