1
0

webgpu_postprocessing_sobel.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Edge Detection with Sobel
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/webgpu": "../build/three.webgpu.js",
  18. "three/tsl": "../build/three.tsl.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { pass, renderOutput } from 'three/tsl';
  26. import { sobel } from 'three/addons/tsl/display/SobelOperatorNode.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  29. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  30. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  31. let camera, scene, renderer, controls;
  32. let postProcessing;
  33. const params = {
  34. enabled: true
  35. };
  36. init();
  37. async function init() {
  38. scene = new THREE.Scene();
  39. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 100 );
  40. camera.position.set( 0, 1, 3 );
  41. //
  42. const loader = new GLTFLoader();
  43. const gltf = await loader.loadAsync( 'models/gltf/DragonAttenuation.glb' );
  44. const model = gltf.scene.children[ 1 ];
  45. model.material = new THREE.MeshStandardNodeMaterial();
  46. scene.add( model );
  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.toneMapping = THREE.LinearToneMapping;
  53. document.body.appendChild( renderer.domElement );
  54. await renderer.init();
  55. const environment = new RoomEnvironment();
  56. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  57. scene.environment = pmremGenerator.fromScene( environment ).texture;
  58. pmremGenerator.dispose();
  59. //
  60. controls = new OrbitControls( camera, renderer.domElement );
  61. controls.enableDamping = true;
  62. controls.enableZoom = false;
  63. controls.target.set( 0, 0.5, 0 );
  64. controls.update();
  65. // postprocessing
  66. postProcessing = new THREE.PostProcessing( renderer );
  67. postProcessing.outputColorTransform = false;
  68. const scenePass = pass( scene, camera );
  69. postProcessing.outputNode = sobel( renderOutput( scenePass ) );
  70. //
  71. const gui = new GUI();
  72. gui.add( params, 'enabled' );
  73. gui.open();
  74. //
  75. window.addEventListener( 'resize', onWindowResize );
  76. }
  77. function onWindowResize() {
  78. camera.aspect = window.innerWidth / window.innerHeight;
  79. camera.updateProjectionMatrix();
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. }
  82. function animate() {
  83. controls.update();
  84. if ( params.enabled === true ) {
  85. postProcessing.render();
  86. } else {
  87. renderer.render( scene, camera );
  88. }
  89. }
  90. </script>
  91. </body>
  92. </html>
粤ICP备19079148号