Skip to content

Commit

Permalink
Pedal shader: adjust opacity based on normalized velocity.
Browse files Browse the repository at this point in the history
  • Loading branch information
kosua20 committed Nov 25, 2020
1 parent 7dc1d5d commit 9cf0d4d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 8 additions & 8 deletions resources/shaders/pedal.frag
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ in INTERFACE {
uniform vec2 inverseScreenSize;

uniform vec3 pedalColor;
uniform ivec4 pedalFlags; // sostenuto, damper, soft, expression
uniform vec4 pedalFlags; // sostenuto, damper, soft, expression
uniform float pedalOpacity;
uniform bool mergePedals;

Expand All @@ -17,20 +17,20 @@ out vec4 fragColor;

void main(){

float vis = pedalOpacity;

// When merging, only display the center pedal.
if(mergePedals && (int(In.id) != 0)){
discard;
}

// Else find if the current pedal (or any if merging) is active.
float maxIntensity = 0.0f;

for(int i = 0; i < 4; ++i){
if((mergePedals || int(In.id) == i) && pedalFlags[i] > 0){
vis = 1.0;
break;
if(mergePedals || int(In.id) == i){
maxIntensity = max(maxIntensity, pedalFlags[i]);
}
}

fragColor = vec4(pedalColor, vis);

float finalOpacity = mix(pedalOpacity, 1.0, maxIntensity);
fragColor = vec4(pedalColor, finalOpacity);
}
2 changes: 1 addition & 1 deletion src/resources/shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const std::map<std::string, std::string> shaders = {
{ "backgroundtexture_vert", "#version 330\n layout(location = 0) in vec2 v;\n out INTERFACE {\n vec2 uv;\n } Out ;\n uniform bool behindKeyboard;\n uniform float keyboardHeight = 0.25;\n void main(){\n vec2 pos = v;\n if(!behindKeyboard){\n pos.y = (1.0-keyboardHeight) * pos.y + keyboardHeight;\n }\n // We directly output the position.\n gl_Position = vec4(pos, 0.0, 1.0);\n // Output the UV coordinates computed from the positions.\n Out.uv = v.xy * 0.5 + 0.5;\n \n }\n "},
{ "backgroundtexture_frag", "#version 330\n in INTERFACE {\n vec2 uv;\n } In ;\n uniform sampler2D screenTexture;\n uniform float textureAlpha;\n uniform bool behindKeyboard;\n out vec4 fragColor;\n void main(){\n fragColor = texture(screenTexture, In.uv);\n fragColor.a *= textureAlpha;\n }\n "},
{ "pedal_vert", "#version 330\n layout(location = 0) in vec2 v;\n uniform vec2 shift;\n uniform vec2 scale;\n out INTERFACE {\n float id;\n } Out ;\n #define SOSTENUTO 33\n #define DAMPER 65\n #define SOFT 97\n #define EXPRESSION -1 damper, soft, expression\n void main(){\n // Translate to put on top of the keyboard.\n gl_Position = vec4(v.xy * scale + shift, 0.5, 1.0);\n // Detect which pedal this vertex belong to.\n Out.id = gl_VertexID < SOSTENUTO ? 0.0 :\n (gl_VertexID < DAMPER ? 1.0 :\n (gl_VertexID < SOFT ? 2.0 :\n 3.0\n ));\n \n }\n "},
{ "pedal_frag", "#version 330\n in INTERFACE {\n float id;\n } In ;\n uniform vec2 inverseScreenSize;\n uniform vec3 pedalColor;\n uniform ivec4 pedalFlags; // sostenuto, damper, soft, expression\n uniform float pedalOpacity;\n uniform bool mergePedals;\n out vec4 fragColor;\n void main(){\n float vis = pedalOpacity;\n // When merging, only display the center pedal.\n if(mergePedals && (int(In.id) != 0)){\n discard;\n }\n // Else find if the current pedal (or any if merging) is active.\n for(int i = 0; i < 4; ++i){\n if((mergePedals || int(In.id) == i) && pedalFlags[i] > 0){\n vis = 1.0;\n break;\n }\n }\n \n fragColor = vec4(pedalColor, vis);\n }\n "},
{ "pedal_frag", "#version 330\n in INTERFACE {\n float id;\n } In ;\n uniform vec2 inverseScreenSize;\n uniform vec3 pedalColor;\n uniform vec4 pedalFlags; // sostenuto, damper, soft, expression\n uniform float pedalOpacity;\n uniform bool mergePedals;\n out vec4 fragColor;\n void main(){\n // When merging, only display the center pedal.\n if(mergePedals && (int(In.id) != 0)){\n discard;\n }\n // Else find if the current pedal (or any if merging) is active.\n float maxIntensity = 0.0f;\n for(int i = 0; i < 4; ++i){\n if(mergePedals || int(In.id) == i){\n maxIntensity = max(maxIntensity, pedalFlags[i]);\n }\n }\n float finalOpacity = mix(pedalOpacity, 1.0, maxIntensity);\n fragColor = vec4(pedalColor, finalOpacity);\n }\n "},
{ "wave_vert", "#version 330\n layout(location = 0) in vec2 v;\n uniform float amplitude;\n uniform float keyboardSize;\n uniform float freq;\n uniform float phase;\n uniform float spread;\n out INTERFACE {\n float grad;\n } Out ;\n void main(){\n // Rescale as a thin line.\n vec2 pos = vec2(1.0, spread*0.02) * v.xy;\n // Sin perturbation.\n float waveShift = amplitude * sin(freq * v.x + phase);\n // Apply wave and translate to put on top of the keyboard.\n pos += vec2(0.0, waveShift + (-1.0 + 2.0 * keyboardSize));\n gl_Position = vec4(pos, 0.5, 1.0);\n Out.grad = v.y;\n }\n "},
{ "wave_frag", "#version 330\n in INTERFACE {\n float grad;\n } In ;\n uniform vec3 waveColor;\n uniform float waveOpacity;\n out vec4 fragColor;\n void main(){\n // Fade out on the edges.\n float intensity = (1.0-abs(In.grad));\n // Premultiplied alpha.\n fragColor = waveOpacity * intensity * vec4(waveColor, 1.0);\n }\n "},
{ "fxaa_vert", "#version 330\n layout(location = 0) in vec3 v;\n out INTERFACE {\n vec2 uv;\n } Out ;\n void main(){\n \n // We directly output the position.\n gl_Position = vec4(v, 1.0);\n // Output the UV coordinates computed from the positions.\n Out.uv = v.xy * 0.5 + 0.5;\n \n }\n "},
Expand Down

0 comments on commit 9cf0d4d

Please sign in to comment.