2
0

webgl_materials_displacementmap.html 6.2 KB

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