ShaderExtras.js 44 KB

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