webgpu_postprocessing.html 3.0 KB

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