-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValueNoise.java
More file actions
66 lines (52 loc) · 1.78 KB
/
ValueNoise.java
File metadata and controls
66 lines (52 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.thrus.test;
import java.util.Random;
public class ValueNoise {
private int seed;
private Random random;
public ValueNoise(int seed) {
this.seed = seed;
random = new Random(seed);
}
// Smoothstep function for interpolation
private double smoothstep(double t) {
return t * t * (3 - 2 * t);
}
// Linear interpolation
private double lerp(double a, double b, double t) {
return a + t * (b - a);
}
// Generate random value at given coordinates
private double randomValue(int x, int y) {
random.setSeed(x * 49632 + y * 325176 + seed);
return random.nextDouble();
}
// Interpolate between surrounding random values
private double interpolate(double x, double y, double tx, double ty) {
int x0 = (int) x;
int y0 = (int) y;
double sx = smoothstep(tx);
double sy = smoothstep(ty);
double v00 = randomValue(x0, y0);
double v01 = randomValue(x0, y0 + 1);
double v10 = randomValue(x0 + 1, y0);
double v11 = randomValue(x0 + 1, y0 + 1);
double ix0 = lerp(v00, v10, sx);
double ix1 = lerp(v01, v11, sx);
return lerp(ix0, ix1, sy);
}
// Generate value noise at given coordinates
public double generateNoise(double x, double y) {
int intX = (int) x;
int intY = (int) y;
double fracX = x - intX;
double fracY = y - intY;
return interpolate(intX, intY, fracX, fracY);
}
// public static void main(String[] args) {
// ValueNoise valueNoise = new ValueNoise(123); // Seed = 123
//
// // Example usage:
// double noiseValue = valueNoise.generateNoise(1.5, 2.3);
// System.out.println("Value noise at (1.5, 2.3): " + noiseValue);
// }
}