webgpu_postprocessing_afterimage.html 5.6 KB

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