patriciogonzalezvivo / thebookofshaders

Step-by-step guide through the abstract and complex universe of Fragment Shaders.

Home Page:http://TheBookOfShaders.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Math error in "Colors"

ollien opened this issue · comments

The "Colors" chapter states

The radius will return a maximum of 0.5 (because we are calculating the distance from the center of the viewport) so we need to double this range (by multiplying by two) to get a maximum of 1.0.

and refers to the following source code.

vec2 toCenter = vec2(0.5)-st;
float radius = length(toCenter)*2.0;

I believe this is incorrect. The length of a vector is defined as sqrt(x^2 + y^2). At maximum, x and y are both 0.5, resulting in sqrt(0.25 + 0.25) = sqrt(0.5). This means that the maximum is in fact 0.7071, which is sqrt(2)/2. This means that this multiplication actually returns a value in the range of [0, sqrt(2)]. Indeed, we can confirm this by adding the following the following to the top of hsb2rgb.

if (c.y > 1.0) {
    return vec3(0);
}

This gives the following result.
image

Changing this to c.y > 1.4142 gives us the result not cropped by the black circle (ignoring some slight black corners due to rounding errors).

corrected