AgentE382

ingie's OpenComputers Palette Based Hologram Animations

Dec 5th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.06 KB | None | 0 0
  1. -- from https://oc.cil.li/index.php?/topic/267-palette-based-hologram-animations/
  2. --[[holo-wave.lua]]--
  3.  
  4. local keyboard = require("keyboard")
  5. local component = require("component")
  6. local hologram = component.hologram
  7.  
  8. hologram.clear()
  9.  
  10. -- because tau is better than Pi
  11. local tau = math.pi * 2
  12.  
  13. -- magic numbers
  14. local scale = 10 -- the size of the hologram
  15. local offset = 20 -- the hologram offset from zero which is the zero axis of the sine wave
  16.  
  17. -- our colours
  18. local dark = 0 -- palette value for "off"
  19. local dim = 0x661133 -- palette value for "nearly off" - change this to 0 for a less subtle animation
  20. local light = 0xff0000 -- palette value for "on"
  21.  
  22.  
  23. -- draw our sine waves
  24. -- no need to do it 3 times, but this is an offset to the sin function/voxel position, which makes the image "thicker"
  25. for n = 1,3 do
  26.  
  27.   os.sleep(0.01)
  28.  
  29.     for x = 1,48 do
  30.       for y = 1,48 do
  31.  
  32.         -- you do the math[s] - the principle is simple sin(x) varies from 0 to 1,
  33.         -- but we want to scale that up, hence the scale
  34.         local z = math.sin( ( y - x + n) / tau ) * scale + offset
  35.  
  36.         -- this might look tricky, but tis just getting the next colour in a sequence of
  37.         -- 1,2,3,1,2,3,1,2,3... etc... in a way which matches the angle of the wave
  38.         -- play with the formula [leaving the %3 + 1 bit] to get funkier ripples
  39.         local col = (( y - x + n) % 3) + 1
  40.  
  41.         hologram.set( x, z, y, col)
  42.  
  43.         if keyboard.isKeyDown(keyboard.keys.q) then -- quicktime event "press q to die" ;)
  44.           break
  45.         end
  46.  
  47.       end
  48.     end
  49. end
  50.  
  51. -- do the animation, i have set this to just 100 for a demo...
  52. -- loop if forever if you want, but beware, there is no yielding for the purposes of speed/smoothness
  53. for n = 1, 100 do
  54.  
  55. -- the % [modulo] bits here are just to get a sequence as 1,2,3 : 2,3,1 : 3,1,2 : 1,2,3 etc..
  56. -- for each of the 3 colours [including dark/off]
  57.   hologram.setPaletteColor(((n-2) % 3) + 1,dark)
  58.   hologram.setPaletteColor(((n-1) % 3) + 1,dim)
  59.   hologram.setPaletteColor((n % 3) + 1,light)
  60.  
  61. end
Advertisement
Add Comment
Please, Sign In to add comment