载入中...
搜索中...
未找到
StyleInstance.cpp
浏览该文件的文档.
2
3#include "stylize/StylePass.h"
5
6#include "common/Exception.h"
7#include "graphics/Shader.h"
8
9#include <algorithm>
10#include <cmath>
11#include <limits>
12#include <utility>
13
14namespace eve::stylize {
15
16namespace {
17const StyleParameterDesc& requireParam(const std::string& style, const std::string& name) {
18 const StyleParameterDesc* desc = findStyleParameter(style, name);
19 if (!desc) throw eve::Exception("StyleInstance: style '%s' has no parameter '%s'", style.c_str(), name.c_str());
20 return *desc;
21}
22} // namespace
23
24StyleInstance::StyleInstance(std::string style) : style_(std::move(style)) {
25 if (!findStyleDefinition(style_)) throw eve::Exception("StyleInstance: unknown style '%s'", style_.c_str());
26}
27
28StyleInstance::StyleInstance(std::string style, std::map<std::string, float> defaults)
29 : style_(std::move(style)), externalDefaults_(std::move(defaults)) {
30 if (!style_.starts_with("asset://")) throw eve::Exception("External mesh styles require an asset URI");
31 for (const auto& [name, value] : *externalDefaults_)
32 if (name.empty() || !std::isfinite(value)) throw eve::Exception("Invalid external style parameter");
33}
34
36 return externalDefaults_ ? int(externalDefaults_->size()) : styleParamCount(style_);
37}
38
43
45 if (externalDefaults_) return priority_.value_or(40);
46 return priority_.value_or(findStyleDefinition(style_)->priority);
47}
48
49bool StyleInstance::requiresInput(const std::string& input) const {
50 if (externalDefaults_) return false;
51 const StyleDefinition* def = findStyleDefinition(style_);
52 if (input == "color") return def->post;
53 if (input == "depth") return def->depth;
54 if (input == "normal") return def->normal;
55 return false;
56}
57
58std::string StyleInstance::getParamName(int index) const {
59 if (externalDefaults_) {
60 if (index < 0 || std::size_t(index) >= externalDefaults_->size())
61 throw eve::Exception("Invalid parameter index");
62 return std::next(externalDefaults_->begin(), index)->first;
63 }
64 return styleParamName(style_, index);
65}
66
67float StyleInstance::getParamDefault(const std::string& name) const {
68 return externalDefaults_ ? externalDefaults_->at(name) : requireParam(style_, name).defaultValue;
69}
70
71float StyleInstance::getParamMin(const std::string& name) const {
72 if (externalDefaults_) {
73 if (!externalDefaults_->contains(name)) throw eve::Exception("Unknown external parameter");
74 return -std::numeric_limits<float>::max();
75 }
76 return requireParam(style_, name).minValue;
77}
78
79float StyleInstance::getParamMax(const std::string& name) const {
80 if (externalDefaults_) {
81 if (!externalDefaults_->contains(name)) throw eve::Exception("Unknown external parameter");
82 return std::numeric_limits<float>::max();
83 }
84 return requireParam(style_, name).maxValue;
85}
86
87bool StyleInstance::hasParam(const std::string& name) const {
88 return externalDefaults_ ? externalDefaults_->contains(name) : findStyleParameter(style_, name) != nullptr;
89}
90
91bool StyleInstance::isOverridden(const std::string& name) const { return overrides_.find(name) != overrides_.end(); }
92
93void StyleInstance::setFloat(const std::string& name, float value) {
94 if (externalDefaults_) {
95 if (!externalDefaults_->contains(name) || !std::isfinite(value))
96 throw eve::Exception("Invalid external style parameter");
97 overrides_[name] = value;
98 return;
99 }
100 const StyleParameterDesc& desc = requireParam(style_, name);
101 overrides_[name] = std::clamp(value, desc.minValue, desc.maxValue);
102}
103
104float StyleInstance::getFloat(const std::string& name) const {
105 const float defaultValue = getParamDefault(name);
106 const auto it = overrides_.find(name);
107 return it == overrides_.end() ? defaultValue : it->second;
108}
109
110void StyleInstance::reset(const std::string& name) {
112 overrides_.erase(name);
113}
114
115void StyleInstance::resetAll() { overrides_.clear(); }
116
118 if (!shader) throw eve::Exception("StyleInstance: null shader");
119 for (const auto& [name, value] : overrides_) {
120 if (shader->hasUniform(name)) shader->sendFloat(name, value);
121 }
122}
123
125 if (externalDefaults_) throw eve::Exception("External style requires its package renderer");
128 return new StylePass(style_, shader);
129}
130
132 if (externalDefaults_) throw eve::Exception("External style requires its package renderer");
135 return shader;
136}
137
138} // namespace eve::stylize
double value
int priority
scene::NodeDesc desc
EvpackChunkInput input
Definition Evpack.cpp:170
std::string name
Shader * shader
uint32_t index
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
Custom GPU program.
Definition Shader.h:39
graphics::Shader * newMeshShader(graphics::Graphics *gfx) const
Create a mesh technique shader and apply compatible overrides.
float getFloat(const std::string &name) const
Returns the float.
std::string getParamName(int index) const
Returns the param name.
bool hasParam(const std::string &name) const
True when param.
StylePass * newPass(graphics::Graphics *gfx) const
Create a post pass and apply this instance's parameter overrides.
void reset(const std::string &name)
Resets .
StyleInstance(std::string style)
Style instance.
int getPriority() const
Returns the priority.
float getParamMin(const std::string &name) const
Returns the param min.
void setFloat(const std::string &name, float value)
Sets the float.
void applyToShader(graphics::Shader *shader) const
Apply current overrides to an existing compatible shader.
int getParamCount() const
Returns the param count.
std::string getStage() const
Returns the stage.
float getParamDefault(const std::string &name) const
Returns the param default.
bool isOverridden(const std::string &name) const
True when overridden.
bool requiresInput(const std::string &input) const
Requires input.
float getParamMax(const std::string &name) const
Returns the param max.
One stylized post-process pass bound to a style id (built-in or custom label). Draws a full-quad of t...
Definition StylePass.h:26
const char * defaultValue
const char * postEffectStageName(PostEffectStage stage)
Post effect stage name.
Definition PostEffect.h:28
graphics::Shader * createMeshShader(graphics::Graphics *gfx, const std::string &style)
Creates mesh shader.
graphics::Shader * createPostShader(graphics::Graphics *gfx, const std::string &style)
Creates post shader.
int styleParamCount(const std::string &style)
Style param count.
const StyleParameterDesc * findStyleParameter(const std::string &style, const std::string &name)
Finds style parameter.
std::string styleParamName(const std::string &style, int index)
Style param name.
const StyleDefinition * findStyleDefinition(const std::string &style)
Return a built-in definition, or nullptr when the id is unknown.
Immutable capability metadata for one built-in stylization recipe.
Tooling metadata for one user-facing float parameter.