2
0

webgl_materials_displacementmap.html 6.5 KB

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