Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| shader:start [2025/05/21 13:48] – [Circle] mh | shader:start [2025/06/02 21:23] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 4: | Line 4: | ||
| ===== Coding Shaders ===== | ===== Coding Shaders ===== | ||
| + | |||
| + | Shaders take a coordinate for each pixel on it's canvas and outputs an rgba value for that coordinate. | ||
| ==== Normalized input Coordinates ==== | ==== Normalized input Coordinates ==== | ||
| - | Generally the '' | + | Generally the **'' |
| - | Different compilers use different syntax to describe the resolution. | + | Different compilers use different syntax to describe the resolution. Here we'll use Shadertoy' |
| <code glsl> | <code glsl> | ||
| Line 50: | Line 52: | ||
| However, __this is not recommended__ as it creates a discontinuity of the values where they jump from 0.0 to 1.0 when reaching the radius of the circle. | However, __this is not recommended__ as it creates a discontinuity of the values where they jump from 0.0 to 1.0 when reaching the radius of the circle. | ||
| - | It's better to use a '' | + | It's better to use a **'' |
| <code glsl> | <code glsl> | ||
| - | float d = length(uv); | ||
| - | float c = ; | ||
| - | | ||
| float d = length(uv); | float d = length(uv); | ||
| float r = .3 | float r = .3 | ||
| - | float c = 1.-smoothstep(r, | + | float c = smoothstep(r, |
| // Output to screen | // Output to screen | ||
| Line 67: | Line 66: | ||
| <code glsl> | <code glsl> | ||
| - | float drawCircle(vec2 uv, float r, float blur) { | + | float drawCircle(vec2 uv, vec2 p, float r, float b) { |
| - | float d = length(uv); | + | //p for Position, r for Radius, b for edge Blur |
| - | float c = 1.-smoothstep(r, | + | |
| + | float d = length(uv-p); //This draws to the | ||
| + | float c = smoothstep(r, | ||
| | | ||
| return c; | return c; | ||
| } | } | ||
| + | </ | ||
| + | |||
| + | In the main function, draw multiple items by adding or subtracting to the color variable (the one being output in the end). | ||
| + | |||
| + | Drawing multiple circles for example : | ||
| + | |||
| + | <code glsl> | ||
| + | float b = 0.01; // Blur of edges of circles | ||
| + | | ||
| + | float c = drawCircle(uv, | ||
| + | c -= drawCircle(uv, | ||
| + | c -= drawCircle(uv, | ||
| + | </ | ||
| + | |||
| + | <WRAP info> | ||
| + | **Note :** when adding or removing regions in this manner, the color value for each RGB component of each pixel **can go** below 0.0 and above 1.0. | ||
| + | |||
| + | Shaders will automatically clamp the output between 0.0 and 1.0 but during the calculation of the color, the real value must be taken into account. | ||
| + | |||
| + | This behavior can lead to unexpected behaviors and colors in certain regions where multiple subtractions/ | ||
| + | |||
| + | ==== Rectangle ==== | ||
| + | |||
| + | To draw a rectangle we can use a function that draws bands using **'' | ||
| + | |||
| + | The band function would look like this | ||
| + | |||
| + | <code glsl> | ||
| + | float drawBand(float t, float start, float end, float blur){ | ||
| + | float step1 = smoothstep(start-blur, | ||
| + | float step2 = smoothstep(end+blur, | ||
| + | | ||
| + | return step1*step2; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Then get a rectangle by calling this function twice on the x and y : | ||
| + | |||
| + | <code glsl> | ||
| + | float mask = 0.; | ||
| + | float b = .01; | ||
| + | | ||
| + | mask = drawBand(uv.x, | ||
| + | mask *= drawBand(uv.y, | ||
| + | </ | ||
| + | ==== Adding Color ==== | ||
| + | |||
| + | Add color by using a **'' | ||
| + | |||
| + | Simply multiply color by the mask. | ||
| + | |||
| + | For the circle example above for example, it would look something like this : | ||
| + | |||
| + | <code glsl> | ||
| + | vec3 col = vec3(1., 1., 0.); // | ||
| + | | ||
| + | float b = 0.01; // Blur of edges of circles | ||
| + | | ||
| + | float mask = drawCircle(uv, | ||
| + | mask -= drawCircle(uv, | ||
| + | mask -= drawCircle(uv, | ||
| + | | ||
| + | |||
| + | col *= mask; | ||
| </ | </ | ||
| ===== ISF ===== | ===== ISF ===== | ||