载入中...
搜索中...
未找到
GrassBillboards.cpp
浏览该文件的文档.
1#include <algorithm>
2#include <cmath>
3#include "graphics/Grass.h"
4namespace eve::graphics::grass {
5namespace {
6float encodedIdentity(const Point &point) {
7 if (point.tint.x < 0) return float(point.id);
8 const auto channel = [](float value) { return uint32_t(std::lround(std::clamp(value, 0.f, 1.f) * 255.f)); };
9 const uint32_t rgb = (channel(point.tint.r) << 16) | (channel(point.tint.g) << 8) | channel(point.tint.b);
10 return -float(rgb + 1u);
11}
12} // namespace
13BillboardMesh buildBillboards(const std::vector<Point> &points, float width, float height, bool alwaysDark) {
14 (void)width;
15 (void)height;
17 const size_t n = points.size();
18 mesh.posXYZ.reserve(n * 4 * 3);
19 mesh.nrmXYZ.reserve(n * 4 * 3);
20 mesh.uvST.reserve(n * 4 * 2);
21 mesh.indices.reserve(n * 6);
22
23 // Local corners: (0,0) bottom-left, (1,0) bottom-right, (1,1) top-right, (0,1) top-left.
24 // Root is the bottom-center of the rectangle, i.e. UV (0.5, 0).
25 const float cu[4] = {0.f, 1.f, 1.f, 0.f};
26 const float cv[4] = {0.f, 0.f, 1.f, 1.f};
27 const uint32_t corners[6] = {0, 1, 2, 0, 2, 3};
28
29 for (size_t i = 0; i < n; ++i) {
30 const Point &p = points[i];
31 const uint32_t base = uint32_t(i * 4);
32 const float identity = encodedIdentity(p);
33 for (int c = 0; c < 4; ++c) {
34 mesh.posXYZ.push_back(p.position.x);
35 mesh.posXYZ.push_back(p.position.y);
36 mesh.posXYZ.push_back(p.position.z);
37 mesh.nrmXYZ.push_back(identity);
38 mesh.nrmXYZ.push_back(p.scale > 1e-3f ? p.scale : 1.f);
39 mesh.nrmXYZ.push_back(p.widthScale > 0.f ? (alwaysDark ? 1.f + p.widthScale : -p.widthScale)
40 : (alwaysDark ? 1.f : 0.f));
41 mesh.uvST.push_back(cu[c]);
42 mesh.uvST.push_back(cv[c]);
43 }
44 for (uint32_t k : corners) mesh.indices.push_back(base + k);
45 }
46 return mesh;
47}
48
49} // namespace eve::graphics::grass
double value
glm::vec4 p[6]
glm::vec3 n
Definition Grass.cpp:63
std::int32_t c
std::uint32_t height
std::uint32_t width
std::shared_ptr< const std::vector< glm::vec2 > > points
Mesh * mesh
glm::vec3 point
t3ssel8r-style stylized grass.
Definition Grass.cpp:28
EVENGINE_API_BACKENDS BillboardMesh buildBillboards(const std::vector< Point > &points, float width=0.62f, float height=0.95f, bool alwaysDark=false)
Expand a unit rectangle per point. Vertex layout: pos = grass root uv = quad corner in [0,...
BillboardMesh public API.
Definition Grass.h:95
std::vector< float > posXYZ
Definition Grass.h:96
Point public API.
Definition Grass.h:74