载入中...
搜索中...
未找到
ContactShadow.cpp
浏览该文件的文档.
2
3#include "common/Exception.h"
6#include "graphics/Color.h"
7#include "graphics/Graphics.h"
8#include "graphics/Shader.h"
9#include "graphics/Texture.h"
11#include "graphics/shaders/contact_shadow_frag_spv.inc"
12
13#include <algorithm>
14#include <cmath>
15#include <cstdint>
16#include <vector>
17
18#include <glm/geometric.hpp>
19#include <glm/gtc/matrix_inverse.hpp>
20#include <glm/gtc/matrix_transform.hpp>
21
22namespace eve::graphics {
23namespace {
24
25Shader* createContactShadowShader(Graphics* gfx) {
26 if (!gfx) throw eve::Exception("ContactShadow: null graphics");
27 Shader* sh = nullptr;
28 if (gfx->getBackendName() == "webgpu") {
29 sh = gfx->newShaderFromWgsl({}, std::string(shaders::kContactShadowCommon) + shaders::kContactShadow);
30 } else {
31 std::vector<uint32_t> frag(contact_shadow_frag_spv, contact_shadow_frag_spv + contact_shadow_frag_spv_count);
32 sh = gfx->newShaderFromSpv({}, frag);
33 }
34 if (!sh || !sh->gpuHandle) throw eve::Exception("ContactShadow: failed to create shader");
35 sh->declareMatrix("invViewProj");
36 sh->declareFloat("nearZ");
37 sh->declareFloat("farZ");
38 sh->declareFloat("length");
39 sh->declareFloat("thickness");
40 sh->declareFloat("intensity");
41 sh->declareFloat("stepCount");
42 sh->declareFloat("lightDirX");
43 sh->declareFloat("lightDirY");
44 sh->declareFloat("lightDirZ");
45 sh->declareFloat("texelW");
46 sh->declareFloat("texelH");
47 sh->declareFloat("hasNormal");
48 sh->declareFloat("startBias");
49 sh->sendMatrix("invViewProj", glm::mat4(1.f));
50 sh->sendFloat("nearZ", 0.1f);
51 sh->sendFloat("farZ", 100.f);
52 sh->sendFloat("length", 0.35f);
53 sh->sendFloat("thickness", 0.12f);
54 sh->sendFloat("intensity", 0.65f);
55 sh->sendFloat("stepCount", 12.f);
56 sh->sendFloat("lightDirX", 0.48f);
57 sh->sendFloat("lightDirY", 0.86f);
58 sh->sendFloat("lightDirZ", 0.28f);
59 sh->sendFloat("texelW", 1.f / 256.f);
60 sh->sendFloat("texelH", 1.f / 256.f);
61 sh->sendFloat("hasNormal", 0.f);
62 sh->sendFloat("startBias", 0.01f);
63 return sh;
64}
65
66} // namespace
67
69 shader_ = createContactShadowShader(gfx);
70 applyQualityDefaults();
71}
72
74
75void ContactShadow::applyQualityDefaults() {
76 if (quality_ == "low") {
77 stepCount_ = 10;
78 length_ = 0.20f;
79 thickness_ = 0.10f;
80 intensity_ = 0.22f;
81 startBias_ = 0.010f;
82 } else if (quality_ == "high") {
83 // Dual-ray soft (center + tangent) + binary refine; keep intensity
84 // modest so feet ground without crushing the lit albedo to black.
85 stepCount_ = 16;
86 length_ = 0.36f;
87 thickness_ = 0.14f;
88 intensity_ = 0.28f;
89 startBias_ = 0.010f;
90 } else {
91 quality_ = "medium";
92 stepCount_ = 14;
93 length_ = 0.28f;
94 thickness_ = 0.12f;
95 intensity_ = 0.26f;
96 startBias_ = 0.012f;
97 }
98}
99
100void ContactShadow::setQuality(const std::string& quality) {
101 quality_ = quality;
102 applyQualityDefaults();
103}
104
106 invViewProj_ = invViewProj;
107 if (shader_) shader_->sendMatrix("invViewProj", invViewProj_);
108}
109
110void ContactShadow::setCamera(float eyeX, float eyeY, float eyeZ, float targetX, float targetY, float targetZ,
111 float upX, float upY, float upZ, float fovYDeg, float aspect, float nearZ, float farZ) {
112 nearZ_ = nearZ > 1e-4f ? nearZ : 0.1f;
113 farZ_ = farZ > nearZ_ ? farZ : nearZ_ + 1.f;
114 const float aspectSafe = aspect > 1e-4f ? aspect : 1.f;
115 const float fovRad = fovYDeg * 0.017453292519943295f;
116 const glm::mat4 view =
117 glm::lookAtRH(glm::vec3(eyeX, eyeY, eyeZ), glm::vec3(targetX, targetY, targetZ), glm::vec3(upX, upY, upZ));
118 const glm::mat4 proj = perspectiveVulkanRH_ZO(fovRad, aspectSafe, nearZ_, farZ_);
119 setInvViewProj(glm::inverse(proj * view));
120 if (shader_) {
121 shader_->sendFloat("nearZ", nearZ_);
122 shader_->sendFloat("farZ", farZ_);
123 }
124}
125
126void ContactShadow::setLightDirection(float x, float y, float z) {
127 glm::vec3 d(x, y, z);
128 const float len = glm::length(d);
129 lightDir_ = len > 1e-6f ? d / len : glm::vec3(0.f, 1.f, 0.f);
130}
131
132void ContactShadow::setLength(float length) { length_ = length > 1e-4f ? length : 1e-4f; }
133
134void ContactShadow::setThickness(float thickness) { thickness_ = thickness > 1e-4f ? thickness : 1e-4f; }
135
136void ContactShadow::setIntensity(float intensity) { intensity_ = intensity < 0.f ? 0.f : intensity; }
137
138void ContactShadow::setStartBias(float bias) { startBias_ = bias < 0.f ? 0.f : bias; }
139
140void ContactShadow::uploadCommon(int width, int height) {
141 if (!shader_) throw eve::Exception("ContactShadow: null shader");
142 if (width < 1) width = 1;
143 if (height < 1) height = 1;
144 shader_->sendMatrix("invViewProj", invViewProj_);
145 shader_->sendFloat("nearZ", nearZ_);
146 shader_->sendFloat("farZ", farZ_);
147 shader_->sendFloat("length", length_);
148 shader_->sendFloat("thickness", thickness_);
149 shader_->sendFloat("intensity", intensity_);
150 shader_->sendFloat("stepCount", float(stepCount_));
151 shader_->sendFloat("lightDirX", lightDir_.x);
152 shader_->sendFloat("lightDirY", lightDir_.y);
153 shader_->sendFloat("lightDirZ", lightDir_.z);
154 shader_->sendFloat("texelW", 1.f / float(width));
155 shader_->sendFloat("texelH", 1.f / float(height));
156 shader_->sendFloat("startBias", startBias_);
157}
158
159void ContactShadow::applyFromGBuffer(Graphics* gfx, Texture* hwDepth, Texture* worldNormal) {
160 if (!gfx) throw eve::Exception("ContactShadow.applyFromGBuffer: null graphics");
161 if (!hwDepth) throw eve::Exception("ContactShadow.applyFromGBuffer: null hwDepth");
162 if (!shader_) throw eve::Exception("ContactShadow.applyFromGBuffer: missing shader");
163 shader_->sendFloat("hasNormal", worldNormal ? 1.f : 0.f);
164 uploadCommon(hwDepth->getWidth(), hwDepth->getHeight());
165 const float dw = gfx->getCanvas() ? float(gfx->getCanvas()->getWidth()) : float(gfx->getWidth());
166 const float dh = gfx->getCanvas() ? float(gfx->getCanvas()->getHeight()) : float(gfx->getHeight());
167 if (worldNormal) {
168 gfx->drawTexturedRectShaderDepth(hwDepth, worldNormal, shader_, 0.f, 0.f, dw, dh, Color(1.f, 1.f, 1.f, 1.f));
169 } else {
170 gfx->drawTexturedRectShaderUV(hwDepth, shader_, 0.f, 0.f, dw, dh, 0.f, 0.f, 1.f, 1.f, Color(1.f, 1.f, 1.f, 1.f),
171 false, BlendMode::Alpha);
172 }
173}
174
175} // namespace eve::graphics
double z
double y
double x
vk::ShaderModule frag
std::uint32_t height
std::uint32_t width
float length
float thickness
OceanSprayView view
float d
float fovRad
glm::mat4 proj
float bias
Definition Shadow.cpp:149
float targetX
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
virtual int getWidth() const =0
Returns the width.
virtual int getHeight() const =0
Returns the height.
void setInvViewProj(const glm::mat4 &invViewProj)
Sets the inv view proj.
void setQuality(const std::string &quality)
"low" | "medium" | "high" (unknown → medium).
void setLength(float length)
Sets the ray length in world units.
void applyFromGBuffer(Graphics *gfx, Texture *hwDepth, Texture *worldNormal)
Darken the current target using contact shadows from GBuffer depth.
void setIntensity(float intensity)
Sets the darkening intensity in [0, +inf).
void setThickness(float thickness)
Sets the contact thickness in view-space meters.
void setLightDirection(float x, float y, float z)
World-space direction toward the light (normalized on write). Matches Light3D directional convention ...
ContactShadow(Graphics *gfx)
Owns post shaders created from the borrowed Graphics factory.
~ContactShadow()
Releases owned shaders.
void setCamera(float eyeX, float eyeY, float eyeZ, float targetX, float targetY, float targetZ, float upX, float upY, float upZ, float fovYDeg, float aspect, float nearZ, float farZ)
Camera for depth reconstruction (RH + ZO). Builds inv(viewProj) and near/far used by apply*.
void setStartBias(float bias)
Sets the start bias along normal/light in world units.
int getWidth() const
Returns the width.
Definition Graphics.h:489
virtual void drawTexturedRectShaderUV(Texture *texture, Shader *shader, float x, float y, float w, float h, float u0, float v0, float u1, float v1, const Color &color, bool rotatedUV=false, BlendMode blend=BlendMode::Alpha)=0
UV draw with an explicit Shader (nullptr = default textured pipeline).
virtual Canvas * getCanvas() const =0
Returns the canvas.
int getHeight() const
Returns the height.
Definition Graphics.h:491
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:107
void sendMatrix(const std::string &name, const glm::mat4 &m)
Definition Shader.cpp:124
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:15
int getWidth() const
Returns the width.
Definition Texture.cpp:29
int getHeight() const
Returns the height.
Definition Texture.cpp:34
constexpr const char * kContactShadowCommon
constexpr const char * kContactShadow
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
glm::mat4 perspectiveVulkanRH_ZO(float fovyRad, float aspect, float zNear, float zFar)
Right-handed, zero-to-one depth perspective for Vulkan swapchains.
Definition ClipSpace.h:22
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::mat4 invViewProj