Guest User

"gas giant" tweetjam explanation

a guest
Nov 12th, 2024
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.23 KB | None | 0 0
  1. -- set the screen's color-palette
  2. -- ?"hello" is a shorthand for print("hello") in pico8.
  3. -- p8 also has a feature called "P8SCII" where
  4. -- you can print special escape-codes to do stuff.
  5. -- \^! is the "blit data to memory" command
  6. -- 5f10 is the target memory address (palette location)
  7. -- each character after that is one color-value to write
  8. ?"\^!5f100β–ˆΛ‡β˜‰8πŸ…ΎοΈβ—†?7"
  9.  
  10.  
  11. -- begin main loop
  12. -- underscore is our goto-label...could be any word,
  13. -- but this looks funny to me in tweetjam-land
  14. ::_::
  15.  
  16.     -- main idea: calculate color for one random pixel at a time
  17.  
  18.     -- pick an (x,y) position from -2 to +2,
  19.     -- we'll consider (0,0) to be the center of the screen
  20.     x = rnd(4) - 2
  21.     y = rnd(4) - 2
  22.  
  23.     -- square distance to center
  24.     d = x*x + y*y
  25.  
  26.     -- "c" is the color we'll use if we're not touching the planet
  27.     c = max(2 - d/4)
  28.  
  29.     -- if the planet has radius=1, are we touching it?
  30.     if (d<1) then
  31.         -- solve for a z position which makes (x,y,z) into a unit-vector.
  32.         -- rearranged version of the pythagorean theorem: x^2+y^2+z^2 = length^2,
  33.         -- where length is assumed to be 1, and x and y are known. solve for z.
  34.         -- (remember, d is x^2+y^2)
  35.         z=sqrt(1-d)
  36.  
  37.         -- the next part where we pick a color is all one huge horrible line of
  38.         -- code in the original, so i've split it apart into intermediate values.
  39.  
  40.         -- lambert-shading term:
  41.         -- assume light-vector is (1,-1,1), and treat (x,y,z) as our surface normal,
  42.         -- because on the surface of a unit sphere centered at the world origin,
  43.         -- your position is also the (unit length) normal.
  44.         -- standard lambert is dot(normal, lightDir), or nX*lX + nY*lY + nZ*lZ
  45.         -- ...but we're assuming that lightDir is (1,-1,1), so this simplifies down
  46.         -- to just nX - nY + nZ. multiply by 2 at the end just expands the range of
  47.         -- possible brightness-values.
  48.         light = (x-y+z) * 2
  49.  
  50.         -- this value is 1 in the center and drops to zero at the outer edges
  51.         -- of the planet. we'll multiply this into our color to make the
  52.         -- planet fade to dark around the edges.
  53.         fresnel = 1 - (1-z)^2
  54.  
  55.         -- prepare some values for a stripey pattern across the surface:
  56.         angle = atan2(x, z) -- current angle around the y axis
  57.         rotate = t() / 8 -- t() is current time in seconds
  58.         wiggle = sin(angle*8 + rotate) / 14 -- add a wiggle as we move around the planet
  59.        
  60.         -- finally, time for stripes!
  61.         stripes = abs((y + wiggle - z/4)%.4 - .2) * 5
  62.  
  63.         -- combine the above stuff into a final color value
  64.         c = 2 + (1.5 + light + stripes) * fresnel
  65.     end
  66.  
  67.     -- convert to screenspace (0 to 127), write our chosen color.
  68.     -- the rnd() at the end applies white-noise dithering
  69.     pset(64 + x*32, 64 + y*32, c + rnd())
  70.  
  71. -- we're done with this pixel! back to the start, do it again
  72. goto _
  73.  
  74. -- this is an infinite loop with no waiting...which would
  75. -- usually crash an application, but pico8 has built-in
  76. -- limits for how much work can be done per frame, so
  77. -- it'll interrupt our work periodically on its own, and
  78. -- the final result is that we update "as many pixels as
  79. -- we can" per frame.
Advertisement
Add Comment
Please, Sign In to add comment