问题
我正在玩Perlin Noise reference implementation。
算法代码未提及它将产生的值的范围。从我读到的关于Perlin Noise的内容来看,结果应该是[0,1]的范围。它不适用于该算法。所以我玩弄了一下,在图表中显示结果,并认为结果应该是-0.5到0.5。
嗯,它也不是。只要您将x参数设置为任何值并将y和z保留为0。
一旦你。 G。有x和y,结果超出-0.5和0.5。
问题
有谁知道如何使用该算法? 结果值在哪个范围内?
代码
在JavaFX中编写了一个示例程序import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
public class NoiseChart extends Application {
@Override
public void start(Stage stage) {
// defining the axes
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
// creating the chart
final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(xAxis, yAxis);
lineChart.setCreateSymbols(false);
// defining a series
XYChart.Series series1 = new XYChart.Series();
series1.setName("Perlin Noise");
// populating the series with data from trhe perlin noise algorithm
double tx = 0;
double ty = 10000;
for (int i = 0; i < 256; i++) {
double x = ImprovedNoise.noise(tx, ty, 0);
series1.getData().add(new XYChart.Data(tx, x));
tx += 0.02;
ty += 0.02;
// output if we cross the [-0.5,0.5] range
if( Double.compare(x, -0.5) < 0 || Double.compare( x, 0.5) > 0) {
System.err.println( "tx=" + tx + ", ty=" + ty + ", perlin noise: " + x);
}
}
// show chart
Scene scene = new Scene(lineChart, 800, 600);
lineChart.getData().addAll(series1);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
// JAVA REFERENCE IMPLEMENTATION OF IMPROVED NOISE - COPYRIGHT 2002 KEN PERLIN.
// http://mrl.nyu.edu/~perlin/paper445.pdf
// http://mrl.nyu.edu/~perlin/noise/
public final static class ImprovedNoise {
static public double noise(double x, double y, double z) {
int X = (int)Math.floor(x) & 255, // FIND UNIT CUBE THAT
Y = (int)Math.floor(y) & 255, // CONTAINS POINT.
Z = (int)Math.floor(z) & 255;
x -= Math.floor(x); // FIND RELATIVE X,Y,Z
y -= Math.floor(y); // OF POINT IN CUBE.
z -= Math.floor(z);
double u = fade(x), // COMPUTE FADE CURVES
v = fade(y), // FOR EACH OF X,Y,Z.
w = fade(z);
int A = p[X ]+Y, AA = p[A]+Z, AB = p[A+1]+Z, // HASH COORDINATES OF
B = p[X+1]+Y, BA = p[B]+Z, BB = p[B+1]+Z; // THE 8 CUBE CORNERS,
return lerp(w, lerp(v, lerp(u, grad(p[AA ], x , y , z ), // AND ADD
grad(p[BA ], x-1, y , z )), // BLENDED
lerp(u, grad(p[AB ], x , y-1, z ), // RESULTS
grad(p[BB ], x-1, y-1, z ))),// FROM 8
lerp(v, lerp(u, grad(p[AA+1], x , y , z-1 ), // CORNERS
grad(p[BA+1], x-1, y , z-1 )), // OF CUBE
lerp(u, grad(p[AB+1], x , y-1, z-1 ),
grad(p[BB+1], x-1, y-1, z-1 ))));
}
static double fade(double t) { return t * t * t * (t * (t * 6 - 15) + 10); }
static double lerp(double t, double a, double b) { return a + t * (b - a); }
static double grad(int hash, double x, double y, double z) {
int h = hash & 15; // CONVERT LO 4 BITS OF HASH CODE
double u = h<8 ? x : y, // INTO 12 GRADIENT DIRECTIONS.
v = h<4 ? y : h==12||h==14 ? x : z;
return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
}
static final int p[] = new int[512], permutation[] = { 151,160,137,91,90,15,
131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180
};
static { for (int i=0; i < 256 ; i++) p[256+i] = p[i] = permutation[i]; }
}
}
超出[-0.5,0.5]范围的值:
tx=0.5200000000000001, ty=10000.520000000011, perlin noise: -0.5000000000027285
tx=0.5400000000000001, ty=10000.540000000012, perlin noise: -0.5084454691119766
tx=0.5600000000000002, ty=10000.560000000012, perlin noise: -0.5135793753147445
tx=0.5800000000000002, ty=10000.580000000013, perlin noise: -0.515139185394058
tx=0.6000000000000002, ty=10000.600000000013, perlin noise: -0.512927358018701
tx=0.6200000000000002, ty=10000.620000000014, perlin noise: -0.5068223692749664
图表:
或者在Perlin Noise结果映射到颜色值的图像中使用更合适的用例:
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class NoiseImage extends Application {
DoubleProperty frequencyProperty = new SimpleDoubleProperty( 5);
@Override
public void start(Stage stage) {
BorderPane root = new BorderPane();
ImageView imageView = new ImageView(createImage());
root.setCenter(imageView);
HBox toolbar = new HBox();
Label frequencyLabel = new Label( "Frequency");
Slider frequencySlider = new Slider(0, 50, 5);
frequencySlider.setPrefWidth(200);
frequencySlider.setShowTickLabels(true);
frequencySlider.setShowTickMarks(true);
frequencySlider.valueProperty().bindBidirectional(frequencyProperty);
frequencyProperty.addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
imageView.setImage(createImage());
}
});
Label frequencyValueLabel = new Label( "Frequency");
frequencyValueLabel.textProperty().bind(frequencyProperty.asString());
toolbar.getChildren().addAll(frequencyLabel, frequencySlider, frequencyValueLabel);
root.setTop(toolbar);
Scene scene = new Scene(root, 800, 600);
stage.setScene(scene);
stage.show();
}
/**
* For each pixel in the image create the perlin noise and set the gray color accordingly
* @return
*/
public Image createImage() {
double noise;
int width = 256;
int height = 256;
WritableImage wr = new WritableImage(width, height);
PixelWriter pw = wr.getPixelWriter();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
double frequency = frequencyProperty.get() / (double) width;
double tx = x * frequency;
double ty = y * frequency;
noise = ImprovedNoise.noise(tx, ty, 0);
double gray = normalizeValue(noise, -.5, .5, 0., 1.);
// output if we cross the [-0.5,0.5] range
if( Double.compare(gray, 0) < 0 || Double.compare( gray, 1) > 0) {
System.err.println( "tx=" + x + ", ty=" + y + ", perlin noise: " + noise);
}
// TODO: this shouldn't be necessary
// question: what range does the noise method return values? currently they can be larger than .5
gray = clamp(gray, 0, 1);
pw.setColor(x, y, Color.gray(gray));
}
}
return wr;
}
public static double normalizeValue( double value, double min, double max, double newMin, double newMax) {
return (value - min) * (newMax - newMin) / (max - min) + newMin;
}
public static double clamp( double value, double min, double max) {
if( Double.compare(value, min) < 0)
return min;
if( Double.compare(value, max) > 0)
return max;
return value;
}
public static void main(String[] args) {
launch(args);
}
// JAVA REFERENCE IMPLEMENTATION OF IMPROVED NOISE - COPYRIGHT 2002 KEN PERLIN.
// http://mrl.nyu.edu/~perlin/paper445.pdf
// http://mrl.nyu.edu/~perlin/noise/
public final static class ImprovedNoise {
static public double noise(double x, double y, double z) {
int X = (int)Math.floor(x) & 255, // FIND UNIT CUBE THAT
Y = (int)Math.floor(y) & 255, // CONTAINS POINT.
Z = (int)Math.floor(z) & 255;
x -= Math.floor(x); // FIND RELATIVE X,Y,Z
y -= Math.floor(y); // OF POINT IN CUBE.
z -= Math.floor(z);
double u = fade(x), // COMPUTE FADE CURVES
v = fade(y), // FOR EACH OF X,Y,Z.
w = fade(z);
int A = p[X ]+Y, AA = p[A]+Z, AB = p[A+1]+Z, // HASH COORDINATES OF
B = p[X+1]+Y, BA = p[B]+Z, BB = p[B+1]+Z; // THE 8 CUBE CORNERS,
return lerp(w, lerp(v, lerp(u, grad(p[AA ], x , y , z ), // AND ADD
grad(p[BA ], x-1, y , z )), // BLENDED
lerp(u, grad(p[AB ], x , y-1, z ), // RESULTS
grad(p[BB ], x-1, y-1, z ))),// FROM 8
lerp(v, lerp(u, grad(p[AA+1], x , y , z-1 ), // CORNERS
grad(p[BA+1], x-1, y , z-1 )), // OF CUBE
lerp(u, grad(p[AB+1], x , y-1, z-1 ),
grad(p[BB+1], x-1, y-1, z-1 ))));
}
static double fade(double t) { return t * t * t * (t * (t * 6 - 15) + 10); }
static double lerp(double t, double a, double b) { return a + t * (b - a); }
static double grad(int hash, double x, double y, double z) {
int h = hash & 15; // CONVERT LO 4 BITS OF HASH CODE
double u = h<8 ? x : y, // INTO 12 GRADIENT DIRECTIONS.
v = h<4 ? y : h==12||h==14 ? x : z;
return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
}
static final int p[] = new int[512], permutation[] = { 151,160,137,91,90,15,
131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180
};
static { for (int i=0; i < 256 ; i++) p[256+i] = p[i] = permutation[i]; }
}
}
超出[-0.5,0.5]的值:
...
tx=250, ty=131, perlin noise: -0.5221718771387314
tx=250, ty=132, perlin noise: -0.5026623324564983
tx=251, ty=124, perlin noise: -0.5021001248099309
tx=251, ty=125, perlin noise: -0.5193524233279807
tx=251, ty=126, perlin noise: -0.5312601268638136
tx=251, ty=127, perlin noise: -0.5375809523803251
tx=251, ty=128, perlin noise: -0.5382082635276979
tx=251, ty=129, perlin noise: -0.5331735730625967
tx=251, ty=130, perlin noise: -0.5226452480286591
tx=251, ty=131, perlin noise: -0.5069234176982861
tx=252, ty=125, perlin noise: -0.509547880806866
tx=252, ty=126, perlin noise: -0.5202072536185804
...
截图:
非常感谢您的帮助!