载入中...
搜索中...
未找到
ClothModule.cpp
浏览该文件的文档.
2
3#include "common/Exception.h"
5#include "gpgpu/Gpgpu.h"
14
15#include <simplesquirrel/simplesquirrel.hpp>
16
17#include <functional>
18
19namespace eve::cloth {
20
22
23eve::physics::Cloth* Cloth::newCloth(int cols, int rows, float spacing, float originX, float originY) {
24 return new eve::physics::Cloth(cols, rows, spacing, originX, originY);
25}
26
27eve::physics::Cloth3D* Cloth::newCloth3D(int cols, int rows, float spacing, float originX, float originY,
28 float originZ) {
29 return new eve::physics::Cloth3D(cols, rows, spacing, originX, originY, originZ);
30}
31
32std::unique_ptr<eve::physics::Cloth3D> Cloth::createCloth(const eve::physics::ClothModel& model) {
33 return std::make_unique<eve::physics::Cloth3D>(model);
34}
35
36eve::physics::ClothGPU* Cloth::newClothGPU(int cols, int rows, float spacing, float originX, float originY) {
37 auto* gpgpu = eve::ModuleManager::getInstance<eve::gpgpu::Gpgpu>("Gpgpu");
38 if (!gpgpu) gpgpu = eve::gpgpu::Gpgpu::create();
39 return new eve::physics::ClothGPU(gpgpu, cols, rows, spacing, originX, originY);
40}
41
42void Cloth::expose(ssq::Table& table) {
43 auto module = table.addClass(name, Cloth::create, false);
44 expose(module);
45
47 using Cloth3D = eve::physics::Cloth3D;
48 using ClothGPU = eve::physics::ClothGPU;
49 using Layered = eve::physics::LayeredCloth3D;
50 using LayeredGpu = eve::physics::LayeredClothGPU;
51 auto layeredGpu = table.addClass<LayeredGpu>(
52 "LayeredClothGPU",
53 std::function<LayeredGpu*(eve::gpgpu::Gpgpu*, eve::graphics::Graphics*, int, int, int, float, float, float,
54 float, float)>([](eve::gpgpu::Gpgpu* compute, eve::graphics::Graphics* graphics,
55 int count, int cols, int rows, float spacing, float gap, float x,
56 float y, float z) {
57 if (!compute || !graphics) throw eve::Exception("LayeredClothGPU: missing providers");
58 return new LayeredGpu(*compute, *graphics, count, cols, rows, spacing, gap, x, y, z);
59 }),
60 true);
61 layeredGpu.addFunc("update", [](LayeredGpu* self, float dt) {
62 auto result = self->update(dt);
63 if (!result) throw eve::Exception("%s", result.status().describe().c_str());
64 });
65 layeredGpu.addFunc("draw", [](LayeredGpu* self) {
66 auto result = self->draw();
67 if (!result) throw eve::Exception("%s", result.status().describe().c_str());
68 });
69 layeredGpu.addFunc("reset", &LayeredGpu::reset);
70 layeredGpu.addFunc("setSolver", &LayeredGpu::setSolver);
71 layeredGpu.addFunc("setGravity", &LayeredGpu::setGravity);
72 layeredGpu.addFunc("setWind", &LayeredGpu::setWind);
73 layeredGpu.addFunc("setInterCollision", &LayeredGpu::setInterCollision);
74 layeredGpu.addFunc("setSelfCollision", &LayeredGpu::setSelfCollision);
75 layeredGpu.addFunc("setContact", &LayeredGpu::setContact);
76 layeredGpu.addFunc("setFloor", &LayeredGpu::setFloor);
77 layeredGpu.addFunc("setParticleInverseMass", &LayeredGpu::setParticleInverseMass);
78 layeredGpu.addFunc("getParticleCount", &LayeredGpu::getParticleCount);
79
80 // Squirrel ABI transfers ownership of the constructed group to the VM.
81 auto layered =
82 table.addClass<Layered>("LayeredCloth3D",
83 std::function<Layered*(int, int, int, float, float, float, float, float)>(
84 [](int count, int cols, int rows, float spacing, float gap, float x, float y,
85 float z) { return new Layered(count, cols, rows, spacing, gap, x, y, z); }),
86 true);
87 layered.addFunc("update", &Layered::update);
88 layered.addFunc("setSolver", &Layered::setSolver);
89 layered.addFunc("setInterCollision", &Layered::setInterCollision);
90 layered.addFunc("getInterCollision", &Layered::getInterCollision);
91 layered.addFunc("getContactCount", &Layered::getContactCount);
92 layered.addFunc("getLayerCount", &Layered::getLayerCount);
93 layered.addFunc("setWindVelocity", &Layered::setWindVelocity);
94 layered.addFunc("reset", &Layered::reset);
95 layered.addFunc("draw", &Layered::draw);
96
97
98 auto cloth =
99 table.addClass<Cloth>("Cloth2D", std::function<Cloth*()>([]() -> Cloth* { return nullptr; }), true);
100 cloth.addFunc("update", &Cloth::update);
101 cloth.addFunc("setGravity", &Cloth::setGravity);
102 cloth.addFunc("getGravityX", &Cloth::getGravityX);
103 cloth.addFunc("getGravityY", &Cloth::getGravityY);
104 cloth.addFunc("setStiffness", &Cloth::setStiffness);
105 cloth.addFunc("getStiffness", &Cloth::getStiffness);
106 cloth.addFunc("setIterations", &Cloth::setIterations);
107 cloth.addFunc("getIterations", &Cloth::getIterations);
108 cloth.addFunc("setDamping", &Cloth::setDamping);
109 cloth.addFunc("getDamping", &Cloth::getDamping);
110 cloth.addFunc("setParticleSize", &Cloth::setParticleSize);
111 cloth.addFunc("getParticleSize", &Cloth::getParticleSize);
112 cloth.addFunc("setParticleMass", &Cloth::setParticleMass);
113 cloth.addFunc("getParticleMass", &Cloth::getParticleMass);
114 cloth.addFunc("setSelfCollision", &Cloth::setSelfCollision);
115 cloth.addFunc("getSelfCollision", &Cloth::getSelfCollision);
116 cloth.addFunc("setFoldStiffness", &Cloth::setFoldStiffness);
117 cloth.addFunc("getFoldStiffness", &Cloth::getFoldStiffness);
118 cloth.addFunc("setMaxFoldAngle", &Cloth::setMaxFoldAngle);
119 cloth.addFunc("getMaxFoldAngle", &Cloth::getMaxFoldAngle);
120 cloth.addFunc("setBounds", &Cloth::setBounds);
121 cloth.addFunc("clearBounds", &Cloth::clearBounds);
122 cloth.addFunc("pin", &Cloth::pin);
123 cloth.addFunc("unpin", &Cloth::unpin);
124 cloth.addFunc("pinTopRow", &Cloth::pinTopRow);
125 cloth.addFunc("isPinned", &Cloth::isPinned);
126 cloth.addFunc("grabAt", &Cloth::grabAt);
127 cloth.addFunc("moveGrab", &Cloth::moveGrab);
128 cloth.addFunc("releaseGrab", &Cloth::releaseGrab);
129 cloth.addFunc("isGrabbing", &Cloth::isGrabbing);
130 cloth.addFunc("getGrabIndex", &Cloth::getGrabIndex);
131 cloth.addFunc("applyForce", &Cloth::applyForce);
132 cloth.addFunc("interactAt", &Cloth::interactAt);
133 cloth.addFunc("setCollideWorld", &Cloth::setCollideWorld);
134 cloth.addFunc("getCollideWorld", &Cloth::getCollideWorld);
135 cloth.addFunc("reset", &Cloth::reset);
136 cloth.addFunc("setColor", &Cloth::setColor);
137 cloth.addFunc("draw", &Cloth::draw);
138 cloth.addFunc("getCols", &Cloth::getCols);
139 cloth.addFunc("getRows", &Cloth::getRows);
140 cloth.addFunc("getParticleCount", &Cloth::getParticleCount);
141 cloth.addFunc("getParticleX", &Cloth::getParticleX);
142 cloth.addFunc("getParticleY", &Cloth::getParticleY);
143 cloth.addFunc("setParticlePosition", &Cloth::setParticlePosition);
144 cloth.addFunc("getSpacing", &Cloth::getSpacing);
145 cloth.addFunc("destroy", &Cloth::destroy);
146
147 auto cloth3 =
148 table.addClass<Cloth3D>("Cloth3D", std::function<Cloth3D*()>([]() -> Cloth3D* { return nullptr; }), true);
149 cloth3.addFunc("captureBake",
150 [vm = table.getHandle()](Cloth3D* self, const ssq::Object& recordingObject, float seconds) {
151 return eve::physics::bake_detail::appendCapturedDeform(vm, *self, recordingObject, seconds);
152 });
153 cloth3.addFunc("update", &Cloth3D::update);
154 cloth3.addFunc("setGravity", &Cloth3D::setGravity);
155 cloth3.addFunc("getGravityX", &Cloth3D::getGravityX);
156 cloth3.addFunc("getGravityY", &Cloth3D::getGravityY);
157 cloth3.addFunc("getGravityZ", &Cloth3D::getGravityZ);
158 cloth3.addFunc("setStiffness", &Cloth3D::setStiffness);
159 cloth3.addFunc("getStiffness", &Cloth3D::getStiffness);
160 cloth3.addFunc("setStretchCompliance", &Cloth3D::setStretchCompliance);
161 cloth3.addFunc("getStretchCompliance", &Cloth3D::getStretchCompliance);
162 cloth3.addFunc("setShearCompliance", &Cloth3D::setShearCompliance);
163 cloth3.addFunc("getShearCompliance", &Cloth3D::getShearCompliance);
164 cloth3.addFunc("setBendCompliance", &Cloth3D::setBendCompliance);
165 cloth3.addFunc("getBendCompliance", &Cloth3D::getBendCompliance);
166 cloth3.addFunc("setTetherScale", &Cloth3D::setTetherScale);
167 cloth3.addFunc("getTetherScale", &Cloth3D::getTetherScale);
168 cloth3.addFunc("setTetherCompliance", &Cloth3D::setTetherCompliance);
169 cloth3.addFunc("getTetherCompliance", &Cloth3D::getTetherCompliance);
170 cloth3.addFunc("setPressure", &Cloth3D::setPressure);
171 cloth3.addFunc("getPressure", &Cloth3D::getPressure);
172 cloth3.addFunc("getBackendName", &Cloth3D::getBackendName);
173 cloth3.addFunc("supportsFeature", &Cloth3D::supportsFeature);
174 cloth3.addFunc("setVolumeCompliance", &Cloth3D::setVolumeCompliance);
175 cloth3.addFunc("getVolumeCompliance", &Cloth3D::getVolumeCompliance);
176 cloth3.addFunc("getCurrentVolume", &Cloth3D::getCurrentVolume);
177 cloth3.addFunc("setIterations", &Cloth3D::setIterations);
178 cloth3.addFunc("getIterations", &Cloth3D::getIterations);
179 cloth3.addFunc("setDamping", &Cloth3D::setDamping);
180 cloth3.addFunc("getDamping", &Cloth3D::getDamping);
181 cloth3.addFunc("setParticleSize", &Cloth3D::setParticleSize);
182 cloth3.addFunc("getParticleSize", &Cloth3D::getParticleSize);
183 cloth3.addFunc("setParticleMass", &Cloth3D::setParticleMass);
184 cloth3.addFunc("getParticleMass", &Cloth3D::getParticleMass);
185 cloth3.addFunc("setSelfCollision", &Cloth3D::setSelfCollision);
186 cloth3.addFunc("getSelfCollision", &Cloth3D::getSelfCollision);
187 cloth3.addFunc("setFoldStiffness", &Cloth3D::setFoldStiffness);
188 cloth3.addFunc("getFoldStiffness", &Cloth3D::getFoldStiffness);
189 cloth3.addFunc("setMaxFoldAngle", &Cloth3D::setMaxFoldAngle);
190 cloth3.addFunc("getMaxFoldAngle", &Cloth3D::getMaxFoldAngle);
191 cloth3.addFunc("setBounds", &Cloth3D::setBounds);
192 cloth3.addFunc("clearBounds", &Cloth3D::clearBounds);
193 cloth3.addFunc("setCollisionMaterial", &Cloth3D::setCollisionMaterial);
194 cloth3.addFunc("getCollisionFriction", &Cloth3D::getCollisionFriction);
195 cloth3.addFunc("getCollisionRestitution", &Cloth3D::getCollisionRestitution);
196 cloth3.addFunc("setCollisionFilter", &Cloth3D::setCollisionFilter);
197 cloth3.addFunc("getCollisionCategoryBits", &Cloth3D::getCollisionCategoryBits);
198 cloth3.addFunc("getCollisionMaskBits", &Cloth3D::getCollisionMaskBits);
199 cloth3.addFunc("setTearThreshold", &Cloth3D::setTearThreshold);
200 cloth3.addFunc("getTearThreshold", &Cloth3D::getTearThreshold);
201 cloth3.addFunc("setMaxTearsPerStep", &Cloth3D::setMaxTearsPerStep);
202 cloth3.addFunc("getMaxTearsPerStep", &Cloth3D::getMaxTearsPerStep);
203 cloth3.addFunc("tearConstraint", &Cloth3D::tearConstraint);
204 cloth3.addFunc("getTornConstraintCount", &Cloth3D::getTornConstraintCount);
205 cloth3.addFunc("pin", &Cloth3D::pin);
206 cloth3.addFunc("unpin", &Cloth3D::unpin);
207 cloth3.addFunc("pinTopRow", &Cloth3D::pinTopRow);
208 cloth3.addFunc("isPinned", &Cloth3D::isPinned);
209 cloth3.addFunc("setParticleInverseMass", &Cloth3D::setParticleInverseMass);
210 cloth3.addFunc("getParticleInverseMass", &Cloth3D::getParticleInverseMass);
211 cloth3.addFunc("setSkinConstraint", &Cloth3D::setSkinConstraint);
212 cloth3.addFunc("updateSkinReference", &Cloth3D::updateSkinReference);
213 cloth3.addFunc("clearSkinConstraint", &Cloth3D::clearSkinConstraint);
214 cloth3.addFunc("hasSkinConstraint", &Cloth3D::hasSkinConstraint);
215 cloth3.addFunc("attachParticle", &Cloth3D::attachParticle);
216 cloth3.addFunc("updateAttachment", &Cloth3D::updateAttachment);
217 cloth3.addFunc("detachParticle", &Cloth3D::detachParticle);
218 cloth3.addFunc("isAttached", &Cloth3D::isAttached);
219 cloth3.addFunc("grabAt", &Cloth3D::grabAt);
220 cloth3.addFunc("moveGrab", &Cloth3D::moveGrab);
221 cloth3.addFunc("releaseGrab", &Cloth3D::releaseGrab);
222 cloth3.addFunc("isGrabbing", &Cloth3D::isGrabbing);
223 cloth3.addFunc("getGrabIndex", &Cloth3D::getGrabIndex);
224 cloth3.addFunc("applyForce", &Cloth3D::applyForce);
225 cloth3.addFunc("setWindVelocity", &Cloth3D::setWindVelocity);
226 cloth3.addFunc("getWindVelocityX", &Cloth3D::getWindVelocityX);
227 cloth3.addFunc("getWindVelocityY", &Cloth3D::getWindVelocityY);
228 cloth3.addFunc("getWindVelocityZ", &Cloth3D::getWindVelocityZ);
229 cloth3.addFunc("setAerodynamics", &Cloth3D::setAerodynamics);
230 cloth3.addFunc("getAirDensity", &Cloth3D::getAirDensity);
231 cloth3.addFunc("getDragCoefficient", &Cloth3D::getDragCoefficient);
232 cloth3.addFunc("getLiftCoefficient", &Cloth3D::getLiftCoefficient);
233 cloth3.addFunc("interactAt", &Cloth3D::interactAt);
234 cloth3.addFunc("setCollideWorld", &Cloth3D::setCollideWorld);
235 cloth3.addFunc("getCollideWorld", &Cloth3D::getCollideWorld);
236 cloth3.addFunc("reset", &Cloth3D::reset);
237 cloth3.addFunc("setColor", &Cloth3D::setColor);
238 cloth3.addFunc("draw", &Cloth3D::draw);
239 cloth3.addFunc("getCols", &Cloth3D::getCols);
240 cloth3.addFunc("getRows", &Cloth3D::getRows);
241 cloth3.addFunc("getParticleCount", &Cloth3D::getParticleCount);
242 cloth3.addFunc("getTriangleCount", &Cloth3D::getTriangleCount);
243 cloth3.addFunc("getDistanceConstraintCount", &Cloth3D::getDistanceConstraintCount);
244 cloth3.addFunc("getTetherConstraintCount", &Cloth3D::getTetherConstraintCount);
245 cloth3.addFunc("getSkinConstraintCount", &Cloth3D::getSkinConstraintCount);
246 cloth3.addFunc("getAttachmentCount", &Cloth3D::getAttachmentCount);
247 cloth3.addFunc("getParticleX", &Cloth3D::getParticleX);
248 cloth3.addFunc("getParticleY", &Cloth3D::getParticleY);
249 cloth3.addFunc("getParticleZ", &Cloth3D::getParticleZ);
250 cloth3.addFunc("setParticlePosition", &Cloth3D::setParticlePosition);
251 cloth3.addFunc("getSpacing", &Cloth3D::getSpacing);
252 cloth3.addFunc("getOriginX", &Cloth3D::getOriginX);
253 cloth3.addFunc("getOriginY", &Cloth3D::getOriginY);
254 cloth3.addFunc("getOriginZ", &Cloth3D::getOriginZ);
255 cloth3.addFunc("destroy", &Cloth3D::destroy);
256
257 auto clothGpu =
258 table.addClass<ClothGPU>("ClothGPU", std::function<ClothGPU*()>([]() -> ClothGPU* { return nullptr; }), true);
259 clothGpu.addFunc("update", &ClothGPU::update);
260 clothGpu.addFunc("getBackendName", &ClothGPU::getBackendName);
261 clothGpu.addFunc("supportsFeature", &ClothGPU::supportsFeature);
262 clothGpu.addFunc("setGravity", &ClothGPU::setGravity);
263 clothGpu.addFunc("getGravityX", &ClothGPU::getGravityX);
264 clothGpu.addFunc("getGravityY", &ClothGPU::getGravityY);
265 clothGpu.addFunc("setStiffness", &ClothGPU::setStiffness);
266 clothGpu.addFunc("getStiffness", &ClothGPU::getStiffness);
267 clothGpu.addFunc("setIterations", &ClothGPU::setIterations);
268 clothGpu.addFunc("getIterations", &ClothGPU::getIterations);
269 clothGpu.addFunc("setDamping", &ClothGPU::setDamping);
270 clothGpu.addFunc("getDamping", &ClothGPU::getDamping);
271 clothGpu.addFunc("setParticleSize", &ClothGPU::setParticleSize);
272 clothGpu.addFunc("getParticleSize", &ClothGPU::getParticleSize);
273 clothGpu.addFunc("setSelfCollision", &ClothGPU::setSelfCollision);
274 clothGpu.addFunc("getSelfCollision", &ClothGPU::getSelfCollision);
275 clothGpu.addFunc("setBounds", &ClothGPU::setBounds);
276 clothGpu.addFunc("clearBounds", &ClothGPU::clearBounds);
277 clothGpu.addFunc("pin", &ClothGPU::pin);
278 clothGpu.addFunc("unpin", &ClothGPU::unpin);
279 clothGpu.addFunc("pinTopRow", &ClothGPU::pinTopRow);
280 clothGpu.addFunc("isPinned", &ClothGPU::isPinned);
281 clothGpu.addFunc("applyForce", &ClothGPU::applyForce);
282 clothGpu.addFunc("interactAt", &ClothGPU::interactAt);
283 clothGpu.addFunc("setColor", &ClothGPU::setColor);
284 clothGpu.addFunc("draw", &ClothGPU::draw);
285 clothGpu.addFunc("getCols", &ClothGPU::getCols);
286 clothGpu.addFunc("getRows", &ClothGPU::getRows);
287 clothGpu.addFunc("getParticleCount", &ClothGPU::getParticleCount);
288 clothGpu.addFunc("getParticleX", &ClothGPU::getParticleX);
289 clothGpu.addFunc("getParticleY", &ClothGPU::getParticleY);
290 clothGpu.addFunc("getSpacing", &ClothGPU::getSpacing);
291 clothGpu.addFunc("getOriginX", &ClothGPU::getOriginX);
292 clothGpu.addFunc("getOriginY", &ClothGPU::getOriginY);
293 clothGpu.addFunc("reset", &ClothGPU::reset);
294 clothGpu.addFunc("destroy", &ClothGPU::destroy);
295}
296
297void Cloth::expose(ssq::Class& cls) {
298 // Every other module class exposes getName() (see Physics::expose); without it a
299 // script could not identify the module it got back from eve.Cloth().
300 cls.addFunc("getName", &eve::cloth::Cloth::getName);
301 cls.addFunc("newCloth", &eve::cloth::Cloth::newCloth);
302 cls.addFunc("newCloth3D", [vm = cls.getHandle()](eve::cloth::Cloth* self, int cols, int rows, float spacing,
303 float x, float y, float z) {
304 auto owned = script::makeOwnedSquirrelInstance(
305 vm, std::unique_ptr<eve::physics::Cloth3D>(self->newCloth3D(cols, rows, spacing, x, y, z)));
306 if (!owned) throw Exception("Cloth.newCloth3D: failed to publish owned script instance");
307 return std::move(owned).takeValue();
308 });
309 cls.addFunc("newClothGPU", &eve::cloth::Cloth::newClothGPU);
310}
311
312} // namespace eve::cloth
double z
double y
double x
float gap
HSQUIRRELVM vm
Definition ECS.cpp:38
HSQOBJECT cls
Definition ECS.cpp:39
int rows
int cols
Poco::JSON::Object::Ptr result
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:27
double dt
OnnxCompute * compute
glm::mat4 model
int count
Explicit Value/Owned/Borrowed semantics at the Squirrel boundary.
int spacing
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
virtual std::string getName() const =0
Returns the name.
Script-facing owner and factory for 2D, 3D, and GPU cloth runtimes.
Definition ClothModule.h:17
eve::physics::Cloth * newCloth(int cols, int rows, float spacing, float originX, float originY)
Create a caller-owned 2D cloth grid in pixel space.
eve::physics::Cloth3D * newCloth3D(int cols, int rows, float spacing, float originX, float originY, float originZ)
Create a caller-owned 3D cloth grid in meter space.
std::unique_ptr< eve::physics::Cloth3D > createCloth(const eve::physics::ClothModel &model)
Create an independent 3D runtime by copying a validated model.
eve::physics::ClothGPU * newClothGPU(int cols, int rows, float spacing, float originX, float originY)
Create a caller-owned Vulkan-compute 2D cloth grid.
GPGPU module — compute shaders + storage buffers via the active Graphics backend. Uses the graphics q...
Definition Gpgpu.h:23
Interactive 3D cloth — Verlet particles + distance constraints. Meter space, +Y up,...
Definition Cloth3D.h:32
GPU-accelerated 2D cloth — Verlet integration and distance-constraint relaxation run in a Vulkan comp...
Definition ClothGPU.h:31
Reusable, validated cloth simulation asset.
Definition ClothModel.h:73
Interactive 2D cloth — Verlet particles + distance constraints. Pixel space (same convention as Box2D...
Definition Cloth.h:28
Owns multiple cloth sheets and advances their contacts synchronously. @ownership Owns every layer; no...
Vulkan-resident multi-sheet 3D XPBD cloth with explicit backend errors. @ownership Owns buffers,...
std::unordered_map< std::string, SkillDefinition > & table()
Definition Skill.cpp:73
gpgpu::Gpgpu * gpgpu