webgpu_postprocessing.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - postprocessing</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. <script type="importmap">
  11. {
  12. "imports": {
  13. "three": "../build/three.webgpu.js",
  14. "three/tsl": "../build/three.webgpu.js",
  15. "three/addons/": "./jsm/"
  16. }
  17. }
  18. </script>
  19. <script type="module">
  20. import * as THREE from 'three';
  21. import { pass } from 'three/tsl';
  22. import { dotScreen } from 'three/addons/tsl/display/DotScreenNode.js';
  23. import { rgbShift } from 'three/addons/tsl/display/RGBShiftNode.js';
  24. let camera, renderer, postProcessing;
  25. let object;
  26. init();
  27. function init() {
  28. renderer = new THREE.WebGPURenderer();
  29. renderer.setPixelRatio( window.devicePixelRatio );
  30. renderer.setSize( window.innerWidth, window.innerHeight );
  31. renderer.setAnimationLoop( animate );
  32. document.body.appendChild( renderer.domElement );
  33. //
  34. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  35. camera.position.z = 400;
  36. const scene = new THREE.Scene();
  37. scene.fog = new THREE.Fog( 0x000000, 1, 1000 );
  38. object = new THREE.Object3D();
  39. scene.add( object );
  40. const geometry = new THREE.SphereGeometry( 1, 4, 4 );
  41. const material = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } );
  42. for ( let i = 0; i < 100; i ++ ) {
  43. const mesh = new THREE.Mesh( geometry, material );
  44. mesh.position.set( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 ).normalize();
  45. mesh.position.multiplyScalar( Math.random() * 400 );
  46. mesh.rotation.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
  47. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 50;
  48. object.add( mesh );
  49. }
  50. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  51. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  52. light.position.set( 1, 1, 1 );
  53. scene.add( light );
  54. // postprocessing
  55. postProcessing = new THREE.PostProcessing( renderer );
  56. const scenePass = pass( scene, camera );
  57. const scenePassColor = scenePass.getTextureNode();
  58. const dotScreenPass = dotScreen( scenePassColor );
  59. dotScreenPass.scale.value = 0.3;
  60. const rgbShiftPass = rgbShift( dotScreenPass );
  61. rgbShiftPass.amount.value = 0.001;
  62. postProcessing.outputNode = rgbShiftPass;
  63. //
  64. window.addEventListener( 'resize', onWindowResize );
  65. }
  66. function onWindowResize() {
  67. camera.aspect = window.innerWidth / window.innerHeight;
  68. camera.updateProjectionMatrix();
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. }
  71. function animate() {
  72. object.rotation.x += 0.005;
  73. object.rotation.y += 0.01;
  74. postProcessing.render();
  75. }
  76. </script>
  77. </body>
  78. </html>