This repository has been archived by the owner on Jan 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathopensimplex_normalized.go
97 lines (83 loc) · 2.56 KB
/
opensimplex_normalized.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package opensimplex
const (
// The normMin and normScale constants are used
// in the formula for normalizing the raw output
// of the OpenSimplex algorithm. They were
// derived from empirical observations of the
// range of raw values. Different constants are
// required for each of Eval2, Eval3, and Eval4.
normMin2 = 0.8659203878240322
normScale2 = 0.577420288914181
normMin3 = 0.9871048542519545
normScale3 = 0.506595297177236
normMin4 = 1.0040848236330158
normScale4 = 0.5007450643319374
)
type normNoise struct {
base Noise
}
// Eval2 returns a random noise value in two dimensions
// in the range [0, 1).
func (s *normNoise) Eval2(x, y float64) float64 {
//return norm2_64(s.base.Eval2(x, y))
r := s.base.Eval2(x, y)
return (r + normMin2) * normScale2
}
// Eval3 returns a random noise value in three dimensions
// in the range [0, 1).
func (s *normNoise) Eval3(x, y, z float64) float64 {
r := s.base.Eval3(x, y, z)
return (r + normMin3) * normScale3
}
// Eval4 returns a random noise value in four dimensions
// in the range [0, 1).
func (s *normNoise) Eval4(x, y, z, t float64) float64 {
r := s.base.Eval4(x, y, z, t)
return (r + normMin4) * normScale4
}
type normNoise32 struct {
base Noise
}
// Eval2 returns a random noise value in two dimensions
// in the range [0, 1).
func (s *normNoise32) Eval2(x, y float32) float32 {
r := s.base.Eval2(float64(x), float64(y))
norm64 := (r + normMin2) * normScale2
norm32 := float32(norm64)
// Empirical testing shows that a simple float32 cast
// from the normalized float64, as above, will sometimes
// produce a value of 1.0.
if norm32 >= 1.0 {
return float32(0.999999)
} else {
return norm32
}
}
// Eval3 returns a random noise value in three dimensions
// in the range [0, 1).
func (s *normNoise32) Eval3(x, y, z float32) float32 {
r := s.base.Eval3(float64(x), float64(y), float64(z))
norm64 := (r + normMin3) * normScale3
norm32 := float32(norm64)
// Unlike Eval2, have not actually tested whether a
// simple float32 cast will produce 1.0, but it seems likely.
if norm32 >= 1.0 {
return float32(0.999999)
} else {
return norm32
}
}
// Eval4 returns a random noise value in four dimensions
// in the range [0, 1).
func (s *normNoise32) Eval4(x, y, z, t float32) float32 {
r := s.base.Eval4(float64(x), float64(y), float64(z), float64(t))
norm64 := (r + normMin4) * normScale4
norm32 := float32(norm64)
// Unlike Eval2, have not actually tested whether a
// simple float32 cast will produce 1.0, but it seems likely.
if norm32 >= 1.0 {
return float32(0.999999)
} else {
return norm32
}
}