ShaderExtras.js 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author zz85 / http://www.lab4games.net/zz85/blog
  4. * @author davidedc / http://www.sketchpatch.net/
  5. *
  6. * ShaderExtras currently contains:
  7. *
  8. * screen
  9. * convolution
  10. * film
  11. * bokeh
  12. * sepia
  13. * dotscreen
  14. * vignette
  15. * bleachbypass
  16. * basic
  17. * dofmipmap
  18. * focus
  19. * triangleBlur
  20. * horizontalBlur + verticalBlur
  21. * horizontalTiltShift + verticalTiltShift
  22. * blend
  23. * fxaa
  24. * luminosity
  25. * colorCorrection
  26. * normalmap
  27. * ssao
  28. * colorify
  29. * unpackDepthRGBA
  30. */
  31. THREE.ShaderExtras = {
  32. /* -------------------------------------------------------------------------
  33. // Full-screen textured quad shader
  34. ------------------------------------------------------------------------- */
  35. 'screen': {
  36. uniforms: {
  37. tDiffuse: { type: "t", value: null },
  38. opacity: { type: "f", value: 1.0 }
  39. },
  40. vertexShader: [
  41. "varying vec2 vUv;",
  42. "void main() {",
  43. "vUv = uv;",
  44. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  45. "}"
  46. ].join("\n"),
  47. fragmentShader: [
  48. "uniform float opacity;",
  49. "uniform sampler2D tDiffuse;",
  50. "varying vec2 vUv;",
  51. "void main() {",
  52. "vec4 texel = texture2D( tDiffuse, vUv );",
  53. "gl_FragColor = opacity * texel;",
  54. "}"
  55. ].join("\n")
  56. },
  57. /* ------------------------------------------------------------------------
  58. // Convolution shader
  59. // - ported from o3d sample to WebGL / GLSL
  60. // http://o3d.googlecode.com/svn/trunk/samples/convolution.html
  61. ------------------------------------------------------------------------ */
  62. 'convolution': {
  63. defines: {
  64. "KERNEL_SIZE_FLOAT": "25.0",
  65. "KERNEL_SIZE_INT": "25",
  66. },
  67. uniforms: {
  68. "tDiffuse" : { type: "t", value: null },
  69. "uImageIncrement" : { type: "v2", value: new THREE.Vector2( 0.001953125, 0.0 ) },
  70. "cKernel" : { type: "fv1", value: [] }
  71. },
  72. vertexShader: [
  73. "uniform vec2 uImageIncrement;",
  74. "varying vec2 vUv;",
  75. "void main() {",
  76. "vUv = uv - ( ( KERNEL_SIZE_FLOAT - 1.0 ) / 2.0 ) * uImageIncrement;",
  77. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  78. "}"
  79. ].join("\n"),
  80. fragmentShader: [
  81. "uniform float cKernel[ KERNEL_SIZE_INT ];",
  82. "uniform sampler2D tDiffuse;",
  83. "uniform vec2 uImageIncrement;",
  84. "varying vec2 vUv;",
  85. "void main() {",
  86. "vec2 imageCoord = vUv;",
  87. "vec4 sum = vec4( 0.0, 0.0, 0.0, 0.0 );",
  88. "for( int i = 0; i < KERNEL_SIZE_INT; i ++ ) {",
  89. "sum += texture2D( tDiffuse, imageCoord ) * cKernel[ i ];",
  90. "imageCoord += uImageIncrement;",
  91. "}",
  92. "gl_FragColor = sum;",
  93. "}"
  94. ].join("\n")
  95. },
  96. /* -------------------------------------------------------------------------
  97. // Film grain & scanlines shader
  98. // - ported from HLSL to WebGL / GLSL
  99. // http://www.truevision3d.com/forums/showcase/staticnoise_colorblackwhite_scanline_shaders-t18698.0.html
  100. // Screen Space Static Postprocessor
  101. //
  102. // Produces an analogue noise overlay similar to a film grain / TV static
  103. //
  104. // Original implementation and noise algorithm
  105. // Pat 'Hawthorne' Shearon
  106. //
  107. // Optimized scanlines + noise version with intensity scaling
  108. // Georg 'Leviathan' Steinrohder
  109. // This version is provided under a Creative Commons Attribution 3.0 License
  110. // http://creativecommons.org/licenses/by/3.0/
  111. ------------------------------------------------------------------------- */
  112. 'film': {
  113. uniforms: {
  114. tDiffuse: { type: "t", value: null },
  115. time: { type: "f", value: 0.0 },
  116. nIntensity: { type: "f", value: 0.5 },
  117. sIntensity: { type: "f", value: 0.05 },
  118. sCount: { type: "f", value: 4096 },
  119. grayscale: { type: "i", value: 1 }
  120. },
  121. vertexShader: [
  122. "varying vec2 vUv;",
  123. "void main() {",
  124. "vUv = uv;",
  125. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  126. "}"
  127. ].join("\n"),
  128. fragmentShader: [
  129. // control parameter
  130. "uniform float time;",
  131. "uniform bool grayscale;",
  132. // noise effect intensity value (0 = no effect, 1 = full effect)
  133. "uniform float nIntensity;",
  134. // scanlines effect intensity value (0 = no effect, 1 = full effect)
  135. "uniform float sIntensity;",
  136. // scanlines effect count value (0 = no effect, 4096 = full effect)
  137. "uniform float sCount;",
  138. "uniform sampler2D tDiffuse;",
  139. "varying vec2 vUv;",
  140. "void main() {",
  141. // sample the source
  142. "vec4 cTextureScreen = texture2D( tDiffuse, vUv );",
  143. // make some noise
  144. "float x = vUv.x * vUv.y * time * 1000.0;",
  145. "x = mod( x, 13.0 ) * mod( x, 123.0 );",
  146. "float dx = mod( x, 0.01 );",
  147. // add noise
  148. "vec3 cResult = cTextureScreen.rgb + cTextureScreen.rgb * clamp( 0.1 + dx * 100.0, 0.0, 1.0 );",
  149. // get us a sine and cosine
  150. "vec2 sc = vec2( sin( vUv.y * sCount ), cos( vUv.y * sCount ) );",
  151. // add scanlines
  152. "cResult += cTextureScreen.rgb * vec3( sc.x, sc.y, sc.x ) * sIntensity;",
  153. // interpolate between source and result by intensity
  154. "cResult = cTextureScreen.rgb + clamp( nIntensity, 0.0,1.0 ) * ( cResult - cTextureScreen.rgb );",
  155. // convert to grayscale if desired
  156. "if( grayscale ) {",
  157. "cResult = vec3( cResult.r * 0.3 + cResult.g * 0.59 + cResult.b * 0.11 );",
  158. "}",
  159. "gl_FragColor = vec4( cResult, cTextureScreen.a );",
  160. "}"
  161. ].join("\n")
  162. },
  163. /* -------------------------------------------------------------------------
  164. // Depth-of-field shader with bokeh
  165. // ported from GLSL shader by Martins Upitis
  166. // http://artmartinsh.blogspot.com/2010/02/glsl-lens-blur-filter-with-bokeh.html
  167. ------------------------------------------------------------------------- */
  168. 'bokeh' : {
  169. uniforms: { tColor: { type: "t", value: null },
  170. tDepth: { type: "t", value: null },
  171. focus: { type: "f", value: 1.0 },
  172. aspect: { type: "f", value: 1.0 },
  173. aperture: { type: "f", value: 0.025 },
  174. maxblur: { type: "f", value: 1.0 }
  175. },
  176. vertexShader: [
  177. "varying vec2 vUv;",
  178. "void main() {",
  179. "vUv = uv;",
  180. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  181. "}"
  182. ].join("\n"),
  183. fragmentShader: [
  184. "varying vec2 vUv;",
  185. "uniform sampler2D tColor;",
  186. "uniform sampler2D tDepth;",
  187. "uniform float maxblur;", // max blur amount
  188. "uniform float aperture;", // aperture - bigger values for shallower depth of field
  189. "uniform float focus;",
  190. "uniform float aspect;",
  191. "void main() {",
  192. "vec2 aspectcorrect = vec2( 1.0, aspect );",
  193. "vec4 depth1 = texture2D( tDepth, vUv );",
  194. "float factor = depth1.x - focus;",
  195. "vec2 dofblur = vec2 ( clamp( factor * aperture, -maxblur, maxblur ) );",
  196. "vec2 dofblur9 = dofblur * 0.9;",
  197. "vec2 dofblur7 = dofblur * 0.7;",
  198. "vec2 dofblur4 = dofblur * 0.4;",
  199. "vec4 col = vec4( 0.0 );",
  200. "col += texture2D( tColor, vUv.xy );",
  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.40, 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.0, -0.4 ) * aspectcorrect ) * dofblur );",
  210. "col += texture2D( tColor, vUv.xy + ( vec2( -0.15, 0.37 ) * aspectcorrect ) * dofblur );",
  211. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, 0.29 ) * aspectcorrect ) * dofblur );",
  212. "col += texture2D( tColor, vUv.xy + ( vec2( 0.37, 0.15 ) * aspectcorrect ) * dofblur );",
  213. "col += texture2D( tColor, vUv.xy + ( vec2( -0.4, 0.0 ) * aspectcorrect ) * dofblur );",
  214. "col += texture2D( tColor, vUv.xy + ( vec2( -0.37, -0.15 ) * aspectcorrect ) * dofblur );",
  215. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, -0.29 ) * aspectcorrect ) * dofblur );",
  216. "col += texture2D( tColor, vUv.xy + ( vec2( 0.15, -0.37 ) * aspectcorrect ) * dofblur );",
  217. "col += texture2D( tColor, vUv.xy + ( vec2( 0.15, 0.37 ) * aspectcorrect ) * dofblur9 );",
  218. "col += texture2D( tColor, vUv.xy + ( vec2( -0.37, 0.15 ) * aspectcorrect ) * dofblur9 );",
  219. "col += texture2D( tColor, vUv.xy + ( vec2( 0.37, -0.15 ) * aspectcorrect ) * dofblur9 );",
  220. "col += texture2D( tColor, vUv.xy + ( vec2( -0.15, -0.37 ) * aspectcorrect ) * dofblur9 );",
  221. "col += texture2D( tColor, vUv.xy + ( vec2( -0.15, 0.37 ) * aspectcorrect ) * dofblur9 );",
  222. "col += texture2D( tColor, vUv.xy + ( vec2( 0.37, 0.15 ) * aspectcorrect ) * dofblur9 );",
  223. "col += texture2D( tColor, vUv.xy + ( vec2( -0.37, -0.15 ) * aspectcorrect ) * dofblur9 );",
  224. "col += texture2D( tColor, vUv.xy + ( vec2( 0.15, -0.37 ) * aspectcorrect ) * dofblur9 );",
  225. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, 0.29 ) * aspectcorrect ) * dofblur7 );",
  226. "col += texture2D( tColor, vUv.xy + ( vec2( 0.40, 0.0 ) * aspectcorrect ) * dofblur7 );",
  227. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, -0.29 ) * aspectcorrect ) * dofblur7 );",
  228. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, -0.4 ) * aspectcorrect ) * dofblur7 );",
  229. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, 0.29 ) * aspectcorrect ) * dofblur7 );",
  230. "col += texture2D( tColor, vUv.xy + ( vec2( -0.4, 0.0 ) * aspectcorrect ) * dofblur7 );",
  231. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, -0.29 ) * aspectcorrect ) * dofblur7 );",
  232. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, 0.4 ) * aspectcorrect ) * dofblur7 );",
  233. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, 0.29 ) * aspectcorrect ) * dofblur4 );",
  234. "col += texture2D( tColor, vUv.xy + ( vec2( 0.4, 0.0 ) * aspectcorrect ) * dofblur4 );",
  235. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, -0.29 ) * aspectcorrect ) * dofblur4 );",
  236. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, -0.4 ) * aspectcorrect ) * dofblur4 );",
  237. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, 0.29 ) * aspectcorrect ) * dofblur4 );",
  238. "col += texture2D( tColor, vUv.xy + ( vec2( -0.4, 0.0 ) * aspectcorrect ) * dofblur4 );",
  239. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, -0.29 ) * aspectcorrect ) * dofblur4 );",
  240. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, 0.4 ) * aspectcorrect ) * dofblur4 );",
  241. "gl_FragColor = col / 41.0;",
  242. "gl_FragColor.a = 1.0;",
  243. "}"
  244. ].join("\n")
  245. },
  246. /* -------------------------------------------------------------------------
  247. // Depth-of-field shader using mipmaps
  248. // - from Matt Handley @applmak
  249. // - requires power-of-2 sized render target with enabled mipmaps
  250. ------------------------------------------------------------------------- */
  251. 'dofmipmap': {
  252. uniforms: {
  253. tColor: { type: "t", value: null },
  254. tDepth: { type: "t", value: null },
  255. focus: { type: "f", value: 1.0 },
  256. maxblur: { type: "f", value: 1.0 }
  257. },
  258. vertexShader: [
  259. "varying vec2 vUv;",
  260. "void main() {",
  261. "vUv = uv;",
  262. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  263. "}"
  264. ].join("\n"),
  265. fragmentShader: [
  266. "uniform float focus;",
  267. "uniform float maxblur;",
  268. "uniform sampler2D tColor;",
  269. "uniform sampler2D tDepth;",
  270. "varying vec2 vUv;",
  271. "void main() {",
  272. "vec4 depth = texture2D( tDepth, vUv );",
  273. "float factor = depth.x - focus;",
  274. "vec4 col = texture2D( tColor, vUv, 2.0 * maxblur * abs( focus - depth.x ) );",
  275. "gl_FragColor = col;",
  276. "gl_FragColor.a = 1.0;",
  277. "}"
  278. ].join("\n")
  279. },
  280. /* -------------------------------------------------------------------------
  281. // Sepia tone shader
  282. // - based on glfx.js sepia shader
  283. // https://github.com/evanw/glfx.js
  284. ------------------------------------------------------------------------- */
  285. 'sepia': {
  286. uniforms: {
  287. tDiffuse: { type: "t", value: null },
  288. amount: { type: "f", value: 1.0 }
  289. },
  290. vertexShader: [
  291. "varying vec2 vUv;",
  292. "void main() {",
  293. "vUv = uv;",
  294. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  295. "}"
  296. ].join("\n"),
  297. fragmentShader: [
  298. "uniform float amount;",
  299. "uniform sampler2D tDiffuse;",
  300. "varying vec2 vUv;",
  301. "void main() {",
  302. "vec4 color = texture2D( tDiffuse, vUv );",
  303. "vec3 c = color.rgb;",
  304. "color.r = dot( c, vec3( 1.0 - 0.607 * amount, 0.769 * amount, 0.189 * amount ) );",
  305. "color.g = dot( c, vec3( 0.349 * amount, 1.0 - 0.314 * amount, 0.168 * amount ) );",
  306. "color.b = dot( c, vec3( 0.272 * amount, 0.534 * amount, 1.0 - 0.869 * amount ) );",
  307. "gl_FragColor = vec4( min( vec3( 1.0 ), color.rgb ), color.a );",
  308. "}"
  309. ].join("\n")
  310. },
  311. /* -------------------------------------------------------------------------
  312. // Dot screen shader
  313. // - based on glfx.js sepia shader
  314. // https://github.com/evanw/glfx.js
  315. ------------------------------------------------------------------------- */
  316. 'dotscreen': {
  317. uniforms: {
  318. tDiffuse: { type: "t", value: null },
  319. tSize: { type: "v2", value: new THREE.Vector2( 256, 256 ) },
  320. center: { type: "v2", value: new THREE.Vector2( 0.5, 0.5 ) },
  321. angle: { type: "f", value: 1.57 },
  322. scale: { type: "f", value: 1.0 }
  323. },
  324. vertexShader: [
  325. "varying vec2 vUv;",
  326. "void main() {",
  327. "vUv = uv;",
  328. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  329. "}"
  330. ].join("\n"),
  331. fragmentShader: [
  332. "uniform vec2 center;",
  333. "uniform float angle;",
  334. "uniform float scale;",
  335. "uniform vec2 tSize;",
  336. "uniform sampler2D tDiffuse;",
  337. "varying vec2 vUv;",
  338. "float pattern() {",
  339. "float s = sin( angle ), c = cos( angle );",
  340. "vec2 tex = vUv * tSize - center;",
  341. "vec2 point = vec2( c * tex.x - s * tex.y, s * tex.x + c * tex.y ) * scale;",
  342. "return ( sin( point.x ) * sin( point.y ) ) * 4.0;",
  343. "}",
  344. "void main() {",
  345. "vec4 color = texture2D( tDiffuse, vUv );",
  346. "float average = ( color.r + color.g + color.b ) / 3.0;",
  347. "gl_FragColor = vec4( vec3( average * 10.0 - 5.0 + pattern() ), color.a );",
  348. "}"
  349. ].join("\n")
  350. },
  351. /* ------------------------------------------------------------------------------------------------
  352. // Vignette shader
  353. // - based on PaintEffect postprocess from ro.me
  354. // http://code.google.com/p/3-dreams-of-black/source/browse/deploy/js/effects/PaintEffect.js
  355. ------------------------------------------------------------------------------------------------ */
  356. 'vignette': {
  357. uniforms: {
  358. tDiffuse: { type: "t", value: null },
  359. offset: { type: "f", value: 1.0 },
  360. darkness: { type: "f", value: 1.0 }
  361. },
  362. vertexShader: [
  363. "varying vec2 vUv;",
  364. "void main() {",
  365. "vUv = uv;",
  366. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  367. "}"
  368. ].join("\n"),
  369. fragmentShader: [
  370. "uniform float offset;",
  371. "uniform float darkness;",
  372. "uniform sampler2D tDiffuse;",
  373. "varying vec2 vUv;",
  374. "void main() {",
  375. // Eskil's vignette
  376. "vec4 texel = texture2D( tDiffuse, vUv );",
  377. "vec2 uv = ( vUv - vec2( 0.5 ) ) * vec2( offset );",
  378. "gl_FragColor = vec4( mix( texel.rgb, vec3( 1.0 - darkness ), dot( uv, uv ) ), texel.a );",
  379. /*
  380. // alternative version from glfx.js
  381. // this one makes more "dusty" look (as opposed to "burned")
  382. "vec4 color = texture2D( tDiffuse, vUv );",
  383. "float dist = distance( vUv, vec2( 0.5 ) );",
  384. "color.rgb *= smoothstep( 0.8, offset * 0.799, dist *( darkness + offset ) );",
  385. "gl_FragColor = color;",
  386. */
  387. "}"
  388. ].join("\n")
  389. },
  390. /* -------------------------------------------------------------------------
  391. // Bleach bypass shader [http://en.wikipedia.org/wiki/Bleach_bypass]
  392. // - based on Nvidia example
  393. // http://developer.download.nvidia.com/shaderlibrary/webpages/shader_library.html#post_bleach_bypass
  394. ------------------------------------------------------------------------- */
  395. 'bleachbypass': {
  396. uniforms: {
  397. tDiffuse: { type: "t", value: null },
  398. opacity: { type: "f", value: 1.0 }
  399. },
  400. vertexShader: [
  401. "varying vec2 vUv;",
  402. "void main() {",
  403. "vUv = uv;",
  404. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  405. "}"
  406. ].join("\n"),
  407. fragmentShader: [
  408. "uniform float opacity;",
  409. "uniform sampler2D tDiffuse;",
  410. "varying vec2 vUv;",
  411. "void main() {",
  412. "vec4 base = texture2D( tDiffuse, vUv );",
  413. "vec3 lumCoeff = vec3( 0.25, 0.65, 0.1 );",
  414. "float lum = dot( lumCoeff, base.rgb );",
  415. "vec3 blend = vec3( lum );",
  416. "float L = min( 1.0, max( 0.0, 10.0 * ( lum - 0.45 ) ) );",
  417. "vec3 result1 = 2.0 * base.rgb * blend;",
  418. "vec3 result2 = 1.0 - 2.0 * ( 1.0 - blend ) * ( 1.0 - base.rgb );",
  419. "vec3 newColor = mix( result1, result2, L );",
  420. "float A2 = opacity * base.a;",
  421. "vec3 mixRGB = A2 * newColor.rgb;",
  422. "mixRGB += ( ( 1.0 - A2 ) * base.rgb );",
  423. "gl_FragColor = vec4( mixRGB, base.a );",
  424. "}"
  425. ].join("\n")
  426. },
  427. /* --------------------------------------------------------------------------------------------------
  428. // Focus shader
  429. // - based on PaintEffect postprocess from ro.me
  430. // http://code.google.com/p/3-dreams-of-black/source/browse/deploy/js/effects/PaintEffect.js
  431. -------------------------------------------------------------------------------------------------- */
  432. 'focus': {
  433. uniforms : {
  434. "tDiffuse": { type: "t", value: null },
  435. "screenWidth": { type: "f", value: 1024 },
  436. "screenHeight": { type: "f", value: 1024 },
  437. "sampleDistance": { type: "f", value: 0.94 },
  438. "waveFactor": { type: "f", value: 0.00125 }
  439. },
  440. vertexShader: [
  441. "varying vec2 vUv;",
  442. "void main() {",
  443. "vUv = uv;",
  444. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  445. "}"
  446. ].join("\n"),
  447. fragmentShader: [
  448. "uniform float screenWidth;",
  449. "uniform float screenHeight;",
  450. "uniform float sampleDistance;",
  451. "uniform float waveFactor;",
  452. "uniform sampler2D tDiffuse;",
  453. "varying vec2 vUv;",
  454. "void main() {",
  455. "vec4 color, org, tmp, add;",
  456. "float sample_dist, f;",
  457. "vec2 vin;",
  458. "vec2 uv = vUv;",
  459. "add = color = org = texture2D( tDiffuse, uv );",
  460. "vin = ( uv - vec2( 0.5 ) ) * vec2( 1.4 );",
  461. "sample_dist = dot( vin, vin ) * 2.0;",
  462. "f = ( waveFactor * 100.0 + sample_dist ) * sampleDistance * 4.0;",
  463. "vec2 sampleSize = vec2( 1.0 / screenWidth, 1.0 / screenHeight ) * vec2( f );",
  464. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.111964, 0.993712 ) * sampleSize );",
  465. "if( tmp.b < color.b ) color = tmp;",
  466. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.846724, 0.532032 ) * sampleSize );",
  467. "if( tmp.b < color.b ) color = tmp;",
  468. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.943883, -0.330279 ) * sampleSize );",
  469. "if( tmp.b < color.b ) color = tmp;",
  470. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.330279, -0.943883 ) * sampleSize );",
  471. "if( tmp.b < color.b ) color = tmp;",
  472. "add += tmp = texture2D( tDiffuse, uv + vec2( -0.532032, -0.846724 ) * sampleSize );",
  473. "if( tmp.b < color.b ) color = tmp;",
  474. "add += tmp = texture2D( tDiffuse, uv + vec2( -0.993712, -0.111964 ) * sampleSize );",
  475. "if( tmp.b < color.b ) color = tmp;",
  476. "add += tmp = texture2D( tDiffuse, uv + vec2( -0.707107, 0.707107 ) * sampleSize );",
  477. "if( tmp.b < color.b ) color = tmp;",
  478. "color = color * vec4( 2.0 ) - ( add / vec4( 8.0 ) );",
  479. "color = color + ( add / vec4( 8.0 ) - color ) * ( vec4( 1.0 ) - vec4( sample_dist * 0.5 ) );",
  480. "gl_FragColor = vec4( color.rgb * color.rgb * vec3( 0.95 ) + color.rgb, 1.0 );",
  481. "}"
  482. ].join("\n")
  483. },
  484. /* -------------------------------------------------------------------------
  485. // Triangle blur shader
  486. // - based on glfx.js triangle blur shader
  487. // https://github.com/evanw/glfx.js
  488. // A basic blur filter, which convolves the image with a
  489. // pyramid filter. The pyramid filter is separable and is applied as two
  490. // perpendicular triangle filters.
  491. ------------------------------------------------------------------------- */
  492. 'triangleBlur': {
  493. uniforms : {
  494. "texture": { type: "t", value: null },
  495. "delta": { type: "v2", value:new THREE.Vector2( 1, 1 ) }
  496. },
  497. vertexShader: [
  498. "varying vec2 vUv;",
  499. "void main() {",
  500. "vUv = uv;",
  501. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  502. "}"
  503. ].join("\n"),
  504. fragmentShader: [
  505. "#define ITERATIONS 10.0",
  506. "uniform sampler2D texture;",
  507. "uniform vec2 delta;",
  508. "varying vec2 vUv;",
  509. "float random( vec3 scale, float seed ) {",
  510. // use the fragment position for a different seed per-pixel
  511. "return fract( sin( dot( gl_FragCoord.xyz + seed, scale ) ) * 43758.5453 + seed );",
  512. "}",
  513. "void main() {",
  514. "vec4 color = vec4( 0.0 );",
  515. "float total = 0.0;",
  516. // randomize the lookup values to hide the fixed number of samples
  517. "float offset = random( vec3( 12.9898, 78.233, 151.7182 ), 0.0 );",
  518. "for ( float t = -ITERATIONS; t <= ITERATIONS; t ++ ) {",
  519. "float percent = ( t + offset - 0.5 ) / ITERATIONS;",
  520. "float weight = 1.0 - abs( percent );",
  521. "color += texture2D( texture, vUv + delta * percent ) * weight;",
  522. "total += weight;",
  523. "}",
  524. "gl_FragColor = color / total;",
  525. "}",
  526. ].join("\n")
  527. },
  528. /* -------------------------------------------------------------------------
  529. // Simple test shader
  530. ------------------------------------------------------------------------- */
  531. 'basic': {
  532. uniforms: {},
  533. vertexShader: [
  534. "void main() {",
  535. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  536. "}"
  537. ].join("\n"),
  538. fragmentShader: [
  539. "void main() {",
  540. "gl_FragColor = vec4( 1.0, 0.0, 0.0, 0.5 );",
  541. "}"
  542. ].join("\n")
  543. },
  544. /* --------------------------------------------------------------------------------------------------
  545. // Two pass Gaussian blur filter (horizontal and vertical blur shaders)
  546. // - described in http://www.gamerendering.com/2008/10/11/gaussian-blur-filter-shader/
  547. // and used in http://www.cake23.de/traveling-wavefronts-lit-up.html
  548. //
  549. // - 9 samples per pass
  550. // - standard deviation 2.7
  551. // - "h" and "v" parameters should be set to "1 / width" and "1 / height"
  552. -------------------------------------------------------------------------------------------------- */
  553. 'horizontalBlur': {
  554. uniforms: {
  555. "tDiffuse": { type: "t", value: null },
  556. "h": { type: "f", value: 1.0 / 512.0 }
  557. },
  558. vertexShader: [
  559. "varying vec2 vUv;",
  560. "void main() {",
  561. "vUv = uv;",
  562. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  563. "}"
  564. ].join("\n"),
  565. fragmentShader: [
  566. "uniform sampler2D tDiffuse;",
  567. "uniform float h;",
  568. "varying vec2 vUv;",
  569. "void main() {",
  570. "vec4 sum = vec4( 0.0 );",
  571. "sum += texture2D( tDiffuse, vec2( vUv.x - 4.0 * h, vUv.y ) ) * 0.051;",
  572. "sum += texture2D( tDiffuse, vec2( vUv.x - 3.0 * h, vUv.y ) ) * 0.0918;",
  573. "sum += texture2D( tDiffuse, vec2( vUv.x - 2.0 * h, vUv.y ) ) * 0.12245;",
  574. "sum += texture2D( tDiffuse, vec2( vUv.x - 1.0 * h, vUv.y ) ) * 0.1531;",
  575. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",
  576. "sum += texture2D( tDiffuse, vec2( vUv.x + 1.0 * h, vUv.y ) ) * 0.1531;",
  577. "sum += texture2D( tDiffuse, vec2( vUv.x + 2.0 * h, vUv.y ) ) * 0.12245;",
  578. "sum += texture2D( tDiffuse, vec2( vUv.x + 3.0 * h, vUv.y ) ) * 0.0918;",
  579. "sum += texture2D( tDiffuse, vec2( vUv.x + 4.0 * h, vUv.y ) ) * 0.051;",
  580. "gl_FragColor = sum;",
  581. "}"
  582. ].join("\n")
  583. },
  584. 'verticalBlur': {
  585. uniforms: {
  586. "tDiffuse": { type: "t", value: null },
  587. "v": { type: "f", value: 1.0 / 512.0 }
  588. },
  589. vertexShader: [
  590. "varying vec2 vUv;",
  591. "void main() {",
  592. "vUv = uv;",
  593. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  594. "}"
  595. ].join("\n"),
  596. fragmentShader: [
  597. "uniform sampler2D tDiffuse;",
  598. "uniform float v;",
  599. "varying vec2 vUv;",
  600. "void main() {",
  601. "vec4 sum = vec4( 0.0 );",
  602. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 4.0 * v ) ) * 0.051;",
  603. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 3.0 * v ) ) * 0.0918;",
  604. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 2.0 * v ) ) * 0.12245;",
  605. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 1.0 * v ) ) * 0.1531;",
  606. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",
  607. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 1.0 * v ) ) * 0.1531;",
  608. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 2.0 * v ) ) * 0.12245;",
  609. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 3.0 * v ) ) * 0.0918;",
  610. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 4.0 * v ) ) * 0.051;",
  611. "gl_FragColor = sum;",
  612. "}"
  613. ].join("\n")
  614. },
  615. /* --------------------------------------------------------------------------------------------------
  616. // Simple fake tilt-shift effect, modulating two pass Gaussian blur (see above) by vertical position
  617. //
  618. // - 9 samples per pass
  619. // - standard deviation 2.7
  620. // - "h" and "v" parameters should be set to "1 / width" and "1 / height"
  621. // - "r" parameter control where "focused" horizontal line lies
  622. -------------------------------------------------------------------------------------------------- */
  623. 'horizontalTiltShift': {
  624. uniforms: {
  625. "tDiffuse": { type: "t", value: null },
  626. "h": { type: "f", value: 1.0 / 512.0 },
  627. "r": { type: "f", value: 0.35 }
  628. },
  629. vertexShader: [
  630. "varying vec2 vUv;",
  631. "void main() {",
  632. "vUv = uv;",
  633. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  634. "}"
  635. ].join("\n"),
  636. fragmentShader: [
  637. "uniform sampler2D tDiffuse;",
  638. "uniform float h;",
  639. "uniform float r;",
  640. "varying vec2 vUv;",
  641. "void main() {",
  642. "vec4 sum = vec4( 0.0 );",
  643. "float hh = h * abs( r - vUv.y );",
  644. "sum += texture2D( tDiffuse, vec2( vUv.x - 4.0 * hh, vUv.y ) ) * 0.051;",
  645. "sum += texture2D( tDiffuse, vec2( vUv.x - 3.0 * hh, vUv.y ) ) * 0.0918;",
  646. "sum += texture2D( tDiffuse, vec2( vUv.x - 2.0 * hh, vUv.y ) ) * 0.12245;",
  647. "sum += texture2D( tDiffuse, vec2( vUv.x - 1.0 * hh, vUv.y ) ) * 0.1531;",
  648. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",
  649. "sum += texture2D( tDiffuse, vec2( vUv.x + 1.0 * hh, vUv.y ) ) * 0.1531;",
  650. "sum += texture2D( tDiffuse, vec2( vUv.x + 2.0 * hh, vUv.y ) ) * 0.12245;",
  651. "sum += texture2D( tDiffuse, vec2( vUv.x + 3.0 * hh, vUv.y ) ) * 0.0918;",
  652. "sum += texture2D( tDiffuse, vec2( vUv.x + 4.0 * hh, vUv.y ) ) * 0.051;",
  653. "gl_FragColor = sum;",
  654. "}"
  655. ].join("\n")
  656. },
  657. 'verticalTiltShift': {
  658. uniforms: {
  659. "tDiffuse": { type: "t", value: null },
  660. "v": { type: "f", value: 1.0 / 512.0 },
  661. "r": { type: "f", value: 0.35 }
  662. },
  663. vertexShader: [
  664. "varying vec2 vUv;",
  665. "void main() {",
  666. "vUv = uv;",
  667. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  668. "}"
  669. ].join("\n"),
  670. fragmentShader: [
  671. "uniform sampler2D tDiffuse;",
  672. "uniform float v;",
  673. "uniform float r;",
  674. "varying vec2 vUv;",
  675. "void main() {",
  676. "vec4 sum = vec4( 0.0 );",
  677. "float vv = v * abs( r - vUv.y );",
  678. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 4.0 * vv ) ) * 0.051;",
  679. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 3.0 * vv ) ) * 0.0918;",
  680. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 2.0 * vv ) ) * 0.12245;",
  681. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 1.0 * vv ) ) * 0.1531;",
  682. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",
  683. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 1.0 * vv ) ) * 0.1531;",
  684. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 2.0 * vv ) ) * 0.12245;",
  685. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 3.0 * vv ) ) * 0.0918;",
  686. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 4.0 * vv ) ) * 0.051;",
  687. "gl_FragColor = sum;",
  688. "}"
  689. ].join("\n")
  690. },
  691. /* -------------------------------------------------------------------------
  692. // Blend two textures
  693. ------------------------------------------------------------------------- */
  694. 'blend': {
  695. uniforms: {
  696. tDiffuse1: { type: "t", value: null },
  697. tDiffuse2: { type: "t", value: null },
  698. mixRatio: { type: "f", value: 0.5 },
  699. opacity: { type: "f", value: 1.0 }
  700. },
  701. vertexShader: [
  702. "varying vec2 vUv;",
  703. "void main() {",
  704. "vUv = uv;",
  705. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  706. "}"
  707. ].join("\n"),
  708. fragmentShader: [
  709. "uniform float opacity;",
  710. "uniform float mixRatio;",
  711. "uniform sampler2D tDiffuse1;",
  712. "uniform sampler2D tDiffuse2;",
  713. "varying vec2 vUv;",
  714. "void main() {",
  715. "vec4 texel1 = texture2D( tDiffuse1, vUv );",
  716. "vec4 texel2 = texture2D( tDiffuse2, vUv );",
  717. "gl_FragColor = opacity * mix( texel1, texel2, mixRatio );",
  718. "}"
  719. ].join("\n")
  720. },
  721. /* -------------------------------------------------------------------------
  722. // NVIDIA FXAA by Timothy Lottes
  723. // http://timothylottes.blogspot.com/2011/06/fxaa3-source-released.html
  724. // - WebGL port by @supereggbert
  725. // http://www.glge.org/demos/fxaa/
  726. ------------------------------------------------------------------------- */
  727. 'fxaa': {
  728. uniforms: {
  729. "tDiffuse": { type: "t", value: null },
  730. "resolution": { type: "v2", value: new THREE.Vector2( 1 / 1024, 1 / 512 ) }
  731. },
  732. vertexShader: [
  733. "varying vec2 vUv;",
  734. "void main() {",
  735. "vUv = uv;",
  736. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  737. "}"
  738. ].join("\n"),
  739. fragmentShader: [
  740. "uniform sampler2D tDiffuse;",
  741. "uniform vec2 resolution;",
  742. "varying vec2 vUv;",
  743. "#define FXAA_REDUCE_MIN (1.0/128.0)",
  744. "#define FXAA_REDUCE_MUL (1.0/8.0)",
  745. "#define FXAA_SPAN_MAX 8.0",
  746. "void main() {",
  747. "vec3 rgbNW = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( -1.0, -1.0 ) ) * resolution ).xyz;",
  748. "vec3 rgbNE = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( 1.0, -1.0 ) ) * resolution ).xyz;",
  749. "vec3 rgbSW = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( -1.0, 1.0 ) ) * resolution ).xyz;",
  750. "vec3 rgbSE = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( 1.0, 1.0 ) ) * resolution ).xyz;",
  751. "vec4 rgbaM = texture2D( tDiffuse, gl_FragCoord.xy * resolution );",
  752. "vec3 rgbM = rgbaM.xyz;",
  753. "float opacity = rgbaM.w;",
  754. "vec3 luma = vec3( 0.299, 0.587, 0.114 );",
  755. "float lumaNW = dot( rgbNW, luma );",
  756. "float lumaNE = dot( rgbNE, luma );",
  757. "float lumaSW = dot( rgbSW, luma );",
  758. "float lumaSE = dot( rgbSE, luma );",
  759. "float lumaM = dot( rgbM, luma );",
  760. "float lumaMin = min( lumaM, min( min( lumaNW, lumaNE ), min( lumaSW, lumaSE ) ) );",
  761. "float lumaMax = max( lumaM, max( max( lumaNW, lumaNE) , max( lumaSW, lumaSE ) ) );",
  762. "vec2 dir;",
  763. "dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));",
  764. "dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));",
  765. "float dirReduce = max( ( lumaNW + lumaNE + lumaSW + lumaSE ) * ( 0.25 * FXAA_REDUCE_MUL ), FXAA_REDUCE_MIN );",
  766. "float rcpDirMin = 1.0 / ( min( abs( dir.x ), abs( dir.y ) ) + dirReduce );",
  767. "dir = min( vec2( FXAA_SPAN_MAX, FXAA_SPAN_MAX),",
  768. "max( vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),",
  769. "dir * rcpDirMin)) * resolution;",
  770. "vec3 rgbA = 0.5 * (",
  771. "texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * ( 1.0 / 3.0 - 0.5 ) ).xyz +",
  772. "texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * ( 2.0 / 3.0 - 0.5 ) ).xyz );",
  773. "vec3 rgbB = rgbA * 0.5 + 0.25 * (",
  774. "texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * -0.5 ).xyz +",
  775. "texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * 0.5 ).xyz );",
  776. "float lumaB = dot( rgbB, luma );",
  777. "if ( ( lumaB < lumaMin ) || ( lumaB > lumaMax ) ) {",
  778. "gl_FragColor = vec4( rgbA, opacity );",
  779. "} else {",
  780. "gl_FragColor = vec4( rgbB, opacity );",
  781. "}",
  782. "}",
  783. ].join("\n")
  784. },
  785. /* -------------------------------------------------------------------------
  786. // Luminosity
  787. // http://en.wikipedia.org/wiki/Luminosity
  788. ------------------------------------------------------------------------- */
  789. 'luminosity': {
  790. uniforms: {
  791. "tDiffuse": { type: "t", value: null }
  792. },
  793. vertexShader: [
  794. "varying vec2 vUv;",
  795. "void main() {",
  796. "vUv = uv;",
  797. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  798. "}"
  799. ].join("\n"),
  800. fragmentShader: [
  801. "uniform sampler2D tDiffuse;",
  802. "varying vec2 vUv;",
  803. "void main() {",
  804. "vec4 texel = texture2D( tDiffuse, vUv );",
  805. "vec3 luma = vec3( 0.299, 0.587, 0.114 );",
  806. "float v = dot( texel.xyz, luma );",
  807. "gl_FragColor = vec4( v, v, v, texel.w );",
  808. "}"
  809. ].join("\n")
  810. },
  811. /* -------------------------------------------------------------------------
  812. // Color correction
  813. ------------------------------------------------------------------------- */
  814. 'colorCorrection': {
  815. uniforms: {
  816. "tDiffuse" : { type: "t", value: null },
  817. "powRGB" : { type: "v3", value: new THREE.Vector3( 2, 2, 2 ) },
  818. "mulRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  819. },
  820. vertexShader: [
  821. "varying vec2 vUv;",
  822. "void main() {",
  823. "vUv = uv;",
  824. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  825. "}"
  826. ].join("\n"),
  827. fragmentShader: [
  828. "uniform sampler2D tDiffuse;",
  829. "uniform vec3 powRGB;",
  830. "uniform vec3 mulRGB;",
  831. "varying vec2 vUv;",
  832. "void main() {",
  833. "gl_FragColor = texture2D( tDiffuse, vUv );",
  834. "gl_FragColor.rgb = mulRGB * pow( gl_FragColor.rgb, powRGB );",
  835. "}"
  836. ].join("\n")
  837. },
  838. /* -------------------------------------------------------------------------
  839. // Normal map shader
  840. // - compute normals from heightmap
  841. ------------------------------------------------------------------------- */
  842. 'normalmap': {
  843. uniforms: {
  844. "heightMap" : { type: "t", value: null },
  845. "resolution": { type: "v2", value: new THREE.Vector2( 512, 512 ) },
  846. "scale" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  847. "height" : { type: "f", value: 0.05 }
  848. },
  849. vertexShader: [
  850. "varying vec2 vUv;",
  851. "void main() {",
  852. "vUv = uv;",
  853. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  854. "}"
  855. ].join("\n"),
  856. fragmentShader: [
  857. "uniform float height;",
  858. "uniform vec2 resolution;",
  859. "uniform sampler2D heightMap;",
  860. "varying vec2 vUv;",
  861. "void main() {",
  862. "float val = texture2D( heightMap, vUv ).x;",
  863. "float valU = texture2D( heightMap, vUv + vec2( 1.0 / resolution.x, 0.0 ) ).x;",
  864. "float valV = texture2D( heightMap, vUv + vec2( 0.0, 1.0 / resolution.y ) ).x;",
  865. "gl_FragColor = vec4( ( 0.5 * normalize( vec3( val - valU, val - valV, height ) ) + 0.5 ), 1.0 );",
  866. "}",
  867. ].join("\n")
  868. },
  869. /* -------------------------------------------------------------------------
  870. // Screen-space ambient occlusion shader
  871. // - ported from
  872. // SSAO GLSL shader v1.2
  873. // assembled by Martins Upitis (martinsh) (http://devlog-martinsh.blogspot.com)
  874. // original technique is made by ArKano22 (http://www.gamedev.net/topic/550699-ssao-no-halo-artifacts/)
  875. // - modifications
  876. // - modified to use RGBA packed depth texture (use clear color 1,1,1,1 for depth pass)
  877. // - made fog more compatible with three.js linear fog
  878. // - refactoring and optimizations
  879. ------------------------------------------------------------------------- */
  880. 'ssao': {
  881. uniforms: {
  882. "tDiffuse": { type: "t", value: null },
  883. "tDepth": { type: "t", value: null },
  884. "size": { type: "v2", value: new THREE.Vector2( 512, 512 ) },
  885. "cameraNear": { type: "f", value: 1 },
  886. "cameraFar": { type: "f", value: 100 },
  887. "fogNear": { type: "f", value: 5 },
  888. "fogFar": { type: "f", value: 100 },
  889. "fogEnabled": { type: "i", value: 0 },
  890. "onlyAO": { type: "i", value: 0 },
  891. "aoClamp": { type: "f", value: 0.3 },
  892. "lumInfluence": { type: "f", value: 0.9 }
  893. },
  894. vertexShader: [
  895. "varying vec2 vUv;",
  896. "void main() {",
  897. "vUv = uv;",
  898. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  899. "}"
  900. ].join("\n"),
  901. fragmentShader: [
  902. "uniform float cameraNear;",
  903. "uniform float cameraFar;",
  904. "uniform float fogNear;",
  905. "uniform float fogFar;",
  906. "uniform bool fogEnabled;", // attenuate AO with linear fog
  907. "uniform bool onlyAO;", // use only ambient occlusion pass?
  908. "uniform vec2 size;", // texture width, height
  909. "uniform float aoClamp;", // depth clamp - reduces haloing at screen edges
  910. "uniform float lumInfluence;", // how much luminance affects occlusion
  911. "uniform sampler2D tDiffuse;",
  912. "uniform sampler2D tDepth;",
  913. "varying vec2 vUv;",
  914. //"#define PI 3.14159265",
  915. "#define DL 2.399963229728653", // PI * ( 3.0 - sqrt( 5.0 ) )
  916. "#define EULER 2.718281828459045",
  917. // helpers
  918. "float width = size.x;", // texture width
  919. "float height = size.y;", // texture height
  920. "float cameraFarPlusNear = cameraFar + cameraNear;",
  921. "float cameraFarMinusNear = cameraFar - cameraNear;",
  922. "float cameraCoef = 2.0 * cameraNear;",
  923. // user variables
  924. "const int samples = 8;", // ao sample count
  925. "const float radius = 5.0;", // ao radius
  926. "const bool useNoise = false;", // use noise instead of pattern for sample dithering
  927. "const float noiseAmount = 0.0003;", // dithering amount
  928. "const float diffArea = 0.4;", // self-shadowing reduction
  929. "const float gDisplace = 0.4;", // gauss bell center
  930. "const vec3 onlyAOColor = vec3( 1.0, 0.7, 0.5 );",
  931. //"const vec3 onlyAOColor = vec3( 1.0, 1.0, 1.0 );",
  932. // RGBA depth
  933. "float unpackDepth( const in vec4 rgba_depth ) {",
  934. "const vec4 bit_shift = vec4( 1.0 / ( 256.0 * 256.0 * 256.0 ), 1.0 / ( 256.0 * 256.0 ), 1.0 / 256.0, 1.0 );",
  935. "float depth = dot( rgba_depth, bit_shift );",
  936. "return depth;",
  937. "}",
  938. // generating noise / pattern texture for dithering
  939. "vec2 rand( const vec2 coord ) {",
  940. "vec2 noise;",
  941. "if ( useNoise ) {",
  942. "float nx = dot ( coord, vec2( 12.9898, 78.233 ) );",
  943. "float ny = dot ( coord, vec2( 12.9898, 78.233 ) * 2.0 );",
  944. "noise = clamp( fract ( 43758.5453 * sin( vec2( nx, ny ) ) ), 0.0, 1.0 );",
  945. "} else {",
  946. "float ff = fract( 1.0 - coord.s * ( width / 2.0 ) );",
  947. "float gg = fract( coord.t * ( height / 2.0 ) );",
  948. "noise = vec2( 0.25, 0.75 ) * vec2( ff ) + vec2( 0.75, 0.25 ) * gg;",
  949. "}",
  950. "return ( noise * 2.0 - 1.0 ) * noiseAmount;",
  951. "}",
  952. "float doFog() {",
  953. "float zdepth = unpackDepth( texture2D( tDepth, vUv ) );",
  954. "float depth = -cameraFar * cameraNear / ( zdepth * cameraFarMinusNear - cameraFar );",
  955. "return smoothstep( fogNear, fogFar, depth );",
  956. "}",
  957. "float readDepth( const in vec2 coord ) {",
  958. //"return ( 2.0 * cameraNear ) / ( cameraFar + cameraNear - unpackDepth( texture2D( tDepth, coord ) ) * ( cameraFar - cameraNear ) );",
  959. "return cameraCoef / ( cameraFarPlusNear - unpackDepth( texture2D( tDepth, coord ) ) * cameraFarMinusNear );",
  960. "}",
  961. "float compareDepths( const in float depth1, const in float depth2, inout int far ) {",
  962. "float garea = 2.0;", // gauss bell width
  963. "float diff = ( depth1 - depth2 ) * 100.0;", // depth difference (0-100)
  964. // reduce left bell width to avoid self-shadowing
  965. "if ( diff < gDisplace ) {",
  966. "garea = diffArea;",
  967. "} else {",
  968. "far = 1;",
  969. "}",
  970. "float dd = diff - gDisplace;",
  971. "float gauss = pow( EULER, -2.0 * dd * dd / ( garea * garea ) );",
  972. "return gauss;",
  973. "}",
  974. "float calcAO( float depth, float dw, float dh ) {",
  975. "float dd = radius - depth * radius;",
  976. "vec2 vv = vec2( dw, dh );",
  977. "vec2 coord1 = vUv + dd * vv;",
  978. "vec2 coord2 = vUv - dd * vv;",
  979. "float temp1 = 0.0;",
  980. "float temp2 = 0.0;",
  981. "int far = 0;",
  982. "temp1 = compareDepths( depth, readDepth( coord1 ), far );",
  983. // DEPTH EXTRAPOLATION
  984. "if ( far > 0 ) {",
  985. "temp2 = compareDepths( readDepth( coord2 ), depth, far );",
  986. "temp1 += ( 1.0 - temp1 ) * temp2;",
  987. "}",
  988. "return temp1;",
  989. "}",
  990. "void main() {",
  991. "vec2 noise = rand( vUv );",
  992. "float depth = readDepth( vUv );",
  993. "float tt = clamp( depth, aoClamp, 1.0 );",
  994. "float w = ( 1.0 / width ) / tt + ( noise.x * ( 1.0 - noise.x ) );",
  995. "float h = ( 1.0 / height ) / tt + ( noise.y * ( 1.0 - noise.y ) );",
  996. "float pw;",
  997. "float ph;",
  998. "float ao;",
  999. "float dz = 1.0 / float( samples );",
  1000. "float z = 1.0 - dz / 2.0;",
  1001. "float l = 0.0;",
  1002. "for ( int i = 0; i <= samples; i ++ ) {",
  1003. "float r = sqrt( 1.0 - z );",
  1004. "pw = cos( l ) * r;",
  1005. "ph = sin( l ) * r;",
  1006. "ao += calcAO( depth, pw * w, ph * h );",
  1007. "z = z - dz;",
  1008. "l = l + DL;",
  1009. "}",
  1010. "ao /= float( samples );",
  1011. "ao = 1.0 - ao;",
  1012. "if ( fogEnabled ) {",
  1013. "ao = mix( ao, 1.0, doFog() );",
  1014. "}",
  1015. "vec3 color = texture2D( tDiffuse, vUv ).rgb;",
  1016. "vec3 lumcoeff = vec3( 0.299, 0.587, 0.114 );",
  1017. "float lum = dot( color.rgb, lumcoeff );",
  1018. "vec3 luminance = vec3( lum );",
  1019. "vec3 final = vec3( color * mix( vec3( ao ), vec3( 1.0 ), luminance * lumInfluence ) );", // mix( color * ao, white, luminance )
  1020. "if ( onlyAO ) {",
  1021. "final = onlyAOColor * vec3( mix( vec3( ao ), vec3( 1.0 ), luminance * lumInfluence ) );", // ambient occlusion only
  1022. "}",
  1023. "gl_FragColor = vec4( final, 1.0 );",
  1024. "}"
  1025. ].join("\n")
  1026. },
  1027. /* -------------------------------------------------------------------------
  1028. // Colorify shader
  1029. ------------------------------------------------------------------------- */
  1030. 'colorify': {
  1031. uniforms: {
  1032. tDiffuse: { type: "t", value: null },
  1033. color: { type: "c", value: new THREE.Color( 0xffffff ) }
  1034. },
  1035. vertexShader: [
  1036. "varying vec2 vUv;",
  1037. "void main() {",
  1038. "vUv = uv;",
  1039. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  1040. "}"
  1041. ].join("\n"),
  1042. fragmentShader: [
  1043. "uniform vec3 color;",
  1044. "uniform sampler2D tDiffuse;",
  1045. "varying vec2 vUv;",
  1046. "void main() {",
  1047. "vec4 texel = texture2D( tDiffuse, vUv );",
  1048. "vec3 luma = vec3( 0.299, 0.587, 0.114 );",
  1049. "float v = dot( texel.xyz, luma );",
  1050. "gl_FragColor = vec4( v * color, texel.w );",
  1051. "}"
  1052. ].join("\n")
  1053. },
  1054. /* -------------------------------------------------------------------------
  1055. // Unpack RGBA depth shader
  1056. // - show RGBA encoded depth as monochrome color
  1057. ------------------------------------------------------------------------- */
  1058. 'unpackDepthRGBA': {
  1059. uniforms: {
  1060. tDiffuse: { type: "t", value: null },
  1061. opacity: { type: "f", value: 1.0 }
  1062. },
  1063. vertexShader: [
  1064. "varying vec2 vUv;",
  1065. "void main() {",
  1066. "vUv = uv;",
  1067. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  1068. "}"
  1069. ].join("\n"),
  1070. fragmentShader: [
  1071. "uniform float opacity;",
  1072. "uniform sampler2D tDiffuse;",
  1073. "varying vec2 vUv;",
  1074. // RGBA depth
  1075. "float unpackDepth( const in vec4 rgba_depth ) {",
  1076. "const vec4 bit_shift = vec4( 1.0 / ( 256.0 * 256.0 * 256.0 ), 1.0 / ( 256.0 * 256.0 ), 1.0 / 256.0, 1.0 );",
  1077. "float depth = dot( rgba_depth, bit_shift );",
  1078. "return depth;",
  1079. "}",
  1080. "void main() {",
  1081. "float depth = 1.0 - unpackDepth( texture2D( tDiffuse, vUv ) );",
  1082. "gl_FragColor = opacity * vec4( vec3( depth ), 1.0 );",
  1083. "}"
  1084. ].join("\n")
  1085. },
  1086. // METHODS
  1087. buildKernel: function( sigma ) {
  1088. // We lop off the sqrt(2 * pi) * sigma term, since we're going to normalize anyway.
  1089. function gauss( x, sigma ) {
  1090. return Math.exp( - ( x * x ) / ( 2.0 * sigma * sigma ) );
  1091. }
  1092. var i, values, sum, halfWidth, kMaxKernelSize = 25, kernelSize = 2 * Math.ceil( sigma * 3.0 ) + 1;
  1093. if ( kernelSize > kMaxKernelSize ) kernelSize = kMaxKernelSize;
  1094. halfWidth = ( kernelSize - 1 ) * 0.5;
  1095. values = new Array( kernelSize );
  1096. sum = 0.0;
  1097. for ( i = 0; i < kernelSize; ++i ) {
  1098. values[ i ] = gauss( i - halfWidth, sigma );
  1099. sum += values[ i ];
  1100. }
  1101. // normalize the kernel
  1102. for ( i = 0; i < kernelSize; ++i ) values[ i ] /= sum;
  1103. return values;
  1104. }
  1105. };