webgl_postprocessing_sobel.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing - sobel (edge detection)</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 noreferrer">three.js</a> - webgl - postprocessing<br/>
  12. sobel (edge detection)
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { GUI } from './jsm/libs/dat.gui.module.js';
  17. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  18. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  19. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  20. import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
  21. import { LuminosityShader } from './jsm/shaders/LuminosityShader.js';
  22. import { SobelOperatorShader } from './jsm/shaders/SobelOperatorShader.js';
  23. var camera, scene, renderer, composer;
  24. var effectSobel;
  25. var params = {
  26. enable: true
  27. };
  28. init();
  29. animate();
  30. function init() {
  31. //
  32. scene = new THREE.Scene();
  33. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  34. camera.position.set( 0, 10, 25 );
  35. camera.lookAt( scene.position );
  36. //
  37. var geometry = new THREE.TorusKnotBufferGeometry( 8, 3, 256, 32, 2, 3 );
  38. var material = new THREE.MeshPhongMaterial( { color: 0xffff00 } );
  39. var mesh = new THREE.Mesh( geometry, material );
  40. scene.add( mesh );
  41. //
  42. var ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
  43. scene.add( ambientLight );
  44. var pointLight = new THREE.PointLight( 0xffffff, 0.8 );
  45. camera.add( pointLight );
  46. scene.add( camera );
  47. //
  48. renderer = new THREE.WebGLRenderer();
  49. renderer.setPixelRatio( window.devicePixelRatio );
  50. renderer.setSize( window.innerWidth, window.innerHeight );
  51. document.body.appendChild( renderer.domElement );
  52. // postprocessing
  53. composer = new EffectComposer( renderer );
  54. var renderPass = new RenderPass( scene, camera );
  55. composer.addPass( renderPass );
  56. // color to grayscale conversion
  57. var effectGrayScale = new ShaderPass( LuminosityShader );
  58. composer.addPass( effectGrayScale );
  59. // you might want to use a gaussian blur filter before
  60. // the next pass to improve the result of the Sobel operator
  61. // Sobel operator
  62. effectSobel = new ShaderPass( SobelOperatorShader );
  63. effectSobel.uniforms[ 'resolution' ].value.x = window.innerWidth * window.devicePixelRatio;
  64. effectSobel.uniforms[ 'resolution' ].value.y = window.innerHeight * window.devicePixelRatio;
  65. composer.addPass( effectSobel );
  66. var controls = new OrbitControls( camera, renderer.domElement );
  67. //
  68. var gui = new GUI();
  69. gui.add( params, 'enable' );
  70. gui.open();
  71. //
  72. window.addEventListener( 'resize', onWindowResize, false );
  73. }
  74. function onWindowResize() {
  75. camera.aspect = window.innerWidth / window.innerHeight;
  76. camera.updateProjectionMatrix();
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. composer.setSize( window.innerWidth, window.innerHeight );
  79. effectSobel.uniforms[ 'resolution' ].value.x = window.innerWidth * window.devicePixelRatio;
  80. effectSobel.uniforms[ 'resolution' ].value.y = window.innerHeight * window.devicePixelRatio;
  81. }
  82. function animate() {
  83. requestAnimationFrame( animate );
  84. if ( params.enable === true ) {
  85. composer.render();
  86. } else {
  87. renderer.render( scene, camera );
  88. }
  89. }
  90. </script>
  91. </body>
  92. </html>