Skip to content

Commit aa7de4b

Browse files
committed
Added seed option to the noise modifier.
1 parent 8485999 commit aa7de4b

File tree

2 files changed

+364
-57
lines changed

2 files changed

+364
-57
lines changed
Lines changed: 70 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,99 @@
11
package mesh.modifier;
22

33
import java.util.List;
4+
import java.util.Random;
45

5-
import math.Mathf;
66
import math.Vector3f;
77
import mesh.Mesh3D;
88
import mesh.util.VertexNormals;
99

1010
public class NoiseModifier implements IMeshModifier {
1111

12-
private float minimum;
12+
private float minimum;
1313

14-
private float maximum;
14+
private float maximum;
1515

16-
private Mesh3D mesh;
16+
private long seed;
1717

18-
private List<Vector3f> vertexNormals;
18+
private Random random;
1919

20-
public NoiseModifier() {
21-
this(0.0f, 1.0f);
22-
}
20+
private Mesh3D mesh;
2321

24-
public NoiseModifier(float minimum, float maximum) {
25-
this.minimum = minimum;
26-
this.maximum = maximum;
27-
}
22+
private List<Vector3f> vertexNormals;
2823

29-
@Override
30-
public Mesh3D modify(Mesh3D mesh) {
31-
setMesh(mesh);
32-
calculateVertexNormals();
33-
applyNoise();
34-
return mesh;
35-
}
24+
public NoiseModifier() {
25+
this(0.0f, 1.0f);
26+
}
3627

37-
private void applyNoise() {
38-
for (int i = 0; i < getVertexCount(); i++) {
39-
float length = createRandomValue();
40-
Vector3f vertex = getVertexAt(i);
41-
Vector3f normal = getVertexNormalAt(i);
42-
vertex.addLocal(normal.mult(length));
43-
}
44-
}
28+
public NoiseModifier(float minimum, float maximum) {
29+
this.minimum = minimum;
30+
this.maximum = maximum;
31+
}
4532

46-
private Vector3f getVertexAt(int index) {
47-
return mesh.getVertexAt(index);
48-
}
33+
@Override
34+
public Mesh3D modify(Mesh3D mesh) {
35+
random = new Random(seed);
36+
setMesh(mesh);
37+
calculateVertexNormals();
38+
applyNoise();
39+
return mesh;
40+
}
4941

50-
private Vector3f getVertexNormalAt(int index) {
51-
return vertexNormals.get(index);
52-
}
42+
private void applyNoise() {
43+
for (int i = 0; i < getVertexCount(); i++) {
44+
float length = createRandomValue();
45+
Vector3f vertex = getVertexAt(i);
46+
Vector3f normal = getVertexNormalAt(i);
47+
vertex.addLocal(normal.mult(length));
48+
}
49+
}
5350

54-
private int getVertexCount() {
55-
return mesh.vertices.size();
56-
}
51+
private Vector3f getVertexAt(int index) {
52+
return mesh.getVertexAt(index);
53+
}
5754

58-
private void calculateVertexNormals() {
59-
vertexNormals = new VertexNormals(mesh).getVertexNormals();
60-
}
55+
private Vector3f getVertexNormalAt(int index) {
56+
return vertexNormals.get(index);
57+
}
6158

62-
private float createRandomValue() {
63-
return Mathf.random(minimum, maximum);
64-
}
59+
private int getVertexCount() {
60+
return mesh.vertices.size();
61+
}
6562

66-
private void setMesh(Mesh3D mesh) {
67-
this.mesh = mesh;
68-
}
63+
private void calculateVertexNormals() {
64+
vertexNormals = new VertexNormals(mesh).getVertexNormals();
65+
}
6966

70-
public float getMinimum() {
71-
return minimum;
72-
}
67+
private float createRandomValue() {
68+
return minimum + random.nextFloat() * (maximum - minimum);
69+
}
7370

74-
public void setMinimum(float minimum) {
75-
this.minimum = minimum;
76-
}
71+
private void setMesh(Mesh3D mesh) {
72+
this.mesh = mesh;
73+
}
7774

78-
public float getMaximum() {
79-
return maximum;
80-
}
75+
public float getMinimum() {
76+
return minimum;
77+
}
8178

82-
public void setMaximum(float maximum) {
83-
this.maximum = maximum;
84-
}
79+
public void setMinimum(float minimum) {
80+
this.minimum = minimum;
81+
}
82+
83+
public float getMaximum() {
84+
return maximum;
85+
}
86+
87+
public void setMaximum(float maximum) {
88+
this.maximum = maximum;
89+
}
90+
91+
public long getSeed() {
92+
return seed;
93+
}
94+
95+
public void setSeed(long seed) {
96+
this.seed = seed;
97+
}
8598

8699
}

0 commit comments

Comments
 (0)