123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - postprocessing - sobel (edge detection)</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - webgl - postprocessing<br/>
- sobel (edge detection)
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { GUI } from './jsm/libs/dat.gui.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
- import { RenderPass } from './jsm/postprocessing/RenderPass.js';
- import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
- import { LuminosityShader } from './jsm/shaders/LuminosityShader.js';
- import { SobelOperatorShader } from './jsm/shaders/SobelOperatorShader.js';
- var camera, scene, renderer, composer;
- var effectSobel;
- var params = {
- enable: true
- };
- init();
- animate();
- function init() {
- //
- scene = new THREE.Scene();
- camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.set( 0, 10, 25 );
- camera.lookAt( scene.position );
- //
- var geometry = new THREE.TorusKnotBufferGeometry( 8, 3, 256, 32, 2, 3 );
- var material = new THREE.MeshPhongMaterial( { color: 0xffff00 } );
- var mesh = new THREE.Mesh( geometry, material );
- scene.add( mesh );
- //
- var ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
- scene.add( ambientLight );
- var pointLight = new THREE.PointLight( 0xffffff, 0.8 );
- camera.add( pointLight );
- scene.add( camera );
- //
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- // postprocessing
- composer = new EffectComposer( renderer );
- var renderPass = new RenderPass( scene, camera );
- composer.addPass( renderPass );
- // color to grayscale conversion
- var effectGrayScale = new ShaderPass( LuminosityShader );
- composer.addPass( effectGrayScale );
- // you might want to use a gaussian blur filter before
- // the next pass to improve the result of the Sobel operator
- // Sobel operator
- effectSobel = new ShaderPass( SobelOperatorShader );
- effectSobel.uniforms[ 'resolution' ].value.x = window.innerWidth * window.devicePixelRatio;
- effectSobel.uniforms[ 'resolution' ].value.y = window.innerHeight * window.devicePixelRatio;
- composer.addPass( effectSobel );
- var controls = new OrbitControls( camera, renderer.domElement );
- //
- var gui = new GUI();
- gui.add( params, 'enable' );
- gui.open();
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- composer.setSize( window.innerWidth, window.innerHeight );
- effectSobel.uniforms[ 'resolution' ].value.x = window.innerWidth * window.devicePixelRatio;
- effectSobel.uniforms[ 'resolution' ].value.y = window.innerHeight * window.devicePixelRatio;
- }
- function animate() {
- requestAnimationFrame( animate );
- if ( params.enable === true ) {
- composer.render();
- } else {
- renderer.render( scene, camera );
- }
- }
- </script>
- </body>
- </html>
|