webgpu_postprocessing_bloom.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - postprocessing - bloom</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. <style>
  9. #info > * {
  10. max-width: 650px;
  11. margin-left: auto;
  12. margin-right: auto;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div id="container"></div>
  18. <div id="info">
  19. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Bloom pass by <a href="http://eduperiment.com" target="_blank" rel="noopener">Prashant Sharma</a> and <a href="https://clara.io" target="_blank" rel="noopener">Ben Houston</a>
  20. <br/>
  21. Model: <a href="https://blog.sketchfab.com/art-spotlight-primary-ion-drive/" target="_blank" rel="noopener">Primary Ion Drive</a> by
  22. <a href="http://mjmurdock.com/" target="_blank" rel="noopener">Mike Murdock</a>, CC Attribution.
  23. </div>
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../build/three.webgpu.js",
  28. "three/webgpu": "../build/three.webgpu.js",
  29. "three/tsl": "../build/three.tsl.js",
  30. "three/addons/": "./jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three';
  36. import { pass } from 'three/tsl';
  37. import { bloom } from 'three/addons/tsl/display/BloomNode.js';
  38. import Stats from 'three/addons/libs/stats.module.js';
  39. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  40. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  41. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  42. let camera, stats;
  43. let postProcessing, renderer, mixer, clock;
  44. const params = {
  45. threshold: 0,
  46. strength: 1,
  47. radius: 0,
  48. exposure: 1
  49. };
  50. init();
  51. async function init() {
  52. const container = document.getElementById( 'container' );
  53. clock = new THREE.Clock();
  54. const scene = new THREE.Scene();
  55. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  56. camera.position.set( - 5, 2.5, - 3.5 );
  57. scene.add( camera );
  58. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  59. const pointLight = new THREE.PointLight( 0xffffff, 100 );
  60. camera.add( pointLight );
  61. const loader = new GLTFLoader();
  62. const gltf = await loader.loadAsync( 'models/gltf/PrimaryIonDrive.glb' );
  63. const model = gltf.scene;
  64. scene.add( model );
  65. mixer = new THREE.AnimationMixer( model );
  66. const clip = gltf.animations[ 0 ];
  67. mixer.clipAction( clip.optimize() ).play();
  68. //
  69. renderer = new THREE.WebGPURenderer( { antialias: true } );
  70. renderer.setPixelRatio( window.devicePixelRatio );
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. renderer.setAnimationLoop( animate );
  73. renderer.toneMapping = THREE.ReinhardToneMapping;
  74. container.appendChild( renderer.domElement );
  75. //
  76. postProcessing = new THREE.PostProcessing( renderer );
  77. const scenePass = pass( scene, camera );
  78. const scenePassColor = scenePass.getTextureNode( 'output' );
  79. const bloomPass = bloom( scenePassColor );
  80. postProcessing.outputNode = scenePassColor.add( bloomPass );
  81. //
  82. stats = new Stats();
  83. container.appendChild( stats.dom );
  84. //
  85. const controls = new OrbitControls( camera, renderer.domElement );
  86. controls.maxPolarAngle = Math.PI * 0.5;
  87. controls.minDistance = 3;
  88. controls.maxDistance = 8;
  89. //
  90. const gui = new GUI();
  91. const bloomFolder = gui.addFolder( 'bloom' );
  92. bloomFolder.add( params, 'threshold', 0.0, 1.0 ).onChange( function ( value ) {
  93. bloomPass.threshold.value = value;
  94. } );
  95. bloomFolder.add( params, 'strength', 0.0, 3.0 ).onChange( function ( value ) {
  96. bloomPass.strength.value = value;
  97. } );
  98. gui.add( params, 'radius', 0.0, 1.0 ).step( 0.01 ).onChange( function ( value ) {
  99. bloomPass.radius.value = value;
  100. } );
  101. const toneMappingFolder = gui.addFolder( 'tone mapping' );
  102. toneMappingFolder.add( params, 'exposure', 0.1, 2 ).onChange( function ( value ) {
  103. renderer.toneMappingExposure = Math.pow( value, 4.0 );
  104. } );
  105. window.addEventListener( 'resize', onWindowResize );
  106. }
  107. function onWindowResize() {
  108. const width = window.innerWidth;
  109. const height = window.innerHeight;
  110. camera.aspect = width / height;
  111. camera.updateProjectionMatrix();
  112. renderer.setSize( width, height );
  113. }
  114. function animate() {
  115. const delta = clock.getDelta();
  116. mixer.update( delta );
  117. postProcessing.render();
  118. stats.update();
  119. }
  120. </script>
  121. </body>
  122. </html>
粤ICP备19079148号