webgl_materials_displacementmap.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. #vt { display:none }
  10. #vt, #vt a { color:orange; }
  11. </style>
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - (<span id="description">normal + ao + displacement + environment</span>) map demo.<br />
  16. 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>
  17. <div id="vt">displacement mapping requires vertex textures</div>
  18. </div>
  19. <script type="module">
  20. import * as THREE from '../build/three.module.js';
  21. import Stats from './jsm/libs/stats.module.js';
  22. import { GUI } from './jsm/libs/dat.gui.module.js';
  23. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  24. import { OBJLoader } from './jsm/loaders/OBJLoader.js';
  25. var stats;
  26. var camera, scene, renderer, controls;
  27. var settings = {
  28. metalness: 1.0,
  29. roughness: 0.4,
  30. ambientIntensity: 0.2,
  31. aoMapIntensity: 1.0,
  32. envMapIntensity: 1.0,
  33. displacementScale: 2.436143, // from original model
  34. normalScale: 1.0
  35. };
  36. var mesh, material;
  37. var pointLight, ambientLight;
  38. var height = 500; // of camera frustum
  39. var r = 0.0;
  40. init();
  41. animate();
  42. initGui();
  43. // Init gui
  44. function initGui() {
  45. var gui = new GUI();
  46. //var gui = gui.addFolder( "Material" );
  47. gui.add( settings, "metalness" ).min( 0 ).max( 1 ).onChange( function ( value ) {
  48. material.metalness = value;
  49. } );
  50. gui.add( settings, "roughness" ).min( 0 ).max( 1 ).onChange( function ( value ) {
  51. material.roughness = value;
  52. } );
  53. gui.add( settings, "aoMapIntensity" ).min( 0 ).max( 1 ).onChange( function ( value ) {
  54. material.aoMapIntensity = value;
  55. } );
  56. gui.add( settings, "ambientIntensity" ).min( 0 ).max( 1 ).onChange( function ( value ) {
  57. ambientLight.intensity = value;
  58. } );
  59. gui.add( settings, "envMapIntensity" ).min( 0 ).max( 3 ).onChange( function ( value ) {
  60. material.envMapIntensity = value;
  61. } );
  62. gui.add( settings, "displacementScale" ).min( 0 ).max( 3.0 ).onChange( function ( value ) {
  63. material.displacementScale = value;
  64. } );
  65. gui.add( settings, "normalScale" ).min( - 1 ).max( 1 ).onChange( function ( value ) {
  66. material.normalScale.set( 1, - 1 ).multiplyScalar( value );
  67. } );
  68. }
  69. function init() {
  70. var container = document.createElement( 'div' );
  71. document.body.appendChild( container );
  72. renderer = new THREE.WebGLRenderer();
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. container.appendChild( renderer.domElement );
  76. renderer.outputEncoding = THREE.sRGBEncoding;
  77. //
  78. scene = new THREE.Scene();
  79. var aspect = window.innerWidth / window.innerHeight;
  80. camera = new THREE.OrthographicCamera( - height * aspect, height * aspect, height, - height, 1, 10000 );
  81. camera.position.z = 1500;
  82. scene.add( camera );
  83. controls = new OrbitControls( camera, renderer.domElement );
  84. controls.enableZoom = false;
  85. controls.enableDamping = true;
  86. // lights
  87. ambientLight = new THREE.AmbientLight( 0xffffff, settings.ambientIntensity );
  88. scene.add( ambientLight );
  89. pointLight = new THREE.PointLight( 0xff0000, 0.5 );
  90. pointLight.position.z = 2500;
  91. scene.add( pointLight );
  92. var pointLight2 = new THREE.PointLight( 0xff6666, 1 );
  93. camera.add( pointLight2 );
  94. var pointLight3 = new THREE.PointLight( 0x0000ff, 0.5 );
  95. pointLight3.position.x = - 1000;
  96. pointLight3.position.z = 1000;
  97. scene.add( pointLight3 );
  98. // env map
  99. var path = "textures/cube/SwedishRoyalCastle/";
  100. var format = '.jpg';
  101. var urls = [
  102. path + 'px' + format, path + 'nx' + format,
  103. path + 'py' + format, path + 'ny' + format,
  104. path + 'pz' + format, path + 'nz' + format
  105. ];
  106. var reflectionCube = new THREE.CubeTextureLoader().load( urls );
  107. reflectionCube.encoding = THREE.sRGBEncoding;
  108. // textures
  109. var textureLoader = new THREE.TextureLoader();
  110. var normalMap = textureLoader.load( "models/obj/ninja/normal.jpg" );
  111. var aoMap = textureLoader.load( "models/obj/ninja/ao.jpg" );
  112. var displacementMap = textureLoader.load( "models/obj/ninja/displacement.jpg" );
  113. // material
  114. material = new THREE.MeshStandardMaterial( {
  115. color: 0x888888,
  116. roughness: settings.roughness,
  117. metalness: settings.metalness,
  118. normalMap: normalMap,
  119. normalScale: new THREE.Vector2( 1, - 1 ), // why does the normal map require negation in this case?
  120. aoMap: aoMap,
  121. aoMapIntensity: 1,
  122. displacementMap: displacementMap,
  123. displacementScale: settings.displacementScale,
  124. displacementBias: - 0.428408, // from original model
  125. envMap: reflectionCube,
  126. envMapIntensity: settings.envMapIntensity,
  127. side: THREE.DoubleSide
  128. } );
  129. //
  130. var loader = new OBJLoader();
  131. loader.load( "models/obj/ninja/ninjaHead_Low.obj", function ( group ) {
  132. var geometry = group.children[ 0 ].geometry;
  133. geometry.attributes.uv2 = geometry.attributes.uv;
  134. geometry.center();
  135. mesh = new THREE.Mesh( geometry, material );
  136. mesh.scale.multiplyScalar( 25 );
  137. scene.add( mesh );
  138. } );
  139. //
  140. var description = "normal + ao" + ( renderer.capabilities.vertexTextures ? " + displacement + environment" : " + <strike>displacement</strike>" );
  141. document.getElementById( "description" ).innerHTML = description;
  142. document.getElementById( "vt" ).style.display = renderer.capabilities.vertexTextures ? "none" : "block";
  143. //
  144. stats = new Stats();
  145. container.appendChild( stats.dom );
  146. //
  147. window.addEventListener( 'resize', onWindowResize, false );
  148. }
  149. function onWindowResize() {
  150. var aspect = window.innerWidth / window.innerHeight;
  151. camera.left = - height * aspect;
  152. camera.right = height * aspect;
  153. camera.top = height;
  154. camera.bottom = - height;
  155. camera.updateProjectionMatrix();
  156. renderer.setSize( window.innerWidth, window.innerHeight );
  157. }
  158. //
  159. function animate() {
  160. requestAnimationFrame( animate );
  161. controls.update();
  162. stats.begin();
  163. render();
  164. stats.end();
  165. }
  166. function render() {
  167. pointLight.position.x = 2500 * Math.cos( r );
  168. pointLight.position.z = 2500 * Math.sin( r );
  169. r += 0.01;
  170. renderer.render( scene, camera );
  171. }
  172. </script>
  173. </body>
  174. </html>