Lecture notes, with the experiments left in.
How Vision draws a world
Here is a strange fact. When Vision opens a world (an ocean, say, with water and rock and sunlight in it) nobody sends it a picture of an ocean. No film. No model. No images at all. What arrives is a short piece of text. The text asks every pixel on your screen one question, and your device answers. it's running right now, on your machine
That's all there is to it. The little world above is made that way. It isn't a video. Grab the coral dot and look around: a video couldn't do that.
One honest note before we start. The little world on this page is this page's own demonstration, written for this lesson. It is not Shark Reef, or Silfra, or any of FMVision's worlds; those are far richer. But they are made of the same stuff, in the same way.
Let's take it apart.
What gets sent
FMVision once had an ocean made the usual way: built ahead of time, compiled, bundled up and shipped. It weighed 120 MB, and you waited 12 s before you saw any water.
The ocean that replaced it is called Shark Reef. It is about 108 KB of source: plain text. Here are the two, drawn to scale, area for area. The big square is the 120 MB. Now find the other one. no cheating. it really is that small
Roughly a thousand times smaller. How can that be the same ocean?
Look, the thing is: it isn't the result that's being sent. It's the recipe. A photograph of a cake is big. "Flour, eggs, sugar, forty minutes" is small, and your kitchen does the rest. The text is the recipe, and your device is the kitchen. So what does the recipe say?
One question per pixel
The text has a name. It's called a shader: a small program that is handed the position of one dot on the screen and gives back that dot's colour. That is its whole job.
Your screen is a few million dots. The device goes to each one and asks: "You are at x, y. What colour are you?" And every dot works it out from the same little rule. Here is a screen with very few dots, so you can catch one in the act. drag the dot. watch the numbers
The rule (the same for every dot)
ripple = ½ + ½ sin(9x + 2 sin 7y)
glow = 1 − 2.2 × distance to (.72, .28)
(never below 0)
red = .05 + .35 ripple + .6 glow
green = .45 + .35 ripple + .3 glow
blue = .55 + .30 ripple
- in
- x 0.50, y 0.50
- out
Now slide More dots to the right. I have not changed the rule. Not one character. I'm only asking more dots, and the blocky thing turns into a smooth picture. Same text, more answers. That is why the text can be small and the picture can be as big and as sharp as your screen likes.
Waves add
Fine, but that rule made a blob. We want water. Water looks terribly complicated, and here's the pleasant surprise: it mostly isn't.
Take one smooth wave, a sine: the plain up-and-down you get from anything going round in a circle. On its own it's dull. Now add a second one, shorter, going a different way. Then a third. You don't simulate anything. You take the heights and you add them. see? it's just adding
Switch them off until only wave one is left: a swimming-pool model. Now bring them back one at a time and watch it turn into something you'd believe. Four sines. Each one costs a line of text.
Light bends
Now the sunshine. Light travels slower in water than in air: 1.33 times slower. So a ray (one thin straight line of light) that hits the surface at a slant gets turned, the way a cart swerves when one wheel reaches the sand first.
There's a rule for how much, called Snell's law, and it fits on one line: the sine of the angle in air is 1.33 times the sine of the angle in water. Swing the sun around and check me. swing the sun
Now here's the fun part. Don't send one ray. Send a whole sunbeam, and make the surface wavy.
Every bit of a wavy surface is a little tilted window, and each window turns its ray a slightly different way. So down on the floor, some spots catch rays from two windows at once, and some spots catch none. Where the rays bunch up, the floor is bright. That wobbling bright net you've seen on the bottom of every pool has a name, a caustic, but the name isn't the thing. The thing is the bunching.
Pull the wave height down to nothing: even light, dull floor. Lift it: the net appears. And notice the waves don't make any light. They only move it about. Every bright line is paid for by a dimmer patch beside it.
One more thing, since we're here. Blue light bends a touch more than red. Split the colours and each bright line grows a rainbow edge. Colour, in this business, is just bending by slightly different amounts.
How far to the nearest thing?
A world needs solid things in it: a floor, a wall, a boulder. The usual way is to send a model, which is a file with thousands of little triangles in it. Here is a cheekier way, with no file at all.
For each shape, write a rule that answers one question: "From this point, how far away are you?" For a ball that's easy. It's the distance to the middle, minus the radius. One line.
Now a line of sight leaves your eye through one pixel and wants to know what it hits. It asks, "how far is the nearest thing?" Say the answer is three paces. Then three paces in any direction is safe, so it takes three paces. Asks again. Steps again. The safe circles shrink as it closes in, and when the answer is "nearly nothing", it has arrived. move the eye, the aim, and the rocks
That is how a world gets walls and boulders without a single model file. A boulder is a sentence. Want it lumpy? Add a wobble to the sentence. Want a hundred? Ask about the nearest of a hundred. The text hardly grows.
Put it together
You now own every part of the little world from the top of the page. Sky, floor, boulders, waves, bending light, and one more: water swallows light, red first, so far-off things go blue-green. Each part is a few lines of text.
Here they are as switches. Start from nearly nothing and build it. Watch the size as you go. this is the whole trick
Layers
All six on? That's the opening figure again, and you built it. And it is still only a page or so of text.
The first frame
So what actually happens between "the text arrives" and "you're looking at water"? Three things, in a row.
The text arrives. Then your device's graphics compiler, a translator that is already built into it, turns the text into a program its graphics chip can run. Then the chip asks every pixel the question. That's the first frame. Scrub through it.
- 1 · arrivethe text comes down the wire
- 2 · translatetext becomes a program
- 3 · askevery pixel answers
The whole trip, for a real world: Shark Reef goes live about 100 ms after its ~108 KB arrive. The compiled ocean it replaced took 12 s and 120 MB. Numbers like that are easy to read and hard to feel, so press the button. The bottom bar takes a true twelve seconds. go on. you'll want to look away
And the little world on this page? I timed it on your device when the page opened: from text to first frame, for of source. (That's this page's toy, on your machine, just now. It says nothing about any other world.)
This is where Vision, the browser, earns its keep. While you're still looking at a gallery of worlds, Vision opens its line to the servers that hand out scenes, on a hunch that you'll want one. That's called a speculative connection. When you do pick a world, the line is already open, and the first frame comes sooner.
And when the people who make Vision want to know whether it's fast, they don't trust an abstract test. The scenes are the benchmark: Listening Court, Silfra, Hamnøy, Shark Reef. If the worlds run well, the browser is good. If they don't, it isn't.
Your turn
Here is the whole text of the little world. It's live: change a number and the world changes while you type. You can't hurt anything. If you write something the compiler can't follow, it will say so politely and keep showing the last version that worked. break it on purpose. best way to learn
try this
A world is not a picture that gets sent.
It is a short piece of text that asks every pixel one question, and your device answers. You've just done it by hand.
Vision is FMVision's own browser: the browser and launcher for the studio's sites, worlds and workspaces. It is built for worlds made this way: no shipped pictures, a few hundred kilobytes of source, and the device grows everything else.
This page is one file of handwritten HTML, CSS, JavaScript and GLSL. It asked the network for nothing.