webgpu_cubemap_adjustments.html 6.4 KB

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