|
1 | 1 | package mesh.modifier; |
2 | 2 |
|
3 | 3 | import java.util.List; |
| 4 | +import java.util.Random; |
4 | 5 |
|
5 | | -import math.Mathf; |
6 | 6 | import math.Vector3f; |
7 | 7 | import mesh.Mesh3D; |
8 | 8 | import mesh.util.VertexNormals; |
9 | 9 |
|
10 | 10 | public class NoiseModifier implements IMeshModifier { |
11 | 11 |
|
12 | | - private float minimum; |
| 12 | + private float minimum; |
13 | 13 |
|
14 | | - private float maximum; |
| 14 | + private float maximum; |
15 | 15 |
|
16 | | - private Mesh3D mesh; |
| 16 | + private long seed; |
17 | 17 |
|
18 | | - private List<Vector3f> vertexNormals; |
| 18 | + private Random random; |
19 | 19 |
|
20 | | - public NoiseModifier() { |
21 | | - this(0.0f, 1.0f); |
22 | | - } |
| 20 | + private Mesh3D mesh; |
23 | 21 |
|
24 | | - public NoiseModifier(float minimum, float maximum) { |
25 | | - this.minimum = minimum; |
26 | | - this.maximum = maximum; |
27 | | - } |
| 22 | + private List<Vector3f> vertexNormals; |
28 | 23 |
|
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 | + } |
36 | 27 |
|
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 | + } |
45 | 32 |
|
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 | + } |
49 | 41 |
|
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 | + } |
53 | 50 |
|
54 | | - private int getVertexCount() { |
55 | | - return mesh.vertices.size(); |
56 | | - } |
| 51 | + private Vector3f getVertexAt(int index) { |
| 52 | + return mesh.getVertexAt(index); |
| 53 | + } |
57 | 54 |
|
58 | | - private void calculateVertexNormals() { |
59 | | - vertexNormals = new VertexNormals(mesh).getVertexNormals(); |
60 | | - } |
| 55 | + private Vector3f getVertexNormalAt(int index) { |
| 56 | + return vertexNormals.get(index); |
| 57 | + } |
61 | 58 |
|
62 | | - private float createRandomValue() { |
63 | | - return Mathf.random(minimum, maximum); |
64 | | - } |
| 59 | + private int getVertexCount() { |
| 60 | + return mesh.vertices.size(); |
| 61 | + } |
65 | 62 |
|
66 | | - private void setMesh(Mesh3D mesh) { |
67 | | - this.mesh = mesh; |
68 | | - } |
| 63 | + private void calculateVertexNormals() { |
| 64 | + vertexNormals = new VertexNormals(mesh).getVertexNormals(); |
| 65 | + } |
69 | 66 |
|
70 | | - public float getMinimum() { |
71 | | - return minimum; |
72 | | - } |
| 67 | + private float createRandomValue() { |
| 68 | + return minimum + random.nextFloat() * (maximum - minimum); |
| 69 | + } |
73 | 70 |
|
74 | | - public void setMinimum(float minimum) { |
75 | | - this.minimum = minimum; |
76 | | - } |
| 71 | + private void setMesh(Mesh3D mesh) { |
| 72 | + this.mesh = mesh; |
| 73 | + } |
77 | 74 |
|
78 | | - public float getMaximum() { |
79 | | - return maximum; |
80 | | - } |
| 75 | + public float getMinimum() { |
| 76 | + return minimum; |
| 77 | + } |
81 | 78 |
|
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 | + } |
85 | 98 |
|
86 | 99 | } |
0 commit comments