ShaderExtras.js 23 KB

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