Advertisement
Yevano

Shader Test File

Sep 10th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.70 KB | None | 0 0
  1. os.loadAPI("gfx")
  2.  
  3. -- For convenience.
  4. local sin   = math.sin
  5. local cos   = math.cos
  6. local tan   = math.tan
  7. local abs   = math.abs
  8. local min   = math.min
  9. local max   = math.max
  10. local floor = math.floor
  11. local sqrt  = math.sqrt
  12. local clock = os.clock
  13.  
  14. -- I got lazy so I just localized all of
  15. -- these functions here.
  16. local STRATEGY_FULL_CELL = gfx.STRATEGY_FULL_CELL
  17. local STRATEGY_HEX_CELL = gfx.STRATEGY_HEX_CELL
  18. local STRATEGY_TWO_CELL = gfx.STRATEGY_TWO_CELL
  19. local vec2 = gfx.vec2
  20. local vec3 = gfx.vec3
  21. local vec4 = gfx.vec4
  22. local length2 = gfx.length2
  23. local length3 = gfx.length3
  24. local length4 = gfx.length4
  25. local normalize2 = gfx.normalize2
  26. local normalize3 = gfx.normalize3
  27. local normalize4 = gfx.normalize4
  28. local distance2 = gfx.distance2
  29. local distance3 = gfx.distance3
  30. local distance4 = gfx.distance4
  31. local dot2 = gfx.dot2
  32. local dot3 = gfx.dot3
  33. local dot4 = gfx.dot4
  34. local add2 = gfx.add2
  35. local add3 = gfx.add3
  36. local add4 = gfx.add4
  37. local sub2 = gfx.sub2
  38. local sub3 = gfx.sub3
  39. local sub4 = gfx.sub4
  40. local mul2 = gfx.mul2
  41. local mul3 = gfx.mul3
  42. local mul4 = gfx.mul4
  43. local div2 = gfx.div2
  44. local div3 = gfx.div3
  45. local div4 = gfx.div4
  46. local add2s = gfx.add2s
  47. local add3s = gfx.add3s
  48. local add4s = gfx.add4s
  49. local sub2s = gfx.sub2s
  50. local sub3s = gfx.sub3s
  51. local sub4s = gfx.sub4s
  52. local mul2s = gfx.mul2s
  53. local mul3s = gfx.mul3s
  54. local mul4s = gfx.mul4s
  55. local div2s = gfx.div2s
  56. local div3s = gfx.div3s
  57. local div4s = gfx.div4s
  58. local mat2mulvec2 = gfx.mat2mulvec2
  59. local mat3mulvec3 = gfx.mat3mulvec3
  60. local getRotMat2 = gfx.getRotMat2
  61. local getRotMat3x = gfx.getRotMat3x
  62. local getRotMat3y = gfx.getRotMat3y
  63. local getRotMat3z = gfx.getRotMat3z
  64. local fract = gfx.fract
  65. local mix = gfx.mix
  66. local clamp = gfx.clamp
  67. local smoothstep = gfx.smoothstep
  68. local lab2xyz = gfx.lab2xyz
  69. local xyz2rgb = gfx.xyz2rgb
  70. local lab2rgb = gfx.lab2rgb
  71. local rgb2lab = gfx.rgb2lab
  72. local colorDistance = gfx.colorDistance
  73. local createPalette = gfx.createPalette
  74. local paletteConvert = gfx.paletteConvert
  75. local createShaderProgram = gfx.createShaderProgram
  76. local addShader = gfx.addShader
  77. local applyShaderProgram = gfx.applyShaderProgram
  78. local createShader = gfx.createShader
  79. local setShaderInput = gfx.setShaderInput
  80. local createFrameBuffer = gfx.createFrameBuffer
  81. local drawFrameBuffer = gfx.drawFrameBuffer
  82. local getUnstretchedResolution = gfx.getUnstretchedResolution
  83. local clearFrameBuffer = gfx.clearFrameBuffer
  84.  
  85. -- This table contains the set of pixels we
  86. -- draw to our frame buffer. In this case,
  87. -- we will just use all the colors with no
  88. -- foreground text.
  89. local charTable = {
  90.     {" ", "0", "0"},
  91.     {" ", "1", "0"},
  92.     {" ", "2", "0"},
  93.     {" ", "3", "0"},
  94.     {" ", "4", "0"},
  95.     {" ", "5", "0"},
  96.     {" ", "6", "0"},
  97.     {" ", "7", "0"},    
  98.     {" ", "8", "0"},
  99.     {" ", "9", "0"},
  100.     {" ", "a", "0"},
  101.     {" ", "b", "0"},
  102.     {" ", "c", "0"},
  103.     {" ", "d", "0"},
  104.     {" ", "e", "0"},
  105.     {" ", "f", "0"}
  106. }
  107.  
  108. -- This table contains the colors for each
  109. -- pixel in charTable. e.g., index 0 is used
  110. -- for colors.white.
  111. local indexTable = {
  112.     vec3(0xF0/0xFF, 0xF0/0xFF, 0xF0/0xFF),
  113.     vec3(0xF2/0xFF, 0xB2/0xFF, 0x33/0xFF),
  114.     vec3(0xE5/0xFF, 0x7F/0xFF, 0xD8/0xFF),
  115.     vec3(0x99/0xFF, 0xB2/0xFF, 0xF2/0xFF),
  116.     vec3(0xDE/0xFF, 0xDE/0xFF, 0x6C/0xFF),
  117.     vec3(0x7F/0xFF, 0xCC/0xFF, 0x19/0xFF),
  118.     vec3(0xF2/0xFF, 0xB2/0xFF, 0xCC/0xFF),
  119.     vec3(0x4C/0xFF, 0x4C/0xFF, 0x4C/0xFF),
  120.     vec3(0x99/0xFF, 0x99/0xFF, 0x99/0xFF),
  121.     vec3(0x4C/0xFF, 0x99/0xFF, 0xB2/0xFF),
  122.     vec3(0xB2/0xFF, 0x66/0xFF, 0xE5/0xFF),
  123.     vec3(0x33/0xFF, 0x66/0xFF, 0xCC/0xFF),
  124.     vec3(0x7F/0xFF, 0x66/0xFF, 0x4C/0xFF),
  125.     vec3(0x57/0xFF, 0xA6/0xFF, 0x4E/0xFF),
  126.     vec3(0xCC/0xFF, 0x4C/0xFF, 0x4C/0xFF),
  127.     vec3(0x19/0xFF, 0x19/0xFF, 0x19/0xFF)
  128. }
  129.  
  130. -- Note that the above two tables must
  131. -- correspond by index.
  132.  
  133. local function shaderFunc(oldColor, fragCoord, inputs)
  134.     --[[ Colors! ]]--
  135.     --[[local res = inputs[1]
  136.     local t = inputs[2]
  137.     local uv = div2(fragCoord, res)
  138.     return vec4(uv.x, uv.y, 0.5 + 0.5 * math.sin(t), 1)]]
  139.  
  140.     --[[ Rotating pattern ]]--
  141.     --[[local res = inputs[1]
  142.     local t = inputs[2]
  143.     local uv = div2(fragCoord, res)
  144.     uv.x = uv.x * res.x/res.y--*2/3
  145.     uv.x = uv.x - res.x/res.y/2--*2/3/2
  146.     uv.y = uv.y - 0.5
  147.     local m = getRotMat2(t)
  148.     local uv = mat2mulvec2(m, sub2(uv, vec2(0, 0)))
  149.     return vec4((uv.x % 0.4)*5*(sin(t)+1)/2, (uv.y % 0.2)*5*(cos(t)+1)/2, 0, 1)]]
  150.  
  151.     --[[ Green vortex ]]--
  152.     local brightness = 3.
  153.     local ray_brightness = 5.
  154.     local gamma = 6.
  155.     local spot_brightness = 1.5
  156.     local ray_density = 6.
  157.     local curvature = 90.
  158.     local red =   1.8
  159.     local green = 3.
  160.     local blue =  .5
  161.     local noisetype = 1
  162.     local sin_freq = 6.
  163.  
  164.     local iResolution = inputs[1]
  165.     local iGlobalTime = inputs[2]
  166.     local fragColor
  167.  
  168.     local function hash(n) return fract(sin(n)*43758.5453) end
  169.  
  170.     local function noise(x)
  171.         x = vec2(x.x, x.y)
  172.         x = mul2s(x, 1.75)
  173.         local p = vec2(floor(x.x), floor(x.y))
  174.         local f = vec2(fract(x.x), fract(x.y))
  175.  
  176.         f = mul2(mul2(f, f), (sub2(vec2(3, 3), mul2s(f, 2))))
  177.  
  178.         local n = p.x + p.y*57.0
  179.  
  180.         local res = mix(mix( hash(n+  0.0), hash(n+  1.0),f.x),
  181.                         mix( hash(n+ 57.0), hash(n+ 58.0),f.x),f.y)
  182.         return res
  183.     end
  184.  
  185.     local m2 = { 0.80,  0.60, -0.60,  0.80 };
  186.     local function fbm(p)
  187.         p = vec2(p.x, p.y)
  188.         local z=2.
  189.         local rz = 0.
  190.         p = mul2s(p, 0.25)
  191.         for i = 1, 5 do
  192.             if noisetype == 1 then
  193.                 rz = rz + abs((noise(p)-0.5)*2.)/z
  194.             elseif noisetype == 2 then
  195.                 rz = rz + (sin(noise(p)*sin_freq)*0.5+0.5) /z
  196.             else
  197.                 rz = rz + noise(p)/z
  198.             end
  199.             z = z*2.
  200.             p = mat2mulvec2(m2, mul2s(p, 2))
  201.         end
  202.         return rz
  203.     end
  204.  
  205.     local function mainImage()
  206.         local t = -iGlobalTime*0.03*4
  207.         uv = sub2s(div2(fragCoord, iResolution), 0.5)
  208.         uv.x = uv.x * iResolution.x/iResolution.y
  209.         uv = add2s(mul2s(mul2s(uv, curvature), .05), 0.0001)
  210.        
  211.         local r = sqrt(dot2(uv, uv))
  212.         local x = dot2(normalize2(uv), vec2(.5,0.)) + t
  213.         local y = dot2(normalize2(uv), vec2(.0,.5)) + t
  214.        
  215.         --x = fbm(vec2(y*ray_density*0.5,r+x*ray_density*.2));
  216.         --y = fbm(vec2(r+y*ray_density*0.1,x*ray_density*.5));
  217.        
  218.         local val
  219.         val = fbm(vec2(r+y*ray_density,r+x*ray_density-y))
  220.         val = smoothstep(gamma*.02-.1,ray_brightness+(gamma*0.02-.1)+.001,val)
  221.         val = sqrt(val)
  222.        
  223.         local col = div3(vec3(val, val, val), vec3(red,green,blue))
  224.         --col = clamp(sub3(vec3(1, 1, 1), col),0,1)
  225.         col = vec3(
  226.             clamp(1-col.x, 0, 1),
  227.             clamp(1-col.y, 0, 1),
  228.             clamp(1-col.z, 0, 1))
  229.  
  230.         --col = mix(col,vec3(1.),spot_brightness-r/0.1/curvature*200./brightness);
  231.         col.x = mix(col.x, 1, spot_brightness-r/0.1/curvature*200./brightness)
  232.         col.y = mix(col.y, 1, spot_brightness-r/0.1/curvature*200./brightness)
  233.         col.z = mix(col.z, 1, spot_brightness-r/0.1/curvature*200./brightness)
  234.        
  235.         fragColor = vec4(col.x, col.y, col.z, 1.0)
  236.     end
  237.  
  238.     mainImage()
  239.     return fragColor
  240. end
  241.  
  242. local function progressHook(prog)
  243.     term.clear()
  244.     term.setCursorPos(1, 1)
  245.     print("Generating color palette... " .. tostring(prog * 100) .. "%")
  246.     return 0.1
  247. end
  248.  
  249. local function main()
  250.     -- Create the color palette. This is what
  251.     -- will convert our RGB colors into pixels
  252.     -- which can be written to the screen.
  253.     local palette = createPalette(indexTable, 32, 32, 32, progressHook)
  254.  
  255.     term.clear()
  256.  
  257.     -- Create the shader program. This handles
  258.     -- all the shaders we will apply.
  259.     local program = createShaderProgram()
  260.  
  261.     -- Create the frame buffer. This is what we
  262.     -- apply the shaders to and can be quickly
  263.     -- blitted onto the screen.
  264.     local tw, th = term.getSize()
  265.     local frameBuffer = createFrameBuffer(palette, charTable, vec2(tw*2, th*3), STRATEGY_HEX_CELL)
  266.    
  267.     -- Create and add the shader.
  268.     local shader = createShader(shaderFunc)
  269.     addShader(program, shader)
  270.  
  271.     local start = clock()
  272.  
  273.     while true do
  274.         -- Set shader inputs.
  275.         setShaderInput(shader, 1, frameBuffer.size)
  276.         setShaderInput(shader, 2, clock() - start)
  277.  
  278.         -- Apply shader program. This draws our
  279.         -- shader on each pixel of the frame buffer.
  280.         applyShaderProgram(program, frameBuffer)
  281.  
  282.         -- Draw the frame buffer onto the screen.
  283.         drawFrameBuffer(frameBuffer, vec2(1, 1))
  284.  
  285.         -- Here I'm just doing a trick to make the
  286.         -- drawing run as fast as possible. You
  287.         -- limit the number of frames drawn if you
  288.         -- do this on a server.
  289.         os.queueEvent("")
  290.         local e = os.pullEvent()
  291.         if e == "key" then
  292.             term.clear()
  293.             term.setCursorPos(1, 1)
  294.             os.queueEvent("")
  295.             os.pullEvent()
  296.             return
  297.         end
  298.     end
  299. end
  300.  
  301. -- Generic trace code.
  302. local function errHandler(e)
  303.     term.setTextColor(colors.red)
  304.     local errors = { }
  305.     local ok, err, name
  306.     local i = 4
  307.     print(e)
  308.     while true do
  309.         ok, err = pcall(error, "", i)
  310.         if err:sub(1, 10) == "TRACE_ROOT" then
  311.             break
  312.         end
  313.         print(err)
  314.         i = i + 1
  315.     end
  316.     term.setTextColor(colors.white)
  317. end
  318.  
  319. local args = { ... }
  320. local function init(main, args)
  321.     main(unpack(args))
  322. end
  323. local bc = string.dump(init)
  324. xpcall(function() loadstring(bc, "TRACE_ROOT")(main, args) end, errHandler)
  325.  
  326. os.unloadAPI("gfx")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement