ShaderExtras.js 36 KB

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