PageRenderTime 62ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/webgl_materials_displacementmap.html

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