webgpu_materials_displacementmap.html 6.6 KB

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