webgpu_postprocessing.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. <meta property="og:title" content="three.js webgpu - postprocessing">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_postprocessing.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_postprocessing.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>Post Processing</span>
  18. </div>
  19. <small>Mix of simple post processing effects.</small>
  20. </div>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.webgpu.js",
  25. "three/webgpu": "../build/three.webgpu.js",
  26. "three/tsl": "../build/three.tsl.js",
  27. "three/addons/": "./jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three/webgpu';
  33. import { pass } from 'three/tsl';
  34. import { dotScreen } from 'three/addons/tsl/display/DotScreenNode.js';
  35. import { rgbShift } from 'three/addons/tsl/display/RGBShiftNode.js';
  36. import { Inspector } from 'three/addons/inspector/Inspector.js';
  37. let camera, renderer, renderPipeline;
  38. let object;
  39. init();
  40. function init() {
  41. renderer = new THREE.WebGPURenderer();
  42. renderer.setPixelRatio( window.devicePixelRatio );
  43. renderer.setSize( window.innerWidth, window.innerHeight );
  44. renderer.setAnimationLoop( animate );
  45. renderer.inspector = new Inspector();
  46. document.body.appendChild( renderer.domElement );
  47. //
  48. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  49. camera.position.z = 400;
  50. const scene = new THREE.Scene();
  51. scene.background = new THREE.Color( 0x000000 );
  52. scene.fog = new THREE.Fog( 0x000000, 1, 1000 );
  53. object = new THREE.Object3D();
  54. scene.add( object );
  55. const geometry = new THREE.SphereGeometry( 1, 4, 4 );
  56. const material = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } );
  57. for ( let i = 0; i < 100; i ++ ) {
  58. const mesh = new THREE.Mesh( geometry, material );
  59. mesh.position.set( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 ).normalize();
  60. mesh.position.multiplyScalar( Math.random() * 400 );
  61. mesh.rotation.set( Math.random() * 2, Math.random() * 2, Math.random() * 2 );
  62. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 50;
  63. object.add( mesh );
  64. }
  65. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  66. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  67. light.position.set( 1, 1, 1 );
  68. scene.add( light );
  69. // postprocessing
  70. renderPipeline = new THREE.RenderPipeline( renderer );
  71. const scenePass = pass( scene, camera );
  72. const scenePassColor = scenePass.getTextureNode().toInspector( 'Scene Color' );
  73. const dotScreenPass = dotScreen( scenePassColor );
  74. dotScreenPass.scale.value = 0.3;
  75. const rgbShiftPass = rgbShift( dotScreenPass );
  76. rgbShiftPass.amount.value = 0.001;
  77. renderPipeline.outputNode = rgbShiftPass;
  78. //
  79. window.addEventListener( 'resize', onWindowResize );
  80. }
  81. function onWindowResize() {
  82. camera.aspect = window.innerWidth / window.innerHeight;
  83. camera.updateProjectionMatrix();
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. }
  86. function animate() {
  87. object.rotation.x += 0.005;
  88. object.rotation.y += 0.01;
  89. renderPipeline.render();
  90. }
  91. </script>
  92. </body>
  93. </html>
粤ICP备19079148号