webgl_postprocessing_outline.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - post processing - Outline Pass</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> - 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/>
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import Stats from './jsm/libs/stats.module.js';
  16. import { GUI } from './jsm/libs/dat.gui.module.js';
  17. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  18. import { OBJLoader } from './jsm/loaders/OBJLoader.js';
  19. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  20. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  21. import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
  22. import { OutlinePass } from './jsm/postprocessing/OutlinePass.js';
  23. import { FXAAShader } from './jsm/shaders/FXAAShader.js';
  24. var container, stats;
  25. var camera, scene, renderer, controls;
  26. var raycaster = new THREE.Raycaster();
  27. var mouse = new THREE.Vector2();
  28. var selectedObjects = [];
  29. var composer, effectFXAA, outlinePass;
  30. var obj3d = new THREE.Object3D();
  31. var group = new THREE.Group();
  32. var params = {
  33. edgeStrength: 3.0,
  34. edgeGlow: 0.0,
  35. edgeThickness: 1.0,
  36. pulsePeriod: 0,
  37. rotate: false,
  38. usePatternTexture: false
  39. };
  40. // Init gui
  41. var gui = new GUI( { width: 300 } );
  42. gui.add( params, 'edgeStrength', 0.01, 10 ).onChange( function ( value ) {
  43. outlinePass.edgeStrength = Number( value );
  44. } );
  45. gui.add( params, 'edgeGlow', 0.0, 1 ).onChange( function ( value ) {
  46. outlinePass.edgeGlow = Number( value );
  47. } );
  48. gui.add( params, 'edgeThickness', 1, 4 ).onChange( function ( value ) {
  49. outlinePass.edgeThickness = Number( value );
  50. } );
  51. gui.add( params, 'pulsePeriod', 0.0, 5 ).onChange( function ( value ) {
  52. outlinePass.pulsePeriod = Number( value );
  53. } );
  54. gui.add( params, 'rotate' );
  55. gui.add( params, 'usePatternTexture' ).onChange( function ( value ) {
  56. outlinePass.usePatternTexture = value;
  57. } );
  58. var Configuration = function () {
  59. this.visibleEdgeColor = '#ffffff';
  60. this.hiddenEdgeColor = '#190a05';
  61. };
  62. var conf = new Configuration();
  63. gui.addColor( conf, 'visibleEdgeColor' ).onChange( function ( value ) {
  64. outlinePass.visibleEdgeColor.set( value );
  65. } );
  66. gui.addColor( conf, 'hiddenEdgeColor' ).onChange( function ( value ) {
  67. outlinePass.hiddenEdgeColor.set( value );
  68. } );
  69. init();
  70. animate();
  71. function init() {
  72. container = document.createElement( 'div' );
  73. document.body.appendChild( container );
  74. var width = window.innerWidth;
  75. var height = window.innerHeight;
  76. renderer = new THREE.WebGLRenderer();
  77. renderer.shadowMap.enabled = true;
  78. // todo - support pixelRatio in this demo
  79. renderer.setSize( width, height );
  80. document.body.appendChild( renderer.domElement );
  81. scene = new THREE.Scene();
  82. camera = new THREE.PerspectiveCamera( 45, width / height, 0.1, 100 );
  83. camera.position.set( 0, 0, 8 );
  84. controls = new OrbitControls( camera, renderer.domElement );
  85. controls.minDistance = 5;
  86. controls.maxDistance = 20;
  87. controls.enablePan = false;
  88. controls.enableDamping = true;
  89. controls.dampingFactor = 0.05;
  90. //
  91. scene.add( new THREE.AmbientLight( 0xaaaaaa, 0.2 ) );
  92. var light = new THREE.DirectionalLight( 0xddffdd, 0.6 );
  93. light.position.set( 1, 1, 1 );
  94. light.castShadow = true;
  95. light.shadow.mapSize.width = 1024;
  96. light.shadow.mapSize.height = 1024;
  97. var d = 10;
  98. light.shadow.camera.left = - d;
  99. light.shadow.camera.right = d;
  100. light.shadow.camera.top = d;
  101. light.shadow.camera.bottom = - d;
  102. light.shadow.camera.far = 1000;
  103. scene.add( light );
  104. // model
  105. var manager = new THREE.LoadingManager();
  106. manager.onProgress = function ( item, loaded, total ) {
  107. console.log( item, loaded, total );
  108. };
  109. var loader = new OBJLoader( manager );
  110. loader.load( 'models/obj/tree.obj', function ( object ) {
  111. var scale = 1.0;
  112. object.traverse( function ( child ) {
  113. if ( child instanceof THREE.Mesh ) {
  114. child.geometry.center();
  115. child.geometry.computeBoundingSphere();
  116. scale = 0.2 * child.geometry.boundingSphere.radius;
  117. var phongMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shininess: 5 } );
  118. child.material = phongMaterial;
  119. child.receiveShadow = true;
  120. child.castShadow = true;
  121. }
  122. } );
  123. object.position.y = 1;
  124. object.scale.divideScalar( scale );
  125. obj3d.add( object );
  126. } );
  127. scene.add( group );
  128. group.add( obj3d );
  129. //
  130. var geometry = new THREE.SphereBufferGeometry( 3, 48, 24 );
  131. for ( var i = 0; i < 20; i ++ ) {
  132. var material = new THREE.MeshLambertMaterial();
  133. material.color.setHSL( Math.random(), 1.0, 0.3 );
  134. var mesh = new THREE.Mesh( geometry, material );
  135. mesh.position.x = Math.random() * 4 - 2;
  136. mesh.position.y = Math.random() * 4 - 2;
  137. mesh.position.z = Math.random() * 4 - 2;
  138. mesh.receiveShadow = true;
  139. mesh.castShadow = true;
  140. mesh.scale.multiplyScalar( Math.random() * 0.3 + 0.1 );
  141. group.add( mesh );
  142. }
  143. var floorMaterial = new THREE.MeshLambertMaterial( { side: THREE.DoubleSide } );
  144. var floorGeometry = new THREE.PlaneBufferGeometry( 12, 12 );
  145. var floorMesh = new THREE.Mesh( floorGeometry, floorMaterial );
  146. floorMesh.rotation.x -= Math.PI * 0.5;
  147. floorMesh.position.y -= 1.5;
  148. group.add( floorMesh );
  149. floorMesh.receiveShadow = true;
  150. var geometry = new THREE.TorusBufferGeometry( 1, 0.3, 16, 100 );
  151. var material = new THREE.MeshPhongMaterial( { color: 0xffaaff } );
  152. var torus = new THREE.Mesh( geometry, material );
  153. torus.position.z = - 4;
  154. group.add( torus );
  155. torus.receiveShadow = true;
  156. torus.castShadow = true;
  157. //
  158. stats = new Stats();
  159. container.appendChild( stats.dom );
  160. // postprocessing
  161. composer = new EffectComposer( renderer );
  162. var renderPass = new RenderPass( scene, camera );
  163. composer.addPass( renderPass );
  164. outlinePass = new OutlinePass( new THREE.Vector2( window.innerWidth, window.innerHeight ), scene, camera );
  165. composer.addPass( outlinePass );
  166. var onLoad = function ( texture ) {
  167. outlinePass.patternTexture = texture;
  168. texture.wrapS = THREE.RepeatWrapping;
  169. texture.wrapT = THREE.RepeatWrapping;
  170. };
  171. var loader = new THREE.TextureLoader();
  172. loader.load( 'textures/tri_pattern.jpg', onLoad );
  173. effectFXAA = new ShaderPass( FXAAShader );
  174. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / window.innerWidth, 1 / window.innerHeight );
  175. composer.addPass( effectFXAA );
  176. window.addEventListener( 'resize', onWindowResize, false );
  177. window.addEventListener( 'mousemove', onTouchMove );
  178. window.addEventListener( 'touchmove', onTouchMove );
  179. function onTouchMove( event ) {
  180. var x, y;
  181. if ( event.changedTouches ) {
  182. x = event.changedTouches[ 0 ].pageX;
  183. y = event.changedTouches[ 0 ].pageY;
  184. } else {
  185. x = event.clientX;
  186. y = event.clientY;
  187. }
  188. mouse.x = ( x / window.innerWidth ) * 2 - 1;
  189. mouse.y = - ( y / window.innerHeight ) * 2 + 1;
  190. checkIntersection();
  191. }
  192. function addSelectedObject( object ) {
  193. selectedObjects = [];
  194. selectedObjects.push( object );
  195. }
  196. function checkIntersection() {
  197. raycaster.setFromCamera( mouse, camera );
  198. var intersects = raycaster.intersectObjects( [ scene ], true );
  199. if ( intersects.length > 0 ) {
  200. var selectedObject = intersects[ 0 ].object;
  201. addSelectedObject( selectedObject );
  202. outlinePass.selectedObjects = selectedObjects;
  203. } else {
  204. // outlinePass.selectedObjects = [];
  205. }
  206. }
  207. }
  208. function onWindowResize() {
  209. var width = window.innerWidth;
  210. var height = window.innerHeight;
  211. camera.aspect = width / height;
  212. camera.updateProjectionMatrix();
  213. renderer.setSize( width, height );
  214. composer.setSize( width, height );
  215. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / window.innerWidth, 1 / window.innerHeight );
  216. }
  217. function animate() {
  218. requestAnimationFrame( animate );
  219. stats.begin();
  220. var timer = performance.now();
  221. if ( params.rotate ) {
  222. group.rotation.y = timer * 0.0001;
  223. }
  224. controls.update();
  225. composer.render();
  226. stats.end();
  227. }
  228. </script>
  229. </body>
  230. </html>