Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Images, Colors, ImageView, LinearAlgebra, Gtk.ShortNames, StaticArrays, BenchmarkTools, ForwardDiff, FixedPointNumbers, Profile, StatProfilerHTML
- function ray_source(x::Real,y::Real,Pos::SVector{3},Direction::SVector{3},
- maxx::Int64,maxy::Int64,Sensorxv::SVector{3},Sensoryv::SVector{3})
- (SVector{3}(Pos+(maxx/2-x)/maxx*Sensorxv+(maxy/2-y)/maxy*Sensoryv),SVector{3}(normalize(Direction)))
- end
- const x = 800
- const y = 800
- Picff = Array{RGB{Normed{UInt8,8}}}(undef,x,y)
- PicTos = Array{RGB{Normed{UInt8,8}}}(undef,x,y)
- PicExp = Array{RGB{Normed{UInt8,8}}}(undef,x,y)
- const CamPosition = SVector{3}(-10.0,10.0,20.0)
- const Normal = SVector{3}(10.0,-10.0,-20.0)
- const SclX = 60.0
- const SclY = 60.0
- const Ortho1 = SVector{3}( 0.0, -0.8944271909999159, 0.4472135954999579)
- const Ortho2 = SVector{3}( 0.9128709291752769, 0.18257418583505536, 0.3651483716701107 )
- function union(SDFs...)
- return (Pos::SVector{3}) -> minimum(map(x->x(Pos),SDFs))
- end
- function trans(Sdf,Vec)
- return (Pos::SVector{3})->(Sdf(Pos-Vec))
- end
- function plane(Normal::SVector{3},Shader)
- return (Pos::SVector{3}) -> (dot(Pos,Normal),rand(),Shader)
- end
- function sphere(Radius::Float64,Shader)
- a = rand() #Needed for strict ordering since my lambdas/shaders can't be compared
- return (Pos::SVector{3}) -> return (norm(Pos)-Radius,a,Shader)
- end
- function unicolor(Color::RGB)
- return (Pos,Normal,Sdf,Step,Inital)->Color
- end
- function checkerbox(BSize,Shader1,Shader2)
- return (Pos::SVector{3},Normal::SVector{3},Sdf,Step,Inital) -> if xor(mod(Pos[1],2*BSize)>BSize,mod(Pos[2],2*BSize)>BSize,mod(Pos[3],2*BSize)>BSize) Shader1(Pos,Normal,Sdf,Step,Inital) else Shader2(Pos,Normal,Sdf,Step,Inital) end
- end
- function gradient(Pos::SVector{3},Normal,Sdf,Step::Float64,Inital)
- normalize([(Sdf(Pos+SVector{3}(Step,0.0,0.0))[1] - Sdf(Pos)[1])/Step,
- (Sdf(Pos+SVector{3}(0.0,Step,0.0))[1] - Sdf(Pos)[1])/Step,
- (Sdf(Pos+SVector{3}(0.0,0.0,Step))[1] - Sdf(Pos)[1])/Step])
- end
- function lambertian(LightDir,Shader)
- return (Pos::SVector{3},Normal,Sdf,Step,Inital)->(Shader(Pos,Normal,Sdf,Step,Inital)*max(0,dot(gradient(Pos,Normal,Sdf,Step,Inital),LightDir)))::RGB{Normed{UInt8,8}}
- end
- function spot(LightPos,Brightness,Shader)
- return (Pos::SVector{3},Normal::SVector{3},Sdf,Step,Inital)->(Shader(Pos,Normal,Sdf,Step,Inital)*min(1,max(0.1,Brightness*dot(gradient(Pos,Normal,Sdf,Step,Inital),normalize(LightPos-Pos)))))
- end
- const Red = unicolor(RGB{Normed{UInt8,8}}(0.8,0.0,0.0))
- const Green = unicolor(RGB{Normed{UInt8,8}}(0.0,0.9,0.0))
- const Blue = unicolor(RGB{Normed{UInt8,8}}(0.0,0.0,0.7))
- const Black = unicolor(RGB{Normed{UInt8,8}}(0.0,0.0,0.0))
- const White = unicolor(RGB{Normed{UInt8,8}}(1.0,1.0,1.0))
- const Grey = unicolor(RGB{Normed{UInt8,8}}(0.5,0.5,0.5))
- function scene_ff(Pos)
- union(trans(sphere(3.0,spot(SVector{3,Float64}(0,0,0),1.5,Red)),SVector{3,Float64}(0,20,0)),
- trans(sphere(12.0,spot(SVector{3,Float64}(0,0,0),1.8,Blue)),SVector{3,Float64}(12,9,-4)),
- trans(sphere(9.0,spot(SVector{3,Float64}(0,0,0),1.0,Green)),SVector{3,Float64}(3,-13,0)),
- sphere(1.0,White),
- trans(sphere(0.5,White),SVector{3,Float64}(-20,0,0)),
- trans(sphere(1.0,Red),SVector{3,Float64}(-18,0,0)),
- plane(SVector{3,Float64}(0.0,0.0,1.0),checkerbox(10,Grey,Black)))(Pos)
- end
- function raymarch(Pos::SVector{3},Normal::SVector{3},Sdf,FbSDF,renderdist)
- Normal=normalize(Normal);
- stepsize=1.0;
- Start=Pos;
- Strecke=0.0;
- while (stepsize > 0.001)
- stepsize=Sdf(Pos)[1];
- Pos+=Normal*stepsize;
- Strecke+=stepsize
- if Strecke > renderdist
- return FbSDF(Pos,Normal,Sdf,stepsize,Start)
- end
- end
- return Sdf(Pos)[3](Pos,Normal,Sdf,stepsize,Start)
- end
- for h=-10.0:0.5:10.0
- for i=-10.0:0.5:10.0
- for j=-10.0:0.5:10.0
- scene_ff(SVector{3,Float64}(h,i,j))
- end
- end
- end
- ## 40.602 ms (1309499 allocations: 178.49 MiB)
- for i=1:x
- @profile for j=1:y
- lpos, lnormal = ray_source(Float64(i),Float64(j),CamPosition,Normal,x,y,SclX * Ortho1,SclY * Ortho2)
- Picff[i,j]=raymarch(lpos,lnormal,scene_ff,Black,500.0)
- end
- end
- statprofilehtml()
- #=
- @btime for i=1:x
- for j=1:y
- lpos, lnormal = ray_source(Float64(i),Float64(j),$CamPosition,$Normal,x,y,$SclX * $Ortho1,$SclY * $Ortho2)
- Picff[i,j]=raymarch(lpos,lnormal,$scene_ff,$Black,500.0)
- end
- end
- ## 5.077 s (133328084 allocations: 16.42 GiB)
- guidict = imshow(Picff);
- if (!isinteractive())
- # Create a condition object
- c = Condition()
- # Get the window
- win = guidict["gui"]["window"]
- # Notify the condition object when the window closes
- signal_connect(win, :destroy) do widget
- notify(c)
- end
- # Wait for the notification before proceeding ...
- wait(c)
- end
- =#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement