class MT_PerlinNoise3DTexture(common.ulb:TrapShape) {
;
; Mark Townsend, May 2008
;
public:
func MT_PerlinNoise3DTexture(Generic pparent)
TrapShape.TrapShape(pparent)
int i = 0
int j = 0
int k = 0
int seed = @seed
while i < 256
p[i] = i
j = 0
while j < 3
seed = random(seed)
g[i, j] = seed / #randomrange
j = j + 1
endwhile
float s = sqrt(g[i, 0] * g[i, 0] + g[i, 1] * \
g[i, 1] + g[i, 2] * g[i, 2])
g[i, 0] = g[i, 0] / s
g[i, 1] = g[i, 1] / s
g[i, 2] = g[i, 2] / s
i = i + 1
endwhile
i = 0
while i < 256
k = p[i]
j = abs(seed) % 256
seed = random(seed)
p[i] = p[j]
p[j] = k
i = i + 1
endwhile
i = 0
while i < 256
p[256 + i] = p[i]
j = 0
while j < 3
g[256 + i , j] = g[i, j]
j = j + 1
endwhile
i = i + 1
endwhile
endfunc
float func Iterate(complex pz)
int iter = 0, float sum = 0
float amplitude = 1.0
r = (0,1) ^ (1/3)
w = pz
while iter < @octaves
float t = real(w) % 4096 + 4096
int bx0 = floor(t) % 256
int bx1 = (bx0 + 1) % 256
float rx0 = t - floor(t)
float rx1 = rx0 - 1
t = imag(w) % 4096 + 4096
int by0 = floor(t) % 256
int by1 = (by0 + 1) % 256
float ry0 = t - floor(t)
float ry1 = ry0 - 1
t = @z % 4096 + 4096
int bz0 = floor(t) % 256
int bz1 = (bz0 + 1) % 256
float rz0 = t - floor(t)
float rz1 = rz0 - 1
int b00 = p[p[bx0] + by0]
int b10 = p[p[bx1] + by0]
int b01 = p[p[bx0] + by1]
int b11 = p[p[bx1] + by1]
t = (rx0 * rx0 * (3.0 - 2.0 * rx0))
float sy = (ry0 * ry0 * (3.0 - 2.0 * ry0))
float sz = (rz0 * rz0 * (3.0 - 2.0 * rz0))
float u = (rx0 * g[b00 + bz0, 0] + ry0 * g[b00 + bz0, 1] + rz0 * g[b00 + bz0, 2])
float v = (rx1 * g[b10 + bz0, 0] + ry0 * g[b10 + bz0, 1] + rz0 * g[b10 + bz0, 2])
float a = (u + t * (v - u))
u = (rx0 * g[b01 + bz0, 0] + ry1 * g[b01 + bz0, 1] + rz0 * g[b01 + bz0, 2])
v = (rx1 * g[b11 + bz0, 0] + ry1 * g[b11 + bz0, 1] + rz0 * g[b11 + bz0, 2])
float b = (u + t * (v - u))
float c = (a + sy * (b - a))
u = (rx0 * g[b00 + bz1, 0] + ry0 * g[b00 + bz1, 1] + rz1 * g[b00 + bz1, 2])
v = (rx1 * g[b10 + bz1, 0] + ry0 * g[b10 + bz1, 1] + rz1 * g[b10 + bz1, 2])
a = (u + t * (v - u))
u = (rx0 * g[b01 + bz1, 0] + ry1 * g[b01 + bz1, 1] + rz1 * g[b01 + bz1, 2])
v = (rx1 * g[b11 + bz1, 0] + ry1 * g[b11 + bz1, 1] + rz1 * g[b11 + bz1, 2])
b = (u + t * (v - u))
float d = (a + sy *(b - a))
sum = sum + real(@fn1(c + sz * (d - c))) * amplitude
w = w * r / 0.5
amplitude = amplitude * @persistence
iter = iter + 1
endwhile
return (sum + 1) * 0.5
endfunc
private:
int p[514]
float g[514, 3]
default:
title = "Perlin Noise 3D Texture"
param octaves
caption = "Octaves"
default = 7
min = 1
endparam
param persistence
caption = "Persistence"
default = 0.5
endparam
func fn1
caption = "Function"
default = ident()
endfunc
param z
caption = "Z Coordinate"
default = 0.0
endparam
param @seed
caption = "Random Seed"
default = 1234567
endparam
}