ShaderExtras.js 30 KB

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