Show t3.py syntax highlighted
#
# Copyright (c) 2003, Tuomas J. Lukka
#
# This file is part of Gzz.
#
# Gzz is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Gzz is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
# Public License for more details.
#
# You should have received a copy of the GNU Lesser General
# Public License along with Gzz; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#
#
# Tests for 3D texture generation using Fourier series and others.
from Numeric import *
from NumTut import view
import math
import random
N = 64
x = (arrayrange(N)[:,NewAxis,NewAxis] / (N+0.0)).astype(Float32)
y = (arrayrange(N)[NewAxis,:,NewAxis] / (N+0.0)).astype(Float32)
z = (arrayrange(N)[NewAxis,NewAxis,:] / (N+0.0)).astype(Float32)
for a in (x,y,z):
a.savespace(1)
def tview(oa):
"""View the given matrix tiled.
"""
print oa.shape
for zcoord in range(0, oa.shape[2], oa.shape[2]/4+1):
print "ZCoord:",zcoord
a = oa[:,:,zcoord]
x = a.shape[0]
y = a.shape[1]
arr = zeros((4*x, 4*y), Float64)
print arr.shape
for i in range(0,4):
for j in range(0,4):
arr[i*x:(i+1)*x, j*y:(j+1)*y] = a
view(arr)
def noise2(coords, freq, df):
"""An isotropic noise, between 0 and 1 in x and y.
"""
print "Noise2"
x,y = coords
color = 0 * (x + y)
freqs = [f*math.pi*2 for f in range(0,2*freq)]
csum = 0
for sx in freqs:
for sy in freqs:
if not freq-df < sqrt(sx**2 + sy**2)/(2*math.pi) < freq+df: continue
cmult = 1
# cmult *= 1/(1+(1-(sx / (freq+1.0)))**2)
# cmult *= 1/(1+(1-(sy / (freq+1.0)))**2)
for fx in (sin,cos):
for fy in (sin,cos):
coeff = cmult * (random.random()-0.5)
color += ( coeff * fx(sx*x) * fy(sy*y))
csum += coeff**2
return color / sqrt(csum)
def noise3(coords, freq, df):
"""An isotropic noise, between 0 and 1 in x and y and z.
"""
print "Noise3"
x,y,z = [c.astype(Float32) for c in coords]
for c in (x,y,z): c.savespace(1)
color = 0 * (x + y + z).astype(Float32)
color.savespace(1)
freqs = [f*math.pi*2 for f in range(0,2*freq)]
csum = 0
for sx in freqs:
for sy in freqs:
for sz in freqs:
if not freq-df <= sqrt(sx**2 + sy**2 + sz**2)/(2*math.pi) <= freq+df: continue
print [f/(2*math.pi) for f in sx,sy,sz]
cmult = 1
# cmult *= 1/(1+(1-(sx / (freq+1.0)))**2)
# cmult *= 1/(1+(1-(sy / (freq+1.0)))**2)
fxs = (sin(sx*x),cos(sx*x))
fys = (sin(sy*y),cos(sy*y))
fzs = (sin(sz*z),cos(sz*z))
for fx in fxs:
for fy in fys:
for fz in fzs:
coeff = cmult * (random.random()-0.5)
color += ( coeff * fx * fy * fz)
csum += coeff**2
return color / sqrt(csum)
def turb(noise, coords, minf, maxf, df):
color = 0
for c in coords: color = color * c
f = 0
while 2**f <= maxf:
if 2**f < minf:
f += 1
continue
c = abs(noise(coords,2**f, df))
tview(c)
color += c/(f+1)
f += 1
return color
# color = turb(noise3, (x,y,z), 2, 4, 2)
ox,oy,oz = [1 * noise3((x,y,z), 2, 0.4) for i in range(0,3)]
shiftednoise = fabs(noise3((x+ox, y+oy, z+oz), 1, 1.5))
tview(shiftednoise)
f = open("c.dat", "w")
f.write(shiftednoise.astype(Float32).tostring())
f.close()
# Clamp
# color += 0.5
# color -= (color<0)*color
# color -= (color>1)*(color-1)
# tview(color)
raw_input()
See more files for this project here