123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - post processing - Outline Pass</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">three.js</a> - Outline Pass by <a href="http://eduperiment.com" target="_blank" rel="noopener">Prashant Sharma</a> and <a href="https://clara.io" target="_blank" rel="noopener">Ben Houston</a><br/><br/>
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { GUI } from './jsm/libs/dat.gui.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { OBJLoader } from './jsm/loaders/OBJLoader.js';
- import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
- import { RenderPass } from './jsm/postprocessing/RenderPass.js';
- import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
- import { OutlinePass } from './jsm/postprocessing/OutlinePass.js';
- import { FXAAShader } from './jsm/shaders/FXAAShader.js';
- var container, stats;
- var camera, scene, renderer, controls;
- var raycaster = new THREE.Raycaster();
- var mouse = new THREE.Vector2();
- var selectedObjects = [];
- var composer, effectFXAA, outlinePass;
- var obj3d = new THREE.Object3D();
- var group = new THREE.Group();
- var params = {
- edgeStrength: 3.0,
- edgeGlow: 0.0,
- edgeThickness: 1.0,
- pulsePeriod: 0,
- rotate: false,
- usePatternTexture: false
- };
- // Init gui
- var gui = new GUI( { width: 300 } );
- gui.add( params, 'edgeStrength', 0.01, 10 ).onChange( function ( value ) {
- outlinePass.edgeStrength = Number( value );
- } );
- gui.add( params, 'edgeGlow', 0.0, 1 ).onChange( function ( value ) {
- outlinePass.edgeGlow = Number( value );
- } );
- gui.add( params, 'edgeThickness', 1, 4 ).onChange( function ( value ) {
- outlinePass.edgeThickness = Number( value );
- } );
- gui.add( params, 'pulsePeriod', 0.0, 5 ).onChange( function ( value ) {
- outlinePass.pulsePeriod = Number( value );
- } );
- gui.add( params, 'rotate' );
- gui.add( params, 'usePatternTexture' ).onChange( function ( value ) {
- outlinePass.usePatternTexture = value;
- } );
- var Configuration = function () {
- this.visibleEdgeColor = '#ffffff';
- this.hiddenEdgeColor = '#190a05';
- };
- var conf = new Configuration();
- gui.addColor( conf, 'visibleEdgeColor' ).onChange( function ( value ) {
- outlinePass.visibleEdgeColor.set( value );
- } );
- gui.addColor( conf, 'hiddenEdgeColor' ).onChange( function ( value ) {
- outlinePass.hiddenEdgeColor.set( value );
- } );
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- var width = window.innerWidth;
- var height = window.innerHeight;
- renderer = new THREE.WebGLRenderer();
- renderer.shadowMap.enabled = true;
- // todo - support pixelRatio in this demo
- renderer.setSize( width, height );
- document.body.appendChild( renderer.domElement );
- scene = new THREE.Scene();
- camera = new THREE.PerspectiveCamera( 45, width / height, 0.1, 100 );
- camera.position.set( 0, 0, 8 );
- controls = new OrbitControls( camera, renderer.domElement );
- controls.minDistance = 5;
- controls.maxDistance = 20;
- controls.enablePan = false;
- controls.enableDamping = true;
- controls.dampingFactor = 0.05;
- //
- scene.add( new THREE.AmbientLight( 0xaaaaaa, 0.2 ) );
- var light = new THREE.DirectionalLight( 0xddffdd, 0.6 );
- light.position.set( 1, 1, 1 );
- light.castShadow = true;
- light.shadow.mapSize.width = 1024;
- light.shadow.mapSize.height = 1024;
- var d = 10;
- light.shadow.camera.left = - d;
- light.shadow.camera.right = d;
- light.shadow.camera.top = d;
- light.shadow.camera.bottom = - d;
- light.shadow.camera.far = 1000;
- scene.add( light );
- // model
- var manager = new THREE.LoadingManager();
- manager.onProgress = function ( item, loaded, total ) {
- console.log( item, loaded, total );
- };
- var loader = new OBJLoader( manager );
- loader.load( 'models/obj/tree.obj', function ( object ) {
- var scale = 1.0;
- object.traverse( function ( child ) {
- if ( child instanceof THREE.Mesh ) {
- child.geometry.center();
- child.geometry.computeBoundingSphere();
- scale = 0.2 * child.geometry.boundingSphere.radius;
- var phongMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shininess: 5 } );
- child.material = phongMaterial;
- child.receiveShadow = true;
- child.castShadow = true;
- }
- } );
- object.position.y = 1;
- object.scale.divideScalar( scale );
- obj3d.add( object );
- } );
- scene.add( group );
- group.add( obj3d );
- //
- var geometry = new THREE.SphereBufferGeometry( 3, 48, 24 );
- for ( var i = 0; i < 20; i ++ ) {
- var material = new THREE.MeshLambertMaterial();
- material.color.setHSL( Math.random(), 1.0, 0.3 );
- var mesh = new THREE.Mesh( geometry, material );
- mesh.position.x = Math.random() * 4 - 2;
- mesh.position.y = Math.random() * 4 - 2;
- mesh.position.z = Math.random() * 4 - 2;
- mesh.receiveShadow = true;
- mesh.castShadow = true;
- mesh.scale.multiplyScalar( Math.random() * 0.3 + 0.1 );
- group.add( mesh );
- }
- var floorMaterial = new THREE.MeshLambertMaterial( { side: THREE.DoubleSide } );
- var floorGeometry = new THREE.PlaneBufferGeometry( 12, 12 );
- var floorMesh = new THREE.Mesh( floorGeometry, floorMaterial );
- floorMesh.rotation.x -= Math.PI * 0.5;
- floorMesh.position.y -= 1.5;
- group.add( floorMesh );
- floorMesh.receiveShadow = true;
- var geometry = new THREE.TorusBufferGeometry( 1, 0.3, 16, 100 );
- var material = new THREE.MeshPhongMaterial( { color: 0xffaaff } );
- var torus = new THREE.Mesh( geometry, material );
- torus.position.z = - 4;
- group.add( torus );
- torus.receiveShadow = true;
- torus.castShadow = true;
- //
- stats = new Stats();
- container.appendChild( stats.dom );
- // postprocessing
- composer = new EffectComposer( renderer );
- var renderPass = new RenderPass( scene, camera );
- composer.addPass( renderPass );
- outlinePass = new OutlinePass( new THREE.Vector2( window.innerWidth, window.innerHeight ), scene, camera );
- composer.addPass( outlinePass );
- var onLoad = function ( texture ) {
- outlinePass.patternTexture = texture;
- texture.wrapS = THREE.RepeatWrapping;
- texture.wrapT = THREE.RepeatWrapping;
- };
- var loader = new THREE.TextureLoader();
- loader.load( 'textures/tri_pattern.jpg', onLoad );
- effectFXAA = new ShaderPass( FXAAShader );
- effectFXAA.uniforms[ 'resolution' ].value.set( 1 / window.innerWidth, 1 / window.innerHeight );
- composer.addPass( effectFXAA );
- window.addEventListener( 'resize', onWindowResize, false );
- window.addEventListener( 'mousemove', onTouchMove );
- window.addEventListener( 'touchmove', onTouchMove );
- function onTouchMove( event ) {
- var x, y;
- if ( event.changedTouches ) {
- x = event.changedTouches[ 0 ].pageX;
- y = event.changedTouches[ 0 ].pageY;
- } else {
- x = event.clientX;
- y = event.clientY;
- }
- mouse.x = ( x / window.innerWidth ) * 2 - 1;
- mouse.y = - ( y / window.innerHeight ) * 2 + 1;
- checkIntersection();
- }
- function addSelectedObject( object ) {
- selectedObjects = [];
- selectedObjects.push( object );
- }
- function checkIntersection() {
- raycaster.setFromCamera( mouse, camera );
- var intersects = raycaster.intersectObjects( [ scene ], true );
- if ( intersects.length > 0 ) {
- var selectedObject = intersects[ 0 ].object;
- addSelectedObject( selectedObject );
- outlinePass.selectedObjects = selectedObjects;
- } else {
- // outlinePass.selectedObjects = [];
- }
- }
- }
- function onWindowResize() {
- var width = window.innerWidth;
- var height = window.innerHeight;
- camera.aspect = width / height;
- camera.updateProjectionMatrix();
- renderer.setSize( width, height );
- composer.setSize( width, height );
- effectFXAA.uniforms[ 'resolution' ].value.set( 1 / window.innerWidth, 1 / window.innerHeight );
- }
- function animate() {
- requestAnimationFrame( animate );
- stats.begin();
- var timer = performance.now();
- if ( params.rotate ) {
- group.rotation.y = timer * 0.0001;
- }
- controls.update();
- composer.render();
- stats.end();
- }
- </script>
- </body>
- </html>
|