ShaderExtras.js 22 KB

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