载入中...
搜索中...
未找到
Cloth3D.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
7
8#include <cstdint>
9#include <string>
10#include <unordered_map>
11#include <unordered_set>
12#include <vector>
13
14namespace eve::graphics {
15class Graphics;
16class Mesh;
17} // namespace eve::graphics
18
19namespace eve::physics {
20
21class World3D;
22
33public:
43 Cloth3D(int cols, int rows, float spacing, float originX, float originY, float originZ);
55 explicit Cloth3D(const ClothModel& model);
57 ~Cloth3D();
58
59 Cloth3D(const Cloth3D&) = delete;
60 Cloth3D& operator=(const Cloth3D&) = delete;
61
63 void update(float dt);
64
66 [[nodiscard("check the cloth step outcome")]]
69 [[nodiscard]] SimulationObservation observation() const noexcept override { return observation_; }
71 [[nodiscard]] SimulationBackendKind kind() const noexcept override { return SimulationBackendKind::Cpu; }
73 [[nodiscard]] SimulationDeterminism determinism() const noexcept override {
74 return SimulationDeterminism::ToleranceBounded;
75 }
77 [[nodiscard]] std::string getBackendName() const { return "cpu"; }
79 [[nodiscard]] bool supportsFeature(const std::string& feature) const;
81 [[nodiscard("check cloth observation restore")]]
82 eve::Result<void> restoreObservation(const SimulationObservation& observation) override;
83
85 void setGravity(float gx, float gy, float gz);
87 float getGravityX() const { return gravityX_; }
89 float getGravityY() const { return gravityY_; }
91 float getGravityZ() const { return gravityZ_; }
92
94 void setStiffness(float stiffness);
96 float getStiffness() const { return stiffness_; }
97
99 void setStretchCompliance(float compliance);
101 [[nodiscard]] float getStretchCompliance() const;
103 void setShearCompliance(float compliance);
105 [[nodiscard]] float getShearCompliance() const;
107 void setBendCompliance(float compliance);
109 [[nodiscard]] float getBendCompliance() const;
110
112 void setTetherScale(float scale);
114 [[nodiscard]] float getTetherScale() const;
116 void setTetherCompliance(float compliance);
118 [[nodiscard]] float getTetherCompliance() const;
119
121 void setPressure(float pressure);
123 [[nodiscard]] float getPressure() const;
125 void setVolumeCompliance(float compliance);
127 [[nodiscard]] float getVolumeCompliance() const;
129 [[nodiscard]] float getCurrentVolume() const;
130
132 void setIterations(int iterations);
134 int getIterations() const { return iterations_; }
135
137 void setDamping(float damping);
139 float getDamping() const { return damping_; }
140
145 void setParticleSize(float size);
147 float getParticleSize() const { return particleSize_; }
148
155 void setSelfCollision(bool on);
157 bool getSelfCollision() const { return selfCollision_; }
158
163 void setParticleMass(float mass);
165 float getParticleMass() const { return particleMass_; }
166
168 void setFoldStiffness(float k);
170 float getFoldStiffness() const { return foldStiffness_; }
171
178 void setMaxFoldAngle(float degrees);
180 float getMaxFoldAngle() const { return maxFoldAngle_ * 180.f / 3.14159265f; }
181
183 void setBounds(float x, float y, float z, float w, float h, float d);
185 void clearBounds();
186
188 void setCollisionMaterial(float friction, float restitution);
190 [[nodiscard]] float getCollisionFriction() const;
192 [[nodiscard]] float getCollisionRestitution() const;
194 void setCollisionFilter(uint64_t categoryBits, uint64_t maskBits);
196 [[nodiscard]] uint64_t getCollisionCategoryBits() const;
198 [[nodiscard]] uint64_t getCollisionMaskBits() const;
199
201 void setTearThreshold(float strain);
203 [[nodiscard]] float getTearThreshold() const;
205 void setMaxTearsPerStep(int count);
207 [[nodiscard]] int getMaxTearsPerStep() const;
209 void tearConstraint(int particleA, int particleB);
211 [[nodiscard]] int getTornConstraintCount() const;
212
213 void pin(int index);
215 void unpin(int index);
217 void pinTopRow();
219 bool isPinned(int index) const;
221 void setParticleInverseMass(int index, float inverseMass);
223 [[nodiscard]] float getParticleInverseMass(int index) const;
224
226 void setSkinConstraint(int index, float x, float y, float z, float nx, float ny, float nz, float radius,
227 float backstopDistance = -1.f, float backstopRadius = 0.f, float compliance = 0.f);
229 void updateSkinReference(int index, float x, float y, float z, float nx, float ny, float nz);
231 void clearSkinConstraint(int index);
233 [[nodiscard]] bool hasSkinConstraint(int index) const;
234
236 void attachParticle(int index, float x, float y, float z, float compliance = 0.f);
238 void updateAttachment(int index, float x, float y, float z);
240 void detachParticle(int index);
242 [[nodiscard]] bool isAttached(int index) const;
243
248 int grabAt(float x, float y, float z, float radius = 0.3f);
250 void moveGrab(float x, float y, float z);
252 void releaseGrab();
254 bool isGrabbing() const { return grabIndex_ >= 0; }
256 int getGrabIndex() const { return grabIndex_; }
257
260 void applyForce(float fx, float fy, float fz);
261
272 void setWindVelocity(float vx, float vy, float vz);
274 [[nodiscard]] float getWindVelocityX() const;
276 [[nodiscard]] float getWindVelocityY() const;
278 [[nodiscard]] float getWindVelocityZ() const;
279
298 void setAerodynamics(float airDensity, float dragCoefficient, float liftCoefficient);
300 [[nodiscard]] float getAirDensity() const;
302 [[nodiscard]] float getDragCoefficient() const;
304 [[nodiscard]] float getLiftCoefficient() const;
305
310 void interactAt(float x, float y, float z, float radius, float strength);
311
324 void setCollideWorld(World3D* world);
333 World3D* getCollideWorld() const { return world_; }
334
336 void reset();
337
339 void setColor(float r, float g, float b, float a = 1.f);
341 float getColorR() const { return colorR_; }
343 float getColorG() const { return colorG_; }
345 float getColorB() const { return colorB_; }
347 float getColorA() const { return colorA_; }
348
350 void draw(graphics::Graphics* gfx);
351
353 int getCols() const { return cols_; }
355 int getRows() const { return rows_; }
357 int getParticleCount() const { return static_cast<int>(particles_.size()); }
364 [[nodiscard]] eve::Result<std::vector<std::uint32_t>> captureTriangleIndices() const;
366 [[nodiscard]] int getTriangleCount() const;
368 [[nodiscard]] int getDistanceConstraintCount() const;
370 [[nodiscard]] int getTetherConstraintCount() const;
372 [[nodiscard]] int getSkinConstraintCount() const;
374 [[nodiscard]] int getAttachmentCount() const { return static_cast<int>(attachments_.size()); }
375 float getParticleX(int index) const;
377 float getParticleY(int index) const;
379 float getParticleZ(int index) const;
381 void setParticlePosition(int index, float x, float y, float z);
382
384 float getSpacing() const { return spacing_; }
386 float getOriginX() const { return originX_; }
388 float getOriginY() const { return originY_; }
390 float getOriginZ() const { return originZ_; }
391
393 void destroy();
394
395private:
396 friend class LayeredCloth3D;
397 struct Particle {
398 float x = 0.f, y = 0.f, z = 0.f;
399 float px = 0.f, py = 0.f, pz = 0.f;
400 float inverseMass = 1.f;
401 bool pinned = false;
402 };
403 struct Link {
404 int a = 0, b = 0;
405 float rest = 0.f;
406 ClothConstraintKind kind = ClothConstraintKind::Structural;
407 float lambda = 0.f;
408 };
409 struct Tri {
410 int v[3] = {0, 0, 0};
411 };
412 // Dihedral pair: shared edge (a,b), opposite vertices k (tri 1) and l (tri 2).
413 struct FoldPair {
414 int a = 0, b = 0, k = 0, l = 0;
415 };
416 struct Tether {
417 int particle = 0;
418 int anchor = 0;
419 float maxLength = 0.f;
420 float lambda = 0.f;
421 };
422 struct SkinConstraint {
423 int particle = 0;
424 float x = 0.f, y = 0.f, z = 0.f;
425 float nx = 0.f, ny = 1.f, nz = 0.f;
426 float radius = 0.f;
427 float backstopDistance = -1.f;
428 float backstopRadius = 0.f;
429 float compliance = 0.f;
430 float lambda = 0.f;
431 };
432 struct Attachment {
433 int particle = 0;
434 float x = 0.f, y = 0.f, z = 0.f;
435 float compliance = 0.f;
436 float lambda = 0.f;
437 };
438
439 void rebuildLinks();
440 void initializeFromModel(const ClothModel& model);
441 void updateSubsteps(float dt, int substeps);
442 void rebuildTriangles();
443 void buildLinkKeys();
444 bool areLinked(int a, int b) const;
445 void integrate(float dt);
446 void accumulateAerodynamicAcceleration(float dt, std::vector<float>& accelerations) const;
447 void solveConstraints(float dt);
448 void solveTetherConstraints(float dt);
449 void solveVolumeConstraint(float dt);
450 void solveSkinConstraints(float dt);
451 void solveAttachments(float dt);
452 void applyAutomaticTearing();
453 void tearLink(size_t linkIndex);
454 void rebuildFoldPairsFromTriangles();
455 void solveFoldConstraint();
456 void solveSelfCollision();
457 void solveSelfCollisionTriangles();
458 void collideWorld(float dt);
459 void collideBounds();
460 void rebuildHash();
461 bool validIndex(int index) const;
462 int64_t cellKey(int cx, int cy, int cz) const;
463
464 int cols_ = 0;
465 int rows_ = 0;
466 float spacing_ = 0.4f;
467 float originX_ = 0.f, originY_ = 0.f, originZ_ = 0.f;
468
469 float gravityX_ = 0.f;
470 float gravityY_ = -9.8f;
471 float gravityZ_ = 0.f;
472 float stiffness_ = 0.85f;
473 float stretchCompliance_ = 0.f;
474 float shearCompliance_ = 0.f;
475 float bendCompliance_ = 0.f;
476 float tetherScale_ = 0.f;
477 float tetherCompliance_ = 0.f;
478 float pressure_ = 0.f;
479 float volumeCompliance_ = 0.f;
480 float restVolume_ = 0.f;
481 float volumeLambda_ = 0.f;
482 float damping_ = 0.01f;
483 int iterations_ = 4;
484 float particleSize_ = 0.1f;
485 float particleMass_ = 0.1f;
486 bool selfCollision_ = true;
487 float foldStiffness_ = 0.5f;
488 float maxFoldAngle_ = 120.f * 3.14159265f / 180.f;
489 float collisionFriction_ = 0.2f;
490 float collisionRestitution_ = 0.15f;
491 uint64_t collisionCategoryBits_ = ~uint64_t{0};
492 uint64_t collisionMaskBits_ = ~uint64_t{0};
493 float tearThreshold_ = 0.f;
494 int maxTearsPerStep_ = 1;
495 int tornConstraintCount_ = 0;
496
497 bool hasBounds_ = false;
498 float boundX_ = 0.f, boundY_ = 0.f, boundZ_ = 0.f;
499 float boundW_ = 0.f, boundH_ = 0.f, boundD_ = 0.f;
500
501 int grabIndex_ = -1;
502 float grabX_ = 0.f, grabY_ = 0.f, grabZ_ = 0.f;
503 float forceX_ = 0.f, forceY_ = 0.f, forceZ_ = 0.f;
504 float windVelocityX_ = 0.f, windVelocityY_ = 0.f, windVelocityZ_ = 0.f;
505 float airDensity_ = 1.225f;
506 float dragCoefficient_ = 0.f;
507 float liftCoefficient_ = 0.f;
508 float interactX_ = 0.f, interactY_ = 0.f, interactZ_ = 0.f;
509 float interactRadius_ = 0.f;
510 float interactStrength_ = 0.f;
511
512 World3D* world_ = nullptr;
513
514 float colorR_ = 0.75f, colorG_ = 0.82f, colorB_ = 0.95f, colorA_ = 1.f;
515
516 bool destroyed_ = false;
517 SimulationObservation observation_;
518
519 std::vector<Particle> particles_;
520 std::vector<Particle> restParticles_;
521 std::vector<Link> links_;
522 std::vector<Tri> triangles_;
523 std::vector<FoldPair> foldPairs_;
524 std::vector<Tether> tethers_;
525 std::vector<SkinConstraint> skinConstraints_;
526 std::vector<Attachment> attachments_;
527 std::unordered_set<int64_t> linkKeys_;
528 std::unordered_map<int64_t, std::vector<int>> hash_;
529
530 graphics::Mesh* mesh_ = nullptr;
531 int meshVertexCount_ = 0;
532 int meshIndexCount_ = 0;
533};
534
535} // namespace eve::physics
double z
double y
double x
float w
Definition AnimClip.cpp:738
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
float degrees
Definition CardTypes.cpp:38
float inverseMass
#define EVENGINE_API_DOMAINS
Definition Export.h:110
int rows
int cols
float strength
float friction
tensor::Graph g
Definition GpuGraph.cpp:7
double r
float v
eve::ResourcePin pinned
int h
std::vector< Colorf > px
void(* destroy)(void *)
std::array< float, 3 > scale
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
MeleePoint3 a
Definition MeleeHit.cpp:40
MeshVfxBatchedDraw draw
OceanWakeSettings settings
double dt
float d
glm::mat4 model
std::string kind
Backend-neutral, observable fixed-step contract for physics domains.
double restitution
int count
int spacing
int iterations
Definition TreeMesh.cpp:448
float step
Definition TreeMesh.cpp:451
float size
Definition TreeMesh.cpp:158
uint32_t index
Move-only operation result carrying either a value or Status.
Definition Result.h:176
Interactive 3D cloth — Verlet particles + distance constraints. Meter space, +Y up,...
Definition Cloth3D.h:32
float getOriginZ() const
Returns the origin z.
Definition Cloth3D.h:390
World3D * getCollideWorld() const
Returns the attached collision world, or null when detached.
Definition Cloth3D.h:333
std::string getBackendName() const
Return stable backend name for scripts and diagnostics.
Definition Cloth3D.h:77
int getRows() const
Returns the rows.
Definition Cloth3D.h:355
SimulationDeterminism determinism() const noexcept override
CPU cloth uses bounded floating-point determinism.
Definition Cloth3D.h:73
float getColorR() const
Returns the color r.
Definition Cloth3D.h:341
Cloth3D & operator=(const Cloth3D &)=delete
float getGravityX() const
Returns the gravity x.
Definition Cloth3D.h:87
int getCols() const
Returns the cols.
Definition Cloth3D.h:353
bool isGrabbing() const
True when grabbing.
Definition Cloth3D.h:254
float getMaxFoldAngle() const
Returns the max fold angle.
Definition Cloth3D.h:180
float getFoldStiffness() const
Returns the fold stiffness.
Definition Cloth3D.h:170
float getColorG() const
Returns the color g.
Definition Cloth3D.h:343
float getParticleSize() const
Returns the particle size.
Definition Cloth3D.h:147
float getOriginY() const
Returns the origin y.
Definition Cloth3D.h:388
float getParticleMass() const
Returns the particle mass.
Definition Cloth3D.h:165
float getColorB() const
Returns the color b.
Definition Cloth3D.h:345
int getIterations() const
Returns the iterations.
Definition Cloth3D.h:134
float getGravityY() const
Returns the gravity y.
Definition Cloth3D.h:89
SimulationObservation observation() const noexcept override
Returns completed tick/time observables.
Definition Cloth3D.h:69
SimulationBackendKind kind() const noexcept override
Identifies this production CPU cloth backend.
Definition Cloth3D.h:71
int getGrabIndex() const
Returns the grab index.
Definition Cloth3D.h:256
float getGravityZ() const
Returns the gravity z.
Definition Cloth3D.h:91
bool getSelfCollision() const
Returns the self collision.
Definition Cloth3D.h:157
float getDamping() const
Returns the damping.
Definition Cloth3D.h:139
int getParticleCount() const
Returns the particle count.
Definition Cloth3D.h:357
float getSpacing() const
Returns the spacing.
Definition Cloth3D.h:384
float getColorA() const
Returns the color a.
Definition Cloth3D.h:347
int getAttachmentCount() const
Return active attachment count.
Definition Cloth3D.h:374
Cloth3D(const Cloth3D &)=delete
float getStiffness() const
Returns the stiffness.
Definition Cloth3D.h:96
float getOriginX() const
Returns the origin x.
Definition Cloth3D.h:386
Reusable, validated cloth simulation asset.
Definition ClothModel.h:73
Minimal observable contract for physics simulation providers.
Owns multiple cloth sheets and advances their contacts synchronously. @ownership Owns every layer; no...
Box3D rigid-body world. Script coordinates are meters (Box3D native), unlike 2D World which uses pixe...
Definition World3D.h:47
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
SimulationBackendKind
Kind of implementation that owns a simulation step.
ClothConstraintKind
Type of a precomputed distance constraint in a cloth model.
Definition ClothModel.h:17
SimulationDeterminism
Determinism guarantee made by a simulation backend.
One deterministic fixed-step emitted by SimulationClock.
Definition Time.h:158
Observable backend progress shared by CPU and accelerator providers.
Validated solver policy for one simulation step.