DEV Community

Farzon Lotfi
Farzon Lotfi

Posted on • Originally published at blog.farzon.org on

The Geometry of Light: Understanding the World Through Shaders

Writing code is usually seen as a pursuit of logic. An order of statements designed to solve a problem. But when you dive into the world of shaders, code starts to feel less like engineering and more like painting with light. Here, code is the medium through which we translate the abstract beauty of mathematics into the tangible reality of light.

A gallery of shaders I made

The Pixel as a First Principle

In a fragment shader, we calculate the color of every pixel simultaneously. Each exists in a state of pure isolation, unaware of its neighbors, yet governed by the same universal functions. There is a staggering elegance in this: a thousand independent points of light following a single mathematical decree to create a unified image. By defining a coordinate system, we aren't just styling a screen; we are uncovering the hidden structures that govern how we perceive space and dimension.

The Beauty of the "Happy Accident"

In traditional software, a bug is a failure of logic. In shader writing, a bug is a discovery. You might accidentally run fract on the wrong vector and suddenly, the screen isn't broken; it is transformed into a kaleidoscope of unexpected geometry. Or you might multiply your vector by the wrong constant and now your lines aren't smooth. That is exactly what happened when I tried to draw a smooth line of a heart and found a cool jagged patter instead.

We should treat these moments like "happy little accidents." When the math produces something unintended, don't rush to "fix" it. Sit with it. These glitches are moments where the math reveals a path you hadn't considered. It is a powerful reminder that there is beauty in the unplanned, and that the most interesting results often live on the jagged edges of our errors.

The Pulse of the Sine Wave

When the world feels complex, we find clarity in the simplest equations. The sine wave is one of the great secrets of our reality. A perfect, oscillating loop that describes everything from the swing of a pendulum to the vibration of light itself to even the ups and downs we face in every day life.

Color = vec3(0.5 + 0.5 * sin(iTime)
Enter fullscreen mode Exit fullscreen mode

As the screen breathes in and out through this function, we see the raw power of a single line of math to create rhythm out of stillness. If you’re feeling overwhelmed, try coding the above simple "pulse" shader and match your breathe to the pulses.

Final Thoughts

We often use code to impose order, breaking the world down into procedures we can manage and solve. But shaders offer a different path. We aren't trying to bend the math to our will or organize the chaos; we are simply using these functions to appreciate the moment. We take the abstract language of mathematics and, for a brief moment, let it exist as pure light.

See more of these mathematical explorations here:

Top comments (0)