2
0

webgpu_postprocessing.html 2.8 KB

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