ShaderExtras.js 44 KB

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