UNPKG

phaser

Version:

A fast, free and fun HTML5 Game Framework for Desktop and Mobile web browsers from the team at Phaser Studio Inc.

197 lines (196 loc) 6.81 kB
module.exports = [ '#extension GL_OES_standard_derivatives : enable', '#define BAND_TREE_DEPTH 0.0', 'uniform sampler2D uRampTexture;', 'uniform vec2 uRampResolution;', 'uniform float uRampBandStart;', 'uniform bool uDither;', 'float decodeNumberSample(vec4 sample)', '{', ' return (', ' sample.r * 255.0 +', ' sample.g * 255.0 * 256.0 +', ' sample.b * 255.0 * 256.0 * 256.0 +', ' sample.a * 255.0 * 256.0 * 256.0 * 256.0', ' ) / 256.0 / 256.0;', '}', 'struct Band', '{', ' vec4 colorStart;', ' vec4 colorEnd;', ' float start;', ' float end;', ' int colorSpace;', ' int interpolation;', ' float middle;', '};', 'Band getBand(float progress)', '{', ' vec2 rampStep = 1.0 / uRampResolution;', ' vec2 c = rampStep / 2.0;', ' float start = decodeNumberSample(texture2D(uRampTexture, c));', ' float end = decodeNumberSample(texture2D(uRampTexture, vec2(1.0, 0.0) * rampStep + c));', ' float TREE_OFFSET = 2.0; // Beginning of tree block.', ' float index = 0.0;', ' float x, y;', ' for (float i = 0.0; i < BAND_TREE_DEPTH; i++)', ' {', ' x = mod(index + TREE_OFFSET, uRampResolution.x);', ' y = floor(x / uRampResolution.x);', ' float pivot = decodeNumberSample(texture2D(uRampTexture, vec2(x, y) * rampStep + c));', ' if (progress > pivot)', ' {', ' start = pivot;', ' index = index * 2.0 + 2.0;', ' }', ' else', ' {', ' end = pivot;', ' index = index * 2.0 + 1.0;', ' }', ' }', ' float bandNumber = index - uRampBandStart + TREE_OFFSET;', ' float bandIndex = bandNumber * 3.0 + uRampBandStart;', ' x = mod(bandIndex, uRampResolution.x);', ' y = floor(bandIndex / uRampResolution.x);', ' vec4 colorStart = texture2D(uRampTexture, vec2(x, y) * rampStep + c);', ' x = mod(bandIndex + 1.0, uRampResolution.x);', ' y = floor(bandIndex / uRampResolution.x);', ' vec4 colorEnd = texture2D(uRampTexture, vec2(x, y) * rampStep + c);', ' x = mod(bandIndex + 2.0, uRampResolution.x);', ' y = floor(bandIndex / uRampResolution.x);', ' float bandData = decodeNumberSample(texture2D(uRampTexture, vec2(x, y) * rampStep + c));', ' int colorSpace = int(floor(bandData / 255.0));', ' int interpolation = int(floor(bandData)) - colorSpace * 255;', ' return Band(colorStart, colorEnd, start, end, colorSpace, interpolation, fract(bandData) * 2.0);', '}', 'float interpolate(float progress, int mode)', '{', ' if (mode == 1)', ' {', ' if ((progress *= 2.0) < 1.0)', ' {', ' return 0.5 * sqrt(1.0 - (--progress * progress));', ' }', ' progress = 1.0 - progress;', ' return 1.0 - 0.5 * sqrt(1.0 - progress * progress);', ' }', ' if (mode == 2)', ' {', ' return sin((progress - 0.5) * 3.14159265358979323846) * 0.5 + 0.5;', ' }', ' if (mode == 3)', ' {', ' return sqrt(1.0 - (--progress * progress));', ' }', ' if (mode == 4)', ' {', ' return 1.0 - sqrt(1.0 - progress * progress);', ' }', ' return progress;', '}', 'float ditherIGN(float value)', '{', ' float dx = dFdx(value);', ' float dy = dFdy(value);', ' float rateOfChange = sqrt(dx * dx + dy * dy);', ' value += (fract(52.9829189 * fract(0.06711056 * gl_FragCoord.x + 0.00583715 * gl_FragCoord.y)) - 0.5) * rateOfChange;', ' return value;', '}', 'vec4 rgbaToHsva(vec4 rgba)', '{', ' float r = rgba.r;', ' float g = rgba.g;', ' float b = rgba.b;', ' float a = rgba.a;', ' float min = min(min(r, g), b);', ' float max = max(max(r, g), b);', ' float d = max - min;', ' float h = 0.0;', ' float s = (max == 0.0) ? 0.0 : d / max;', ' float v = max;', ' if (max != min)', ' {', ' if (max == r)', ' {', ' h = (g - b) / d + ((g < b) ? 6.0 : 0.0);', ' }', ' else if (max == g)', ' {', ' h = (b - r) / d + 2.0;', ' }', ' else', ' {', ' h = (r - g) / d + 4.0;', ' }', ' h /= 6.0;', ' }', ' return vec4(h, s, v, a);', '}', 'float hsvaToRgbaConvert(float n, vec4 hsva)', '{', ' float k = mod(n + hsva.x * 6.0, 6.0);', ' float min = min(min(k, 4.0 - k), 1.0);', ' return hsva.z - hsva.z * hsva.y * max(0.0, min);', '}', 'vec4 hsvaToRgba(vec4 hsva)', '{', ' return vec4(', ' hsvaToRgbaConvert(5.0, hsva),', ' hsvaToRgbaConvert(3.0, hsva),', ' hsvaToRgbaConvert(1.0, hsva),', ' hsva.a', ' );', '}', 'vec4 mixBandColor(float progress, Band band)', '{', ' if (band.colorSpace == 0)', ' {', ' return mix(band.colorStart, band.colorEnd, progress);', ' }', ' vec4 hsvaStart = rgbaToHsva(band.colorStart);', ' vec4 hsvaEnd = rgbaToHsva(band.colorEnd);', ' if (band.colorSpace == 1)', ' {', ' float dH = hsvaStart.x - hsvaEnd.x;', ' if (dH > 0.5)', ' {', ' hsvaStart.x -= 1.0;', ' }', ' else if (dH < -0.5)', ' {', ' hsvaStart.x += 1.0;', ' }', ' }', ' else if (band.colorSpace == 2)', ' {', ' if (hsvaStart.x > hsvaEnd.x)', ' {', ' hsvaStart.x -= 1.0;', ' }', ' }', ' else if (band.colorSpace == 3)', ' {', ' if (hsvaStart.x < hsvaEnd.x)', ' {', ' hsvaStart.x += 1.0;', ' }', ' }', ' vec4 hsvaMix = mix(hsvaStart, hsvaEnd, progress);', ' hsvaMix.x = mod(hsvaMix.x, 1.0);', ' return hsvaToRgba(hsvaMix);', '}', 'vec4 getRampAt(float progress)', '{', ' Band band = getBand(progress);', ' float bandProgress = (progress - band.start) / (band.end - band.start);', ' float gamma = log(0.5) / log(band.middle);', ' bandProgress = pow(bandProgress, gamma);', ' bandProgress = interpolate(bandProgress, band.interpolation);', ' if (uDither)', ' {', ' bandProgress = ditherIGN(bandProgress);', ' }', ' return mixBandColor(bandProgress, band);', '}', ].join('\n');