Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- set the screen's color-palette
- -- ?"hello" is a shorthand for print("hello") in pico8.
- -- p8 also has a feature called "P8SCII" where
- -- you can print special escape-codes to do stuff.
- -- \^! is the "blit data to memory" command
- -- 5f10 is the target memory address (palette location)
- -- each character after that is one color-value to write
- ?"\^!5f100βΛβ8π ΎοΈβ?7"
- -- begin main loop
- -- underscore is our goto-label...could be any word,
- -- but this looks funny to me in tweetjam-land
- ::_::
- -- main idea: calculate color for one random pixel at a time
- -- pick an (x,y) position from -2 to +2,
- -- we'll consider (0,0) to be the center of the screen
- x = rnd(4) - 2
- y = rnd(4) - 2
- -- square distance to center
- d = x*x + y*y
- -- "c" is the color we'll use if we're not touching the planet
- c = max(2 - d/4)
- -- if the planet has radius=1, are we touching it?
- if (d<1) then
- -- solve for a z position which makes (x,y,z) into a unit-vector.
- -- rearranged version of the pythagorean theorem: x^2+y^2+z^2 = length^2,
- -- where length is assumed to be 1, and x and y are known. solve for z.
- -- (remember, d is x^2+y^2)
- z=sqrt(1-d)
- -- the next part where we pick a color is all one huge horrible line of
- -- code in the original, so i've split it apart into intermediate values.
- -- lambert-shading term:
- -- assume light-vector is (1,-1,1), and treat (x,y,z) as our surface normal,
- -- because on the surface of a unit sphere centered at the world origin,
- -- your position is also the (unit length) normal.
- -- standard lambert is dot(normal, lightDir), or nX*lX + nY*lY + nZ*lZ
- -- ...but we're assuming that lightDir is (1,-1,1), so this simplifies down
- -- to just nX - nY + nZ. multiply by 2 at the end just expands the range of
- -- possible brightness-values.
- light = (x-y+z) * 2
- -- this value is 1 in the center and drops to zero at the outer edges
- -- of the planet. we'll multiply this into our color to make the
- -- planet fade to dark around the edges.
- fresnel = 1 - (1-z)^2
- -- prepare some values for a stripey pattern across the surface:
- angle = atan2(x, z) -- current angle around the y axis
- rotate = t() / 8 -- t() is current time in seconds
- wiggle = sin(angle*8 + rotate) / 14 -- add a wiggle as we move around the planet
- -- finally, time for stripes!
- stripes = abs((y + wiggle - z/4)%.4 - .2) * 5
- -- combine the above stuff into a final color value
- c = 2 + (1.5 + light + stripes) * fresnel
- end
- -- convert to screenspace (0 to 127), write our chosen color.
- -- the rnd() at the end applies white-noise dithering
- pset(64 + x*32, 64 + y*32, c + rnd())
- -- we're done with this pixel! back to the start, do it again
- goto _
- -- this is an infinite loop with no waiting...which would
- -- usually crash an application, but pico8 has built-in
- -- limits for how much work can be done per frame, so
- -- it'll interrupt our work periodically on its own, and
- -- the final result is that we update "as many pixels as
- -- we can" per frame.
Advertisement
Add Comment
Please, Sign In to add comment