Code Monkey home page Code Monkey logo

glsl-starfield's Introduction

Learning GLSL Fragement Shaders with Starfields

I love 80s- and early 90s-era graphical effects. I remember being blown away the first time I watched Future Crew's Second reality on my 486. It inspired me to learn trig, dive into Mode 13h, Turbo Pascal, assembly language. The best I could muster was some basic perspective font scrolling and simple fire effects. (ToT)

Second Reality
Scene from Second Reality

OpenGL fragment shaders provide a fun way to get back into building some simple effects. Of course one can build incredible, mind-blowing effects in GLSL. I cannot, or at least I will not be doing that here. I'm going to build the simplest of effects: the basic starfield "warp" effect.

What's in this repo

In this repo I build a simple 80s/90s starfield effect in a few different ways. I enjoy teaching. I also find that I think deeper about what I'm building if I have to explain it to others. So rather than just build this simple effect, I decided to write up the steps I took as sort of a tutorial.

Why Fragment Shaders / Why a Starfield

Starfields are really simple effects. At its simplest it's just pixels randomly spawned near the center of the screen that animate towards the edge of the screen over time, giving the effect of motion.

Fragment shaders are really interesting. They effectively run in parallel for every pixel on the screen. State can be passed into the shader from the calling program but it's more interesting (to me at least) to try and make the shader as stateless as possible.

The intersection of a starfield and a mostly stateless fragment shader creates an interesting problem. Without an array of star locations that we can update and render over time how do we create the effect such that each pixel can render in parallel given only the current time as an input. This is what makes this fun.

Goal

We want to create an effect like this screensaver from Windows 3.1...or maybe a little nicer.

Approaches

I've explored two primary approaches:

  1. Radial - approach the effect from a polar coordinate perspective
  2. Zoom - approach the effect using randomly rendered "layers" of stars that approach the camera using a zoom approach

During this exercise I heavily relied on the excellent Book of Shaders - highly recommend.

Running

To run the demos just serve this directory with a local webserver, such as:

npx http-server .

...and open your browser, typically to http://localhost:8080

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.