Skip to content

Instantly share code, notes, and snippets.

@owenbutler
Last active September 14, 2023 11:45
Show Gist options
  • Select an option

  • Save owenbutler/71f33c665115e84f0374e5d5d7204190 to your computer and use it in GitHub Desktop.

Select an option

Save owenbutler/71f33c665115e84f0374e5d5d7204190 to your computer and use it in GitHub Desktop.
DragonRuby Game Toolkit Cosine based Palette Test
$debug = false
$current_palette = 0
$palettes = [
[[0.5,0.5,0.5],[0.5,0.5,0.5],[1.0,1.0,1.0],[0,0.3333,0.6666]],
[[0.5,0.5,0.5],[0.666,0.666,0.666],[1.0,1.0,1.0],[0,0.3333,0.6666]],
[[0.5,0.5,0.5],[0.75,0.75,0.75],[1.0,1.0,1.0],[0,0.3333,0.6666]],
[[0.5,0.5,0.5],[1,1,1],[1.0,1.0,1.0],[0,0.3333,0.6666]],
[[1,0.5,0.5],[0.5,0.5,0.5],[0.75,1.0,0.6666],[0.8,1.0,0.3333]],
[[0.5,0.5,0.5],[0.5,0.5,0.5],[0.8,0.8,0.5],[0,0.2,0.5]],
[[0.6666,0.5,0.5],[0.5,0.6666,0.5],[0.6666,0.666,0.5],[0.2,0.0,0.5]],
[[0.5,0.5,0],[0.5,0.5,0],[0.5,0.5,0],[0.5,0.0,0]],
[[0.0,0.5,0.5],[0,0.5,0.5],[0.0,0.3333,0.5],[0.0,0.6666,0.5]],
[[0.5,0.5,0],[0.5,0.5,0],[0.1,0.5,0],[0.0,0.0,0]],
[[0.0,0.5,0.5],[0,0.5,0.5],[0.0,0.5,0.3333],[0.0,0.5,0.6666]],
[[0.5,0,0.5],[0.5,0,0.5],[0.5,0,0.5],[0,0,0.5]],
[[0.650,0.5,0.310],[-0.650,0.5,0.6],[0.333,0.278,0.278],[0.660,0.0,0.667]],
[[0.660,0.56,0.680],[0.718,0.438,0.720],[0.520,0.8,0.520],[-0.430,-0.397,-0.083]],
[[0.610,0.498,0.650],[0.388,0.498,0.350],[0.530,0.498,0.620],[3.438,3.012,4.025]],
[[0.731,1.098,0.192],[0.358,1.090,0.657],[1.077,0.360,0.328],[0.965,2.265,0.837]],
[[0.892,0.725,0.000],[0.878,0.278,0.725],[0.332,0.518,0.545],[2.440,5.043,0.732]],
[[0.821,0.328,0.242],[0.659,0.481,0.896],[0.612,0.340,0.296],[2.820,3.026,-0.273]],
[[0.938,0.328,0.718],[0.659,0.438,0.328],[0.388,0.388,0.296],[2.538,2.478,0.168]],
[[0.590,0.811,0.120],[0.410,0.392,0.590],[0.940,0.548,0.278],[-4.242,-6.611,-4.045]],
]
def tick args
args.outputs.background_color = [0, 0, 0]
if args.tick_count == 0
setup args
end
clear = false
if args.inputs.keyboard.key_down.p
reset args
clear = true
end
t = args.tick_count
c = colors(t * 2000, $palettes[$current_palette])
size = Math.sin(t / 43).remap(-1, 1, 30, 100)
args.outputs[:canvas].transient!
args.outputs[:canvas].clear_before_render = clear
args.outputs[:canvas].sprites << {
x: Math.sin(t / 127).remap(-1, 1, 0.0, 1180.0),
y: Math.cos(t / 179).remap(-1, 1, 100.0, 620.0),
w: size,
h: size,
**c,
path: 'sprites/circle.png',
}
args.outputs.sprites << {
x: 0, y: 0, w: 1280, h: 720, path: :canvas,
}
if $debug
args.outputs.primitives << args.gtk.current_framerate_primitives
args.outputs.labels << { r: 255, g: 255, b: 255, x: 640, y: 100, text: "#{args.gtk.current_framerate_render} fps render, #{args.gtk.current_framerate_calc} fps simulation", size_enum: 20, alignment_enum: 1, vertical_alignment_enum: 1 }
end
if args.inputs.keyboard.key_down.backspace
$debug = !$debug
end
end
def reset args
$current_palette += 1
$current_palette %= $palettes.length
end
def setup args
args.outputs[:canvas].w = 1280
args.outputs[:canvas].h = 720
args.outputs[:canvas].transient!
end
# Ruby implementation (without vectors) of the cosine based palette generator from here:
# https://iquilezles.org/articles/palettes/
# Example palettes (and create your own) here:
# http://dev.thi.ng/gradients/
def colors t
# [[0.500 0.500 0.500] [0.500 0.500 0.500] [1.000 1.000 1.000] [0.000 0.333 0.667]]
# [[0.892 0.725 0.000] [0.878 0.278 0.725] [0.332 0.518 0.545] [2.440 5.043 0.732]]
a1 = 0.892
a2 = 0.725
a3 = 0.000
b1 = 0.878
b2 = 0.278
b3 = 0.725
c1 = 0.332
c2 = 0.518
c3 = 0.545
d1 = 2.44
d2 = 5.043
d3 = 0.732
{
r: (a1 + b1 * (Math.cos(6.28318*(c1+t+d1)))) * 255,
g: (a2 + b2 * (Math.cos(6.28318*(c2+t+d2)))) * 255,
b: (a3 + b3 * (Math.cos(6.28318*(c3+t+d3)))) * 255,
}
end
def colors t, p
{
r: (p[0][0] + p[1][0] * (Math.cos(6.28318*(p[2][0]+t+p[3][0])))) * 255,
g: (p[0][1] + p[1][1] * (Math.cos(6.28318*(p[2][1]+t+p[3][1])))) * 255,
b: (p[0][2] + p[1][2] * (Math.cos(6.28318*(p[2][2]+t+p[3][2])))) * 255,
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment