ShaderExtras.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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. // Sepia tone shader
  232. // - based on glfx.js sepia shader
  233. // https://github.com/evanw/glfx.js
  234. ------------------------------------------------------------------------- */
  235. 'sepia': {
  236. uniforms: {
  237. tDiffuse: { type: "t", value: 0, texture: null },
  238. amount: { type: "f", value: 1.0 }
  239. },
  240. vertexShader: [
  241. "varying vec2 vUv;",
  242. "void main() {",
  243. "vUv = vec2( uv.x, 1.0 - uv.y );",
  244. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  245. "}"
  246. ].join("\n"),
  247. fragmentShader: [
  248. "uniform float amount;",
  249. "uniform sampler2D tDiffuse;",
  250. "varying vec2 vUv;",
  251. "void main() {",
  252. "vec4 color = texture2D( tDiffuse, vUv );",
  253. "vec3 c = color.rgb;",
  254. "color.r = dot( c, vec3( 1.0 - 0.607 * amount, 0.769 * amount, 0.189 * amount ) );",
  255. "color.g = dot( c, vec3( 0.349 * amount, 1.0 - 0.314 * amount, 0.168 * amount ) );",
  256. "color.b = dot( c, vec3( 0.272 * amount, 0.534 * amount, 1.0 - 0.869 * amount ) );",
  257. "gl_FragColor = vec4( min( vec3( 1.0 ), color.rgb ), color.a );",
  258. "}"
  259. ].join("\n")
  260. },
  261. /* -------------------------------------------------------------------------
  262. // Dot screen shader
  263. // - based on glfx.js sepia shader
  264. // https://github.com/evanw/glfx.js
  265. ------------------------------------------------------------------------- */
  266. 'dotscreen': {
  267. uniforms: {
  268. tDiffuse: { type: "t", value: 0, texture: null },
  269. tSize: { type: "v2", value: new THREE.Vector2( 256, 256 ) },
  270. center: { type: "v2", value: new THREE.Vector2( 0.5, 0.5 ) },
  271. angle: { type: "f", value: 1.57 },
  272. scale: { 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 vec2 center;",
  283. "uniform float angle;",
  284. "uniform float scale;",
  285. "uniform vec2 tSize;",
  286. "uniform sampler2D tDiffuse;",
  287. "varying vec2 vUv;",
  288. "float pattern() {",
  289. "float s = sin( angle ), c = cos( angle );",
  290. "vec2 tex = vUv * tSize - center;",
  291. "vec2 point = vec2( c * tex.x - s * tex.y, s * tex.x + c * tex.y ) * scale;",
  292. "return ( sin( point.x ) * sin( point.y ) ) * 4.0;",
  293. "}",
  294. "void main() {",
  295. "vec4 color = texture2D( tDiffuse, vUv );",
  296. "float average = ( color.r + color.g + color.b ) / 3.0;",
  297. "gl_FragColor = vec4( vec3( average * 10.0 - 5.0 + pattern() ), color.a );",
  298. "}"
  299. ].join("\n")
  300. },
  301. /* ------------------------------------------------------------------------------------------------
  302. // Vignette shader
  303. // - based on PaintEffect postprocess from ro.me
  304. // http://code.google.com/p/3-dreams-of-black/source/browse/deploy/js/effects/PaintEffect.js
  305. ------------------------------------------------------------------------------------------------ */
  306. 'vignette': {
  307. uniforms: {
  308. tDiffuse: { type: "t", value: 0, texture: null },
  309. offset: { type: "f", value: 1.0 },
  310. darkness: { type: "f", value: 1.0 }
  311. },
  312. vertexShader: [
  313. "varying vec2 vUv;",
  314. "void main() {",
  315. "vUv = vec2( uv.x, 1.0 - uv.y );",
  316. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  317. "}"
  318. ].join("\n"),
  319. fragmentShader: [
  320. "uniform float offset;",
  321. "uniform float darkness;",
  322. "uniform sampler2D tDiffuse;",
  323. "varying vec2 vUv;",
  324. "void main() {",
  325. // Eskil's vignette
  326. "vec4 texel = texture2D( tDiffuse, vUv );",
  327. "vec2 uv = ( vUv - vec2( 0.5 ) ) * vec2( offset );",
  328. "gl_FragColor = vec4( mix( texel.rgb, vec3( 1.0 - darkness ), dot( uv, uv ) ), texel.a );",
  329. /*
  330. // alternative version from glfx.js
  331. // this one makes more "dusty" look (as opposed to "burned")
  332. "vec4 color = texture2D( tDiffuse, vUv );",
  333. "float dist = distance( vUv, vec2( 0.5 ) );",
  334. "color.rgb *= smoothstep( 0.8, offset * 0.799, dist *( darkness + offset ) );",
  335. "gl_FragColor = color;",
  336. */
  337. "}"
  338. ].join("\n")
  339. },
  340. /* -------------------------------------------------------------------------
  341. // Bleach bypass shader [http://en.wikipedia.org/wiki/Bleach_bypass]
  342. // - based on Nvidia example
  343. // http://developer.download.nvidia.com/shaderlibrary/webpages/shader_library.html#post_bleach_bypass
  344. ------------------------------------------------------------------------- */
  345. 'bleachbypass': {
  346. uniforms: {
  347. tDiffuse: { type: "t", value: 0, texture: null },
  348. opacity: { type: "f", value: 1.0 }
  349. },
  350. vertexShader: [
  351. "varying vec2 vUv;",
  352. "void main() {",
  353. "vUv = vec2( uv.x, 1.0 - uv.y );",
  354. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  355. "}"
  356. ].join("\n"),
  357. fragmentShader: [
  358. "uniform float opacity;",
  359. "uniform sampler2D tDiffuse;",
  360. "varying vec2 vUv;",
  361. "void main() {",
  362. "vec4 base = texture2D( tDiffuse, vUv );",
  363. "vec3 lumCoeff = vec3( 0.25, 0.65, 0.1 );",
  364. "float lum = dot( lumCoeff, base.rgb );",
  365. "vec3 blend = vec3( lum );",
  366. "float L = min( 1.0, max( 0.0, 10.0 * ( lum - 0.45 ) ) );",
  367. "vec3 result1 = 2.0 * base.rgb * blend;",
  368. "vec3 result2 = 1.0 - 2.0 * ( 1.0 - blend ) * ( 1.0 - base.rgb );",
  369. "vec3 newColor = mix( result1, result2, L );",
  370. "float A2 = opacity * base.a;",
  371. "vec3 mixRGB = A2 * newColor.rgb;",
  372. "mixRGB += ( ( 1.0 - A2 ) * base.rgb );",
  373. "gl_FragColor = vec4( mixRGB, base.a );",
  374. "}"
  375. ].join("\n")
  376. },
  377. /* --------------------------------------------------------------------------------------------------
  378. // Focus shader
  379. // - based on PaintEffect postprocess from ro.me
  380. // http://code.google.com/p/3-dreams-of-black/source/browse/deploy/js/effects/PaintEffect.js
  381. -------------------------------------------------------------------------------------------------- */
  382. 'focus': {
  383. uniforms : {
  384. "tDiffuse": { type: "t", value: 0, texture: null },
  385. "screenWidth": { type: "f", value: 1024 },
  386. "screenHeight": { type: "f", value: 1024 },
  387. "sampleDistance": { type: "f", value: 0.94 },
  388. "waveFactor": { type: "f", value: 0.00125 }
  389. },
  390. vertexShader: [
  391. "varying vec2 vUv;",
  392. "void main() {",
  393. "vUv = vec2( uv.x, 1.0 - uv.y );",
  394. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  395. "}"
  396. ].join("\n"),
  397. fragmentShader: [
  398. "uniform float screenWidth;",
  399. "uniform float screenHeight;",
  400. "uniform float sampleDistance;",
  401. "uniform float waveFactor;",
  402. "uniform sampler2D tDiffuse;",
  403. "varying vec2 vUv;",
  404. "void main() {",
  405. "vec4 color, org, tmp, add;",
  406. "float sample_dist, f;",
  407. "vec2 vin;",
  408. "vec2 uv = vUv;",
  409. "add += color = org = texture2D( tDiffuse, uv );",
  410. "vin = ( uv - vec2( 0.5 ) ) * vec2( 1.4 );",
  411. "sample_dist = dot( vin, vin ) * 2.0;",
  412. "f = ( waveFactor * 100.0 + sample_dist ) * sampleDistance * 4.0;",
  413. "vec2 sampleSize = vec2( 1.0 / screenWidth, 1.0 / screenHeight ) * vec2( f );",
  414. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.111964, 0.993712 ) * sampleSize );",
  415. "if( tmp.b < color.b ) color = tmp;",
  416. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.846724, 0.532032 ) * sampleSize );",
  417. "if( tmp.b < color.b ) color = tmp;",
  418. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.943883, -0.330279 ) * sampleSize );",
  419. "if( tmp.b < color.b ) color = tmp;",
  420. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.330279, -0.943883 ) * sampleSize );",
  421. "if( tmp.b < color.b ) color = tmp;",
  422. "add += tmp = texture2D( tDiffuse, uv + vec2( -0.532032, -0.846724 ) * sampleSize );",
  423. "if( tmp.b < color.b ) color = tmp;",
  424. "add += tmp = texture2D( tDiffuse, uv + vec2( -0.993712, -0.111964 ) * sampleSize );",
  425. "if( tmp.b < color.b ) color = tmp;",
  426. "add += tmp = texture2D( tDiffuse, uv + vec2( -0.707107, 0.707107 ) * sampleSize );",
  427. "if( tmp.b < color.b ) color = tmp;",
  428. "color = color * vec4( 2.0 ) - ( add / vec4( 8.0 ) );",
  429. "color = color + ( add / vec4( 8.0 ) - color ) * ( vec4( 1.0 ) - vec4( sample_dist * 0.5 ) );",
  430. "gl_FragColor = vec4( color.rgb * color.rgb * vec3( 0.95 ) + color.rgb, 1.0 );",
  431. "}"
  432. ].join("\n")
  433. },
  434. /* -------------------------------------------------------------------------
  435. // Simple test shader
  436. ------------------------------------------------------------------------- */
  437. 'basic': {
  438. uniforms: {},
  439. vertexShader: [
  440. "void main() {",
  441. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  442. "}"
  443. ].join("\n"),
  444. fragmentShader: [
  445. "void main() {",
  446. "gl_FragColor = vec4( 1.0, 0.0, 0.0, 0.5 );",
  447. "}"
  448. ].join("\n")
  449. },
  450. // METHODS
  451. buildKernel: function( sigma ) {
  452. // We lop off the sqrt(2 * pi) * sigma term, since we're going to normalize anyway.
  453. function gauss( x, sigma ) {
  454. return Math.exp( - ( x * x ) / ( 2.0 * sigma * sigma ) );
  455. }
  456. var i, values, sum, halfWidth, kMaxKernelSize = 25, kernelSize = 2 * Math.ceil( sigma * 3.0 ) + 1;
  457. if ( kernelSize > kMaxKernelSize ) kernelSize = kMaxKernelSize;
  458. halfWidth = ( kernelSize - 1 ) * 0.5
  459. values = new Array( kernelSize );
  460. sum = 0.0;
  461. for ( i = 0; i < kernelSize; ++i ) {
  462. values[ i ] = gauss( i - halfWidth, sigma );
  463. sum += values[ i ];
  464. }
  465. // normalize the kernel
  466. for ( i = 0; i < kernelSize; ++i ) values[ i ] /= sum;
  467. return values;
  468. }
  469. };