class MT_PerlinNoiseTexture(common.ulb:TrapShape) {
;
; Mark Townsend, May 2008
;
public:
func MT_PerlinNoiseTexture(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 < 2
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, 0] = g[i, 0] / s
g[i, 1] = g[i, 1] / s
i = i + 1
endwhile
i = 0
while i < 256
k = p[i]
seed = random(seed)
j = abs(seed) % 256
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 < 2
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
int b00 = p[p[bx0] + by0]
int b10 = p[p[bx1] + by0]
int b01 = p[p[bx0] + by1]
int b11 = p[p[bx1] + by1]
float sx = (rx0 * rx0 * (3 - 2 * rx0))
float sy = (ry0 * ry0 * (3 - 2 * ry0))
float u = rx0 * g[b00, 0] + ry0 * g[b00, 1]
float v = rx1 * g[b10, 0] + ry0 * g[b10, 1]
float a = u + sx * (v - u)
u = rx0 * g[b01, 0] + ry1 * g[b01, 1]
v = rx1 * g[b11, 0] + ry1 * g[b11, 1]
float b = u + sx * (v - u)
sum = sum + real(@fn1(a + sy * (b - a))) * 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, 2]
default:
title = "Perlin Noise 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 @seed
caption = "Random Seed"
default = 1234567
endparam
}