Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- p={}
- depth={}
- pos={}
- groundy=-6
- function getindex(x,y)
- x=flr(x)
- y=flr(y)
- if (x%128!=x or y%128!=y) return nil
- return x*128+y
- end
- function rnddir(ground)
- local a=rnd()
- local y=rnd(2)-1
- local r=sqrt(1-y*y)
- local x=cos(a)*r
- local z=sin(a)*r
- if ground then
- y=abs(y)
- else
- z=-abs(z)
- end
- return x,y,z
- end
- function worldtoscr(x,y,z)
- local persp=60/z
- local u=64+x*persp
- local v=64-y*persp
- return u,v,persp
- end
- function scrtoworld(u,v)
- local y=groundy
- local persp=(64-v)/y
- local x=(u-64)/persp
- local z=60/persp
- return x,y,z,persp
- end
- dirsx={1,-1,0,0}
- dirsy={0,0,1,-1}
- function outline(u,v)
- local index=getindex(u,v)
- local dist=depth[index]
- for i=1,#dirsx do
- local u2=u+dirsx[i]
- local v2=v+dirsy[i]
- local index2=getindex(u2,v2)
- if index2 then
- local dist2=depth[index2]
- if dist2 then
- if dist2>dist*1.1+sqrt(dist)*.5 then
- return true
- end
- else
- return true
- end
- end
- end
- return false
- end
- function ssao(point,dist)
- local x=point[1]
- local y=point[2]
- local z=point[3]
- local ground = (y==groundy)
- local hits=0
- local totalweight=0
- local raycount=60
- local raysamples=4
- local _,__,persp=worldtoscr(x,y,z)
- local raylen=4/persp
- for i=1,raycount do
- local dx,dy,dz=rnddir(ground)
- local rayhits=0
- local weight=1
- for j=1,raysamples do
- local d=j/raysamples*raylen
- local u,v=worldtoscr(x+dx*d,y+dy*d,z+dz*d)
- local index=getindex(u,v)
- if index then
- local otherdepth=depth[index]
- if otherdepth and otherdepth<dist then
- rayhits+=1/raysamples
- end
- else
- if rayhits==0 then
- break
- end
- end
- end
- hits+=rayhits*rayhits*weight
- totalweight+=weight
- end
- return 1-(hits/totalweight)^2
- end
- function drawpoint(x,y,z,u,v)
- if not u then
- u,v=worldtoscr(x,y,z)
- end
- index=getindex(u,v)
- if index then
- dist=z
- if not depth[index] or dist<depth[index] then
- col=sqrt(dist/50)*15
- pset(u,v,col)
- depth[index]=dist
- if pos[index] then
- pos[index][1]=x
- pos[index][2]=y
- pos[index][3]=z
- else
- pos[index]={x,y,z}
- end
- end
- end
- end
- function drawleaf(lx,ly,lz,count,jit)
- for j=1,count do
- x=lx+rnd(jit)-jit/2
- y=ly+rnd(jit)-jit/2
- z=lz+rnd(jit)-jit/2
- drawpoint(x,y,z)
- end
- end
- function maketree(tx,tz)
- local height=rnd(6)+6
- for i=0,100 do
- t=i/100
- l=3*(1-t^2)
- a=i*.618+.1
- dx=cos(a)
- dz=sin(a)
- for j=0,l,.1 do
- t2=j/l
- x=dx*j+tx
- y=groundy+t*height-j/2
- z=dz*j+tz
- drawleaf(x,y,z+8,8,.3)
- end
- end
- end
- function makeplant(x,z)
- local h=rnd(2)
- for i=0,h,.1 do
- for j=1,1 do
- drawpoint(x+rnd(.2)-.1,
- groundy+i,
- z+rnd(.2)-.1)
- end
- end
- end
- function makemountain(x,z,h)
- local u,v,persp=worldtoscr(x,groundy,z)
- local wheight=h/persp
- local y=groundy+wheight
- local u2,v2=worldtoscr(x,y,z)
- local sheight=v-v2
- for i=0,sheight,.2 do
- local t=i/sheight
- local radius=t*wheight
- for a=0,.5,.1/radius do
- local dx=cos(a)*radius
- local dz=sin(a)*radius
- local wave=sin(a*12+t*4)*2/persp
- drawpoint(x+dx,y-wheight*t+wave-rnd(6)/persp,z+dz)
- end
- end
- end
- function makecloud(x,y,z,screenw)
- local u,v,persp=worldtoscr(x,y,z)
- local w=screenw/persp
- for i=1,80 do
- drawleaf(x+rnd(w)-w/2,
- y+rnd(w/8)-w/16,
- z+rnd(w/8)-w/16,
- 80,
- w/20)
- end
- end
- cls(13)
- for u=0,127 do
- for v=65,127 do
- index=getindex(u,v)
- x,y,z=scrtoworld(u,v)
- drawpoint(x,y,z,u,v)
- end
- end
- srand(3)
- nextseed=rnd(-1)
- for i=1,8 do
- makemountain(rnd(400)-200,
- 200+i*30,
- 15+rnd(30))
- srand(nextseed)
- nextseed=rnd(-1)
- end
- srand(0)
- nextseed=rnd(-1)
- for i=1,20 do
- makecloud(rnd(1000)-500,
- 350+rnd(100),
- 500+rnd(200),
- rnd(60)+30)
- srand(nextseed)
- nextseed=rnd(-1)
- end
- srand(0)
- nextseed=rnd(-1)
- for j=0,4 do
- for i=-1-j,1+j do
- maketree((i+rnd()-.5)*7,
- (j+rnd()-.5)*6)
- srand(nextseed)
- nextseed=rnd(-1)
- end
- end
- srand(0)
- nextseed=rnd(-1)
- for i=1,100 do
- local px=rnd(20)-10
- local pz=rnd(12)+3
- for j=1,5+rnd(10) do
- makeplant(px+rnd(2)-1,
- pz+rnd(1)-.5)
- end
- srand(nextseed)
- nextseed=rnd(-1)
- end
- for u=0,127 do
- for v=0,127 do
- index=getindex(u,v)
- dist=depth[index]
- if dist then
- light=ssao(pos[index],dist)
- //light+=(.5-light)*mid(dist/1200,0,1)
- local col=5+light*2.5
- if outline(u,v) then
- //col=1+flr(light*1.2)*4
- col=5
- end
- pset(u,v,col)
- end
- end
- end
- function _update()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement