Benutzer-Werkzeuge

Webseiten-Werkzeuge


Seitenleiste

ws1415:projekte_im_wintersemester_2014_15:perlin2d.py-3dgen

perlin2D.py

#-*-coding:utf-8-*-
from __future__ import division
import math
import numpy
 
g = []
 
def noise(x, y):
    def interpolate(x_punkt, y_punkt, w_00, w_10, w_01, w_11):
        #eigentliche interpolationsfunktion
        def s_x(x):
            return 10*x**3 - 15*x**4 + 6*x**5
 
        s_schlange_x = s_x(x_punkt)
        s_schlange_y = s_x(y_punkt)
        w_0 = (1-s_schlange_x)*w_00 + s_schlange_x*w_10
        w_1 = (1-s_schlange_x)*w_01 + s_schlange_x*w_11
        return (1 - s_schlange_y)*w_0 + s_schlange_y*w_1
    p_x = int(math.floor(x))
    p_y = int(math.floor(y))
    x_punkt = x - p_x
    y_punkt = y - p_y
    g_00 = g[p_x][p_y]
    g_10 = g[p_x + 1][p_y]
    g_01 = g[p_x][p_y + 1]
    g_11 = g[p_x + 1][p_y + 1]
    w_00 = g_00[0]*x_punkt       + g_00[1]*y_punkt
    w_10 = g_10[0]*(x_punkt - 1) + g_10[1]*y_punkt
    w_01 = g_01[0]*x_punkt       + g_01[1]*(y_punkt - 1)
    w_11 = g_11[0]*(x_punkt - 1) + g_11[1]*(y_punkt - 1)
    w = interpolate(x_punkt, y_punkt, w_00, w_10, w_01, w_11)
    return w
 
def perlin_noise(x, y, f_min, f_max = 0.5, phi = 0.3, a = 1, frequenz_faktor = 2):
    """Perlin-Noise-Funktion
    x: x Position 
    y: y Position
    f_min: niedrigste Frequenz der noise-Funktion (wird verdoppelt bis f_max erreicht ist)
    f_max: höchste Frequenz der noise-Funktion
    phi: reduziert die Amplitude (am Anfang 1)
    """
    summe = 0
    f = f_min
    while f <= f_max:
        summe += a*noise(f*x, f*y)
        f = f*frequenz_faktor
        a = phi*a
    return summe
 
def genheightmap(breite, f_min, f_max = 0.5, phi = 0.3, a = 1, frequenz_faktor = 2):
    '''Generiert eine quadratische zweidimensionale heightmap.
    Gibt eine Liste die eine 2D-Heightmap darstellt zurück.'''
    global g
    heightmap = []
    g = [[(numpy.random.uniform(-1, 1), numpy.random.uniform(-1, 1)) for wer2 in range(0,breite)] for wert in range(0,breite)]
    for i in range(breite):
        inmap = []
        for j in range(breite):
            inmap.append(perlin_noise(i, j,f_min, f_max, phi, a, frequenz_faktor)*1.6)
        heightmap.append(inmap)
    return heightmap
ws1415/projekte_im_wintersemester_2014_15/perlin2d.py-3dgen.txt · Zuletzt geändert: 2016/05/10 14:46 (Externe Bearbeitung)