webgpu_postprocessing_sobel.html 2.8 KB

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