ShaderExtras.js 43 KB

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