Path: blob/main/projects/HexGL/libs/ShaderExtras.js
2280 views
/**1* @author alteredq / http://alteredqualia.com/2* @author zz85 / http://www.lab4games.net/zz85/blog3* @author davidedc / http://www.sketchpatch.net/4*5* ShaderExtras currently contains:6*7* screen8* convolution9* film10* bokeh11* sepia12* dotscreen13* vignette14* bleachbypass15* basic16* dofmipmap17* focus18* triangleBlur19* horizontalBlur + verticalBlur20* horizontalTiltShift + verticalTiltShift21* blend22* fxaa23* luminosity24* colorCorrection25* normalmap26* ssao27* colorify28* unpackDepthRGBA29*/3031THREE.ShaderExtras = {3233/* -------------------------------------------------------------------------34// Full-screen textured quad shader35------------------------------------------------------------------------- */3637'screen': {3839uniforms: {4041tDiffuse: { type: "t", value: 0, texture: null },42opacity: { type: "f", value: 1.0 }4344},4546vertexShader: [4748"varying vec2 vUv;",4950"void main() {",5152"vUv = uv;",53"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",5455"}"5657].join("\n"),5859fragmentShader: [6061"uniform float opacity;",6263"uniform sampler2D tDiffuse;",6465"varying vec2 vUv;",6667"void main() {",6869"vec4 texel = texture2D( tDiffuse, vUv );",70"gl_FragColor = opacity * texel;",7172"}"7374].join("\n")7576},7778/* ------------------------------------------------------------------------79// Convolution shader80// - ported from o3d sample to WebGL / GLSL81// http://o3d.googlecode.com/svn/trunk/samples/convolution.html82------------------------------------------------------------------------ */8384'convolution': {8586uniforms: {8788"tDiffuse" : { type: "t", value: 0, texture: null },89"uImageIncrement" : { type: "v2", value: new THREE.Vector2( 0.001953125, 0.0 ) },90"cKernel" : { type: "fv1", value: [] }9192},9394vertexShader: [9596//"#define KERNEL_SIZE 25.0",9798"uniform vec2 uImageIncrement;",99100"varying vec2 vUv;",101102"void main() {",103104"vUv = uv - ( ( KERNEL_SIZE - 1.0 ) / 2.0 ) * uImageIncrement;",105"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",106107"}"108109].join("\n"),110111fragmentShader: [112113//"#define KERNEL_SIZE 25",114"uniform float cKernel[ KERNEL_SIZE ];",115116"uniform sampler2D tDiffuse;",117"uniform vec2 uImageIncrement;",118119"varying vec2 vUv;",120121"void main() {",122123"vec2 imageCoord = vUv;",124"vec4 sum = vec4( 0.0, 0.0, 0.0, 0.0 );",125126"for( int i = 0; i < KERNEL_SIZE; i ++ ) {",127128"sum += texture2D( tDiffuse, imageCoord ) * cKernel[ i ];",129"imageCoord += uImageIncrement;",130131"}",132133"gl_FragColor = sum;",134135"}"136137138].join("\n")139140},141142/* -------------------------------------------------------------------------143144// Film grain & scanlines shader145146// - ported from HLSL to WebGL / GLSL147// http://www.truevision3d.com/forums/showcase/staticnoise_colorblackwhite_scanline_shaders-t18698.0.html148149// Screen Space Static Postprocessor150//151// Produces an analogue noise overlay similar to a film grain / TV static152//153// Original implementation and noise algorithm154// Pat 'Hawthorne' Shearon155//156// Optimized scanlines + noise version with intensity scaling157// Georg 'Leviathan' Steinrohder158159// This version is provided under a Creative Commons Attribution 3.0 License160// http://creativecommons.org/licenses/by/3.0/161------------------------------------------------------------------------- */162163'film': {164165uniforms: {166167tDiffuse: { type: "t", value: 0, texture: null },168time: { type: "f", value: 0.0 },169nIntensity: { type: "f", value: 0.5 },170sIntensity: { type: "f", value: 0.05 },171sCount: { type: "f", value: 4096 },172grayscale: { type: "i", value: 1 }173174},175176vertexShader: [177178"varying vec2 vUv;",179180"void main() {",181182"vUv = uv;",183"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",184185"}"186187].join("\n"),188189fragmentShader: [190191// control parameter192"uniform float time;",193194"uniform bool grayscale;",195196// noise effect intensity value (0 = no effect, 1 = full effect)197"uniform float nIntensity;",198199// scanlines effect intensity value (0 = no effect, 1 = full effect)200"uniform float sIntensity;",201202// scanlines effect count value (0 = no effect, 4096 = full effect)203"uniform float sCount;",204205"uniform sampler2D tDiffuse;",206207"varying vec2 vUv;",208209"void main() {",210211// sample the source212"vec4 cTextureScreen = texture2D( tDiffuse, vUv );",213214// make some noise215"float x = vUv.x * vUv.y * time * 1000.0;",216"x = mod( x, 13.0 ) * mod( x, 123.0 );",217"float dx = mod( x, 0.01 );",218219// add noise220"vec3 cResult = cTextureScreen.rgb + cTextureScreen.rgb * clamp( 0.1 + dx * 100.0, 0.0, 1.0 );",221222// get us a sine and cosine223"vec2 sc = vec2( sin( vUv.y * sCount ), cos( vUv.y * sCount ) );",224225// add scanlines226"cResult += cTextureScreen.rgb * vec3( sc.x, sc.y, sc.x ) * sIntensity;",227228// interpolate between source and result by intensity229"cResult = cTextureScreen.rgb + clamp( nIntensity, 0.0,1.0 ) * ( cResult - cTextureScreen.rgb );",230231// convert to grayscale if desired232"if( grayscale ) {",233234"cResult = vec3( cResult.r * 0.3 + cResult.g * 0.59 + cResult.b * 0.11 );",235236"}",237238"gl_FragColor = vec4( cResult, cTextureScreen.a );",239240"}"241242].join("\n")243244},245246247/* -------------------------------------------------------------------------248// Depth-of-field shader with bokeh249// ported from GLSL shader by Martins Upitis250// http://artmartinsh.blogspot.com/2010/02/glsl-lens-blur-filter-with-bokeh.html251------------------------------------------------------------------------- */252253'bokeh' : {254255uniforms: { tColor: { type: "t", value: 0, texture: null },256tDepth: { type: "t", value: 1, texture: null },257focus: { type: "f", value: 1.0 },258aspect: { type: "f", value: 1.0 },259aperture: { type: "f", value: 0.025 },260maxblur: { type: "f", value: 1.0 }261},262263vertexShader: [264265"varying vec2 vUv;",266267"void main() {",268269"vUv = uv;",270"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",271272"}"273274].join("\n"),275276fragmentShader: [277278"varying vec2 vUv;",279280"uniform sampler2D tColor;",281"uniform sampler2D tDepth;",282283"uniform float maxblur;", // max blur amount284"uniform float aperture;", // aperture - bigger values for shallower depth of field285286"uniform float focus;",287"uniform float aspect;",288289"void main() {",290291"vec2 aspectcorrect = vec2( 1.0, aspect );",292293"vec4 depth1 = texture2D( tDepth, vUv );",294295"float factor = depth1.x - focus;",296297"vec2 dofblur = vec2 ( clamp( factor * aperture, -maxblur, maxblur ) );",298299"vec2 dofblur9 = dofblur * 0.9;",300"vec2 dofblur7 = dofblur * 0.7;",301"vec2 dofblur4 = dofblur * 0.4;",302303"vec4 col = vec4( 0.0 );",304305"col += texture2D( tColor, vUv.xy );",306"col += texture2D( tColor, vUv.xy + ( vec2( 0.0, 0.4 ) * aspectcorrect ) * dofblur );",307"col += texture2D( tColor, vUv.xy + ( vec2( 0.15, 0.37 ) * aspectcorrect ) * dofblur );",308"col += texture2D( tColor, vUv.xy + ( vec2( 0.29, 0.29 ) * aspectcorrect ) * dofblur );",309"col += texture2D( tColor, vUv.xy + ( vec2( -0.37, 0.15 ) * aspectcorrect ) * dofblur );",310"col += texture2D( tColor, vUv.xy + ( vec2( 0.40, 0.0 ) * aspectcorrect ) * dofblur );",311"col += texture2D( tColor, vUv.xy + ( vec2( 0.37, -0.15 ) * aspectcorrect ) * dofblur );",312"col += texture2D( tColor, vUv.xy + ( vec2( 0.29, -0.29 ) * aspectcorrect ) * dofblur );",313"col += texture2D( tColor, vUv.xy + ( vec2( -0.15, -0.37 ) * aspectcorrect ) * dofblur );",314"col += texture2D( tColor, vUv.xy + ( vec2( 0.0, -0.4 ) * aspectcorrect ) * dofblur );",315"col += texture2D( tColor, vUv.xy + ( vec2( -0.15, 0.37 ) * aspectcorrect ) * dofblur );",316"col += texture2D( tColor, vUv.xy + ( vec2( -0.29, 0.29 ) * aspectcorrect ) * dofblur );",317"col += texture2D( tColor, vUv.xy + ( vec2( 0.37, 0.15 ) * aspectcorrect ) * dofblur );",318"col += texture2D( tColor, vUv.xy + ( vec2( -0.4, 0.0 ) * aspectcorrect ) * dofblur );",319"col += texture2D( tColor, vUv.xy + ( vec2( -0.37, -0.15 ) * aspectcorrect ) * dofblur );",320"col += texture2D( tColor, vUv.xy + ( vec2( -0.29, -0.29 ) * aspectcorrect ) * dofblur );",321"col += texture2D( tColor, vUv.xy + ( vec2( 0.15, -0.37 ) * aspectcorrect ) * dofblur );",322323"col += texture2D( tColor, vUv.xy + ( vec2( 0.15, 0.37 ) * aspectcorrect ) * dofblur9 );",324"col += texture2D( tColor, vUv.xy + ( vec2( -0.37, 0.15 ) * aspectcorrect ) * dofblur9 );",325"col += texture2D( tColor, vUv.xy + ( vec2( 0.37, -0.15 ) * aspectcorrect ) * dofblur9 );",326"col += texture2D( tColor, vUv.xy + ( vec2( -0.15, -0.37 ) * aspectcorrect ) * dofblur9 );",327"col += texture2D( tColor, vUv.xy + ( vec2( -0.15, 0.37 ) * aspectcorrect ) * dofblur9 );",328"col += texture2D( tColor, vUv.xy + ( vec2( 0.37, 0.15 ) * aspectcorrect ) * dofblur9 );",329"col += texture2D( tColor, vUv.xy + ( vec2( -0.37, -0.15 ) * aspectcorrect ) * dofblur9 );",330"col += texture2D( tColor, vUv.xy + ( vec2( 0.15, -0.37 ) * aspectcorrect ) * dofblur9 );",331332"col += texture2D( tColor, vUv.xy + ( vec2( 0.29, 0.29 ) * aspectcorrect ) * dofblur7 );",333"col += texture2D( tColor, vUv.xy + ( vec2( 0.40, 0.0 ) * aspectcorrect ) * dofblur7 );",334"col += texture2D( tColor, vUv.xy + ( vec2( 0.29, -0.29 ) * aspectcorrect ) * dofblur7 );",335"col += texture2D( tColor, vUv.xy + ( vec2( 0.0, -0.4 ) * aspectcorrect ) * dofblur7 );",336"col += texture2D( tColor, vUv.xy + ( vec2( -0.29, 0.29 ) * aspectcorrect ) * dofblur7 );",337"col += texture2D( tColor, vUv.xy + ( vec2( -0.4, 0.0 ) * aspectcorrect ) * dofblur7 );",338"col += texture2D( tColor, vUv.xy + ( vec2( -0.29, -0.29 ) * aspectcorrect ) * dofblur7 );",339"col += texture2D( tColor, vUv.xy + ( vec2( 0.0, 0.4 ) * aspectcorrect ) * dofblur7 );",340341"col += texture2D( tColor, vUv.xy + ( vec2( 0.29, 0.29 ) * aspectcorrect ) * dofblur4 );",342"col += texture2D( tColor, vUv.xy + ( vec2( 0.4, 0.0 ) * aspectcorrect ) * dofblur4 );",343"col += texture2D( tColor, vUv.xy + ( vec2( 0.29, -0.29 ) * aspectcorrect ) * dofblur4 );",344"col += texture2D( tColor, vUv.xy + ( vec2( 0.0, -0.4 ) * aspectcorrect ) * dofblur4 );",345"col += texture2D( tColor, vUv.xy + ( vec2( -0.29, 0.29 ) * aspectcorrect ) * dofblur4 );",346"col += texture2D( tColor, vUv.xy + ( vec2( -0.4, 0.0 ) * aspectcorrect ) * dofblur4 );",347"col += texture2D( tColor, vUv.xy + ( vec2( -0.29, -0.29 ) * aspectcorrect ) * dofblur4 );",348"col += texture2D( tColor, vUv.xy + ( vec2( 0.0, 0.4 ) * aspectcorrect ) * dofblur4 );",349350"gl_FragColor = col / 41.0;",351"gl_FragColor.a = 1.0;",352353"}"354355].join("\n")356357},358359/* -------------------------------------------------------------------------360// Depth-of-field shader using mipmaps361// - from Matt Handley @applmak362// - requires power-of-2 sized render target with enabled mipmaps363------------------------------------------------------------------------- */364365'dofmipmap': {366367uniforms: {368369tColor: { type: "t", value: 0, texture: null },370tDepth: { type: "t", value: 1, texture: null },371focus: { type: "f", value: 1.0 },372maxblur: { type: "f", value: 1.0 }373374},375376vertexShader: [377378"varying vec2 vUv;",379380"void main() {",381382"vUv = uv;",383"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",384385"}"386387].join("\n"),388389fragmentShader: [390391"uniform float focus;",392"uniform float maxblur;",393394"uniform sampler2D tColor;",395"uniform sampler2D tDepth;",396397"varying vec2 vUv;",398399"void main() {",400401"vec4 depth = texture2D( tDepth, vUv );",402403"float factor = depth.x - focus;",404405"vec4 col = texture2D( tColor, vUv, 2.0 * maxblur * abs( focus - depth.x ) );",406407"gl_FragColor = col;",408"gl_FragColor.a = 1.0;",409410"}"411412].join("\n")413414},415416/* -------------------------------------------------------------------------417// Sepia tone shader418// - based on glfx.js sepia shader419// https://github.com/evanw/glfx.js420------------------------------------------------------------------------- */421422'sepia': {423424uniforms: {425426tDiffuse: { type: "t", value: 0, texture: null },427amount: { type: "f", value: 1.0 }428429},430431vertexShader: [432433"varying vec2 vUv;",434435"void main() {",436437"vUv = uv;",438"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",439440"}"441442].join("\n"),443444fragmentShader: [445446"uniform float amount;",447448"uniform sampler2D tDiffuse;",449450"varying vec2 vUv;",451452"void main() {",453454"vec4 color = texture2D( tDiffuse, vUv );",455"vec3 c = color.rgb;",456457"color.r = dot( c, vec3( 1.0 - 0.607 * amount, 0.769 * amount, 0.189 * amount ) );",458"color.g = dot( c, vec3( 0.349 * amount, 1.0 - 0.314 * amount, 0.168 * amount ) );",459"color.b = dot( c, vec3( 0.272 * amount, 0.534 * amount, 1.0 - 0.869 * amount ) );",460461"gl_FragColor = vec4( min( vec3( 1.0 ), color.rgb ), color.a );",462463"}"464465].join("\n")466467},468469/* -------------------------------------------------------------------------470// Dot screen shader471// - based on glfx.js sepia shader472// https://github.com/evanw/glfx.js473------------------------------------------------------------------------- */474475'dotscreen': {476477uniforms: {478479tDiffuse: { type: "t", value: 0, texture: null },480tSize: { type: "v2", value: new THREE.Vector2( 256, 256 ) },481center: { type: "v2", value: new THREE.Vector2( 0.5, 0.5 ) },482angle: { type: "f", value: 1.57 },483scale: { type: "f", value: 1.0 }484485},486487vertexShader: [488489"varying vec2 vUv;",490491"void main() {",492493"vUv = uv;",494"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",495496"}"497498].join("\n"),499500fragmentShader: [501502"uniform vec2 center;",503"uniform float angle;",504"uniform float scale;",505"uniform vec2 tSize;",506507"uniform sampler2D tDiffuse;",508509"varying vec2 vUv;",510511"float pattern() {",512513"float s = sin( angle ), c = cos( angle );",514515"vec2 tex = vUv * tSize - center;",516"vec2 point = vec2( c * tex.x - s * tex.y, s * tex.x + c * tex.y ) * scale;",517518"return ( sin( point.x ) * sin( point.y ) ) * 4.0;",519520"}",521522"void main() {",523524"vec4 color = texture2D( tDiffuse, vUv );",525526"float average = ( color.r + color.g + color.b ) / 3.0;",527528"gl_FragColor = vec4( vec3( average * 10.0 - 5.0 + pattern() ), color.a );",529530"}"531532].join("\n")533534},535536/* ------------------------------------------------------------------------------------------------537// Vignette shader538// - based on PaintEffect postprocess from ro.me539// http://code.google.com/p/3-dreams-of-black/source/browse/deploy/js/effects/PaintEffect.js540------------------------------------------------------------------------------------------------ */541542'vignette': {543544uniforms: {545546tDiffuse: { type: "t", value: 0, texture: null },547offset: { type: "f", value: 1.0 },548darkness: { type: "f", value: 1.0 }549550},551552vertexShader: [553554"varying vec2 vUv;",555556"void main() {",557558"vUv = uv;",559"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",560561"}"562563].join("\n"),564565fragmentShader: [566567"uniform float offset;",568"uniform float darkness;",569570"uniform sampler2D tDiffuse;",571572"varying vec2 vUv;",573574"void main() {",575576// Eskil's vignette577578"vec4 texel = texture2D( tDiffuse, vUv );",579"vec2 uv = ( vUv - vec2( 0.5 ) ) * vec2( offset );",580"gl_FragColor = vec4( mix( texel.rgb, vec3( 1.0 - darkness ), dot( uv, uv ) ), texel.a );",581582/*583// alternative version from glfx.js584// this one makes more "dusty" look (as opposed to "burned")585586"vec4 color = texture2D( tDiffuse, vUv );",587"float dist = distance( vUv, vec2( 0.5 ) );",588"color.rgb *= smoothstep( 0.8, offset * 0.799, dist *( darkness + offset ) );",589"gl_FragColor = color;",590*/591592"}"593594].join("\n")595596},597598/* -------------------------------------------------------------------------599// Bleach bypass shader [http://en.wikipedia.org/wiki/Bleach_bypass]600// - based on Nvidia example601// http://developer.download.nvidia.com/shaderlibrary/webpages/shader_library.html#post_bleach_bypass602------------------------------------------------------------------------- */603604'bleachbypass': {605606uniforms: {607608tDiffuse: { type: "t", value: 0, texture: null },609opacity: { type: "f", value: 1.0 }610611},612613vertexShader: [614615"varying vec2 vUv;",616617"void main() {",618619"vUv = uv;",620"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",621622"}"623624].join("\n"),625626fragmentShader: [627628"uniform float opacity;",629630"uniform sampler2D tDiffuse;",631632"varying vec2 vUv;",633634"void main() {",635636"vec4 base = texture2D( tDiffuse, vUv );",637638"vec3 lumCoeff = vec3( 0.25, 0.65, 0.1 );",639"float lum = dot( lumCoeff, base.rgb );",640"vec3 blend = vec3( lum );",641642"float L = min( 1.0, max( 0.0, 10.0 * ( lum - 0.45 ) ) );",643644"vec3 result1 = 2.0 * base.rgb * blend;",645"vec3 result2 = 1.0 - 2.0 * ( 1.0 - blend ) * ( 1.0 - base.rgb );",646647"vec3 newColor = mix( result1, result2, L );",648649"float A2 = opacity * base.a;",650"vec3 mixRGB = A2 * newColor.rgb;",651"mixRGB += ( ( 1.0 - A2 ) * base.rgb );",652653"gl_FragColor = vec4( mixRGB, base.a );",654655"}"656657].join("\n")658659},660661/* --------------------------------------------------------------------------------------------------662// Focus shader663// - based on PaintEffect postprocess from ro.me664// http://code.google.com/p/3-dreams-of-black/source/browse/deploy/js/effects/PaintEffect.js665-------------------------------------------------------------------------------------------------- */666667'focus': {668669uniforms : {670671"tDiffuse": { type: "t", value: 0, texture: null },672"screenWidth": { type: "f", value: 1024 },673"screenHeight": { type: "f", value: 1024 },674"sampleDistance": { type: "f", value: 0.94 },675"waveFactor": { type: "f", value: 0.00125 }676677},678679vertexShader: [680681"varying vec2 vUv;",682683"void main() {",684685"vUv = uv;",686"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",687688"}"689690].join("\n"),691692fragmentShader: [693694"uniform float screenWidth;",695"uniform float screenHeight;",696"uniform float sampleDistance;",697"uniform float waveFactor;",698699"uniform sampler2D tDiffuse;",700701"varying vec2 vUv;",702703"void main() {",704705"vec4 color, org, tmp, add;",706"float sample_dist, f;",707"vec2 vin;",708"vec2 uv = vUv;",709710"add = color = org = texture2D( tDiffuse, uv );",711712"vin = ( uv - vec2( 0.5 ) ) * vec2( 1.4 );",713"sample_dist = dot( vin, vin ) * 2.0;",714715"f = ( waveFactor * 100.0 + sample_dist ) * sampleDistance * 4.0;",716717"vec2 sampleSize = vec2( 1.0 / screenWidth, 1.0 / screenHeight ) * vec2( f );",718719"add += tmp = texture2D( tDiffuse, uv + vec2( 0.111964, 0.993712 ) * sampleSize );",720"if( tmp.b < color.b ) color = tmp;",721722"add += tmp = texture2D( tDiffuse, uv + vec2( 0.846724, 0.532032 ) * sampleSize );",723"if( tmp.b < color.b ) color = tmp;",724725"add += tmp = texture2D( tDiffuse, uv + vec2( 0.943883, -0.330279 ) * sampleSize );",726"if( tmp.b < color.b ) color = tmp;",727728"add += tmp = texture2D( tDiffuse, uv + vec2( 0.330279, -0.943883 ) * sampleSize );",729"if( tmp.b < color.b ) color = tmp;",730731"add += tmp = texture2D( tDiffuse, uv + vec2( -0.532032, -0.846724 ) * sampleSize );",732"if( tmp.b < color.b ) color = tmp;",733734"add += tmp = texture2D( tDiffuse, uv + vec2( -0.993712, -0.111964 ) * sampleSize );",735"if( tmp.b < color.b ) color = tmp;",736737"add += tmp = texture2D( tDiffuse, uv + vec2( -0.707107, 0.707107 ) * sampleSize );",738"if( tmp.b < color.b ) color = tmp;",739740"color = color * vec4( 2.0 ) - ( add / vec4( 8.0 ) );",741"color = color + ( add / vec4( 8.0 ) - color ) * ( vec4( 1.0 ) - vec4( sample_dist * 0.5 ) );",742743"gl_FragColor = vec4( color.rgb * color.rgb * vec3( 0.95 ) + color.rgb, 1.0 );",744745"}"746747748].join("\n")749},750751/* -------------------------------------------------------------------------752// Triangle blur shader753// - based on glfx.js triangle blur shader754// https://github.com/evanw/glfx.js755756// A basic blur filter, which convolves the image with a757// pyramid filter. The pyramid filter is separable and is applied as two758// perpendicular triangle filters.759------------------------------------------------------------------------- */760761'triangleBlur': {762763764uniforms : {765766"texture": { type: "t", value: 0, texture: null },767"delta": { type: "v2", value:new THREE.Vector2( 1, 1 ) }768769},770771vertexShader: [772773"varying vec2 vUv;",774775"void main() {",776777"vUv = uv;",778"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",779780"}"781782].join("\n"),783784fragmentShader: [785786"#define ITERATIONS 10.0",787788"uniform sampler2D texture;",789"uniform vec2 delta;",790791"varying vec2 vUv;",792793"float random( vec3 scale, float seed ) {",794795// use the fragment position for a different seed per-pixel796797"return fract( sin( dot( gl_FragCoord.xyz + seed, scale ) ) * 43758.5453 + seed );",798799"}",800801"void main() {",802803"vec4 color = vec4( 0.0 );",804805"float total = 0.0;",806807// randomize the lookup values to hide the fixed number of samples808809"float offset = random( vec3( 12.9898, 78.233, 151.7182 ), 0.0 );",810811"for ( float t = -ITERATIONS; t <= ITERATIONS; t ++ ) {",812813"float percent = ( t + offset - 0.5 ) / ITERATIONS;",814"float weight = 1.0 - abs( percent );",815816"color += texture2D( texture, vUv + delta * percent ) * weight;",817"total += weight;",818819"}",820821"gl_FragColor = color / total;",822823"}"824825].join("\n")826827},828829/* -------------------------------------------------------------------------830// Simple test shader831------------------------------------------------------------------------- */832833'basic': {834835uniforms: {},836837vertexShader: [838839"void main() {",840841"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",842843"}"844845].join("\n"),846847fragmentShader: [848849"void main() {",850851"gl_FragColor = vec4( 1.0, 0.0, 0.0, 0.5 );",852853"}"854855].join("\n")856857},858859/* --------------------------------------------------------------------------------------------------860// Two pass Gaussian blur filter (horizontal and vertical blur shaders)861// - described in http://www.gamerendering.com/2008/10/11/gaussian-blur-filter-shader/862// and used in http://www.cake23.de/traveling-wavefronts-lit-up.html863//864// - 9 samples per pass865// - standard deviation 2.7866// - "h" and "v" parameters should be set to "1 / width" and "1 / height"867-------------------------------------------------------------------------------------------------- */868869'horizontalBlur': {870871uniforms: {872873"tDiffuse": { type: "t", value: 0, texture: null },874"h": { type: "f", value: 1.0 / 512.0 }875876},877878vertexShader: [879880"varying vec2 vUv;",881882"void main() {",883884"vUv = uv;",885"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",886887"}"888889].join("\n"),890891fragmentShader: [892893"uniform sampler2D tDiffuse;",894"uniform float h;",895896"varying vec2 vUv;",897898"void main() {",899900"vec4 sum = vec4( 0.0 );",901902"sum += texture2D( tDiffuse, vec2( vUv.x - 4.0 * h, vUv.y ) ) * 0.051;",903"sum += texture2D( tDiffuse, vec2( vUv.x - 3.0 * h, vUv.y ) ) * 0.0918;",904"sum += texture2D( tDiffuse, vec2( vUv.x - 2.0 * h, vUv.y ) ) * 0.12245;",905"sum += texture2D( tDiffuse, vec2( vUv.x - 1.0 * h, vUv.y ) ) * 0.1531;",906"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",907"sum += texture2D( tDiffuse, vec2( vUv.x + 1.0 * h, vUv.y ) ) * 0.1531;",908"sum += texture2D( tDiffuse, vec2( vUv.x + 2.0 * h, vUv.y ) ) * 0.12245;",909"sum += texture2D( tDiffuse, vec2( vUv.x + 3.0 * h, vUv.y ) ) * 0.0918;",910"sum += texture2D( tDiffuse, vec2( vUv.x + 4.0 * h, vUv.y ) ) * 0.051;",911912"gl_FragColor = sum;",913914"}"915916917].join("\n")918919},920921'verticalBlur': {922923uniforms: {924925"tDiffuse": { type: "t", value: 0, texture: null },926"v": { type: "f", value: 1.0 / 512.0 }927928},929930vertexShader: [931932"varying vec2 vUv;",933934"void main() {",935936"vUv = uv;",937"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",938939"}"940941].join("\n"),942943fragmentShader: [944945"uniform sampler2D tDiffuse;",946"uniform float v;",947948"varying vec2 vUv;",949950"void main() {",951952"vec4 sum = vec4( 0.0 );",953954"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 4.0 * v ) ) * 0.051;",955"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 3.0 * v ) ) * 0.0918;",956"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 2.0 * v ) ) * 0.12245;",957"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 1.0 * v ) ) * 0.1531;",958"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",959"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 1.0 * v ) ) * 0.1531;",960"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 2.0 * v ) ) * 0.12245;",961"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 3.0 * v ) ) * 0.0918;",962"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 4.0 * v ) ) * 0.051;",963964"gl_FragColor = sum;",965966"}"967968969].join("\n")970971},972973/* --------------------------------------------------------------------------------------------------974// Simple fake tilt-shift effect, modulating two pass Gaussian blur (see above) by vertical position975//976// - 9 samples per pass977// - standard deviation 2.7978// - "h" and "v" parameters should be set to "1 / width" and "1 / height"979// - "r" parameter control where "focused" horizontal line lies980-------------------------------------------------------------------------------------------------- */981982'horizontalTiltShift': {983984uniforms: {985986"tDiffuse": { type: "t", value: 0, texture: null },987"h": { type: "f", value: 1.0 / 512.0 },988"r": { type: "f", value: 0.35 }989990},991992vertexShader: [993994"varying vec2 vUv;",995996"void main() {",997998"vUv = uv;",999"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",10001001"}"10021003].join("\n"),10041005fragmentShader: [10061007"uniform sampler2D tDiffuse;",1008"uniform float h;",1009"uniform float r;",10101011"varying vec2 vUv;",10121013"void main() {",10141015"vec4 sum = vec4( 0.0 );",10161017"float hh = h * abs( r - vUv.y );",10181019"sum += texture2D( tDiffuse, vec2( vUv.x - 4.0 * hh, vUv.y ) ) * 0.051;",1020"sum += texture2D( tDiffuse, vec2( vUv.x - 3.0 * hh, vUv.y ) ) * 0.0918;",1021"sum += texture2D( tDiffuse, vec2( vUv.x - 2.0 * hh, vUv.y ) ) * 0.12245;",1022"sum += texture2D( tDiffuse, vec2( vUv.x - 1.0 * hh, vUv.y ) ) * 0.1531;",1023"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",1024"sum += texture2D( tDiffuse, vec2( vUv.x + 1.0 * hh, vUv.y ) ) * 0.1531;",1025"sum += texture2D( tDiffuse, vec2( vUv.x + 2.0 * hh, vUv.y ) ) * 0.12245;",1026"sum += texture2D( tDiffuse, vec2( vUv.x + 3.0 * hh, vUv.y ) ) * 0.0918;",1027"sum += texture2D( tDiffuse, vec2( vUv.x + 4.0 * hh, vUv.y ) ) * 0.051;",10281029"gl_FragColor = sum;",10301031"}"103210331034].join("\n")10351036},10371038'verticalTiltShift': {10391040uniforms: {10411042"tDiffuse": { type: "t", value: 0, texture: null },1043"v": { type: "f", value: 1.0 / 512.0 },1044"r": { type: "f", value: 0.35 }10451046},10471048vertexShader: [10491050"varying vec2 vUv;",10511052"void main() {",10531054"vUv = uv;",1055"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",10561057"}"10581059].join("\n"),10601061fragmentShader: [10621063"uniform sampler2D tDiffuse;",1064"uniform float v;",1065"uniform float r;",10661067"varying vec2 vUv;",10681069"void main() {",10701071"vec4 sum = vec4( 0.0 );",10721073"float vv = v * abs( r - vUv.y );",10741075"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 4.0 * vv ) ) * 0.051;",1076"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 3.0 * vv ) ) * 0.0918;",1077"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 2.0 * vv ) ) * 0.12245;",1078"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y - 1.0 * vv ) ) * 0.1531;",1079"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;",1080"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 1.0 * vv ) ) * 0.1531;",1081"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 2.0 * vv ) ) * 0.12245;",1082"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 3.0 * vv ) ) * 0.0918;",1083"sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y + 4.0 * vv ) ) * 0.051;",10841085"gl_FragColor = sum;",10861087"}"108810891090].join("\n")10911092},10931094/* -------------------------------------------------------------------------1095// Blend two textures1096------------------------------------------------------------------------- */10971098'blend': {10991100uniforms: {11011102tDiffuse1: { type: "t", value: 0, texture: null },1103tDiffuse2: { type: "t", value: 1, texture: null },1104mixRatio: { type: "f", value: 0.5 },1105opacity: { type: "f", value: 1.0 }11061107},11081109vertexShader: [11101111"varying vec2 vUv;",11121113"void main() {",11141115"vUv = uv;",1116"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",11171118"}"11191120].join("\n"),11211122fragmentShader: [11231124"uniform float opacity;",1125"uniform float mixRatio;",11261127"uniform sampler2D tDiffuse1;",1128"uniform sampler2D tDiffuse2;",11291130"varying vec2 vUv;",11311132"void main() {",11331134"vec4 texel1 = texture2D( tDiffuse1, vUv );",1135"vec4 texel2 = texture2D( tDiffuse2, vUv );",1136"gl_FragColor = opacity * mix( texel1, texel2, mixRatio );",11371138"}"11391140].join("\n")11411142},11431144/* -------------------------------------------------------------------------1145// NVIDIA FXAA by Timothy Lottes1146// http://timothylottes.blogspot.com/2011/06/fxaa3-source-released.html1147// - WebGL port by @supereggbert1148// http://www.glge.org/demos/fxaa/1149------------------------------------------------------------------------- */11501151'fxaa': {11521153uniforms: {11541155"tDiffuse": { type: "t", value: 0, texture: null },1156"resolution": { type: "v2", value: new THREE.Vector2( 1 / 1024, 1 / 512 ) }11571158},11591160vertexShader: [11611162"varying vec2 vUv;",11631164"void main() {",11651166"vUv = uv;",11671168"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",11691170"}"11711172].join("\n"),11731174fragmentShader: [11751176"uniform sampler2D tDiffuse;",1177"uniform vec2 resolution;",11781179"varying vec2 vUv;",11801181"#define FXAA_REDUCE_MIN (1.0/128.0)",1182"#define FXAA_REDUCE_MUL (1.0/8.0)",1183"#define FXAA_SPAN_MAX 8.0",11841185"void main() {",11861187"vec3 rgbNW = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( -1.0, -1.0 ) ) * resolution ).xyz;",1188"vec3 rgbNE = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( 1.0, -1.0 ) ) * resolution ).xyz;",1189"vec3 rgbSW = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( -1.0, 1.0 ) ) * resolution ).xyz;",1190"vec3 rgbSE = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( 1.0, 1.0 ) ) * resolution ).xyz;",1191"vec4 rgbaM = texture2D( tDiffuse, gl_FragCoord.xy * resolution );",1192"vec3 rgbM = rgbaM.xyz;",1193"float opacity = rgbaM.w;",11941195"vec3 luma = vec3( 0.299, 0.587, 0.114 );",11961197"float lumaNW = dot( rgbNW, luma );",1198"float lumaNE = dot( rgbNE, luma );",1199"float lumaSW = dot( rgbSW, luma );",1200"float lumaSE = dot( rgbSE, luma );",1201"float lumaM = dot( rgbM, luma );",1202"float lumaMin = min( lumaM, min( min( lumaNW, lumaNE ), min( lumaSW, lumaSE ) ) );",1203"float lumaMax = max( lumaM, max( max( lumaNW, lumaNE) , max( lumaSW, lumaSE ) ) );",12041205"vec2 dir;",1206"dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));",1207"dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));",12081209"float dirReduce = max( ( lumaNW + lumaNE + lumaSW + lumaSE ) * ( 0.25 * FXAA_REDUCE_MUL ), FXAA_REDUCE_MIN );",12101211"float rcpDirMin = 1.0 / ( min( abs( dir.x ), abs( dir.y ) ) + dirReduce );",1212"dir = min( vec2( FXAA_SPAN_MAX, FXAA_SPAN_MAX),",1213"max( vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),",1214"dir * rcpDirMin)) * resolution;",12151216"vec3 rgbA = 0.5 * (",1217"texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * ( 1.0 / 3.0 - 0.5 ) ).xyz +",1218"texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * ( 2.0 / 3.0 - 0.5 ) ).xyz );",12191220"vec3 rgbB = rgbA * 0.5 + 0.25 * (",1221"texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * -0.5 ).xyz +",1222"texture2D( tDiffuse, gl_FragCoord.xy * resolution + dir * 0.5 ).xyz );",12231224"float lumaB = dot( rgbB, luma );",12251226"if ( ( lumaB < lumaMin ) || ( lumaB > lumaMax ) ) {",12271228"gl_FragColor = vec4( rgbA, opacity );",12291230"} else {",12311232"gl_FragColor = vec4( rgbB, opacity );",12331234"}",12351236"}"12371238].join("\n")12391240},12411242/* -------------------------------------------------------------------------1243// Luminosity1244// http://en.wikipedia.org/wiki/Luminosity1245------------------------------------------------------------------------- */12461247'luminosity': {12481249uniforms: {12501251"tDiffuse": { type: "t", value: 0, texture: null }12521253},12541255vertexShader: [12561257"varying vec2 vUv;",12581259"void main() {",12601261"vUv = uv;",12621263"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",12641265"}"12661267].join("\n"),12681269fragmentShader: [12701271"uniform sampler2D tDiffuse;",12721273"varying vec2 vUv;",12741275"void main() {",12761277"vec4 texel = texture2D( tDiffuse, vUv );",12781279"vec3 luma = vec3( 0.299, 0.587, 0.114 );",12801281"float v = dot( texel.xyz, luma );",12821283"gl_FragColor = vec4( v, v, v, texel.w );",12841285"}"12861287].join("\n")12881289},12901291/* -------------------------------------------------------------------------1292// Color correction1293------------------------------------------------------------------------- */12941295'colorCorrection': {12961297uniforms: {12981299"tDiffuse" : { type: "t", value: 0, texture: null },1300"powRGB" : { type: "v3", value: new THREE.Vector3( 2, 2, 2 ) },1301"mulRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }13021303},13041305vertexShader: [13061307"varying vec2 vUv;",13081309"void main() {",13101311"vUv = uv;",13121313"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",13141315"}"13161317].join("\n"),13181319fragmentShader: [13201321"uniform sampler2D tDiffuse;",1322"uniform vec3 powRGB;",1323"uniform vec3 mulRGB;",13241325"varying vec2 vUv;",13261327"void main() {",13281329"gl_FragColor = texture2D( tDiffuse, vUv );",1330"gl_FragColor.rgb = mulRGB * pow( gl_FragColor.rgb, powRGB );",13311332"}"13331334].join("\n")13351336},13371338/* -------------------------------------------------------------------------1339// Normal map shader1340// - compute normals from heightmap1341------------------------------------------------------------------------- */13421343'normalmap': {13441345uniforms: {13461347"heightMap" : { type: "t", value: 0, texture: null },1348"resolution": { type: "v2", value: new THREE.Vector2( 512, 512 ) },1349"scale" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },1350"height" : { type: "f", value: 0.05 }13511352},13531354vertexShader: [13551356"varying vec2 vUv;",13571358"void main() {",13591360"vUv = uv;",13611362"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",13631364"}"13651366].join("\n"),13671368fragmentShader: [13691370"uniform float height;",1371"uniform vec2 resolution;",1372"uniform sampler2D heightMap;",13731374"varying vec2 vUv;",13751376"void main() {",13771378"float val = texture2D( heightMap, vUv ).x;",13791380"float valU = texture2D( heightMap, vUv + vec2( 1.0 / resolution.x, 0.0 ) ).x;",1381"float valV = texture2D( heightMap, vUv + vec2( 0.0, 1.0 / resolution.y ) ).x;",13821383"gl_FragColor = vec4( ( 0.5 * normalize( vec3( val - valU, val - valV, height ) ) + 0.5 ), 1.0 );",13841385"}"13861387].join("\n")13881389},13901391/* -------------------------------------------------------------------------1392// Screen-space ambient occlusion shader1393// - ported from1394// SSAO GLSL shader v1.21395// assembled by Martins Upitis (martinsh) (http://devlog-martinsh.blogspot.com)1396// original technique is made by ArKano22 (http://www.gamedev.net/topic/550699-ssao-no-halo-artifacts/)1397// - modifications1398// - modified to use RGBA packed depth texture (use clear color 1,1,1,1 for depth pass)1399// - made fog more compatible with three.js linear fog1400// - refactoring and optimizations1401------------------------------------------------------------------------- */14021403'ssao': {14041405uniforms: {14061407"tDiffuse": { type: "t", value: 0, texture: null },1408"tDepth": { type: "t", value: 1, texture: null },1409"size": { type: "v2", value: new THREE.Vector2( 512, 512 ) },1410"cameraNear": { type: "f", value: 1 },1411"cameraFar": { type: "f", value: 100 },1412"fogNear": { type: "f", value: 5 },1413"fogFar": { type: "f", value: 100 },1414"fogEnabled": { type: "i", value: 0 },1415"onlyAO": { type: "i", value: 0 },1416"aoClamp": { type: "f", value: 0.3 },1417"lumInfluence": { type: "f", value: 0.9 }14181419},14201421vertexShader: [14221423"varying vec2 vUv;",14241425"void main() {",14261427"vUv = uv;",14281429"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",14301431"}"14321433].join("\n"),14341435fragmentShader: [14361437"uniform float cameraNear;",1438"uniform float cameraFar;",14391440"uniform float fogNear;",1441"uniform float fogFar;",14421443"uniform bool fogEnabled;", // attenuate AO with linear fog1444"uniform bool onlyAO;", // use only ambient occlusion pass?14451446"uniform vec2 size;", // texture width, height1447"uniform float aoClamp;", // depth clamp - reduces haloing at screen edges14481449"uniform float lumInfluence;", // how much luminance affects occlusion14501451"uniform sampler2D tDiffuse;",1452"uniform sampler2D tDepth;",14531454"varying vec2 vUv;",14551456//"#define PI 3.14159265",1457"#define DL 2.399963229728653", // PI * ( 3.0 - sqrt( 5.0 ) )1458"#define EULER 2.718281828459045",14591460// helpers14611462"float width = size.x;", // texture width1463"float height = size.y;", // texture height14641465"float cameraFarPlusNear = cameraFar + cameraNear;",1466"float cameraFarMinusNear = cameraFar - cameraNear;",1467"float cameraCoef = 2.0 * cameraNear;",14681469// user variables14701471"const int samples = 8;", // ao sample count1472"const float radius = 5.0;", // ao radius14731474"const bool useNoise = true;", // use noise instead of pattern for sample dithering1475"const float noiseAmount = 0.0003;", // dithering amount14761477"const float diffArea = 0.4;", // self-shadowing reduction1478"const float gDisplace = 0.4;", // gauss bell center14791480"const vec3 onlyAOColor = vec3( 1.0, 0.7, 0.5 );",1481//"const vec3 onlyAOColor = vec3( 1.0, 1.0, 1.0 );",148214831484// RGBA depth14851486"float unpackDepth( const in vec4 rgba_depth ) {",14871488"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 );",1489"float depth = dot( rgba_depth, bit_shift );",1490"return depth;",14911492"}",14931494// generating noise / pattern texture for dithering14951496"vec2 rand( const vec2 coord ) {",14971498"vec2 noise;",14991500"if ( useNoise ) {",15011502"float nx = dot ( coord, vec2( 12.9898, 78.233 ) );",1503"float ny = dot ( coord, vec2( 12.9898, 78.233 ) * 2.0 );",15041505"noise = clamp( fract ( 43758.5453 * sin( vec2( nx, ny ) ) ), 0.0, 1.0 );",15061507"} else {",15081509"float ff = fract( 1.0 - coord.s * ( width / 2.0 ) );",1510"float gg = fract( coord.t * ( height / 2.0 ) );",15111512"noise = vec2( 0.25, 0.75 ) * vec2( ff ) + vec2( 0.75, 0.25 ) * gg;",15131514"}",15151516"return ( noise * 2.0 - 1.0 ) * noiseAmount;",15171518"}",15191520"float doFog() {",15211522"float zdepth = unpackDepth( texture2D( tDepth, vUv ) );",1523"float depth = -cameraFar * cameraNear / ( zdepth * cameraFarMinusNear - cameraFar );",15241525"return smoothstep( fogNear, fogFar, depth );",15261527"}",15281529"float readDepth( const in vec2 coord ) {",15301531//"return ( 2.0 * cameraNear ) / ( cameraFar + cameraNear - unpackDepth( texture2D( tDepth, coord ) ) * ( cameraFar - cameraNear ) );",1532"return cameraCoef / ( cameraFarPlusNear - unpackDepth( texture2D( tDepth, coord ) ) * cameraFarMinusNear );",153315341535"}",15361537"float compareDepths( const in float depth1, const in float depth2, inout int far ) {",15381539"float garea = 2.0;", // gauss bell width1540"float diff = ( depth1 - depth2 ) * 100.0;", // depth difference (0-100)15411542// reduce left bell width to avoid self-shadowing15431544"if ( diff < gDisplace ) {",15451546"garea = diffArea;",15471548"} else {",15491550"far = 1;",15511552"}",15531554"float dd = diff - gDisplace;",1555"float gauss = pow( EULER, -2.0 * dd * dd / ( garea * garea ) );",1556"return gauss;",15571558"}",15591560"float calcAO( float depth, float dw, float dh ) {",15611562"float dd = radius - depth * radius;",1563"vec2 vv = vec2( dw, dh );",15641565"vec2 coord1 = vUv + dd * vv;",1566"vec2 coord2 = vUv - dd * vv;",15671568"float temp1 = 0.0;",1569"float temp2 = 0.0;",15701571"int far = 0;",1572"temp1 = compareDepths( depth, readDepth( coord1 ), far );",15731574// DEPTH EXTRAPOLATION15751576"if ( far > 0 ) {",15771578"temp2 = compareDepths( readDepth( coord2 ), depth, far );",1579"temp1 += ( 1.0 - temp1 ) * temp2;",15801581"}",15821583"return temp1;",15841585"}",15861587"void main() {",15881589"vec2 noise = rand( vUv );",1590"float depth = readDepth( vUv );",15911592"float tt = clamp( depth, aoClamp, 1.0 );",15931594"float w = ( 1.0 / width ) / tt + ( noise.x * ( 1.0 - noise.x ) );",1595"float h = ( 1.0 / height ) / tt + ( noise.y * ( 1.0 - noise.y ) );",15961597"float pw;",1598"float ph;",15991600"float ao;",16011602"float dz = 1.0 / float( samples );",1603"float z = 1.0 - dz / 2.0;",1604"float l = 0.0;",16051606"for ( int i = 0; i <= samples; i ++ ) {",16071608"float r = sqrt( 1.0 - z );",16091610"pw = cos( l ) * r;",1611"ph = sin( l ) * r;",1612"ao += calcAO( depth, pw * w, ph * h );",1613"z = z - dz;",1614"l = l + DL;",16151616"}",16171618"ao /= float( samples );",1619"ao = 1.0 - ao;",16201621"if ( fogEnabled ) {",16221623"ao = mix( ao, 1.0, doFog() );",16241625"}",16261627"vec3 color = texture2D( tDiffuse, vUv ).rgb;",16281629"vec3 lumcoeff = vec3( 0.299, 0.587, 0.114 );",1630"float lum = dot( color.rgb, lumcoeff );",1631"vec3 luminance = vec3( lum );",16321633"vec3 final = vec3( color * mix( vec3( ao ), vec3( 1.0 ), luminance * lumInfluence ) );", // mix( color * ao, white, luminance )16341635"if ( onlyAO ) {",16361637"final = onlyAOColor * vec3( mix( vec3( ao ), vec3( 1.0 ), luminance * lumInfluence ) );", // ambient occlusion only16381639"}",16401641"gl_FragColor = vec4( final, 1.0 );",16421643"}"16441645].join("\n")16461647},16481649/* -------------------------------------------------------------------------1650// Colorify shader1651------------------------------------------------------------------------- */16521653'colorify': {16541655uniforms: {16561657tDiffuse: { type: "t", value: 0, texture: null },1658color: { type: "c", value: new THREE.Color( 0xffffff ) }16591660},16611662vertexShader: [16631664"varying vec2 vUv;",16651666"void main() {",16671668"vUv = uv;",1669"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",16701671"}"16721673].join("\n"),16741675fragmentShader: [16761677"uniform vec3 color;",1678"uniform sampler2D tDiffuse;",16791680"varying vec2 vUv;",16811682"void main() {",16831684"vec4 texel = texture2D( tDiffuse, vUv );",16851686"vec3 luma = vec3( 0.299, 0.587, 0.114 );",1687"float v = dot( texel.xyz, luma );",16881689"gl_FragColor = vec4( v * color, texel.w );",16901691"}"16921693].join("\n")16941695},16961697/* -------------------------------------------------------------------------1698// Unpack RGBA depth shader1699// - show RGBA encoded depth as monochrome color1700------------------------------------------------------------------------- */17011702'unpackDepthRGBA': {17031704uniforms: {17051706tDiffuse: { type: "t", value: 0, texture: null },1707opacity: { type: "f", value: 1.0 }17081709},17101711vertexShader: [17121713"varying vec2 vUv;",17141715"void main() {",17161717"vUv = uv;",1718"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",17191720"}"17211722].join("\n"),17231724fragmentShader: [17251726"uniform float opacity;",17271728"uniform sampler2D tDiffuse;",17291730"varying vec2 vUv;",17311732// RGBA depth17331734"float unpackDepth( const in vec4 rgba_depth ) {",17351736"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 );",1737"float depth = dot( rgba_depth, bit_shift );",1738"return depth;",17391740"}",17411742"void main() {",17431744"float depth = 1.0 - unpackDepth( texture2D( tDiffuse, vUv ) );",1745"gl_FragColor = opacity * vec4( vec3( depth ), 1.0 );",17461747"}"17481749].join("\n")17501751},17521753// METHODS17541755buildKernel: function( sigma ) {17561757// We lop off the sqrt(2 * pi) * sigma term, since we're going to normalize anyway.17581759function gauss( x, sigma ) {17601761return Math.exp( - ( x * x ) / ( 2.0 * sigma * sigma ) );17621763}17641765var i, values, sum, halfWidth, kMaxKernelSize = 25, kernelSize = 2 * Math.ceil( sigma * 3.0 ) + 1;17661767if ( kernelSize > kMaxKernelSize ) kernelSize = kMaxKernelSize;1768halfWidth = ( kernelSize - 1 ) * 0.5;17691770values = new Array( kernelSize );1771sum = 0.0;1772for ( i = 0; i < kernelSize; ++i ) {17731774values[ i ] = gauss( i - halfWidth, sigma );1775sum += values[ i ];17761777}17781779// normalize the kernel17801781for ( i = 0; i < kernelSize; ++i ) values[ i ] /= sum;17821783return values;17841785}17861787};178817891790