Fragment Shaders
Fragment shader code is executed entirely on the GPU (Graphics Processing Unit).
Process Overview:
When rendering a scene:
Example:
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// Compute UV coordinates
vec2 uv = fragCoord.xy / iResolution.xy;
// Set the output color
fragColor = vec4(uv, 0.0, 1.0); // Sets a color based on UV
}
Color Space
In GLSL (and other graphics APIs), colors are typically represented using RGB (Red, Green, Blue) values. These values can exist in two spaces: the original space (or "integer space") and the normalized space (or "floating-point space"). Here’s how these two spaces differ:
1. Integer Space
Example:
2. Normalized Space (Floating-Point Space)
Conversion Formula:
To convert from the original space to normalized space: normalized value=original value255\text{normalized value} = \frac{\text{original value}}{255}
For example:
Relationship Between Spaces:
Example in GLSL:
In GLSL, colors are usually represented in normalized space. For example:
Here, the red channel is set to 1.0
, which corresponds to 255
in integer space, and the green and blue channels are 0.0
, corresponding to 0
in integer space.
Practical Use in Shaders:
When working with textures or colors in shaders:
Summary:
Would you like more details on any specific use of color spaces in shaders?
Common GLSL Variables and Functions:
These functions could be specific to Shadertoy or OpenGL ES
fragCoord.xy
: Current pixel coordinates (in pixels).iResolution.xy
: Screen resolution, used for normalization.iTime
: Time since shader started, useful for animation.iMouse
: Mouse position, used for interaction.iChannel0
,iChannel1
, etc.: Texture samplers for loading images or videos.gl_FragCoord
: Built-in pixel coordinates in GLSL.dot
,cross
,normalize
: Vector math functions for lighting and geometric operations.- Transformations: Rotation, scaling, and translation toanipulate coordinates.
mod
,fract
: Useful for creating repeating or tiled patterns.
Getting Started
When you're working in shaders, the pixel coordinates from fragCoord.xy
are typically in pixel units (e.g., ranging from (0,0)
to (1920,1080)
for an HD screen). If you want to make your shader work consistently across different resolutions, you need to normalize these coordinates.
By dividing fragCoord.xy
by iResolution.xy
, you map the pixel coordinates to a normalized space, which usually ranges from [0, 1]
.
INCLUDE CODE HERE
1. fragCoord.xy
:
- Definition:
fragCoord.xy
is a built-in variable in GLSL that represents the coordinates of the current fragment (or pixel) on the screen. - Values: It provides the (x, y) pixel coordinates of the fragment being processed, starting from the bottom-left corner (which is
(0, 0)
in most shader coordinate systems) and going up to the width and height of the viewport (i.e., screen resolution). - Purpose: It allows you to know the exact location of the pixel you're working with. For instance, if your window is 1920x1080,
fragCoord.xy
will range from(0,0)
(bottom-left corner) to(1920,1080)
(top-right corner).
2. iResolution.xy
:
- Definition:
iResolution.xy
is a uniform variable specific to ShaderToy that represents the dimensions of the screen (viewport) in pixels, i.e., the resolution of the output window. - Values:
iResolution.x
is the width of the screen in pixels, andiResolution.y
is the height of the screen in pixels. - Purpose: It provides the resolution of the screen so that you can scale or normalize coordinates based on the size of the output window. This is important to make sure your shader adapts to different screen resolutions.
The length
function in GLSL is a useful tool for calculating the magnitude (or Euclidean distance) of a vector. It's often used in shaders to compute distances between points or to create effects like radial gradients, circular shapes, and more.
Definition:
length(x)
: Returns the Euclidean length (or magnitude) of the vectorx
. It works for both 2D, 3D, and 4D vectors.
No Comments