webgpu_materials_displacementmap.html 6.2 KB

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