Skip to content

Instantly share code, notes, and snippets.

@coderobe
Created January 26, 2024 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coderobe/3164a4e6d99da89cc8e0a8bc2d67d788 to your computer and use it in GitHub Desktop.
Save coderobe/3164a4e6d99da89cc8e0a8bc2d67d788 to your computer and use it in GitHub Desktop.
software rendered "shadertoy" lol
#!/usr/bin/env bash
# This program was written by Mara Broda (coderobe) in 2020
printf "\033c"
trap "tput cnorm" EXIT
trap "exec bash "$(readlink -f "$0")"" SIGWINCH
tput civis
echo "starting bc coproc"
coproc bc { bc -l; }
echo "setting up in fd"
exec 3>&"${bc[1]}"
echo "setting up out fd"
exec 4<&"${bc[0]}"
cat >&3 <<-EOF
scale=6
pi=3.1415926535897932;
e=2.7182818284590452;
define floor(x) { auto s; s=scale; scale=0; x=x/1; scale=s; return x }
define ceil(x) { return floor(x-.01)+1 }
define sin(x) {
if (x == pi/2)
return 1
return s(x)
}
define cos(x) {
if (x == pi/2)
return 0
return c(x)
}
define tan(x) {
if (x == pi/4)
return 1
if (x == -pi/4)
return -1
return s(x)/c(x)
}
define abs(x) {
if(x<0)
return -x
return x
}
# integer modulo
define imod(a, b) { auto s; s=scale; scale=0; x=a%b; scale=s; return x }
# integrated smoothstep
define tween(time, minv, maxv) {
return minv+abs(imod(time, (maxv*2))-maxv)
}
# mix palette by precedence(b>a), -1 is transparent sentinel value
define mix(a, b) {
if(b == -1)
return a
return b
}
# generate shapes
define genb(s, x, y, b) {
if(imod(ceil(s+x)+ceil(s+y), 2) == 0)
return b
return -1
}
define gend(s, x, y, b) {
if(imod(ceil(s*2+x+y), 2) == 0)
return b
return -1
}
define gens(s, x, y, b) {
if(imod(ceil(s+(sin(y)+cos(x))), 2) == 0)
return b
return -1
}
out = -1
# "fragment shader" for the background
define bg() {
out = -1
zoom = 1.2
# premultiplied normalized coords
stx = px*zoom / prx
sty = py*zoom / pry
# animation progress speed (integrated)
#s = .3*t
out = genb(s, stx*2, sty*2, 6)
if(out != -1) return imod(floor(out), 8) # we don't need to calculate anything underneath
out = gens(s, -stx*3, tween(t, 0, 10)-sty*3, 3)
if(out != -1) return imod(floor(out), 8) # we don't need to calculate anything underneath
out = gend(s, -stx*5, -sty*5, 1)
if(out != -1) return imod(floor(out), 8) # we don't need to calculate anything underneath
out = genb(s, sin(t)+stx*2, cos(t)+sty*2, 7)
if(out == -1) return 0 # never return -1
return imod(floor(out), 8)
}
# "fragment shader" for the foreground
# define fg() {
# out += 2
#
# return imod(floor(out), 8)
# }
EOF
rx=$(($(tput cols)-1))
ry=$(($(tput lines)-3))
char=" "
echo "prx=$rx; pry=$ry" >&3
# this was used to generate the declare below
# NOTE: unused
cat >/dev/null <<EOF
art=File.readlines("art").map(&:chomp)
rx=art.first.length
ry=art.length
puts "declare -A texture; texture=("+
art.map{|e| e.split ""}
.each_with_index
.map{|row,y| row.each_with_index.map{|col,x| [col, x, y]}}
.flatten(1)
.group_by(&:first)
.reject{|k,v| k==" "}
.map{|group| [group[0], group[1].map{|l| l.drop(1)
.each_with_index
.map{|v,i| (v+1).to_s}
.reduce{|x, y| "#{x},#{y}"}
}]}
.to_h
.map{|k,v| v.map{|v2| [v2, k]}}
.flatten(1)
.map{|k,v| "['#{k}']='#{v}'"}
.join(" ") + ")"
EOF
#declare -A texture; texture=(['1,1']='~' ['2,1']='c' ['3,1']='o' ['7,1']='o' ['4,1']='d' ['5,1']='e' ['9,1']='e' ['6,1']='r' ['8,1']='b' ['83,1']='!' ['82,2']='!' ['83,2']='!' ['84,2']='!' ['90,2']='!' ['81,3']='!' ['82,3']='!' ['83,3']='!' ['89,3']='!' ['90,3']='!' ['91,3']='!' ['73,4']='!' ['74,4']='!' ['75,4']='!' ['80,4']='!' ['81,4']='!' ['82,4']='!' ['88,4']='!' ['89,4']='!' ['90,4']='!' ['72,5']='!' ['73,5']='!' ['74,5']='!' ['75,5']='!' ['76,5']='!' ['79,5']='!' ['80,5']='!' ['81,5']='!' ['87,5']='!' ['88,5']='!' ['89,5']='!' ['72,6']='!' ['73,6']='!' ['74,6']='!' ['75,6']='!' ['78,6']='!' ['79,6']='!' ['80,6']='!' ['86,6']='!' ['87,6']='!' ['88,6']='!' ['71,7']='!' ['72,7']='!' ['73,7']='!' ['77,7']='!' ['78,7']='!' ['79,7']='!' ['84,7']='!' ['85,7']='!' ['86,7']='!' ['87,7']='!' ['71,8']='!' ['72,8']='!' ['73,8']='!' ['74,8']='!' ['78,8']='!' ['83,8']='!' ['84,8']='!' ['85,8']='!' ['86,8']='!' ['93,8']='!' ['72,9']='!' ['73,9']='!' ['74,9']='!' ['75,9']='!' ['84,9']='!' ['85,9']='!' ['91,9']='!' ['92,9']='!' ['93,9']='!' ['94,9']='!' ['73,10']='!' ['74,10']='!' ['75,10']='!' ['76,10']='!' ['77,10']='!' ['78,10']='!' ['88,10']='!' ['89,10']='!' ['90,10']='!' ['91,10']='!' ['92,10']='!' ['93,10']='!' ['74,11']='!' ['75,11']='!' ['76,11']='!' ['77,11']='!' ['78,11']='!' ['79,11']='!' ['80,11']='!' ['81,11']='!' ['82,11']='!' ['83,11']='!' ['84,11']='!' ['85,11']='!' ['86,11']='!' ['87,11']='!' ['88,11']='!' ['89,11']='!' ['90,11']='!' ['91,11']='!' ['92,11']='!' ['76,12']='!' ['77,12']='!' ['78,12']='!' ['79,12']='!' ['80,12']='!' ['81,12']='!' ['82,12']='!' ['83,12']='!' ['84,12']='!' ['85,12']='!' ['86,12']='!' ['87,12']='!' ['88,12']='!' ['89,12']='!' ['90,12']='!' ['79,13']='!' ['80,13']='!' ['81,13']='!' ['82,13']='!' ['83,13']='!' ['84,13']='!' ['85,13']='!' ['86,13']='!' ['87,13']='!' ['16,3']='3' ['17,3']='3' ['18,3']='3' ['19,3']='3' ['20,3']='3' ['21,3']='3' ['12,4']='3' ['13,4']='3' ['14,4']='3' ['15,4']='3' ['16,4']='3' ['17,4']='3' ['18,4']='3' ['19,4']='3' ['20,4']='3' ['21,4']='3' ['22,4']='3' ['23,4']='3' ['24,4']='3' ['25,4']='3' ['49,4']='3' ['10,5']='3' ['11,5']='3' ['12,5']='3' ['13,5']='3' ['14,5']='3' ['15,5']='3' ['16,5']='3' ['17,5']='3' ['18,5']='3' ['19,5']='3' ['20,5']='3' ['21,5']='3' ['22,5']='3' ['23,5']='3' ['24,5']='3' ['25,5']='3' ['26,5']='3' ['48,5']='3' ['49,5']='3' ['50,5']='3' ['8,6']='3' ['9,6']='3' ['10,6']='3' ['11,6']='3' ['12,6']='3' ['13,6']='3' ['14,6']='3' ['15,6']='3' ['22,6']='3' ['23,6']='3' ['24,6']='3' ['25,6']='3' ['48,6']='3' ['49,6']='3' ['50,6']='3' ['7,7']='3' ['8,7']='3' ['9,7']='3' ['10,7']='3' ['11,7']='3' ['47,7']='3' ['48,7']='3' ['49,7']='3' ['5,8']='3' ['6,8']='3' ['7,8']='3' ['8,8']='3' ['9,8']='3' ['47,8']='3' ['48,8']='3' ['49,8']='3' ['4,9']='3' ['5,9']='3' ['6,9']='3' ['7,9']='3' ['47,9']='3' ['48,9']='3' ['49,9']='3' ['4,10']='3' ['5,10']='3' ['6,10']='3' ['47,10']='3' ['48,10']='3' ['49,10']='3' ['4,11']='3' ['5,11']='3' ['6,11']='3' ['46,11']='3' ['47,11']='3' ['48,11']='3' ['3,12']='3' ['4,12']='3' ['5,12']='3' ['22,12']='3' ['45,12']='3' ['46,12']='3' ['47,12']='3' ['48,12']='3' ['3,13']='3' ['4,13']='3' ['5,13']='3' ['21,13']='3' ['22,13']='3' ['23,13']='3' ['43,13']='3' ['44,13']='3' ['45,13']='3' ['46,13']='3' ['47,13']='3' ['48,13']='3' ['4,14']='3' ['5,14']='3' ['6,14']='3' ['22,14']='3' ['23,14']='3' ['24,14']='3' ['41,14']='3' ['42,14']='3' ['43,14']='3' ['44,14']='3' ['45,14']='3' ['46,14']='3' ['47,14']='3' ['48,14']='3' ['4,15']='3' ['5,15']='3' ['6,15']='3' ['7,15']='3' ['23,15']='3' ['24,15']='3' ['25,15']='3' ['26,15']='3' ['40,15']='3' ['41,15']='3' ['42,15']='3' ['43,15']='3' ['44,15']='3' ['45,15']='3' ['46,15']='3' ['47,15']='3' ['51,15']='3' ['52,15']='3' ['53,15']='3' ['5,16']='3' ['6,16']='3' ['7,16']='3' ['8,16']='3' ['24,16']='3' ['25,16']='3' ['26,16']='3' ['27,16']='3' ['38,16']='3' ['39,16']='3' ['40,16']='3' ['41,16']='3' ['42,16']='3' ['45,16']='3' ['46,16']='3' ['47,16']='3' ['48,16']='3' ['49,16']='3' ['50,16']='3' ['51,16']='3' ['52,16']='3' ['53,16']='3' ['54,16']='3' ['55,16']='3' ['68,16']='3' ['69,16']='3' ['70,16']='3' ['6,17']='3' ['7,17']='3' ['8,17']='3' ['9,17']='3' ['10,17']='3' ['11,17']='3' ['12,17']='3' ['25,17']='3' ['26,17']='3' ['27,17']='3' ['28,17']='3' ['29,17']='3' ['36,17']='3' ['37,17']='3' ['38,17']='3' ['39,17']='3' ['40,17']='3' ['41,17']='3' ['45,17']='3' ['46,17']='3' ['47,17']='3' ['48,17']='3' ['49,17']='3' ['50,17']='3' ['51,17']='3' ['52,17']='3' ['53,17']='3' ['54,17']='3' ['55,17']='3' ['56,17']='3' ['65,17']='3' ['66,17']='3' ['67,17']='3' ['68,17']='3' ['69,17']='3' ['70,17']='3' ['71,17']='3' ['72,17']='3' ['73,17']='3' ['85,17']='3' ['7,18']='3' ['8,18']='3' ['9,18']='3' ['10,18']='3' ['11,18']='3' ['12,18']='3' ['13,18']='3' ['14,18']='3' ['15,18']='3' ['16,18']='3' ['17,18']='3' ['18,18']='3' ['19,18']='3' ['27,18']='3' ['28,18']='3' ['29,18']='3' ['30,18']='3' ['35,18']='3' ['36,18']='3' ['37,18']='3' ['38,18']='3' ['39,18']='3' ['45,18']='3' ['46,18']='3' ['47,18']='3' ['48,18']='3' ['49,18']='3' ['50,18']='3' ['54,18']='3' ['55,18']='3' ['56,18']='3' ['57,18']='3' ['64,18']='3' ['65,18']='3' ['66,18']='3' ['67,18']='3' ['68,18']='3' ['69,18']='3' ['70,18']='3' ['71,18']='3' ['72,18']='3' ['73,18']='3' ['74,18']='3' ['75,18']='3' ['84,18']='3' ['85,18']='3' ['86,18']='3' ['9,19']='3' ['10,19']='3' ['11,19']='3' ['12,19']='3' ['13,19']='3' ['14,19']='3' ['15,19']='3' ['16,19']='3' ['17,19']='3' ['18,19']='3' ['19,19']='3' ['20,19']='3' ['21,19']='3' ['22,19']='3' ['28,19']='3' ['29,19']='3' ['30,19']='3' ['31,19']='3' ['32,19']='3' ['33,19']='3' ['34,19']='3' ['35,19']='3' ['36,19']='3' ['37,19']='3' ['44,19']='3' ['45,19']='3' ['46,19']='3' ['47,19']='3' ['55,19']='3' ['56,19']='3' ['57,19']='3' ['63,19']='3' ['64,19']='3' ['65,19']='3' ['66,19']='3' ['67,19']='3' ['71,19']='3' ['72,19']='3' ['73,19']='3' ['74,19']='3' ['75,19']='3' ['76,19']='3' ['84,19']='3' ['85,19']='3' ['86,19']='3' ['13,20']='3' ['14,20']='3' ['15,20']='3' ['16,20']='3' ['17,20']='3' ['18,20']='3' ['19,20']='3' ['20,20']='3' ['21,20']='3' ['22,20']='3' ['23,20']='3' ['30,20']='3' ['31,20']='3' ['32,20']='3' ['33,20']='3' ['34,20']='3' ['35,20']='3' ['44,20']='3' ['45,20']='3' ['46,20']='3' ['54,20']='3' ['55,20']='3' ['56,20']='3' ['62,20']='3' ['63,20']='3' ['64,20']='3' ['74,20']='3' ['75,20']='3' ['76,20']='3' ['84,20']='3' ['85,20']='3' ['86,20']='3' ['20,21']='3' ['21,21']='3' ['22,21']='3' ['31,21']='3' ['32,21']='3' ['33,21']='3' ['34,21']='3' ['44,21']='3' ['45,21']='3' ['46,21']='3' ['53,21']='3' ['54,21']='3' ['55,21']='3' ['56,21']='3' ['61,21']='3' ['62,21']='3' ['63,21']='3' ['64,21']='3' ['73,21']='3' ['74,21']='3' ['75,21']='3' ['83,21']='3' ['84,21']='3' ['85,21']='3' ['89,21']='3' ['90,21']='3' ['91,21']='3' ['92,21']='3' ['93,21']='3' ['94,21']='3' ['95,21']='3' ['96,21']='3' ['30,22']='3' ['31,22']='3' ['32,22']='3' ['33,22']='3' ['44,22']='3' ['45,22']='3' ['46,22']='3' ['52,22']='3' ['53,22']='3' ['54,22']='3' ['55,22']='3' ['61,22']='3' ['62,22']='3' ['63,22']='3' ['64,22']='3' ['65,22']='3' ['66,22']='3' ['67,22']='3' ['68,22']='3' ['69,22']='3' ['70,22']='3' ['71,22']='3' ['72,22']='3' ['73,22']='3' ['74,22']='3' ['75,22']='3' ['83,22']='3' ['84,22']='3' ['85,22']='3' ['86,22']='3' ['87,22']='3' ['88,22']='3' ['89,22']='3' ['90,22']='3' ['91,22']='3' ['92,22']='3' ['93,22']='3' ['94,22']='3' ['95,22']='3' ['96,22']='3' ['97,22']='3' ['29,23']='3' ['30,23']='3' ['31,23']='3' ['32,23']='3' ['43,23']='3' ['44,23']='3' ['45,23']='3' ['50,23']='3' ['51,23']='3' ['52,23']='3' ['53,23']='3' ['54,23']='3' ['60,23']='3' ['61,23']='3' ['62,23']='3' ['63,23']='3' ['64,23']='3' ['65,23']='3' ['66,23']='3' ['67,23']='3' ['68,23']='3' ['69,23']='3' ['70,23']='3' ['71,23']='3' ['72,23']='3' ['73,23']='3' ['74,23']='3' ['75,23']='3' ['83,23']='3' ['84,23']='3' ['85,23']='3' ['86,23']='3' ['87,23']='3' ['88,23']='3' ['89,23']='3' ['90,23']='3' ['91,23']='3' ['92,23']='3' ['93,23']='3' ['94,23']='3' ['95,23']='3' ['96,23']='3' ['27,24']='3' ['28,24']='3' ['29,24']='3' ['30,24']='3' ['42,24']='3' ['43,24']='3' ['44,24']='3' ['45,24']='3' ['49,24']='3' ['50,24']='3' ['51,24']='3' ['52,24']='3' ['53,24']='3' ['59,24']='3' ['60,24']='3' ['61,24']='3' ['62,24']='3' ['63,24']='3' ['64,24']='3' ['65,24']='3' ['66,24']='3' ['67,24']='3' ['68,24']='3' ['69,24']='3' ['70,24']='3' ['71,24']='3' ['72,24']='3' ['73,24']='3' ['74,24']='3' ['82,24']='3' ['83,24']='3' ['84,24']='3' ['85,24']='3' ['86,24']='3' ['87,24']='3' ['88,24']='3' ['26,25']='3' ['27,25']='3' ['28,25']='3' ['29,25']='3' ['42,25']='3' ['43,25']='3' ['44,25']='3' ['47,25']='3' ['48,25']='3' ['49,25']='3' ['50,25']='3' ['51,25']='3' ['60,25']='3' ['61,25']='3' ['62,25']='3' ['63,25']='3' ['64,25']='3' ['65,25']='3' ['66,25']='3' ['67,25']='3' ['81,25']='3' ['82,25']='3' ['83,25']='3' ['84,25']='3' ['25,26']='3' ['26,26']='3' ['27,26']='3' ['28,26']='3' ['41,26']='3' ['42,26']='3' ['43,26']='3' ['44,26']='3' ['45,26']='3' ['46,26']='3' ['47,26']='3' ['48,26']='3' ['49,26']='3' ['62,26']='3' ['63,26']='3' ['64,26']='3' ['65,26']='3' ['66,26']='3' ['67,26']='3' ['68,26']='3' ['69,26']='3' ['70,26']='3' ['71,26']='3' ['76,26']='3' ['77,26']='3' ['81,26']='3' ['82,26']='3' ['83,26']='3' ['84,26']='3' ['24,27']='3' ['25,27']='3' ['26,27']='3' ['27,27']='3' ['42,27']='3' ['43,27']='3' ['44,27']='3' ['45,27']='3' ['46,27']='3' ['47,27']='3' ['48,27']='3' ['65,27']='3' ['66,27']='3' ['67,27']='3' ['68,27']='3' ['69,27']='3' ['70,27']='3' ['71,27']='3' ['72,27']='3' ['73,27']='3' ['74,27']='3' ['75,27']='3' ['76,27']='3' ['77,27']='3' ['78,27']='3' ['81,27']='3' ['82,27']='3' ['83,27']='3' ['23,28']='3' ['24,28']='3' ['25,28']='3' ['26,28']='3' ['43,28']='3' ['44,28']='3' ['45,28']='3' ['46,28']='3' ['47,28']='3' ['68,28']='3' ['69,28']='3' ['70,28']='3' ['71,28']='3' ['72,28']='3' ['73,28']='3' ['74,28']='3' ['75,28']='3' ['76,28']='3' ['77,28']='3' ['80,28']='3' ['81,28']='3' ['82,28']='3' ['83,28']='3' ['22,29']='3' ['23,29']='3' ['24,29']='3' ['72,29']='3' ['73,29']='3' ['74,29']='3' ['75,29']='3' ['80,29']='3' ['81,29']='3' ['82,29']='3' ['23,30']='3' ['81,30']='3')
declare -A screen
while true
do
sync=$(date +%s%3N)
echo -n "t=$sync/800;s=.3*t;" >&3
echo -ne "\e[1H"
for ((y=1; y<=ry; y++))
do echo -n "py=$y;" >&3
for ((x=1; x<=rx; x++))
do \
char="${texture[$x,$y]}"
echo "px=$x;bg();" >&3
read -r bg <&4
#if [[ $char == "" ]]
# then echo "fg();" >&3
# read -r fg <&4
#fi
#[[ $char == "" ]] && char=" "
screen[$x,$y]="\e[1;4${bg};3${fg}m "
done
#echo
done
for ((y=1; y<=ry; y++))
do for ((x=1; x<=rx; x++))
do echo -ne "${screen[$x,$y]}"
done
echo
done
echo -e "\e[0m\e[K\e[1mlast vblank took" $(($(date +%s%3N)-sync)) "ms"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment