summaryrefslogtreecommitdiff
path: root/Runtime/Terrain/TreeDatabase.h
blob: 6f07e8c828972a3085fee4a9e16112a7cfbd3b9d (plain)
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
#pragma once
#include "Configuration/UnityConfigure.h"

#if ENABLE_TERRAIN

#include "Runtime/BaseClasses/BaseObject.h"
#include "Runtime/Scripting/ScriptingUtility.h"
#include "Runtime/Geometry/AABB.h"
#include "Runtime/Filters/Mesh/LodMesh.h"

#include <vector>

class Vector2f;
class ColorRGBAf;
class TerrainData;
struct TreeInstance;

namespace Unity
{
	class GameObject;
	class Material;
}

struct TreeInstance
{
	DECLARE_SERIALIZE (TreeInstance)
	
	Vector3f		position;
	float			widthScale;
	float			heightScale;
	ColorRGBA32		color;
	ColorRGBA32		lightmapColor;
	int				index;
	float			temporaryDistance;
};

template<class TransferFunc>
void TreeInstance::Transfer (TransferFunc& transfer)
{
	TRANSFER (position);
	TRANSFER (widthScale);
	TRANSFER (heightScale);
	TRANSFER (color);
	TRANSFER (lightmapColor);
	TRANSFER (index);
}

struct TreePrototype
{
	DECLARE_SERIALIZE (TreePrototype)
	
	PPtr<GameObject> prefab;
	float      bendFactor;
	
	TreePrototype ()
	{
		bendFactor = 1.0F;
	}
};


struct MonoTreePrototype {
	ScriptingObjectPtr prefab;
	float bendFactor;
};
void TreePrototypeToMono (const TreePrototype &src, MonoTreePrototype &dest) ;
void TreePrototypeToCpp (MonoTreePrototype &src, TreePrototype &dest);

template<class TransferFunc>
void TreePrototype::Transfer (TransferFunc& transfer)
{
	TRANSFER (prefab);
	TRANSFER (bendFactor);
}

class TreeDatabase
{
public:
	class Prototype 
	{
	public:
		Prototype();
		~Prototype();

		void Set (const PPtr<Unity::GameObject>& source, float inBendFactor, const TerrainData& terrainData);
		bool SetMaterial (int index, Material* material);

		// if a tree is more tall than wide then we want to use square billboards, 
		// because tree has to fit into billboard when looking from above (or bellow)
		float getBillboardAspect() const { return std::min<float>(1.f, treeAspectRatio); }
		float getBillboardHeight() const { return std::max<float>(treeHeight, treeWidth); }

		// offset of center point of the tree from the ground (i.e. from pivot point/root of the tree)
		float getCenterOffset() const { return treeVisibleHeight - treeHeight / 2; }

	public:
		PPtr<Unity::GameObject> prefab;
		PPtr<Mesh> mesh;

		std::vector<float> inverseAlphaCutoff;
		std::vector<Material*> materials;
		std::vector<ColorRGBAf> originalMaterialColors;
		std::vector<Material*> imposterMaterials;

		// actual tree height from the bottom to the top
		float treeHeight;
		// visible tree height, i.e. the part which is above terrain (i.e. from pivot point/root of the tree to the top)
		float treeVisibleHeight;
		// The width of the tree
		float treeWidth;
		// How wide is the tree relative to the height. (Usually less than 1)
		float treeAspectRatio;
		AABB bounds;
		float bendFactor;
	};

	typedef std::vector<Prototype> PrototypeVector;

public:
	TreeDatabase (TerrainData& source);
		
	void AddTree (const TreeInstance& tree);
	int RemoveTrees (const Vector2f& position, float radius, int prototypeIndex);
	
	TerrainData& GetTerrainData() const { return m_SourceData; }

	PrototypeVector& GetPrototypes() { return m_Prototypes; }
	const std::vector<Prototype>& GetPrototypes() const { return m_Prototypes; }	
	
	
	std::vector<TreeInstance> &GetInstances () { return m_Instances; }
	std::vector<TreePrototype> &GetTreePrototypes () { return m_TreePrototypes; }
	
	void SetTreePrototypes (const std::vector<TreePrototype> &treePrototypes );
	void RefreshPrototypes ();
	void RemoveTreePrototype (int index);
	
	void UpdateTreeInstances ();
	void RecalculateTreePositions ();
	void ValidateTrees ();

private:
	TerrainData& m_SourceData;
	std::vector<TreePrototype>	m_TreePrototypes;
	std::vector<TreeInstance> m_Instances;

	PrototypeVector m_Prototypes;
};

#endif // ENABLE_TERRAIN