ShaderExtras.js 45 KB

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