ShaderExtras.js 26 KB

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