webgl_postprocessing_advanced.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing</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> - webgl postprocessing example<br/>
  12. <a href="http://g3d.cs.williams.edu/g3d/data10/index.html#mesh19" target="_blank" rel="noopener">Lee Perry-Smith</a> head
  13. </div>
  14. <div id="container"></div>
  15. <script type="module">
  16. import * as THREE from '../build/three.module.js';
  17. import Stats from './jsm/libs/stats.module.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 { BloomPass } from './jsm/postprocessing/BloomPass.js';
  22. import { FilmPass } from './jsm/postprocessing/FilmPass.js';
  23. import { DotScreenPass } from './jsm/postprocessing/DotScreenPass.js';
  24. import { MaskPass, ClearMaskPass } from './jsm/postprocessing/MaskPass.js';
  25. import { TexturePass } from './jsm/postprocessing/TexturePass.js';
  26. import { BleachBypassShader } from './jsm/shaders/BleachBypassShader.js';
  27. import { ColorifyShader } from './jsm/shaders/ColorifyShader.js';
  28. import { HorizontalBlurShader } from './jsm/shaders/HorizontalBlurShader.js';
  29. import { VerticalBlurShader } from './jsm/shaders/VerticalBlurShader.js';
  30. import { SepiaShader } from './jsm/shaders/SepiaShader.js';
  31. import { VignetteShader } from './jsm/shaders/VignetteShader.js';
  32. import { GammaCorrectionShader } from './jsm/shaders/GammaCorrectionShader.js';
  33. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  34. var container, stats;
  35. var composerScene, composer1, composer2, composer3, composer4;
  36. var cameraOrtho, cameraPerspective, sceneModel, sceneBG, renderer, mesh, directionalLight;
  37. var width = window.innerWidth || 2;
  38. var height = window.innerHeight || 2;
  39. var halfWidth = width / 2;
  40. var halfHeight = height / 2;
  41. var quadBG, quadMask, renderScene;
  42. var delta = 0.01;
  43. init();
  44. animate();
  45. function init() {
  46. container = document.getElementById( 'container' );
  47. //
  48. cameraOrtho = new THREE.OrthographicCamera( - halfWidth, halfWidth, halfHeight, - halfHeight, - 10000, 10000 );
  49. cameraOrtho.position.z = 100;
  50. cameraPerspective = new THREE.PerspectiveCamera( 50, width / height, 1, 10000 );
  51. cameraPerspective.position.z = 900;
  52. //
  53. sceneModel = new THREE.Scene();
  54. sceneBG = new THREE.Scene();
  55. //
  56. directionalLight = new THREE.DirectionalLight( 0xffffff );
  57. directionalLight.position.set( 0, - 0.1, 1 ).normalize();
  58. sceneModel.add( directionalLight );
  59. var loader = new GLTFLoader();
  60. loader.load( "models/gltf/LeePerrySmith/LeePerrySmith.glb", function ( gltf ) {
  61. createMesh( gltf.scene.children[ 0 ].geometry, sceneModel, 100 );
  62. } );
  63. //
  64. var diffuseMap = new THREE.TextureLoader().load( "textures/cube/SwedishRoyalCastle/pz.jpg" );
  65. diffuseMap.encoding = THREE.sRGBEncoding;
  66. var materialColor = new THREE.MeshBasicMaterial( {
  67. map: diffuseMap,
  68. depthTest: false
  69. } );
  70. quadBG = new THREE.Mesh( new THREE.PlaneBufferGeometry( 1, 1 ), materialColor );
  71. quadBG.position.z = - 500;
  72. quadBG.scale.set( width, height, 1 );
  73. sceneBG.add( quadBG );
  74. //
  75. var sceneMask = new THREE.Scene();
  76. quadMask = new THREE.Mesh( new THREE.PlaneBufferGeometry( 1, 1 ), new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) );
  77. quadMask.position.z = - 300;
  78. quadMask.scale.set( width / 2, height / 2, 1 );
  79. sceneMask.add( quadMask );
  80. //
  81. renderer = new THREE.WebGLRenderer();
  82. renderer.setPixelRatio( window.devicePixelRatio );
  83. renderer.setSize( width, height );
  84. renderer.autoClear = false;
  85. //
  86. container.appendChild( renderer.domElement );
  87. //
  88. stats = new Stats();
  89. container.appendChild( stats.dom );
  90. //
  91. var shaderBleach = BleachBypassShader;
  92. var shaderSepia = SepiaShader;
  93. var shaderVignette = VignetteShader;
  94. // var shaderCopy = CopyShader;
  95. var effectBleach = new ShaderPass( shaderBleach );
  96. var effectSepia = new ShaderPass( shaderSepia );
  97. var effectVignette = new ShaderPass( shaderVignette );
  98. var gammaCorrection = new ShaderPass( GammaCorrectionShader );
  99. // var effectCopy = new ShaderPass( shaderCopy );
  100. effectBleach.uniforms[ "opacity" ].value = 0.95;
  101. effectSepia.uniforms[ "amount" ].value = 0.9;
  102. effectVignette.uniforms[ "offset" ].value = 0.95;
  103. effectVignette.uniforms[ "darkness" ].value = 1.6;
  104. var effectBloom = new BloomPass( 0.5 );
  105. var effectFilm = new FilmPass( 0.35, 0.025, 648, false );
  106. var effectFilmBW = new FilmPass( 0.35, 0.5, 2048, true );
  107. var effectDotScreen = new DotScreenPass( new THREE.Vector2( 0, 0 ), 0.5, 0.8 );
  108. var effectHBlur = new ShaderPass( HorizontalBlurShader );
  109. var effectVBlur = new ShaderPass( VerticalBlurShader );
  110. effectHBlur.uniforms[ 'h' ].value = 2 / ( width / 2 );
  111. effectVBlur.uniforms[ 'v' ].value = 2 / ( height / 2 );
  112. var effectColorify1 = new ShaderPass( ColorifyShader );
  113. var effectColorify2 = new ShaderPass( ColorifyShader );
  114. effectColorify1.uniforms[ 'color' ] = new THREE.Uniform( new THREE.Color( 1, 0.8, 0.8 ) );
  115. effectColorify2.uniforms[ 'color' ] = new THREE.Uniform( new THREE.Color( 1, 0.75, 0.5 ) );
  116. var clearMask = new ClearMaskPass();
  117. var renderMask = new MaskPass( sceneModel, cameraPerspective );
  118. var renderMaskInverse = new MaskPass( sceneModel, cameraPerspective );
  119. renderMaskInverse.inverse = true;
  120. //
  121. var rtParameters = {
  122. minFilter: THREE.LinearFilter,
  123. magFilter: THREE.LinearFilter,
  124. format: THREE.RGBFormat,
  125. stencilBuffer: true
  126. };
  127. var rtWidth = width / 2;
  128. var rtHeight = height / 2;
  129. //
  130. var renderBackground = new RenderPass( sceneBG, cameraOrtho );
  131. var renderModel = new RenderPass( sceneModel, cameraPerspective );
  132. renderModel.clear = false;
  133. composerScene = new EffectComposer( renderer, new THREE.WebGLRenderTarget( rtWidth * 2, rtHeight * 2, rtParameters ) );
  134. composerScene.addPass( renderBackground );
  135. composerScene.addPass( renderModel );
  136. composerScene.addPass( renderMaskInverse );
  137. composerScene.addPass( effectHBlur );
  138. composerScene.addPass( effectVBlur );
  139. composerScene.addPass( clearMask );
  140. //
  141. renderScene = new TexturePass( composerScene.renderTarget2.texture );
  142. //
  143. composer1 = new EffectComposer( renderer, new THREE.WebGLRenderTarget( rtWidth, rtHeight, rtParameters ) );
  144. composer1.addPass( renderScene );
  145. composer1.addPass( gammaCorrection );
  146. //composer1.addPass( renderMask );
  147. composer1.addPass( effectFilmBW );
  148. //composer1.addPass( clearMask );
  149. composer1.addPass( effectVignette );
  150. //
  151. composer2 = new EffectComposer( renderer, new THREE.WebGLRenderTarget( rtWidth, rtHeight, rtParameters ) );
  152. composer2.addPass( renderScene );
  153. composer2.addPass( gammaCorrection );
  154. composer2.addPass( effectDotScreen );
  155. composer2.addPass( renderMask );
  156. composer2.addPass( effectColorify1 );
  157. composer2.addPass( clearMask );
  158. composer2.addPass( renderMaskInverse );
  159. composer2.addPass( effectColorify2 );
  160. composer2.addPass( clearMask );
  161. composer2.addPass( effectVignette );
  162. //
  163. composer3 = new EffectComposer( renderer, new THREE.WebGLRenderTarget( rtWidth, rtHeight, rtParameters ) );
  164. composer3.addPass( renderScene );
  165. composer3.addPass( gammaCorrection );
  166. //composer3.addPass( renderMask );
  167. composer3.addPass( effectSepia );
  168. composer3.addPass( effectFilm );
  169. //composer3.addPass( clearMask );
  170. composer3.addPass( effectVignette );
  171. //
  172. composer4 = new EffectComposer( renderer, new THREE.WebGLRenderTarget( rtWidth, rtHeight, rtParameters ) );
  173. composer4.addPass( renderScene );
  174. composer4.addPass( gammaCorrection );
  175. //composer4.addPass( renderMask );
  176. composer4.addPass( effectBloom );
  177. composer4.addPass( effectFilm );
  178. composer4.addPass( effectBleach );
  179. //composer4.addPass( clearMask );
  180. composer4.addPass( effectVignette );
  181. renderScene.uniforms[ "tDiffuse" ].value = composerScene.renderTarget2.texture;
  182. window.addEventListener( 'resize', onWindowResize, false );
  183. }
  184. function onWindowResize() {
  185. halfWidth = window.innerWidth / 2;
  186. halfHeight = window.innerHeight / 2;
  187. cameraPerspective.aspect = window.innerWidth / window.innerHeight;
  188. cameraPerspective.updateProjectionMatrix();
  189. cameraOrtho.left = - halfWidth;
  190. cameraOrtho.right = halfWidth;
  191. cameraOrtho.top = halfHeight;
  192. cameraOrtho.bottom = - halfHeight;
  193. cameraOrtho.updateProjectionMatrix();
  194. renderer.setSize( window.innerWidth, window.innerHeight );
  195. composerScene.setSize( halfWidth * 2, halfHeight * 2 );
  196. composer1.setSize( halfWidth, halfHeight );
  197. composer2.setSize( halfWidth, halfHeight );
  198. composer3.setSize( halfWidth, halfHeight );
  199. composer4.setSize( halfWidth, halfHeight );
  200. renderScene.uniforms[ "tDiffuse" ].value = composerScene.renderTarget2.texture;
  201. quadBG.scale.set( window.innerWidth, window.innerHeight, 1 );
  202. quadMask.scale.set( window.innerWidth / 2, window.innerHeight / 2, 1 );
  203. }
  204. function createMesh( geometry, scene, scale ) {
  205. var diffuseMap = new THREE.TextureLoader().load( "models/gltf/LeePerrySmith/Map-COL.jpg" );
  206. diffuseMap.encoding = THREE.sRGBEncoding;
  207. var mat2 = new THREE.MeshPhongMaterial( {
  208. color: 0x999999,
  209. specular: 0x080808,
  210. shininess: 20,
  211. map: diffuseMap,
  212. normalMap: new THREE.TextureLoader().load( "models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg" ),
  213. normalScale: new THREE.Vector2( 0.75, 0.75 )
  214. } );
  215. mesh = new THREE.Mesh( geometry, mat2 );
  216. mesh.position.set( 0, - 50, 0 );
  217. mesh.scale.set( scale, scale, scale );
  218. scene.add( mesh );
  219. }
  220. //
  221. function animate() {
  222. requestAnimationFrame( animate );
  223. stats.begin();
  224. render();
  225. stats.end();
  226. }
  227. function render() {
  228. var time = Date.now() * 0.0004;
  229. if ( mesh ) mesh.rotation.y = - time;
  230. renderer.setViewport( 0, 0, halfWidth, halfHeight );
  231. composerScene.render( delta );
  232. renderer.setViewport( 0, 0, halfWidth, halfHeight );
  233. composer1.render( delta );
  234. renderer.setViewport( halfWidth, 0, halfWidth, halfHeight );
  235. composer2.render( delta );
  236. renderer.setViewport( 0, halfHeight, halfWidth, halfHeight );
  237. composer3.render( delta );
  238. renderer.setViewport( halfWidth, halfHeight, halfWidth, halfHeight );
  239. composer4.render( delta );
  240. }
  241. </script>
  242. </body>
  243. </html>