webgpu_postprocessing_afterimage.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - postprocessing afterimage</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="example.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>After Image</span>
  14. </div>
  15. <small>
  16. Based on <a href="https://oosmoxiecode.com/archive/js_webgl/spiral/" target="_blank" rel="noopener">Particle Spiral</a>
  17. by <a href="https://github.com/oosmoxiecode" target="_blank" rel="noopener">oosmoxiecode</a>.
  18. </small>
  19. </div>
  20. <script type="importmap">
  21. {
  22. "imports": {
  23. "three": "../build/three.webgpu.js",
  24. "three/webgpu": "../build/three.webgpu.js",
  25. "three/tsl": "../build/three.tsl.js",
  26. "three/addons/": "./jsm/"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three/webgpu';
  32. import { instancedBufferAttribute, uniform, mod, pass, texture, float, time, vec2, vec3, vec4, sin, cos } from 'three/tsl';
  33. import { afterImage } from 'three/addons/tsl/display/AfterImageNode.js';
  34. import { Inspector } from 'three/addons/inspector/Inspector.js';
  35. let camera, scene, renderer, particles;
  36. let postProcessing, afterImagePass, scenePass;
  37. const params = {
  38. damp: uniform( 0.8, 'float' ).setName( 'damp' ),
  39. enabled: true
  40. };
  41. init();
  42. function init() {
  43. renderer = new THREE.WebGPURenderer();
  44. renderer.setPixelRatio( window.devicePixelRatio );
  45. renderer.setSize( window.innerWidth, window.innerHeight );
  46. renderer.setAnimationLoop( animate );
  47. renderer.inspector = new Inspector();
  48. document.body.appendChild( renderer.domElement );
  49. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
  50. camera.position.z = 1000;
  51. scene = new THREE.Scene();
  52. const sprite = new THREE.TextureLoader().load( 'textures/sprites/circle.png' );
  53. // geometry
  54. const radius = 600;
  55. const count = 50000;
  56. const vertex = new THREE.Vector3();
  57. const color = new THREE.Color();
  58. const colors = [];
  59. const vertices = [];
  60. const timeOffsets = [];
  61. for ( var i = 0; i < count; i ++ ) {
  62. getRandomPointOnSphere( radius, vertex );
  63. vertices.push( vertex.x, vertex.y, vertex.z );
  64. color.setHSL( i / count, 0.7, 0.7, THREE.SRGBColorSpace );
  65. colors.push( color.r, color.g, color.b );
  66. timeOffsets.push( i / count );
  67. }
  68. const positionAttribute = new THREE.InstancedBufferAttribute( new Float32Array( vertices ), 3 );
  69. const colorAttribute = new THREE.InstancedBufferAttribute( new Float32Array( colors ), 3 );
  70. const timeAttribute = new THREE.InstancedBufferAttribute( new Float32Array( timeOffsets ), 1 );
  71. // material and TSL
  72. const material = new THREE.SpriteNodeMaterial( { blending: THREE.AdditiveBlending, depthWrite: false } );
  73. const localTime = instancedBufferAttribute( timeAttribute ).add( time.mul( 0.1 ) );
  74. const modTime = mod( localTime, 1.0 );
  75. const accTime = modTime.mul( modTime );
  76. const angle = accTime.mul( 40.0 );
  77. const pulse = vec2( sin( angle ).mul( 20.0 ), cos( angle ).mul( 20.0 ) );
  78. const pos = instancedBufferAttribute( positionAttribute );
  79. const animated = vec3( pos.x.mul( accTime ).add( pulse.x ), pos.y.mul( accTime ).add( pulse.y ), pos.z.mul( accTime ).mul( 1.75 ) );
  80. const fAlpha = modTime.oneMinus().mul( 2.0 );
  81. material.colorNode = texture( sprite ).mul( vec4( instancedBufferAttribute( colorAttribute ), fAlpha ) );
  82. material.positionNode = animated;
  83. material.scaleNode = float( 2 );
  84. particles = new THREE.Sprite( material );
  85. particles.count = count;
  86. scene.add( particles );
  87. // postprocessing
  88. postProcessing = new THREE.PostProcessing( renderer );
  89. scenePass = pass( scene, camera );
  90. afterImagePass = afterImage( scenePass, params.damp );
  91. postProcessing.outputNode = afterImagePass;
  92. //
  93. const gui = renderer.inspector.createParameters( 'Settings' );
  94. gui.add( afterImagePass.damp, 'value', 0.25, 1 );
  95. gui.add( params, 'enabled' ).onChange( updatePassChain );
  96. window.addEventListener( 'resize', onWindowResize );
  97. }
  98. function updatePassChain() {
  99. if ( params.enabled === true ) {
  100. postProcessing.outputNode = afterImagePass;
  101. } else {
  102. postProcessing.outputNode = scenePass;
  103. }
  104. postProcessing.needsUpdate = true;
  105. }
  106. function getRandomPointOnSphere( r, v ) {
  107. const angle = Math.random() * Math.PI * 2;
  108. const u = Math.random() * 2 - 1;
  109. v.set(
  110. Math.cos( angle ) * Math.sqrt( 1 - Math.pow( u, 2 ) ) * r,
  111. Math.sin( angle ) * Math.sqrt( 1 - Math.pow( u, 2 ) ) * r,
  112. u * r
  113. );
  114. return v;
  115. }
  116. function onWindowResize() {
  117. camera.aspect = window.innerWidth / window.innerHeight;
  118. camera.updateProjectionMatrix();
  119. renderer.setSize( window.innerWidth, window.innerHeight );
  120. }
  121. function animate( time ) {
  122. particles.rotation.z = time * 0.001;
  123. postProcessing.render();
  124. }
  125. </script>
  126. </body>
  127. </html>
粤ICP备19079148号