Last active
September 4, 2019 18:10
-
-
Save Francesco149/6f9bb656d5e3974bb77805a45fbe0ddb to your computer and use it in GitHub Desktop.
vlang playground raymarcher in <1000 characters. paste this in https://vlang.io/play and run, it renders a shaded sphere. relies on an early version of vlang and quirks with autoformat, might break pretty soon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const(W=25 H=11 E=0.001 F=100.0 C=['-','.',' ']) | |
fn q(n f64)f64{mut x:=n mut y:=1.0 for x-y>E{x=(x+y)/2 y=n/x}return x} | |
struct V{mut: x f64 y f64 z f64} | |
fn (v V)a(o V)V{return V{x:v.x+o.x,y:v.y+o.y,z:v.z+o.z}} | |
fn (v V)m(s f64)V{return V{x:v.x*s,y:v.y*s,z:v.z*s}} | |
fn (v V)g()f64{return q(v.x*v.x+v.y*v.y+v.z*v.z)} | |
fn (v V)n()V{m:=v.g() if m<E{return v} return V{x:v.x/m,y:v.y/m,z:v.z/m}} | |
fn s(p V)f64{d:=p.g()-0.1 if d<-p.y+0.2{return d}return -p.y+0.2} | |
fn h(e,r V)f64{mut t:=0.0 for i:= 0;i<99;i++{d:=s(e.a(r.m(t))) if d<E{return t} t+=d if t>=F{break}}return -1} | |
fn f(u,v f64)string{e:=V{x:0,y:0,z:-1} r:=V{x:u,y:v,z:1} d:=h(e,r.n()) | |
if d<0{return '\x23'}p:=e.a(r.n().m(d)) | |
mut a:=p a.x+=E | |
mut b:=p b.y+=E | |
mut c:=p c.z+=E | |
j:=s(p) k:=V{x:s(a)-j,y:s(b)-j,z:s(c)-j}l:=k.n() | |
mut y:=V{x:3,y:-7,z:-3} y=y.n() | |
t:=(l.x*y.x+l.y*y.y+l.z*y.z)*6000 | |
if t>2{return ' '} | |
if t<0{return '-'} | |
return C[int(t)] | |
} | |
fn main(){for v:=0;v<H;v++{for u:=0;u<W;u++{p:=f(f64(u)/W-0.5,f64(v)/H-0.5) print('$p')}println('')}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Still works!