1
0

webgpu_postprocessing_sobel.html 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - postprocessing sobel</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, sobel } from 'three/tsl';
  22. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  23. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  24. let camera, scene, renderer;
  25. let postProcessing;
  26. const params = {
  27. enable: true
  28. };
  29. init();
  30. function init() {
  31. scene = new THREE.Scene();
  32. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 100 );
  33. camera.position.set( 0, 1, 3 );
  34. camera.lookAt( scene.position );
  35. //
  36. const ambientLight = new THREE.AmbientLight( 0xe7e7e7 );
  37. scene.add( ambientLight );
  38. const pointLight = new THREE.PointLight( 0xffffff, 20 );
  39. camera.add( pointLight );
  40. scene.add( camera );
  41. //
  42. const geometry = new THREE.TorusKnotGeometry( 1, 0.3, 256, 32 );
  43. const material = new THREE.MeshPhongMaterial( { color: 0xffff00 } );
  44. const mesh = new THREE.Mesh( geometry, material );
  45. scene.add( mesh );
  46. //
  47. renderer = new THREE.WebGPURenderer( { antialias: true } );
  48. renderer.setPixelRatio( window.devicePixelRatio );
  49. renderer.setSize( window.innerWidth, window.innerHeight );
  50. renderer.setAnimationLoop( animate );
  51. renderer.outputColorSpace = THREE.LinearSRGBColorSpace;
  52. document.body.appendChild( renderer.domElement );
  53. //
  54. const controls = new OrbitControls( camera, renderer.domElement );
  55. controls.enableZoom = false;
  56. // postprocessing
  57. postProcessing = new THREE.PostProcessing( renderer );
  58. const scenePass = pass( scene, camera );
  59. const scenePassColor = scenePass.getTextureNode();
  60. postProcessing.outputNode = sobel( scenePassColor );
  61. //
  62. const gui = new GUI();
  63. gui.add( params, 'enable' );
  64. gui.open();
  65. //
  66. window.addEventListener( 'resize', onWindowResize );
  67. }
  68. function onWindowResize() {
  69. camera.aspect = window.innerWidth / window.innerHeight;
  70. camera.updateProjectionMatrix();
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. }
  73. function animate() {
  74. if ( params.enable === true ) {
  75. postProcessing.render();
  76. } else {
  77. renderer.render( scene, camera );
  78. }
  79. }
  80. </script>
  81. </body>
  82. </html>