Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("gfx")
- -- For convenience.
- local sin = math.sin
- local cos = math.cos
- local tan = math.tan
- local abs = math.abs
- local min = math.min
- local max = math.max
- local floor = math.floor
- local sqrt = math.sqrt
- local clock = os.clock
- -- I got lazy so I just localized all of
- -- these functions here.
- local STRATEGY_FULL_CELL = gfx.STRATEGY_FULL_CELL
- local STRATEGY_HEX_CELL = gfx.STRATEGY_HEX_CELL
- local STRATEGY_TWO_CELL = gfx.STRATEGY_TWO_CELL
- local vec2 = gfx.vec2
- local vec3 = gfx.vec3
- local vec4 = gfx.vec4
- local length2 = gfx.length2
- local length3 = gfx.length3
- local length4 = gfx.length4
- local normalize2 = gfx.normalize2
- local normalize3 = gfx.normalize3
- local normalize4 = gfx.normalize4
- local distance2 = gfx.distance2
- local distance3 = gfx.distance3
- local distance4 = gfx.distance4
- local dot2 = gfx.dot2
- local dot3 = gfx.dot3
- local dot4 = gfx.dot4
- local add2 = gfx.add2
- local add3 = gfx.add3
- local add4 = gfx.add4
- local sub2 = gfx.sub2
- local sub3 = gfx.sub3
- local sub4 = gfx.sub4
- local mul2 = gfx.mul2
- local mul3 = gfx.mul3
- local mul4 = gfx.mul4
- local div2 = gfx.div2
- local div3 = gfx.div3
- local div4 = gfx.div4
- local add2s = gfx.add2s
- local add3s = gfx.add3s
- local add4s = gfx.add4s
- local sub2s = gfx.sub2s
- local sub3s = gfx.sub3s
- local sub4s = gfx.sub4s
- local mul2s = gfx.mul2s
- local mul3s = gfx.mul3s
- local mul4s = gfx.mul4s
- local div2s = gfx.div2s
- local div3s = gfx.div3s
- local div4s = gfx.div4s
- local mat2mulvec2 = gfx.mat2mulvec2
- local mat3mulvec3 = gfx.mat3mulvec3
- local getRotMat2 = gfx.getRotMat2
- local getRotMat3x = gfx.getRotMat3x
- local getRotMat3y = gfx.getRotMat3y
- local getRotMat3z = gfx.getRotMat3z
- local fract = gfx.fract
- local mix = gfx.mix
- local clamp = gfx.clamp
- local smoothstep = gfx.smoothstep
- local lab2xyz = gfx.lab2xyz
- local xyz2rgb = gfx.xyz2rgb
- local lab2rgb = gfx.lab2rgb
- local rgb2lab = gfx.rgb2lab
- local colorDistance = gfx.colorDistance
- local createPalette = gfx.createPalette
- local paletteConvert = gfx.paletteConvert
- local createShaderProgram = gfx.createShaderProgram
- local addShader = gfx.addShader
- local applyShaderProgram = gfx.applyShaderProgram
- local createShader = gfx.createShader
- local setShaderInput = gfx.setShaderInput
- local createFrameBuffer = gfx.createFrameBuffer
- local drawFrameBuffer = gfx.drawFrameBuffer
- local getUnstretchedResolution = gfx.getUnstretchedResolution
- local clearFrameBuffer = gfx.clearFrameBuffer
- -- This table contains the set of pixels we
- -- draw to our frame buffer. In this case,
- -- we will just use all the colors with no
- -- foreground text.
- local charTable = {
- {" ", "0", "0"},
- {" ", "1", "0"},
- {" ", "2", "0"},
- {" ", "3", "0"},
- {" ", "4", "0"},
- {" ", "5", "0"},
- {" ", "6", "0"},
- {" ", "7", "0"},
- {" ", "8", "0"},
- {" ", "9", "0"},
- {" ", "a", "0"},
- {" ", "b", "0"},
- {" ", "c", "0"},
- {" ", "d", "0"},
- {" ", "e", "0"},
- {" ", "f", "0"}
- }
- -- This table contains the colors for each
- -- pixel in charTable. e.g., index 0 is used
- -- for colors.white.
- local indexTable = {
- vec3(0xF0/0xFF, 0xF0/0xFF, 0xF0/0xFF),
- vec3(0xF2/0xFF, 0xB2/0xFF, 0x33/0xFF),
- vec3(0xE5/0xFF, 0x7F/0xFF, 0xD8/0xFF),
- vec3(0x99/0xFF, 0xB2/0xFF, 0xF2/0xFF),
- vec3(0xDE/0xFF, 0xDE/0xFF, 0x6C/0xFF),
- vec3(0x7F/0xFF, 0xCC/0xFF, 0x19/0xFF),
- vec3(0xF2/0xFF, 0xB2/0xFF, 0xCC/0xFF),
- vec3(0x4C/0xFF, 0x4C/0xFF, 0x4C/0xFF),
- vec3(0x99/0xFF, 0x99/0xFF, 0x99/0xFF),
- vec3(0x4C/0xFF, 0x99/0xFF, 0xB2/0xFF),
- vec3(0xB2/0xFF, 0x66/0xFF, 0xE5/0xFF),
- vec3(0x33/0xFF, 0x66/0xFF, 0xCC/0xFF),
- vec3(0x7F/0xFF, 0x66/0xFF, 0x4C/0xFF),
- vec3(0x57/0xFF, 0xA6/0xFF, 0x4E/0xFF),
- vec3(0xCC/0xFF, 0x4C/0xFF, 0x4C/0xFF),
- vec3(0x19/0xFF, 0x19/0xFF, 0x19/0xFF)
- }
- -- Note that the above two tables must
- -- correspond by index.
- local function shaderFunc(oldColor, fragCoord, inputs)
- --[[ Colors! ]]--
- --[[local res = inputs[1]
- local t = inputs[2]
- local uv = div2(fragCoord, res)
- return vec4(uv.x, uv.y, 0.5 + 0.5 * math.sin(t), 1)]]
- --[[ Rotating pattern ]]--
- --[[local res = inputs[1]
- local t = inputs[2]
- local uv = div2(fragCoord, res)
- uv.x = uv.x * res.x/res.y--*2/3
- uv.x = uv.x - res.x/res.y/2--*2/3/2
- uv.y = uv.y - 0.5
- local m = getRotMat2(t)
- local uv = mat2mulvec2(m, sub2(uv, vec2(0, 0)))
- return vec4((uv.x % 0.4)*5*(sin(t)+1)/2, (uv.y % 0.2)*5*(cos(t)+1)/2, 0, 1)]]
- --[[ Green vortex ]]--
- local brightness = 3.
- local ray_brightness = 5.
- local gamma = 6.
- local spot_brightness = 1.5
- local ray_density = 6.
- local curvature = 90.
- local red = 1.8
- local green = 3.
- local blue = .5
- local noisetype = 1
- local sin_freq = 6.
- local iResolution = inputs[1]
- local iGlobalTime = inputs[2]
- local fragColor
- local function hash(n) return fract(sin(n)*43758.5453) end
- local function noise(x)
- x = vec2(x.x, x.y)
- x = mul2s(x, 1.75)
- local p = vec2(floor(x.x), floor(x.y))
- local f = vec2(fract(x.x), fract(x.y))
- f = mul2(mul2(f, f), (sub2(vec2(3, 3), mul2s(f, 2))))
- local n = p.x + p.y*57.0
- local res = mix(mix( hash(n+ 0.0), hash(n+ 1.0),f.x),
- mix( hash(n+ 57.0), hash(n+ 58.0),f.x),f.y)
- return res
- end
- local m2 = { 0.80, 0.60, -0.60, 0.80 };
- local function fbm(p)
- p = vec2(p.x, p.y)
- local z=2.
- local rz = 0.
- p = mul2s(p, 0.25)
- for i = 1, 5 do
- if noisetype == 1 then
- rz = rz + abs((noise(p)-0.5)*2.)/z
- elseif noisetype == 2 then
- rz = rz + (sin(noise(p)*sin_freq)*0.5+0.5) /z
- else
- rz = rz + noise(p)/z
- end
- z = z*2.
- p = mat2mulvec2(m2, mul2s(p, 2))
- end
- return rz
- end
- local function mainImage()
- local t = -iGlobalTime*0.03*4
- uv = sub2s(div2(fragCoord, iResolution), 0.5)
- uv.x = uv.x * iResolution.x/iResolution.y
- uv = add2s(mul2s(mul2s(uv, curvature), .05), 0.0001)
- local r = sqrt(dot2(uv, uv))
- local x = dot2(normalize2(uv), vec2(.5,0.)) + t
- local y = dot2(normalize2(uv), vec2(.0,.5)) + t
- --x = fbm(vec2(y*ray_density*0.5,r+x*ray_density*.2));
- --y = fbm(vec2(r+y*ray_density*0.1,x*ray_density*.5));
- local val
- val = fbm(vec2(r+y*ray_density,r+x*ray_density-y))
- val = smoothstep(gamma*.02-.1,ray_brightness+(gamma*0.02-.1)+.001,val)
- val = sqrt(val)
- local col = div3(vec3(val, val, val), vec3(red,green,blue))
- --col = clamp(sub3(vec3(1, 1, 1), col),0,1)
- col = vec3(
- clamp(1-col.x, 0, 1),
- clamp(1-col.y, 0, 1),
- clamp(1-col.z, 0, 1))
- --col = mix(col,vec3(1.),spot_brightness-r/0.1/curvature*200./brightness);
- col.x = mix(col.x, 1, spot_brightness-r/0.1/curvature*200./brightness)
- col.y = mix(col.y, 1, spot_brightness-r/0.1/curvature*200./brightness)
- col.z = mix(col.z, 1, spot_brightness-r/0.1/curvature*200./brightness)
- fragColor = vec4(col.x, col.y, col.z, 1.0)
- end
- mainImage()
- return fragColor
- end
- local function progressHook(prog)
- term.clear()
- term.setCursorPos(1, 1)
- print("Generating color palette... " .. tostring(prog * 100) .. "%")
- return 0.1
- end
- local function main()
- -- Create the color palette. This is what
- -- will convert our RGB colors into pixels
- -- which can be written to the screen.
- local palette = createPalette(indexTable, 32, 32, 32, progressHook)
- term.clear()
- -- Create the shader program. This handles
- -- all the shaders we will apply.
- local program = createShaderProgram()
- -- Create the frame buffer. This is what we
- -- apply the shaders to and can be quickly
- -- blitted onto the screen.
- local tw, th = term.getSize()
- local frameBuffer = createFrameBuffer(palette, charTable, vec2(tw*2, th*3), STRATEGY_HEX_CELL)
- -- Create and add the shader.
- local shader = createShader(shaderFunc)
- addShader(program, shader)
- local start = clock()
- while true do
- -- Set shader inputs.
- setShaderInput(shader, 1, frameBuffer.size)
- setShaderInput(shader, 2, clock() - start)
- -- Apply shader program. This draws our
- -- shader on each pixel of the frame buffer.
- applyShaderProgram(program, frameBuffer)
- -- Draw the frame buffer onto the screen.
- drawFrameBuffer(frameBuffer, vec2(1, 1))
- -- Here I'm just doing a trick to make the
- -- drawing run as fast as possible. You
- -- limit the number of frames drawn if you
- -- do this on a server.
- os.queueEvent("")
- local e = os.pullEvent()
- if e == "key" then
- term.clear()
- term.setCursorPos(1, 1)
- os.queueEvent("")
- os.pullEvent()
- return
- end
- end
- end
- -- Generic trace code.
- local function errHandler(e)
- term.setTextColor(colors.red)
- local errors = { }
- local ok, err, name
- local i = 4
- print(e)
- while true do
- ok, err = pcall(error, "", i)
- if err:sub(1, 10) == "TRACE_ROOT" then
- break
- end
- print(err)
- i = i + 1
- end
- term.setTextColor(colors.white)
- end
- local args = { ... }
- local function init(main, args)
- main(unpack(args))
- end
- local bc = string.dump(init)
- xpcall(function() loadstring(bc, "TRACE_ROOT")(main, args) end, errHandler)
- os.unloadAPI("gfx")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement