1
0

webgpu_postprocessing.html 3.4 KB

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