04.03.2013, 00:52
|
#4
|
Мастер
Регистрация: 13.06.2011
Сообщений: 1,103
Написано 481 полезных сообщений (для 1,836 пользователей)
|
Ответ: Simplex noise 2d 3d 4d

Слегка модифицированный шум перлина.

using UnityEngine; using System.Collections; using System;
public class PerlinNoise : MonoBehaviour { private static System.Random randGen; private static int[] rand = new int[512]; private static float[] reliefFactor = {1.0f,0.5f,0.25f,0.125f,0.125f,0.125f,0.125f,0.125f}; public static float zoom = 1.0f; // Use this for initialization public static void Init ( int seed) { randGen = new System.Random(seed); for(int i=0; i<511; i++) rand[i] = randGen.Next(0,255); } private static float fade(float t) { return t*t*t*(t*(t*6-15)+10); } private static int myRand(int x, int y){ int x0 = x%256; int y0 = y%512; int result = 0; if(x0>=0 && y0>=0)result = rand[x0+rand[y0]]; return result; } public static float perlin2d(float x, float y, int octave){ int length = reliefFactor.Length; float result = 0.0f; float height = 0.0f; for( int i=1; i<=length; i++){ //принимаю шум от текущей октавы в (0;1) перевожу к (-1;1); float octaveNoise =2*noise(x*zoom*i,y*zoom*i)-1; result += octaveNoise*reliefFactor[i-1]; height += reliefFactor[i-1]; } //перевожу к (-1;1); result = result/height; float degree = (Mathf.Abs(result) + 1)*2.5f; float reliefHeight = 1.15f; if(result>=0){result = Mathf.Pow(reliefHeight*result,degree);} else{result = -Mathf.Pow(Mathf.Abs(reliefHeight*result),degree);} return result; } private static float noise(float x, float y){ //получаю координаты ячейки int x0 = Mathf.FloorToInt(x); int y0 = Mathf.FloorToInt(y); //смещение внутри ячейки float dx = x-x0; float dy = y-y0; //получаю четыре значения в углах клетки int x00 = myRand(x0,y0); int x01 = myRand(x0,y0+1); int x10 = myRand(x0+1,y0); int x11 = myRand(x0+1,y0+1);
//получаю значение в клетке для текущей координаты float dif0 = Mathf.Lerp(x00,x01,fade (dy)); float dif1 = Mathf.Lerp(x10,x11,fade (dy)); float result = Mathf.Lerp(dif0,dif1,fade (dx)); return result*0.0039215686274509803921568627451f; } }
Последний раз редактировалось dsd, 04.03.2013 в 13:28.
|
(Offline)
|
|