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
271
272
273
274
275
276
277
278
279
280
281
282
283
|
#include "UnityPrefix.h"
#include "TreeDatabase.h"
#if ENABLE_TERRAIN
#include "TerrainData.h"
#include "Runtime/Filters/Mesh/LodMeshFilter.h"
#include "Runtime/Shaders/Material.h"
#include "Runtime/Shaders/Shader.h"
#include "Runtime/Filters/Renderer.h"
#include "Runtime/Scripting/Scripting.h"
TreeDatabase::Prototype::Prototype()
: treeHeight (1)
, treeVisibleHeight (1)
, treeWidth (1)
, treeAspectRatio (1)
, bounds (Vector3f::zero, Vector3f::zero)
, bendFactor (0)
{
prefab = NULL;
mesh = NULL;
}
TreeDatabase::Prototype::~Prototype()
{
for (std::vector<Material*>::iterator it = imposterMaterials.begin(), end = imposterMaterials.end(); it != end; ++it)
DestroySingleObject(*it);
for (std::vector<Material*>::iterator it = materials.begin(), end = materials.end(); it != end; ++it)
DestroySingleObject(*it);
}
void TreeDatabase::Prototype::Set(const PPtr<Unity::GameObject>& source, float inBendFactor, const TerrainData& terrainData)
{
prefab = source;
if (!source)
{
ErrorStringObject(
Format(
"A tree couldn't be loaded because the prefab is missing.\nPlease select any instance of the %s asset in a scene and make it reference only tree prefabs that exist.",
terrainData.GetName ()),
&terrainData);
return;
}
MeshFilter* filter = prefab->QueryComponent(MeshFilter);
Renderer* renderer = prefab->QueryComponent(Renderer);
if (!filter || !filter->GetSharedMesh() || !renderer)
{
WarningStringObject(std::string() + "The tree " + source->GetName() + " couldn't be instanced because the prefab contains no valid mesh renderer", source);
return;
}
mesh = filter->GetSharedMesh();
const Renderer::MaterialArray& originalMaterials = renderer->GetMaterialArray();
for (int m = 0; m < originalMaterials.size(); ++m) {
if (!originalMaterials[m])
{
WarningStringObject(std::string() + "The tree " + source->GetName() + " couldn't be instanced because one of the materials is missing", source);
return;
}
}
// * Create cloned imposter materials using special billboard shader
// * Create cloned normal materials so we can modify the color in place
// * Setup orginal colors array
//materials = originalMaterials;
imposterMaterials.resize(originalMaterials.size());
materials.resize(originalMaterials.size());
originalMaterialColors.resize(originalMaterials.size());
inverseAlphaCutoff.resize(originalMaterials.size());
for (int m = 0; m < originalMaterials.size(); m++)
{
if (!SetMaterial (m, originalMaterials[m]))
{
WarningStringObject(std::string() + "The tree " + source->GetName() + " must use the Nature/Soft Occlusion shader. Otherwise billboarding/lighting will not work correctly.", source);
}
}
bounds = mesh->GetBounds();
MinMaxAABB minMaxBounds(bounds);
treeVisibleHeight = minMaxBounds.GetMax().y;
treeHeight = bounds.GetExtent().y * 2;
float x = std::max(std::abs(minMaxBounds.GetMin().x), std::abs(minMaxBounds.GetMax().x));
float z = std::max(std::abs(minMaxBounds.GetMin().z), std::abs(minMaxBounds.GetMax().z));
treeWidth = std::max(x, z) * 1.9F;
treeAspectRatio = treeWidth / treeHeight;
bendFactor = inBendFactor;
}
bool TreeDatabase::Prototype::SetMaterial (int index, Material* material)
{
if (index < 0 || index >= materials.size())
return true;
const ShaderLab::FastPropertyName colorProperty = ShaderLab::Property("_Color");
const ShaderLab::FastPropertyName cutoffProperty = ShaderLab::Property("_Cutoff");
if (material->HasProperty(colorProperty))
originalMaterialColors[index] = material->GetColor(colorProperty);
else
originalMaterialColors[index].Set(1, 1, 1, 1);
inverseAlphaCutoff[index] = 1.0F;
if (material->HasProperty(cutoffProperty))
inverseAlphaCutoff[index] = 0.5F / material->GetFloat(cutoffProperty);
// Instantiate normal material.
if (materials[index])
DestroySingleObject (materials[index]);
materials[index] = Material::CreateMaterial (*material, Object::kHideAndDontSave);
// Instantiate a special billboarding material.
// Eg. leaves shader needs specialized premultiplied alpha rendering into render tex.
if (imposterMaterials[index])
DestroySingleObject (imposterMaterials[index]);
imposterMaterials[index] = Material::CreateMaterial (*material, Object::kHideAndDontSave);
Shader* imposterShader = imposterMaterials[index]->GetShader()->GetDependency ("BillboardShader");
if (!imposterShader)
return false;
imposterMaterials[index]->SetShader(imposterShader);
return true;
}
TreeDatabase::TreeDatabase (TerrainData& source)
: m_SourceData(source)
{
}
void TreeDatabase::RefreshPrototypes ()
{
m_Prototypes.clear();
const vector<TreePrototype>& sourcePrototypes = GetTreePrototypes();
m_Prototypes.resize(sourcePrototypes.size());
for (int i = 0; i < m_Prototypes.size(); ++i)
m_Prototypes[i].Set(sourcePrototypes[i].prefab, sourcePrototypes[i].bendFactor, m_SourceData);
m_SourceData.UpdateUsers (TerrainData::kFlushEverythingImmediately);
}
// TODO: make sure all trees are within bounds.
void TreeDatabase::ValidateTrees ()
{
int prototypeCount = m_TreePrototypes.size();
for (vector<TreeInstance>::iterator i = m_Instances.begin(); i != m_Instances.end(); i++)
{
i->position.x = clamp01(i->position.x);
i->position.y = clamp01(i->position.y);
i->position.z = clamp01(i->position.z);
i->index = clamp(i->index,0,prototypeCount - 1);
}
}
void TreeDatabase::RecalculateTreePositions ()
{
Heightmap *heightmap = &m_SourceData.GetHeightmap();
Vector3f terrainSize = heightmap->GetSize();
for (int i = 0; i < m_Instances.size(); i++)
{
Vector3f pos = m_Instances[i].position;
pos.y = heightmap->GetInterpolatedHeight(pos.x, pos.z) / terrainSize.y;
m_Instances[i].position = pos;
}
UpdateTreeInstances();
}
void TreeDatabase::AddTree (const TreeInstance& tree)
{
m_Instances.push_back(tree);
const Heightmap& heightmap = m_SourceData.GetHeightmap();
float height = heightmap.GetInterpolatedHeight(tree.position.x, tree.position.z);
height /= heightmap.GetSize().y;
m_Instances.back().position.y = height;
UpdateTreeInstances();
}
int TreeDatabase::RemoveTrees (const Vector2f& position, float radius, int prototypeIndex)
{
float sqrRadius = radius * radius;
std::vector<TreeInstance> instances;
instances.reserve(m_Instances.size());
for (int i=0; i<m_Instances.size(); ++i)
{
const TreeInstance& instance = m_Instances[i];
Vector2f offset(instance.position.x - position.x, instance.position.z - position.y);
bool shouldRemovePrototypeIndex = prototypeIndex == instance.index || prototypeIndex == -1;
if (!shouldRemovePrototypeIndex || SqrMagnitude(offset) > sqrRadius)
instances.push_back(instance);
}
int removedTrees = 0;
if (m_Instances.size() != instances.size())
{
removedTrees = m_Instances.size() - instances.size();
m_Instances = instances;
UpdateTreeInstances();
}
return removedTrees;
}
void TreeDatabase::UpdateTreeInstances ()
{
ValidateTrees ();
m_SourceData.SetDirty ();
m_SourceData.UpdateUsers (TerrainData::kTreeInstances);
}
void TreeDatabase::SetTreePrototypes (const vector<TreePrototype> &treePrototypes)
{
m_TreePrototypes = treePrototypes;
ValidateTrees ();
RefreshPrototypes();
m_SourceData.SetDirty ();
}
void TreeDatabase::RemoveTreePrototype (int index)
{
#if UNITY_EDITOR
if( index < 0 || index >= m_TreePrototypes.size() )
{
ErrorString("invalid tree prototype index");
return;
}
// erase tree prototype
m_TreePrototypes.erase( m_TreePrototypes.begin() + index );
// update tree instance indices
for( vector<TreeInstance>::iterator it = m_Instances.begin(); it != m_Instances.end(); /**/ )
{
if( it->index == index )
{
it = m_Instances.erase( it );
}
else
{
if( it->index > index )
it->index--;
++it;
}
}
m_SourceData.SetDirty();
RefreshPrototypes();
#else
ErrorString("only implemented in editor");
#endif
}
void TreePrototypeToMono (const TreePrototype &src, MonoTreePrototype &dest) {
dest.prefab = Scripting::ScriptingWrapperFor (src.prefab);
dest.bendFactor = src.bendFactor;
}
void TreePrototypeToCpp (MonoTreePrototype &src, TreePrototype &dest) {
dest.prefab = ScriptingObjectToObject<GameObject> (src.prefab);
dest.bendFactor = src.bendFactor;
}
#endif // ENABLE_TERRAIN
|