-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
142 lines (123 loc) · 4.13 KB
/
main.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
140
141
142
package main
import (
"image"
"image/color"
"image/jpeg"
"math"
"math/rand"
"os"
"github.com/EliCDavis/polyform/drawing/coloring"
"github.com/EliCDavis/polyform/formats/obj"
"github.com/EliCDavis/polyform/math/noise"
"github.com/EliCDavis/polyform/math/sample"
"github.com/EliCDavis/polyform/modeling"
"github.com/EliCDavis/polyform/modeling/triangulation"
"github.com/EliCDavis/vector/vector2"
"github.com/EliCDavis/vector/vector3"
)
func sigmoid(xScale, x, xShift, yShift float64) float64 {
denominator := 1 + math.Pow(math.E, -xScale*(x-xShift))
return (-1 / denominator) + yShift
}
func Texture(textureSize int, mapSize, height, waterLevel float64, name string, landNoise, waterNoise sample.Vec2ToFloat, landColors, waterColors coloring.ColorStack) {
tex := image.NewRGBA(image.Rect(0, 0, textureSize, textureSize))
scaleFactor := mapSize / float64(textureSize)
for x := 0; x < textureSize; x++ {
for y := 0; y < textureSize; y++ {
samplePos := vector2.New(float64(x), float64(y)).Scale(scaleFactor)
sample := landNoise(samplePos)
if sample <= waterLevel {
tex.Set(x, y, waterColors.LinearSample(waterNoise(samplePos)))
} else {
tex.Set(x, y, landColors.LinearSample((sample-waterLevel)/(height-waterLevel)))
}
}
}
texOut, err := os.Create(name)
if err != nil {
panic(err)
}
defer texOut.Close()
err = jpeg.Encode(texOut, tex, &jpeg.Options{Quality: 100})
if err != nil {
panic(err)
}
}
func main() {
n := 5000
mapSize := 3000.
mapRadius := mapSize / 2
mapOffset := vector2.New(mapRadius, mapRadius)
totalHeight := 200.
waterLevel := 15.
points := make([]vector2.Float64, n)
for i := 0; i < n; i++ {
theta := rand.Float64() * 2 * math.Pi
points[i] = vector2.
New(math.Cos(theta), math.Sin(theta)).
Scale(mapRadius * math.Sqrt(rand.Float64())).
Add(mapOffset)
}
perlinStack := noise.PerlinStack(
noise.Stack2DEntry{Scalar: 1 / 300., Amplitude: totalHeight / 2},
noise.Stack2DEntry{Scalar: 1 / 150., Amplitude: totalHeight / 4},
noise.Stack2DEntry{Scalar: 1 / 75., Amplitude: totalHeight / 8},
noise.Stack2DEntry{Scalar: 1 / 37.5, Amplitude: totalHeight / 16},
)
heightFunc := sample.Vec2ToFloat(func(v vector2.Float64) float64 {
rollOff := sigmoid(20, v.Sub(mapOffset).Length()/mapRadius, .5, 1)
return math.Max(perlinStack.Value(v)*rollOff, waterLevel)
})
textureName := "terrain.jpg"
mat := modeling.Material{
Name: "Terrain",
ColorTextureURI: &textureName,
}
Texture(
2048,
mapSize,
totalHeight,
waterLevel,
textureName,
heightFunc,
sample.Vec2ToFloat(noise.PerlinStack(
noise.Stack2DEntry{Scalar: 1 / 300., Amplitude: 1. / 2},
noise.Stack2DEntry{Scalar: 1 / 150., Amplitude: 1. / 4},
noise.Stack2DEntry{Scalar: 1 / 75., Amplitude: 1. / 8},
noise.Stack2DEntry{Scalar: 1 / 37.5, Amplitude: 1. / 16},
).Value),
coloring.NewColorStack(
coloring.NewColorStackEntry(0.1, 0.5, 0.7, color.RGBA{199, 237, 255, 255}), // Water Foam
coloring.NewColorStackEntry(0.5, 0.5, 0.1, color.RGBA{209, 191, 138, 255}), // Sand
coloring.NewColorStackEntry(3, 0.1, 0.5, color.RGBA{59, 120, 65, 255}), // Grass
coloring.NewColorStackEntry(2, 0.5, 0.5, color.RGBA{145, 145, 145, 255}), // Stone
coloring.NewColorStackEntry(2, 0.5, 0.5, color.RGBA{224, 224, 224, 255}), // Mountain Top Snow
),
coloring.NewColorStack(
coloring.NewColorStackEntry(1, 0.8, 0.8, color.RGBA{0, 174, 255, 255}),
coloring.NewColorStackEntry(0.5, 0.8, 0.8, color.RGBA{84, 201, 255, 255}),
),
)
uvs := make([]vector2.Float64, len(points))
terrain := triangulation.
BowyerWatson(points).
ModifyFloat3AttributeParallel(modeling.PositionAttribute, func(i int, v vector3.Float64) vector3.Float64 {
uvs[i] = vector2.New(v.X(), -v.Z()).
DivByConstant(mapSize)
return v.SetY(heightFunc(v.XZ()))
}).
SetFloat2Attribute(modeling.TexCoordAttribute, uvs).
SetMaterial(mat)
objFile, err := os.Create("terrain.obj")
if err != nil {
panic(err)
}
defer objFile.Close()
mtlFile, err := os.Create("terrain.mtl")
if err != nil {
panic(err)
}
defer mtlFile.Close()
obj.WriteMesh(terrain, "terrain.mtl", objFile)
obj.WriteMaterialsFromMesh(terrain, mtlFile)
}