ShaderExtras.js 47 KB

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