webgpu_materials_displacementmap.html 6.3 KB

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