ShaderExtras.js 35 KB

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