webgpu_cubemap_adjustments.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - cubemap adjustments</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> - Env. Adjustments<br />
  12. Battle Damaged Sci-fi Helmet by
  13. <a href="https://sketchfab.com/theblueturtle_" target="_blank" rel="noopener">theblueturtle_</a><br />
  14. </div>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.webgpu.js",
  19. "three/tsl": "../build/three.webgpu.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { uniform, mix, pmremTexture, reference, positionLocal, hue, saturation, positionWorld, normalWorld, positionWorldDirection, reflectVector } from 'three/tsl';
  27. import { RGBMLoader } from 'three/addons/loaders/RGBMLoader.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  30. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  31. let camera, scene, renderer;
  32. init();
  33. async function init() {
  34. const container = document.createElement( 'div' );
  35. document.body.appendChild( container );
  36. const initialDistance = 2;
  37. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  38. camera.position.set( - 1.8 * initialDistance, 0.6 * initialDistance, 2.7 * initialDistance );
  39. scene = new THREE.Scene();
  40. // cube textures
  41. const rgbmUrls = [ 'px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png' ];
  42. const cube1Texture = await new RGBMLoader()
  43. .setMaxRange( 16 )
  44. .setPath( './textures/cube/pisaRGBM16/' )
  45. .loadCubemapAsync( rgbmUrls );
  46. cube1Texture.generateMipmaps = true;
  47. cube1Texture.minFilter = THREE.LinearMipmapLinearFilter;
  48. const cube2Urls = [ 'posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg' ];
  49. const cube2Texture = await new THREE.CubeTextureLoader()
  50. .setPath( './textures/cube/Park2/' )
  51. .loadAsync( cube2Urls );
  52. cube2Texture.generateMipmaps = true;
  53. cube2Texture.minFilter = THREE.LinearMipmapLinearFilter;
  54. // nodes and environment
  55. const adjustments = {
  56. mix: 0,
  57. procedural: 0,
  58. intensity: 1,
  59. hue: 0,
  60. saturation: 1
  61. };
  62. const mixNode = reference( 'mix', 'float', adjustments );
  63. const proceduralNode = reference( 'procedural', 'float', adjustments );
  64. const intensityNode = reference( 'intensity', 'float', adjustments );
  65. const hueNode = reference( 'hue', 'float', adjustments );
  66. const saturationNode = reference( 'saturation', 'float', adjustments );
  67. const rotateY1Matrix = new THREE.Matrix4();
  68. const rotateY2Matrix = new THREE.Matrix4();
  69. const getEnvironmentNode = ( reflectNode, positionNode ) => {
  70. const custom1UV = reflectNode.xyz.mul( uniform( rotateY1Matrix ) );
  71. const custom2UV = reflectNode.xyz.mul( uniform( rotateY2Matrix ) );
  72. const mixCubeMaps = mix( pmremTexture( cube1Texture, custom1UV ), pmremTexture( cube2Texture, custom2UV ), positionNode.y.add( mixNode ).clamp() );
  73. const proceduralEnv = mix( mixCubeMaps, normalWorld, proceduralNode );
  74. const intensityFilter = proceduralEnv.mul( intensityNode );
  75. const hueFilter = hue( intensityFilter, hueNode );
  76. return saturation( hueFilter, saturationNode );
  77. };
  78. const blurNode = uniform( 0 );
  79. scene.environmentNode = getEnvironmentNode( reflectVector, positionWorld );
  80. scene.backgroundNode = getEnvironmentNode( positionWorldDirection, positionLocal ).context( {
  81. getTextureLevel: () => blurNode
  82. } );
  83. // scene objects
  84. const loader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
  85. const gltf = await loader.loadAsync( 'DamagedHelmet.gltf' );
  86. scene.add( gltf.scene );
  87. const sphereGeometry = new THREE.SphereGeometry( .5, 64, 32 );
  88. const sphereRightView = new THREE.Mesh( sphereGeometry, new THREE.MeshStandardMaterial( { roughness: 0, metalness: 1 } ) );
  89. sphereRightView.position.x += 2;
  90. const sphereLeftView = new THREE.Mesh( sphereGeometry, new THREE.MeshStandardMaterial( { roughness: 1, metalness: 1 } ) );
  91. sphereLeftView.position.x -= 2;
  92. scene.add( sphereLeftView );
  93. scene.add( sphereRightView );
  94. // renderer and controls
  95. renderer = new THREE.WebGPURenderer( { antialias: true } );
  96. renderer.setPixelRatio( window.devicePixelRatio );
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. renderer.toneMapping = THREE.LinearToneMapping;
  99. renderer.setAnimationLoop( render );
  100. container.appendChild( renderer.domElement );
  101. const controls = new OrbitControls( camera, renderer.domElement );
  102. controls.minDistance = 2;
  103. controls.maxDistance = 10;
  104. window.addEventListener( 'resize', onWindowResize );
  105. // gui
  106. const gui = new GUI();
  107. gui.add( { blurBackground: blurNode.value }, 'blurBackground', 0, 1, 0.01 ).onChange( value => {
  108. blurNode.value = value;
  109. } );
  110. gui.add( { offsetCube1: 0 }, 'offsetCube1', 0, Math.PI * 2, 0.01 ).onChange( value => {
  111. rotateY1Matrix.makeRotationY( value );
  112. } );
  113. gui.add( { offsetCube2: 0 }, 'offsetCube2', 0, Math.PI * 2, 0.01 ).onChange( value => {
  114. rotateY2Matrix.makeRotationY( value );
  115. } );
  116. gui.add( adjustments, 'mix', - 1, 2, 0.01 );
  117. gui.add( adjustments, 'procedural', 0, 1, 0.01 );
  118. gui.add( adjustments, 'intensity', 0, 5, 0.01 );
  119. gui.add( adjustments, 'hue', 0, Math.PI * 2, 0.01 );
  120. gui.add( adjustments, 'saturation', 0, 2, 0.01 );
  121. }
  122. function onWindowResize() {
  123. camera.aspect = window.innerWidth / window.innerHeight;
  124. camera.updateProjectionMatrix();
  125. renderer.setSize( window.innerWidth, window.innerHeight );
  126. }
  127. //
  128. function render() {
  129. renderer.render( scene, camera );
  130. }
  131. </script>
  132. </body>
  133. </html>