1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * FXGL - JavaFX Game Library
5
+ *
6
+ * Copyright (c) 2015-2017 AlmasB ([email protected] )
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ * of this software and associated documentation files (the "Software"), to deal
10
+ * in the Software without restriction, including without limitation the rights
11
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ * copies of the Software, and to permit persons to whom the Software is
13
+ * furnished to do so, subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in
16
+ * all copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ * SOFTWARE.
25
+ */
26
+
27
+ package com.almasb.fxgl.core.math
28
+
29
+ /* *
30
+ * Adapted from https://github.com/CRYTEK/CRYENGINE/blob/release/Code/CryEngine/CryCommon/CryMath/PNoise3.h
31
+ *
32
+ * @author Almas Baimagambetov ([email protected] )
33
+ */
34
+ internal object PerlinNoiseGenerator {
35
+
36
+ // from CRYtek
37
+ private val NOISE_TABLE_SIZE = 256
38
+ private val NOISE_MASK = 255
39
+
40
+ private val gx = FloatArray (NOISE_TABLE_SIZE )
41
+ private val gy = FloatArray (NOISE_TABLE_SIZE )
42
+
43
+ init {
44
+ setSeedAndReinitialize()
45
+ }
46
+
47
+ private fun setSeedAndReinitialize () {
48
+ // Generate the gradient lookup tables
49
+ for (i in 0 .. NOISE_TABLE_SIZE - 1 ) {
50
+ // Ken Perlin proposes that the gradients are taken from the unit
51
+ // circle/sphere for 2D/3D.
52
+ // So lets generate a good pseudo-random vector and normalize it
53
+
54
+ val v = Vec2 ()
55
+ // random is in the 0..1 range, so we bring to -0.5..0.5
56
+ v.x = FXGLMath .random() - 0.5f
57
+ v.y = FXGLMath .random() - 0.5f
58
+ v.normalizeLocal()
59
+
60
+ gx[i] = v.x
61
+ gy[i] = v.y
62
+ }
63
+ }
64
+
65
+ /* *
66
+ * Generates a value in [-0.5..0.5), t > 0.
67
+ */
68
+ fun noise1D (t : Float ): Float {
69
+ // Compute what gradients to use
70
+ var qx0 = Math .floor(t.toDouble()).toInt()
71
+ var qx1 = qx0 + 1
72
+ val tx0 = t - qx0
73
+ val tx1 = tx0 - 1
74
+
75
+ // Make sure we don't come outside the lookup table
76
+ qx0 = qx0 and NOISE_MASK
77
+ qx1 = qx1 and NOISE_MASK
78
+
79
+ // Compute the dotproduct between the vectors and the gradients
80
+ val v0 = gx[qx0] * tx0
81
+ val v1 = gx[qx1] * tx1
82
+
83
+ // Modulate with the weight function
84
+ val wx = (3 - 2 * tx0) * tx0 * tx0
85
+ return v0 - wx * (v0 - v1)
86
+ }
87
+ }
0 commit comments