Removed sun shower. Thunder and ripples are now handled by a third compute shader.

This commit is contained in:
Rezmason
2022-09-14 22:57:39 -07:00
parent 0d1d661401
commit 4c6e6fd662
9 changed files with 163 additions and 121 deletions

View File

@@ -0,0 +1,99 @@
precision highp float;
// These effects are used to spice up the non-canon versions of the code rain.
// The shader writes them to the channels of a data texture:
// R: multiplied effects— magnify the cell's brightness
// G: added effects— offset the cell's brightness
// B: unused
// A: unused
#define SQRT_2 1.4142135623730951
#define SQRT_5 2.23606797749979
uniform sampler2D previousEffectState;
uniform float numColumns, numRows;
uniform float time, tick;
uniform float animationSpeed;
uniform bool hasThunder, loops;
uniform float glyphHeightToWidth;
uniform int rippleType;
uniform float rippleScale, rippleSpeed, rippleThickness;
// Helper functions for generating randomness, borrowed from elsewhere
vec2 randomVec2( const in vec2 uv ) {
return fract(vec2(sin(uv.x * 591.32 + uv.y * 154.077), cos(uv.x * 391.32 + uv.y * 49.077)));
}
float wobble(float x) {
return x + 0.3 * sin(SQRT_2 * x) + 0.2 * sin(SQRT_5 * x);
}
float getThunder(float simTime, vec2 screenPos) {
if (!hasThunder) {
return 0.;
}
simTime *= 0.5;
float thunder = 1. - fract(wobble(simTime));
if (loops) {
thunder = 1. - fract(simTime + 0.3);
}
thunder = log(thunder * 1.5) * 4.;
thunder = clamp(thunder, 0., 1.) * 10. * pow(screenPos.y, 2.);
return thunder;
}
float getRipple(float simTime, vec2 screenPos) {
if (rippleType == -1) {
return 0.;
}
float rippleTime = (simTime * 0.5 + sin(simTime) * 0.2) * rippleSpeed + 1.; // TODO: clarify
if (loops) {
rippleTime = (simTime * 0.5) * rippleSpeed + 1.;
}
vec2 offset = randomVec2(vec2(floor(rippleTime), 0.)) - 0.5;
if (loops) {
offset = vec2(0.);
}
vec2 ripplePos = screenPos * 2. - 1. + offset;
float rippleDistance;
if (rippleType == 0) {
vec2 boxDistance = abs(ripplePos) * vec2(1., glyphHeightToWidth);
rippleDistance = max(boxDistance.x, boxDistance.y);
} else if (rippleType == 1) {
rippleDistance = length(ripplePos);
}
float rippleValue = fract(rippleTime) * rippleScale - rippleDistance;
if (rippleValue > 0. && rippleValue < rippleThickness) {
return 0.75;
}
return 0.;
}
// Main function
vec4 computeResult(float simTime, bool isFirstFrame, vec2 glyphPos, vec2 screenPos, vec4 previous) {
float multipliedEffects = 1.0 + getThunder(simTime, screenPos);
float addedEffects = getRipple(simTime, screenPos); // Round or square ripples across the grid
vec4 result = vec4(multipliedEffects, addedEffects, 0., 0.);
return result;
}
void main() {
float simTime = time * animationSpeed;
bool isFirstFrame = tick <= 1.;
vec2 glyphPos = gl_FragCoord.xy;
vec2 screenPos = glyphPos / vec2(numColumns, numRows);
vec4 previous = texture2D( previousEffectState, screenPos );
gl_FragColor = computeResult(simTime, isFirstFrame, glyphPos, screenPos, previous);
}

View File

@@ -4,7 +4,7 @@
#endif
precision lowp float;
uniform sampler2D shineState, symbolState;
uniform sampler2D shineState, symbolState, effectState;
uniform float numColumns, numRows;
uniform sampler2D glyphTex;
uniform float glyphHeightToWidth, glyphSequenceLength, glyphEdgeCrop;
@@ -17,7 +17,7 @@ uniform bool showDebugView;
uniform bool volumetric;
varying vec2 vUV;
varying vec4 vShine, vSymbol;
varying vec4 vShine, vSymbol, vEffect;
varying float vDepth;
float median3(vec3 i) {
@@ -63,6 +63,7 @@ void main() {
// Unpack the values from the data textures
vec4 shine = volumetric ? vShine : texture2D(shineState, uv);
vec4 symbol = volumetric ? vSymbol : texture2D(symbolState, uv);
vec4 effect = volumetric ? vEffect : texture2D(effectState, uv);
vec2 symbolUV = getSymbolUV(symbol.r);
float brightness = shine.r;
@@ -72,8 +73,10 @@ void main() {
brightness = brightnessOverride;
}
brightness *= effect.r; // multiplied effects
brightness += effect.g; // added effects
brightness = max(shine.b * cursorBrightness, brightness);
brightness = max(shine.a, brightness);
// In volumetric mode, distant glyphs are dimmer
if (volumetric && !showDebugView) {
brightness = brightness * min(1., vDepth);

View File

@@ -1,11 +1,11 @@
precision highp float;
// This shader is the star of the show.
// It writes falling rain to four channels of a data texture:
// It writes falling rain to the channels of a data texture:
// R: brightness
// G: unused
// B: whether the cell is a "cursor"
// A: some other effect, such as a ripple
// A: unused
// Listen.
// I understand if this shader looks confusing. Please don't be discouraged!
@@ -21,12 +21,10 @@ uniform float numColumns, numRows;
uniform float time, tick;
uniform float animationSpeed, fallSpeed;
uniform bool hasSun, hasThunder, loops;
uniform bool loops;
uniform float brightnessDecay;
uniform float baseContrast, baseBrightness;
uniform float raindropLength, glyphHeightToWidth;
uniform int rippleType;
uniform float rippleScale, rippleSpeed, rippleThickness;
// Helper functions for generating randomness, borrowed from elsewhere
@@ -66,88 +64,21 @@ float getBrightness(float rainTime) {
return (1. - fract(rainTime)) * baseContrast + baseBrightness;
}
// Additional effects
float applySunShowerBrightness(float brightness, vec2 screenPos) {
if (brightness >= -4.) {
brightness = pow(fract(brightness * 0.5), 3.) * screenPos.y * 1.5;
}
return brightness;
}
float applyThunderBrightness(float brightness, float simTime, vec2 screenPos) {
simTime *= 0.5;
float thunder = 1. - fract(wobble(simTime));
if (loops) {
thunder = 1. - fract(simTime + 0.3);
}
thunder = log(thunder * 1.5) * 4.;
thunder = clamp(thunder, 0., 1.);
thunder = thunder * pow(screenPos.y, 2.) * 3.;
return brightness + thunder;
}
float applyRippleEffect(float effect, float simTime, vec2 screenPos) {
if (rippleType == -1) {
return effect;
}
float rippleTime = (simTime * 0.5 + sin(simTime) * 0.2) * rippleSpeed + 1.; // TODO: clarify
if (loops) {
rippleTime = (simTime * 0.5) * rippleSpeed + 1.;
}
vec2 offset = randomVec2(vec2(floor(rippleTime), 0.)) - 0.5;
if (loops) {
offset = vec2(0.);
}
vec2 ripplePos = screenPos * 2. - 1. + offset;
float rippleDistance;
if (rippleType == 0) {
vec2 boxDistance = abs(ripplePos) * vec2(1., glyphHeightToWidth);
rippleDistance = max(boxDistance.x, boxDistance.y);
} else if (rippleType == 1) {
rippleDistance = length(ripplePos);
}
float rippleValue = fract(rippleTime) * rippleScale - rippleDistance;
if (rippleValue > 0. && rippleValue < rippleThickness) {
effect += 0.75;
}
return effect;
}
// Main function
vec4 computeResult(float simTime, bool isFirstFrame, vec2 glyphPos, vec2 screenPos, vec4 previous) {
// Determine the glyph's local time.
float rainTime = getRainTime(simTime, glyphPos);
float rainTimeBelow = getRainTime(simTime, glyphPos + vec2(0., -1.));
float cursor = fract(rainTime) < fract(rainTimeBelow) ? 1.0 : 0.0;
// Rain time is the backbone of this effect.
// Determine the glyph's brightness.
float brightness = getBrightness(rainTime);
if (hasSun) brightness = applySunShowerBrightness(brightness, screenPos);
if (hasThunder) brightness = applyThunderBrightness(brightness, simTime, screenPos);
// Determine the glyph's effect— the amount the glyph lights up for other reasons
float effect = 0.;
effect = applyRippleEffect(effect, simTime, screenPos); // Round or square ripples across the grid
// Blend the glyph's brightness with its previous brightness, so it winks on and off organically
if (!isFirstFrame) {
float previousBrightness = previous.r;
brightness = mix(previousBrightness, brightness, brightnessDecay);
}
vec4 result = vec4(brightness, fract(rainTime), cursor, effect);
vec4 result = vec4(brightness, fract(rainTime), cursor, 0.0);
return result;
}

View File

@@ -1,7 +1,7 @@
precision highp float;
// This shader governs the glyphs appearing in the rain.
// It writes each glyph's state to four channels of a data texture:
// It writes each glyph's state to the channels of a data texture:
// R: symbol
// G: age
// B: unused

View File

@@ -1,7 +1,7 @@
#define PI 3.14159265359
precision lowp float;
attribute vec2 aPosition, aCorner;
uniform sampler2D shineState, symbolState;
uniform sampler2D shineState, symbolState, effectState;
uniform float density;
uniform vec2 quadSize;
uniform float glyphHeightToWidth, glyphVerticalSpacing;
@@ -10,7 +10,7 @@ uniform vec2 screenSize;
uniform float time, animationSpeed, forwardSpeed;
uniform bool volumetric;
varying vec2 vUV;
varying vec4 vShine, vSymbol;
varying vec4 vShine, vSymbol, vEffect;
varying float vDepth;
highp float rand( const in vec2 uv ) {
@@ -24,6 +24,7 @@ void main() {
vUV = (aPosition + aCorner) * quadSize;
vShine = texture2D(shineState, aPosition * quadSize);
vSymbol = texture2D(symbolState, aPosition * quadSize);
vEffect = texture2D(effectState, aPosition * quadSize);
// Calculate the world space position
float quadDepth = 0.0;