postprocessing.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - postprocessing - webgl</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. color: #000;
  9. font-family:Monospace;
  10. font-size:13px;
  11. text-align:center;
  12. font-weight: bold;
  13. background-color: #fff;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. color:#000;
  19. position: absolute;
  20. top: 0px; width: 100%;
  21. padding: 5px;
  22. }
  23. #oldie {
  24. font-family:monospace;
  25. font-size:13px;
  26. text-align:center;
  27. background:#eee;
  28. color:#000;
  29. padding:1em;
  30. width:475px;
  31. margin:5em auto 0;
  32. display:none;
  33. }
  34. a { color: red; }
  35. </style>
  36. </head>
  37. <body>
  38. <div id="container"></div>
  39. <div id="info">
  40. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl postprocessing example -
  41. <a href="http://www.ir-ltd.net/infinite-3d-head-scan-released/" target="_blank">Lee Perry-Smith</a> head
  42. </div>
  43. <center>
  44. <div id="oldie">
  45. Sorry, your browser doesn't support <a href="http://khronos.org/webgl/wiki/Getting_a_WebGL_Implementation">WebGL</a>
  46. and <a href="http://www.whatwg.org/specs/web-workers/current-work/">Web Workers</a>.<br/>
  47. <br/>
  48. Please try in
  49. <a href="http://www.chromium.org/getting-involved/dev-channel">Chrome 9+</a> /
  50. <a href="http://www.mozilla.com/en-US/firefox/all-beta.html">Firefox 4+</a> /
  51. <a href="http://nightly.webkit.org/">Safari OSX 10.6+</a>
  52. </div>
  53. </center>
  54. <script type="text/javascript" src="js/Stats.js"></script>
  55. <script type="text/javascript" src="../build/ThreeExtras.js"></script>
  56. <!-- -----------------------------------------------------------------
  57. Film grain & scanlines shader
  58. - ported from HLSL to WebGL / GLSL
  59. http://www.truevision3d.com/forums/showcase/staticnoise_colorblackwhite_scanline_shaders-t18698.0.html
  60. // Screen Space Static Postprocessor
  61. //
  62. // Produces an analogue noise overlay similar to a film grain / TV static
  63. //
  64. // Original implementation and noise algorithm
  65. // Pat 'Hawthorne' Shearon
  66. //
  67. // Optimized scanlines + noise version with intensity scaling
  68. // Georg 'Leviathan' Steinrohder
  69. // This version is provided under a Creative Commons Attribution 3.0 License
  70. // http://creativecommons.org/licenses/by/3.0/
  71. ----------------------------------------------------------------- -->
  72. <!-- Film grain & scanlines fragment shader -->
  73. <script id="fs-film" type="x-shader/x-fragment">
  74. varying vec2 vUv;
  75. uniform sampler2D tDiffuse;
  76. // control parameter
  77. uniform float time;
  78. // noise effect intensity value (0 = no effect, 1 = full effect)
  79. const float fNintensity = 0.35;
  80. // scanlines effect intensity value (0 = no effect, 1 = full effect)
  81. const float fSintensity = 0.35;
  82. // scanlines effect count value (0 = no effect, 4096 = full effect)
  83. const float fScount = 4096.0;
  84. void main(void) {
  85. // sample the source
  86. vec4 cTextureScreen = texture2D( tDiffuse, vUv );
  87. // make some noise
  88. float x = vUv.x * vUv.y * time * 1000.0;
  89. x = mod( x, 13.0 ) * mod( x, 123.0 );
  90. float dx = mod( x, 0.01 );
  91. // add noise
  92. vec3 cResult = cTextureScreen.rgb + cTextureScreen.rgb * clamp( 0.1 + dx * 100.0, 0.0, 1.0 );
  93. // get us a sine and cosine
  94. vec2 sc = vec2( sin(vUv.y * fScount), cos(vUv.y * fScount) );
  95. // add scanlines
  96. cResult += cTextureScreen.rgb * vec3( sc.x, sc.y, sc.x ) * fSintensity;
  97. // interpolate between source and result by intensity
  98. cResult = cTextureScreen.rgb + clamp( fNintensity, 0.0,1.0 ) * ( cResult - cTextureScreen.rgb );
  99. // convert to grayscale if desired
  100. cResult = vec3( cResult.r * 0.3 + cResult.g * 0.59 + cResult.b * 0.11 );
  101. gl_FragColor = vec4( cResult, cTextureScreen.a );
  102. }
  103. </script>
  104. <!-- Render parameter modulated texture fragment shader -->
  105. <script id="fs-screen" type="x-shader/x-fragment">
  106. varying vec2 vUv;
  107. uniform sampler2D tDiffuse;
  108. uniform float opacity;
  109. void main(void)
  110. {
  111. vec4 texel = texture2D( tDiffuse, vUv );
  112. gl_FragColor = opacity * texel;
  113. }
  114. </script>
  115. <!-- Time modulated procedural color fragment shader -->
  116. <script id="fs-colors" type="x-shader/x-fragment">
  117. varying vec2 vUv;
  118. uniform float time;
  119. void main(void)
  120. {
  121. gl_FragColor = vec4( time, vUv.x, vUv.y, 1.0 );
  122. }
  123. </script>
  124. <!-- Generic vertex shader -->
  125. <script id="vs-generic" type="x-shader/x-vertex">
  126. varying vec2 vUv;
  127. void main()
  128. {
  129. vUv = vec2( uv.x, 1.0 - uv.y );
  130. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  131. }
  132. </script>
  133. <script type="text/javascript">
  134. if ( !is_browser_compatible() ) {
  135. document.getElementById( "oldie" ).style.display = "block";
  136. }
  137. var container, stats;
  138. var cameraOrtho, cameraPerspective, sceneModel, sceneScreen, sceneBG, renderer, mesh, directionalLight;
  139. var windowHalfX = window.innerWidth / 2;
  140. var windowHalfY = window.innerHeight / 2;
  141. var rtTexture, materialColor, materialScreen, materialFilm, materialConvolution, blurx, blury, quadBG, quadScreen;
  142. init();
  143. setInterval( loop, 1000 / 60 );
  144. function init() {
  145. container = document.getElementById( 'container' );
  146. cameraOrtho = new THREE.Camera();
  147. cameraOrtho.projectionMatrix = THREE.Matrix4.makeOrtho( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, -10000, 10000 );
  148. cameraOrtho.position.z = 100;
  149. cameraPerspective = new THREE.Camera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  150. cameraPerspective.position.z = 900;
  151. sceneModel = new THREE.Scene();
  152. sceneScreen = new THREE.Scene();
  153. sceneBG = new THREE.Scene();
  154. directionalLight = new THREE.DirectionalLight( 0xffffff );
  155. directionalLight.position.x = 0;
  156. directionalLight.position.y = 0;
  157. directionalLight.position.z = 1;
  158. directionalLight.position.normalize();
  159. sceneModel.addLight( directionalLight );
  160. rtTexture1 = new THREE.RenderTarget( window.innerWidth, window.innerHeight, { min_filter: THREE.LinearFilter, mag_filter: THREE.NearestFilter } );
  161. rtTexture2 = new THREE.RenderTarget( 256, 512, { min_filter: THREE.LinearFilter, mag_filter: THREE.LinearFilter } );
  162. rtTexture3 = new THREE.RenderTarget( 512, 256, { min_filter: THREE.LinearFilter, mag_filter: THREE.LinearFilter } );
  163. materialColor = new THREE.MeshShaderMaterial( {
  164. uniforms: { time: { type: "f", value: 0.0 } },
  165. vertex_shader: getText( 'vs-generic' ),
  166. fragment_shader: getText( 'fs-colors' )
  167. } );
  168. materialScreen = new THREE.MeshShaderMaterial( {
  169. uniforms: { tDiffuse: { type: "t", value: 0, texture: rtTexture1 },
  170. opacity: { type: "f", value: 0.4 }
  171. },
  172. vertex_shader: getText( 'vs-generic' ),
  173. fragment_shader: getText( 'fs-screen' ),
  174. blending: THREE.AdditiveBlending
  175. } );
  176. materialFilm = new THREE.MeshShaderMaterial( {
  177. uniforms: { tDiffuse: { type: "t", value: 0, texture: rtTexture1 },
  178. time: { type: "f", value: 0.0 }
  179. },
  180. vertex_shader: getText( 'vs-generic' ),
  181. fragment_shader: getText( 'fs-film' )
  182. } );
  183. var increment = 0.001953125;
  184. blurx = new THREE.Vector2( increment, 0.0 ),
  185. blury = new THREE.Vector2( 0.0, increment );
  186. var shader = ShaderUtils.lib["convolution"];
  187. var uniforms = Uniforms.clone( shader.uniforms );
  188. uniforms["tDiffuse"].texture = rtTexture1;
  189. uniforms["uImageIncrement"].value = blurx;
  190. uniforms["cKernel"].value = ShaderUtils.buildKernel( 4.0 );
  191. materialConvolution = new THREE.MeshShaderMaterial( {
  192. uniforms: uniforms,
  193. vertex_shader: shader.vertex_shader,
  194. fragment_shader: shader.fragment_shader
  195. } );
  196. var plane = new Plane( window.innerWidth, window.innerHeight );
  197. quadBG = new THREE.Mesh( plane, materialColor );
  198. quadBG.position.z = -500;
  199. sceneBG.addObject( quadBG );
  200. loader = new THREE.Loader( true );
  201. document.body.appendChild( loader.statusDomElement );
  202. loader.loadAscii( { model: "obj/leeperrysmith/LeePerrySmith.js", callback: function( geometry ) { createMesh( geometry, sceneModel, 100 ) } } );
  203. quadScreen = new THREE.Mesh( plane, materialConvolution );
  204. quadScreen.position.z = -100;
  205. sceneScreen.addObject( quadScreen );
  206. renderer = new THREE.WebGLRenderer();
  207. renderer.setSize( window.innerWidth, window.innerHeight );
  208. renderer.autoClear = false;
  209. container.appendChild( renderer.domElement );
  210. stats = new Stats();
  211. stats.domElement.style.position = 'absolute';
  212. stats.domElement.style.top = '0px';
  213. //container.appendChild( stats.domElement );
  214. }
  215. function getText( id ) {
  216. return document.getElementById( id ).textContent;
  217. }
  218. function createMesh( geometry, scene, scale ) {
  219. geometry.computeTangents();
  220. var ambient = 0x444444, diffuse = 0x888888, specular = 0x080810, shininess = 2;
  221. var shader = ShaderUtils.lib[ "normal" ];
  222. var uniforms = Uniforms.clone( shader.uniforms );
  223. uniforms[ "tNormal" ].texture = ImageUtils.loadTexture( "obj/leeperrysmith/Infinite-Level_02_Tangent_SmoothUV.jpg" );
  224. uniforms[ "uNormalScale" ].value = - 0.75;
  225. uniforms[ "tDiffuse" ].texture = ImageUtils.loadTexture( "obj/leeperrysmith/Map-COL.jpg" );
  226. uniforms[ "enableAO" ].value = false;
  227. uniforms[ "enableDiffuse" ].value = true;
  228. uniforms[ "uPointLightPos" ].value = new THREE.Vector3(0,0,0);
  229. uniforms[ "uPointLightColor" ].value = new THREE.Color(1,0,0);
  230. uniforms[ "uDirLightPos" ].value = directionalLight.position;
  231. uniforms[ "uDirLightColor" ].value = directionalLight.color;
  232. uniforms[ "uAmbientLightColor" ].value = new THREE.Color(0,0,0);
  233. uniforms[ "uDiffuseColor" ].value.setHex( diffuse );
  234. uniforms[ "uSpecularColor" ].value.setHex( specular );
  235. uniforms[ "uAmbientColor" ].value.setHex( ambient );
  236. uniforms[ "uShininess" ].value = shininess;
  237. var parameters = { fragment_shader: shader.fragment_shader, vertex_shader: shader.vertex_shader, uniforms: uniforms };
  238. var mat2 = new THREE.MeshShaderMaterial( parameters );
  239. mesh = new THREE.Mesh( geometry, mat2 );
  240. mesh.position.x = 0;
  241. mesh.position.y = -50;
  242. mesh.position.z = 0;
  243. mesh.scale.x = mesh.scale.y = mesh.scale.z = scale;
  244. mesh.updateMatrix();
  245. scene.addObject( mesh );
  246. loader.statusDomElement.style.display = "none";
  247. }
  248. var delta = 0.01;
  249. var start = 0;
  250. function loop() {
  251. if ( ! start ) start = new Date().getTime();
  252. var time = ( new Date().getTime() - start ) * 0.0004;
  253. if ( mesh ) {
  254. mesh.rotation.y = -time;
  255. }
  256. if ( materialColor.uniforms.time.value > 1 || materialColor.uniforms.time.value < 0 ) {
  257. delta *= -1;
  258. }
  259. materialColor.uniforms.time.value += delta/2;
  260. materialFilm.uniforms.time.value += delta;
  261. renderer.clear();
  262. // Render scene into texture
  263. // background
  264. renderer.context.disable( renderer.context.DEPTH_TEST );
  265. renderer.render( sceneBG, cameraOrtho, rtTexture1 );
  266. renderer.context.enable( renderer.context.DEPTH_TEST );
  267. // model
  268. renderer.render( sceneModel, cameraPerspective, rtTexture1 );
  269. // Render quad with blured scene into texture (convolution pass 1)
  270. quadScreen.materials = [ materialConvolution ];
  271. materialConvolution.uniforms.tDiffuse.texture = rtTexture1;
  272. materialConvolution.uniforms.uImageIncrement.value = blurx;
  273. renderer.render( sceneScreen, cameraOrtho, rtTexture2 );
  274. // Render quad with blured scene into texture (convolution pass 2)
  275. materialConvolution.uniforms.tDiffuse.texture = rtTexture2;
  276. materialConvolution.uniforms.uImageIncrement.value = blury;
  277. renderer.render( sceneScreen, cameraOrtho, rtTexture3 );
  278. // Render original scene with superimposed blur into texture
  279. quadScreen.materials = [ materialScreen ];
  280. materialScreen.uniforms.tDiffuse.texture = rtTexture3;
  281. materialScreen.uniforms.opacity.value = 1.0;
  282. renderer.render( sceneScreen, cameraOrtho, rtTexture1, false );
  283. // Render final scene to the screen with film shader
  284. materialFilm.uniforms.tDiffuse.texture = rtTexture1;
  285. quadScreen.materials = [ materialFilm ];
  286. renderer.render( sceneScreen, cameraOrtho );
  287. stats.update();
  288. }
  289. function is_browser_compatible() {
  290. // WebGL support
  291. try { var test = new Float32Array(1); } catch(e) { return false; }
  292. // Web workers
  293. return !!window.Worker;
  294. }
  295. </script>
  296. </body>
  297. </html>