/
noisemap.go
139 lines (109 loc) · 3.13 KB
/
noisemap.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package noiselib
import (
"math"
)
func MapToCylinder(angle, height float64, source Module) float64 {
var x, y, z float64
x = math.Cos(angle * DegToRad)
y = height
z = math.Sin(angle * DegToRad)
return source.GetValue(x, y, z)
}
func MapToLine(sx, sy, sz, ex, ey, ez, p float64, attenuate bool, source Module) float64 {
x := (ex-sx)*p + sx
y := (ey-sy)*p + sy
z := (ez-sz)*p + sz
value := source.GetValue(x, y, z)
if attenuate {
return p * (1.0 - p) * 4 * value
} else {
return value
}
}
func MapToPlane(x, y float64, source Module) float64 {
return source.GetValue(x, 0, y)
}
func MapToSphere(lat, lon float64, source Module) float64 {
r := math.Cos(DegToRad * lat)
x := r * math.Cos(DegToRad*lon)
y := math.Sin(DegToRad * lat)
z := r * math.Sin(DegToRad*lon)
return source.GetValue(x, y, z)
}
func NoiseMapCylinder(lowerAngleBound, upperAngleBound,
lowerHeightBound, upperHeightBound float64,
width, height int, source Module) [][]float64 {
m := MakeMap(width, height)
angleExtent := upperAngleBound - lowerAngleBound
heightExtent := upperHeightBound - lowerHeightBound
xDelta := angleExtent / float64(width)
yDelta := heightExtent / float64(height)
curAngle := lowerAngleBound
curHeight := lowerHeightBound
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
m[x][y] = MapToCylinder(curAngle, curHeight, source)
curAngle += xDelta
}
curAngle = lowerAngleBound
curHeight += yDelta
}
return m
}
func NoiseMapPlane(lowerXBound, upperXBound, lowerZBound, upperZBound float64,
width, height int, seamless bool, source Module) [][]float64 {
m := MakeMap(width, height)
xExtent := upperXBound - lowerXBound
zExtent := upperZBound - lowerZBound
xDelta := xExtent / float64(width)
zDelta := zExtent / float64(height)
xCur := lowerXBound
zCur := lowerZBound
for z := 0; z < height; z++ {
for x := 0; x < width; x++ {
if seamless {
swValue := MapToPlane(xCur, zCur, source)
seValue := MapToPlane(xCur+xExtent, zCur, source)
nwValue := MapToPlane(xCur, zCur+zExtent, source)
neValue := MapToPlane(xCur+xExtent, zCur+zExtent, source)
xBlend := 1.0 - ((xCur - lowerXBound) / xExtent)
zBlend := 1.0 - ((zCur - lowerZBound) / zExtent)
z0 := LinearInterp(swValue, seValue, xBlend)
z1 := LinearInterp(nwValue, neValue, xBlend)
m[x][z] = LinearInterp(z0, z1, zBlend)
} else {
m[x][z] = MapToPlane(xCur, zCur, source)
}
xCur += xDelta
}
xCur = lowerXBound
zCur += zDelta
}
return m
}
func NoiseMapSphere(eastBound, westBound, northBound, southBound float64,
width, height int, source Module) [][]float64 {
m := MakeMap(width, height)
lonExtent := eastBound - westBound
latExtent := northBound - southBound
xDelta := lonExtent / float64(width)
yDelta := latExtent / float64(height)
curLon := westBound
curLat := southBound
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
m[x][y] = MapToSphere(curLat, curLon, source)
curLon += xDelta
}
curLon = westBound
curLat += yDelta
}
return m
}
func MakeMap(width, height int) [][]float64 {
m := make([][]float64, width)
for i := range m {
m[i] = make([]float64, height)
}
return m
}