webgpu_cubemap_adjustments.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 WebGL from 'three/addons/capabilities/WebGL.js';
  32. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  33. import { RGBMLoader } from 'three/addons/loaders/RGBMLoader.js';
  34. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  35. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  36. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  37. let camera, scene, renderer;
  38. init();
  39. function init() {
  40. if ( WebGPU.isAvailable() === false && WebGL.isWebGL2Available() === false ) {
  41. document.body.appendChild( WebGPU.getErrorMessage() );
  42. throw new Error( 'No WebGPU or WebGL2 support' );
  43. }
  44. const container = document.createElement( 'div' );
  45. document.body.appendChild( container );
  46. const initialDistance = 2;
  47. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  48. camera.position.set( - 1.8 * initialDistance, 0.6 * initialDistance, 2.7 * initialDistance );
  49. scene = new THREE.Scene();
  50. // cube textures
  51. const rgbmUrls = [ 'px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png' ];
  52. const cube1Texture = new RGBMLoader()
  53. .setMaxRange( 16 )
  54. .setPath( './textures/cube/pisaRGBM16/' )
  55. .loadCubemap( rgbmUrls );
  56. cube1Texture.generateMipmaps = true;
  57. cube1Texture.minFilter = THREE.LinearMipmapLinearFilter;
  58. const cube2Urls = [ 'posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg' ];
  59. const cube2Texture = new THREE.CubeTextureLoader()
  60. .setPath( './textures/cube/Park2/' )
  61. .load( cube2Urls );
  62. cube2Texture.generateMipmaps = true;
  63. cube2Texture.minFilter = THREE.LinearMipmapLinearFilter;
  64. // nodes and environment
  65. const adjustments = {
  66. mix: 0,
  67. procedural: 0,
  68. intensity: 1,
  69. hue: 0,
  70. saturation: 1
  71. };
  72. const mixNode = reference( 'mix', 'float', adjustments );
  73. const proceduralNode = reference( 'procedural', 'float', adjustments );
  74. const intensityNode = reference( 'intensity', 'float', adjustments );
  75. const hueNode = reference( 'hue', 'float', adjustments );
  76. const saturationNode = reference( 'saturation', 'float', adjustments );
  77. const rotateY1Matrix = new THREE.Matrix4();
  78. const rotateY2Matrix = new THREE.Matrix4();
  79. const getEnvironmentNode = ( reflectNode, positionNode ) => {
  80. const custom1UV = reflectNode.xyz.mul( uniform( rotateY1Matrix ) );
  81. const custom2UV = reflectNode.xyz.mul( uniform( rotateY2Matrix ) );
  82. const mixCubeMaps = mix( cubeTexture( cube1Texture, custom1UV ), cubeTexture( cube2Texture, custom2UV ), positionNode.y.add( mixNode ).clamp() );
  83. const proceduralEnv = mix( mixCubeMaps, normalWorld, proceduralNode );
  84. const intensityFilter = proceduralEnv.mul( intensityNode );
  85. const hueFilter = intensityFilter.hue( hueNode );
  86. return hueFilter.saturation( saturationNode );
  87. };
  88. const blurNode = uniform( 0 );
  89. scene.environmentNode = getEnvironmentNode( reflectVector, positionWorld );
  90. scene.backgroundNode = getEnvironmentNode( positionWorldDirection, positionLocal ).context( {
  91. getSamplerLevelNode: () => blurNode
  92. } );
  93. // scene objects
  94. const loader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
  95. loader.load( 'DamagedHelmet.gltf', function ( gltf ) {
  96. scene.add( gltf.scene );
  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( { antialias: true } );
  107. renderer.setPixelRatio( window.devicePixelRatio );
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. renderer.toneMappingNode = toneMapping( THREE.LinearToneMapping, 1 );
  110. renderer.setAnimationLoop( render );
  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, 1, 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. }
  133. function onWindowResize() {
  134. camera.aspect = window.innerWidth / window.innerHeight;
  135. camera.updateProjectionMatrix();
  136. renderer.setSize( window.innerWidth, window.innerHeight );
  137. }
  138. //
  139. function render() {
  140. renderer.render( scene, camera );
  141. }
  142. </script>
  143. </body>
  144. </html>