ShaderExtras.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. *
  4. * ShaderExtras currently contains:
  5. *
  6. * screen
  7. * convolution
  8. * film
  9. * bokeh
  10. * sepia
  11. * dotscreen
  12. * vignette
  13. * bleachbypass
  14. * basic
  15. *
  16. */
  17. THREE.ShaderExtras = {
  18. /* -------------------------------------------------------------------------
  19. // Full-screen textured quad shader
  20. ------------------------------------------------------------------------- */
  21. 'screen': {
  22. uniforms: {
  23. tDiffuse: { type: "t", value: 0, texture: null },
  24. opacity: { type: "f", value: 1.0 }
  25. },
  26. vertexShader: [
  27. "varying vec2 vUv;",
  28. "void main() {",
  29. "vUv = vec2( uv.x, 1.0 - uv.y );",
  30. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  31. "}"
  32. ].join("\n"),
  33. fragmentShader: [
  34. "uniform float opacity;",
  35. "uniform sampler2D tDiffuse;",
  36. "varying vec2 vUv;",
  37. "void main() {",
  38. "vec4 texel = texture2D( tDiffuse, vUv );",
  39. "gl_FragColor = opacity * texel;",
  40. "}"
  41. ].join("\n")
  42. },
  43. /* ------------------------------------------------------------------------
  44. // Convolution shader
  45. // - ported from o3d sample to WebGL / GLSL
  46. // http://o3d.googlecode.com/svn/trunk/samples/convolution.html
  47. ------------------------------------------------------------------------ */
  48. 'convolution': {
  49. uniforms: {
  50. "tDiffuse" : { type: "t", value: 0, texture: null },
  51. "uImageIncrement" : { type: "v2", value: new THREE.Vector2( 0.001953125, 0.0 ) },
  52. "cKernel" : { type: "fv1", value: [] }
  53. },
  54. vertexShader: [
  55. //"#define KERNEL_SIZE 25.0",
  56. "uniform vec2 uImageIncrement;",
  57. "varying vec2 vUv;",
  58. "void main() {",
  59. "vUv = uv - ( ( KERNEL_SIZE - 1.0 ) / 2.0 ) * uImageIncrement;",
  60. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  61. "}"
  62. ].join("\n"),
  63. fragmentShader: [
  64. //"#define KERNEL_SIZE 25",
  65. "uniform float cKernel[ KERNEL_SIZE ];",
  66. "uniform sampler2D tDiffuse;",
  67. "uniform vec2 uImageIncrement;",
  68. "varying vec2 vUv;",
  69. "void main() {",
  70. "vec2 imageCoord = vUv;",
  71. "vec4 sum = vec4( 0.0, 0.0, 0.0, 0.0 );",
  72. "for( int i = 0; i < KERNEL_SIZE; i ++ ) {",
  73. "sum += texture2D( tDiffuse, imageCoord ) * cKernel[ i ];",
  74. "imageCoord += uImageIncrement;",
  75. "}",
  76. "gl_FragColor = sum;",
  77. "}"
  78. ].join("\n")
  79. },
  80. /* -------------------------------------------------------------------------
  81. // Film grain & scanlines shader
  82. // - ported from HLSL to WebGL / GLSL
  83. // http://www.truevision3d.com/forums/showcase/staticnoise_colorblackwhite_scanline_shaders-t18698.0.html
  84. // Screen Space Static Postprocessor
  85. //
  86. // Produces an analogue noise overlay similar to a film grain / TV static
  87. //
  88. // Original implementation and noise algorithm
  89. // Pat 'Hawthorne' Shearon
  90. //
  91. // Optimized scanlines + noise version with intensity scaling
  92. // Georg 'Leviathan' Steinrohder
  93. // This version is provided under a Creative Commons Attribution 3.0 License
  94. // http://creativecommons.org/licenses/by/3.0/
  95. ------------------------------------------------------------------------- */
  96. 'film': {
  97. uniforms: {
  98. tDiffuse: { type: "t", value: 0, texture: null },
  99. time: { type: "f", value: 0.0 },
  100. nIntensity: { type: "f", value: 0.5 },
  101. sIntensity: { type: "f", value: 0.05 },
  102. sCount: { type: "f", value: 4096 },
  103. grayscale: { type: "i", value: 1 }
  104. },
  105. vertexShader: [
  106. "varying vec2 vUv;",
  107. "void main() {",
  108. "vUv = vec2( uv.x, 1.0 - uv.y );",
  109. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  110. "}"
  111. ].join("\n"),
  112. fragmentShader: [
  113. // control parameter
  114. "uniform float time;",
  115. "uniform bool grayscale;",
  116. // noise effect intensity value (0 = no effect, 1 = full effect)
  117. "uniform float nIntensity;",
  118. // scanlines effect intensity value (0 = no effect, 1 = full effect)
  119. "uniform float sIntensity;",
  120. // scanlines effect count value (0 = no effect, 4096 = full effect)
  121. "uniform float sCount;",
  122. "uniform sampler2D tDiffuse;",
  123. "varying vec2 vUv;",
  124. "void main() {",
  125. // sample the source
  126. "vec4 cTextureScreen = texture2D( tDiffuse, vUv );",
  127. // make some noise
  128. "float x = vUv.x * vUv.y * time * 1000.0;",
  129. "x = mod( x, 13.0 ) * mod( x, 123.0 );",
  130. "float dx = mod( x, 0.01 );",
  131. // add noise
  132. "vec3 cResult = cTextureScreen.rgb + cTextureScreen.rgb * clamp( 0.1 + dx * 100.0, 0.0, 1.0 );",
  133. // get us a sine and cosine
  134. "vec2 sc = vec2( sin( vUv.y * sCount ), cos( vUv.y * sCount ) );",
  135. // add scanlines
  136. "cResult += cTextureScreen.rgb * vec3( sc.x, sc.y, sc.x ) * sIntensity;",
  137. // interpolate between source and result by intensity
  138. "cResult = cTextureScreen.rgb + clamp( nIntensity, 0.0,1.0 ) * ( cResult - cTextureScreen.rgb );",
  139. // convert to grayscale if desired
  140. "if( grayscale ) {",
  141. "cResult = vec3( cResult.r * 0.3 + cResult.g * 0.59 + cResult.b * 0.11 );",
  142. "}",
  143. "gl_FragColor = vec4( cResult, cTextureScreen.a );",
  144. "}"
  145. ].join("\n")
  146. },
  147. /* -------------------------------------------------------------------------
  148. // Depth-of-field shader with bokeh
  149. // ported from GLSL shader by Martins Upitis
  150. // http://artmartinsh.blogspot.com/2010/02/glsl-lens-blur-filter-with-bokeh.html
  151. ------------------------------------------------------------------------- */
  152. 'bokeh' : {
  153. uniforms: { tColor: { type: "t", value: 0, texture: null },
  154. tDepth: { type: "t", value: 1, texture: null },
  155. focus: { type: "f", value: 1.0 },
  156. aspect: { type: "f", value: 1.0 },
  157. aperture: { type: "f", value: 0.025 },
  158. maxblur: { type: "f", value: 1.0 },
  159. },
  160. vertexShader: [
  161. "varying vec2 vUv;",
  162. "void main() {",
  163. "vUv = vec2( uv.x, 1.0 - uv.y );",
  164. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  165. "}"
  166. ].join("\n"),
  167. fragmentShader: [
  168. "varying vec2 vUv;",
  169. "uniform sampler2D tColor;",
  170. "uniform sampler2D tDepth;",
  171. "uniform float maxblur;", // max blur amount
  172. "uniform float aperture;", // aperture - bigger values for shallower depth of field
  173. "uniform float focus;",
  174. "uniform float aspect;",
  175. "void main() {",
  176. "vec2 aspectcorrect = vec2( 1.0, aspect );",
  177. "vec4 depth1 = texture2D( tDepth, vUv );",
  178. "float factor = depth1.x - focus;",
  179. "vec2 dofblur = vec2 ( clamp( factor * aperture, -maxblur, maxblur ) );",
  180. "vec2 dofblur9 = dofblur * 0.9;",
  181. "vec2 dofblur7 = dofblur * 0.7;",
  182. "vec2 dofblur4 = dofblur * 0.4;",
  183. "vec4 col = vec4( 0.0 );",
  184. "col += texture2D( tColor, vUv.xy );",
  185. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, 0.4 ) * aspectcorrect ) * dofblur );",
  186. "col += texture2D( tColor, vUv.xy + ( vec2( 0.15, 0.37 ) * aspectcorrect ) * dofblur );",
  187. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, 0.29 ) * aspectcorrect ) * dofblur );",
  188. "col += texture2D( tColor, vUv.xy + ( vec2( -0.37, 0.15 ) * aspectcorrect ) * dofblur );",
  189. "col += texture2D( tColor, vUv.xy + ( vec2( 0.40, 0.0 ) * aspectcorrect ) * dofblur );",
  190. "col += texture2D( tColor, vUv.xy + ( vec2( 0.37, -0.15 ) * aspectcorrect ) * dofblur );",
  191. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, -0.29 ) * aspectcorrect ) * dofblur );",
  192. "col += texture2D( tColor, vUv.xy + ( vec2( -0.15, -0.37 ) * aspectcorrect ) * dofblur );",
  193. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, -0.4 ) * aspectcorrect ) * dofblur );",
  194. "col += texture2D( tColor, vUv.xy + ( vec2( -0.15, 0.37 ) * aspectcorrect ) * dofblur );",
  195. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, 0.29 ) * aspectcorrect ) * dofblur );",
  196. "col += texture2D( tColor, vUv.xy + ( vec2( 0.37, 0.15 ) * aspectcorrect ) * dofblur );",
  197. "col += texture2D( tColor, vUv.xy + ( vec2( -0.4, 0.0 ) * aspectcorrect ) * dofblur );",
  198. "col += texture2D( tColor, vUv.xy + ( vec2( -0.37, -0.15 ) * aspectcorrect ) * dofblur );",
  199. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, -0.29 ) * aspectcorrect ) * dofblur );",
  200. "col += texture2D( tColor, vUv.xy + ( vec2( 0.15, -0.37 ) * aspectcorrect ) * dofblur );",
  201. "col += texture2D( tColor, vUv.xy + ( vec2( 0.15, 0.37 ) * aspectcorrect ) * dofblur9 );",
  202. "col += texture2D( tColor, vUv.xy + ( vec2( -0.37, 0.15 ) * aspectcorrect ) * dofblur9 );",
  203. "col += texture2D( tColor, vUv.xy + ( vec2( 0.37, -0.15 ) * aspectcorrect ) * dofblur9 );",
  204. "col += texture2D( tColor, vUv.xy + ( vec2( -0.15, -0.37 ) * aspectcorrect ) * dofblur9 );",
  205. "col += texture2D( tColor, vUv.xy + ( vec2( -0.15, 0.37 ) * aspectcorrect ) * dofblur9 );",
  206. "col += texture2D( tColor, vUv.xy + ( vec2( 0.37, 0.15 ) * aspectcorrect ) * dofblur9 );",
  207. "col += texture2D( tColor, vUv.xy + ( vec2( -0.37, -0.15 ) * aspectcorrect ) * dofblur9 );",
  208. "col += texture2D( tColor, vUv.xy + ( vec2( 0.15, -0.37 ) * aspectcorrect ) * dofblur9 );",
  209. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, 0.29 ) * aspectcorrect ) * dofblur7 );",
  210. "col += texture2D( tColor, vUv.xy + ( vec2( 0.40, 0.0 ) * aspectcorrect ) * dofblur7 );",
  211. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, -0.29 ) * aspectcorrect ) * dofblur7 );",
  212. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, -0.4 ) * aspectcorrect ) * dofblur7 );",
  213. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, 0.29 ) * aspectcorrect ) * dofblur7 );",
  214. "col += texture2D( tColor, vUv.xy + ( vec2( -0.4, 0.0 ) * aspectcorrect ) * dofblur7 );",
  215. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, -0.29 ) * aspectcorrect ) * dofblur7 );",
  216. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, 0.4 ) * aspectcorrect ) * dofblur7 );",
  217. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, 0.29 ) * aspectcorrect ) * dofblur4 );",
  218. "col += texture2D( tColor, vUv.xy + ( vec2( 0.4, 0.0 ) * aspectcorrect ) * dofblur4 );",
  219. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, -0.29 ) * aspectcorrect ) * dofblur4 );",
  220. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, -0.4 ) * aspectcorrect ) * dofblur4 );",
  221. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, 0.29 ) * aspectcorrect ) * dofblur4 );",
  222. "col += texture2D( tColor, vUv.xy + ( vec2( -0.4, 0.0 ) * aspectcorrect ) * dofblur4 );",
  223. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, -0.29 ) * aspectcorrect ) * dofblur4 );",
  224. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, 0.4 ) * aspectcorrect ) * dofblur4 );",
  225. "gl_FragColor = col / 41.0;",
  226. "gl_FragColor.a = 1.0;",
  227. "}"
  228. ].join("\n")
  229. },
  230. /* -------------------------------------------------------------------------
  231. // Depth-of-field shader using mipmaps
  232. // - from Matt Handley @applmak
  233. // - requires power-of-2 sized render target with enabled mipmaps
  234. ------------------------------------------------------------------------- */
  235. 'dofmipmap': {
  236. uniforms: {
  237. tColor: { type: "t", value: 0, texture: null },
  238. tDepth: { type: "t", value: 1, texture: null },
  239. focus: { type: "f", value: 1.0 },
  240. maxblur: { type: "f", value: 1.0 }
  241. },
  242. vertexShader: [
  243. "varying vec2 vUv;",
  244. "void main() {",
  245. "vUv = vec2( uv.x, 1.0 - uv.y );",
  246. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  247. "}"
  248. ].join("\n"),
  249. fragmentShader: [
  250. "uniform float focus;",
  251. "uniform float maxblur;",
  252. "uniform sampler2D tColor;",
  253. "uniform sampler2D tDepth;",
  254. "varying vec2 vUv;",
  255. "void main() {",
  256. "vec4 depth = texture2D( tDepth, vUv );",
  257. "float factor = depth.x - focus;",
  258. "vec4 col = texture2D( tColor, vUv, 2.0 * maxblur * abs( focus - depth.x ) );",
  259. "gl_FragColor = col;",
  260. "gl_FragColor.a = 1.0;",
  261. "}"
  262. ].join("\n")
  263. },
  264. /* -------------------------------------------------------------------------
  265. // Sepia tone shader
  266. // - based on glfx.js sepia shader
  267. // https://github.com/evanw/glfx.js
  268. ------------------------------------------------------------------------- */
  269. 'sepia': {
  270. uniforms: {
  271. tDiffuse: { type: "t", value: 0, texture: null },
  272. amount: { type: "f", value: 1.0 }
  273. },
  274. vertexShader: [
  275. "varying vec2 vUv;",
  276. "void main() {",
  277. "vUv = vec2( uv.x, 1.0 - uv.y );",
  278. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  279. "}"
  280. ].join("\n"),
  281. fragmentShader: [
  282. "uniform float amount;",
  283. "uniform sampler2D tDiffuse;",
  284. "varying vec2 vUv;",
  285. "void main() {",
  286. "vec4 color = texture2D( tDiffuse, vUv );",
  287. "vec3 c = color.rgb;",
  288. "color.r = dot( c, vec3( 1.0 - 0.607 * amount, 0.769 * amount, 0.189 * amount ) );",
  289. "color.g = dot( c, vec3( 0.349 * amount, 1.0 - 0.314 * amount, 0.168 * amount ) );",
  290. "color.b = dot( c, vec3( 0.272 * amount, 0.534 * amount, 1.0 - 0.869 * amount ) );",
  291. "gl_FragColor = vec4( min( vec3( 1.0 ), color.rgb ), color.a );",
  292. "}"
  293. ].join("\n")
  294. },
  295. /* -------------------------------------------------------------------------
  296. // Dot screen shader
  297. // - based on glfx.js sepia shader
  298. // https://github.com/evanw/glfx.js
  299. ------------------------------------------------------------------------- */
  300. 'dotscreen': {
  301. uniforms: {
  302. tDiffuse: { type: "t", value: 0, texture: null },
  303. tSize: { type: "v2", value: new THREE.Vector2( 256, 256 ) },
  304. center: { type: "v2", value: new THREE.Vector2( 0.5, 0.5 ) },
  305. angle: { type: "f", value: 1.57 },
  306. scale: { type: "f", value: 1.0 }
  307. },
  308. vertexShader: [
  309. "varying vec2 vUv;",
  310. "void main() {",
  311. "vUv = vec2( uv.x, 1.0 - uv.y );",
  312. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  313. "}"
  314. ].join("\n"),
  315. fragmentShader: [
  316. "uniform vec2 center;",
  317. "uniform float angle;",
  318. "uniform float scale;",
  319. "uniform vec2 tSize;",
  320. "uniform sampler2D tDiffuse;",
  321. "varying vec2 vUv;",
  322. "float pattern() {",
  323. "float s = sin( angle ), c = cos( angle );",
  324. "vec2 tex = vUv * tSize - center;",
  325. "vec2 point = vec2( c * tex.x - s * tex.y, s * tex.x + c * tex.y ) * scale;",
  326. "return ( sin( point.x ) * sin( point.y ) ) * 4.0;",
  327. "}",
  328. "void main() {",
  329. "vec4 color = texture2D( tDiffuse, vUv );",
  330. "float average = ( color.r + color.g + color.b ) / 3.0;",
  331. "gl_FragColor = vec4( vec3( average * 10.0 - 5.0 + pattern() ), color.a );",
  332. "}"
  333. ].join("\n")
  334. },
  335. /* ------------------------------------------------------------------------------------------------
  336. // Vignette shader
  337. // - based on PaintEffect postprocess from ro.me
  338. // http://code.google.com/p/3-dreams-of-black/source/browse/deploy/js/effects/PaintEffect.js
  339. ------------------------------------------------------------------------------------------------ */
  340. 'vignette': {
  341. uniforms: {
  342. tDiffuse: { type: "t", value: 0, texture: null },
  343. offset: { type: "f", value: 1.0 },
  344. darkness: { type: "f", value: 1.0 }
  345. },
  346. vertexShader: [
  347. "varying vec2 vUv;",
  348. "void main() {",
  349. "vUv = vec2( uv.x, 1.0 - uv.y );",
  350. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  351. "}"
  352. ].join("\n"),
  353. fragmentShader: [
  354. "uniform float offset;",
  355. "uniform float darkness;",
  356. "uniform sampler2D tDiffuse;",
  357. "varying vec2 vUv;",
  358. "void main() {",
  359. // Eskil's vignette
  360. "vec4 texel = texture2D( tDiffuse, vUv );",
  361. "vec2 uv = ( vUv - vec2( 0.5 ) ) * vec2( offset );",
  362. "gl_FragColor = vec4( mix( texel.rgb, vec3( 1.0 - darkness ), dot( uv, uv ) ), texel.a );",
  363. /*
  364. // alternative version from glfx.js
  365. // this one makes more "dusty" look (as opposed to "burned")
  366. "vec4 color = texture2D( tDiffuse, vUv );",
  367. "float dist = distance( vUv, vec2( 0.5 ) );",
  368. "color.rgb *= smoothstep( 0.8, offset * 0.799, dist *( darkness + offset ) );",
  369. "gl_FragColor = color;",
  370. */
  371. "}"
  372. ].join("\n")
  373. },
  374. /* -------------------------------------------------------------------------
  375. // Bleach bypass shader [http://en.wikipedia.org/wiki/Bleach_bypass]
  376. // - based on Nvidia example
  377. // http://developer.download.nvidia.com/shaderlibrary/webpages/shader_library.html#post_bleach_bypass
  378. ------------------------------------------------------------------------- */
  379. 'bleachbypass': {
  380. uniforms: {
  381. tDiffuse: { type: "t", value: 0, texture: null },
  382. opacity: { type: "f", value: 1.0 }
  383. },
  384. vertexShader: [
  385. "varying vec2 vUv;",
  386. "void main() {",
  387. "vUv = vec2( uv.x, 1.0 - uv.y );",
  388. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  389. "}"
  390. ].join("\n"),
  391. fragmentShader: [
  392. "uniform float opacity;",
  393. "uniform sampler2D tDiffuse;",
  394. "varying vec2 vUv;",
  395. "void main() {",
  396. "vec4 base = texture2D( tDiffuse, vUv );",
  397. "vec3 lumCoeff = vec3( 0.25, 0.65, 0.1 );",
  398. "float lum = dot( lumCoeff, base.rgb );",
  399. "vec3 blend = vec3( lum );",
  400. "float L = min( 1.0, max( 0.0, 10.0 * ( lum - 0.45 ) ) );",
  401. "vec3 result1 = 2.0 * base.rgb * blend;",
  402. "vec3 result2 = 1.0 - 2.0 * ( 1.0 - blend ) * ( 1.0 - base.rgb );",
  403. "vec3 newColor = mix( result1, result2, L );",
  404. "float A2 = opacity * base.a;",
  405. "vec3 mixRGB = A2 * newColor.rgb;",
  406. "mixRGB += ( ( 1.0 - A2 ) * base.rgb );",
  407. "gl_FragColor = vec4( mixRGB, base.a );",
  408. "}"
  409. ].join("\n")
  410. },
  411. /* --------------------------------------------------------------------------------------------------
  412. // Focus shader
  413. // - based on PaintEffect postprocess from ro.me
  414. // http://code.google.com/p/3-dreams-of-black/source/browse/deploy/js/effects/PaintEffect.js
  415. -------------------------------------------------------------------------------------------------- */
  416. 'focus': {
  417. uniforms : {
  418. "tDiffuse": { type: "t", value: 0, texture: null },
  419. "screenWidth": { type: "f", value: 1024 },
  420. "screenHeight": { type: "f", value: 1024 },
  421. "sampleDistance": { type: "f", value: 0.94 },
  422. "waveFactor": { type: "f", value: 0.00125 }
  423. },
  424. vertexShader: [
  425. "varying vec2 vUv;",
  426. "void main() {",
  427. "vUv = vec2( uv.x, 1.0 - uv.y );",
  428. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  429. "}"
  430. ].join("\n"),
  431. fragmentShader: [
  432. "uniform float screenWidth;",
  433. "uniform float screenHeight;",
  434. "uniform float sampleDistance;",
  435. "uniform float waveFactor;",
  436. "uniform sampler2D tDiffuse;",
  437. "varying vec2 vUv;",
  438. "void main() {",
  439. "vec4 color, org, tmp, add;",
  440. "float sample_dist, f;",
  441. "vec2 vin;",
  442. "vec2 uv = vUv;",
  443. "add += color = org = texture2D( tDiffuse, uv );",
  444. "vin = ( uv - vec2( 0.5 ) ) * vec2( 1.4 );",
  445. "sample_dist = dot( vin, vin ) * 2.0;",
  446. "f = ( waveFactor * 100.0 + sample_dist ) * sampleDistance * 4.0;",
  447. "vec2 sampleSize = vec2( 1.0 / screenWidth, 1.0 / screenHeight ) * vec2( f );",
  448. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.111964, 0.993712 ) * sampleSize );",
  449. "if( tmp.b < color.b ) color = tmp;",
  450. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.846724, 0.532032 ) * sampleSize );",
  451. "if( tmp.b < color.b ) color = tmp;",
  452. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.943883, -0.330279 ) * sampleSize );",
  453. "if( tmp.b < color.b ) color = tmp;",
  454. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.330279, -0.943883 ) * sampleSize );",
  455. "if( tmp.b < color.b ) color = tmp;",
  456. "add += tmp = texture2D( tDiffuse, uv + vec2( -0.532032, -0.846724 ) * sampleSize );",
  457. "if( tmp.b < color.b ) color = tmp;",
  458. "add += tmp = texture2D( tDiffuse, uv + vec2( -0.993712, -0.111964 ) * sampleSize );",
  459. "if( tmp.b < color.b ) color = tmp;",
  460. "add += tmp = texture2D( tDiffuse, uv + vec2( -0.707107, 0.707107 ) * sampleSize );",
  461. "if( tmp.b < color.b ) color = tmp;",
  462. "color = color * vec4( 2.0 ) - ( add / vec4( 8.0 ) );",
  463. "color = color + ( add / vec4( 8.0 ) - color ) * ( vec4( 1.0 ) - vec4( sample_dist * 0.5 ) );",
  464. "gl_FragColor = vec4( color.rgb * color.rgb * vec3( 0.95 ) + color.rgb, 1.0 );",
  465. "}"
  466. ].join("\n")
  467. },
  468. /* -------------------------------------------------------------------------
  469. // Simple test shader
  470. ------------------------------------------------------------------------- */
  471. 'basic': {
  472. uniforms: {},
  473. vertexShader: [
  474. "void main() {",
  475. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  476. "}"
  477. ].join("\n"),
  478. fragmentShader: [
  479. "void main() {",
  480. "gl_FragColor = vec4( 1.0, 0.0, 0.0, 0.5 );",
  481. "}"
  482. ].join("\n")
  483. },
  484. // METHODS
  485. buildKernel: function( sigma ) {
  486. // We lop off the sqrt(2 * pi) * sigma term, since we're going to normalize anyway.
  487. function gauss( x, sigma ) {
  488. return Math.exp( - ( x * x ) / ( 2.0 * sigma * sigma ) );
  489. }
  490. var i, values, sum, halfWidth, kMaxKernelSize = 25, kernelSize = 2 * Math.ceil( sigma * 3.0 ) + 1;
  491. if ( kernelSize > kMaxKernelSize ) kernelSize = kMaxKernelSize;
  492. halfWidth = ( kernelSize - 1 ) * 0.5
  493. values = new Array( kernelSize );
  494. sum = 0.0;
  495. for ( i = 0; i < kernelSize; ++i ) {
  496. values[ i ] = gauss( i - halfWidth, sigma );
  497. sum += values[ i ];
  498. }
  499. // normalize the kernel
  500. for ( i = 0; i < kernelSize; ++i ) values[ i ] /= sum;
  501. return values;
  502. }
  503. };