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