def pandaPerlin(): loopCount = 100000 print 'reg panda2d' s = PerlinNoise2() maxV = 0.0 minV = 0.0 t = time() for a in xrange(loopCount): v = s.noise(*[random.random() * 100 for x in xrange(2)]) maxV = max(maxV, v) minV = min(minV, v) print 2, (time() - t) / loopCount * 1000, maxV, minV print 'reg panda3d' s = PerlinNoise3() maxV = 0.0 minV = 0.0 t = time() for a in xrange(loopCount): v = s.noise(*[random.random() * 100 for x in xrange(3)]) maxV = max(maxV, v) minV = min(minV, v) print 3, (time() - t) / loopCount * 1000, maxV, minV #pandaPerlin()
def __init__(self, editorFile, bakeryFolder): #id is a seed for the map and unique name for any cached heightmap images self.dice = RandomNumGen(TimeVal().getUsec()) self.id = self.dice.randint(2, 1000000) # the overall smoothness/roughness of the terrain smoothness = 80 # how quickly altitude and roughness shift self.consistency = smoothness * 8 # waterHeight is expressed as a multiplier to the max height self.waterHeight = 0.3 # for realism the flatHeight should be at or very close to waterHeight self.flatHeight = self.waterHeight + 0.04 #creates noise objects that will be used by the getHeight function """Create perlin noise.""" # See getHeight() for more details.... # where perlin 1 is low terrain will be mostly low and flat # where it is high terrain will be higher and slopes will be exagerrated # increase perlin1 to create larger areas of geographic consistency self.perlin1 = StackedPerlinNoise2() perlin1a = PerlinNoise2(0, 0, 256, seed=self.id) perlin1a.setScale(self.consistency) self.perlin1.addLevel(perlin1a) perlin1b = PerlinNoise2(0, 0, 256, seed=self.id * 2 + 123) perlin1b.setScale(self.consistency / 2) self.perlin1.addLevel(perlin1b, 1 / 2) # perlin2 creates the noticeable noise in the terrain # without perlin2 everything would look unnaturally smooth and regular # increase perlin2 to make the terrain smoother self.perlin2 = StackedPerlinNoise2() frequencySpread = 3.0 amplitudeSpread = 3.4 perlin2a = PerlinNoise2(0, 0, 256, seed=self.id * 2) perlin2a.setScale(smoothness) self.perlin2.addLevel(perlin2a) perlin2b = PerlinNoise2(0, 0, 256, seed=self.id * 3 + 3) perlin2b.setScale(smoothness / frequencySpread) self.perlin2.addLevel(perlin2b, 1 / amplitudeSpread) perlin2c = PerlinNoise2(0, 0, 256, seed=self.id * 4 + 4) perlin2c.setScale(smoothness / (frequencySpread * frequencySpread)) self.perlin2.addLevel(perlin2c, 1 / (amplitudeSpread * amplitudeSpread)) perlin2d = PerlinNoise2(0, 0, 256, seed=self.id * 5 + 5) perlin2d.setScale(smoothness / (math.pow(frequencySpread, 3))) self.perlin2.addLevel(perlin2d, 1 / (math.pow(amplitudeSpread, 3))) perlin2e = PerlinNoise2(0, 0, 256, seed=self.id * 6 + 6) perlin2e.setScale(smoothness / (math.pow(frequencySpread, 4))) self.perlin2.addLevel(perlin2e, 1 / (math.pow(amplitudeSpread, 4)))
def __init__(self, size): self.bw = BulletWorld() self.bw.setGravity(0, 0, 0) self.size = size self.perlin = PerlinNoise2() #utilities.app.accept('bullet-contact-added', self.onContactAdded) #utilities.app.accept('bullet-contact-destroyed', self.onContactDestroyed) self.player = Player(self) self.player.initialise() self.entities = list() self.bgs = list() self.makeChunk(Point2(0, 0), Point2(3.0, 3.0)) self.cmap = buildMap(self.entities, self.player.location) self.mps = list() self.entities.append( Catcher(Point2(10, 10), self.player, self.cmap, self))
def generateNoiseObjects(self): """Create perlin noise.""" # See getHeight() for more details.... # where perlin 1 is low terrain will be mostly low and flat # where it is high terrain will be higher and slopes will be exagerrated # increase perlin1 to create larger areas of geographic consistency self.perlin1 = StackedPerlinNoise2() perlin1a = PerlinNoise2(0, 0, 256, seed=self.id) perlin1a.setScale(self.consistency) self.perlin1.addLevel(perlin1a) perlin1b = PerlinNoise2(0, 0, 256, seed=self.id * 2 + 123) perlin1b.setScale(self.consistency / 2) self.perlin1.addLevel(perlin1b, 1 / 2) # perlin2 creates the noticeable noise in the terrain # without perlin2 everything would look unnaturally smooth and regular # increase perlin2 to make the terrain smoother self.perlin2 = StackedPerlinNoise2() frequencySpread = 3.0 amplitudeSpread = 3.4 perlin2a = PerlinNoise2(0, 0, 256, seed=self.id * 2) perlin2a.setScale(self.smoothness) self.perlin2.addLevel(perlin2a) perlin2b = PerlinNoise2(0, 0, 256, seed=self.id * 3 + 3) perlin2b.setScale(self.smoothness / frequencySpread) self.perlin2.addLevel(perlin2b, 1 / amplitudeSpread) perlin2c = PerlinNoise2(0, 0, 256, seed=self.id * 4 + 4) perlin2c.setScale(self.smoothness / (frequencySpread * frequencySpread)) self.perlin2.addLevel(perlin2c, 1 / (amplitudeSpread * amplitudeSpread)) perlin2d = PerlinNoise2(0, 0, 256, seed=self.id * 5 + 5) perlin2d.setScale(self.smoothness / (math.pow(frequencySpread, 3))) self.perlin2.addLevel(perlin2d, 1 / (math.pow(amplitudeSpread, 3))) perlin2e = PerlinNoise2(0, 0, 256, seed=self.id * 6 + 6) perlin2e.setScale(self.smoothness / (math.pow(frequencySpread, 4))) self.perlin2.addLevel(perlin2e, 1 / (math.pow(amplitudeSpread, 4)))
class StarRender(object): body = None atmosphere = None light = None noise = PerlinNoise2(64, 64) noise_texture = None
def gpugemsTerrain(): W, H, D = (1,1,2) noise = PerlinNoise3(1,1,1,256,0) warpNoise = PerlinNoise3(1,1,1,256,1) # noise = PerlinNoise3() # warpNoise = PerlinNoise3() def densityFunction(x, y, z): point = LVecBase3f(x, y, z) warpfactor = 0.0004 warp = warpNoise(LVecBase3f(x*warpfactor, y*warpfactor, z*warpfactor)) * 8 point.componentwiseMult(LVecBase3f(warp, warp, warp)) density = -point.z floor = 4 ω = [16.03, 8.05, 4.03, 1.96, 1.01, 0.49, 0.23, 0.097] # frequencies A = [0.25, 0.25, 0.5, 0.5, 1, 2, 8, 32] # amplitudes for i in range(len(ω)): ωVec = LVecBase3f(ω[i], ω[i], ω[i]) ωVec.componentwiseMult(point) density += noise(ωVec) * A[i] density += max(min((floor - point.z)*3, 1), 0)*40; return density def casePoint(density): if density < 0: return 0 else: return 1 g = 1 # granularity chunk = [[[ [[[densityFunction((x+32*w)/g, (y+32*h)/g, (z+32*d)/g) for x in range(33)] for y in range(33)] for z in range(33)] for w in range(W)] for h in range(H)] for d in range(D)] vdata = GeomVertexData('Land', GeomVertexFormat.get_v3n3c4(), Geom.UHStatic) vdata.setNumRows(33*33*4) vertices = GeomVertexWriter(vdata, 'vertex') normals = GeomVertexWriter(vdata, 'normal') colors = GeomVertexWriter(vdata, 'color') waterNoise = PerlinNoise2() ct = 0 for h in range(H): for w in range(W): for d in range(D): # bigger! for z in range(32): for y in range(32): for x in range(32): v0 = chunk[d][h][w][z+1][y][x] v1 = chunk[d][h][w][z][y][x] v2 = chunk[d][h][w][z][y][x+1] v3 = chunk[d][h][w][z+1][y][x+1] v4 = chunk[d][h][w][z+1][y+1][x] v5 = chunk[d][h][w][z][y+1][x] v6 = chunk[d][h][w][z][y+1][x+1] v7 = chunk[d][h][w][z+1][y+1][x+1] case = casePoint(chunk[d][h][w][z+1][y][x])\ + 2*casePoint(chunk[d][h][w][z][y][x])\ + 4*casePoint(chunk[d][h][w][z][y][x+1])\ + 8*casePoint(chunk[d][h][w][z+1][y][x+1])\ + 16*casePoint(chunk[d][h][w][z+1][y+1][x])\ + 32*casePoint(chunk[d][h][w][z][y+1][x])\ + 64*casePoint(chunk[d][h][w][z][y+1][x+1])\ + 128*casePoint(chunk[d][h][w][z+1][y+1][x+1])\ if case == 0 or case == 255: continue numpolys = TABLE['case_to_numpolys'][case][0] for polyIndex in range(numpolys): edgeConnects = TABLE['g_triTable'][case][polyIndex] currentTriangleVertices = [] currentTriangleColors = [] for edgeIndex in range(3): edge = edgeConnects[edgeIndex] X = x+32*w Y = y+32*h Z = z+32*d scale = 1 X = scale*X Y = scale*Y Z = scale*Z ph = min(1, (d*32+z)/32) # point height ph = ph*ph*ph color = None # if d == 0 and z <= 7: # v = waterNoise.noise(LVecBase2f(X, Y)) # b= 1 - v**2 # if b > 0.99: # color = [0.12, 0.29, b, 1] if edge == 0: diff = abs(v0)/(abs(v0)+abs(v1)) diff = scale * diff currentTriangleVertices.append([X, Y, Z-diff]) if color is None: color = [0.49+0.51*ph, 0.84+0.16*ph, 0.06+0.94*ph, 1] currentTriangleColors.append(color) elif edge == 1: diff = abs(v1)/(abs(v1)+abs(v2)) diff = scale * diff currentTriangleVertices.append([X+diff, Y, Z-scale]) currentTriangleColors.append([0.89, 0.17, 0.1, 1]) elif edge == 2: diff = abs(v3)/(abs(v3)+abs(v2)) diff = scale * diff currentTriangleVertices.append([X+scale, Y, Z-diff]) if color is None: color = [0.49+0.51*ph, 0.84+0.16*ph, 0.06+0.94*ph, 1] currentTriangleColors.append(color) elif edge == 3: diff = abs(v0)/(abs(v0)+abs(v3)) diff = scale * diff currentTriangleVertices.append([X+diff, Y, Z]) currentTriangleColors.append([0.89, 0.17, 0.1, 1]) elif edge == 4: diff = abs(v4)/(abs(v4)+abs(v5)) diff = scale * diff currentTriangleVertices.append([X, Y+scale, Z-diff]) if color is None: color = [0.49+0.51*ph, 0.84+0.16*ph, 0.06+0.94*ph, 1] currentTriangleColors.append(color) elif edge == 5: diff = abs(v5)/(abs(v5)+abs(v6)) diff = scale * diff currentTriangleVertices.append([X+diff, Y+scale, Z-scale]) currentTriangleColors.append([0.89, 0.17, 0.1, 1]) elif edge == 6: diff = abs(v7)/(abs(v7)+abs(v6)) diff = scale * diff currentTriangleVertices.append([X+scale, Y+scale, Z-diff]) if color is None: color = [0.49+0.51*ph, 0.84+0.16*ph, 0.06+0.94*ph, 1] currentTriangleColors.append(color) elif edge == 7: diff = abs(v4)/(abs(v4)+abs(v7)) diff = scale * diff currentTriangleVertices.append([X+diff, Y+scale, Z]) currentTriangleColors.append([0.89, 0.17, 0.1, 1]) elif edge == 8: diff = abs(v0)/(abs(v0)+abs(v4)) diff = scale * diff currentTriangleVertices.append([X, Y+diff, Z]) currentTriangleColors.append([0.89, 0.34, 0.1, 1]) elif edge == 9: diff = abs(v1)/(abs(v1)+abs(v5)) diff = scale * diff currentTriangleVertices.append([X, Y+diff, Z-scale]) currentTriangleColors.append([0.89, 0.34, 0.1, 1]) elif edge == 10: diff = abs(v2)/(abs(v2)+abs(v6)) diff = scale * diff currentTriangleVertices.append([X+scale, Y+diff, Z-scale]) currentTriangleColors.append([0.89, 0.34, 0.1, 1]) elif edge == 11: diff = abs(v3)/(abs(v3)+abs(v7)) diff = scale * diff currentTriangleVertices.append([X+scale, Y+diff, Z]) currentTriangleColors.append([0.89, 0.34, 0.1, 1]) a = currentTriangleVertices[0] b = currentTriangleVertices[1] c = currentTriangleVertices[2] ba = LVecBase3f(b[0]-a[0], b[1]-a[1], b[2]-a[2]) ca = LVecBase3f(c[0]-a[0], c[1]-a[1], c[2]-a[2]) normal = ba.cross(ca).normalized() for i in range(3): ct += 1 cv = currentTriangleVertices[i] cn = normal cc = currentTriangleColors[i] vertices.addData3f(cv[0], cv[1], cv[2]) normals.addData3f(cn[0], cn[1], cn[2]) colors.addData4f(cc[0], cc[1], cc[2], cc[3])
from panda3d.core import PerlinNoise2, PNMImage, PointLight, Shader, Texture, Vec3, Vec4 from direct.filter.CommonFilters import CommonFilters #sandbox.base.render.setShaderAuto() planet = shapeGenerator.Sphere(1, 64, 'planet') planet.reparentTo(sandbox.base.render) planet.setPos(5, 20, 0) mesh = shapeGenerator.Sphere(1, 64, 'star') mesh.reparentTo(sandbox.base.render) mesh.setPos(-5, 20, 0) noise = PerlinNoise2(64, 64) texture = Texture('noise') texture.setup2dTexture() img = PNMImage(1024, 1024) for y in range(1024): for x in range(1024): img.setXel(x, y, noise.noise(x, y)) texture.load(img) mesh.setTexture(texture, 1) mesh.setShaderInput('time', 0) #shaders = Shader.load(Shader.SLGLSL, 'vortexVertex.glsl', 'starFrag2.glsl') shaders = Shader.load(Shader.SLGLSL, 'sphereVertex.glsl', 'starFrag2.glsl') mesh.setShader(shaders) light = mesh.attachNewNode(PointLight("sun"))
def generateStackedPerlin(self, perlin, frequency, layers, frequencySpread, amplitudeSpread, id): for x in range(layers): layer = PerlinNoise2(0, 0, 256, seed=id + x) layer.setScale(frequency / (math.pow(frequencySpread, x))) perlin.addLevel(layer, 1 / (math.pow(amplitudeSpread, x)))