-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlanet.cpp
More file actions
162 lines (130 loc) · 4.64 KB
/
Copy pathPlanet.cpp
File metadata and controls
162 lines (130 loc) · 4.64 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "template.h"
#include "scene.h"
#include "Noise.h"
#include "PlanetGen.h"
#include "GameGUI.h"
#include "Planet.h"
static inline Material::ID_T GetWater(Level::Outputs& out)
{
const std::string name = "Water";
return out.m_self->m_materialManager.GetRandomMaterialVariantR(name).m_index;
};
static inline Material::ID_T GetGrass(Level::Outputs& out)
{
const std::string name = "Grass";
return out.m_self->m_materialManager.GetRandomMaterialVariantR(name).m_index;
};
static inline Material::ID_T GetStone(Level::Outputs& out)
{
const std::string name = "Stone";
return out.m_self->m_materialManager.GetRandomMaterialVariantR(name).m_index;
};
static inline Material::ID_T GetSnow(Level::Outputs& out)
{
const std::string name = "Snow";
return out.m_self->m_materialManager.GetRandomMaterialVariantR(name).m_index;
};
static inline constexpr Material::ID_T GetAir()
{
return Material::ID_T(0);
};
void Planet::Create(Scene& scene)
{
Level* planet = scene.AddLevel("Planet");
const float3 center = 0.5f;
planet->AddSphere(center, 0.1f, 2);
planet->AddLightSource(Light(Light::Type::Spot).Intensity(0.4f).Angle(27.f).Softness(0.07f).Radius(0.8f).Position(float3(0)));
planet->Init([](Level* level)
{
level->m_materialManager.New("Air").Color(float3(0, 0, 0)).Use();
for (int i = 0; i < 1; ++i)
{
float3 rgb = float3(Noise::SimpleWhite::GetNext<float>(Noise::Config().MinMax(0.6f, 0.65f)),
Noise::SimpleWhite::GetNext<float>(Noise::Config().MinMax(0.6f, 0.7f)),
Noise::SimpleWhite::GetNext<float>(Noise::Config().MinMax(0.78f, 0.99f)));
level->m_materialManager.New("Water").Color(rgb).Transparency(0.8f).Roughness(0.1f).Reflectivity(0.2f).Density(0.4f).Use();
}
for (int i = 0; i < 5; ++i)
{
float3 rgb = float3(Noise::SimpleWhite::GetNext<float>(Noise::Config().MinMax(0.4f, 0.55f)));
level->m_materialManager.New("Stone").Color(rgb).Use();
}
for (int i = 0; i < 5; ++i)
{
float3 rgb = float3(Noise::SimpleWhite::GetNext<float>(Noise::Config().MinMax(0.4f, 0.7f)),
Noise::SimpleWhite::GetNext<float>(Noise::Config().MinMax(0.53f, 0.84f)),
Noise::SimpleWhite::GetNext<float>(Noise::Config().MinMax(0.24f, 0.75f)));
level->m_materialManager.New("Grass").Color(rgb).Use();
}
for (int i = 0; i < 5; ++i)
{
float3 rgb = float3(Noise::SimpleWhite::GetNext<float>(Noise::Config().MinMax(0.86f, 0.99f)),
Noise::SimpleWhite::GetNext<float>(Noise::Config().MinMax(0.89f, 0.99f)),
Noise::SimpleWhite::GetNext<float>(Noise::Config().MinMax(0.93f, 0.99f)));
level->m_materialManager.New("Snow").Color(rgb).Reflectivity(0.3f).Roughness(0.4f).Use();
}
});
planet->SetupLazyGenerate([](Level::Inputs /*inputs*/, Level::Outputs& outputs)
{
Level::FillBox(outputs, int3(0), int3(WORLDSIZE), [&outputs](int3 /*worldPos*/, int3 /*localPos*/, float3 relPos) -> Material::ID_T
{
PlanetGen params = GameGUI::m_planetGen;
const float3 pos = (relPos - float3(0.5f)) * 2.f;
const float sqrlen = sqrLength(pos);
if (sqrlen < 0.01f) return Material::ID_T(1);
float len = std::sqrtf(sqrlen);
const float3 dir = pos / len;
const float baseRadius = params.radius;
if (len < baseRadius)
{
return GetStone(outputs);
}
float continents = Noise::Perlin::Get3D(
dir.x + 100,
dir.y + 200,
dir.z + 300,
Noise::Config().Scale(0.5f).MinMax(0.f, 1.f).Saturation(3.f)
);
float mountainMask = Noise::Perlin::Get3D(
dir.x,
dir.y,
dir.z,
Noise::Config().Scale(params.mountainsFreq).MinMax(0.f, 1.f).Saturation(params.peaks)
);
float terrain =
baseRadius +
continents * 0.08f +
mountainMask * params.mountainsFac;
float detail = Noise::Perlin::Get3D(
dir.x * 4.f,
dir.y * 4.f,
dir.z * 4.f,
Noise::Config().Scale(params.detail).MinMax(-0.02f, 0.02f)
);
terrain += detail;
float seaLevel = params.seaLevel;
float latitude = dir.y;
float coldness = abs(latitude);
float riverNoise = Noise::Perlin::Get3D(
dir.x * 3.f + 500,
dir.y * 3.f + 200,
dir.z * 3.f + 100,
Noise::Config().Scale(params.riversFreq).MinMax(0.f, 1.f)
);
const float riverWidth = params.riverWidth / 10.f;
const bool isRiver = (riverNoise > 0.5f - riverWidth && riverNoise < 0.5f + riverWidth) && (terrain > seaLevel) && len <= terrain;
const bool isMountain = mountainMask > 0.6f;
if ((len < seaLevel || isRiver) && !isMountain)
return GetWater(outputs);
if (len <= terrain)
{
if (terrain > params.snow && coldness > 0.4f)
return GetSnow(outputs);
if (isMountain)
return GetStone(outputs);
return GetGrass(outputs);
}
return GetAir();
});
});
}