update: Just one demo
Code: Select all
namespace defocus_dot
/'
- defocus dot - 2023 June 13 - by dafhi --
effect of a dot passing through a focus
usage:
'' order important
1. var a = defocus_dot.new_alpha( rad, z )
2. var m = defocus_dot.rad_mul
update: usage comment, iris default
'/
dim as single iris_diam = .5
dim as single focus_z = 10
dim as single _m, _a
function new_alpha( rad as single, z as single ) as single
var r_expan = rad + iris_diam * abs(z - focus_z)
_m = r_expan / rad
_a = rad^2 / r_expan^2
return _a
End function
function rad_mul( pixel_budget as single = 1 / 60 ) as single
'' pixel budget -> reduce radius
return iif( _a < pixel_budget, _m * _a / pixel_budget, _m )
end function
end namespace
' -------------------
#undef int '' text hack
#define int as integer
#define sng as single
#define dbl as double
#define flo(x) (((x)*2.0-0.5)shr 1) '' replaces int (and faster) - http://www.freebasic.net/forum/viewtopic.php?p=118633
function min( a as double, b as double ) as double
return iif( a < b, a, b)
end function
function max( a as double, b as double ) as double
return iif( a > b, a, b)
end function
function clamp( in dbl, hi dbl = 1, lo dbl = 0) dbl
return min( max(in, lo), hi ) '' June 12
End Function
' ---------------
function triwave( i sng ) sng
return abs( i - flo(i) - .5 ) - .25 '' by Stonemonkey
end function
function _cchsv(h sng, s sng, v sng) as ubyte
var wave_hgt = s * v
var elevate = v - wave_hgt
return 255.499 * (wave_hgt * clamp(triwave(h)*6 + .5) + elevate)
end function
function hsv( h sng=0, s sng=1, v sng=1 ) as ulong '' 2023 April 8
return rgb( _
_cchsv( h + 0/3, s,v ), _
_cchsv( h + 2/3, s,v ), _
_cchsv( h + 1/3, s,v ) )
end function
Union UnionARGB
As Ulong col
Type: As UByte B,G,R,A
End Type
declare operator cast as ulong
declare operator let( as ulong )
End Union
operator unionargb.cast as ulong
return col
end operator
operator unionargb.let( i as ulong )
col = i
end operator
' ---- demo
type v3 '' dodicat introduced us to this wild nomenclature
declare constructor
as single x,y,z
End Type
constructor v3
x = rnd - .5
y = rnd - .5
z = rnd - .5
end constructor
var w = 800, wh = w/2
var h = 600, hh = h/2
#include "fbgfx.bi" '' used for alpha primitives, in this example
screenres w,h,32,, fb.gfx_alpha_primitives
var u = 999
dim as v3 my_points(u)
dim as unionargb col(u)
for i as long = 0 to u
col(i) = hsv(rnd, rnd*rnd, 1)
next
var seconds = 10
var t0 = timer
dim as double t, tp, dt, dt2 '' fps average helper dt2
var report_next = 1.5#
defocus_dot.focus_z = 5
defocus_dot.iris_diam = 20
var general_scale_2d = defocus_dot.focus_z * (wh+hh)
var z_ = .5'defocus_dot.focus_z + 1'seconds
while t < seconds
tp = t
t = timer - t0
dt = t - tp
var rad = general_scale_2d * .005
screenlock
cls
for i as long = 0 to u
var z_stretched = 50*(my_points(i).z + z_)
if z_stretched > .1 then
var a = defocus_dot.new_alpha( rad, z_stretched )
var r = rad * defocus_dot.rad_mul / z_stretched
var z_s = general_scale_2d / z_stretched
var x = my_points(i).x * z_s + wh
var y = my_points(i).y * z_s + hh
col(i).a = 255.499 * a
circle (x, y), r, col(i),,,, f
endif
next
if t < .75 then
locate 2,2
elseif t > report_next then
var m = str(report_next)
windowtitle "FPS: " + str( 2 / (dt + dt2) ) '' deltatime avg
dt2 = dt
report_next += 1
endif
screenunlock
/'
inside screenlock, pressing escape will cause
frozen executable
'/
if inkey = chr(27) then exit while
for i as long = 0 to u
if my_points(i).z < -.5 then
my_points(i).z += 1
col(i) = hsv(rnd, rnd*rnd, 1)
else
my_points(i).z -= dt/20
endif
next
sleep 1
wend
locate 2,2
? "Done !"
sleep 999