2
$\begingroup$

I have a scene rendered entirely with objects with additive blend mode. I have a value that is oscillating from 0, to 1. When it is 1 the object is supposed to be invisible, 0 should be entirely visible.

However I have noticed that something seems off about it, specifically it stays dark for too long. It is not my oscillating function, I suspect it having something to do with light using a logarithmic scale.

How would I convert my linear 0->1 value into a properly scaled value between 0->1

Fragment:

precision highp float;

uniform sampler2D s_texture;

varying vec2 v_texcoord;
varying float alpha;

void main()
{
    vec4 col = texture2D(s_texture, v_texcoord);
    gl_FragColor = vec4( col.rgba  - alpha);

    if (col.a < 0.001)
    {
        discard; //11.7->12.2
    }
    //gl_FragColor = vec4(0.4);

}

Vertex:

precision highp float;

attribute vec2 pos;
attribute vec2 tc;

uniform mat4 matrix;
uniform float time;
uniform float maxLeafs;
uniform vec2 position;
uniform float instance;
uniform vec2 anchorPoint;


varying vec2 v_texcoord;
varying float alpha;

vec4 finalTranslate(vec3 pos)
{
    return vec4(pos + vec3(position, 0.0), 1.0);
}
vec3 rotate(vec3 pos, float angle)
{
    float s = sin(-angle);
    float c = cos(-angle);
    return vec3(c * pos.x - s * pos.y, s * pos.x + c * pos.y, 0.0);
}
vec3 translate(vec2 pos)
{
    return vec3(pos - anchorPoint, 0.0);
}
vec3 scale(vec3 pos, float amm)
{
    return pos * amm;
}
void main()
{

    float v = log((time * 0.9) + 0.1);
    alpha = 0.0;

    float leafCoverage = (3.14149 * 2.0);
    float goal = ((instance) / (maxLeafs + 1.0)) * leafCoverage;
    float timeFactor = smoothstep(0.0, 1.0, time);
    float calculation1 = goal * timeFactor;
    float calculation2 = clamp(timeFactor * leafCoverage, 0.0, goal);

    float scaleFactor = clamp(smoothstep(0.0, 1.0, time * 1.2), 0.0, 1.0);

    gl_Position = matrix * finalTranslate(rotate( scale(translate(pos) ,scaleFactor) ,  calculation1));
    v_texcoord = tc;
}

Right now I am just taking the oscillating float time and passing it in directly, but eventually I will put it on a function so it fades in, temporarily is extra bright, then goes to the source texture.

Edit: I solved my problem, to my surprise the GLSL log function is in base e rather then 10.

$\endgroup$
3
  • $\begingroup$ Are you using $sine/cosine$ as the oscillating function ? $\endgroup$
    – user3437
    Commented Jun 1, 2016 at 17:30
  • $\begingroup$ Yes technically. However as you can see by the value v I am going to map it differently. $\endgroup$
    – J.Doe
    Commented Jun 1, 2016 at 17:37
  • 1
    $\begingroup$ If you solved your own problem, you can also post an answer for anyone who sees the same problem in future. This is actively encouraged. $\endgroup$ Commented Jun 1, 2016 at 21:28

1 Answer 1

1
$\begingroup$

Turns this was not an issue with the scale of the values passed in, just that the log function in glsl is base e. The solution was merely to do log(stuff)/log(10) and that got the result of a base 10 log.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.