ShaderExtras.js 42 KB

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