ShaderExtras.js 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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 + verticalBlur
  20. * horizontalTiltShift + verticalTiltShift
  21. * blend
  22. * fxaa
  23. * luminosity
  24. */
  25. THREE.ShaderExtras = {
  26. /* -------------------------------------------------------------------------
  27. // Full-screen textured quad shader
  28. ------------------------------------------------------------------------- */
  29. 'screen': {
  30. uniforms: {
  31. tDiffuse: { type: "t", value: 0, texture: null },
  32. opacity: { type: "f", value: 1.0 }
  33. },
  34. vertexShader: [
  35. "varying vec2 vUv;",
  36. "void main() {",
  37. "vUv = vec2( uv.x, 1.0 - uv.y );",
  38. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  39. "}"
  40. ].join("\n"),
  41. fragmentShader: [
  42. "uniform float opacity;",
  43. "uniform sampler2D tDiffuse;",
  44. "varying vec2 vUv;",
  45. "void main() {",
  46. "vec4 texel = texture2D( tDiffuse, vUv );",
  47. "gl_FragColor = opacity * texel;",
  48. "}"
  49. ].join("\n")
  50. },
  51. /* ------------------------------------------------------------------------
  52. // Convolution shader
  53. // - ported from o3d sample to WebGL / GLSL
  54. // http://o3d.googlecode.com/svn/trunk/samples/convolution.html
  55. ------------------------------------------------------------------------ */
  56. 'convolution': {
  57. uniforms: {
  58. "tDiffuse" : { type: "t", value: 0, texture: null },
  59. "uImageIncrement" : { type: "v2", value: new THREE.Vector2( 0.001953125, 0.0 ) },
  60. "cKernel" : { type: "fv1", value: [] }
  61. },
  62. vertexShader: [
  63. //"#define KERNEL_SIZE 25.0",
  64. "uniform vec2 uImageIncrement;",
  65. "varying vec2 vUv;",
  66. "void main() {",
  67. "vUv = uv - ( ( KERNEL_SIZE - 1.0 ) / 2.0 ) * uImageIncrement;",
  68. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  69. "}"
  70. ].join("\n"),
  71. fragmentShader: [
  72. //"#define KERNEL_SIZE 25",
  73. "uniform float cKernel[ KERNEL_SIZE ];",
  74. "uniform sampler2D tDiffuse;",
  75. "uniform vec2 uImageIncrement;",
  76. "varying vec2 vUv;",
  77. "void main() {",
  78. "vec2 imageCoord = vUv;",
  79. "vec4 sum = vec4( 0.0, 0.0, 0.0, 0.0 );",
  80. "for( int i = 0; i < KERNEL_SIZE; i ++ ) {",
  81. "sum += texture2D( tDiffuse, imageCoord ) * cKernel[ i ];",
  82. "imageCoord += uImageIncrement;",
  83. "}",
  84. "gl_FragColor = sum;",
  85. "}"
  86. ].join("\n")
  87. },
  88. /* -------------------------------------------------------------------------
  89. // Film grain & scanlines shader
  90. // - ported from HLSL to WebGL / GLSL
  91. // http://www.truevision3d.com/forums/showcase/staticnoise_colorblackwhite_scanline_shaders-t18698.0.html
  92. // Screen Space Static Postprocessor
  93. //
  94. // Produces an analogue noise overlay similar to a film grain / TV static
  95. //
  96. // Original implementation and noise algorithm
  97. // Pat 'Hawthorne' Shearon
  98. //
  99. // Optimized scanlines + noise version with intensity scaling
  100. // Georg 'Leviathan' Steinrohder
  101. // This version is provided under a Creative Commons Attribution 3.0 License
  102. // http://creativecommons.org/licenses/by/3.0/
  103. ------------------------------------------------------------------------- */
  104. 'film': {
  105. uniforms: {
  106. tDiffuse: { type: "t", value: 0, texture: null },
  107. time: { type: "f", value: 0.0 },
  108. nIntensity: { type: "f", value: 0.5 },
  109. sIntensity: { type: "f", value: 0.05 },
  110. sCount: { type: "f", value: 4096 },
  111. grayscale: { type: "i", value: 1 }
  112. },
  113. vertexShader: [
  114. "varying vec2 vUv;",
  115. "void main() {",
  116. "vUv = vec2( uv.x, 1.0 - uv.y );",
  117. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  118. "}"
  119. ].join("\n"),
  120. fragmentShader: [
  121. // control parameter
  122. "uniform float time;",
  123. "uniform bool grayscale;",
  124. // noise effect intensity value (0 = no effect, 1 = full effect)
  125. "uniform float nIntensity;",
  126. // scanlines effect intensity value (0 = no effect, 1 = full effect)
  127. "uniform float sIntensity;",
  128. // scanlines effect count value (0 = no effect, 4096 = full effect)
  129. "uniform float sCount;",
  130. "uniform sampler2D tDiffuse;",
  131. "varying vec2 vUv;",
  132. "void main() {",
  133. // sample the source
  134. "vec4 cTextureScreen = texture2D( tDiffuse, vUv );",
  135. // make some noise
  136. "float x = vUv.x * vUv.y * time * 1000.0;",
  137. "x = mod( x, 13.0 ) * mod( x, 123.0 );",
  138. "float dx = mod( x, 0.01 );",
  139. // add noise
  140. "vec3 cResult = cTextureScreen.rgb + cTextureScreen.rgb * clamp( 0.1 + dx * 100.0, 0.0, 1.0 );",
  141. // get us a sine and cosine
  142. "vec2 sc = vec2( sin( vUv.y * sCount ), cos( vUv.y * sCount ) );",
  143. // add scanlines
  144. "cResult += cTextureScreen.rgb * vec3( sc.x, sc.y, sc.x ) * sIntensity;",
  145. // interpolate between source and result by intensity
  146. "cResult = cTextureScreen.rgb + clamp( nIntensity, 0.0,1.0 ) * ( cResult - cTextureScreen.rgb );",
  147. // convert to grayscale if desired
  148. "if( grayscale ) {",
  149. "cResult = vec3( cResult.r * 0.3 + cResult.g * 0.59 + cResult.b * 0.11 );",
  150. "}",
  151. "gl_FragColor = vec4( cResult, cTextureScreen.a );",
  152. "}"
  153. ].join("\n")
  154. },
  155. /* -------------------------------------------------------------------------
  156. // Depth-of-field shader with bokeh
  157. // ported from GLSL shader by Martins Upitis
  158. // http://artmartinsh.blogspot.com/2010/02/glsl-lens-blur-filter-with-bokeh.html
  159. ------------------------------------------------------------------------- */
  160. 'bokeh' : {
  161. uniforms: { tColor: { type: "t", value: 0, texture: null },
  162. tDepth: { type: "t", value: 1, texture: null },
  163. focus: { type: "f", value: 1.0 },
  164. aspect: { type: "f", value: 1.0 },
  165. aperture: { type: "f", value: 0.025 },
  166. maxblur: { type: "f", value: 1.0 },
  167. },
  168. vertexShader: [
  169. "varying vec2 vUv;",
  170. "void main() {",
  171. "vUv = vec2( uv.x, 1.0 - uv.y );",
  172. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  173. "}"
  174. ].join("\n"),
  175. fragmentShader: [
  176. "varying vec2 vUv;",
  177. "uniform sampler2D tColor;",
  178. "uniform sampler2D tDepth;",
  179. "uniform float maxblur;", // max blur amount
  180. "uniform float aperture;", // aperture - bigger values for shallower depth of field
  181. "uniform float focus;",
  182. "uniform float aspect;",
  183. "void main() {",
  184. "vec2 aspectcorrect = vec2( 1.0, aspect );",
  185. "vec4 depth1 = texture2D( tDepth, vUv );",
  186. "float factor = depth1.x - focus;",
  187. "vec2 dofblur = vec2 ( clamp( factor * aperture, -maxblur, maxblur ) );",
  188. "vec2 dofblur9 = dofblur * 0.9;",
  189. "vec2 dofblur7 = dofblur * 0.7;",
  190. "vec2 dofblur4 = dofblur * 0.4;",
  191. "vec4 col = vec4( 0.0 );",
  192. "col += texture2D( tColor, vUv.xy );",
  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.40, 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.0, -0.4 ) * aspectcorrect ) * dofblur );",
  202. "col += texture2D( tColor, vUv.xy + ( vec2( -0.15, 0.37 ) * aspectcorrect ) * dofblur );",
  203. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, 0.29 ) * aspectcorrect ) * dofblur );",
  204. "col += texture2D( tColor, vUv.xy + ( vec2( 0.37, 0.15 ) * aspectcorrect ) * dofblur );",
  205. "col += texture2D( tColor, vUv.xy + ( vec2( -0.4, 0.0 ) * aspectcorrect ) * dofblur );",
  206. "col += texture2D( tColor, vUv.xy + ( vec2( -0.37, -0.15 ) * aspectcorrect ) * dofblur );",
  207. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, -0.29 ) * aspectcorrect ) * dofblur );",
  208. "col += texture2D( tColor, vUv.xy + ( vec2( 0.15, -0.37 ) * aspectcorrect ) * dofblur );",
  209. "col += texture2D( tColor, vUv.xy + ( vec2( 0.15, 0.37 ) * aspectcorrect ) * dofblur9 );",
  210. "col += texture2D( tColor, vUv.xy + ( vec2( -0.37, 0.15 ) * aspectcorrect ) * dofblur9 );",
  211. "col += texture2D( tColor, vUv.xy + ( vec2( 0.37, -0.15 ) * aspectcorrect ) * dofblur9 );",
  212. "col += texture2D( tColor, vUv.xy + ( vec2( -0.15, -0.37 ) * aspectcorrect ) * dofblur9 );",
  213. "col += texture2D( tColor, vUv.xy + ( vec2( -0.15, 0.37 ) * aspectcorrect ) * dofblur9 );",
  214. "col += texture2D( tColor, vUv.xy + ( vec2( 0.37, 0.15 ) * aspectcorrect ) * dofblur9 );",
  215. "col += texture2D( tColor, vUv.xy + ( vec2( -0.37, -0.15 ) * aspectcorrect ) * dofblur9 );",
  216. "col += texture2D( tColor, vUv.xy + ( vec2( 0.15, -0.37 ) * aspectcorrect ) * dofblur9 );",
  217. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, 0.29 ) * aspectcorrect ) * dofblur7 );",
  218. "col += texture2D( tColor, vUv.xy + ( vec2( 0.40, 0.0 ) * aspectcorrect ) * dofblur7 );",
  219. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, -0.29 ) * aspectcorrect ) * dofblur7 );",
  220. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, -0.4 ) * aspectcorrect ) * dofblur7 );",
  221. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, 0.29 ) * aspectcorrect ) * dofblur7 );",
  222. "col += texture2D( tColor, vUv.xy + ( vec2( -0.4, 0.0 ) * aspectcorrect ) * dofblur7 );",
  223. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, -0.29 ) * aspectcorrect ) * dofblur7 );",
  224. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, 0.4 ) * aspectcorrect ) * dofblur7 );",
  225. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, 0.29 ) * aspectcorrect ) * dofblur4 );",
  226. "col += texture2D( tColor, vUv.xy + ( vec2( 0.4, 0.0 ) * aspectcorrect ) * dofblur4 );",
  227. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, -0.29 ) * aspectcorrect ) * dofblur4 );",
  228. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, -0.4 ) * aspectcorrect ) * dofblur4 );",
  229. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, 0.29 ) * aspectcorrect ) * dofblur4 );",
  230. "col += texture2D( tColor, vUv.xy + ( vec2( -0.4, 0.0 ) * aspectcorrect ) * dofblur4 );",
  231. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, -0.29 ) * aspectcorrect ) * dofblur4 );",
  232. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, 0.4 ) * aspectcorrect ) * dofblur4 );",
  233. "gl_FragColor = col / 41.0;",
  234. "gl_FragColor.a = 1.0;",
  235. "}"
  236. ].join("\n")
  237. },
  238. /* -------------------------------------------------------------------------
  239. // Depth-of-field shader using mipmaps
  240. // - from Matt Handley @applmak
  241. // - requires power-of-2 sized render target with enabled mipmaps
  242. ------------------------------------------------------------------------- */
  243. 'dofmipmap': {
  244. uniforms: {
  245. tColor: { type: "t", value: 0, texture: null },
  246. tDepth: { type: "t", value: 1, texture: null },
  247. focus: { type: "f", value: 1.0 },
  248. maxblur: { type: "f", value: 1.0 }
  249. },
  250. vertexShader: [
  251. "varying vec2 vUv;",
  252. "void main() {",
  253. "vUv = vec2( uv.x, 1.0 - uv.y );",
  254. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  255. "}"
  256. ].join("\n"),
  257. fragmentShader: [
  258. "uniform float focus;",
  259. "uniform float maxblur;",
  260. "uniform sampler2D tColor;",
  261. "uniform sampler2D tDepth;",
  262. "varying vec2 vUv;",
  263. "void main() {",
  264. "vec4 depth = texture2D( tDepth, vUv );",
  265. "float factor = depth.x - focus;",
  266. "vec4 col = texture2D( tColor, vUv, 2.0 * maxblur * abs( focus - depth.x ) );",
  267. "gl_FragColor = col;",
  268. "gl_FragColor.a = 1.0;",
  269. "}"
  270. ].join("\n")
  271. },
  272. /* -------------------------------------------------------------------------
  273. // Sepia tone shader
  274. // - based on glfx.js sepia shader
  275. // https://github.com/evanw/glfx.js
  276. ------------------------------------------------------------------------- */
  277. 'sepia': {
  278. uniforms: {
  279. tDiffuse: { type: "t", value: 0, texture: null },
  280. amount: { type: "f", value: 1.0 }
  281. },
  282. vertexShader: [
  283. "varying vec2 vUv;",
  284. "void main() {",
  285. "vUv = vec2( uv.x, 1.0 - uv.y );",
  286. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  287. "}"
  288. ].join("\n"),
  289. fragmentShader: [
  290. "uniform float amount;",
  291. "uniform sampler2D tDiffuse;",
  292. "varying vec2 vUv;",
  293. "void main() {",
  294. "vec4 color = texture2D( tDiffuse, vUv );",
  295. "vec3 c = color.rgb;",
  296. "color.r = dot( c, vec3( 1.0 - 0.607 * amount, 0.769 * amount, 0.189 * amount ) );",
  297. "color.g = dot( c, vec3( 0.349 * amount, 1.0 - 0.314 * amount, 0.168 * amount ) );",
  298. "color.b = dot( c, vec3( 0.272 * amount, 0.534 * amount, 1.0 - 0.869 * amount ) );",
  299. "gl_FragColor = vec4( min( vec3( 1.0 ), color.rgb ), color.a );",
  300. "}"
  301. ].join("\n")
  302. },
  303. /* -------------------------------------------------------------------------
  304. // Dot screen shader
  305. // - based on glfx.js sepia shader
  306. // https://github.com/evanw/glfx.js
  307. ------------------------------------------------------------------------- */
  308. 'dotscreen': {
  309. uniforms: {
  310. tDiffuse: { type: "t", value: 0, texture: null },
  311. tSize: { type: "v2", value: new THREE.Vector2( 256, 256 ) },
  312. center: { type: "v2", value: new THREE.Vector2( 0.5, 0.5 ) },
  313. angle: { type: "f", value: 1.57 },
  314. scale: { type: "f", value: 1.0 }
  315. },
  316. vertexShader: [
  317. "varying vec2 vUv;",
  318. "void main() {",
  319. "vUv = vec2( uv.x, 1.0 - uv.y );",
  320. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  321. "}"
  322. ].join("\n"),
  323. fragmentShader: [
  324. "uniform vec2 center;",
  325. "uniform float angle;",
  326. "uniform float scale;",
  327. "uniform vec2 tSize;",
  328. "uniform sampler2D tDiffuse;",
  329. "varying vec2 vUv;",
  330. "float pattern() {",
  331. "float s = sin( angle ), c = cos( angle );",
  332. "vec2 tex = vUv * tSize - center;",
  333. "vec2 point = vec2( c * tex.x - s * tex.y, s * tex.x + c * tex.y ) * scale;",
  334. "return ( sin( point.x ) * sin( point.y ) ) * 4.0;",
  335. "}",
  336. "void main() {",
  337. "vec4 color = texture2D( tDiffuse, vUv );",
  338. "float average = ( color.r + color.g + color.b ) / 3.0;",
  339. "gl_FragColor = vec4( vec3( average * 10.0 - 5.0 + pattern() ), color.a );",
  340. "}"
  341. ].join("\n")
  342. },
  343. /* ------------------------------------------------------------------------------------------------
  344. // Vignette shader
  345. // - based on PaintEffect postprocess from ro.me
  346. // http://code.google.com/p/3-dreams-of-black/source/browse/deploy/js/effects/PaintEffect.js
  347. ------------------------------------------------------------------------------------------------ */
  348. 'vignette': {
  349. uniforms: {
  350. tDiffuse: { type: "t", value: 0, texture: null },
  351. offset: { type: "f", value: 1.0 },
  352. darkness: { type: "f", value: 1.0 }
  353. },
  354. vertexShader: [
  355. "varying vec2 vUv;",
  356. "void main() {",
  357. "vUv = vec2( uv.x, 1.0 - uv.y );",
  358. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  359. "}"
  360. ].join("\n"),
  361. fragmentShader: [
  362. "uniform float offset;",
  363. "uniform float darkness;",
  364. "uniform sampler2D tDiffuse;",
  365. "varying vec2 vUv;",
  366. "void main() {",
  367. // Eskil's vignette
  368. "vec4 texel = texture2D( tDiffuse, vUv );",
  369. "vec2 uv = ( vUv - vec2( 0.5 ) ) * vec2( offset );",
  370. "gl_FragColor = vec4( mix( texel.rgb, vec3( 1.0 - darkness ), dot( uv, uv ) ), texel.a );",
  371. /*
  372. // alternative version from glfx.js
  373. // this one makes more "dusty" look (as opposed to "burned")
  374. "vec4 color = texture2D( tDiffuse, vUv );",
  375. "float dist = distance( vUv, vec2( 0.5 ) );",
  376. "color.rgb *= smoothstep( 0.8, offset * 0.799, dist *( darkness + offset ) );",
  377. "gl_FragColor = color;",
  378. */
  379. "}"
  380. ].join("\n")
  381. },
  382. /* -------------------------------------------------------------------------
  383. // Bleach bypass shader [http://en.wikipedia.org/wiki/Bleach_bypass]
  384. // - based on Nvidia example
  385. // http://developer.download.nvidia.com/shaderlibrary/webpages/shader_library.html#post_bleach_bypass
  386. ------------------------------------------------------------------------- */
  387. 'bleachbypass': {
  388. uniforms: {
  389. tDiffuse: { type: "t", value: 0, texture: null },
  390. opacity: { type: "f", value: 1.0 }
  391. },
  392. vertexShader: [
  393. "varying vec2 vUv;",
  394. "void main() {",
  395. "vUv = vec2( uv.x, 1.0 - uv.y );",
  396. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  397. "}"
  398. ].join("\n"),
  399. fragmentShader: [
  400. "uniform float opacity;",
  401. "uniform sampler2D tDiffuse;",
  402. "varying vec2 vUv;",
  403. "void main() {",
  404. "vec4 base = texture2D( tDiffuse, vUv );",
  405. "vec3 lumCoeff = vec3( 0.25, 0.65, 0.1 );",
  406. "float lum = dot( lumCoeff, base.rgb );",
  407. "vec3 blend = vec3( lum );",
  408. "float L = min( 1.0, max( 0.0, 10.0 * ( lum - 0.45 ) ) );",
  409. "vec3 result1 = 2.0 * base.rgb * blend;",
  410. "vec3 result2 = 1.0 - 2.0 * ( 1.0 - blend ) * ( 1.0 - base.rgb );",
  411. "vec3 newColor = mix( result1, result2, L );",
  412. "float A2 = opacity * base.a;",
  413. "vec3 mixRGB = A2 * newColor.rgb;",
  414. "mixRGB += ( ( 1.0 - A2 ) * base.rgb );",
  415. "gl_FragColor = vec4( mixRGB, base.a );",
  416. "}"
  417. ].join("\n")
  418. },
  419. /* --------------------------------------------------------------------------------------------------
  420. // Focus shader
  421. // - based on PaintEffect postprocess from ro.me
  422. // http://code.google.com/p/3-dreams-of-black/source/browse/deploy/js/effects/PaintEffect.js
  423. -------------------------------------------------------------------------------------------------- */
  424. 'focus': {
  425. uniforms : {
  426. "tDiffuse": { type: "t", value: 0, texture: null },
  427. "screenWidth": { type: "f", value: 1024 },
  428. "screenHeight": { type: "f", value: 1024 },
  429. "sampleDistance": { type: "f", value: 0.94 },
  430. "waveFactor": { type: "f", value: 0.00125 }
  431. },
  432. vertexShader: [
  433. "varying vec2 vUv;",
  434. "void main() {",
  435. "vUv = vec2( uv.x, 1.0 - uv.y );",
  436. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  437. "}"
  438. ].join("\n"),
  439. fragmentShader: [
  440. "uniform float screenWidth;",
  441. "uniform float screenHeight;",
  442. "uniform float sampleDistance;",
  443. "uniform float waveFactor;",
  444. "uniform sampler2D tDiffuse;",
  445. "varying vec2 vUv;",
  446. "void main() {",
  447. "vec4 color, org, tmp, add;",
  448. "float sample_dist, f;",
  449. "vec2 vin;",
  450. "vec2 uv = vUv;",
  451. "add += color = org = texture2D( tDiffuse, uv );",
  452. "vin = ( uv - vec2( 0.5 ) ) * vec2( 1.4 );",
  453. "sample_dist = dot( vin, vin ) * 2.0;",
  454. "f = ( waveFactor * 100.0 + sample_dist ) * sampleDistance * 4.0;",
  455. "vec2 sampleSize = vec2( 1.0 / screenWidth, 1.0 / screenHeight ) * vec2( f );",
  456. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.111964, 0.993712 ) * sampleSize );",
  457. "if( tmp.b < color.b ) color = tmp;",
  458. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.846724, 0.532032 ) * sampleSize );",
  459. "if( tmp.b < color.b ) color = tmp;",
  460. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.943883, -0.330279 ) * sampleSize );",
  461. "if( tmp.b < color.b ) color = tmp;",
  462. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.330279, -0.943883 ) * sampleSize );",
  463. "if( tmp.b < color.b ) color = tmp;",
  464. "add += tmp = texture2D( tDiffuse, uv + vec2( -0.532032, -0.846724 ) * sampleSize );",
  465. "if( tmp.b < color.b ) color = tmp;",
  466. "add += tmp = texture2D( tDiffuse, uv + vec2( -0.993712, -0.111964 ) * sampleSize );",
  467. "if( tmp.b < color.b ) color = tmp;",
  468. "add += tmp = texture2D( tDiffuse, uv + vec2( -0.707107, 0.707107 ) * sampleSize );",
  469. "if( tmp.b < color.b ) color = tmp;",
  470. "color = color * vec4( 2.0 ) - ( add / vec4( 8.0 ) );",
  471. "color = color + ( add / vec4( 8.0 ) - color ) * ( vec4( 1.0 ) - vec4( sample_dist * 0.5 ) );",
  472. "gl_FragColor = vec4( color.rgb * color.rgb * vec3( 0.95 ) + color.rgb, 1.0 );",
  473. "}"
  474. ].join("\n")
  475. },
  476. /* -------------------------------------------------------------------------
  477. // Triangle blur shader
  478. // - based on glfx.js triangle blur shader
  479. // https://github.com/evanw/glfx.js
  480. // A basic blur filter, which convolves the image with a
  481. // pyramid filter. The pyramid filter is separable and is applied as two
  482. // perpendicular triangle filters.
  483. ------------------------------------------------------------------------- */
  484. 'triangleBlur': {
  485. uniforms : {
  486. "texture": { type: "t", value: 0, texture: null },
  487. "delta": { type: "v2", value:new THREE.Vector2( 1, 1 ) }
  488. },
  489. vertexShader: [
  490. "varying vec2 vUv;",
  491. "void main() {",
  492. "vUv = vec2( uv.x, 1.0 - uv.y );",
  493. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  494. "}"
  495. ].join("\n"),
  496. fragmentShader: [
  497. "#define ITERATIONS 10.0",
  498. "uniform sampler2D texture;",
  499. "uniform vec2 delta;",
  500. "varying vec2 vUv;",
  501. "float random( vec3 scale, float seed ) {",
  502. // use the fragment position for a different seed per-pixel
  503. "return fract( sin( dot( gl_FragCoord.xyz + seed, scale ) ) * 43758.5453 + seed );",
  504. "}",
  505. "void main() {",
  506. "vec4 color = vec4( 0.0 );",
  507. "float total = 0.0;",
  508. // randomize the lookup values to hide the fixed number of samples
  509. "float offset = random( vec3( 12.9898, 78.233, 151.7182 ), 0.0 );",
  510. "for ( float t = -ITERATIONS; t <= ITERATIONS; t ++ ) {",
  511. "float percent = ( t + offset - 0.5 ) / ITERATIONS;",
  512. "float weight = 1.0 - abs( percent );",
  513. "color += texture2D( texture, vUv + delta * percent ) * weight;",
  514. "total += weight;",
  515. "}",
  516. "gl_FragColor = color / total;",
  517. "}",
  518. ].join("\n")
  519. },
  520. /* -------------------------------------------------------------------------
  521. // Simple test shader
  522. ------------------------------------------------------------------------- */
  523. 'basic': {
  524. uniforms: {},
  525. vertexShader: [
  526. "void main() {",
  527. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  528. "}"
  529. ].join("\n"),
  530. fragmentShader: [
  531. "void main() {",
  532. "gl_FragColor = vec4( 1.0, 0.0, 0.0, 0.5 );",
  533. "}"
  534. ].join("\n")
  535. },
  536. /* --------------------------------------------------------------------------------------------------
  537. // Two pass Gaussian blur filter (horizontal and vertical blur shaders)
  538. // - described in http://www.gamerendering.com/2008/10/11/gaussian-blur-filter-shader/
  539. // and used in http://www.cake23.de/traveling-wavefronts-lit-up.html
  540. //
  541. // - 9 samples per pass
  542. // - standard deviation 2.7
  543. // - "h" and "v" parameters should be set to "1 / width" and "1 / height"
  544. -------------------------------------------------------------------------------------------------- */
  545. 'horizontalBlur': {
  546. uniforms: {
  547. "tDiffuse": { type: "t", value: 0, texture: null },
  548. "h": { type: "f", value: 1.0 / 512.0 }
  549. },
  550. vertexShader: [
  551. "varying vec2 vUv;",
  552. "void main() {",
  553. "vUv = vec2( uv.x, 1.0 - uv.y );",
  554. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  555. "}"
  556. ].join("\n"),
  557. fragmentShader: [
  558. "uniform sampler2D tDiffuse;",
  559. "uniform float h;",
  560. "varying vec2 vUv;",
  561. "void main() {",
  562. "vec4 sum = vec4( 0.0 );",
  563. "sum += texture2D( tDiffuse, vec2( vUv.x - 4.0 * h, vUv.y ) ) * 0.051;",
  564. "sum += texture2D( tDiffuse, vec2( vUv.x - 3.0 * h, vUv.y ) ) * 0.0918;",
  565. "sum += texture2D( tDiffuse, vec2( vUv.x - 2.0 * h, vUv.y ) ) * 0.12245;",
  566. "sum += texture2D( tDiffuse, vec2( vUv.x - 1.0 * h, vUv.y ) ) * 0.1531;",
  567. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",
  568. "sum += texture2D( tDiffuse, vec2( vUv.x + 1.0 * h, vUv.y ) ) * 0.1531;",
  569. "sum += texture2D( tDiffuse, vec2( vUv.x + 2.0 * h, vUv.y ) ) * 0.12245;",
  570. "sum += texture2D( tDiffuse, vec2( vUv.x + 3.0 * h, vUv.y ) ) * 0.0918;",
  571. "sum += texture2D( tDiffuse, vec2( vUv.x + 4.0 * h, vUv.y ) ) * 0.051;",
  572. "gl_FragColor = sum;",
  573. "}"
  574. ].join("\n")
  575. },
  576. 'verticalBlur': {
  577. uniforms: {
  578. "tDiffuse": { type: "t", value: 0, texture: null },
  579. "v": { type: "f", value: 1.0 / 512.0 }
  580. },
  581. vertexShader: [
  582. "varying vec2 vUv;",
  583. "void main() {",
  584. "vUv = vec2( uv.x, 1.0 - uv.y );",
  585. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  586. "}"
  587. ].join("\n"),
  588. fragmentShader: [
  589. "uniform sampler2D tDiffuse;",
  590. "uniform float v;",
  591. "varying vec2 vUv;",
  592. "void main() {",
  593. "vec4 sum = vec4( 0.0 );",
  594. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 4.0 * v ) ) * 0.051;",
  595. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 3.0 * v ) ) * 0.0918;",
  596. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 2.0 * v ) ) * 0.12245;",
  597. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 1.0 * v ) ) * 0.1531;",
  598. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",
  599. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 1.0 * v ) ) * 0.1531;",
  600. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 2.0 * v ) ) * 0.12245;",
  601. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 3.0 * v ) ) * 0.0918;",
  602. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 4.0 * v ) ) * 0.051;",
  603. "gl_FragColor = sum;",
  604. "}"
  605. ].join("\n")
  606. },
  607. /* --------------------------------------------------------------------------------------------------
  608. // Simple fake tilt-shift effect, modulating two pass Gaussian blur (see above) by vertical position
  609. //
  610. // - 9 samples per pass
  611. // - standard deviation 2.7
  612. // - "h" and "v" parameters should be set to "1 / width" and "1 / height"
  613. // - "r" parameter control where "focused" horizontal line lies
  614. -------------------------------------------------------------------------------------------------- */
  615. 'horizontalTiltShift': {
  616. uniforms: {
  617. "tDiffuse": { type: "t", value: 0, texture: null },
  618. "h": { type: "f", value: 1.0 / 512.0 },
  619. "r": { type: "f", value: 0.35 }
  620. },
  621. vertexShader: [
  622. "varying vec2 vUv;",
  623. "void main() {",
  624. "vUv = vec2( uv.x, 1.0 - uv.y );",
  625. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  626. "}"
  627. ].join("\n"),
  628. fragmentShader: [
  629. "uniform sampler2D tDiffuse;",
  630. "uniform float h;",
  631. "uniform float r;",
  632. "varying vec2 vUv;",
  633. "void main() {",
  634. "vec4 sum = vec4( 0.0 );",
  635. "float hh = h * abs( r - vUv.y );",
  636. "sum += texture2D( tDiffuse, vec2( vUv.x - 4.0 * hh, vUv.y ) ) * 0.051;",
  637. "sum += texture2D( tDiffuse, vec2( vUv.x - 3.0 * hh, vUv.y ) ) * 0.0918;",
  638. "sum += texture2D( tDiffuse, vec2( vUv.x - 2.0 * hh, vUv.y ) ) * 0.12245;",
  639. "sum += texture2D( tDiffuse, vec2( vUv.x - 1.0 * hh, vUv.y ) ) * 0.1531;",
  640. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",
  641. "sum += texture2D( tDiffuse, vec2( vUv.x + 1.0 * hh, vUv.y ) ) * 0.1531;",
  642. "sum += texture2D( tDiffuse, vec2( vUv.x + 2.0 * hh, vUv.y ) ) * 0.12245;",
  643. "sum += texture2D( tDiffuse, vec2( vUv.x + 3.0 * hh, vUv.y ) ) * 0.0918;",
  644. "sum += texture2D( tDiffuse, vec2( vUv.x + 4.0 * hh, vUv.y ) ) * 0.051;",
  645. "gl_FragColor = sum;",
  646. "}"
  647. ].join("\n")
  648. },
  649. 'verticalTiltShift': {
  650. uniforms: {
  651. "tDiffuse": { type: "t", value: 0, texture: null },
  652. "v": { type: "f", value: 1.0 / 512.0 },
  653. "r": { type: "f", value: 0.35 }
  654. },
  655. vertexShader: [
  656. "varying vec2 vUv;",
  657. "void main() {",
  658. "vUv = vec2( uv.x, 1.0 - uv.y );",
  659. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  660. "}"
  661. ].join("\n"),
  662. fragmentShader: [
  663. "uniform sampler2D tDiffuse;",
  664. "uniform float v;",
  665. "uniform float r;",
  666. "varying vec2 vUv;",
  667. "void main() {",
  668. "vec4 sum = vec4( 0.0 );",
  669. "float vv = v * abs( r - vUv.y );",
  670. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 4.0 * vv ) ) * 0.051;",
  671. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 3.0 * vv ) ) * 0.0918;",
  672. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 2.0 * vv ) ) * 0.12245;",
  673. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 1.0 * vv ) ) * 0.1531;",
  674. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",
  675. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 1.0 * vv ) ) * 0.1531;",
  676. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 2.0 * vv ) ) * 0.12245;",
  677. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 3.0 * vv ) ) * 0.0918;",
  678. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 4.0 * vv ) ) * 0.051;",
  679. "gl_FragColor = sum;",
  680. "}"
  681. ].join("\n")
  682. },
  683. /* -------------------------------------------------------------------------
  684. // Blend two textures
  685. ------------------------------------------------------------------------- */
  686. 'blend': {
  687. uniforms: {
  688. tDiffuse1: { type: "t", value: 0, texture: null },
  689. tDiffuse2: { type: "t", value: 1, texture: null },
  690. mixRatio: { type: "f", value: 0.5 },
  691. opacity: { type: "f", value: 1.0 }
  692. },
  693. vertexShader: [
  694. "varying vec2 vUv;",
  695. "void main() {",
  696. "vUv = vec2( uv.x, 1.0 - uv.y );",
  697. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  698. "}"
  699. ].join("\n"),
  700. fragmentShader: [
  701. "uniform float opacity;",
  702. "uniform float mixRatio;",
  703. "uniform sampler2D tDiffuse1;",
  704. "uniform sampler2D tDiffuse2;",
  705. "varying vec2 vUv;",
  706. "void main() {",
  707. "vec4 texel1 = texture2D( tDiffuse1, vUv );",
  708. "vec4 texel2 = texture2D( tDiffuse2, vUv );",
  709. "gl_FragColor = opacity * mix( texel1, texel2, mixRatio );",
  710. "}"
  711. ].join("\n")
  712. },
  713. /* -------------------------------------------------------------------------
  714. // NVIDIA FXAA by Timothy Lottes
  715. // http://timothylottes.blogspot.com/2011/06/fxaa3-source-released.html
  716. // - WebGL port by @supereggbert
  717. // http://www.glge.org/demos/fxaa/
  718. ------------------------------------------------------------------------- */
  719. 'fxaa': {
  720. uniforms: {
  721. "tDiffuse": { type: "t", value: 0, texture: null },
  722. "resolution": { type: "v2", value: new THREE.Vector2( 1 / 1024, 1 / 512 ) }
  723. },
  724. vertexShader: [
  725. "varying vec2 vUv;",
  726. "void main() {",
  727. "vUv = vec2( uv.x, 1.0 - uv.y );",
  728. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  729. "}"
  730. ].join("\n"),
  731. fragmentShader: [
  732. "uniform sampler2D tDiffuse;",
  733. "uniform vec2 resolution;",
  734. "varying vec2 vUv;",
  735. "#define FXAA_REDUCE_MIN (1.0/128.0)",
  736. "#define FXAA_REDUCE_MUL (1.0/8.0)",
  737. "#define FXAA_SPAN_MAX 8.0",
  738. "void main() {",
  739. "vec3 rgbNW = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( -1.0, -1.0 ) ) * resolution ).xyz;",
  740. "vec3 rgbNE = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( 1.0, -1.0 ) ) * resolution ).xyz;",
  741. "vec3 rgbSW = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( -1.0, 1.0 ) ) * resolution ).xyz;",
  742. "vec3 rgbSE = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( 1.0, 1.0 ) ) * resolution ).xyz;",
  743. "vec3 rgbM = texture2D( tDiffuse, gl_FragCoord.xy * resolution ).xyz;",
  744. "vec3 luma = vec3( 0.299, 0.587, 0.114 );",
  745. "float lumaNW = dot( rgbNW, luma );",
  746. "float lumaNE = dot( rgbNE, luma );",
  747. "float lumaSW = dot( rgbSW, luma );",
  748. "float lumaSE = dot( rgbSE, luma );",
  749. "float lumaM = dot( rgbM, luma );",
  750. "float lumaMin = min( lumaM, min( min( lumaNW, lumaNE ), min( lumaSW, lumaSE ) ) );",
  751. "float lumaMax = max( lumaM, max( max( lumaNW, lumaNE) , max( lumaSW, lumaSE ) ) );",
  752. "vec2 dir;",
  753. "dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));",
  754. "dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));",
  755. "float dirReduce = max( ( lumaNW + lumaNE + lumaSW + lumaSE ) * ( 0.25 * FXAA_REDUCE_MUL ), FXAA_REDUCE_MIN );",
  756. "float rcpDirMin = 1.0 / ( min( abs( dir.x ), abs( dir.y ) ) + dirReduce );",
  757. "dir = min( vec2( FXAA_SPAN_MAX, FXAA_SPAN_MAX),",
  758. "max( vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),",
  759. "dir * rcpDirMin)) * resolution;",
  760. "vec3 rgbA = 0.5 * (",
  761. "texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * ( 1.0 / 3.0 - 0.5 ) ).xyz +",
  762. "texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * ( 2.0 / 3.0 - 0.5 ) ).xyz );",
  763. "vec3 rgbB = rgbA * 0.5 + 0.25 * (",
  764. "texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * -0.5 ).xyz +",
  765. "texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * 0.5 ).xyz );",
  766. "float lumaB = dot( rgbB, luma );",
  767. "if ( ( lumaB < lumaMin ) || ( lumaB > lumaMax ) ) {",
  768. "gl_FragColor = vec4( rgbA, 1.0 );",
  769. "} else {",
  770. "gl_FragColor = vec4( rgbB, 1.0 );",
  771. "}",
  772. "}",
  773. ].join("\n"),
  774. },
  775. /* -------------------------------------------------------------------------
  776. // Luminosity
  777. // http://en.wikipedia.org/wiki/Luminosity
  778. ------------------------------------------------------------------------- */
  779. 'luminosity': {
  780. uniforms: {
  781. "tDiffuse": { type: "t", value: 0, texture: null }
  782. },
  783. vertexShader: [
  784. "varying vec2 vUv;",
  785. "void main() {",
  786. "vUv = vec2( uv.x, 1.0 - uv.y );",
  787. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  788. "}"
  789. ].join("\n"),
  790. fragmentShader: [
  791. "uniform sampler2D tDiffuse;",
  792. "varying vec2 vUv;",
  793. "void main() {",
  794. "vec4 texel = texture2D( tDiffuse, vUv );",
  795. "vec3 luma = vec3( 0.299, 0.587, 0.114 );",
  796. "float v = dot( texel.xyz, luma );",
  797. "gl_FragColor = vec4( v, v, v, texel.w );",
  798. "}"
  799. ].join("\n")
  800. },
  801. // METHODS
  802. buildKernel: function( sigma ) {
  803. // We lop off the sqrt(2 * pi) * sigma term, since we're going to normalize anyway.
  804. function gauss( x, sigma ) {
  805. return Math.exp( - ( x * x ) / ( 2.0 * sigma * sigma ) );
  806. }
  807. var i, values, sum, halfWidth, kMaxKernelSize = 25, kernelSize = 2 * Math.ceil( sigma * 3.0 ) + 1;
  808. if ( kernelSize > kMaxKernelSize ) kernelSize = kMaxKernelSize;
  809. halfWidth = ( kernelSize - 1 ) * 0.5
  810. values = new Array( kernelSize );
  811. sum = 0.0;
  812. for ( i = 0; i < kernelSize; ++i ) {
  813. values[ i ] = gauss( i - halfWidth, sigma );
  814. sum += values[ i ];
  815. }
  816. // normalize the kernel
  817. for ( i = 0; i < kernelSize; ++i ) values[ i ] /= sum;
  818. return values;
  819. }
  820. };