webgpu_cubemap_adjustments.html 6.5 KB

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