webgl_materials_displacementmap.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. </style>
  29. </head>
  30. <body>
  31. <div id="info">
  32. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - (<span id="description">normal + ao + displacement + environment</span>) map demo.<br />
  33. ninja head from <a href="http://developer.amd.com/tools-and-sdks/archive/legacy-cpu-gpu-tools/amd-gpu-meshmapper/" target="_blank" rel="noopener">AMD GPU MeshMapper</a>
  34. <div id="vt">displacement mapping requires vertex textures</div>
  35. </div>
  36. <script src="../build/three.js"></script>
  37. <script src="js/controls/OrbitControls.js"></script>
  38. <script src="js/loaders/OBJLoader.js"></script>
  39. <script src="js/WebGL.js"></script>
  40. <script src="js/libs/stats.min.js"></script>
  41. <script src='js/libs/dat.gui.min.js'></script>
  42. <script>
  43. if ( WEBGL.isWebGLAvailable() === false ) {
  44. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  45. }
  46. var stats;
  47. var camera, scene, renderer, controls;
  48. var settings = {
  49. metalness: 1.0,
  50. roughness: 0.4,
  51. ambientIntensity: 0.2,
  52. aoMapIntensity: 1.0,
  53. envMapIntensity: 1.0,
  54. displacementScale: 2.436143, // from original model
  55. normalScale: 1.0
  56. };
  57. var mesh, material;
  58. var pointLight, ambientLight;
  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, "ambientIntensity" ).min( 0 ).max( 1 ).onChange( function( value ) {
  82. ambientLight.intensity = value;
  83. } );
  84. gui.add( settings, "envMapIntensity" ).min( 0 ).max( 3 ).onChange( function( value ) {
  85. material.envMapIntensity = value;
  86. } );
  87. gui.add( settings, "displacementScale" ).min( 0 ).max( 3.0 ).onChange( function( value ) {
  88. material.displacementScale = value;
  89. } );
  90. gui.add( settings, "normalScale" ).min( - 1 ).max( 1 ).onChange( function( value ) {
  91. material.normalScale.set( 1, - 1 ).multiplyScalar( value );
  92. } );
  93. }
  94. function init() {
  95. var container = document.createElement( 'div' );
  96. document.body.appendChild( container );
  97. renderer = new THREE.WebGLRenderer();
  98. renderer.setPixelRatio( window.devicePixelRatio );
  99. renderer.setSize( window.innerWidth, window.innerHeight );
  100. container.appendChild( renderer.domElement );
  101. renderer.gammaInput = true;
  102. renderer.gammaOutput = true;
  103. //
  104. scene = new THREE.Scene();
  105. var aspect = window.innerWidth / window.innerHeight;
  106. camera = new THREE.OrthographicCamera( - height * aspect, height * aspect, height, - height, 1, 10000 );
  107. camera.position.z = 1500;
  108. scene.add( camera );
  109. controls = new THREE.OrbitControls( camera, renderer.domElement );
  110. controls.enableZoom = false;
  111. controls.enableDamping = true;
  112. // lights
  113. ambientLight = new THREE.AmbientLight( 0xffffff, settings.ambientIntensity );
  114. scene.add( ambientLight );
  115. pointLight = new THREE.PointLight( 0xff0000, 0.5 );
  116. pointLight.position.z = 2500;
  117. scene.add( pointLight );
  118. var pointLight2 = new THREE.PointLight( 0xff6666, 1 );
  119. camera.add( pointLight2 );
  120. var pointLight3 = new THREE.PointLight( 0x0000ff, 0.5 );
  121. pointLight3.position.x = - 1000;
  122. pointLight3.position.z = 1000;
  123. scene.add( pointLight3 );
  124. // env map
  125. var path = "textures/cube/SwedishRoyalCastle/";
  126. var format = '.jpg';
  127. var urls = [
  128. path + 'px' + format, path + 'nx' + format,
  129. path + 'py' + format, path + 'ny' + format,
  130. path + 'pz' + format, path + 'nz' + format
  131. ];
  132. var reflectionCube = new THREE.CubeTextureLoader().load( urls );
  133. // textures
  134. var textureLoader = new THREE.TextureLoader();
  135. var normalMap = textureLoader.load( "models/obj/ninja/normal.jpg" );
  136. var aoMap = textureLoader.load( "models/obj/ninja/ao.jpg" );
  137. var displacementMap = textureLoader.load( "models/obj/ninja/displacement.jpg" );
  138. // material
  139. material = new THREE.MeshStandardMaterial( {
  140. color: 0x888888,
  141. roughness: settings.roughness,
  142. metalness: settings.metalness,
  143. normalMap: normalMap,
  144. normalScale: new THREE.Vector2( 1, - 1 ), // why does the normal map require negation in this case?
  145. aoMap: aoMap,
  146. aoMapIntensity: 1,
  147. displacementMap: displacementMap,
  148. displacementScale: settings.displacementScale,
  149. displacementBias: - 0.428408, // from original model
  150. envMap: reflectionCube,
  151. envMapIntensity: settings.envMapIntensity,
  152. side: THREE.DoubleSide
  153. } );
  154. //
  155. var loader = new THREE.OBJLoader();
  156. loader.load( "models/obj/ninja/ninjaHead_Low.obj", function ( group ) {
  157. var geometry = group.children[ 0 ].geometry;
  158. geometry.attributes.uv2 = geometry.attributes.uv;
  159. geometry.center();
  160. mesh = new THREE.Mesh( geometry, material );
  161. mesh.scale.multiplyScalar( 25 );
  162. scene.add( mesh );
  163. } );
  164. //
  165. var description = "normal + ao" + ( renderer.capabilities.vertexTextures ? " + displacement + environment" : " + <strike>displacement</strike>" );
  166. document.getElementById( "description" ).innerHTML = description;
  167. document.getElementById( "vt" ).style.display = renderer.capabilities.vertexTextures ? "none" : "block";
  168. //
  169. stats = new Stats();
  170. container.appendChild( stats.dom );
  171. //
  172. window.addEventListener( 'resize', onWindowResize, false );
  173. }
  174. function onWindowResize() {
  175. var aspect = window.innerWidth / window.innerHeight;
  176. camera.left = - height * aspect;
  177. camera.right = height * aspect;
  178. camera.top = height;
  179. camera.bottom = - height;
  180. camera.updateProjectionMatrix();
  181. renderer.setSize( window.innerWidth, window.innerHeight );
  182. }
  183. //
  184. function animate() {
  185. requestAnimationFrame( animate );
  186. stats.begin();
  187. render();
  188. stats.end();
  189. }
  190. function render() {
  191. pointLight.position.x = 2500 * Math.cos( r );
  192. pointLight.position.z = 2500 * Math.sin( r );
  193. r += 0.01;
  194. renderer.render( scene, camera );
  195. }
  196. </script>
  197. </body>
  198. </html>