| const std::map<OpenGLData::OpenGLUniforms, String> OpenGLData::OpenGLUniformsMap = | |
| { | |
| { OpenGLUniforms::Bounds, "bounds" }, | |
| { OpenGLUniforms::InnerColor, "innerColor" }, | |
| { OpenGLUniforms::OuterColor, "outerColor" }, | |
| { OpenGLUniforms::Hollow, "hollow" }, | |
| { OpenGLUniforms::RoundedBoxWidth, "roundedBoxWidth" }, | |
| { OpenGLUniforms::RoundedBoxHeight, "roundedBoxHeight" }, | |
| { OpenGLUniforms::Gradient, "gradient" }, | |
| { OpenGLUniforms::Radius, "radius" } | |
| }; | |
| #define NEW_LINE "\n" | |
| #define NEW_LINE "\n" | |
| String makeFragmentShaderHeader() | |
| { | |
| return | |
| "uniform vec4 " + OpenGLData::OpenGLUniformsMap.at(OpenGLData::OpenGLUniforms::Bounds) + ";" NEW_LINE | |
| "uniform vec4 " + OpenGLData::OpenGLUniformsMap.at(OpenGLData::OpenGLUniforms::InnerColor) + ";" NEW_LINE | |
| "uniform vec4 " + OpenGLData::OpenGLUniformsMap.at(OpenGLData::OpenGLUniforms::OuterColor) + ";" NEW_LINE | |
| "uniform bool " + OpenGLData::OpenGLUniformsMap.at(OpenGLData::OpenGLUniforms::Hollow) + ";" NEW_LINE | |
| "uniform float " + OpenGLData::OpenGLUniformsMap.at(OpenGLData::OpenGLUniforms::RoundedBoxWidth) + ";" NEW_LINE | |
| "uniform float " + OpenGLData::OpenGLUniformsMap.at(OpenGLData::OpenGLUniforms::RoundedBoxHeight) + ";" NEW_LINE | |
| "uniform float " + OpenGLData::OpenGLUniformsMap.at(OpenGLData::OpenGLUniforms::Gradient) + ";" NEW_LINE | |
| "uniform float " + OpenGLData::OpenGLUniformsMap.at(OpenGLData::OpenGLUniforms::Radius) + ";" NEW_LINE | |
| ; | |
| } | |
| String fragmentShader = makeFragmentShaderHeader() + | |
| "float roundedBox(vec2 point, vec2 rectBounds, float radius)" NEW_LINE | |
| "{" NEW_LINE | |
| //this returns the distance | |
| " return length(max (abs(point) - rectBounds + radius, 0.0)) - radius;" NEW_LINE | |
| "}" NEW_LINE | |
| "float hsluv_fromLinear(float c) {" NEW_LINE | |
| " return c <= 0.0031308 ? 12.92 * c : 1.055 * pow(c, 1.0 / 2.4) - 0.055;" NEW_LINE | |
| "}" NEW_LINE | |
| "void main(void)" NEW_LINE | |
| "{" NEW_LINE | |
| // Normalized pixel coordinates (from 0 to 1) | |
| " vec2 normalizedFragCoord = gl_FragCoord.xy / bounds.xy;" NEW_LINE | |
| // " float ratio = bounds.y / bounds.x;" NEW_LINE | |
| //change normalized pixel coord range from [0,1] to [-1,1] | |
| " vec2 p = normalizedFragCoord * 2.0 - 1.0;" NEW_LINE | |
| //remove y-axis scaling from normalizing | |
| // " p.y *= ratio;" NEW_LINE | |
| " vec4 col;" NEW_LINE | |
| " float d = roundedBox(p, vec2(roundedBoxWidth, roundedBoxHeight), radius);" NEW_LINE//distance from rounded rect bounds | |
| " if( hollow == false )" NEW_LINE | |
| " {" NEW_LINE | |
| " col = sqrt(mix(innerColor*innerColor, outerColor*outerColor, smoothstep(0., gradient, d ) ));" NEW_LINE | |
| " }" NEW_LINE | |
| " else" NEW_LINE | |
| " {" NEW_LINE | |
| " if( abs(d) < gradient )" NEW_LINE | |
| " {" NEW_LINE | |
| " if( d == 0. )" NEW_LINE | |
| " {" NEW_LINE | |
| " col = innerColor; " NEW_LINE //purple | |
| " }" NEW_LINE | |
| " else" NEW_LINE | |
| " {" NEW_LINE | |
| " col = sqrt(mix(innerColor*innerColor, outerColor*outerColor, smoothstep(0., gradient, abs(d)) ));" NEW_LINE | |
| " }" NEW_LINE | |
| " }" NEW_LINE | |
| " else" NEW_LINE | |
| " {" NEW_LINE | |
| " col = outerColor; //yellow" NEW_LINE | |
| " }" NEW_LINE | |
| " }" NEW_LINE | |
| // "col.x = hsluv_fromLinear(col.x);" | |
| // "col.y = hsluv_fromLinear(col.y);" | |
| // "col.z = hsluv_fromLinear(col.z);" | |
| // "col.w = hsluv_fromLinear(col.w);" | |
| " gl_FragColor = col;" NEW_LINE | |
| "}"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment