forked from icexin/gocraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.go
82 lines (69 loc) · 1.53 KB
/
math.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
package main
import (
"math"
"github.com/go-gl/mathgl/mgl32"
opensimplex "github.com/ojrac/opensimplex-go"
)
var (
sim = opensimplex.New(0)
)
func abs(x float32) float32 {
return float32(math.Abs(float64(x)))
}
func round(x float32) float32 {
return float32(math.Round(float64(x)))
}
func sin(x float32) float32 {
return float32(math.Sin(float64(x)))
}
func cos(x float32) float32 {
return float32(math.Cos(float64(x)))
}
func radian(angle float32) float32 {
return mgl32.DegToRad(angle)
}
func max(a, b float32) float32 {
if a > b {
return a
}
return b
}
func min(a, b float32) float32 {
if a < b {
return a
}
return b
}
func mix(a, b, factor float32) float32 {
return a*(1-factor) + factor*b
}
func noise2(x, y float32, octaves int, persistence, lacunarity float32) float32 {
var (
freq float32 = 1
amp float32 = 1
max float32 = 1
total = sim.Eval2(float64(x), float64(y))
)
for i := 0; i < octaves; i++ {
freq *= lacunarity
amp *= persistence
max += amp
total += sim.Eval2(float64(x*freq), float64(y*freq)) * float64(amp)
}
return (1 + float32(total)/max) / 2
}
func noise3(x, y, z float32, octaves int, persistence, lacunarity float32) float32 {
var (
freq float32 = 1
amp float32 = 1
max float32 = 1
total = sim.Eval3(float64(x), float64(y), float64(z))
)
for i := 0; i < octaves; i++ {
freq *= lacunarity
amp *= persistence
max += amp
total += sim.Eval3(float64(x*freq), float64(y*freq), float64(z*freq)) * float64(amp)
}
return (1 + float32(total)/max) / 2
}