载入中...
搜索中...
未找到
HairCards.cpp
浏览该文件的文档.
2#include <array>
3#include <cmath>
4#include <vector>
5#include "common/Exception.h"
6#include "graphics/Graphics.h"
8#include "graphics/Material.h"
9#include "graphics/Mesh.h"
11#include "graphics/Shader.h"
13#include "graphics/shaders/mesh3d_hair_frag_spv.inc"
14#include "graphics/shaders/mesh3d_hair_vert_spv.inc"
15
16// HairCards.cpp
17namespace eve::graphics::hair {
18namespace {
19
20void pushVertex(CardMeshData &mesh, float x, float y, float z, float nx, float ny, float nz, float u, float v) {
21 mesh.positions.push_back(x);
22 mesh.positions.push_back(y);
23 mesh.positions.push_back(z);
24 mesh.normals.push_back(nx);
25 mesh.normals.push_back(ny);
26 mesh.normals.push_back(nz);
27 mesh.uvs.push_back(u);
28 mesh.uvs.push_back(v);
29}
30
31void pushQuad(CardMeshData &mesh, float ax, float ay, float az, float bx, float by, float bz, float cx, float cy,
32 float cz, float dx, float dy, float dz, float nx, float ny, float nz) {
33 const uint32_t base = static_cast<uint32_t>(mesh.positions.size() / 3);
34 pushVertex(mesh, ax, ay, az, nx, ny, nz, 0.f, 0.f);
35 pushVertex(mesh, bx, by, bz, nx, ny, nz, 1.f, 0.f);
36 pushVertex(mesh, cx, cy, cz, nx, ny, nz, 1.f, 1.f);
37 pushVertex(mesh, dx, dy, dz, nx, ny, nz, 0.f, 1.f);
38 mesh.indices.push_back(base + 0);
39 mesh.indices.push_back(base + 1);
40 mesh.indices.push_back(base + 2);
41 mesh.indices.push_back(base + 0);
42 mesh.indices.push_back(base + 2);
43 mesh.indices.push_back(base + 3);
44}
45
46void normalize3(float &x, float &y, float &z) {
47 const float len = std::sqrt(x * x + y * y + z * z);
48 if (len <= 1e-8f) {
49 x = 0.f;
50 y = 1.f;
51 z = 0.f;
52 return;
53 }
54 x /= len;
55 y /= len;
56 z /= len;
57}
58
59void cross3(float ax, float ay, float az, float bx, float by, float bz, float &ox, float &oy, float &oz) {
60 ox = ay * bz - az * by;
61 oy = az * bx - ax * bz;
62 oz = ax * by - ay * bx;
63}
64
65} // namespace
66
69 const float w = width > 1e-4f ? width : 1e-4f;
70 const float h = height > 1e-4f ? height : 1e-4f;
71 const float hw = 0.5f * w;
72 pushQuad(mesh, -hw, 0.f, 0.f, hw, 0.f, 0.f, hw, h, 0.f, -hw, h, 0.f, 0.f, 0.f, 1.f);
73 return mesh;
74}
75
76CardMeshData buildCardsAlongPolyline(const float *pointsXYZ, int pointCount, float width) {
78 if (!pointsXYZ || pointCount < 2) return mesh;
79 const float w = width > 1e-4f ? width : 1e-4f;
80 const float hw = 0.5f * w;
81 for (int i = 0; i + 1 < pointCount; ++i) {
82 const float *a = pointsXYZ + i * 3;
83 const float *b = pointsXYZ + (i + 1) * 3;
84 float dx = b[0] - a[0], dy = b[1] - a[1], dz = b[2] - a[2];
85 normalize3(dx, dy, dz);
86 float sx = 0.f, sy = 0.f, sz = 0.f;
87 cross3(dx, dy, dz, 0.f, 1.f, 0.f, sx, sy, sz);
88 if (sx * sx + sy * sy + sz * sz < 1e-8f) cross3(dx, dy, dz, 1.f, 0.f, 0.f, sx, sy, sz);
89 normalize3(sx, sy, sz);
90 float nx = 0.f, ny = 0.f, nz = 0.f;
91 cross3(sx, sy, sz, dx, dy, dz, nx, ny, nz);
92 normalize3(nx, ny, nz);
93 pushQuad(mesh, a[0] - sx * hw, a[1] - sy * hw, a[2] - sz * hw, a[0] + sx * hw, a[1] + sy * hw, a[2] + sz * hw,
94 b[0] + sx * hw, b[1] + sy * hw, b[2] + sz * hw, b[0] - sx * hw, b[1] - sy * hw, b[2] - sz * hw, nx, ny,
95 nz);
96 }
97 return mesh;
98}
99
100CardMeshData buildCardsFromRoots(const CardRoot *roots, int rootCount) {
102 if (!roots || rootCount <= 0) return mesh;
103 for (int i = 0; i < rootCount; ++i) {
104 const CardRoot &r = roots[i];
105 float dx = r.dirX, dy = r.dirY, dz = r.dirZ;
106 normalize3(dx, dy, dz);
107 const float len = r.length > 1e-4f ? r.length : 1e-4f;
108 const float tip[3] = {r.x + dx * len, r.y + dy * len, r.z + dz * len};
109 const float pts[6] = {r.x, r.y, r.z, tip[0], tip[1], tip[2]};
111 }
112 return mesh;
113}
114
116 const uint32_t base = static_cast<uint32_t>(dst.positions.size() / 3);
117 dst.positions.insert(dst.positions.end(), src.positions.begin(), src.positions.end());
118 dst.normals.insert(dst.normals.end(), src.normals.begin(), src.normals.end());
119 dst.uvs.insert(dst.uvs.end(), src.uvs.begin(), src.uvs.end());
120 for (uint32_t idx : src.indices) dst.indices.push_back(base + idx);
121}
122
124 if (!gfx) throw eve::Exception("hair::uploadCardMesh: null graphics");
125 if (data.positions.empty() || data.indices.empty()) return nullptr;
126 const int vertexCount = static_cast<int>(data.positions.size() / 3);
127 return gfx->newMeshFromArrays(data.positions.data(), data.normals.data(), data.uvs.data(), vertexCount,
128 data.indices.data(), static_cast<int>(data.indices.size()));
129}
130
131Mesh *newCardMesh(Graphics *gfx, float width, float height) { return uploadCardMesh(gfx, buildCard(width, height)); }
132
134 mat.setHair(true);
135 mat.setShadingModel("hair");
136 mat.setSurfaceMode("transparent");
137 mat.setBlendMode("alpha");
138 mat.setDoubleSided(true);
139 mat.setDepthWrite(false);
140 mat.setCastShadow(false);
141 mat.setAlphaCutoff(0.15f);
142 if (mat.getSortPriority() == 0) mat.setSortPriority(10);
143}
144
146 if (!gfx) throw eve::Exception("hair::makeCardMaterial: null graphics");
147 auto *mat = new Material();
148 applyCardDefaults(*mat);
149 if (albedo) mat->setAlbedoTexture(albedo);
150 Shader *shader = hairShader ? hairShader : createShader(gfx);
151 mat->setShader(shader);
152 return mat;
153}
154
155void configureCardLod(Renderable3D *renderable, Mesh *nearCards, Mesh *farProxy, float switchDistance) {
156 if (!renderable) throw eve::Exception("hair::configureCardLod: null renderable");
157 if (!nearCards) throw eve::Exception("hair::configureCardLod: null near mesh");
158 const float dist = switchDistance > 0.f ? switchDistance : 8.f;
159 renderable->setMeshLod(0, nearCards, 0.f);
160 if (farProxy) renderable->setMeshLod(1, farProxy, dist);
161 renderable->setHair(true);
162}
163
164} // namespace eve::graphics::hair
165
166// HairShader.cpp
167namespace eve::graphics::hair {
168namespace {
169
170const std::array<const char *, 15> kParams = {
171 "specExp", "specStrength", "primaryShift", "secondaryShift", "alphaCutoff",
172 "rimStrength", "strandDirX", "strandDirY", "strandDirZ", "marschnerR",
173 "marschnerTT", "marschnerTRT", "selfShadowStrength", "selfShadowBias", "rootAoStrength"};
174
175
176std::vector<uint32_t> copySpv(const uint32_t *data, size_t count) { return std::vector<uint32_t>(data, data + count); }
177
178} // namespace
179
180int paramCount() { return int(kParams.size()); }
181
182std::string paramName(int index) {
183 if (index < 0 || index >= int(kParams.size())) return {};
184 return kParams[size_t(index)];
185}
186
188 if (!shader) throw eve::Exception("hair::bindDefaults: null shader");
189 shader->declareFloat("specExp");
190 shader->declareFloat("specStrength");
191 shader->declareFloat("primaryShift");
192 shader->declareFloat("secondaryShift");
193 shader->declareFloat("alphaCutoff");
194 shader->declareFloat("rimStrength");
195 shader->declareVec3("strandDir");
196 shader->declareFloat("marschnerR");
197 shader->declareFloat("marschnerTT");
198 shader->declareFloat("marschnerTRT");
199 shader->declareFloat("selfShadowStrength");
200 shader->declareFloat("selfShadowBias");
201 shader->declareFloat("rootAoStrength");
202 shader->sendFloat("specExp", 80.f);
203 shader->sendFloat("specStrength", 0.85f);
204 shader->sendFloat("primaryShift", 0.08f);
205 shader->sendFloat("secondaryShift", -0.06f);
206 shader->sendFloat("alphaCutoff", 0.15f);
207 shader->sendFloat("rimStrength", 0.35f);
208 shader->sendVec3("strandDir", 0.f, 0.f, 0.f);
209 shader->sendFloat("marschnerR", 1.f);
210 shader->sendFloat("marschnerTT", 0.45f);
211 shader->sendFloat("marschnerTRT", 0.25f);
212 // Mild analytical self-shadow on by default; set strength 0 to disable.
213 shader->sendFloat("selfShadowStrength", 0.35f);
214 shader->sendFloat("selfShadowBias", 0.25f);
215 shader->sendFloat("rootAoStrength", 0.3f);
216}
217
219 if (!gfx) throw eve::Exception("hair::createShader: null graphics");
220 auto vert = copySpv(mesh3d_hair_vert_spv, mesh3d_hair_vert_spv_count);
221 auto frag = copySpv(mesh3d_hair_frag_spv, mesh3d_hair_frag_spv_count);
222 Shader *sh = gfx->getBackendName() == "webgpu" ? gfx->newHairShaderFromWgsl({}, shaders::kHairFragWgsl)
224 if (!sh || !sh->gpuHandle) throw eve::Exception("hair::createShader: failed to create hair shader");
225 bindDefaults(sh);
226 return sh;
227}
228
229} // namespace eve::graphics::hair
double z
double y
double x
float w
Definition AnimClip.cpp:738
int tip
Definition AnimSmr.cpp:121
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
graphics::Texture * albedo
std::uint32_t vertexCount
vk::ShaderModule vert
vk::ShaderModule frag
float u
Definition Grass.cpp:233
double r
float v
int h
std::uint32_t height
std::uint32_t width
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
OceanMeshSettings mesh
glm::dvec3 dx
glm::dvec3 dz
int idx
Shader * shader
float dy
Definition Shadow.cpp:151
int count
uint32_t index
double oy
double ox
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
virtual std::string getBackendName() const =0
Renderer backend id used by sibling modules (e.g. Gpgpu).
virtual Shader * newHairShaderFromWgsl(const std::string &vertWgsl, const std::string &fragWgsl)=0
Create an alpha-blended hair/card shader from WGSL on WebGPU.
virtual Shader * newHairShaderFromSpv(const std::vector< uint32_t > &vertSpv, const std::vector< uint32_t > &fragSpv)=0
Hair/fur card shader (alpha blend + Kajiya-Kay). Empty vert → mesh3d_hair.vert. Owned by Graphics.
virtual Mesh * newMeshFromArrays(const float *posXYZ, const float *nrmXYZ, const float *uvST, int vertexCount, const uint32_t *indices, int indexCount)=0
Upload a triangle mesh from packed CPU arrays. Owned by Graphics. posXYZ required (vertexCount*3)....
Packages shading method + surface parameters into one attachable asset.
Definition Material.h:38
void setCastShadow(bool cast)
Sets the cast shadow.
Definition Material.h:277
void setBlendMode(const std::string &mode)
Set "alpha", "premultiplied", "additive", or "multiply".
Definition Material.cpp:183
int getSortPriority() const
Returns the sort priority.
Definition Material.h:352
void setAlphaCutoff(float cutoff)
Sets the alpha cutoff.
Definition Material.cpp:181
void setSurfaceMode(const std::string &mode)
Set "opaque", "masked", or "transparent" surface classification.
Definition Material.cpp:166
void setShadingModel(const std::string &model)
"pbr" | "unlit" | "hair" | "custom" (unknown → pbr).
Definition Material.cpp:106
void setDepthWrite(bool enabled)
Sets the depth write.
Definition Material.h:338
void setSortPriority(int priority)
Sets the sort priority.
Definition Material.h:350
void setHair(bool hair)
Sets the hair.
Definition Material.cpp:152
void setDoubleSided(bool enabled)
Sets the double sided.
Definition Material.h:342
GPU mesh handle (+ optional CPU morph targets).
Definition Mesh.h:26
EVENGINE_API_BACKENDS public API.
void setMeshLod(int index, Mesh *mesh, float switchDistance=0.f)
Configure geometric LOD. index 0 = highest detail. For index > 0, switchDistance is the camera distan...
Custom GPU program.
Definition Shader.h:41
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:15
void cross3(float ax, float ay, float az, float bx, float by, float bz, float &x, float &y, float &z)
Procedural hair/fur card meshes + material/LOD helpers (no strand simulation).
Definition Graphics.h:64
int paramCount()
Push-constant parameter names (same order as mesh3d_hair.frag).
CardMeshData buildCardsAlongPolyline(const float *pointsXYZ, int pointCount, float width)
Build one card per polyline segment, facing a camera-facing side approximated by world up.
Definition HairCards.cpp:76
CardMeshData buildCard(float width, float height)
Build a single upright card in local space (origin at root, height along +Y, face +Z).
Definition HairCards.cpp:67
CardMeshData buildCardsFromRoots(const CardRoot *roots, int rootCount)
Expand root samples into independent cards.
Mesh * newCardMesh(Graphics *gfx, float width, float height)
Convenience: single card mesh.
void applyCardDefaults(Material &mat)
Apply transparent hair-card surface defaults (double-sided, no depth write/shadows)....
Material * makeCardMaterial(Graphics *gfx, Texture *albedo, Shader *hairShader)
Create a Material configured for hair cards and optionally bind the hair shader + albedo.
std::string paramName(int index)
Param name.
Mesh * uploadCardMesh(Graphics *gfx, const CardMeshData &data)
Upload card arrays as a Graphics-owned Mesh.
void configureCardLod(Renderable3D *renderable, Mesh *nearCards, Mesh *farProxy, float switchDistance)
Wire near/far geometric LOD on a renderable (index 0 = cards, index 1 = proxy).
void appendCardMesh(CardMeshData &dst, const CardMeshData &src)
Append src into dst (vertex/index rebase).
Shader * createShader(Graphics *gfx)
Create the default hair shader (alpha-blended, anisotropic specular). Owned by Graphics.
void bindDefaults(Shader *shader)
Bind default push-constant knobs on an existing hair shader.
constexpr const char * kHairFragWgsl
Hair fragment shader WGSL source. @ownership Borrowed immutable string literal; callers must not free...
Definition HairWgsl.h:8
CPU-side interleaved mesh arrays for upload via Graphics::newMeshFromArrays.
Definition HairCards.h:27
std::vector< uint32_t > indices
Definition HairCards.h:31
std::vector< float > normals
xyz per vertex
Definition HairCards.h:29
std::vector< float > positions
xyz per vertex
Definition HairCards.h:28
std::vector< float > uvs
uv per vertex
Definition HairCards.h:30
One root sample expanded into a card.
Definition HairCards.h:35