ShaderExtras.js 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author zz85 / http://www.lab4games.net/zz85/blog
  4. *
  5. * ShaderExtras currently contains:
  6. *
  7. * screen
  8. * convolution
  9. * film
  10. * bokeh
  11. * sepia
  12. * dotscreen
  13. * vignette
  14. * bleachbypass
  15. * basic
  16. * dofmipmap
  17. * focus
  18. * triangleBlur
  19. * horizontalBlur
  20. * verticalBlur
  21. */
  22. THREE.ShaderExtras = {
  23. /* -------------------------------------------------------------------------
  24. // Full-screen textured quad shader
  25. ------------------------------------------------------------------------- */
  26. 'screen': {
  27. uniforms: {
  28. tDiffuse: { type: "t", value: 0, texture: null },
  29. opacity: { type: "f", value: 1.0 }
  30. },
  31. vertexShader: [
  32. "varying vec2 vUv;",
  33. "void main() {",
  34. "vUv = vec2( uv.x, 1.0 - uv.y );",
  35. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  36. "}"
  37. ].join("\n"),
  38. fragmentShader: [
  39. "uniform float opacity;",
  40. "uniform sampler2D tDiffuse;",
  41. "varying vec2 vUv;",
  42. "void main() {",
  43. "vec4 texel = texture2D( tDiffuse, vUv );",
  44. "gl_FragColor = opacity * texel;",
  45. "}"
  46. ].join("\n")
  47. },
  48. /* ------------------------------------------------------------------------
  49. // Convolution shader
  50. // - ported from o3d sample to WebGL / GLSL
  51. // http://o3d.googlecode.com/svn/trunk/samples/convolution.html
  52. ------------------------------------------------------------------------ */
  53. 'convolution': {
  54. uniforms: {
  55. "tDiffuse" : { type: "t", value: 0, texture: null },
  56. "uImageIncrement" : { type: "v2", value: new THREE.Vector2( 0.001953125, 0.0 ) },
  57. "cKernel" : { type: "fv1", value: [] }
  58. },
  59. vertexShader: [
  60. //"#define KERNEL_SIZE 25.0",
  61. "uniform vec2 uImageIncrement;",
  62. "varying vec2 vUv;",
  63. "void main() {",
  64. "vUv = uv - ( ( KERNEL_SIZE - 1.0 ) / 2.0 ) * uImageIncrement;",
  65. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  66. "}"
  67. ].join("\n"),
  68. fragmentShader: [
  69. //"#define KERNEL_SIZE 25",
  70. "uniform float cKernel[ KERNEL_SIZE ];",
  71. "uniform sampler2D tDiffuse;",
  72. "uniform vec2 uImageIncrement;",
  73. "varying vec2 vUv;",
  74. "void main() {",
  75. "vec2 imageCoord = vUv;",
  76. "vec4 sum = vec4( 0.0, 0.0, 0.0, 0.0 );",
  77. "for( int i = 0; i < KERNEL_SIZE; i ++ ) {",
  78. "sum += texture2D( tDiffuse, imageCoord ) * cKernel[ i ];",
  79. "imageCoord += uImageIncrement;",
  80. "}",
  81. "gl_FragColor = sum;",
  82. "}"
  83. ].join("\n")
  84. },
  85. /* -------------------------------------------------------------------------
  86. // Film grain & scanlines shader
  87. // - ported from HLSL to WebGL / GLSL
  88. // http://www.truevision3d.com/forums/showcase/staticnoise_colorblackwhite_scanline_shaders-t18698.0.html
  89. // Screen Space Static Postprocessor
  90. //
  91. // Produces an analogue noise overlay similar to a film grain / TV static
  92. //
  93. // Original implementation and noise algorithm
  94. // Pat 'Hawthorne' Shearon
  95. //
  96. // Optimized scanlines + noise version with intensity scaling
  97. // Georg 'Leviathan' Steinrohder
  98. // This version is provided under a Creative Commons Attribution 3.0 License
  99. // http://creativecommons.org/licenses/by/3.0/
  100. ------------------------------------------------------------------------- */
  101. 'film': {
  102. uniforms: {
  103. tDiffuse: { type: "t", value: 0, texture: null },
  104. time: { type: "f", value: 0.0 },
  105. nIntensity: { type: "f", value: 0.5 },
  106. sIntensity: { type: "f", value: 0.05 },
  107. sCount: { type: "f", value: 4096 },
  108. grayscale: { type: "i", value: 1 }
  109. },
  110. vertexShader: [
  111. "varying vec2 vUv;",
  112. "void main() {",
  113. "vUv = vec2( uv.x, 1.0 - uv.y );",
  114. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  115. "}"
  116. ].join("\n"),
  117. fragmentShader: [
  118. // control parameter
  119. "uniform float time;",
  120. "uniform bool grayscale;",
  121. // noise effect intensity value (0 = no effect, 1 = full effect)
  122. "uniform float nIntensity;",
  123. // scanlines effect intensity value (0 = no effect, 1 = full effect)
  124. "uniform float sIntensity;",
  125. // scanlines effect count value (0 = no effect, 4096 = full effect)
  126. "uniform float sCount;",
  127. "uniform sampler2D tDiffuse;",
  128. "varying vec2 vUv;",
  129. "void main() {",
  130. // sample the source
  131. "vec4 cTextureScreen = texture2D( tDiffuse, vUv );",
  132. // make some noise
  133. "float x = vUv.x * vUv.y * time * 1000.0;",
  134. "x = mod( x, 13.0 ) * mod( x, 123.0 );",
  135. "float dx = mod( x, 0.01 );",
  136. // add noise
  137. "vec3 cResult = cTextureScreen.rgb + cTextureScreen.rgb * clamp( 0.1 + dx * 100.0, 0.0, 1.0 );",
  138. // get us a sine and cosine
  139. "vec2 sc = vec2( sin( vUv.y * sCount ), cos( vUv.y * sCount ) );",
  140. // add scanlines
  141. "cResult += cTextureScreen.rgb * vec3( sc.x, sc.y, sc.x ) * sIntensity;",
  142. // interpolate between source and result by intensity
  143. "cResult = cTextureScreen.rgb + clamp( nIntensity, 0.0,1.0 ) * ( cResult - cTextureScreen.rgb );",
  144. // convert to grayscale if desired
  145. "if( grayscale ) {",
  146. "cResult = vec3( cResult.r * 0.3 + cResult.g * 0.59 + cResult.b * 0.11 );",
  147. "}",
  148. "gl_FragColor = vec4( cResult, cTextureScreen.a );",
  149. "}"
  150. ].join("\n")
  151. },
  152. /* -------------------------------------------------------------------------
  153. // Depth-of-field shader with bokeh
  154. // ported from GLSL shader by Martins Upitis
  155. // http://artmartinsh.blogspot.com/2010/02/glsl-lens-blur-filter-with-bokeh.html
  156. ------------------------------------------------------------------------- */
  157. 'bokeh' : {
  158. uniforms: { tColor: { type: "t", value: 0, texture: null },
  159. tDepth: { type: "t", value: 1, texture: null },
  160. focus: { type: "f", value: 1.0 },
  161. aspect: { type: "f", value: 1.0 },
  162. aperture: { type: "f", value: 0.025 },
  163. maxblur: { type: "f", value: 1.0 },
  164. },
  165. vertexShader: [
  166. "varying vec2 vUv;",
  167. "void main() {",
  168. "vUv = vec2( uv.x, 1.0 - uv.y );",
  169. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  170. "}"
  171. ].join("\n"),
  172. fragmentShader: [
  173. "varying vec2 vUv;",
  174. "uniform sampler2D tColor;",
  175. "uniform sampler2D tDepth;",
  176. "uniform float maxblur;", // max blur amount
  177. "uniform float aperture;", // aperture - bigger values for shallower depth of field
  178. "uniform float focus;",
  179. "uniform float aspect;",
  180. "void main() {",
  181. "vec2 aspectcorrect = vec2( 1.0, aspect );",
  182. "vec4 depth1 = texture2D( tDepth, vUv );",
  183. "float factor = depth1.x - focus;",
  184. "vec2 dofblur = vec2 ( clamp( factor * aperture, -maxblur, maxblur ) );",
  185. "vec2 dofblur9 = dofblur * 0.9;",
  186. "vec2 dofblur7 = dofblur * 0.7;",
  187. "vec2 dofblur4 = dofblur * 0.4;",
  188. "vec4 col = vec4( 0.0 );",
  189. "col += texture2D( tColor, vUv.xy );",
  190. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, 0.4 ) * aspectcorrect ) * dofblur );",
  191. "col += texture2D( tColor, vUv.xy + ( vec2( 0.15, 0.37 ) * aspectcorrect ) * dofblur );",
  192. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, 0.29 ) * aspectcorrect ) * dofblur );",
  193. "col += texture2D( tColor, vUv.xy + ( vec2( -0.37, 0.15 ) * aspectcorrect ) * dofblur );",
  194. "col += texture2D( tColor, vUv.xy + ( vec2( 0.40, 0.0 ) * aspectcorrect ) * dofblur );",
  195. "col += texture2D( tColor, vUv.xy + ( vec2( 0.37, -0.15 ) * aspectcorrect ) * dofblur );",
  196. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, -0.29 ) * aspectcorrect ) * dofblur );",
  197. "col += texture2D( tColor, vUv.xy + ( vec2( -0.15, -0.37 ) * aspectcorrect ) * dofblur );",
  198. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, -0.4 ) * aspectcorrect ) * dofblur );",
  199. "col += texture2D( tColor, vUv.xy + ( vec2( -0.15, 0.37 ) * aspectcorrect ) * dofblur );",
  200. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, 0.29 ) * aspectcorrect ) * dofblur );",
  201. "col += texture2D( tColor, vUv.xy + ( vec2( 0.37, 0.15 ) * aspectcorrect ) * dofblur );",
  202. "col += texture2D( tColor, vUv.xy + ( vec2( -0.4, 0.0 ) * aspectcorrect ) * dofblur );",
  203. "col += texture2D( tColor, vUv.xy + ( vec2( -0.37, -0.15 ) * aspectcorrect ) * dofblur );",
  204. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, -0.29 ) * aspectcorrect ) * dofblur );",
  205. "col += texture2D( tColor, vUv.xy + ( vec2( 0.15, -0.37 ) * aspectcorrect ) * dofblur );",
  206. "col += texture2D( tColor, vUv.xy + ( vec2( 0.15, 0.37 ) * aspectcorrect ) * dofblur9 );",
  207. "col += texture2D( tColor, vUv.xy + ( vec2( -0.37, 0.15 ) * aspectcorrect ) * dofblur9 );",
  208. "col += texture2D( tColor, vUv.xy + ( vec2( 0.37, -0.15 ) * aspectcorrect ) * dofblur9 );",
  209. "col += texture2D( tColor, vUv.xy + ( vec2( -0.15, -0.37 ) * aspectcorrect ) * dofblur9 );",
  210. "col += texture2D( tColor, vUv.xy + ( vec2( -0.15, 0.37 ) * aspectcorrect ) * dofblur9 );",
  211. "col += texture2D( tColor, vUv.xy + ( vec2( 0.37, 0.15 ) * aspectcorrect ) * dofblur9 );",
  212. "col += texture2D( tColor, vUv.xy + ( vec2( -0.37, -0.15 ) * aspectcorrect ) * dofblur9 );",
  213. "col += texture2D( tColor, vUv.xy + ( vec2( 0.15, -0.37 ) * aspectcorrect ) * dofblur9 );",
  214. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, 0.29 ) * aspectcorrect ) * dofblur7 );",
  215. "col += texture2D( tColor, vUv.xy + ( vec2( 0.40, 0.0 ) * aspectcorrect ) * dofblur7 );",
  216. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, -0.29 ) * aspectcorrect ) * dofblur7 );",
  217. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, -0.4 ) * aspectcorrect ) * dofblur7 );",
  218. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, 0.29 ) * aspectcorrect ) * dofblur7 );",
  219. "col += texture2D( tColor, vUv.xy + ( vec2( -0.4, 0.0 ) * aspectcorrect ) * dofblur7 );",
  220. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, -0.29 ) * aspectcorrect ) * dofblur7 );",
  221. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, 0.4 ) * aspectcorrect ) * dofblur7 );",
  222. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, 0.29 ) * aspectcorrect ) * dofblur4 );",
  223. "col += texture2D( tColor, vUv.xy + ( vec2( 0.4, 0.0 ) * aspectcorrect ) * dofblur4 );",
  224. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, -0.29 ) * aspectcorrect ) * dofblur4 );",
  225. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, -0.4 ) * aspectcorrect ) * dofblur4 );",
  226. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, 0.29 ) * aspectcorrect ) * dofblur4 );",
  227. "col += texture2D( tColor, vUv.xy + ( vec2( -0.4, 0.0 ) * aspectcorrect ) * dofblur4 );",
  228. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, -0.29 ) * aspectcorrect ) * dofblur4 );",
  229. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, 0.4 ) * aspectcorrect ) * dofblur4 );",
  230. "gl_FragColor = col / 41.0;",
  231. "gl_FragColor.a = 1.0;",
  232. "}"
  233. ].join("\n")
  234. },
  235. /* -------------------------------------------------------------------------
  236. // Depth-of-field shader using mipmaps
  237. // - from Matt Handley @applmak
  238. // - requires power-of-2 sized render target with enabled mipmaps
  239. ------------------------------------------------------------------------- */
  240. 'dofmipmap': {
  241. uniforms: {
  242. tColor: { type: "t", value: 0, texture: null },
  243. tDepth: { type: "t", value: 1, texture: null },
  244. focus: { type: "f", value: 1.0 },
  245. maxblur: { type: "f", value: 1.0 }
  246. },
  247. vertexShader: [
  248. "varying vec2 vUv;",
  249. "void main() {",
  250. "vUv = vec2( uv.x, 1.0 - uv.y );",
  251. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  252. "}"
  253. ].join("\n"),
  254. fragmentShader: [
  255. "uniform float focus;",
  256. "uniform float maxblur;",
  257. "uniform sampler2D tColor;",
  258. "uniform sampler2D tDepth;",
  259. "varying vec2 vUv;",
  260. "void main() {",
  261. "vec4 depth = texture2D( tDepth, vUv );",
  262. "float factor = depth.x - focus;",
  263. "vec4 col = texture2D( tColor, vUv, 2.0 * maxblur * abs( focus - depth.x ) );",
  264. "gl_FragColor = col;",
  265. "gl_FragColor.a = 1.0;",
  266. "}"
  267. ].join("\n")
  268. },
  269. /* -------------------------------------------------------------------------
  270. // Sepia tone shader
  271. // - based on glfx.js sepia shader
  272. // https://github.com/evanw/glfx.js
  273. ------------------------------------------------------------------------- */
  274. 'sepia': {
  275. uniforms: {
  276. tDiffuse: { type: "t", value: 0, texture: null },
  277. amount: { type: "f", value: 1.0 }
  278. },
  279. vertexShader: [
  280. "varying vec2 vUv;",
  281. "void main() {",
  282. "vUv = vec2( uv.x, 1.0 - uv.y );",
  283. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  284. "}"
  285. ].join("\n"),
  286. fragmentShader: [
  287. "uniform float amount;",
  288. "uniform sampler2D tDiffuse;",
  289. "varying vec2 vUv;",
  290. "void main() {",
  291. "vec4 color = texture2D( tDiffuse, vUv );",
  292. "vec3 c = color.rgb;",
  293. "color.r = dot( c, vec3( 1.0 - 0.607 * amount, 0.769 * amount, 0.189 * amount ) );",
  294. "color.g = dot( c, vec3( 0.349 * amount, 1.0 - 0.314 * amount, 0.168 * amount ) );",
  295. "color.b = dot( c, vec3( 0.272 * amount, 0.534 * amount, 1.0 - 0.869 * amount ) );",
  296. "gl_FragColor = vec4( min( vec3( 1.0 ), color.rgb ), color.a );",
  297. "}"
  298. ].join("\n")
  299. },
  300. /* -------------------------------------------------------------------------
  301. // Dot screen shader
  302. // - based on glfx.js sepia shader
  303. // https://github.com/evanw/glfx.js
  304. ------------------------------------------------------------------------- */
  305. 'dotscreen': {
  306. uniforms: {
  307. tDiffuse: { type: "t", value: 0, texture: null },
  308. tSize: { type: "v2", value: new THREE.Vector2( 256, 256 ) },
  309. center: { type: "v2", value: new THREE.Vector2( 0.5, 0.5 ) },
  310. angle: { type: "f", value: 1.57 },
  311. scale: { type: "f", value: 1.0 }
  312. },
  313. vertexShader: [
  314. "varying vec2 vUv;",
  315. "void main() {",
  316. "vUv = vec2( uv.x, 1.0 - uv.y );",
  317. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  318. "}"
  319. ].join("\n"),
  320. fragmentShader: [
  321. "uniform vec2 center;",
  322. "uniform float angle;",
  323. "uniform float scale;",
  324. "uniform vec2 tSize;",
  325. "uniform sampler2D tDiffuse;",
  326. "varying vec2 vUv;",
  327. "float pattern() {",
  328. "float s = sin( angle ), c = cos( angle );",
  329. "vec2 tex = vUv * tSize - center;",
  330. "vec2 point = vec2( c * tex.x - s * tex.y, s * tex.x + c * tex.y ) * scale;",
  331. "return ( sin( point.x ) * sin( point.y ) ) * 4.0;",
  332. "}",
  333. "void main() {",
  334. "vec4 color = texture2D( tDiffuse, vUv );",
  335. "float average = ( color.r + color.g + color.b ) / 3.0;",
  336. "gl_FragColor = vec4( vec3( average * 10.0 - 5.0 + pattern() ), color.a );",
  337. "}"
  338. ].join("\n")
  339. },
  340. /* ------------------------------------------------------------------------------------------------
  341. // Vignette shader
  342. // - based on PaintEffect postprocess from ro.me
  343. // http://code.google.com/p/3-dreams-of-black/source/browse/deploy/js/effects/PaintEffect.js
  344. ------------------------------------------------------------------------------------------------ */
  345. 'vignette': {
  346. uniforms: {
  347. tDiffuse: { type: "t", value: 0, texture: null },
  348. offset: { type: "f", value: 1.0 },
  349. darkness: { type: "f", value: 1.0 }
  350. },
  351. vertexShader: [
  352. "varying vec2 vUv;",
  353. "void main() {",
  354. "vUv = vec2( uv.x, 1.0 - uv.y );",
  355. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  356. "}"
  357. ].join("\n"),
  358. fragmentShader: [
  359. "uniform float offset;",
  360. "uniform float darkness;",
  361. "uniform sampler2D tDiffuse;",
  362. "varying vec2 vUv;",
  363. "void main() {",
  364. // Eskil's vignette
  365. "vec4 texel = texture2D( tDiffuse, vUv );",
  366. "vec2 uv = ( vUv - vec2( 0.5 ) ) * vec2( offset );",
  367. "gl_FragColor = vec4( mix( texel.rgb, vec3( 1.0 - darkness ), dot( uv, uv ) ), texel.a );",
  368. /*
  369. // alternative version from glfx.js
  370. // this one makes more "dusty" look (as opposed to "burned")
  371. "vec4 color = texture2D( tDiffuse, vUv );",
  372. "float dist = distance( vUv, vec2( 0.5 ) );",
  373. "color.rgb *= smoothstep( 0.8, offset * 0.799, dist *( darkness + offset ) );",
  374. "gl_FragColor = color;",
  375. */
  376. "}"
  377. ].join("\n")
  378. },
  379. /* -------------------------------------------------------------------------
  380. // Bleach bypass shader [http://en.wikipedia.org/wiki/Bleach_bypass]
  381. // - based on Nvidia example
  382. // http://developer.download.nvidia.com/shaderlibrary/webpages/shader_library.html#post_bleach_bypass
  383. ------------------------------------------------------------------------- */
  384. 'bleachbypass': {
  385. uniforms: {
  386. tDiffuse: { type: "t", value: 0, texture: null },
  387. opacity: { type: "f", value: 1.0 }
  388. },
  389. vertexShader: [
  390. "varying vec2 vUv;",
  391. "void main() {",
  392. "vUv = vec2( uv.x, 1.0 - uv.y );",
  393. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  394. "}"
  395. ].join("\n"),
  396. fragmentShader: [
  397. "uniform float opacity;",
  398. "uniform sampler2D tDiffuse;",
  399. "varying vec2 vUv;",
  400. "void main() {",
  401. "vec4 base = texture2D( tDiffuse, vUv );",
  402. "vec3 lumCoeff = vec3( 0.25, 0.65, 0.1 );",
  403. "float lum = dot( lumCoeff, base.rgb );",
  404. "vec3 blend = vec3( lum );",
  405. "float L = min( 1.0, max( 0.0, 10.0 * ( lum - 0.45 ) ) );",
  406. "vec3 result1 = 2.0 * base.rgb * blend;",
  407. "vec3 result2 = 1.0 - 2.0 * ( 1.0 - blend ) * ( 1.0 - base.rgb );",
  408. "vec3 newColor = mix( result1, result2, L );",
  409. "float A2 = opacity * base.a;",
  410. "vec3 mixRGB = A2 * newColor.rgb;",
  411. "mixRGB += ( ( 1.0 - A2 ) * base.rgb );",
  412. "gl_FragColor = vec4( mixRGB, base.a );",
  413. "}"
  414. ].join("\n")
  415. },
  416. /* --------------------------------------------------------------------------------------------------
  417. // Focus shader
  418. // - based on PaintEffect postprocess from ro.me
  419. // http://code.google.com/p/3-dreams-of-black/source/browse/deploy/js/effects/PaintEffect.js
  420. -------------------------------------------------------------------------------------------------- */
  421. 'focus': {
  422. uniforms : {
  423. "tDiffuse": { type: "t", value: 0, texture: null },
  424. "screenWidth": { type: "f", value: 1024 },
  425. "screenHeight": { type: "f", value: 1024 },
  426. "sampleDistance": { type: "f", value: 0.94 },
  427. "waveFactor": { type: "f", value: 0.00125 }
  428. },
  429. vertexShader: [
  430. "varying vec2 vUv;",
  431. "void main() {",
  432. "vUv = vec2( uv.x, 1.0 - uv.y );",
  433. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  434. "}"
  435. ].join("\n"),
  436. fragmentShader: [
  437. "uniform float screenWidth;",
  438. "uniform float screenHeight;",
  439. "uniform float sampleDistance;",
  440. "uniform float waveFactor;",
  441. "uniform sampler2D tDiffuse;",
  442. "varying vec2 vUv;",
  443. "void main() {",
  444. "vec4 color, org, tmp, add;",
  445. "float sample_dist, f;",
  446. "vec2 vin;",
  447. "vec2 uv = vUv;",
  448. "add += color = org = texture2D( tDiffuse, uv );",
  449. "vin = ( uv - vec2( 0.5 ) ) * vec2( 1.4 );",
  450. "sample_dist = dot( vin, vin ) * 2.0;",
  451. "f = ( waveFactor * 100.0 + sample_dist ) * sampleDistance * 4.0;",
  452. "vec2 sampleSize = vec2( 1.0 / screenWidth, 1.0 / screenHeight ) * vec2( f );",
  453. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.111964, 0.993712 ) * sampleSize );",
  454. "if( tmp.b < color.b ) color = tmp;",
  455. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.846724, 0.532032 ) * sampleSize );",
  456. "if( tmp.b < color.b ) color = tmp;",
  457. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.943883, -0.330279 ) * sampleSize );",
  458. "if( tmp.b < color.b ) color = tmp;",
  459. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.330279, -0.943883 ) * sampleSize );",
  460. "if( tmp.b < color.b ) color = tmp;",
  461. "add += tmp = texture2D( tDiffuse, uv + vec2( -0.532032, -0.846724 ) * sampleSize );",
  462. "if( tmp.b < color.b ) color = tmp;",
  463. "add += tmp = texture2D( tDiffuse, uv + vec2( -0.993712, -0.111964 ) * sampleSize );",
  464. "if( tmp.b < color.b ) color = tmp;",
  465. "add += tmp = texture2D( tDiffuse, uv + vec2( -0.707107, 0.707107 ) * sampleSize );",
  466. "if( tmp.b < color.b ) color = tmp;",
  467. "color = color * vec4( 2.0 ) - ( add / vec4( 8.0 ) );",
  468. "color = color + ( add / vec4( 8.0 ) - color ) * ( vec4( 1.0 ) - vec4( sample_dist * 0.5 ) );",
  469. "gl_FragColor = vec4( color.rgb * color.rgb * vec3( 0.95 ) + color.rgb, 1.0 );",
  470. "}"
  471. ].join("\n")
  472. },
  473. /* -------------------------------------------------------------------------
  474. // Triangle blur shader
  475. // - based on glfx.js triangle blur shader
  476. // https://github.com/evanw/glfx.js
  477. // A basic blur filter, which convolves the image with a
  478. // pyramid filter. The pyramid filter is separable and is applied as two
  479. // perpendicular triangle filters.
  480. ------------------------------------------------------------------------- */
  481. 'triangleBlur': {
  482. uniforms : {
  483. "texture": { type: "t", value: 0, texture: null },
  484. "delta": { type: "v2", value:new THREE.Vector2( 1, 1 ) }
  485. },
  486. vertexShader: [
  487. "varying vec2 vUv;",
  488. "void main() {",
  489. "vUv = vec2( uv.x, 1.0 - uv.y );",
  490. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  491. "}"
  492. ].join("\n"),
  493. fragmentShader: [
  494. "#define ITERATIONS 10.0",
  495. "uniform sampler2D texture;",
  496. "uniform vec2 delta;",
  497. "varying vec2 vUv;",
  498. "float random( vec3 scale, float seed ) {",
  499. // use the fragment position for a different seed per-pixel
  500. "return fract( sin( dot( gl_FragCoord.xyz + seed, scale ) ) * 43758.5453 + seed );",
  501. "}",
  502. "void main() {",
  503. "vec4 color = vec4( 0.0 );",
  504. "float total = 0.0;",
  505. // randomize the lookup values to hide the fixed number of samples
  506. "float offset = random( vec3( 12.9898, 78.233, 151.7182 ), 0.0 );",
  507. "for ( float t = -ITERATIONS; t <= ITERATIONS; t ++ ) {",
  508. "float percent = ( t + offset - 0.5 ) / ITERATIONS;",
  509. "float weight = 1.0 - abs( percent );",
  510. "color += texture2D( texture, vUv + delta * percent ) * weight;",
  511. "total += weight;",
  512. "}",
  513. "gl_FragColor = color / total;",
  514. "}",
  515. ].join("\n")
  516. },
  517. /* -------------------------------------------------------------------------
  518. // Simple test shader
  519. ------------------------------------------------------------------------- */
  520. 'basic': {
  521. uniforms: {},
  522. vertexShader: [
  523. "void main() {",
  524. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  525. "}"
  526. ].join("\n"),
  527. fragmentShader: [
  528. "void main() {",
  529. "gl_FragColor = vec4( 1.0, 0.0, 0.0, 0.5 );",
  530. "}"
  531. ].join("\n")
  532. },
  533. /* --------------------------------------------------------------------------------------------------
  534. // Two pass Gaussian blur filter (horizontal and vertical blur shaders)
  535. // - described in http://www.gamerendering.com/2008/10/11/gaussian-blur-filter-shader/
  536. // and used in http://www.cake23.de/traveling-wavefronts-lit-up.html
  537. //
  538. // - 9 samples per pass
  539. // - standard deviation 2.7
  540. // - "h" and "v" parameters should be set to "1 / width" and "1 / height"
  541. -------------------------------------------------------------------------------------------------- */
  542. 'horizontalBlur': {
  543. uniforms: {
  544. "tDiffuse": { type: "t", value: 0, texture: null },
  545. "h": { type: "f", value: 1.0 / 512.0 }
  546. },
  547. vertexShader: [
  548. "varying vec2 vUv;",
  549. "void main() {",
  550. "vUv = vec2( uv.x, 1.0 - uv.y );",
  551. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  552. "}"
  553. ].join("\n"),
  554. fragmentShader: [
  555. "uniform sampler2D tDiffuse;",
  556. "uniform float h;",
  557. "varying vec2 vUv;",
  558. "void main() {",
  559. "vec4 sum = vec4( 0.0 );",
  560. "sum += texture2D( tDiffuse, vec2( vUv.x - 4.0 * h, vUv.y ) ) * 0.051;",
  561. "sum += texture2D( tDiffuse, vec2( vUv.x - 3.0 * h, vUv.y ) ) * 0.0918;",
  562. "sum += texture2D( tDiffuse, vec2( vUv.x - 2.0 * h, vUv.y ) ) * 0.12245;",
  563. "sum += texture2D( tDiffuse, vec2( vUv.x - 1.0 * h, vUv.y ) ) * 0.1531;",
  564. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",
  565. "sum += texture2D( tDiffuse, vec2( vUv.x + 1.0 * h, vUv.y ) ) * 0.1531;",
  566. "sum += texture2D( tDiffuse, vec2( vUv.x + 2.0 * h, vUv.y ) ) * 0.12245;",
  567. "sum += texture2D( tDiffuse, vec2( vUv.x + 3.0 * h, vUv.y ) ) * 0.0918;",
  568. "sum += texture2D( tDiffuse, vec2( vUv.x + 4.0 * h, vUv.y ) ) * 0.051;",
  569. "gl_FragColor = sum;",
  570. "}"
  571. ].join("\n")
  572. },
  573. 'verticalBlur': {
  574. uniforms: {
  575. "tDiffuse": { type: "t", value: 0, texture: null },
  576. "v": { type: "f", value: 1.0 / 512.0 }
  577. },
  578. vertexShader: [
  579. "varying vec2 vUv;",
  580. "void main() {",
  581. "vUv = vec2( uv.x, 1.0 - uv.y );",
  582. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  583. "}"
  584. ].join("\n"),
  585. fragmentShader: [
  586. "uniform sampler2D tDiffuse;",
  587. "uniform float v;",
  588. "varying vec2 vUv;",
  589. "void main() {",
  590. "vec4 sum = vec4( 0.0 );",
  591. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 4.0 * v ) ) * 0.051;",
  592. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 3.0 * v ) ) * 0.0918;",
  593. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 2.0 * v ) ) * 0.12245;",
  594. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 1.0 * v ) ) * 0.1531;",
  595. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",
  596. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 1.0 * v ) ) * 0.1531;",
  597. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 2.0 * v ) ) * 0.12245;",
  598. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 3.0 * v ) ) * 0.0918;",
  599. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 4.0 * v ) ) * 0.051;",
  600. "gl_FragColor = sum;",
  601. "}"
  602. ].join("\n")
  603. },
  604. /* --------------------------------------------------------------------------------------------------
  605. // Simple fake tilt-shift effect, modulating two pass Gaussian blur (see above) by vertical position
  606. //
  607. // - 9 samples per pass
  608. // - standard deviation 2.7
  609. // - "h" and "v" parameters should be set to "1 / width" and "1 / height"
  610. // - "r" parameter control where "focused" horizontal line lies
  611. -------------------------------------------------------------------------------------------------- */
  612. 'horizontalTiltShift': {
  613. uniforms: {
  614. "tDiffuse": { type: "t", value: 0, texture: null },
  615. "h": { type: "f", value: 1.0 / 512.0 },
  616. "r": { type: "f", value: 0.35 }
  617. },
  618. vertexShader: [
  619. "varying vec2 vUv;",
  620. "void main() {",
  621. "vUv = vec2( uv.x, 1.0 - uv.y );",
  622. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  623. "}"
  624. ].join("\n"),
  625. fragmentShader: [
  626. "uniform sampler2D tDiffuse;",
  627. "uniform float h;",
  628. "uniform float r;",
  629. "varying vec2 vUv;",
  630. "void main() {",
  631. "vec4 sum = vec4( 0.0 );",
  632. "float hh = h * abs( r - vUv.y );",
  633. "sum += texture2D( tDiffuse, vec2( vUv.x - 4.0 * hh, vUv.y ) ) * 0.051;",
  634. "sum += texture2D( tDiffuse, vec2( vUv.x - 3.0 * hh, vUv.y ) ) * 0.0918;",
  635. "sum += texture2D( tDiffuse, vec2( vUv.x - 2.0 * hh, vUv.y ) ) * 0.12245;",
  636. "sum += texture2D( tDiffuse, vec2( vUv.x - 1.0 * hh, vUv.y ) ) * 0.1531;",
  637. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",
  638. "sum += texture2D( tDiffuse, vec2( vUv.x + 1.0 * hh, vUv.y ) ) * 0.1531;",
  639. "sum += texture2D( tDiffuse, vec2( vUv.x + 2.0 * hh, vUv.y ) ) * 0.12245;",
  640. "sum += texture2D( tDiffuse, vec2( vUv.x + 3.0 * hh, vUv.y ) ) * 0.0918;",
  641. "sum += texture2D( tDiffuse, vec2( vUv.x + 4.0 * hh, vUv.y ) ) * 0.051;",
  642. "gl_FragColor = sum;",
  643. "}"
  644. ].join("\n")
  645. },
  646. 'verticalTiltShift': {
  647. uniforms: {
  648. "tDiffuse": { type: "t", value: 0, texture: null },
  649. "v": { type: "f", value: 1.0 / 512.0 },
  650. "r": { type: "f", value: 0.35 }
  651. },
  652. vertexShader: [
  653. "varying vec2 vUv;",
  654. "void main() {",
  655. "vUv = vec2( uv.x, 1.0 - uv.y );",
  656. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  657. "}"
  658. ].join("\n"),
  659. fragmentShader: [
  660. "uniform sampler2D tDiffuse;",
  661. "uniform float v;",
  662. "uniform float r;",
  663. "varying vec2 vUv;",
  664. "void main() {",
  665. "vec4 sum = vec4( 0.0 );",
  666. "float vv = v * abs( r - vUv.y );",
  667. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 4.0 * vv ) ) * 0.051;",
  668. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 3.0 * vv ) ) * 0.0918;",
  669. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 2.0 * vv ) ) * 0.12245;",
  670. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 1.0 * vv ) ) * 0.1531;",
  671. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",
  672. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 1.0 * vv ) ) * 0.1531;",
  673. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 2.0 * vv ) ) * 0.12245;",
  674. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 3.0 * vv ) ) * 0.0918;",
  675. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 4.0 * vv ) ) * 0.051;",
  676. "gl_FragColor = sum;",
  677. "}"
  678. ].join("\n")
  679. },
  680. // METHODS
  681. buildKernel: function( sigma ) {
  682. // We lop off the sqrt(2 * pi) * sigma term, since we're going to normalize anyway.
  683. function gauss( x, sigma ) {
  684. return Math.exp( - ( x * x ) / ( 2.0 * sigma * sigma ) );
  685. }
  686. var i, values, sum, halfWidth, kMaxKernelSize = 25, kernelSize = 2 * Math.ceil( sigma * 3.0 ) + 1;
  687. if ( kernelSize > kMaxKernelSize ) kernelSize = kMaxKernelSize;
  688. halfWidth = ( kernelSize - 1 ) * 0.5
  689. values = new Array( kernelSize );
  690. sum = 0.0;
  691. for ( i = 0; i < kernelSize; ++i ) {
  692. values[ i ] = gauss( i - halfWidth, sigma );
  693. sum += values[ i ];
  694. }
  695. // normalize the kernel
  696. for ( i = 0; i < kernelSize; ++i ) values[ i ] /= sum;
  697. return values;
  698. }
  699. };