ShaderExtras.js 44 KB

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