载入中...
搜索中...
未找到
AlphaMask.cpp
浏览该文件的文档.
2
3#include "common/Exception.h"
4#include "graphics/Color.h"
5#include "graphics/Graphics.h"
6#include "graphics/Shader.h"
7#include "graphics/Texture.h"
8
9#include "graphics/shaders/alpha_mask_frag_spv.inc"
10
11#include <algorithm>
12#include <vector>
13
14namespace eve::graphics {
15
16AlphaMask::AlphaMask(Graphics *graphics) : graphics_(graphics) {
17 if (!graphics_) throw eve::Exception("AlphaMask: null graphics");
18 if (graphics_->getBackendName() == "webgpu") {
19 static const std::string fragment = R"(
20struct Externals { data: array<f32, 32>, };
21@group(0) @binding(0) var mainTex: texture_2d<f32>;
22@group(0) @binding(1) var maskTex: texture_2d<f32>;
23@group(0) @binding(2) var mainSampler: sampler;
24@group(0) @binding(3) var maskSampler: sampler;
25@group(0) @binding(4) var<uniform> ext: Externals;
26struct In { @location(0) color: vec4<f32>, @location(1) uv: vec2<f32>, };
27@fragment fn fs_main(input: In) -> @location(0) vec4<f32> {
28 let color = textureSample(mainTex, mainSampler, input.uv) * input.color;
29 let ms = textureSample(maskTex, maskSampler, input.uv);
30 var m = ms.r;
31 if (ext.data[2] > 0.5) { m = 1.0 - m; }
32 let width = max(ext.data[1], 0.0001);
33 let coverage = smoothstep(ext.data[0] - width, ext.data[0] + width, m);
34 return vec4<f32>(color.rgb, color.a * coverage);
35})";
36 shader_ = graphics_->newShaderFromWgsl({}, fragment);
37 } else {
38 std::vector<uint32_t> fragment(alpha_mask_frag_spv,
39 alpha_mask_frag_spv + alpha_mask_frag_spv_count);
40 shader_ = graphics_->newShaderFromSpv({}, fragment);
41 }
42 if (!shader_) throw eve::Exception("AlphaMask: shader creation failed");
43 shader_->declareFloat("threshold");
44 shader_->declareFloat("softness");
45 shader_->declareFloat("inverted");
46 syncUniforms();
47}
48
49AlphaMask::~AlphaMask() = default;
50
51void AlphaMask::syncUniforms() {
52 shader_->sendFloat("threshold", threshold_);
53 shader_->sendFloat("softness", softness_);
54 shader_->sendFloat("inverted", inverted_ ? 1.f : 0.f);
55}
56
57void AlphaMask::setThreshold(float threshold) {
58 threshold_ = std::clamp(threshold, 0.f, 1.f);
59 syncUniforms();
60}
61
62void AlphaMask::setSoftness(float softness) {
63 softness_ = std::clamp(softness, 0.f, 0.5f);
64 syncUniforms();
65}
66
67void AlphaMask::setInverted(bool inverted) {
68 inverted_ = inverted;
69 syncUniforms();
70}
71
72void AlphaMask::draw(Texture *color, Texture *mask, float x, float y, float width, float height,
73 float r, float g, float b, float a) {
74 if (!color || !mask) return;
75 graphics_->drawTexturedRectShaderDepth(color, mask, shader_, x, y, width, height,
76 Color(r, g, b, a));
77}
78
79} // namespace eve::graphics
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
int mask
tensor::Graph g
Definition GpuGraph.cpp:7
double r
std::uint32_t height
std::uint32_t width
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
void setInverted(bool inverted)
Invert mask coverage.
Definition AlphaMask.cpp:67
AlphaMask(Graphics *graphics)
Create a mask renderer backed by the supplied graphics device.
Definition AlphaMask.cpp:16
void setSoftness(float softness)
Set the normalized soft-edge half width.
Definition AlphaMask.cpp:62
void draw(Texture *color, Texture *mask, float x, float y, float width, float height, float r=1.f, float g=1.f, float b=1.f, float a=1.f)
Draw a color texture multiplied by a same-UV alpha mask.
Definition AlphaMask.cpp:72
void setThreshold(float threshold)
Set the mask cutoff in normalized alpha space.
Definition AlphaMask.cpp:57
virtual std::string getBackendName() const =0
Renderer backend id used by sibling modules (e.g. Gpgpu).
virtual Shader * newShaderFromWgsl(const std::string &vertWgsl, const std::string &fragWgsl)=0
Create a 2D custom shader from WGSL source (WebGPU backend). Empty vert → default textured vertex sha...
virtual Shader * newShaderFromSpv(const std::vector< uint32_t > &vertSpv, const std::vector< uint32_t > &fragSpv)=0
Create a custom 2D shader from SPIR-V words (vert + frag). Owned by Graphics. Vertex stage may be emp...
virtual void drawTexturedRectShaderDepth(Texture *color, Texture *depth, Shader *shader, float x, float y, float w, float h, const Color &tint)=0
Fullscreen/post draw sampling color at binding 0 and depth at binding 1 (hardware D32,...
void sendFloat(const std::string &name, float x)
Definition Shader.cpp:95
int declareFloat(const std::string &name)
Reserve sequential float slots in the push-constant block. Returns start index.
Definition Shader.cpp:43
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:18
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
eve::Color Color
RGBA color used by every graphics draw call. Lives inside eve::graphics so including a graphics heade...
Definition Color.h:13
glm::vec4 color