载入中...
搜索中...
未找到
VegetationRender.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cmath>
4#include "graphics/Graphics.h"
5#include "graphics/Material.h"
6#include "graphics/Mesh.h"
9
10namespace eve::graphics {
11namespace {
13 return Diagnostic::error(DiagnosticCode::InvalidArgument, "invalid vegetation render data", {}, {},
14 "graphics.vegetation");
15}
16bool unit(float value) { return std::isfinite(value) && value >= 0 && value <= 1; }
17bool finite(glm::vec4 value) {
18 return std::isfinite(value.x) && std::isfinite(value.y) && std::isfinite(value.z) && std::isfinite(value.w);
19}
20} // namespace
21
23 std::span<const VegetationVertex> vertices, const VegetationMotion& motion,
24 std::span<const float> texcoords, std::span<const uint32_t> indices) {
25 if (vertices.empty() || vertices.size() > 4 * 1024 * 1024 || texcoords.size() != vertices.size() * 2 ||
26 indices.empty() || indices.size() % 3 != 0 || indices.size() > 12 * 1024 * 1024)
27 return Result<void>::failure(invalid());
28 for (auto i : indices)
29 if (i >= vertices.size()) return Result<void>::failure(invalid());
30 for (auto uv : texcoords)
31 if (!std::isfinite(uv)) return Result<void>::failure(invalid());
32 auto geometry = deformVegetation(field, vertices, motion);
33 if (!geometry.ok()) return Result<void>::failure(geometry.status());
34 auto& data = geometry.value();
35 auto frameValid = mesh.validateTangentFrame(data.tangents, data.bitangents);
36 if (!frameValid) return frameValid;
37 auto highlightsValid = mesh.validateMotionHighlights(data.motionHighlights);
38 if (!highlightsValid) return highlightsValid;
39 auto factorsValid = mesh.validateVegetationFactors(data.vegetationFactors);
40 if (!factorsValid) return factorsValid;
41 if (!graphics.updateMeshVertices(&mesh, data.positions.data(), data.normals.data(), texcoords.data(),
42 int(vertices.size()), indices.data(), int(indices.size())))
44 DiagnosticCode::Unsupported, "backend rejected vegetation mesh update", {}, {}, "graphics.vegetation"));
45 mesh.computeBounds(data.positions.data(), int(vertices.size()));
46 // The vertex count and owning frame were validated before upload. This swap cannot allocate.
47 auto adopted = mesh.adoptTangentFrame(std::move(data.tangents), std::move(data.bitangents));
48 if (!adopted) return adopted;
49 auto highlightsAdopted = mesh.adoptMotionHighlights(std::move(data.motionHighlights));
50 if (!highlightsAdopted) return highlightsAdopted;
51 auto factorsAdopted = mesh.adoptVegetationFactors(std::move(data.vegetationFactors));
52 if (!factorsAdopted) return factorsAdopted;
53 return Result<void>::success();
54}
55
57 const VegetationSurface& surface) {
58 if (!finite(sample.color) || !finite(sample.extras) || !unit(surface.roughness) ||
59 !unit(surface.overlaySmoothness) || !unit(surface.wetnessCoverage) || !unit(surface.wetnessContrast) ||
60 !unit(surface.overlayCoverage) ||
61 !unit(surface.overlayVariation) || !unit(surface.overlayProjection) ||
62 !unit(surface.vertexOcclusionAlpha) || !unit(surface.overlayNormalScale) ||
63 !unit(surface.wetnessNormalScale) || !unit(surface.overlaySubsurface) || !unit(surface.alphaCutoff) ||
64 !unit(surface.colorsCoverage) || !std::isfinite(surface.colorsIntensity) || surface.colorsIntensity < 0 ||
65 surface.colorsIntensity > 2 || !unit(surface.colorsMask) || !unit(surface.colorsVariation) ||
67 surface.vertexOcclusionMaximum - surface.vertexOcclusionMinimum + .0001f == 0.f)
68 return Result<void>::failure(invalid());
69 for (auto value : surface.albedo)
70 if (!std::isfinite(value) || value < 0) return Result<void>::failure(invalid());
71 for (auto value : surface.overlayColor)
72 if (!std::isfinite(value) || value < 0) return Result<void>::failure(invalid());
73 for (auto value : surface.emission)
74 if (!std::isfinite(value) || value < 0) return Result<void>::failure(invalid());
75 for (auto value : surface.vertexOcclusionColor)
76 if (!std::isfinite(value) || value < 0) return Result<void>::failure(invalid());
77 const float wetness = std::clamp(sample.extras.y, 0.f, 1.f);
78 const float overlay = std::clamp(sample.extras.z * surface.overlayCoverage, 0.f, 1.f);
79 const float alpha = std::clamp(surface.albedo[3] * sample.extras.w, 0.f, 1.f);
80 auto pbr = material.pbrSurface();
81 pbr.vegetationColor.fieldColor = {std::max(sample.color.r, 0.f), std::max(sample.color.g, 0.f),
82 std::max(sample.color.b, 0.f), std::clamp(sample.color.a, 0.f, 1.f)};
83 pbr.vegetationColor.overlayColor = surface.overlayColor;
84 pbr.vegetationColor.overlay = overlay;
85 pbr.vegetationColor.wetness = std::clamp(wetness * surface.wetnessCoverage, 0.f, 1.f);
86 pbr.vegetationColor.overlayVariation = surface.overlayVariation;
87 pbr.vegetationColor.overlayProjection = surface.overlayProjection;
88 pbr.vegetationColor.vertexOcclusionAlpha = surface.vertexOcclusionAlpha;
89 pbr.vegetationColor.vertexOcclusionColor = surface.vertexOcclusionColor;
90 pbr.vegetationColor.invertVertexOcclusion = surface.invertVertexOcclusion;
91 pbr.vegetationColor.overlayNormalScale = surface.overlayNormalScale;
92 pbr.vegetationColor.wetnessNormalScale = surface.wetnessNormalScale;
93 pbr.vegetationColor.overlaySmoothness = surface.overlaySmoothness;
94 pbr.vegetationColor.wetnessContrast = surface.wetnessContrast;
95 pbr.vegetationColor.overlaySubsurface = surface.overlaySubsurface;
96 pbr.vegetationColor.colorsCoverage = surface.colorsCoverage;
97 pbr.vegetationColor.colorsIntensity = surface.colorsIntensity;
98 pbr.vegetationColor.colorsMask = surface.colorsMask;
99 pbr.vegetationColor.colorsVariation = surface.colorsVariation;
100 pbr.vegetationColor.vertexOcclusionMinimum = surface.vertexOcclusionMinimum;
101 pbr.vegetationColor.vertexOcclusionMaximum = surface.vertexOcclusionMaximum;
102 pbr.vegetationColor.invertVertexOcclusionColors = surface.invertVertexOcclusionColors;
103 pbr.vegetationColor.backfaceNormalMode = surface.backfaceNormalMode;
104 for (size_t c = 0; c < 3; ++c) {
105 pbr.emissive[c] = surface.emission[c] * std::max(sample.extras.x, 0.f);
106 if (!std::isfinite(pbr.emissive[c])) return Result<void>::failure(invalid());
107 }
108 auto accepted = material.setPbrSurface(pbr);
109 if (!accepted.ok()) return accepted;
110 material.setTint(surface.albedo[0], surface.albedo[1], surface.albedo[2], alpha);
111 material.setRoughness(surface.roughness);
112 material.setSurfaceMode("masked");
113 material.setAlphaCutoff(surface.alphaCutoff);
114 material.setDoubleSided(true);
115 return Result<void>::success();
116}
117} // namespace eve::graphics
float uv
float wetness
std::vector< std::uint32_t > indices
std::vector< float > texcoords
std::int32_t c
graphics::PbrSurface pbr
OceanMeshSettings mesh
bool finite
std::array< uint32_t, 16 > material
Object::Ptr value
std::string geometry
Graphics * graphics
TacticalUnit * unit
std::array< double, 2 > vertices[3]
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
bool ok() const noexcept
Whether this result represents a non-failure outcome.
Definition Result.h:514
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
virtual bool updateMeshVertices(Mesh *mesh, const float *posXYZ, const float *nrmXYZ, const float *uvST, int vertexCount, const uint32_t *indices, int indexCount)=0
In-place update of a mesh's vertex/index data (CPU -> host-visible VBO). Only call when content chang...
Packages shading method + surface parameters into one attachable asset.
Definition Material.h:38
GPU mesh handle (+ optional CPU morph targets).
Definition Mesh.h:26
Backend-independent, owning vegetation influence field. Single writer; concurrent const evaluation is...
eve::Diagnostic Diagnostic
Status invalid(const char *message)
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
Result< void > applyVegetationSurface(Material &material, const VegetationSample &sample, const VegetationSurface &surface)
Apply an independently sampled field to a masked double-sided PBR material. Render-thread only,...
Result< VegetationGeometry > deformVegetation(const VegetationField &field, std::span< const VegetationVertex > vertices, const VegetationMotion &motion)
Evaluate plant bending, squash, rolling, flutter, interaction and perspective. Worker-safe with immut...
Result< void > updateVegetationMesh(Graphics &graphics, Mesh &mesh, const VegetationField &field, std::span< const VegetationVertex > vertices, const VegetationMotion &motion, std::span< const float > texcoords, std::span< const uint32_t > indices)
Deform rest geometry then update an existing mesh through the active Graphics backend....
Source-equivalent plant motion parameters with explicit time and texture inputs. All data is owned....
Owning sampled field channels; no lifetime dependency on the field.
Owning surface inputs, supplied afresh to avoid accumulating tint/wetness across frames....
std::array< float, 3 > vertexOcclusionColor
PbrVegetationBackfaceNormalMode backfaceNormalMode
std::array< float, 3 > emission
std::array< float, 3 > overlayColor