ShaderExtras.js 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  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. */
  24. THREE.ShaderExtras = {
  25. /* -------------------------------------------------------------------------
  26. // Full-screen textured quad shader
  27. ------------------------------------------------------------------------- */
  28. 'screen': {
  29. uniforms: {
  30. tDiffuse: { type: "t", value: 0, texture: null },
  31. opacity: { type: "f", value: 1.0 }
  32. },
  33. vertexShader: [
  34. "varying vec2 vUv;",
  35. "void main() {",
  36. "vUv = vec2( uv.x, 1.0 - uv.y );",
  37. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  38. "}"
  39. ].join("\n"),
  40. fragmentShader: [
  41. "uniform float opacity;",
  42. "uniform sampler2D tDiffuse;",
  43. "varying vec2 vUv;",
  44. "void main() {",
  45. "vec4 texel = texture2D( tDiffuse, vUv );",
  46. "gl_FragColor = opacity * texel;",
  47. "}"
  48. ].join("\n")
  49. },
  50. /* ------------------------------------------------------------------------
  51. // Convolution shader
  52. // - ported from o3d sample to WebGL / GLSL
  53. // http://o3d.googlecode.com/svn/trunk/samples/convolution.html
  54. ------------------------------------------------------------------------ */
  55. 'convolution': {
  56. uniforms: {
  57. "tDiffuse" : { type: "t", value: 0, texture: null },
  58. "uImageIncrement" : { type: "v2", value: new THREE.Vector2( 0.001953125, 0.0 ) },
  59. "cKernel" : { type: "fv1", value: [] }
  60. },
  61. vertexShader: [
  62. //"#define KERNEL_SIZE 25.0",
  63. "uniform vec2 uImageIncrement;",
  64. "varying vec2 vUv;",
  65. "void main() {",
  66. "vUv = uv - ( ( KERNEL_SIZE - 1.0 ) / 2.0 ) * uImageIncrement;",
  67. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  68. "}"
  69. ].join("\n"),
  70. fragmentShader: [
  71. //"#define KERNEL_SIZE 25",
  72. "uniform float cKernel[ KERNEL_SIZE ];",
  73. "uniform sampler2D tDiffuse;",
  74. "uniform vec2 uImageIncrement;",
  75. "varying vec2 vUv;",
  76. "void main() {",
  77. "vec2 imageCoord = vUv;",
  78. "vec4 sum = vec4( 0.0, 0.0, 0.0, 0.0 );",
  79. "for( int i = 0; i < KERNEL_SIZE; i ++ ) {",
  80. "sum += texture2D( tDiffuse, imageCoord ) * cKernel[ i ];",
  81. "imageCoord += uImageIncrement;",
  82. "}",
  83. "gl_FragColor = sum;",
  84. "}"
  85. ].join("\n")
  86. },
  87. /* -------------------------------------------------------------------------
  88. // Film grain & scanlines shader
  89. // - ported from HLSL to WebGL / GLSL
  90. // http://www.truevision3d.com/forums/showcase/staticnoise_colorblackwhite_scanline_shaders-t18698.0.html
  91. // Screen Space Static Postprocessor
  92. //
  93. // Produces an analogue noise overlay similar to a film grain / TV static
  94. //
  95. // Original implementation and noise algorithm
  96. // Pat 'Hawthorne' Shearon
  97. //
  98. // Optimized scanlines + noise version with intensity scaling
  99. // Georg 'Leviathan' Steinrohder
  100. // This version is provided under a Creative Commons Attribution 3.0 License
  101. // http://creativecommons.org/licenses/by/3.0/
  102. ------------------------------------------------------------------------- */
  103. 'film': {
  104. uniforms: {
  105. tDiffuse: { type: "t", value: 0, texture: null },
  106. time: { type: "f", value: 0.0 },
  107. nIntensity: { type: "f", value: 0.5 },
  108. sIntensity: { type: "f", value: 0.05 },
  109. sCount: { type: "f", value: 4096 },
  110. grayscale: { type: "i", value: 1 }
  111. },
  112. vertexShader: [
  113. "varying vec2 vUv;",
  114. "void main() {",
  115. "vUv = vec2( uv.x, 1.0 - uv.y );",
  116. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  117. "}"
  118. ].join("\n"),
  119. fragmentShader: [
  120. // control parameter
  121. "uniform float time;",
  122. "uniform bool grayscale;",
  123. // noise effect intensity value (0 = no effect, 1 = full effect)
  124. "uniform float nIntensity;",
  125. // scanlines effect intensity value (0 = no effect, 1 = full effect)
  126. "uniform float sIntensity;",
  127. // scanlines effect count value (0 = no effect, 4096 = full effect)
  128. "uniform float sCount;",
  129. "uniform sampler2D tDiffuse;",
  130. "varying vec2 vUv;",
  131. "void main() {",
  132. // sample the source
  133. "vec4 cTextureScreen = texture2D( tDiffuse, vUv );",
  134. // make some noise
  135. "float x = vUv.x * vUv.y * time * 1000.0;",
  136. "x = mod( x, 13.0 ) * mod( x, 123.0 );",
  137. "float dx = mod( x, 0.01 );",
  138. // add noise
  139. "vec3 cResult = cTextureScreen.rgb + cTextureScreen.rgb * clamp( 0.1 + dx * 100.0, 0.0, 1.0 );",
  140. // get us a sine and cosine
  141. "vec2 sc = vec2( sin( vUv.y * sCount ), cos( vUv.y * sCount ) );",
  142. // add scanlines
  143. "cResult += cTextureScreen.rgb * vec3( sc.x, sc.y, sc.x ) * sIntensity;",
  144. // interpolate between source and result by intensity
  145. "cResult = cTextureScreen.rgb + clamp( nIntensity, 0.0,1.0 ) * ( cResult - cTextureScreen.rgb );",
  146. // convert to grayscale if desired
  147. "if( grayscale ) {",
  148. "cResult = vec3( cResult.r * 0.3 + cResult.g * 0.59 + cResult.b * 0.11 );",
  149. "}",
  150. "gl_FragColor = vec4( cResult, cTextureScreen.a );",
  151. "}"
  152. ].join("\n")
  153. },
  154. /* -------------------------------------------------------------------------
  155. // Depth-of-field shader with bokeh
  156. // ported from GLSL shader by Martins Upitis
  157. // http://artmartinsh.blogspot.com/2010/02/glsl-lens-blur-filter-with-bokeh.html
  158. ------------------------------------------------------------------------- */
  159. 'bokeh' : {
  160. uniforms: { tColor: { type: "t", value: 0, texture: null },
  161. tDepth: { type: "t", value: 1, texture: null },
  162. focus: { type: "f", value: 1.0 },
  163. aspect: { type: "f", value: 1.0 },
  164. aperture: { type: "f", value: 0.025 },
  165. maxblur: { type: "f", value: 1.0 },
  166. },
  167. vertexShader: [
  168. "varying vec2 vUv;",
  169. "void main() {",
  170. "vUv = vec2( uv.x, 1.0 - uv.y );",
  171. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  172. "}"
  173. ].join("\n"),
  174. fragmentShader: [
  175. "varying vec2 vUv;",
  176. "uniform sampler2D tColor;",
  177. "uniform sampler2D tDepth;",
  178. "uniform float maxblur;", // max blur amount
  179. "uniform float aperture;", // aperture - bigger values for shallower depth of field
  180. "uniform float focus;",
  181. "uniform float aspect;",
  182. "void main() {",
  183. "vec2 aspectcorrect = vec2( 1.0, aspect );",
  184. "vec4 depth1 = texture2D( tDepth, vUv );",
  185. "float factor = depth1.x - focus;",
  186. "vec2 dofblur = vec2 ( clamp( factor * aperture, -maxblur, maxblur ) );",
  187. "vec2 dofblur9 = dofblur * 0.9;",
  188. "vec2 dofblur7 = dofblur * 0.7;",
  189. "vec2 dofblur4 = dofblur * 0.4;",
  190. "vec4 col = vec4( 0.0 );",
  191. "col += texture2D( tColor, vUv.xy );",
  192. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, 0.4 ) * aspectcorrect ) * dofblur );",
  193. "col += texture2D( tColor, vUv.xy + ( vec2( 0.15, 0.37 ) * aspectcorrect ) * dofblur );",
  194. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, 0.29 ) * aspectcorrect ) * dofblur );",
  195. "col += texture2D( tColor, vUv.xy + ( vec2( -0.37, 0.15 ) * aspectcorrect ) * dofblur );",
  196. "col += texture2D( tColor, vUv.xy + ( vec2( 0.40, 0.0 ) * aspectcorrect ) * dofblur );",
  197. "col += texture2D( tColor, vUv.xy + ( vec2( 0.37, -0.15 ) * aspectcorrect ) * dofblur );",
  198. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, -0.29 ) * aspectcorrect ) * dofblur );",
  199. "col += texture2D( tColor, vUv.xy + ( vec2( -0.15, -0.37 ) * aspectcorrect ) * dofblur );",
  200. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, -0.4 ) * aspectcorrect ) * dofblur );",
  201. "col += texture2D( tColor, vUv.xy + ( vec2( -0.15, 0.37 ) * aspectcorrect ) * dofblur );",
  202. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, 0.29 ) * aspectcorrect ) * dofblur );",
  203. "col += texture2D( tColor, vUv.xy + ( vec2( 0.37, 0.15 ) * aspectcorrect ) * dofblur );",
  204. "col += texture2D( tColor, vUv.xy + ( vec2( -0.4, 0.0 ) * aspectcorrect ) * dofblur );",
  205. "col += texture2D( tColor, vUv.xy + ( vec2( -0.37, -0.15 ) * aspectcorrect ) * dofblur );",
  206. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, -0.29 ) * aspectcorrect ) * dofblur );",
  207. "col += texture2D( tColor, vUv.xy + ( vec2( 0.15, -0.37 ) * aspectcorrect ) * dofblur );",
  208. "col += texture2D( tColor, vUv.xy + ( vec2( 0.15, 0.37 ) * aspectcorrect ) * dofblur9 );",
  209. "col += texture2D( tColor, vUv.xy + ( vec2( -0.37, 0.15 ) * aspectcorrect ) * dofblur9 );",
  210. "col += texture2D( tColor, vUv.xy + ( vec2( 0.37, -0.15 ) * aspectcorrect ) * dofblur9 );",
  211. "col += texture2D( tColor, vUv.xy + ( vec2( -0.15, -0.37 ) * aspectcorrect ) * dofblur9 );",
  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.29, 0.29 ) * aspectcorrect ) * dofblur7 );",
  217. "col += texture2D( tColor, vUv.xy + ( vec2( 0.40, 0.0 ) * aspectcorrect ) * dofblur7 );",
  218. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, -0.29 ) * aspectcorrect ) * dofblur7 );",
  219. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, -0.4 ) * aspectcorrect ) * dofblur7 );",
  220. "col += texture2D( tColor, vUv.xy + ( vec2( -0.29, 0.29 ) * aspectcorrect ) * dofblur7 );",
  221. "col += texture2D( tColor, vUv.xy + ( vec2( -0.4, 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 ) * dofblur4 );",
  225. "col += texture2D( tColor, vUv.xy + ( vec2( 0.4, 0.0 ) * aspectcorrect ) * dofblur4 );",
  226. "col += texture2D( tColor, vUv.xy + ( vec2( 0.29, -0.29 ) * aspectcorrect ) * dofblur4 );",
  227. "col += texture2D( tColor, vUv.xy + ( vec2( 0.0, -0.4 ) * aspectcorrect ) * dofblur4 );",
  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. "gl_FragColor = col / 41.0;",
  233. "gl_FragColor.a = 1.0;",
  234. "}"
  235. ].join("\n")
  236. },
  237. /* -------------------------------------------------------------------------
  238. // Depth-of-field shader using mipmaps
  239. // - from Matt Handley @applmak
  240. // - requires power-of-2 sized render target with enabled mipmaps
  241. ------------------------------------------------------------------------- */
  242. 'dofmipmap': {
  243. uniforms: {
  244. tColor: { type: "t", value: 0, texture: null },
  245. tDepth: { type: "t", value: 1, texture: null },
  246. focus: { type: "f", value: 1.0 },
  247. maxblur: { type: "f", value: 1.0 }
  248. },
  249. vertexShader: [
  250. "varying vec2 vUv;",
  251. "void main() {",
  252. "vUv = vec2( uv.x, 1.0 - uv.y );",
  253. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  254. "}"
  255. ].join("\n"),
  256. fragmentShader: [
  257. "uniform float focus;",
  258. "uniform float maxblur;",
  259. "uniform sampler2D tColor;",
  260. "uniform sampler2D tDepth;",
  261. "varying vec2 vUv;",
  262. "void main() {",
  263. "vec4 depth = texture2D( tDepth, vUv );",
  264. "float factor = depth.x - focus;",
  265. "vec4 col = texture2D( tColor, vUv, 2.0 * maxblur * abs( focus - depth.x ) );",
  266. "gl_FragColor = col;",
  267. "gl_FragColor.a = 1.0;",
  268. "}"
  269. ].join("\n")
  270. },
  271. /* -------------------------------------------------------------------------
  272. // Sepia tone shader
  273. // - based on glfx.js sepia shader
  274. // https://github.com/evanw/glfx.js
  275. ------------------------------------------------------------------------- */
  276. 'sepia': {
  277. uniforms: {
  278. tDiffuse: { type: "t", value: 0, texture: null },
  279. amount: { type: "f", value: 1.0 }
  280. },
  281. vertexShader: [
  282. "varying vec2 vUv;",
  283. "void main() {",
  284. "vUv = vec2( uv.x, 1.0 - uv.y );",
  285. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  286. "}"
  287. ].join("\n"),
  288. fragmentShader: [
  289. "uniform float amount;",
  290. "uniform sampler2D tDiffuse;",
  291. "varying vec2 vUv;",
  292. "void main() {",
  293. "vec4 color = texture2D( tDiffuse, vUv );",
  294. "vec3 c = color.rgb;",
  295. "color.r = dot( c, vec3( 1.0 - 0.607 * amount, 0.769 * amount, 0.189 * amount ) );",
  296. "color.g = dot( c, vec3( 0.349 * amount, 1.0 - 0.314 * amount, 0.168 * amount ) );",
  297. "color.b = dot( c, vec3( 0.272 * amount, 0.534 * amount, 1.0 - 0.869 * amount ) );",
  298. "gl_FragColor = vec4( min( vec3( 1.0 ), color.rgb ), color.a );",
  299. "}"
  300. ].join("\n")
  301. },
  302. /* -------------------------------------------------------------------------
  303. // Dot screen shader
  304. // - based on glfx.js sepia shader
  305. // https://github.com/evanw/glfx.js
  306. ------------------------------------------------------------------------- */
  307. 'dotscreen': {
  308. uniforms: {
  309. tDiffuse: { type: "t", value: 0, texture: null },
  310. tSize: { type: "v2", value: new THREE.Vector2( 256, 256 ) },
  311. center: { type: "v2", value: new THREE.Vector2( 0.5, 0.5 ) },
  312. angle: { type: "f", value: 1.57 },
  313. scale: { type: "f", value: 1.0 }
  314. },
  315. vertexShader: [
  316. "varying vec2 vUv;",
  317. "void main() {",
  318. "vUv = vec2( uv.x, 1.0 - uv.y );",
  319. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  320. "}"
  321. ].join("\n"),
  322. fragmentShader: [
  323. "uniform vec2 center;",
  324. "uniform float angle;",
  325. "uniform float scale;",
  326. "uniform vec2 tSize;",
  327. "uniform sampler2D tDiffuse;",
  328. "varying vec2 vUv;",
  329. "float pattern() {",
  330. "float s = sin( angle ), c = cos( angle );",
  331. "vec2 tex = vUv * tSize - center;",
  332. "vec2 point = vec2( c * tex.x - s * tex.y, s * tex.x + c * tex.y ) * scale;",
  333. "return ( sin( point.x ) * sin( point.y ) ) * 4.0;",
  334. "}",
  335. "void main() {",
  336. "vec4 color = texture2D( tDiffuse, vUv );",
  337. "float average = ( color.r + color.g + color.b ) / 3.0;",
  338. "gl_FragColor = vec4( vec3( average * 10.0 - 5.0 + pattern() ), color.a );",
  339. "}"
  340. ].join("\n")
  341. },
  342. /* ------------------------------------------------------------------------------------------------
  343. // Vignette shader
  344. // - based on PaintEffect postprocess from ro.me
  345. // http://code.google.com/p/3-dreams-of-black/source/browse/deploy/js/effects/PaintEffect.js
  346. ------------------------------------------------------------------------------------------------ */
  347. 'vignette': {
  348. uniforms: {
  349. tDiffuse: { type: "t", value: 0, texture: null },
  350. offset: { type: "f", value: 1.0 },
  351. darkness: { type: "f", value: 1.0 }
  352. },
  353. vertexShader: [
  354. "varying vec2 vUv;",
  355. "void main() {",
  356. "vUv = vec2( uv.x, 1.0 - uv.y );",
  357. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  358. "}"
  359. ].join("\n"),
  360. fragmentShader: [
  361. "uniform float offset;",
  362. "uniform float darkness;",
  363. "uniform sampler2D tDiffuse;",
  364. "varying vec2 vUv;",
  365. "void main() {",
  366. // Eskil's vignette
  367. "vec4 texel = texture2D( tDiffuse, vUv );",
  368. "vec2 uv = ( vUv - vec2( 0.5 ) ) * vec2( offset );",
  369. "gl_FragColor = vec4( mix( texel.rgb, vec3( 1.0 - darkness ), dot( uv, uv ) ), texel.a );",
  370. /*
  371. // alternative version from glfx.js
  372. // this one makes more "dusty" look (as opposed to "burned")
  373. "vec4 color = texture2D( tDiffuse, vUv );",
  374. "float dist = distance( vUv, vec2( 0.5 ) );",
  375. "color.rgb *= smoothstep( 0.8, offset * 0.799, dist *( darkness + offset ) );",
  376. "gl_FragColor = color;",
  377. */
  378. "}"
  379. ].join("\n")
  380. },
  381. /* -------------------------------------------------------------------------
  382. // Bleach bypass shader [http://en.wikipedia.org/wiki/Bleach_bypass]
  383. // - based on Nvidia example
  384. // http://developer.download.nvidia.com/shaderlibrary/webpages/shader_library.html#post_bleach_bypass
  385. ------------------------------------------------------------------------- */
  386. 'bleachbypass': {
  387. uniforms: {
  388. tDiffuse: { type: "t", value: 0, texture: null },
  389. opacity: { type: "f", value: 1.0 }
  390. },
  391. vertexShader: [
  392. "varying vec2 vUv;",
  393. "void main() {",
  394. "vUv = vec2( uv.x, 1.0 - uv.y );",
  395. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  396. "}"
  397. ].join("\n"),
  398. fragmentShader: [
  399. "uniform float opacity;",
  400. "uniform sampler2D tDiffuse;",
  401. "varying vec2 vUv;",
  402. "void main() {",
  403. "vec4 base = texture2D( tDiffuse, vUv );",
  404. "vec3 lumCoeff = vec3( 0.25, 0.65, 0.1 );",
  405. "float lum = dot( lumCoeff, base.rgb );",
  406. "vec3 blend = vec3( lum );",
  407. "float L = min( 1.0, max( 0.0, 10.0 * ( lum - 0.45 ) ) );",
  408. "vec3 result1 = 2.0 * base.rgb * blend;",
  409. "vec3 result2 = 1.0 - 2.0 * ( 1.0 - blend ) * ( 1.0 - base.rgb );",
  410. "vec3 newColor = mix( result1, result2, L );",
  411. "float A2 = opacity * base.a;",
  412. "vec3 mixRGB = A2 * newColor.rgb;",
  413. "mixRGB += ( ( 1.0 - A2 ) * base.rgb );",
  414. "gl_FragColor = vec4( mixRGB, base.a );",
  415. "}"
  416. ].join("\n")
  417. },
  418. /* --------------------------------------------------------------------------------------------------
  419. // Focus shader
  420. // - based on PaintEffect postprocess from ro.me
  421. // http://code.google.com/p/3-dreams-of-black/source/browse/deploy/js/effects/PaintEffect.js
  422. -------------------------------------------------------------------------------------------------- */
  423. 'focus': {
  424. uniforms : {
  425. "tDiffuse": { type: "t", value: 0, texture: null },
  426. "screenWidth": { type: "f", value: 1024 },
  427. "screenHeight": { type: "f", value: 1024 },
  428. "sampleDistance": { type: "f", value: 0.94 },
  429. "waveFactor": { type: "f", value: 0.00125 }
  430. },
  431. vertexShader: [
  432. "varying vec2 vUv;",
  433. "void main() {",
  434. "vUv = vec2( uv.x, 1.0 - uv.y );",
  435. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  436. "}"
  437. ].join("\n"),
  438. fragmentShader: [
  439. "uniform float screenWidth;",
  440. "uniform float screenHeight;",
  441. "uniform float sampleDistance;",
  442. "uniform float waveFactor;",
  443. "uniform sampler2D tDiffuse;",
  444. "varying vec2 vUv;",
  445. "void main() {",
  446. "vec4 color, org, tmp, add;",
  447. "float sample_dist, f;",
  448. "vec2 vin;",
  449. "vec2 uv = vUv;",
  450. "add += color = org = texture2D( tDiffuse, uv );",
  451. "vin = ( uv - vec2( 0.5 ) ) * vec2( 1.4 );",
  452. "sample_dist = dot( vin, vin ) * 2.0;",
  453. "f = ( waveFactor * 100.0 + sample_dist ) * sampleDistance * 4.0;",
  454. "vec2 sampleSize = vec2( 1.0 / screenWidth, 1.0 / screenHeight ) * vec2( f );",
  455. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.111964, 0.993712 ) * sampleSize );",
  456. "if( tmp.b < color.b ) color = tmp;",
  457. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.846724, 0.532032 ) * sampleSize );",
  458. "if( tmp.b < color.b ) color = tmp;",
  459. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.943883, -0.330279 ) * sampleSize );",
  460. "if( tmp.b < color.b ) color = tmp;",
  461. "add += tmp = texture2D( tDiffuse, uv + vec2( 0.330279, -0.943883 ) * sampleSize );",
  462. "if( tmp.b < color.b ) color = tmp;",
  463. "add += tmp = texture2D( tDiffuse, uv + vec2( -0.532032, -0.846724 ) * sampleSize );",
  464. "if( tmp.b < color.b ) color = tmp;",
  465. "add += tmp = texture2D( tDiffuse, uv + vec2( -0.993712, -0.111964 ) * sampleSize );",
  466. "if( tmp.b < color.b ) color = tmp;",
  467. "add += tmp = texture2D( tDiffuse, uv + vec2( -0.707107, 0.707107 ) * sampleSize );",
  468. "if( tmp.b < color.b ) color = tmp;",
  469. "color = color * vec4( 2.0 ) - ( add / vec4( 8.0 ) );",
  470. "color = color + ( add / vec4( 8.0 ) - color ) * ( vec4( 1.0 ) - vec4( sample_dist * 0.5 ) );",
  471. "gl_FragColor = vec4( color.rgb * color.rgb * vec3( 0.95 ) + color.rgb, 1.0 );",
  472. "}"
  473. ].join("\n")
  474. },
  475. /* -------------------------------------------------------------------------
  476. // Triangle blur shader
  477. // - based on glfx.js triangle blur shader
  478. // https://github.com/evanw/glfx.js
  479. // A basic blur filter, which convolves the image with a
  480. // pyramid filter. The pyramid filter is separable and is applied as two
  481. // perpendicular triangle filters.
  482. ------------------------------------------------------------------------- */
  483. 'triangleBlur': {
  484. uniforms : {
  485. "texture": { type: "t", value: 0, texture: null },
  486. "delta": { type: "v2", value:new THREE.Vector2( 1, 1 ) }
  487. },
  488. vertexShader: [
  489. "varying vec2 vUv;",
  490. "void main() {",
  491. "vUv = vec2( uv.x, 1.0 - uv.y );",
  492. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  493. "}"
  494. ].join("\n"),
  495. fragmentShader: [
  496. "#define ITERATIONS 10.0",
  497. "uniform sampler2D texture;",
  498. "uniform vec2 delta;",
  499. "varying vec2 vUv;",
  500. "float random( vec3 scale, float seed ) {",
  501. // use the fragment position for a different seed per-pixel
  502. "return fract( sin( dot( gl_FragCoord.xyz + seed, scale ) ) * 43758.5453 + seed );",
  503. "}",
  504. "void main() {",
  505. "vec4 color = vec4( 0.0 );",
  506. "float total = 0.0;",
  507. // randomize the lookup values to hide the fixed number of samples
  508. "float offset = random( vec3( 12.9898, 78.233, 151.7182 ), 0.0 );",
  509. "for ( float t = -ITERATIONS; t <= ITERATIONS; t ++ ) {",
  510. "float percent = ( t + offset - 0.5 ) / ITERATIONS;",
  511. "float weight = 1.0 - abs( percent );",
  512. "color += texture2D( texture, vUv + delta * percent ) * weight;",
  513. "total += weight;",
  514. "}",
  515. "gl_FragColor = color / total;",
  516. "}",
  517. ].join("\n")
  518. },
  519. /* -------------------------------------------------------------------------
  520. // Simple test shader
  521. ------------------------------------------------------------------------- */
  522. 'basic': {
  523. uniforms: {},
  524. vertexShader: [
  525. "void main() {",
  526. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  527. "}"
  528. ].join("\n"),
  529. fragmentShader: [
  530. "void main() {",
  531. "gl_FragColor = vec4( 1.0, 0.0, 0.0, 0.5 );",
  532. "}"
  533. ].join("\n")
  534. },
  535. /* --------------------------------------------------------------------------------------------------
  536. // Two pass Gaussian blur filter (horizontal and vertical blur shaders)
  537. // - described in http://www.gamerendering.com/2008/10/11/gaussian-blur-filter-shader/
  538. // and used in http://www.cake23.de/traveling-wavefronts-lit-up.html
  539. //
  540. // - 9 samples per pass
  541. // - standard deviation 2.7
  542. // - "h" and "v" parameters should be set to "1 / width" and "1 / height"
  543. -------------------------------------------------------------------------------------------------- */
  544. 'horizontalBlur': {
  545. uniforms: {
  546. "tDiffuse": { type: "t", value: 0, texture: null },
  547. "h": { type: "f", value: 1.0 / 512.0 }
  548. },
  549. vertexShader: [
  550. "varying vec2 vUv;",
  551. "void main() {",
  552. "vUv = vec2( uv.x, 1.0 - uv.y );",
  553. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  554. "}"
  555. ].join("\n"),
  556. fragmentShader: [
  557. "uniform sampler2D tDiffuse;",
  558. "uniform float h;",
  559. "varying vec2 vUv;",
  560. "void main() {",
  561. "vec4 sum = vec4( 0.0 );",
  562. "sum += texture2D( tDiffuse, vec2( vUv.x - 4.0 * h, vUv.y ) ) * 0.051;",
  563. "sum += texture2D( tDiffuse, vec2( vUv.x - 3.0 * h, vUv.y ) ) * 0.0918;",
  564. "sum += texture2D( tDiffuse, vec2( vUv.x - 2.0 * h, vUv.y ) ) * 0.12245;",
  565. "sum += texture2D( tDiffuse, vec2( vUv.x - 1.0 * h, vUv.y ) ) * 0.1531;",
  566. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",
  567. "sum += texture2D( tDiffuse, vec2( vUv.x + 1.0 * h, vUv.y ) ) * 0.1531;",
  568. "sum += texture2D( tDiffuse, vec2( vUv.x + 2.0 * h, vUv.y ) ) * 0.12245;",
  569. "sum += texture2D( tDiffuse, vec2( vUv.x + 3.0 * h, vUv.y ) ) * 0.0918;",
  570. "sum += texture2D( tDiffuse, vec2( vUv.x + 4.0 * h, vUv.y ) ) * 0.051;",
  571. "gl_FragColor = sum;",
  572. "}"
  573. ].join("\n")
  574. },
  575. 'verticalBlur': {
  576. uniforms: {
  577. "tDiffuse": { type: "t", value: 0, texture: null },
  578. "v": { type: "f", value: 1.0 / 512.0 }
  579. },
  580. vertexShader: [
  581. "varying vec2 vUv;",
  582. "void main() {",
  583. "vUv = vec2( uv.x, 1.0 - uv.y );",
  584. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  585. "}"
  586. ].join("\n"),
  587. fragmentShader: [
  588. "uniform sampler2D tDiffuse;",
  589. "uniform float v;",
  590. "varying vec2 vUv;",
  591. "void main() {",
  592. "vec4 sum = vec4( 0.0 );",
  593. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 4.0 * v ) ) * 0.051;",
  594. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 3.0 * v ) ) * 0.0918;",
  595. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 2.0 * v ) ) * 0.12245;",
  596. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 1.0 * v ) ) * 0.1531;",
  597. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",
  598. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 1.0 * v ) ) * 0.1531;",
  599. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 2.0 * v ) ) * 0.12245;",
  600. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 3.0 * v ) ) * 0.0918;",
  601. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 4.0 * v ) ) * 0.051;",
  602. "gl_FragColor = sum;",
  603. "}"
  604. ].join("\n")
  605. },
  606. /* --------------------------------------------------------------------------------------------------
  607. // Simple fake tilt-shift effect, modulating two pass Gaussian blur (see above) by vertical position
  608. //
  609. // - 9 samples per pass
  610. // - standard deviation 2.7
  611. // - "h" and "v" parameters should be set to "1 / width" and "1 / height"
  612. // - "r" parameter control where "focused" horizontal line lies
  613. -------------------------------------------------------------------------------------------------- */
  614. 'horizontalTiltShift': {
  615. uniforms: {
  616. "tDiffuse": { type: "t", value: 0, texture: null },
  617. "h": { type: "f", value: 1.0 / 512.0 },
  618. "r": { type: "f", value: 0.35 }
  619. },
  620. vertexShader: [
  621. "varying vec2 vUv;",
  622. "void main() {",
  623. "vUv = vec2( uv.x, 1.0 - uv.y );",
  624. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  625. "}"
  626. ].join("\n"),
  627. fragmentShader: [
  628. "uniform sampler2D tDiffuse;",
  629. "uniform float h;",
  630. "uniform float r;",
  631. "varying vec2 vUv;",
  632. "void main() {",
  633. "vec4 sum = vec4( 0.0 );",
  634. "float hh = h * abs( r - vUv.y );",
  635. "sum += texture2D( tDiffuse, vec2( vUv.x - 4.0 * hh, vUv.y ) ) * 0.051;",
  636. "sum += texture2D( tDiffuse, vec2( vUv.x - 3.0 * hh, vUv.y ) ) * 0.0918;",
  637. "sum += texture2D( tDiffuse, vec2( vUv.x - 2.0 * hh, vUv.y ) ) * 0.12245;",
  638. "sum += texture2D( tDiffuse, vec2( vUv.x - 1.0 * hh, vUv.y ) ) * 0.1531;",
  639. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",
  640. "sum += texture2D( tDiffuse, vec2( vUv.x + 1.0 * hh, vUv.y ) ) * 0.1531;",
  641. "sum += texture2D( tDiffuse, vec2( vUv.x + 2.0 * hh, vUv.y ) ) * 0.12245;",
  642. "sum += texture2D( tDiffuse, vec2( vUv.x + 3.0 * hh, vUv.y ) ) * 0.0918;",
  643. "sum += texture2D( tDiffuse, vec2( vUv.x + 4.0 * hh, vUv.y ) ) * 0.051;",
  644. "gl_FragColor = sum;",
  645. "}"
  646. ].join("\n")
  647. },
  648. 'verticalTiltShift': {
  649. uniforms: {
  650. "tDiffuse": { type: "t", value: 0, texture: null },
  651. "v": { type: "f", value: 1.0 / 512.0 },
  652. "r": { type: "f", value: 0.35 }
  653. },
  654. vertexShader: [
  655. "varying vec2 vUv;",
  656. "void main() {",
  657. "vUv = vec2( uv.x, 1.0 - uv.y );",
  658. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  659. "}"
  660. ].join("\n"),
  661. fragmentShader: [
  662. "uniform sampler2D tDiffuse;",
  663. "uniform float v;",
  664. "uniform float r;",
  665. "varying vec2 vUv;",
  666. "void main() {",
  667. "vec4 sum = vec4( 0.0 );",
  668. "float vv = v * abs( r - vUv.y );",
  669. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 4.0 * vv ) ) * 0.051;",
  670. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 3.0 * vv ) ) * 0.0918;",
  671. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 2.0 * vv ) ) * 0.12245;",
  672. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 1.0 * vv ) ) * 0.1531;",
  673. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",
  674. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 1.0 * vv ) ) * 0.1531;",
  675. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 2.0 * vv ) ) * 0.12245;",
  676. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 3.0 * vv ) ) * 0.0918;",
  677. "sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 4.0 * vv ) ) * 0.051;",
  678. "gl_FragColor = sum;",
  679. "}"
  680. ].join("\n")
  681. },
  682. /* -------------------------------------------------------------------------
  683. // Blend two textures
  684. ------------------------------------------------------------------------- */
  685. 'blend': {
  686. uniforms: {
  687. tDiffuse1: { type: "t", value: 0, texture: null },
  688. tDiffuse2: { type: "t", value: 1, texture: null },
  689. mixRatio: { type: "f", value: 0.5 },
  690. opacity: { type: "f", value: 1.0 }
  691. },
  692. vertexShader: [
  693. "varying vec2 vUv;",
  694. "void main() {",
  695. "vUv = vec2( uv.x, 1.0 - uv.y );",
  696. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  697. "}"
  698. ].join("\n"),
  699. fragmentShader: [
  700. "uniform float opacity;",
  701. "uniform float mixRatio;",
  702. "uniform sampler2D tDiffuse1;",
  703. "uniform sampler2D tDiffuse2;",
  704. "varying vec2 vUv;",
  705. "void main() {",
  706. "vec4 texel1 = texture2D( tDiffuse1, vUv );",
  707. "vec4 texel2 = texture2D( tDiffuse2, vUv );",
  708. "gl_FragColor = opacity * mix( texel1, texel2, mixRatio );",
  709. "}"
  710. ].join("\n")
  711. },
  712. /* -------------------------------------------------------------------------
  713. // NVIDIA FXAA by Timothy Lottes
  714. // http://timothylottes.blogspot.com/2011/06/fxaa3-source-released.html
  715. // - WebGL port by @supereggbert
  716. // http://www.glge.org/demos/fxaa/
  717. ------------------------------------------------------------------------- */
  718. 'fxaa': {
  719. uniforms: {
  720. "tDiffuse": { type: "t", value: 0, texture: null },
  721. "resolution": { type: "v2", value: new THREE.Vector2( 1 / 1024, 1 / 512 ) }
  722. },
  723. vertexShader: [
  724. "varying vec2 vUv;",
  725. "void main() {",
  726. "vUv = vec2( uv.x, 1.0 - uv.y );",
  727. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  728. "}"
  729. ].join("\n"),
  730. fragmentShader: [
  731. "uniform sampler2D tDiffuse;",
  732. "uniform vec2 resolution;",
  733. "varying vec2 vUv;",
  734. "#define FXAA_REDUCE_MIN (1.0/128.0)",
  735. "#define FXAA_REDUCE_MUL (1.0/8.0)",
  736. "#define FXAA_SPAN_MAX 8.0",
  737. "void main() {",
  738. "vec3 rgbNW = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( -1.0, -1.0 ) ) * resolution ).xyz;",
  739. "vec3 rgbNE = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( 1.0, -1.0 ) ) * resolution ).xyz;",
  740. "vec3 rgbSW = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( -1.0, 1.0 ) ) * resolution ).xyz;",
  741. "vec3 rgbSE = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( 1.0, 1.0 ) ) * resolution ).xyz;",
  742. "vec3 rgbM = texture2D( tDiffuse, gl_FragCoord.xy * resolution ).xyz;",
  743. "vec3 luma = vec3( 0.299, 0.587, 0.114 );",
  744. "float lumaNW = dot( rgbNW, luma );",
  745. "float lumaNE = dot( rgbNE, luma );",
  746. "float lumaSW = dot( rgbSW, luma );",
  747. "float lumaSE = dot( rgbSE, luma );",
  748. "float lumaM = dot( rgbM, luma );",
  749. "float lumaMin = min( lumaM, min( min( lumaNW, lumaNE ), min( lumaSW, lumaSE ) ) );",
  750. "float lumaMax = max( lumaM, max( max( lumaNW, lumaNE) , max( lumaSW, lumaSE ) ) );",
  751. "vec2 dir;",
  752. "dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));",
  753. "dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));",
  754. "float dirReduce = max( ( lumaNW + lumaNE + lumaSW + lumaSE ) * ( 0.25 * FXAA_REDUCE_MUL ), FXAA_REDUCE_MIN );",
  755. "float rcpDirMin = 1.0 / ( min( abs( dir.x ), abs( dir.y ) ) + dirReduce );",
  756. "dir = min( vec2( FXAA_SPAN_MAX, FXAA_SPAN_MAX),",
  757. "max( vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),",
  758. "dir * rcpDirMin)) * resolution;",
  759. "vec3 rgbA = 0.5 * (",
  760. "texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * ( 1.0 / 3.0 - 0.5 ) ).xyz +",
  761. "texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * ( 2.0 / 3.0 - 0.5 ) ).xyz );",
  762. "vec3 rgbB = rgbA * 0.5 + 0.25 * (",
  763. "texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * -0.5 ).xyz +",
  764. "texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * 0.5 ).xyz );",
  765. "float lumaB = dot( rgbB, luma );",
  766. "if ( ( lumaB < lumaMin ) || ( lumaB > lumaMax ) ) {",
  767. "gl_FragColor = vec4( rgbA, 1.0 );",
  768. "} else {",
  769. "gl_FragColor = vec4( rgbB, 1.0 );",
  770. "}",
  771. "}",
  772. ].join("\n"),
  773. },
  774. // METHODS
  775. buildKernel: function( sigma ) {
  776. // We lop off the sqrt(2 * pi) * sigma term, since we're going to normalize anyway.
  777. function gauss( x, sigma ) {
  778. return Math.exp( - ( x * x ) / ( 2.0 * sigma * sigma ) );
  779. }
  780. var i, values, sum, halfWidth, kMaxKernelSize = 25, kernelSize = 2 * Math.ceil( sigma * 3.0 ) + 1;
  781. if ( kernelSize > kMaxKernelSize ) kernelSize = kMaxKernelSize;
  782. halfWidth = ( kernelSize - 1 ) * 0.5
  783. values = new Array( kernelSize );
  784. sum = 0.0;
  785. for ( i = 0; i < kernelSize; ++i ) {
  786. values[ i ] = gauss( i - halfWidth, sigma );
  787. sum += values[ i ];
  788. }
  789. // normalize the kernel
  790. for ( i = 0; i < kernelSize; ++i ) values[ i ] /= sum;
  791. return values;
  792. }
  793. };