webgl_materials_displacementmap.html 6.2 KB

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