Turbulence By Jim Shaw.
Originally Posted at the Home Brew Forums
Here's a little idea i've been working on to do some marbling textures. It also looks a lot like clouds.
It uses Perlin's noise and turbulence functions to distort a stripy texture into the plasma style shapes.
AppTitle("noise")
Graphics 640,480,0,2
Const noise_shift%=6
Const max_noise%=1 Shl noise_shift
Const max_noise_mask%=max_noise-1
Dim noise_table%(max_noise,max_noise,max_noise)
Dim bitmap%(256,256)
For y=0 To 256
For x=0 To 256
r%=127+100*Sin(x*7)
g%=r
b%=r
r = r + Int(16*(1+Sin(x*3)))
b = b + Int(16*(1+Sin(x*5)))
If r>=255 r=255
If r<0 r=0
If g>=255 g=255
If g<0 g=0
If b>=255 b=255
If b<0 b=0
bitmap(x,y)=r Shl 16 + g Shl 8 + b
Next
Next
Const tscale%=40
Const toff%=2*2
SeedRnd MilliSecs()
init_noise()
SetBuffer BackBuffer()
time% = MilliSecs()
Repeat
Cls
tick% = MilliSecs()
dt% = tick-time
time% = tick
Text 10,10,"fps "+(1000.0/dt)
LockBuffer()
For y=0 To 256-tscale
For x=0 To 256-tscale
WritePixelFast 192+x,112+y,bitmap(x+tscale*turbulence(x*toff,y*toff,z),y+tscale*turbulence(x*toff,y*toff,z))
Next
If KeyDown(1) Exit
Next
UnlockBuffer()
z=z+1
Flip
Until KeyDown(1)
End
Function init_noise()
Local x%,y%,z%,xx%,yy%,zz%
For z=0 To max_noise
For y=0 To max_noise
For x=0 To max_noise
noise_table(x,y,z) = Rand(10000)
If x=max_noise xx=0 Else xx=x
If y=max_noise yy=0 Else yy=y
If z=max_noise zz=0 Else zz=z
noise_table(x,y,z)=noise_table(xx,yy,zz)
Next
Next
Next
End Function
Function noise#(x%,y%,z%)
Local ix%,iy%,iz%
Local ox%,oy%,oz%
Local n%
Local n00%,n01%,n10%,n11%
Local n0%,n1%
ix = x Shr noise_shift
iy = y Shr noise_shift
iz = z Shr noise_shift
ox = x And max_noise_mask
oy = y And max_noise_mask
oz = z And max_noise_mask
ix = ix And max_noise_mask
iy = iy And max_noise_mask
iz = iz And max_noise_mask
n = noise_table(ix,iy,iz)
n00 = n + (ox * (noise_table(ix+1,iy,iz) - n)) Sar noise_shift
n = noise_table(ix,iy,iz+1)
n01 = n + (ox * (noise_table(ix+1,iy,iz+1) - n)) Sar noise_shift
n = noise_table(ix,iy+1,iz)
n10 = n + (ox * (noise_table(ix+1,iy+1,iz) - n)) Sar noise_shift
n = noise_table(ix,iy+1,iz+1)
n11 = n + (ox * (noise_table(ix+1,iy+1,iz+1) - n)) Sar noise_shift
n0 = n00 + (oy * (n10-n00)) Sar noise_shift
n1 = n01 + (oy * (n11-n01)) Sar noise_shift
Return (n0 + (oz * (n1-n0)) Sar noise_shift) * 0.0001
End Function
Function turbulence#(x%,y%,z%)
Local t#
Local scale#
scale=0.6
t=0
While scale >= 0.07
t# = t# + scale * noise(x,y,z)
scale = scale / 2
x = x Shl 1
y = y Shl 1
z = z Shl 1
Wend
Return t
End Function
Jim