You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
159 lines
4.1 KiB
159 lines
4.1 KiB
<!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>
|
|
|
|
<!-- Import maps polyfill -->
|
|
<!-- Remove this when import maps will be widely supported -->
|
|
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
|
|
|
|
<script type="importmap">
|
|
{
|
|
"imports": {
|
|
"three": "../build/three.module.js",
|
|
"three/addons/": "./jsm/"
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<script type="module">
|
|
|
|
import * as THREE from 'three';
|
|
|
|
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
|
|
|
|
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
|
|
|
|
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
|
|
import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
|
|
import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js';
|
|
|
|
import { LuminosityShader } from 'three/addons/shaders/LuminosityShader.js';
|
|
import { SobelOperatorShader } from 'three/addons/shaders/SobelOperatorShader.js';
|
|
|
|
let camera, scene, renderer, composer;
|
|
|
|
let effectSobel;
|
|
|
|
const params = {
|
|
enable: true
|
|
};
|
|
|
|
init();
|
|
animate();
|
|
|
|
function init() {
|
|
|
|
//
|
|
|
|
scene = new THREE.Scene();
|
|
|
|
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 200 );
|
|
camera.position.set( 0, 10, 25 );
|
|
camera.lookAt( scene.position );
|
|
|
|
//
|
|
|
|
const geometry = new THREE.TorusKnotGeometry( 8, 3, 256, 32, 2, 3 );
|
|
const material = new THREE.MeshPhongMaterial( { color: 0xffff00 } );
|
|
|
|
const mesh = new THREE.Mesh( geometry, material );
|
|
scene.add( mesh );
|
|
|
|
//
|
|
|
|
const ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
|
|
scene.add( ambientLight );
|
|
|
|
const 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 );
|
|
const renderPass = new RenderPass( scene, camera );
|
|
composer.addPass( renderPass );
|
|
|
|
// color to grayscale conversion
|
|
|
|
const 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 );
|
|
|
|
const controls = new OrbitControls( camera, renderer.domElement );
|
|
controls.minDistance = 10;
|
|
controls.maxDistance = 100;
|
|
|
|
//
|
|
|
|
const gui = new GUI();
|
|
|
|
gui.add( params, 'enable' );
|
|
gui.open();
|
|
|
|
//
|
|
|
|
window.addEventListener( 'resize', onWindowResize );
|
|
|
|
}
|
|
|
|
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>
|
|
|