forked from icexin/gocraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.go
64 lines (54 loc) · 1.32 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
package main
import (
"math"
"github.com/go-gl/mathgl/mgl32"
opensimplex "github.com/ojrac/opensimplex-go"
)
var (
sim = opensimplex.NewWithSeed(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 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
}