webgpu_hdr.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
  6. <title>three.js webgpu - HDR Draw</title>
  7. <link type="text/css" rel="stylesheet" href="main.css" />
  8. <style>
  9. #no-hdr {
  10. position: absolute;
  11. font-family: monospace;
  12. font-size: 11px;
  13. font-weight: normal;
  14. text-align: center;
  15. background: #000;
  16. color: #fff;
  17. left: 50%;
  18. transform: translateX(-50%);
  19. padding: 1.5em;
  20. max-width: 600px;
  21. margin: 5em auto 0;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="info" style="color: #000">
  27. <a href="https://threejs.org" target="_blank" rel="noopener">threejs</a> - HDR Draw
  28. </div>
  29. <div id="no-hdr" style="display: none">
  30. <div>
  31. The browser says your device or monitor doesn't support HDR.<br />
  32. If you're on a laptop using an external monitor, try the built in
  33. monitor<br />
  34. or, try this site on your phone. Most phones support HDR.
  35. </div>
  36. </div>
  37. <script type="importmap">
  38. {
  39. "imports": {
  40. "three": "../build/three.webgpu.js",
  41. "three/webgpu": "../build/three.webgpu.js",
  42. "three/tsl": "../build/three.tsl.js",
  43. "three/addons/": "./jsm/"
  44. }
  45. }
  46. </script>
  47. <script type="module">
  48. import * as THREE from 'three/webgpu';
  49. import { pass, uv, uniform } from 'three/tsl';
  50. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  51. import { afterImage } from 'three/addons/tsl/display/AfterImageNode.js';
  52. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  53. import { ExtendedSRGBColorSpace, ExtendedSRGBColorSpaceImpl } from 'three/addons/math/ColorSpaces.js';
  54. const params = {
  55. intensity: uniform( 4.0, 'float' ).setName( 'intensity' ),
  56. hardness: uniform( 0.4, 'float' ).setName( 'hardness' ),
  57. radius: uniform( 0.5, 'float' ).setName( 'radius' ),
  58. afterImageDecay: uniform( 0.985, 'float' ).setName( 'afterImageDecay' ),
  59. };
  60. const hdrMediaQuery = window.matchMedia( '(dynamic-range: high)' );
  61. function updateHDRWarning() {
  62. const displayIsHDR = hdrMediaQuery.matches;
  63. document.querySelector( '#no-hdr' ).style.display = displayIsHDR ? 'none' : '';
  64. }
  65. hdrMediaQuery.addEventListener( 'change', updateHDRWarning );
  66. updateHDRWarning();
  67. if ( WebGPU.isAvailable() === false ) {
  68. document.body.appendChild( WebGPU.getErrorMessage() );
  69. throw new Error( 'No WebGPU support' );
  70. }
  71. // Enable Extended sRGB output color space for HDR presentation
  72. THREE.ColorManagement.define( { [ ExtendedSRGBColorSpace ]: ExtendedSRGBColorSpaceImpl } );
  73. // Renderer (HalfFloat output + Extended sRGB)
  74. const renderer = new THREE.WebGPURenderer( {
  75. antialias: true,
  76. outputType: THREE.HalfFloatType,
  77. } );
  78. renderer.outputColorSpace = ExtendedSRGBColorSpace;
  79. renderer.setPixelRatio( window.devicePixelRatio );
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. document.body.appendChild( renderer.domElement );
  82. const camera = new THREE.OrthographicCamera( 0, window.innerWidth, window.innerHeight, 0, 1, 2 );
  83. camera.position.z = 1;
  84. // Brush scene (rendered into drawTarget)
  85. const brushScene = new THREE.Scene();
  86. brushScene.background = new THREE.Color( 0xffffff );
  87. const brushMat = new THREE.MeshBasicNodeMaterial();
  88. brushMat.transparent = true;
  89. brushMat.depthTest = false;
  90. brushMat.depthWrite = false;
  91. brushMat.blending = THREE.AdditiveBlending; // additive to build HDR energy
  92. const postProcessing = new THREE.PostProcessing( renderer );
  93. const brushPass = pass( brushScene, camera, { type: THREE.HalfFloatType } );
  94. brushPass.renderTarget.texture.colorSpace = ExtendedSRGBColorSpace;
  95. postProcessing.outputNode = afterImage( brushPass, params.afterImageDecay );
  96. // HDR brush uniforms
  97. const uColor = params.intensity;
  98. const uHard = params.hardness;
  99. const uRadius = params.radius;
  100. // Radial falloff in TSL
  101. const d = uv().sub( 0.5 ).length();
  102. const t = d.div( uRadius );
  103. const a = t.clamp().oneMinus().pow( uHard.mul( 8.0 ).add( 1.0 ) );
  104. brushMat.colorNode = uColor.mul( a );
  105. brushMat.opacityNode = a; // premultiplied style with additive blending
  106. const brushMesh = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), brushMat );
  107. brushMesh.scale.set( 300, 300, 1 ); // ~300px default brush size
  108. brushScene.add( brushMesh );
  109. function onPointerMove( e ) {
  110. const rect = renderer.domElement.getBoundingClientRect();
  111. const x = e.clientX - rect.left;
  112. const y = e.clientY - rect.top;
  113. // camera has origin at bottom-left (0,0)
  114. brushMesh.position.set( x, window.innerHeight - y, 0 );
  115. }
  116. window.addEventListener( 'pointermove', onPointerMove, { passive: false } );
  117. // Prevent mobile scroll on touch
  118. renderer.domElement.addEventListener( 'touchstart', ( e ) => e.preventDefault(), { passive: false } );
  119. renderer.domElement.addEventListener( 'touchmove', ( e ) => e.preventDefault(), { passive: false } );
  120. renderer.domElement.addEventListener( 'touchend', ( e ) => e.preventDefault(), { passive: false } );
  121. // GUI setup
  122. const gui = new GUI();
  123. const colorFolder = gui.addFolder( 'HDR' );
  124. colorFolder.add( params.intensity, 'value', 0, 10, 0.1 ).name( 'Intensity' );
  125. colorFolder.open();
  126. const brushFolder = gui.addFolder( 'Brush Settings' );
  127. brushFolder.add( params.hardness, 'value', 0, 0.99, 0.01 ).name( 'Hardness' );
  128. brushFolder.add( params.radius, 'value', 0.1, 2.0, 0.01 ).name( 'Radius' );
  129. brushFolder.open();
  130. const effectFolder = gui.addFolder( 'Effects' );
  131. effectFolder
  132. .add( params.afterImageDecay, 'value', 0.9, 0.999, 0.001 )
  133. .name( 'After Image Decay' );
  134. effectFolder.open();
  135. gui.open();
  136. // Resize handling
  137. function onResize() {
  138. renderer.setSize( window.innerWidth, window.innerHeight );
  139. camera.right = window.innerWidth;
  140. camera.top = window.innerHeight;
  141. camera.updateProjectionMatrix();
  142. }
  143. window.addEventListener( 'resize', onResize );
  144. // Main loop
  145. renderer.setAnimationLoop( async () => {
  146. postProcessing.render();
  147. } );
  148. </script>
  149. </body>
  150. </html>
粤ICP备19079148号