Skip to content

Commit

Permalink
Redesign the shader
Browse files Browse the repository at this point in the history
  • Loading branch information
Asvarox committed Oct 29, 2023
1 parent 42ee173 commit acb052e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export default class CanvasDrawing {
const beatsSinceLastHit = drawingData.currentBeat - lastPlayerNoteEnd;

const forcePercent = Math.max(
Math.min(Math.pow((sungNotes - beatsSinceLastHit / 1.5) / MAX_LOOKUP_RANGE, 3), 0.99),
Math.min(Math.pow((sungNotes - beatsSinceLastHit / 1.5) / MAX_LOOKUP_RANGE, 3), 0.98),
0,
);
this.shaders?.updatePlayerForce(drawingData.playerNumber, forcePercent);
Expand Down
46 changes: 25 additions & 21 deletions src/Scenes/Game/Singing/GameOverlay/Drawing/Shaders/shader.frag
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
precision mediump float;

float MAX_FORCE = 300.0;
const float MAX_FORCE = 300.0;
const int SAMPLE_COUNT = 8; // 2^x
const float INTENSITY = 0.051;
// get our varyings
varying vec3 vVertexPosition;
varying vec2 vTextureCoord;
Expand All @@ -22,34 +24,36 @@ float random (vec2 st) {
return rnd(dot(st.xy, vec2(12.9898, 78.233)));
}

void distort(vec2 center, float force, float zoom, float noise) {
float actualForce = force * MAX_FORCE;
vec2 centerAbs = center * uResolution;
vec2 diff = (gl_FragCoord.xy - centerAbs.xy);
float dist = length(diff);
vec4 directionalBlur(in vec2 uv, in vec2 direction, in float intensity)
{
vec4 color = vec4(0.0);

vec4 newColor = texture2D(planeTexture, (centerAbs + (diff * zoom) + (noise * 20.0 * (1.0 - pow(zoom, 12.0)))) / uResolution);
gl_FragColor = newColor;
for (int i=1; i<=SAMPLE_COUNT; i++)
{
color += texture2D(planeTexture,uv+float(i)*intensity/float(SAMPLE_COUNT)*direction);
}

return color/float(SAMPLE_COUNT);
}

float calculateZoom(vec2 center, float force) {
float distort(vec2 center, float force, float comparedIntensity) {
float actualForce = force * MAX_FORCE;
vec2 centerAbs = center * uResolution;
vec2 diff = (gl_FragCoord.xy - centerAbs.xy);
float dist = length(diff);

return pow(smoothstep(0.0, actualForce, dist), 1.0/6.0);
vec2 direction = gl_FragCoord.xy - centerAbs.xy;
float dist = length(direction) / length(uResolution);


float intensity = max(0.0, 0.50 - dist) * INTENSITY * pow(force, 1.5);
if (intensity > comparedIntensity) {
vec4 newColor = directionalBlur(gl_FragCoord.xy / uResolution, normalize(direction), intensity);
gl_FragColor = newColor;
}
return intensity;
}

void main() {
float p0zoom = calculateZoom(uP0center, uP0force);
float p1zoom = calculateZoom(uP1center, uP1force);
float noise = (random(gl_FragCoord.xy)) / 2.0 - 0.5;

if (p0zoom < p1zoom) {
distort(uP0center, uP0force, p0zoom, noise);
} else {
distort(uP1center, uP1force, p1zoom, noise);
}
gl_FragColor = texture2D(planeTexture, vTextureCoord);
float p1intensity = distort(uP0center, uP0force, 0.0);
float p2intensity = distort(uP1center, uP1force, p1intensity);
}

0 comments on commit acb052e

Please sign in to comment.