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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
#pragma once
#include "Configuration/UnityConfigure.h"
#if ENABLE_TERRAIN
#include "Runtime/Geometry/AABB.h"
#include "Runtime/BaseClasses/GameObject.h"
#include "Runtime/Math/Color.h"
#include "Runtime/Graphics/Texture2D.h"
#include "Runtime/Math/Random/rand.h"
#include "Runtime/Math/Rect.h"
#include <vector>
#include "TreeDatabase.h"
class TerrainData;
class Heightmap;
class Mesh;
using std::vector;
struct DetailPatch
{
public:
DECLARE_SERIALIZE (DetailPatch)
AABB bounds;
bool dirty;
vector<UInt8> layerIndices;
vector<UInt8> numberOfObjects;
DetailPatch () { dirty = false; }
};
template<class TransferFunc>
void DetailPatch::Transfer (TransferFunc& transfer)
{
TRANSFER (bounds);
TRANSFER (layerIndices);
TRANSFER (numberOfObjects);
}
enum DetailRenderMode
{
kDetailBillboard = 0, // billboard
kDetailMeshLit, // just a mesh, lit like everything else
kDetailMeshGrass, // mesh (user supplied or generated grass crosses), waves in the wind
kDetailRenderModeCount
};
struct DetailPrototype
{
DECLARE_SERIALIZE (DetailPrototype)
PPtr<GameObject> prototype;
PPtr<Texture2D> prototypeTexture;
float minWidth, maxWidth; ///< Width of the grass billboards (if renderMode is grassBillboard)
float minHeight, maxHeight; ///< Height of the grass billboards (if renderMode is grassBillboard)
float noiseSpread;
float bendFactor;
ColorRGBAf healthyColor;
ColorRGBAf dryColor;
float lightmapFactor;
int renderMode;
int usePrototypeMesh;
vector<Vector3f> vertices;
vector<Vector3f> normals;
vector<Vector2f> uvs;
vector<ColorRGBA32> colors;
vector<UInt16> triangles;
DetailPrototype () :
healthyColor (67/255.0F, 249/255.0F, 42/255.0F, 1 ),
dryColor(205/255.0F, 188/255.0F, 26/255.0F, 1.0F )
{
minWidth = 1.0F;
maxWidth = 2.0F;
minHeight = 1.0F;
maxHeight = 2.0F;
noiseSpread = 10.0F;
bendFactor = 1.0F;
lightmapFactor = 1.0F;
renderMode = kDetailMeshGrass;
usePrototypeMesh = false;
}
};
template<class TransferFunc>
void DetailPrototype::Transfer (TransferFunc& transfer)
{
transfer.SetVersion(2);
TRANSFER (prototype);
TRANSFER (prototypeTexture);
TRANSFER (minWidth);
TRANSFER (maxWidth);
TRANSFER (minHeight);
TRANSFER (maxHeight);
TRANSFER (noiseSpread);
TRANSFER (bendFactor);
TRANSFER (healthyColor);
TRANSFER (dryColor);
TRANSFER (lightmapFactor);
TRANSFER (renderMode);
TRANSFER (usePrototypeMesh);
if (transfer.IsOldVersion(1))
{
if (prototype)
usePrototypeMesh = 1;
else
usePrototypeMesh = 0;
}
}
class DetailDatabase
{
public:
DetailDatabase (TerrainData* terrainData, TreeDatabase* treeDatabase);
~DetailDatabase ();
DECLARE_SERIALIZE (DetailDatabase)
Texture2D* GetAtlasTexture () { return m_AtlasTexture; }
GET_SET (ColorRGBAf, WavingGrassTint, m_WavingGrassTint);
GET_SET (float, WavingGrassStrength, m_WavingGrassStrength);
GET_SET (float, WavingGrassAmount, m_WavingGrassAmount);
GET_SET (float, WavingGrassSpeed, m_WavingGrassSpeed);
int GetPatchCount () const { return m_PatchCount; }
const vector<DetailPrototype> &GetDetailPrototypes () const { return m_DetailPrototypes; }
void SetDetailPrototypes (const vector<DetailPrototype> &treePrototypes );
void RemoveDetailPrototype (int index);
void SetDetailResolution (int resolution, int resolutionPerPatch);
void ResetDirtyDetails ();
void SetDetailPrototypesDirty ();
void UpdateDetailPrototypesIfDirty ();
void GenerateTextureAtlasThreaded ();
void SetDirty ();
int GetWidth () const { return GetResolution (); }
int GetHeight () const { return GetResolution (); }
int GetResolution () const { return m_PatchSamples * m_PatchCount; }
int GetResolutionPerPatch () const { return m_PatchSamples; }
int GetSupportedLayers (int xBase, int yBase, int totalWidth, int totalHeight, int *buffer) const;
void GetLayer (int xBase, int yBase, int totalWidth, int totalHeight, int detailIndex, int *buffer) const;
void SetLayer (int xBase, int yBase, int totalWidth, int totalHeight, int detailIndex, const int *buffer);
int GetIndex (int x, int y, int l) const
{
int res = m_PatchSamples;
int nbIndex = y * res + x + l * res * res;
return nbIndex;
}
void RefreshPrototypes ();
Rectf GetNormalizedArea (int x, int y) const
{
float fx = (float)x / m_PatchCount;
float fy = (float)y / m_PatchCount;
float size = 1.0F / m_PatchCount;
return Rectf (fx, fy, size, size);
}
// void GenerateBounds (DetailPatch &patch, int patchX, int patchY);
Mesh* BuildMesh (int patchX, int patchY, Vector3f size, int lightmapIndex, DetailRenderMode renderMode, float density);
bool IsPatchEmpty (int x, int y) const;
bool IsPatchDirty (int x, int y) const;
private:
void GenerateMesh (Mesh& mesh, int patchX, int patchY, Vector3f size, int lightmapIndex, DetailRenderMode renderMode, float density, int totalVertexCount, int totalTriangleCount);
#if UNITY_EDITOR
void SetupPreloadTextureAtlasData ();
#endif
void RefreshPrototypesStep1 (Texture2D** sourceTextures);
void CleanupPrototype (DetailPrototype &proto, string const& error);
void ComputeVertexAndTriangleCount(DetailPatch &patch, DetailRenderMode renderMode, float density, int* vertexCount, int* triangleCount);
static int GetActualResolution (int resolution, int heightmapResolution);
int AddLayerIndex (int detailIndex, DetailPatch &patch);
void RemoveLocalLayerIndex (int detailIndex, DetailPatch& patch);
const DetailPatch& GetPatch (int x, int y) const { return m_Patches[y * m_PatchCount + x]; }
DetailPatch& GetPatch (int x, int y) { return m_Patches[y * m_PatchCount + x]; }
bool m_IsPrototypesDirty;
vector<DetailPatch> m_Patches;
vector<DetailPrototype> m_DetailPrototypes;
TerrainData* m_TerrainData;
TreeDatabase* m_TreeDatabase;
int m_PatchCount;
int m_PatchSamples;
vector<Vector3f> m_RandomRotations;
Texture2D* m_AtlasTexture;
ColorRGBAf m_WavingGrassTint;
float m_WavingGrassStrength;
float m_WavingGrassAmount;
float m_WavingGrassSpeed;
Rand m_Random;
vector<PPtr<Texture2D> > m_PreloadTextureAtlasData;
vector<Rectf> m_PreloadTextureAtlasUVLayout;
};
template<class TransferFunc>
void DetailDatabase::Transfer (TransferFunc& transfer)
{
TRANSFER (m_Patches);
TRANSFER (m_DetailPrototypes);
TRANSFER (m_PatchCount);
TRANSFER (m_PatchSamples);
TRANSFER (m_RandomRotations);
transfer.Transfer( m_WavingGrassTint, "WavingGrassTint" );
TRANSFER (m_WavingGrassStrength);
TRANSFER (m_WavingGrassAmount);
TRANSFER (m_WavingGrassSpeed);
transfer.Transfer (m_TreeDatabase->GetInstances(), "m_TreeInstances");
transfer.Transfer (m_TreeDatabase->GetTreePrototypes(), "m_TreePrototypes");
#if UNITY_EDITOR
if (transfer.IsBuildingTargetPlatform(kBuildAnyPlayerData))
{
SetupPreloadTextureAtlasData();
TRANSFER (m_PreloadTextureAtlasData);
m_PreloadTextureAtlasData.clear();
}
else
#endif
{
TRANSFER (m_PreloadTextureAtlasData);
}
}
struct MonoDetailPrototype {
ScriptingObjectPtr prototype;
ScriptingObjectPtr prototypeTexture;
ColorRGBAf healthyColor;
ColorRGBAf dryColor;
float minWidth, maxWidth;
float minHeight, maxHeight;
float noiseSpread;
float bendFactor;
int renderMode;
int usePrototypeMesh;
};
void DetailPrototypeToMono (const DetailPrototype &src, MonoDetailPrototype &dest);
void DetailPrototypeToCpp (MonoDetailPrototype &src, DetailPrototype &dest) ;
#endif // ENABLE_TERRAIN
|