webgpu_cubemap_adjustments.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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-nodes/": "./jsm/nodes/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import * as Nodes from 'three-nodes/Nodes.js';
  29. import { uniform, mix, cubeTexture, mul, reference, add, positionWorld, normalWorld, modelWorldMatrix, transformDirection, saturate, saturation, hue, reflectVector, context } from 'three-nodes/Nodes.js';
  30. import WebGPU from './jsm/capabilities/WebGPU.js';
  31. import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
  32. import { RGBMLoader } from './jsm/loaders/RGBMLoader.js';
  33. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  34. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  35. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  36. let camera, scene, renderer;
  37. init().then( render ).catch( error );
  38. async 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 ) => {
  79. const custom1UV = mul( reflectNode.xyz, uniform( rotateY1Matrix ) );
  80. const custom2UV = mul( reflectNode.xyz, uniform( rotateY2Matrix ) );
  81. const mixCubeMaps = mix( cubeTexture( cube1Texture, custom1UV ), cubeTexture( cube2Texture, custom2UV ), saturate( add( positionWorld.y, mixNode ) ) );
  82. const proceduralEnv = mix( mixCubeMaps, normalWorld, proceduralNode );
  83. const intensityFilter = mul( proceduralEnv, intensityNode );
  84. const hueFilter = hue( intensityFilter, hueNode );
  85. return saturation( hueFilter, saturationNode );
  86. }
  87. const blurNode = uniform( 0 );
  88. scene.environmentNode = getEnvironmentNode( reflectVector );
  89. scene.backgroundNode = context( getEnvironmentNode( transformDirection( positionWorld, modelWorldMatrix ) ), {
  90. levelNode : blurNode // @TODO: currently it uses mipmaps value, I think it should be replaced for [0,1]
  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. render();
  97. } );
  98. const sphereGeometry = new THREE.SphereGeometry( .5, 64, 32 );
  99. const sphereRightView = new THREE.Mesh( sphereGeometry, new THREE.MeshStandardMaterial( { roughness: 0, metalness: 1 } ) );
  100. sphereRightView.position.x += 2;
  101. const sphereLeftView = new THREE.Mesh( sphereGeometry, new THREE.MeshStandardMaterial( { roughness: 1, metalness: 1 } ) );
  102. sphereLeftView.position.x -= 2;
  103. scene.add( sphereLeftView );
  104. scene.add( sphereRightView );
  105. // renderer and controls
  106. renderer = new WebGPURenderer();
  107. renderer.setPixelRatio( window.devicePixelRatio );
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. renderer.toneMappingNode = new Nodes.ToneMappingNode( THREE.LinearToneMapping, 1 );
  110. renderer.outputEncoding = THREE.sRGBEncoding;
  111. container.appendChild( renderer.domElement );
  112. const controls = new OrbitControls( camera, renderer.domElement );
  113. controls.minDistance = 2;
  114. controls.maxDistance = 10;
  115. window.addEventListener( 'resize', onWindowResize );
  116. // gui
  117. const gui = new GUI();
  118. gui.add( { blurBackground: blurNode.value }, 'blurBackground', 0, 10, 0.01 ).onChange( value => {
  119. blurNode.value = value;
  120. } );
  121. gui.add( { offsetCube1: 0 }, 'offsetCube1', 0, Math.PI * 2, 0.01 ).onChange( value => {
  122. rotateY1Matrix.makeRotationY( value );
  123. } );
  124. gui.add( { offsetCube2: 0 }, 'offsetCube2', 0, Math.PI * 2, 0.01 ).onChange( value => {
  125. rotateY2Matrix.makeRotationY( value );
  126. } );
  127. gui.add( adjustments, 'mix', - 1, 2, 0.01 );
  128. gui.add( adjustments, 'procedural', 0, 1, 0.01 );
  129. gui.add( adjustments, 'intensity', 0, 5, 0.01 );
  130. gui.add( adjustments, 'hue', 0, Math.PI * 2, 0.01 );
  131. gui.add( adjustments, 'saturation', 0, 2, 0.01 );
  132. return renderer.init();
  133. }
  134. function onWindowResize() {
  135. camera.aspect = window.innerWidth / window.innerHeight;
  136. camera.updateProjectionMatrix();
  137. renderer.setSize( window.innerWidth, window.innerHeight );
  138. }
  139. //
  140. function render() {
  141. requestAnimationFrame( render );
  142. renderer.render( scene, camera );
  143. }
  144. function error( error ) {
  145. console.error( error );
  146. }
  147. </script>
  148. </body>
  149. </html>