Repository navigation
Expand file tree
/
Copy pathTreeComponent.cpp
More file actions
187 lines (170 loc) · 6.28 KB
/
Copy pathTreeComponent.cpp
File metadata and controls
187 lines (170 loc) · 6.28 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
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
#include "TreeComponent.h"
#include "deki-3d/Mesh3DBackend.h" // NextRevision
#include <algorithm>
namespace DekiTrees
{
namespace
{
// The parts of a grown tree as they are drawn: the wood, the leaves, and the
// far leaves, with the leaves' material.
constexpr int kWood = 0;
constexpr int kLeaves = 1;
constexpr int kFarLeaves = 2;
constexpr int kParts = 3;
// A tree this many pixels across or more draws its leaves; one this few or
// fewer draws its far leaves instead; between, a share of each. A cluster is
// about an eighth of its tree across, so they change while a few pixels each.
constexpr float kLeavesAcross = 260.0f;
constexpr float kFarLeavesAcross = 70.0f;
// Its twigs go when most of its leaves are the far ones: they are a pixel thin by then.
constexpr float kTwigsUntil = 0.35f;
TreeColor ToTreeColor(const Deki::Color& color)
{
return TreeColor{ color.r, color.g, color.b };
}
/// The leaves' texture, painted once and shared by every tree.
const Deki3D::Texture3D* LeafTexture()
{
static Deki::Buffer<uint8_t> s_Pixels;
static Deki3D::Texture3D s_Texture;
if (!s_Texture.Valid() && PaintLeafTexture(s_Pixels))
{
uint8_t shift = 0;
while ((1 << shift) < kLeafTextureSize)
{
++shift;
}
s_Texture.pixels = s_Pixels.Data();
s_Texture.format = Deki3D::TexelFormat::ALPHA8;
s_Texture.width = static_cast<uint16_t>(kLeafTextureSize);
s_Texture.height = static_cast<uint16_t>(kLeafTextureSize);
s_Texture.widthShift = shift;
s_Texture.heightShift = shift;
s_Texture.hasAlpha = true;
s_Texture.filter = Deki::TextureFilter::Bilinear;
s_Texture.revision = Deki3D::NextRevision();
}
return s_Texture.Valid() ? &s_Texture : nullptr;
}
} // namespace
TreeSpec TreeComponent::Spec() const
{
// A tree of one's own starts from the oak, whose other numbers it keeps.
TreeSpec spec = PresetSpec(preset == TreePreset::Custom ? TreePreset::Oak : preset);
if (preset == TreePreset::Custom)
{
spec.shape = shape;
spec.scale = size;
spec.baseSize = bareTrunk;
spec.ratio = thickness;
spec.flare = flare;
spec.baseSplits = baseForks;
spec.levels = twigs ? 3 : 2;
spec.attractionUp = turnUp;
spec.level[0].length = trunkLength;
spec.level[0].curveV = trunkWander;
spec.level[0].segSplits = trunkForks;
spec.level[0].splitAngle = trunkForkAngle;
spec.level[0].curveRes = trunkSegments;
spec.level[1].branches = branchCount;
spec.level[1].length = branchLength;
spec.level[1].downAngle = branchAngle;
spec.level[1].downAngleV = branchAngleChange;
spec.level[1].rotate = branchRound;
spec.level[1].curve = branchBend;
spec.level[1].curveBack = branchBendBack;
spec.level[1].curveV = branchWander;
spec.level[1].segSplits = branchForks;
spec.level[1].curveRes = branchSegments;
spec.level[2].branches = twigCount;
spec.level[2].length = twigLength;
spec.level[2].downAngle = twigAngle;
spec.level[2].rotate = twigRound;
spec.level[2].curveV = twigWander;
spec.leafLay = leafLay;
spec.leaves = leaves;
spec.leafSize = leafSize;
spec.masses = masses;
spec.hull = hull;
spec.leafDark = ToTreeColor(leafDark);
spec.leafLight = ToTreeColor(leafLight);
spec.bark = ToTreeColor(bark);
}
spec.seed = seed;
spec.detail = detail;
return spec;
}
void TreeComponent::Refresh()
{
const TreeSpec spec = Spec();
if (m_Grown && spec == m_GrownSpec)
{
return;
}
m_GrownSpec = spec;
m_Grown = true;
m_Mesh = Deki3D::Mesh3D{};
if (!GrowTree(spec, m_Tree))
{
return;
}
const uint32_t counts[kParts] = { m_Tree.woodIndices, m_Tree.leafIndices, m_Tree.farLeafIndices };
uint32_t first = 0;
for (int part = 0; part < kParts; ++part)
{
m_Submeshes[part] = Deki3D::Submesh3D{};
m_Submeshes[part].firstIndex = first;
m_Submeshes[part].indexCount = counts[part];
m_Submeshes[part].materialIndex = static_cast<uint16_t>(std::min(part, kLeaves));
first += counts[part];
}
for (Deki3D::Material3D& material : m_Materials)
{
material = Deki3D::Material3D{};
}
// The leaves are cards cut out by the leaves' texture, seen from both sides.
m_Materials[kLeaves].texture = LeafTexture();
m_Materials[kLeaves].alphaTest = true;
m_Materials[kLeaves].doubleSided = true;
m_Mesh.vertices = m_Tree.vertices.Data();
m_Mesh.vertexCount = m_Tree.VertexCount();
m_Mesh.layout = Deki3D::LayoutPNTC();
m_Mesh.indices = m_Tree.indices.Data();
m_Mesh.indexCount = m_Tree.IndexCount();
m_Mesh.indexBytes = 2;
m_Mesh.submeshes = m_Submeshes;
m_Mesh.submeshCount = kParts;
m_Mesh.boundsMin = m_Tree.boundsMin;
m_Mesh.boundsMax = m_Tree.boundsMax;
m_Mesh.revision = Deki3D::NextRevision();
}
const Deki3D::Mesh3D* TreeComponent::ProvideMesh()
{
Refresh();
m_Submeshes[kWood].indexCount = m_Tree.woodIndices;
m_Submeshes[kLeaves].indexCount = m_Tree.leafIndices;
m_Submeshes[kFarLeaves].indexCount = 0;
return m_Mesh.vertexCount > 0 ? &m_Mesh : nullptr;
}
const Deki3D::Mesh3D* TreeComponent::ProvideMeshAt(float pixelsAcross)
{
Refresh();
// Both sets of leaves are in no order of place, so the first cards of each thin it evenly.
const float near = std::clamp((pixelsAcross - kFarLeavesAcross) / (kLeavesAcross - kFarLeavesAcross), 0.0f, 1.0f);
const auto cards = [](uint32_t indices, float share)
{
const float count = static_cast<float>(indices / kLeafCardIndices);
return static_cast<uint32_t>(count * share + 0.5f) * kLeafCardIndices;
};
m_Submeshes[kWood].indexCount = near > kTwigsUntil ? m_Tree.woodIndices : m_Tree.limbIndices;
m_Submeshes[kLeaves].indexCount = cards(m_Tree.leafIndices, near);
m_Submeshes[kFarLeaves].indexCount = cards(m_Tree.farLeafIndices, 1.0f - near);
return m_Mesh.vertexCount > 0 ? &m_Mesh : nullptr;
}
const Deki3D::Material3D* TreeComponent::ProvideMaterials(uint16_t& outCount)
{
Refresh();
outCount = m_Mesh.vertexCount > 0 ? 2 : 0;
return m_Materials;
}
} // namespace DekiTrees