diff options
| author | Marijn Tamis <[email protected]> | 2017-04-28 14:19:07 +0200 |
|---|---|---|
| committer | Marijn Tamis <[email protected]> | 2017-04-28 14:19:07 +0200 |
| commit | b350eb5f4d44e8448115796144375d79438d74ae (patch) | |
| tree | 8e102e8c28f45a1b87bd335ceee4f33c3d4ee7c2 /NvCloth/samples/SampleBase/scene | |
| parent | Add visual samples. (diff) | |
| download | nvcloth-b350eb5f4d44e8448115796144375d79438d74ae.tar.xz nvcloth-b350eb5f4d44e8448115796144375d79438d74ae.zip | |
NvCloth 1.1.0 Release. (22041545)
Diffstat (limited to 'NvCloth/samples/SampleBase/scene')
36 files changed, 3025 insertions, 74 deletions
diff --git a/NvCloth/samples/SampleBase/scene/Scene.cpp b/NvCloth/samples/SampleBase/scene/Scene.cpp index 130dad3..6296a05 100644 --- a/NvCloth/samples/SampleBase/scene/Scene.cpp +++ b/NvCloth/samples/SampleBase/scene/Scene.cpp @@ -13,14 +13,391 @@ #include <NvCloth/Solver.h> #include <NvCloth/Fabric.h> #include "Renderer.h" +#include "imgui.h" +#include <sstream> +#include "utils/DebugLineRenderBuffer.h" +#include <foundation/PxVec2.h> std::vector<SceneFactory> Scene::sSceneFactories; +unsigned int Scene::mDebugVisualizationFlags; +Scene::SceneDebugRenderParams Scene::sSceneDebugRenderParams; Scene::~Scene() { } +void Scene::UpdateParticleDragging(float dt) +{ + physx::PxVec2 screenResolution( + mSceneController->getRenderer().getScreenWidth(), + mSceneController->getRenderer().getScreenHeight()); + + physx::PxMat33 invScreenTransform; + { + float m[]{2.0f / screenResolution.x, 0.0f, 0.0f, 0.0f, -2.0f / screenResolution.y, 0.0f, -1.0f, 1.0f, 1.0f}; + invScreenTransform = physx::PxMat33(m); + } + + physx::PxVec2 mousePoint; + { + POINT wpt; + GetCursorPos(&wpt); + ScreenToClient(GetDeviceManager()->GetHWND(), &wpt); + int x, y; + x = wpt.x; + y = wpt.y; + physx::PxVec3 pt(invScreenTransform*physx::PxVec3((float)x, (float)y, 1.0f)); + mousePoint.x = pt.x; + mousePoint.y = pt.y; + } + + physx::PxMat44 viewProjectionMatrix; + physx::PxVec3 viewDirection; + physx::PxVec3 viewPoint; + { + auto camera = mSceneController->getRenderer().getCamera(); + auto m1 = camera.GetViewMatrix(); + auto m2 = camera.GetProjMatrix(); + viewProjectionMatrix = physx::PxMat44((float*)&m2.r)*physx::PxMat44((float*)&m1.r); + auto v1 = camera.GetWorldAhead(); + viewDirection = physx::PxVec3(*(physx::PxVec3*)&v1); + auto v2 = camera.GetEyePt(); + viewPoint = physx::PxVec3(*(physx::PxVec3*)&v2); + } + + if(mDraggingParticle.mTrackedCloth) + { + nv::cloth::Cloth* cloth = mDraggingParticle.mTrackedCloth->mCloth; + physx::PxMat44 modelMatrix = GetDebugDrawTransform(*mDraggingParticle.mTrackedCloth); + nv::cloth::Range<physx::PxVec4> particles = cloth->getCurrentParticles(); + nv::cloth::Range<physx::PxVec4> prevParticles = cloth->getPreviousParticles(); + + physx::PxMat44 tmp = viewProjectionMatrix; + auto tmp2 = DirectX::XMMatrixInverse(nullptr, DirectX::XMMATRIX(tmp.front())); + tmp = physx::PxMat44((float*)&tmp2.r); + physx::PxMat44 invMatrix = tmp; + + physx::PxVec3 particleWorld = modelMatrix.transform(particles[mDraggingParticle.mParticleIndex].getXYZ()); + physx::PxVec4 mousePointWorldT = invMatrix.transform(physx::PxVec4(mousePoint.x, mousePoint.y, 1.0, 1.0)); + physx::PxVec3 mousePointWorld = mousePointWorldT.getXYZ() / mousePointWorldT.w; + physx::PxVec3 mouseRayStart = viewPoint; + physx::PxVec3 mouseRayDir = (mousePointWorld - mouseRayStart).getNormalized(); + //float rayT = -viewDirection.dot(mouseRayStart - particleWorld) / viewDirection.dot(mouseRayDir); //intersect plane + float rayT = mDraggingParticle.mDist; + physx::PxVec3 mousePointPlane = mouseRayStart + mouseRayDir*rayT; + + physx::PxVec3 offset = mousePointPlane - particleWorld; + if(offset.magnitudeSquared() > 2.5f*2.5f) + offset = offset.getNormalized()*2.5f; + + for(int i = 0; i < (int)particles.size(); i++) + { + physx::PxVec4 p = particles[i]; + float dist = (p.getXYZ() - particleWorld).magnitude(); + + if(p.w > 0.0f) //Only move dynamic points + { + float weight = max(0.0,min(1.0,0.4-dist)); + physx::PxVec3 point0(prevParticles[i].x, prevParticles[i].y, prevParticles[i].z); + point0 = point0 - weight*offset; + point0 = point0*0.99f + p.getXYZ()*0.01f; + //move previous particle in the opposite direction to avoid invalid configurations in the next solver iteration. + prevParticles[i] = physx::PxVec4(point0.x, point0.y, point0.z, prevParticles[i].w); + } + } + } +} + + +bool Scene::HandleEvent(UINT uMsg, WPARAM wParam, LPARAM lParam) +{ + auto camera = mSceneController->getRenderer().getCamera(); + auto m1 = camera.GetViewMatrix(); + auto m2 = camera.GetProjMatrix(); + + return HandlePickingEvent(uMsg, wParam, lParam, physx::PxMat44((float*)&m2.r)*physx::PxMat44((float*)&m1.r)); +} + +bool Scene::HandlePickingEvent(UINT uMsg, WPARAM wParam, LPARAM lParam, physx::PxMat44 viewProjectionMatrix2) +{ + if(uMsg != WM_LBUTTONDOWN && uMsg != WM_LBUTTONUP) + return false; + + physx::PxVec2 screenResolution( + mSceneController->getRenderer().getScreenWidth(), + mSceneController->getRenderer().getScreenHeight()); + + physx::PxMat33 invScreenTransform; + { + float m[]{2.0f / screenResolution.x, 0.0f, 0.0f, 0.0f, -2.0f / screenResolution.y, 0.0f, -1.0f, 1.0f, 1.0f}; + invScreenTransform = physx::PxMat33(m); + } + + physx::PxVec2 mousePoint; + { + int x, y; + x = LOWORD(lParam); + y = HIWORD(lParam); + physx::PxVec3 pt(invScreenTransform*physx::PxVec3((float)x, (float)y, 1.0f)); + mousePoint.x = pt.x; + mousePoint.y = pt.y; + } + + physx::PxMat44 viewProjectionMatrix; + physx::PxVec3 viewDirection; + physx::PxVec3 viewPoint; + { + auto camera = mSceneController->getRenderer().getCamera(); + auto m1 = camera.GetViewMatrix(); + auto m2 = camera.GetProjMatrix(); + viewProjectionMatrix = physx::PxMat44((float*)&m2.r)*physx::PxMat44((float*)&m1.r); + auto v1 = camera.GetWorldAhead(); + viewDirection = physx::PxVec3(*(physx::PxVec3*)&v1); + auto v2 = camera.GetEyePt(); + viewPoint = physx::PxVec3(*(physx::PxVec3*)&v2); + } + + if(uMsg == WM_LBUTTONDOWN) + { + mDraggingParticle.mDist = 9999999.0f; + mDraggingParticle.mOffset = 9999999.0f; + mDraggingParticle.mTrackedCloth = nullptr; + for(auto it : mClothList) + { + nv::cloth::Cloth* cloth = it->mCloth; + physx::PxMat44 modelMatrix = GetDebugDrawTransform(*it); + nv::cloth::Range<physx::PxVec4> particles = cloth->getCurrentParticles(); + + physx::PxMat44 tmp = viewProjectionMatrix; + auto tmp2 = DirectX::XMMatrixInverse(nullptr, DirectX::XMMATRIX(tmp.front())); + tmp = physx::PxMat44((float*)&tmp2.r); + physx::PxMat44 invMatrix = tmp; + + physx::PxVec4 mousePointWorldT = invMatrix.transform(physx::PxVec4(mousePoint.x, mousePoint.y, 1.0, 1.0)); + physx::PxVec3 mousePointWorld = mousePointWorldT.getXYZ() / mousePointWorldT.w; + physx::PxVec3 mouseRayStart = viewPoint; + physx::PxVec3 mouseRayDir = (mousePointWorld - mouseRayStart).getNormalized(); + + for(int i = 0; i<(int)particles.size(); i++) + { + physx::PxVec4 p = particles[i]; + physx::PxVec4 point(p.x, p.y, p.z, 1.0f); + + float dist = mouseRayDir.dot(point.getXYZ() - mouseRayStart); + float offset = (point.getXYZ() - (dist*mouseRayDir + mouseRayStart)).magnitude(); + + if(offset<0.1f) + { + if(mDraggingParticle.mDist + 0.5f*mDraggingParticle.mOffset>dist + 0.5f*offset) + { + mDraggingParticle.mTrackedCloth = it; + //mDraggingParticle.mOffset = offset; + mDraggingParticle.mDist = dist; + mDraggingParticle.mParticleIndex = i; + } + } + } + } + return true; + } + if(uMsg == WM_LBUTTONUP) + { + mDraggingParticle.mTrackedCloth = nullptr; + return true; + } + return false; +} + +void Scene::drawUI() +{ + static int activeCloth = 0; + + if(ImGui::TreeNode("Cloth Properties")) + { + activeCloth = min(activeCloth, (int)mClothList.size() - 1); + for(int i = 0; i < (int)mClothList.size(); i++) + { + if(i) + ImGui::SameLine(); + nv::cloth::Cloth* cloth = mClothList[i]->mCloth; + std::stringstream clothName; + clothName << "Cloth " << i; + ImGui::RadioButton(clothName.str().c_str(), &activeCloth, i); + } + + nv::cloth::Cloth* cloth = mClothList[activeCloth]->mCloth; + + + { + bool b = cloth->isContinuousCollisionEnabled(); + if(ImGui::Checkbox("Continuous Collision Detection (CCD)", &b)) + cloth->enableContinuousCollision(b); + } + { + physx::PxVec3 f3 = cloth->getDamping(); + if(ImGui::DragFloat3("Damping", &f3.x, 0.02f, 0.0f, 1.0f, "%.2f")) + cloth->setDamping(f3); + float f = f3.maxElement(); + if(ImGui::DragFloat("Damping xyz", &f, 0.02f, 0.0f, 1.0f, "%.2f")) + cloth->setDamping(physx::PxVec3(f,f,f)); + } + { + float f = cloth->getDragCoefficient(); + if(ImGui::DragFloat("Drag Coefficient", &f, 0.02f, 0.0f, 0.99f, "%.2f")) + cloth->setDragCoefficient(f); + } + { + float f = cloth->getFriction(); + if(ImGui::DragFloat("Friction", &f, 0.04f, 0.0f, 2.0f, "%.2f")) + cloth->setFriction(f); + } + { + physx::PxVec3 f3 = cloth->getGravity(); + if(ImGui::DragFloat3("Gravity", &f3.x, 0.5f, -50.0f, 50.0f, "%.1f")) + cloth->setGravity(f3); + } + { + float f = cloth->getLiftCoefficient(); + if(ImGui::DragFloat("Lift Coefficient", &f, 0.02f, 0.0f, 1.0f, "%.2f")) + cloth->setLiftCoefficient(f); + } + { + physx::PxVec3 f3 = cloth->getLinearInertia(); + if(ImGui::DragFloat3("Linear Inertia", &f3.x, 0.02f, 0.0f, 1.0f, "%.2f")) + cloth->setLinearInertia(f3); + float f = f3.maxElement(); + if(ImGui::DragFloat("Linear Inertia xyz", &f, 0.02f, 0.0f, 1.0f, "%.2f")) + cloth->setLinearInertia(physx::PxVec3(f, f, f)); + } + { + float f = cloth->getMotionConstraintScale(); + if(ImGui::DragFloat("Motion Constraint Scale", &f, 0.08f, 0.0f, 4.0f, "%.2f")) + cloth->setMotionConstraintScaleBias(f, cloth->getMotionConstraintBias()); + } + { + float f = cloth->getMotionConstraintBias(); + if(ImGui::DragFloat("Motion Constraint Bias", &f, 0.16f, 0.0f, 8.0f, "%.2f")) + cloth->setMotionConstraintScaleBias(cloth->getMotionConstraintScale(),f); + } + { + float f = cloth->getSelfCollisionDistance(); + if(ImGui::DragFloat("Self Collision Distance", &f, 0.005f, 0.0f, 0.3f, "%.3f")) + cloth->setSelfCollisionDistance(f); + } + { + float f = cloth->getSelfCollisionStiffness(); + if(ImGui::DragFloat("Self Collision Stiffness", &f, 0.02f, 0.0f, 1.0f, "%.2f")) + cloth->setSelfCollisionStiffness(f); + } + { + float f = cloth->getSleepThreshold(); + if(ImGui::DragFloat("Sleep Threshold", &f, 0.02f, 0.0f, 1.0f, "%.2f")) + cloth->setSleepThreshold(f); + } + { + float f = cloth->getStiffnessFrequency(); + if(ImGui::DragFloat("Stiffness Frequency", &f, 1.0f, 0.0f, 600.0f, "%.0f",1.5f)) + cloth->setStiffnessFrequency(f); + } + { + float f = cloth->getSolverFrequency(); + if(ImGui::DragFloat("Solver Frequency", &f, 1.0f, 0.0f, 600.0f, "%.0f", 1.5f)) + cloth->setSolverFrequency(f); + } + { + float f = cloth->getTetherConstraintScale(); + if(ImGui::DragFloat("Tether Constraint Scale", &f, 0.08f, 0.0f, 4.0f, "%.2f")) + cloth->setTetherConstraintScale(f); + } + { + float f = cloth->getTetherConstraintStiffness(); + if(ImGui::DragFloat("Tether Constraint Stiffness", &f, 0.02f, 0.0f, 1.0f, "%.2f")) + cloth->setTetherConstraintStiffness(f); + } + { + physx::PxVec3 f3 = cloth->getWindVelocity(); + if(ImGui::DragFloat3("Wind Velocity", &f3.x, 0.5f, -50.0f, 50.0f, "%.1f")) + cloth->setWindVelocity(f3); + } + ImGui::TreePop(); + } + + if(ImGui::TreeNode("Debug Visualization")) + { + ImGui::CheckboxFlags("Tethers", &mDebugVisualizationFlags, DEBUG_VIS_TETHERS); + ImGui::CheckboxFlags("Constraints", &mDebugVisualizationFlags, DEBUG_VIS_CONSTRAINTS); + if(mDebugVisualizationFlags&DEBUG_VIS_CONSTRAINTS) + { + ImGui::DragInt("Start Constraint Phase Range", &sSceneDebugRenderParams.mVisiblePhaseRangeBegin, 0.05, 0, 30); + ImGui::DragInt("End", &sSceneDebugRenderParams.mVisiblePhaseRangeEnd, 0.05, 0, 30); + } + ImGui::CheckboxFlags("Constraint Stiffness", &mDebugVisualizationFlags, DEBUG_VIS_CONSTRAINTS_STIFFNESS); + + ImGui::CheckboxFlags("Constraint Error", &mDebugVisualizationFlags, DEBUG_VIS_CONSTRAINT_ERROR); + ImGui::CheckboxFlags("Position Delta", &mDebugVisualizationFlags, DEBUG_VIS_POSITION_DELTA); + ImGui::CheckboxFlags("Bounding Box", &mDebugVisualizationFlags, DEBUG_VIS_BOUNDING_BOX); + ImGui::CheckboxFlags("Distance Constraints", &mDebugVisualizationFlags, DEBUG_VIS_DISTANCE_CONSTRAINTS); + + ImGui::TreePop(); + } + + static int activeSolver = 0; + + if(ImGui::TreeNode("Solver Properties")) + { + activeSolver = min(activeSolver, (int)mSolverList.size() - 1); + for(int i = 0; i < (int)mSolverList.size(); i++) + { + if(i) + ImGui::SameLine(); + nv::cloth::Solver* solver = mSolverList[i]; + std::stringstream clothName; + clothName << "Solver " << i; + ImGui::RadioButton(clothName.str().c_str(), &activeSolver, i); + } + + nv::cloth::Solver* solver = mSolverList[activeSolver]; + + { + float f = solver->getInterCollisionDistance(); + if(ImGui::DragFloat("Inter Collision Distance", &f, 0.005f, 0.0f, 2.0f, "%.2f")) + solver->setInterCollisionDistance(f); + } + { + uint32_t i = solver->getInterCollisionNbIterations(); + if(ImGui::DragInt("Inter Collision Iterations", (int*)&i, 0.25, 0, 16)) + solver->setInterCollisionNbIterations(i); + } + { + float f = solver->getInterCollisionStiffness(); + if(ImGui::DragFloat("Inter Collision Stiffness", &f, 0.005f, 0.0f, 1.0f, "%.2f")) + solver->setInterCollisionStiffness(f); + } + ImGui::TreePop(); + } +} + +void Scene::drawDebugVisualization() +{ + if(mDebugVisualizationFlags & DEBUG_VIS_TETHERS) + DebugRenderTethers(); + if(mDebugVisualizationFlags & DEBUG_VIS_CONSTRAINTS) + DebugRenderConstraints(); + if(mDebugVisualizationFlags & DEBUG_VIS_CONSTRAINTS_STIFFNESS) + DebugRenderConstraintStiffness(); + + if(mDebugVisualizationFlags & DEBUG_VIS_CONSTRAINT_ERROR) + DebugRenderConstraintError(); + if(mDebugVisualizationFlags & DEBUG_VIS_POSITION_DELTA) + DebugRenderPositionDelta(); + if(mDebugVisualizationFlags & DEBUG_VIS_BOUNDING_BOX) + DebugRenderBoundingBox(); + if(mDebugVisualizationFlags & DEBUG_VIS_DISTANCE_CONSTRAINTS) + DebugRenderDistanceConstraints(); + +} + namespace { template <typename T> void trackT(std::vector<T>& list, T object) @@ -127,9 +504,13 @@ void Scene::autoDeinitialize() void Scene::doSimulationStep(float dt) { + if(dt < FLT_EPSILON) + return; startSimulationStep(dt); waitForSimulationStep(); updateSimulationGraphics(); + + UpdateParticleDragging(dt); } void Scene::startSimulationStep(float dt) @@ -160,3 +541,337 @@ void Scene::updateSimulationGraphics() } } +physx::PxMat44 Scene::GetDebugDrawTransform(const ClothActor& actor) +{ + physx::PxMat44 transform; + if(actor.mClothRenderable != nullptr) + { + transform = actor.mClothRenderable->getModelMatrix(); + } + else + { + physx::PxVec3 translation = actor.mCloth->getTranslation(); + physx::PxQuat rotation = actor.mCloth->getRotation(); + transform = physx::PxMat44(rotation); + transform[3] = physx::PxVec4(translation.x, translation.y, translation.z, 1.0f); + } + return transform; +} + +void Scene::DebugRenderDistanceConstraints() +{ + DebugLineRenderBuffer* dbl = mSceneController->getDebugLineRenderBuffer(); + + for(auto it : mClothList) + { + nv::cloth::Cloth& cloth = *it->mCloth; + if(cloth.getNumMotionConstraints() == 0) + continue; + + nv::cloth::Factory& factory = cloth.getFactory(); + + physx::PxMat44 transform = GetDebugDrawTransform(*it); + + nv::cloth::MappedRange<physx::PxVec4> particles = cloth.getCurrentParticles(); + std::vector<physx::PxVec4> motionConstraints; + motionConstraints.reserve(cloth.getNumMotionConstraints() * 2); + motionConstraints.resize(cloth.getNumMotionConstraints()); + factory.extractMotionConstraints(cloth, nv::cloth::Range<physx::PxVec4>(&motionConstraints[0], &motionConstraints[0] + motionConstraints.size())); + motionConstraints.resize(cloth.getNumMotionConstraints() * 2); + + nv::cloth::MappedRange<physx::PxVec4> positions = cloth.getCurrentParticles(); + + assert(positions.size() == cloth.getNumMotionConstraints()); + + + //Set to 1 to color code lines based on distance constraint length (as % of max constraint distance in cloth) + //Set to 0 to color code lines based on how close the particle is to the distance constraint (as % of max distance per constraint) +#define SHOW_DISTANCE_COLOR 0 +#if SHOW_DISTANCE_COLOR + float maxDist = 0.0f; + for(int i = (int)(motionConstraints.size() >> 1) - 1; i >= 0; i--) + { + maxDist = max(maxDist, motionConstraints[i].w); + } +#endif + + for(int i = ((int)motionConstraints.size() >> 1) - 1; i >= 0; i--) + { + float l = motionConstraints[i].w; + physx::PxVec3 a = motionConstraints[i].getXYZ(); + physx::PxVec3 b = positions[i].getXYZ(); + physx::PxVec3 d = b - a; + float currentDist = d.magnitude(); + if(d.magnitudeSquared() < 0.00001f) + { + d = physx::PxVec3(0.0f, 0.0f, 1.0f); + } + else + { + d.normalize(); + } + unsigned char clerp; +#if SHOW_DISTANCE_COLOR + clerp = (unsigned char)(max(0.0f,min(1.0f,(l / maxDist)))*255.0f); +#else + clerp = (unsigned char)(max(0.0f, min(1.0f, (currentDist / l)))*255.0f); +#endif + unsigned int c = ((255-clerp) << 8) + clerp; + dbl->addLine(transform, a, a+d*l, c); + } + } +} + +void Scene::DebugRenderTethers() +{ + DebugLineRenderBuffer* dbl = mSceneController->getDebugLineRenderBuffer(); + + for(auto it : mClothList) + { + nv::cloth::Cloth& cloth = *it->mCloth; + nv::cloth::Fabric& fabric = cloth.getFabric(); + if(fabric.getNumTethers() == 0) + continue; + + nv::cloth::Factory& factory = cloth.getFactory(); + + physx::PxMat44 transform = GetDebugDrawTransform(*it); + + nv::cloth::MappedRange<physx::PxVec4> particles = cloth.getCurrentParticles(); + + std::vector<float> tetherLengths; + tetherLengths.resize(fabric.getNumTethers()); + std::vector<uint32_t> anchors; + anchors.resize(fabric.getNumTethers()); + + factory.extractFabricData(fabric, nv::cloth::Range<uint32_t>(0, 0), nv::cloth::Range<uint32_t>(0, 0), nv::cloth::Range<float>(0, 0), nv::cloth::Range<float>(0, 0), nv::cloth::Range<uint32_t>(0, 0), + nv::cloth::Range<uint32_t>(&anchors[0], &anchors[0] + anchors.size()), nv::cloth::Range<float>(&tetherLengths[0], &tetherLengths[0] + tetherLengths.size()), nv::cloth::Range<uint32_t>(0, 0)); + + int particleCount = fabric.getNumParticles(); + + for(int i = 0; i < (int)anchors.size(); i++) + { + float lengthDiff = (particles[anchors[i]].getXYZ() - particles[i].getXYZ()).magnitude() - tetherLengths[i]; + dbl->addLine(transform, particles[anchors[i]].getXYZ(), particles[i%particleCount].getXYZ(), lengthDiff>0.0f?0x0000FF:0x00FFFF); + } + } +} + +void Scene::DebugRenderConstraints() +{ + DebugLineRenderBuffer* dbl = mSceneController->getDebugLineRenderBuffer(); + + for(auto it : mClothList) + { + nv::cloth::Cloth& cloth = *it->mCloth; + nv::cloth::Fabric& fabric = cloth.getFabric(); + if(fabric.getNumIndices() == 0) + continue; + + nv::cloth::Factory& factory = cloth.getFactory(); + + physx::PxMat44 transform = GetDebugDrawTransform(*it); + + nv::cloth::MappedRange<physx::PxVec4> particles = cloth.getCurrentParticles(); + + if(sSceneDebugRenderParams.mVisiblePhaseRangeBegin >= sSceneDebugRenderParams.mVisiblePhaseRangeEnd) + { + //then simply render all constraints in one go + std::vector<uint32_t> indices; + indices.resize(fabric.getNumIndices()); + + factory.extractFabricData(fabric, nv::cloth::Range<uint32_t>(0, 0), nv::cloth::Range<uint32_t>(0, 0), nv::cloth::Range<float>(0, 0), nv::cloth::Range<float>(0, 0), nv::cloth::Range<uint32_t>(&indices[0], &indices[0] + indices.size()), + nv::cloth::Range<uint32_t>(0, 0), nv::cloth::Range<float>(0, 0), nv::cloth::Range<uint32_t>(0, 0)); + + for(int i = 1; i < (int)indices.size(); i+=2) + { + dbl->addLine(transform, particles[indices[i]].getXYZ(), particles[indices[i-1]].getXYZ(), 0x991919); + } + } + else + { + //otherwise we render individual phases + std::vector<uint32_t> indices; + indices.resize(fabric.getNumIndices()); + std::vector<uint32_t> phases; + phases.resize(fabric.getNumPhases()); + std::vector<uint32_t> sets; + sets.resize(fabric.getNumSets()); + + factory.extractFabricData(fabric, nv::cloth::Range<uint32_t>(&phases[0], &phases[0] + phases.size()), nv::cloth::Range<uint32_t>(&sets[0], &sets[0] + sets.size()), nv::cloth::Range<float>(0, 0), nv::cloth::Range<float>(0, 0), nv::cloth::Range<uint32_t>(&indices[0], &indices[0] + indices.size()), + nv::cloth::Range<uint32_t>(0, 0), nv::cloth::Range<float>(0, 0), nv::cloth::Range<uint32_t>(0, 0)); + + uint32_t* iIt = &indices[0]; + for(int phaseIndex = 0; phaseIndex < (int)fabric.getNumPhases(); phaseIndex++) + { + uint32_t* sIt = &sets[phases[phaseIndex]]; + uint32_t* iEnd = &indices[0] + (sIt[0] * 2); + uint32_t* iStart = iIt; + + if(!(phaseIndex >= sSceneDebugRenderParams.mVisiblePhaseRangeBegin && phaseIndex < sSceneDebugRenderParams.mVisiblePhaseRangeEnd)) + { + iIt = iEnd; + continue; + } + + for(iIt; iIt < iEnd; iIt+=2) + { + float c = (float)(iIt - iStart) / (float)(iEnd - iStart); + unsigned char c255 = (unsigned char)(c*255.0f); + + unsigned int colorTable[3] + { + 0xFF0000, + 0x00FF00, + 0x0000FF + }; + unsigned int shiftTable[3] + { + 8, + 0, + 16 + }; + + dbl->addLine(transform, particles[*iIt].getXYZ(), particles[*(iIt+1)].getXYZ(), colorTable[phaseIndex%3] + (c255<< shiftTable[phaseIndex%3])); + } + } + } + } +} + +void Scene::DebugRenderConstraintStiffness() +{ + DebugLineRenderBuffer* dbl = mSceneController->getDebugLineRenderBuffer(); + + for(auto it : mClothList) + { + nv::cloth::Cloth& cloth = *it->mCloth; + nv::cloth::Fabric& fabric = cloth.getFabric(); + if(fabric.getNumIndices() == 0) + continue; + + if(!fabric.getNumStiffnessValues()) + continue; + + nv::cloth::Factory& factory = cloth.getFactory(); + + physx::PxMat44 transform = GetDebugDrawTransform(*it); + + nv::cloth::MappedRange<physx::PxVec4> particles = cloth.getCurrentParticles(); + + std::vector<uint32_t> indices; + indices.resize(fabric.getNumIndices()); + std::vector<float> stiffness; + stiffness.resize(fabric.getNumRestvalues()); + + factory.extractFabricData(fabric, nv::cloth::Range<uint32_t>(0, 0), nv::cloth::Range<uint32_t>(0, 0), nv::cloth::Range<float>(0, 0), nv::cloth::Range<float>(&stiffness[0], &stiffness[0] + stiffness.size()), nv::cloth::Range<uint32_t>(&indices[0], &indices[0] + indices.size()), + nv::cloth::Range<uint32_t>(0, 0), nv::cloth::Range<float>(0, 0), nv::cloth::Range<uint32_t>(0, 0)); + + for(int i = 1; i < (int)indices.size(); i+=2) + { + float c = 1.0f - exp2f(stiffness[i >> 1]); + + unsigned char ca255 = (unsigned char)(c*255.0f*0.8f); + unsigned char cb255 = (unsigned char)((1.0f-c)*255.0f*0.8f); + dbl->addLine(transform, particles[indices[i-1]].getXYZ(), particles[indices[i]].getXYZ(), (ca255<<8) + (cb255<<0)); + } + } +} + +void Scene::DebugRenderConstraintError() +{ + DebugLineRenderBuffer* dbl = mSceneController->getDebugLineRenderBuffer(); + + for(auto it : mClothList) + { + nv::cloth::Cloth& cloth = *it->mCloth; + nv::cloth::Fabric& fabric = cloth.getFabric(); + if(fabric.getNumRestvalues() == 0) { continue; } + nv::cloth::Factory& factory = cloth.getFactory(); + + physx::PxMat44 transform = GetDebugDrawTransform(*it); + + nv::cloth::MappedRange<physx::PxVec4> particles = cloth.getCurrentParticles(); + + std::vector<uint32_t> indices; + indices.resize(fabric.getNumIndices()); + std::vector<float> restLengths; + restLengths.resize(fabric.getNumRestvalues()); + + factory.extractFabricData(fabric, nv::cloth::Range<uint32_t>(0, 0), nv::cloth::Range<uint32_t>(0, 0), nv::cloth::Range<float>(&restLengths[0], &restLengths[0] + restLengths.size()), nv::cloth::Range<float>(0, 0), nv::cloth::Range<uint32_t>(&indices[0], &indices[0] + indices.size()), + nv::cloth::Range<uint32_t>(0, 0), nv::cloth::Range<float>(0, 0), nv::cloth::Range<uint32_t>(0, 0)); + + for(int i = 0; i < (int)indices.size(); i += 2) + { + physx::PxVec4 p0 = particles[indices[i]]; + physx::PxVec4 p1 = particles[indices[i + 1]]; + float restLength = restLengths[i >> 1]; + float length = (p0 - p1).magnitude(); + const float scale = 2.0f; + float error = (length / restLength * 0.5f - 0.5f) * scale + 0.5f; + error = max(0.0f, min(1.0f, error)); + unsigned char c255 = (unsigned char)(error*255.0f*0.8f); + dbl->addLine(transform, p0.getXYZ(), p1.getXYZ(), ((255-c255) << 8) + (c255 << 0)); + } + } +} + +void Scene::DebugRenderPositionDelta() +{ + DebugLineRenderBuffer* dbl = mSceneController->getDebugLineRenderBuffer(); + + for(auto it : mClothList) + { + nv::cloth::Cloth& cloth = *it->mCloth; + + physx::PxMat44 transform = GetDebugDrawTransform(*it); + + nv::cloth::MappedRange<physx::PxVec4> particles1 = cloth.getCurrentParticles(); + nv::cloth::MappedRange<physx::PxVec4> particles0 = cloth.getPreviousParticles(); + + std::vector<physx::PxVec4> lines; + + //scale so that the solver frequency doesn't affect the position delta length assuming 60fps + float iterationsPerFrame = max(1, int(1.0f / 60.0f * cloth.getSolverFrequency() + 0.5f)); + + for(int i = 0; i < (int)particles1.size(); i++) + { + dbl->addVector(transform, particles1[i].getXYZ(), (particles1[i] - particles0[i]).getXYZ()*iterationsPerFrame * 2.0f,0xFFFF00); + } + } +} + +void Scene::DebugRenderBoundingBox() +{ + DebugLineRenderBuffer* dbl = mSceneController->getDebugLineRenderBuffer(); + + for(auto it : mClothList) + { + nv::cloth::Cloth& cloth = *it->mCloth; + + physx::PxMat44 transform = GetDebugDrawTransform(*it); + + physx::PxVec3 c = cloth.getBoundingBoxCenter(); + physx::PxVec3 d = cloth.getBoundingBoxScale(); + physx::PxVec3 dx = physx::PxVec3(d.x, 0.0f, 0.0f); + physx::PxVec3 dy = physx::PxVec3(0.0f, d.y, 0.0f); + physx::PxVec3 dz = physx::PxVec3(0.0f, 0.0f, d.z); + + dbl->addLine(transform,c + dy + dz - dx,c + dy + dz + dx, 0x00FFFF); + dbl->addLine(transform,c + dy - dz - dx,c + dy - dz + dx, 0x00FFFF); + dbl->addLine(transform,c - dy + dz - dx,c - dy + dz + dx, 0x00FFFF); + dbl->addLine(transform,c - dy - dz - dx,c - dy - dz + dx, 0x00FFFF); + dbl->addLine(transform,c + dy + dx - dz,c + dy + dx + dz, 0x00FFFF); + dbl->addLine(transform,c + dy - dx - dz,c + dy - dx + dz, 0x00FFFF); + dbl->addLine(transform,c - dy + dx - dz,c - dy + dx + dz, 0x00FFFF); + dbl->addLine(transform,c - dy - dx - dz,c - dy - dx + dz, 0x00FFFF); + dbl->addLine(transform,c + dz + dx - dy,c + dz + dx + dy, 0x00FFFF); + dbl->addLine(transform,c + dz - dx - dy,c + dz - dx + dy, 0x00FFFF); + dbl->addLine(transform,c - dz + dx - dy,c - dz + dx + dy, 0x00FFFF); + dbl->addLine(transform,c - dz - dx - dy,c - dz - dx + dy, 0x00FFFF); + dbl->addLine(transform,c + dy + dz + dx,c - dy - dz - dx, 0x007777); + dbl->addLine(transform,c + dy + dz - dx,c - dy - dz + dx, 0x007777); + dbl->addLine(transform,c - dy + dz + dx,c + dy - dz - dx, 0x007777); + dbl->addLine(transform,c - dy + dz - dx,c + dy - dz + dx, 0x007777); + } +}
\ No newline at end of file diff --git a/NvCloth/samples/SampleBase/scene/Scene.h b/NvCloth/samples/SampleBase/scene/Scene.h index 3a11ec7..1c14fbd 100644 --- a/NvCloth/samples/SampleBase/scene/Scene.h +++ b/NvCloth/samples/SampleBase/scene/Scene.h @@ -14,6 +14,7 @@ #include <vector> #include <map> #include "scene/SceneController.h" +#include "foundation/PxMat44.h" namespace nv { namespace cloth @@ -33,7 +34,7 @@ struct SceneFactory const char* mName; }; -#define DECLARE_SCENE_NAME(classname, name) static int classname##id = Scene::AddSceneFactory([](SceneController* c) {return (Scene*)new classname(c); }, name); +#define DECLARE_SCENE_NAME(classname, name) static int classname##id = Scene::AddSceneFactory([](SceneController* c) {return static_cast<Scene*>(new classname(c)); }, name); #define DECLARE_SCENE(classname) DECLARE_SCENE_NAME(classname,#classname) class Scene @@ -43,9 +44,13 @@ public: Scene(SceneController* sceneController):mSceneController(sceneController) {} virtual ~Scene(); - virtual void Animate(double dt) { doSimulationStep(dt); } - virtual void drawUI() {} + virtual void Animate(double dt) { doSimulationStep(dt); drawDebugVisualization(); } + void UpdateParticleDragging(float dt); + virtual bool HandleEvent(UINT uMsg, WPARAM wParam, LPARAM lParam); + bool HandlePickingEvent(UINT uMsg, WPARAM wParam, LPARAM lParam, physx::PxMat44 viewProjectionMatrix); + virtual void drawUI(); virtual void drawStatsUI() {} + virtual void drawDebugVisualization(); virtual void onInitialize() = 0; virtual void onTerminate() { autoDeinitialize(); } @@ -86,6 +91,15 @@ protected: private: Scene& operator= (Scene&); // not implemented + physx::PxMat44 GetDebugDrawTransform(const ClothActor& actor); + void DebugRenderDistanceConstraints(); + void DebugRenderTethers(); + void DebugRenderConstraints(); + void DebugRenderConstraintStiffness(); + void DebugRenderConstraintError(); + void DebugRenderPositionDelta(); + void DebugRenderBoundingBox(); + private: SceneController* mSceneController; @@ -98,6 +112,43 @@ private: static std::vector<SceneFactory> sSceneFactories; + enum + { + DEBUG_VIS_DISTANCE_CONSTRAINTS = 1, + DEBUG_VIS_TETHERS = 2, + DEBUG_VIS_CONSTRAINTS = 4, + DEBUG_VIS_CONSTRAINTS_STIFFNESS = 8, + DEBUG_VIS_NORMALS = 16, + DEBUG_VIS_TANGENTS = 32, + DEBUG_VIS_BITANGENTS = 64, + DEBUG_VIS_CONSTRAINT_ERROR = 128, + DEBUG_VIS_POSITION_DELTA = 256, + DEBUG_VIS_ACCELERATION = 512, + DEBUG_VIS_BOUNDING_BOX = 1024, + DEBUG_VIS_LAST + }; + + static unsigned int mDebugVisualizationFlags; + + struct SceneDebugRenderParams + { + //Constraint render params + int mVisiblePhaseRangeBegin; + int mVisiblePhaseRangeEnd; + }; + + static SceneDebugRenderParams sSceneDebugRenderParams; + + //Particle dragging + struct DraggingParticle + { + DraggingParticle() { mTrackedCloth = nullptr; } + ClothActor* mTrackedCloth; + float mDist; + float mOffset; + int mParticleIndex; + }; + DraggingParticle mDraggingParticle; }; diff --git a/NvCloth/samples/SampleBase/scene/SceneController.cpp b/NvCloth/samples/SampleBase/scene/SceneController.cpp index 90b499c..fa35d5d 100644 --- a/NvCloth/samples/SampleBase/scene/SceneController.cpp +++ b/NvCloth/samples/SampleBase/scene/SceneController.cpp @@ -40,6 +40,8 @@ #include "scene/scenes/SimpleScene.h" #include "scene/scenes/WindScene.h" +#include "utils/DebugLineRenderBuffer.h" + JobManager SceneController::sJobManager; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -51,6 +53,9 @@ SceneController::SceneController() : mTimeScale(1.0f), mStartDelay(0.f) mActivePlatform = (int)nv::cloth::Platform::CPU; mCUDAInitialized = false; mDXInitialized = false; + mLeftOverTime = 0.0; + mPaused = false; + mSingleStep = 0; } SceneController::~SceneController() @@ -137,6 +142,8 @@ void SceneController::onInitialize() mFactories[(int)nv::cloth::Platform::DX11] = NvClothCreateFactoryDX11(mGraphicsContextManager); mDXInitialized &= mFactories[(int)nv::cloth::Platform::DX11] != nullptr; } while(false); + + mDebugLineRenderBuffer = new DebugLineRenderBuffer; } void SceneController::onSampleStop() @@ -181,16 +188,38 @@ void SceneController::changeScene(int index) void SceneController::Animate(double dt) { - double simulationStep = dt * mTimeScale; - if(simulationStep > 1.0 / 60.0) + if(mPaused && (mSingleStep <= 0)) + { + //Re render debug lines from last frame + getRenderer().queueRenderBuffer(mDebugLineRenderBuffer); + return; + } + if(mSingleStep > 0) + { + mSingleStep--; + dt = 1.0 / 60.0; + } + + mDebugLineRenderBuffer->clear(); + + mLeftOverTime += dt * mTimeScale; + + double simulationStep = 0.0; + while(mLeftOverTime > 1.0 / 60.0) + simulationStep += 1.0 / 60.0, mLeftOverTime -= 1.0 / 60.0; + if(simulationStep >= 1.0 / 60.0) simulationStep = 1.0 / 60.0; mActiveScene->Animate(simulationStep); + getRenderer().queueRenderBuffer(mDebugLineRenderBuffer); } LRESULT SceneController::MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { + if(mActiveScene->HandleEvent(uMsg, wParam, lParam)) + return 1; + if (uMsg == WM_KEYDOWN) { int iKeyPressed = static_cast<int>(wParam); @@ -202,10 +231,12 @@ LRESULT SceneController::MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPa switch (iKeyPressed) { - case 'R': - return 0; - case 'F': - return 0; + case 'P': + mPaused = !mPaused; + break; + case 'O': + mSingleStep++; + break; default: break; } diff --git a/NvCloth/samples/SampleBase/scene/SceneController.h b/NvCloth/samples/SampleBase/scene/SceneController.h index d527597..9f4f428 100644 --- a/NvCloth/samples/SampleBase/scene/SceneController.h +++ b/NvCloth/samples/SampleBase/scene/SceneController.h @@ -37,6 +37,7 @@ namespace nv class RenderMaterial; class Renderable; +class DebugLineRenderBuffer; struct ClothActor { @@ -91,6 +92,10 @@ public: return mPhysXPlaneRenderMaterial; } + DebugLineRenderBuffer* getDebugLineRenderBuffer() + { + return mDebugLineRenderBuffer; + } private: SceneController& operator= (SceneController&); // not implemented @@ -104,6 +109,7 @@ private: Renderable* mPlane; std::vector<Renderable*> mBoxes; + DebugLineRenderBuffer* mDebugLineRenderBuffer; float mTimeScale; float mStartDelay; @@ -121,8 +127,13 @@ private: ID3D11DeviceContext* mDXDeviceContext; nv::cloth::DxContextManagerCallback* mGraphicsContextManager; - Scene* mActiveScene; - int mActiveSceneIndex; + Scene* mActiveScene; + int mActiveSceneIndex; + double mLeftOverTime; + + bool mPaused; + int mSingleStep; + }; #endif
\ No newline at end of file diff --git a/NvCloth/samples/SampleBase/scene/scenes/CCDScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/CCDScene.cpp new file mode 100644 index 0000000..2d0eaa8 --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/CCDScene.cpp @@ -0,0 +1,153 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "CCDScene.h" +#include "Scene/SceneController.h" +#include <NvClothExt/ClothFabricCooker.h> +#include "ClothMeshGenerator.h" +#include <NvCloth/Fabric.h> +#include <NvCloth/Solver.h> +#include <NvCloth/Cloth.h> +#include <NvCloth/Factory.h> +#include "Renderer.h" +#include "renderer/RenderUtils.h" +#include "windows.h" +#include "utils/MeshGenerator.h" + +DECLARE_SCENE_NAME(CCDScene, "CCD Scene") + +void CCDScene::Animate(double dt) +{ + static float time = 0.0f; + time += dt; + + physx::PxTransform invTranslation(-mOffset - physx::PxVec3(0.f, 10.f, -2.f)); + physx::PxTransform rotation(physx::PxQuat(cosf(time)*physx::PxHalfPi-physx::PxHalfPi,physx::PxVec3(0.0f,1.0f,0.0f))); + physx::PxTransform translation(mOffset + physx::PxVec3(0.f, 10.f, -2.f) + physx::PxVec3(0.0f,0.0f,10.0f*sinf(time))); + physx::PxTransform totalTransform = translation.transform(rotation.transform(invTranslation)); + + physx::PxVec4 spheres[2] = {physx::PxVec4(totalTransform.transform(physx::PxVec3(0.f, 10.f, -2.f) + mOffset),2.0), + physx::PxVec4(totalTransform.transform(physx::PxVec3(0.f, 11.f, 3.f) + mOffset),0.5)}; + + mClothActor[0]->mCloth->setSpheres(nv::cloth::Range<physx::PxVec4>(spheres, spheres + 2), 0, mClothActor[0]->mCloth->getNumSpheres()); + + mCollisionMehs->setTransform(totalTransform); + + Scene::Animate(dt); +} + +void CCDScene::initializeCloth(int index, physx::PxVec3 offset) +{ + mOffset = offset; + + /////////////////////////////////////////////////////////////////////// + ClothMeshData clothMesh; + + physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f)+ mOffset, PxQuat(0, PxVec3(1.f, 0.f, 0.f))); + clothMesh.GeneratePlaneCloth(5.f, 6.f, 69, 79, false, transform); + clothMesh.AttachClothPlaneByAngles(69, 79); + clothMesh.SetInvMasses(0.5f + (float)index * 2.0f); + + mClothActor[index] = new ClothActor; + nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc(); + { + mClothActor[index]->mClothRenderMesh = new ClothRenderMesh(meshDesc); + mClothActor[index]->mClothRenderable = getSceneController()->getRenderer().createRenderable(*(static_cast<IRenderMesh*>(mClothActor[index]->mClothRenderMesh)), *getSceneController()->getDefaultMaterial()); + + float r, g, b; + r = index == 0 ? 1.0f : 0.3f; + g = index == 1 ? 1.0f : 0.3f; + b = index == 2 ? 1.0f : 0.3f; + + mClothActor[index]->mClothRenderable->setColor(DirectX::XMFLOAT4(r, g, b, 1.0f)); + } + + nv::cloth::Vector<int32_t>::Type phaseTypeInfo; + mFabric[index] = NvClothCookFabricFromMesh(getSceneController()->getFactory(), meshDesc, physx::PxVec3(0.0f, 0.0f, 1.0f), &phaseTypeInfo, false); + trackFabric(mFabric[index]); + + // Initialize start positions and masses for the actual cloth instance + // (note: the particle/vertex positions do not have to match the mesh description here. Set the positions to the initial shape of this cloth instance) + std::vector<physx::PxVec4> particlesCopy; + particlesCopy.resize(clothMesh.mVertices.size()); + + physx::PxVec3 clothOffset = transform.getPosition(); + for(int i = 0; i < (int)clothMesh.mVertices.size(); i++) + { + // To put attachment point closer to each other + if(clothMesh.mInvMasses[i] < 1e-6) + clothMesh.mVertices[i] = (clothMesh.mVertices[i] - clothOffset)*0.95f + clothOffset; + + particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles + } + + // Create the cloth from the initial positions/masses and the fabric + mClothActor[index]->mCloth = getSceneController()->getFactory()->createCloth(nv::cloth::Range<physx::PxVec4>(&particlesCopy[0], &particlesCopy[0] + particlesCopy.size()), *mFabric[index]); + particlesCopy.clear(); particlesCopy.shrink_to_fit(); + + mClothActor[index]->mCloth->setGravity(physx::PxVec3(0.0f, -9.8f, 0.0f)); + mClothActor[index]->mCloth->setDamping(physx::PxVec3(0.1f, 0.1f, 0.1f)); + mClothActor[index]->mCloth->setSelfCollisionDistance(0.07f); + mClothActor[index]->mCloth->enableContinuousCollision(true); + mClothActor[index]->mCloth->setSolverFrequency(240); + + + physx::PxVec4 spheres[2] = {physx::PxVec4(physx::PxVec3(0.f, 10.f, -2.f) + mOffset,2.0), + physx::PxVec4(physx::PxVec3(0.f, 11.f, 3.f) + mOffset,0.5)}; + + mClothActor[index]->mCloth->setSpheres(nv::cloth::Range<physx::PxVec4>(spheres, spheres + 2), 0, mClothActor[index]->mCloth->getNumSpheres()); + + uint32_t caps[4]; + caps[0] = 0; + caps[1] = 1; + + mClothActor[index]->mCloth->setCapsules(nv::cloth::Range<uint32_t>(caps, caps + 2), 0, mClothActor[index]->mCloth->getNumCapsules()); + + //create render mesh + auto mesh = MeshGenerator::generateCollisionCapsules(spheres,2,caps,2,-0.05f); + auto renderMesh = new MeshGenerator::MeshGeneratorRenderMesh(mesh); + mCollisionMehs = getSceneController()->getRenderer().createRenderable(*renderMesh, *getSceneController()->getDefaultMaterial()); + trackRenderable(mCollisionMehs); + + // Setup phase configs + std::vector<nv::cloth::PhaseConfig> phases(mFabric[index]->getNumPhases()); + for(int i = 0; i < (int)phases.size(); i++) + { + phases[i].mPhaseIndex = i; + phases[i].mStiffness = 1.0f; + phases[i].mStiffnessMultiplier = 1.0f; + phases[i].mCompressionLimit = 1.0f; + phases[i].mStretchLimit = 1.0f; + } + mClothActor[index]->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back())); + mClothActor[index]->mCloth->setDragCoefficient(0.5f); + mClothActor[index]->mCloth->setLiftCoefficient(0.6f); + + trackClothActor(mClothActor[index]); + + // Add the cloth to the solver for simulation + addClothToSolver(mClothActor[index], mSolver); +} + +void CCDScene::onInitialize() +{ + mSolver = getSceneController()->getFactory()->createSolver(); + trackSolver(mSolver); + + initializeCloth(0, physx::PxVec3(0.0f, 0.0f, 0.0f)); + + { + IRenderMesh* mesh = getSceneController()->getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane); + Renderable* plane = getSceneController()->getRenderer().createRenderable(*mesh, *getSceneController()->getDefaultPlaneMaterial()); + plane->setTransform(PxTransform(PxVec3(0.f, 0.f, 0.f), PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f)))); + plane->setScale(PxVec3(1000.f)); + trackRenderable(plane); + } +} diff --git a/NvCloth/samples/SampleBase/scene/scenes/CCDScene.h b/NvCloth/samples/SampleBase/scene/scenes/CCDScene.h new file mode 100644 index 0000000..e988fb7 --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/CCDScene.h @@ -0,0 +1,37 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef CCD_SCENE_H +#define CCD_SCENE_H + +#include "scene/Scene.h" +#include <foundation/PxVec3.h> + +class CCDScene : public Scene +{ +public: + + CCDScene(SceneController* sceneController): Scene(sceneController) {} + + virtual void Animate(double dt) override; + void initializeCloth(int index, physx::PxVec3 offset); + virtual void onInitialize() override; + +private: + nv::cloth::Fabric* mFabric[1]; + nv::cloth::Solver* mSolver; + ClothActor* mClothActor[1]; + + physx::PxVec3 mOffset; + Renderable* mCollisionMehs; +}; + + +#endif
\ No newline at end of file diff --git a/NvCloth/samples/SampleBase/scene/scenes/CapsuleScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/CapsuleScene.cpp new file mode 100644 index 0000000..e16899c --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/CapsuleScene.cpp @@ -0,0 +1,129 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "CapsuleScene.h" +#include "Scene/SceneController.h" +#include <NvClothExt/ClothFabricCooker.h> +#include "ClothMeshGenerator.h" +#include <NvCloth/Fabric.h> +#include <NvCloth/Solver.h> +#include <NvCloth/Cloth.h> +#include <NvCloth/Factory.h> +#include "Renderer.h" +#include "renderer/RenderUtils.h" +#include "windows.h" +#include "utils/MeshGenerator.h" + +DECLARE_SCENE_NAME(CapsuleScene, "Capsule Scene") + + +void CapsuleScene::initializeCloth(int index, physx::PxVec3 offset) +{ + /////////////////////////////////////////////////////////////////////// + ClothMeshData clothMesh; + + physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f)+ offset, PxQuat(0, PxVec3(1.f, 0.f, 0.f))); + clothMesh.GeneratePlaneCloth(5.f, 6.f, 69, 79, false, transform); + clothMesh.AttachClothPlaneByAngles(69, 79); + clothMesh.SetInvMasses(0.5f + (float)index * 2.0f); + + mClothActor[index] = new ClothActor; + nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc(); + { + mClothActor[index]->mClothRenderMesh = new ClothRenderMesh(meshDesc); + mClothActor[index]->mClothRenderable = getSceneController()->getRenderer().createRenderable(*(static_cast<IRenderMesh*>(mClothActor[index]->mClothRenderMesh)), *getSceneController()->getDefaultMaterial()); + + float r, g, b; + r = index == 0 ? 1.0f : 0.3f; + g = index == 1 ? 1.0f : 0.3f; + b = index == 2 ? 1.0f : 0.3f; + + mClothActor[index]->mClothRenderable->setColor(DirectX::XMFLOAT4(r, g, b, 1.0f)); + } + + nv::cloth::Vector<int32_t>::Type phaseTypeInfo; + mFabric[index] = NvClothCookFabricFromMesh(getSceneController()->getFactory(), meshDesc, physx::PxVec3(0.0f, 0.0f, 1.0f), &phaseTypeInfo, false); + trackFabric(mFabric[index]); + + // Initialize start positions and masses for the actual cloth instance + // (note: the particle/vertex positions do not have to match the mesh description here. Set the positions to the initial shape of this cloth instance) + std::vector<physx::PxVec4> particlesCopy; + particlesCopy.resize(clothMesh.mVertices.size()); + + physx::PxVec3 clothOffset = transform.getPosition(); + for(int i = 0; i < (int)clothMesh.mVertices.size(); i++) + { + // To put attachment point closer to each other + if(clothMesh.mInvMasses[i] < 1e-6) + clothMesh.mVertices[i] = (clothMesh.mVertices[i] - clothOffset)*0.95f + clothOffset; + + particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles + } + + // Create the cloth from the initial positions/masses and the fabric + mClothActor[index]->mCloth = getSceneController()->getFactory()->createCloth(nv::cloth::Range<physx::PxVec4>(&particlesCopy[0], &particlesCopy[0] + particlesCopy.size()), *mFabric[index]); + particlesCopy.clear(); particlesCopy.shrink_to_fit(); + + mClothActor[index]->mCloth->setGravity(physx::PxVec3(0.0f, -9.8f, 0.0f)); + mClothActor[index]->mCloth->setDamping(physx::PxVec3(0.1f, 0.1f, 0.1f)); + + + physx::PxVec4 spheres[2] = {physx::PxVec4(physx::PxVec3(0.f, 10.f, -2.f) + offset,3.0), + physx::PxVec4(physx::PxVec3(0.f, 10.f, 2.f) + offset,1.0)}; + + mClothActor[index]->mCloth->setSpheres(nv::cloth::Range<physx::PxVec4>(spheres, spheres + 2), 0, mClothActor[index]->mCloth->getNumSpheres()); + + uint32_t caps[4]; + caps[0] = 0; + caps[1] = 1; + + mClothActor[index]->mCloth->setCapsules(nv::cloth::Range<uint32_t>(caps, caps + 2), 0, mClothActor[index]->mCloth->getNumCapsules()); + + //create render mesh + auto mesh = MeshGenerator::generateCollisionCapsules(spheres,2,caps,2,-0.05f); + auto renderMesh = new MeshGenerator::MeshGeneratorRenderMesh(mesh); + Renderable* renderable = getSceneController()->getRenderer().createRenderable(*renderMesh, *getSceneController()->getDefaultMaterial()); + trackRenderable(renderable); + + // Setup phase configs + std::vector<nv::cloth::PhaseConfig> phases(mFabric[index]->getNumPhases()); + for(int i = 0; i < (int)phases.size(); i++) + { + phases[i].mPhaseIndex = i; + phases[i].mStiffness = 1.0f; + phases[i].mStiffnessMultiplier = 1.0f; + phases[i].mCompressionLimit = 1.0f; + phases[i].mStretchLimit = 1.0f; + } + mClothActor[index]->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back())); + mClothActor[index]->mCloth->setDragCoefficient(0.5f); + mClothActor[index]->mCloth->setLiftCoefficient(0.6f); + + trackClothActor(mClothActor[index]); + + // Add the cloth to the solver for simulation + addClothToSolver(mClothActor[index], mSolver); +} + +void CapsuleScene::onInitialize() +{ + mSolver = getSceneController()->getFactory()->createSolver(); + trackSolver(mSolver); + + initializeCloth(0, physx::PxVec3(0.0f, 0.0f, 0.0f)); + + { + IRenderMesh* mesh = getSceneController()->getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane); + Renderable* plane = getSceneController()->getRenderer().createRenderable(*mesh, *getSceneController()->getDefaultPlaneMaterial()); + plane->setTransform(PxTransform(PxVec3(0.f, 0.f, 0.f), PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f)))); + plane->setScale(PxVec3(1000.f)); + trackRenderable(plane); + } +} diff --git a/NvCloth/samples/SampleBase/scene/scenes/CapsuleScene.h b/NvCloth/samples/SampleBase/scene/scenes/CapsuleScene.h new file mode 100644 index 0000000..f1f68fd --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/CapsuleScene.h @@ -0,0 +1,33 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef CAPSULE_SCENE_H +#define CAPSULE_SCENE_H + +#include "scene/Scene.h" +#include <foundation/PxVec3.h> + +class CapsuleScene : public Scene +{ +public: + + CapsuleScene(SceneController* sceneController): Scene(sceneController) {} + + void initializeCloth(int index, physx::PxVec3 offset); + virtual void onInitialize() override; + +private: + nv::cloth::Fabric* mFabric[1]; + nv::cloth::Solver* mSolver; + ClothActor* mClothActor[1]; +}; + + +#endif
\ No newline at end of file diff --git a/NvCloth/samples/SampleBase/scene/scenes/ConvexCollisionScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/ConvexCollisionScene.cpp new file mode 100644 index 0000000..28df734 --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/ConvexCollisionScene.cpp @@ -0,0 +1,138 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "ConvexCollisionScene.h" +#include "Scene/SceneController.h" +#include <NvClothExt/ClothFabricCooker.h> +#include "ClothMeshGenerator.h" +#include <NvCloth/Fabric.h> +#include <NvCloth/Solver.h> +#include <NvCloth/Cloth.h> +#include <NvCloth/Factory.h> +#include "Renderer.h" +#include "renderer/RenderUtils.h" +#include "windows.h" +#include "utils/MeshGenerator.h" + +DECLARE_SCENE_NAME(ConvexCollisionScene, "Convex Collision Scene") + + +void ConvexCollisionScene::initializeCloth(int index, physx::PxVec3 offset) +{ + /////////////////////////////////////////////////////////////////////// + ClothMeshData clothMesh; + + physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f)+ offset, PxQuat(0, PxVec3(1.f, 0.f, 0.f))); + clothMesh.GeneratePlaneCloth(5.f, 6.f, 59, 69, false, transform); + clothMesh.AttachClothPlaneByAngles(59, 69); + clothMesh.SetInvMasses(0.5f + (float)index * 2.0f); + + mClothActor[index] = new ClothActor; + nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc(); + { + mClothActor[index]->mClothRenderMesh = new ClothRenderMesh(meshDesc); + mClothActor[index]->mClothRenderable = getSceneController()->getRenderer().createRenderable(*(static_cast<IRenderMesh*>(mClothActor[index]->mClothRenderMesh)), *getSceneController()->getDefaultMaterial()); + + float r, g, b; + r = index == 0 ? 1.0f : 0.3f; + g = index == 1 ? 1.0f : 0.3f; + b = index == 2 ? 1.0f : 0.3f; + + mClothActor[index]->mClothRenderable->setColor(DirectX::XMFLOAT4(r, g, b, 1.0f)); + } + + nv::cloth::Vector<int32_t>::Type phaseTypeInfo; + mFabric[index] = NvClothCookFabricFromMesh(getSceneController()->getFactory(), meshDesc, physx::PxVec3(0.0f, 0.0f, 1.0f), &phaseTypeInfo, false); + trackFabric(mFabric[index]); + + // Initialize start positions and masses for the actual cloth instance + // (note: the particle/vertex positions do not have to match the mesh description here. Set the positions to the initial shape of this cloth instance) + std::vector<physx::PxVec4> particlesCopy; + particlesCopy.resize(clothMesh.mVertices.size()); + + physx::PxVec3 clothOffset = transform.getPosition(); + for(int i = 0; i < (int)clothMesh.mVertices.size(); i++) + { + // To put attachment point closer to each other + if(clothMesh.mInvMasses[i] < 1e-6) + clothMesh.mVertices[i] = (clothMesh.mVertices[i] - clothOffset)*0.95f + clothOffset; + + particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles + } + + // Create the cloth from the initial positions/masses and the fabric + mClothActor[index]->mCloth = getSceneController()->getFactory()->createCloth(nv::cloth::Range<physx::PxVec4>(&particlesCopy[0], &particlesCopy[0] + particlesCopy.size()), *mFabric[index]); + particlesCopy.clear(); particlesCopy.shrink_to_fit(); + + mClothActor[index]->mCloth->setGravity(physx::PxVec3(0.0f, -9.8f, 0.0f)); + mClothActor[index]->mCloth->setDamping(physx::PxVec3(0.1f, 0.1f, 0.1f)); + + //Generate collision planes + std::vector<physx::PxVec4> planes; + uint32_t mask1 = MeshGenerator::generateConvexPolyhedronPlanes(4, 4, physx::PxVec3(0.7f, 10.0f, -1.0f), 1.0f, &planes); + uint32_t mask2 = MeshGenerator::generateConvexPolyhedronPlanes(4, 4, physx::PxVec3(-0.7f, 10.0f, 0.0f), 1.0f, &planes); + MeshGenerator::Mesh mesh1 = MeshGenerator::generateCollisionConvex(planes.data(),mask1,-0.05f,false); + MeshGenerator::Mesh mesh2 = MeshGenerator::generateCollisionConvex(planes.data(), mask2, -0.05f, false); + + //assign as collision data + nv::cloth::Range<const physx::PxVec4> planesR(&planes[0], &planes[0] + planes.size()); + mClothActor[index]->mCloth->setPlanes(planesR, 0, mClothActor[index]->mCloth->getNumPlanes()); + + //assign convex indices + std::vector<uint32_t> indices; + indices.push_back(mask1); + indices.push_back(mask2); + nv::cloth::Range<uint32_t> cind(&indices[0], &indices[0] + indices.size()); + mClothActor[index]->mCloth->setConvexes(cind, 0, mClothActor[index]->mCloth->getNumConvexes()); + + //create render mesh + //scale down the render model to not intersect the triangles so much + auto renderMesh1 = new MeshGenerator::MeshGeneratorRenderMesh(mesh1); + Renderable* renderable1 = getSceneController()->getRenderer().createRenderable(*renderMesh1, *getSceneController()->getDefaultMaterial()); + trackRenderable(renderable1); + auto renderMesh2 = new MeshGenerator::MeshGeneratorRenderMesh(mesh2); + Renderable* renderable2 = getSceneController()->getRenderer().createRenderable(*renderMesh2, *getSceneController()->getDefaultMaterial()); + trackRenderable(renderable2); + + // Setup phase configs + std::vector<nv::cloth::PhaseConfig> phases(mFabric[index]->getNumPhases()); + for(int i = 0; i < (int)phases.size(); i++) + { + phases[i].mPhaseIndex = i; + phases[i].mStiffness = 1.0f; + phases[i].mStiffnessMultiplier = 1.0f; + phases[i].mCompressionLimit = 1.0f; + phases[i].mStretchLimit = 1.0f; + } + mClothActor[index]->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back())); + mClothActor[index]->mCloth->setDragCoefficient(0.1f); + mClothActor[index]->mCloth->setLiftCoefficient(0.1f); + + trackClothActor(mClothActor[index]); + + // Add the cloth to the solver for simulation + addClothToSolver(mClothActor[index], mSolver); +} + +void ConvexCollisionScene::onInitialize() +{ + mSolver = getSceneController()->getFactory()->createSolver(); + trackSolver(mSolver); + + initializeCloth(0, physx::PxVec3(0.0f, 0.0f, 0.0f)); + + { + IRenderMesh* mesh = getSceneController()->getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane); + Renderable* plane = getSceneController()->getRenderer().createRenderable(*mesh, *getSceneController()->getDefaultPlaneMaterial()); + plane->setTransform(PxTransform(PxVec3(0.f, 0.f, 0.f), PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f)))); + plane->setScale(PxVec3(1000.f)); + trackRenderable(plane); + } +} diff --git a/NvCloth/samples/SampleBase/scene/scenes/ConvexCollisionScene.h b/NvCloth/samples/SampleBase/scene/scenes/ConvexCollisionScene.h new file mode 100644 index 0000000..7c9d062 --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/ConvexCollisionScene.h @@ -0,0 +1,33 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef CONVEX_COLLISION_SCENE_H +#define CONVEX_COLLISION_SCENE_H + +#include "scene/Scene.h" +#include <foundation/PxVec3.h> + +class ConvexCollisionScene : public Scene +{ +public: + + ConvexCollisionScene(SceneController* sceneController): Scene(sceneController) {} + + void initializeCloth(int index, physx::PxVec3 offset); + virtual void onInitialize() override; + +private: + nv::cloth::Fabric* mFabric[1]; + nv::cloth::Solver* mSolver; + ClothActor* mClothActor[1]; +}; + + +#endif
\ No newline at end of file diff --git a/NvCloth/samples/SampleBase/scene/scenes/DistanceConstraintScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/DistanceConstraintScene.cpp new file mode 100644 index 0000000..eaac8e1 --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/DistanceConstraintScene.cpp @@ -0,0 +1,100 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "DistanceConstraintScene.h" +#include "Scene/SceneController.h" +#include <NvClothExt/ClothFabricCooker.h> +#include "ClothMeshGenerator.h" +#include <NvCloth/Fabric.h> +#include <NvCloth/Solver.h> +#include <NvCloth/Cloth.h> +#include <NvCloth/Factory.h> +#include "Renderer.h" +#include "renderer/RenderUtils.h" + +DECLARE_SCENE_NAME(DistanceConstraintScene,"Distance Constraint Scene") + +void DistanceConstraintScene::onInitialize() +{ + /////////////////////////////////////////////////////////////////////// + ClothMeshData clothMesh; + + physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f), physx::PxQuat(-0.9f, physx::PxVec3(0.0f, 1.0f, 0.0f)) * physx::PxQuat(PxPiDivFour, physx::PxVec3(1.0f, 0.0f, 0.0f))); + clothMesh.GeneratePlaneCloth(6.f, 7.f, 39, 39, false, transform); + clothMesh.AttachClothPlaneByAngles(39, 39); + + mClothActor = new ClothActor; + nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc(); + { + mClothActor->mClothRenderMesh = new ClothRenderMesh(meshDesc); + mClothActor->mClothRenderable = getSceneController()->getRenderer().createRenderable(*(static_cast<IRenderMesh*>(mClothActor->mClothRenderMesh)), *getSceneController()->getDefaultMaterial()); + mClothActor->mClothRenderable->setColor(getRandomPastelColor()); + } + + nv::cloth::Vector<int32_t>::Type phaseTypeInfo; + mFabric = NvClothCookFabricFromMesh(getSceneController()->getFactory(), meshDesc, physx::PxVec3(0.0f, -1.0f, 0.0f), &phaseTypeInfo, false); + trackFabric(mFabric); + + // Initialize start positions and masses for the actual cloth instance + // (note: the particle/vertex positions do not have to match the mesh description here. Set the positions to the initial shape of this cloth instance) + std::vector<physx::PxVec4> particlesCopy; + particlesCopy.resize(clothMesh.mVertices.size()); + + for(int i = 0; i < (int)clothMesh.mVertices.size(); i++) + { + particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles + } + + // Create the cloth from the initial positions/masses and the fabric + mClothActor->mCloth = getSceneController()->getFactory()->createCloth(nv::cloth::Range<physx::PxVec4>(&particlesCopy[0], &particlesCopy[0] + particlesCopy.size()), *mFabric); + + + //////////////////////////////////// + //// Set distance constraints //// + //////////////////////////////////// + nv::cloth::Range<physx::PxVec4> distanceConstraints = mClothActor->mCloth->getMotionConstraints(); + for(int i = 0; i < (int)distanceConstraints.size(); i++) + { + distanceConstraints[i] = physx::PxVec4(particlesCopy[i].getXYZ(), 0.002f*(i % 800) * 0.002f*(i % 800)); + } + + particlesCopy.clear(); particlesCopy.shrink_to_fit(); + + mClothActor->mCloth->setGravity(physx::PxVec3(0.0f, -9.8f, 0.0f)); + + // Setup phase configs + std::vector<nv::cloth::PhaseConfig> phases(mFabric->getNumPhases()); + for(int i = 0; i < (int)phases.size(); i++) + { + phases[i].mPhaseIndex = i; + phases[i].mStiffness = 1.0f; + phases[i].mStiffnessMultiplier = 1.0f; + phases[i].mCompressionLimit = 1.0f; + phases[i].mStretchLimit = 1.0f; + } + mClothActor->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back())); +// mClothActor->mCloth->setDragCoefficient(0.1f); +// mClothActor->mCloth->setDragCoefficient(0.1f); + + mSolver = getSceneController()->getFactory()->createSolver(); + trackSolver(mSolver); + trackClothActor(mClothActor); + + // Add the cloth to the solver for simulation + addClothToSolver(mClothActor, mSolver); + + { + IRenderMesh* mesh = getSceneController()->getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane); + Renderable* plane = getSceneController()->getRenderer().createRenderable(*mesh, *getSceneController()->getDefaultPlaneMaterial()); + plane->setTransform(PxTransform(PxVec3(0.f, 0.f, 0.f), PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f)))); + plane->setScale(PxVec3(1000.f)); + trackRenderable(plane); + } +} diff --git a/NvCloth/samples/SampleBase/scene/scenes/DistanceConstraintScene.h b/NvCloth/samples/SampleBase/scene/scenes/DistanceConstraintScene.h new file mode 100644 index 0000000..5c61195 --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/DistanceConstraintScene.h @@ -0,0 +1,32 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef DISTANCE_CONSTRAINT_SCENE_H +#define DISTANCE_CONSTRAINT_SCENE_H + +#include "scene/Scene.h" + +class DistanceConstraintScene : public Scene +{ +public: + + DistanceConstraintScene(SceneController* sceneController):Scene(sceneController) {} + + virtual void onInitialize() override; + +private: + nv::cloth::Fabric* mFabric; + nv::cloth::Solver* mSolver; + ClothActor* mClothActor; + +}; + + +#endif
\ No newline at end of file diff --git a/NvCloth/samples/SampleBase/scene/scenes/FreeFallScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/FreeFallScene.cpp index c1f6a00..9cd1121 100644 --- a/NvCloth/samples/SampleBase/scene/scenes/FreeFallScene.cpp +++ b/NvCloth/samples/SampleBase/scene/scenes/FreeFallScene.cpp @@ -74,7 +74,7 @@ void FreeFallScene::initializeCloth(int index, physx::PxVec3 offset) mClothActor[index]->mCloth->setGravity(physx::PxVec3(0.0f, -1.0f, 0.0f)); mClothActor[index]->mCloth->setFriction(0.1); - mClothActor[index]->mCloth->setDragCoefficient(0.5); + mClothActor[index]->mCloth->setDragCoefficient(0.1); mClothActor[index]->mCloth->setLiftCoefficient(0.0); // Setup phase configs @@ -89,8 +89,6 @@ void FreeFallScene::initializeCloth(int index, physx::PxVec3 offset) } mClothActor[index]->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back())); - mSolver = getSceneController()->getFactory()->createSolver(); - trackSolver(mSolver); trackClothActor(mClothActor[index]); // Add the cloth to the solver for simulation @@ -99,12 +97,12 @@ void FreeFallScene::initializeCloth(int index, physx::PxVec3 offset) void FreeFallScene::onInitialize() { - float spaceX = -1.5f; + mSolver = getSceneController()->getFactory()->createSolver(); + trackSolver(mSolver); + float spaceX = -1.1f; for(int i = 0; i < 4; ++i) - { - initializeCloth(i, physx::PxVec3(16.f + float((i+1)*(i+1)) * spaceX, 2.f, -7.f)); - } + initializeCloth(i, physx::PxVec3(8.f + float((i+1)*(i+1)) * spaceX, 2.f, -7.f)); { IRenderMesh* mesh = getSceneController()->getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane); diff --git a/NvCloth/samples/SampleBase/scene/scenes/FrictionScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/FrictionScene.cpp index 3c181ea..3848c6f 100644 --- a/NvCloth/samples/SampleBase/scene/scenes/FrictionScene.cpp +++ b/NvCloth/samples/SampleBase/scene/scenes/FrictionScene.cpp @@ -26,8 +26,8 @@ void FrictionScene::initializeCloth(int index, physx::PxVec3 offset, float frict /////////////////////////////////////////////////////////////////////// ClothMeshData clothMesh; - physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f)+ offset, PxQuat(PxPi / 6.f, PxVec3(1.f, 0.f, 0.f))); - clothMesh.GeneratePlaneCloth(3.f, 3.f, 29, 29, false, transform); + physx::PxMat44 transform = PxTransform(PxVec3(0.f, 9.f, 0.f) + offset, PxQuat(PxPi / 6.f, PxVec3(1.f, 0.f, 0.f))); + clothMesh.GeneratePlaneCloth(4.f, 5.f, 29, 34, false, transform); mClothActor[index] = new ClothActor; nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc(); @@ -47,7 +47,7 @@ void FrictionScene::initializeCloth(int index, physx::PxVec3 offset, float frict particlesCopy.resize(clothMesh.mVertices.size()); physx::PxVec3 clothOffset = transform.getPosition(); - for(int i = 0; i < (int)clothMesh.mVertices.size(); i++) + for (int i = 0; i < (int)clothMesh.mVertices.size(); i++) { // To put attachment point closer to each other if(clothMesh.mInvMasses[i] < 1e-6) @@ -67,7 +67,7 @@ void FrictionScene::initializeCloth(int index, physx::PxVec3 offset, float frict mClothActor[index]->mCloth->setPlanes(planesR, 0, mClothActor[index]->mCloth->getNumPlanes()); std::vector<uint32_t> indices; indices.resize(planes.size()); - for(int i = 0; i < (int)indices.size(); i++) + for (int i = 0; i < (int)indices.size(); i++) indices[i] = 1 << i; nv::cloth::Range<uint32_t> cind(&indices[0], &indices[0] + indices.size()); mClothActor[index]->mCloth->setConvexes(cind, 0, mClothActor[index]->mCloth->getNumConvexes()); @@ -76,7 +76,7 @@ void FrictionScene::initializeCloth(int index, physx::PxVec3 offset, float frict // Setup phase configs std::vector<nv::cloth::PhaseConfig> phases(mFabric[index]->getNumPhases()); - for(int i = 0; i < (int)phases.size(); i++) + for (int i = 0; i < (int)phases.size(); i++) { phases[i].mPhaseIndex = i; phases[i].mStiffness = 1.0f; @@ -87,8 +87,6 @@ void FrictionScene::initializeCloth(int index, physx::PxVec3 offset, float frict mClothActor[index]->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back())); mClothActor[index]->mCloth->setFriction(frictionCoef); - mSolver = getSceneController()->getFactory()->createSolver(); - trackSolver(mSolver); trackClothActor(mClothActor[index]); // Add the cloth to the solver for simulation @@ -97,15 +95,15 @@ void FrictionScene::initializeCloth(int index, physx::PxVec3 offset, float frict void FrictionScene::onInitialize() { - + mSolver = getSceneController()->getFactory()->createSolver(); + trackSolver(mSolver); - float spaceX = -4.f; + float spaceX = -5.f; float frictionDelta = 0.2f; - for(int i = 0; i < 4; ++i) + for (int i = 0; i < 5; ++i) { - float friction = i > 0 ? float(i) * frictionDelta : 0.f; // 0.0, 0.2, 0.4, 0.6 - + float friction = i > 0 ? float(i) * frictionDelta : 0.f; // 0.0, 0.2, 0.4, 0.6, 0.8 initializeCloth(i, physx::PxVec3(4.f + float(i) * spaceX, 4.f, -18.f), friction); } diff --git a/NvCloth/samples/SampleBase/scene/scenes/FrictionScene.h b/NvCloth/samples/SampleBase/scene/scenes/FrictionScene.h index 4173e09..8d03307 100644 --- a/NvCloth/samples/SampleBase/scene/scenes/FrictionScene.h +++ b/NvCloth/samples/SampleBase/scene/scenes/FrictionScene.h @@ -24,9 +24,9 @@ public: virtual void onInitialize() override; private: - nv::cloth::Fabric* mFabric[4]; + nv::cloth::Fabric* mFabric[5]; nv::cloth::Solver* mSolver; - ClothActor* mClothActor[4]; + ClothActor* mClothActor[5]; }; diff --git a/NvCloth/samples/SampleBase/scene/scenes/GeodesicScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/GeodesicScene.cpp new file mode 100644 index 0000000..3216dca --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/GeodesicScene.cpp @@ -0,0 +1,114 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "GeodesicScene.h" +#include "Scene/SceneController.h" +#include <NvClothExt/ClothFabricCooker.h> +#include "ClothMeshGenerator.h" +#include <NvCloth/Fabric.h> +#include <NvCloth/Solver.h> +#include <NvCloth/Cloth.h> +#include <NvCloth/Factory.h> +#include "Renderer.h" +#include "renderer/RenderUtils.h" + +DECLARE_SCENE_NAME(GeodesicScene, "Geodesic Scene") + +void GeodesicScene::initializeCloth(int index, physx::PxVec3 offset, bool geodesic) +{ + /////////////////////////////////////////////////////////////////////// + ClothMeshData clothMesh; + + physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f) + offset, PxQuat(0, PxVec3(1.f, 0.f, 0.f))); + clothMesh.GeneratePlaneCloth(6.f, 10.0f, 39, 59, false, transform,true,true); + clothMesh.AttachClothPlaneByAngles(39, 59); + + /*for(int y = 0; y < 60; y++) + { + for(int x = 0; x < 40; x++) + { + //clothMesh.mVertices[x + y * 40].y = transform.transform(PxVec3(0.0f, y&2?0.25f:-0.25f, 0.0f)).y; + PxVec3 pos = transform.transform(PxVec3(0.0f, y & 1 ? 0.1f : -0.1f, 10.0f * (float)(y>>2)/(float)60.0f)); + clothMesh.mVertices[x + y * 40].y = pos.y; + clothMesh.mVertices[x + y * 40].z = pos.z; + } + }*/ + + mClothActor[index] = new ClothActor; + nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc(); + { + mClothActor[index]->mClothRenderMesh = new ClothRenderMesh(meshDesc); + mClothActor[index]->mClothRenderable = getSceneController()->getRenderer().createRenderable(*(static_cast<IRenderMesh*>(mClothActor[index]->mClothRenderMesh)), *getSceneController()->getDefaultMaterial()); + mClothActor[index]->mClothRenderable->setColor(getRandomPastelColor()); + } + + nv::cloth::Vector<int32_t>::Type phaseTypeInfo; + mFabric[index] = NvClothCookFabricFromMesh(getSceneController()->getFactory(), meshDesc, physx::PxVec3(0.0f, 0.0f, 1.0f), + &phaseTypeInfo, geodesic); + trackFabric(mFabric[index]); + + // Initialize start positions and masses for the actual cloth instance + // (note: the particle/vertex positions do not have to match the mesh description here. Set the positions to the initial shape of this cloth instance) + std::vector<physx::PxVec4> particlesCopy; + particlesCopy.resize(clothMesh.mVertices.size()); + + physx::PxVec3 clothOffset = transform.getPosition(); + for (int i = 0; i < (int)clothMesh.mVertices.size(); i++) + { + // To put attachment point closer to each other + if(clothMesh.mInvMasses[i] < 1e-6) + clothMesh.mVertices[i] = (clothMesh.mVertices[i] - clothOffset)*0.99f + clothOffset; + + particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles + } + + // Create the cloth from the initial positions/masses and the fabric + mClothActor[index]->mCloth = getSceneController()->getFactory()->createCloth(nv::cloth::Range<physx::PxVec4>(&particlesCopy[0], &particlesCopy[0] + particlesCopy.size()), *mFabric[index]); + particlesCopy.clear(); particlesCopy.shrink_to_fit(); + + mClothActor[index]->mCloth->setGravity(physx::PxVec3(0.0f, -9.8f, 0.0f)); + mClothActor[index]->mCloth->setTetherConstraintStiffness(1.0f); + + // Setup phase configs + std::vector<nv::cloth::PhaseConfig> phases(mFabric[index]->getNumPhases()); + for (int i = 0; i < (int)phases.size(); i++) + { + phases[i].mPhaseIndex = i; + phases[i].mStiffness = 1.0f; + phases[i].mStiffnessMultiplier = 1.0f; + phases[i].mCompressionLimit = 1.0f; + phases[i].mStretchLimit = 1.0f; + } + mClothActor[index]->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back())); + + trackClothActor(mClothActor[index]); + + // Add the cloth to the solver for simulation + addClothToSolver(mClothActor[index], mSolver); +} + +void GeodesicScene::onInitialize() +{ + mSolver = getSceneController()->getFactory()->createSolver(); + trackSolver(mSolver); + + initializeCloth(0, physx::PxVec3(-5.0f, 0.0f, 0.0f), false); + initializeCloth(1, physx::PxVec3(4.0f, 0.0f, 0.0f), true); + + mTime = 0.0f; + + { + IRenderMesh* mesh = getSceneController()->getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane); + Renderable* plane = getSceneController()->getRenderer().createRenderable(*mesh, *getSceneController()->getDefaultPlaneMaterial()); + plane->setTransform(PxTransform(PxVec3(0.f, 0.f, 0.f), PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f)))); + plane->setScale(PxVec3(1000.f)); + trackRenderable(plane); + } +} diff --git a/NvCloth/samples/SampleBase/scene/scenes/GeodesicScene.h b/NvCloth/samples/SampleBase/scene/scenes/GeodesicScene.h new file mode 100644 index 0000000..3a0822e --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/GeodesicScene.h @@ -0,0 +1,36 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef GEODESIC_SCENE_H +#define GEODESIC_SCENE_H + +#include "scene/Scene.h" +#include <foundation/PxVec3.h> + +class GeodesicScene : public Scene +{ +public: + + GeodesicScene(SceneController* sceneController): Scene(sceneController) {} + + void initializeCloth(int index, physx::PxVec3 offset, bool geodesic); + virtual void onInitialize() override; + +private: + nv::cloth::Fabric* mFabric[2]; + nv::cloth::Solver* mSolver; + ClothActor* mClothActor[2]; + + float mTime; + +}; + + +#endif
\ No newline at end of file diff --git a/NvCloth/samples/SampleBase/scene/scenes/InterCollisionScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/InterCollisionScene.cpp new file mode 100644 index 0000000..b75facd --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/InterCollisionScene.cpp @@ -0,0 +1,128 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "InterCollisionScene.h" +#include "Scene/SceneController.h" +#include <NvClothExt/ClothFabricCooker.h> +#include "ClothMeshGenerator.h" +#include <NvCloth/Fabric.h> +#include <NvCloth/Solver.h> +#include <NvCloth/Cloth.h> +#include <NvCloth/Factory.h> +#include "Renderer.h" +#include "renderer/RenderUtils.h" + +DECLARE_SCENE_NAME(InterCollisionScene, "Inter Collision Scene") + +void InterCollisionScene::initializeCloth(int index, physx::PxMat44 transform) +{ + /////////////////////////////////////////////////////////////////////// + ClothMeshData clothMesh; + float w = 5.f - index; + float h = 6.f + index; + transform *= PxTransform(PxVec3(0.f, 13.f, 0.f), PxQuat(0, PxVec3(1.f, 0.f, 0.f))); + clothMesh.GeneratePlaneCloth(w,h, 5*w, 5*h, false, transform); + clothMesh.AttachClothPlaneByAngles(5 * w, 5 * h); + //clothMesh.SetInvMasses(1.0f / (1000.0f / (5.0f*w*5.0f*h))); + + mClothActor[index] = new ClothActor; + nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc(); + { + mClothActor[index]->mClothRenderMesh = new ClothRenderMesh(meshDesc); + mClothActor[index]->mClothRenderable = getSceneController()->getRenderer().createRenderable(*(static_cast<IRenderMesh*>(mClothActor[index]->mClothRenderMesh)), *getSceneController()->getDefaultMaterial()); + mClothActor[index]->mClothRenderable->setColor(getRandomPastelColor()); + } + + nv::cloth::Vector<int32_t>::Type phaseTypeInfo; + mFabric[index] = NvClothCookFabricFromMesh(getSceneController()->getFactory(), meshDesc, physx::PxVec3(0.0f, 0.0f, 1.0f), &phaseTypeInfo, false); + trackFabric(mFabric[index]); + + // Initialize start positions and masses for the actual cloth instance + // (note: the particle/vertex positions do not have to match the mesh description here. Set the positions to the initial shape of this cloth instance) + std::vector<physx::PxVec4> particlesCopy; + particlesCopy.resize(clothMesh.mVertices.size()); + + physx::PxVec3 clothOffset = transform.getPosition(); + for(int i = 0; i < (int)clothMesh.mVertices.size(); i++) + { + // To put attachment point closer to each other + if(clothMesh.mInvMasses[i] < 1e-6) + clothMesh.mVertices[i] = (clothMesh.mVertices[i] - clothOffset)*0.9f + clothOffset; + + particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles + } + + // Create the cloth from the initial positions/masses and the fabric + mClothActor[index]->mCloth = getSceneController()->getFactory()->createCloth(nv::cloth::Range<physx::PxVec4>(&particlesCopy[0], &particlesCopy[0] + particlesCopy.size()), *mFabric[index]); + particlesCopy.clear(); particlesCopy.shrink_to_fit(); + + std::vector<physx::PxVec4> planes; + planes.push_back(physx::PxVec4(physx::PxVec3(0.0f, 1.f, 0.0f), -0.01f)); + + nv::cloth::Range<const physx::PxVec4> planesR(&planes[0], &planes[0] + planes.size()); + mClothActor[index]->mCloth->setPlanes(planesR, 0, mClothActor[index]->mCloth->getNumPlanes()); + std::vector<uint32_t> indices; + indices.resize(planes.size()); + for(int i = 0; i < (int)indices.size(); i++) + indices[i] = 1 << i; + nv::cloth::Range<uint32_t> cind(&indices[0], &indices[0] + indices.size()); + mClothActor[index]->mCloth->setConvexes(cind, 0, mClothActor[index]->mCloth->getNumConvexes()); + + mClothActor[index]->mCloth->setGravity(physx::PxVec3(0.0f, -1.0f, 0.0f)); + mClothActor[index]->mCloth->setFriction(0.1); + mClothActor[index]->mCloth->setDragCoefficient(0.1f); + mClothActor[index]->mCloth->setLiftCoefficient(0.1f); + mClothActor[index]->mCloth->setSolverFrequency(120.0f); + + // Setup phase configs + std::vector<nv::cloth::PhaseConfig> phases(mFabric[index]->getNumPhases()); + for(int i = 0; i < (int)phases.size(); i++) + { + phases[i].mPhaseIndex = i; + phases[i].mStiffness = 1.0f; + phases[i].mStiffnessMultiplier = 1.0f; + phases[i].mCompressionLimit = 1.0f; + phases[i].mStretchLimit = 1.0f; + } + mClothActor[index]->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back())); + + trackClothActor(mClothActor[index]); + + // Add the cloth to the solver for simulation + addClothToSolver(mClothActor[index], mSolver); +} + +void InterCollisionScene::onInitialize() +{ + mSolver = getSceneController()->getFactory()->createSolver(); + mSolver->setInterCollisionNbIterations(8); + mSolver->setInterCollisionDistance(0.4f); + mSolver->setInterCollisionStiffness(0.95f); + mSolver->setInterCollisionFilter( + [](void* a, void* b) { + return true; + } + ); + trackSolver(mSolver); + + physx::PxMat44 posTrans(physx::PxIdentity); + posTrans.setPosition(physx::PxVec3(0.0f, 0.f, -1.0f)); + initializeCloth(0, posTrans); + posTrans.setPosition(physx::PxVec3(0.0f, 0.8f, -1.2f)); + initializeCloth(1, posTrans); + + { + IRenderMesh* mesh = getSceneController()->getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane); + Renderable* plane = getSceneController()->getRenderer().createRenderable(*mesh, *getSceneController()->getDefaultPlaneMaterial()); + plane->setTransform(PxTransform(PxVec3(0.f, 0.f, 0.f), PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f)))); + plane->setScale(PxVec3(1000.f)); + trackRenderable(plane); + } +} diff --git a/NvCloth/samples/SampleBase/scene/scenes/InterCollisionScene.h b/NvCloth/samples/SampleBase/scene/scenes/InterCollisionScene.h new file mode 100644 index 0000000..1ef0b45 --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/InterCollisionScene.h @@ -0,0 +1,33 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef INTER_COLLISION_SCENE_H +#define INTER_COLLISION_SCENE_H + +#include "scene/Scene.h" +#include <foundation/PxVec3.h> + +class InterCollisionScene : public Scene +{ +public: + + InterCollisionScene(SceneController* sceneController): Scene(sceneController) {} + + void initializeCloth(int index, physx::PxMat44 transform); + virtual void onInitialize() override; + +private: + nv::cloth::Fabric* mFabric[2]; + nv::cloth::Solver* mSolver; + ClothActor* mClothActor[2]; +}; + + +#endif
\ No newline at end of file diff --git a/NvCloth/samples/SampleBase/scene/scenes/LocalGlobalScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/LocalGlobalScene.cpp new file mode 100644 index 0000000..57cd1a2 --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/LocalGlobalScene.cpp @@ -0,0 +1,141 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "LocalGlobalScene.h" +#include "Scene/SceneController.h" +#include <NvClothExt/ClothFabricCooker.h> +#include "ClothMeshGenerator.h" +#include <NvCloth/Fabric.h> +#include <NvCloth/Solver.h> +#include <NvCloth/Cloth.h> +#include <NvCloth/Factory.h> +#include "Renderer.h" +#include "renderer/RenderUtils.h" +#include "windows.h" + +DECLARE_SCENE_NAME(LocalGlobalScene, "Local/Global Scene") + +void LocalGlobalScene::Animate(double dt) +{ + physx::PxVec3 position(sin(mTime * 2.0f) * 3.0f, sinf(mTime) * 2.0f, cosf(mTime) - 1.0f); + physx::PxQuat rotation(sin(mTime * 1.0f) * 4.0f, physx::PxVec3(0.0f, 1.0f, 0.0f)); + + + mTime += dt; + + mClothActor[0]->mCloth->setTranslation(position); + mClothActor[0]->mCloth->setRotation(rotation); + mClothActor[0]->mClothRenderable->setTransform(physx::PxTransform(position + physx::PxVec3(-4.f, 0.f, 0.f), rotation)); + + mClothActor[1]->mClothRenderable->setTransform(physx::PxTransform(physx::PxVec3(4.f, 0.f, 0.f), physx::PxQuat(1.f))); + { + nv::cloth::MappedRange<physx::PxVec4> particles = mClothActor[1]->mCloth->getCurrentParticles(); + for (int i = 0; i < 2; i++) + { + particles[mAttachmentVertices[i]] = physx::PxVec4(physx::PxTransform(position, rotation).transform(mAttachmentVertexOriginalPositions[i].getXYZ()), mAttachmentVertexOriginalPositions[i].w); + } + } + Scene::Animate(dt); +} + +void LocalGlobalScene::initializeCloth(int index, physx::PxVec3 offset) +{ + /////////////////////////////////////////////////////////////////////// + ClothMeshData clothMesh; + + physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f)+ offset, PxQuat(PxPi / 2.0f, PxVec3(1.f, 0.f, 0.f))); + clothMesh.GeneratePlaneCloth(5.f, 6.f, 69, 79, false, transform); + clothMesh.AttachClothPlaneByAngles(69, 79); + clothMesh.SetInvMasses(0.5f + (float)index * 2.0f); + + mClothActor[index] = new ClothActor; + nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc(); + { + mClothActor[index]->mClothRenderMesh = new ClothRenderMesh(meshDesc); + mClothActor[index]->mClothRenderable = getSceneController()->getRenderer().createRenderable(*(static_cast<IRenderMesh*>(mClothActor[index]->mClothRenderMesh)), *getSceneController()->getDefaultMaterial()); + + float r, g, b; + r = index == 0 ? 1.0f : 0.3f; + g = index == 1 ? 1.0f : 0.3f; + b = index == 2 ? 1.0f : 0.3f; + + mClothActor[index]->mClothRenderable->setColor(DirectX::XMFLOAT4(r, g, b, 1.0f)); + } + + nv::cloth::Vector<int32_t>::Type phaseTypeInfo; + mFabric[index] = NvClothCookFabricFromMesh(getSceneController()->getFactory(), meshDesc, physx::PxVec3(0.0f, 0.0f, 1.0f), &phaseTypeInfo, false); + trackFabric(mFabric[index]); + + // Initialize start positions and masses for the actual cloth instance + // (note: the particle/vertex positions do not have to match the mesh description here. Set the positions to the initial shape of this cloth instance) + std::vector<physx::PxVec4> particlesCopy; + particlesCopy.resize(clothMesh.mVertices.size()); + + physx::PxVec3 clothOffset = transform.getPosition(); + for (int i = 0; i < (int)clothMesh.mVertices.size(); i++) + { + // To put attachment point closer to each other + if(clothMesh.mInvMasses[i] < 1e-6) + clothMesh.mVertices[i] = (clothMesh.mVertices[i] - clothOffset)*0.95f + clothOffset; + + particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles + } + + if(index == 1) + { + mAttachmentVertices[0] = 0; + mAttachmentVertices[1] = 69; + mAttachmentVertexOriginalPositions[0] = particlesCopy[mAttachmentVertices[0]]; + mAttachmentVertexOriginalPositions[1] = particlesCopy[mAttachmentVertices[1]]; + } + + // Create the cloth from the initial positions/masses and the fabric + mClothActor[index]->mCloth = getSceneController()->getFactory()->createCloth(nv::cloth::Range<physx::PxVec4>(&particlesCopy[0], &particlesCopy[0] + particlesCopy.size()), *mFabric[index]); + particlesCopy.clear(); particlesCopy.shrink_to_fit(); + + mClothActor[index]->mCloth->setGravity(physx::PxVec3(0.0f, -9.8f, 0.0f)); + + // Setup phase configs + std::vector<nv::cloth::PhaseConfig> phases(mFabric[index]->getNumPhases()); + for (int i = 0; i < (int)phases.size(); i++) + { + phases[i].mPhaseIndex = i; + phases[i].mStiffness = 1.0f; + phases[i].mStiffnessMultiplier = 1.0f; + phases[i].mCompressionLimit = 1.0f; + phases[i].mStretchLimit = 1.0f; + } + mClothActor[index]->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back())); + mClothActor[index]->mCloth->setDragCoefficient(0.1f); + mClothActor[index]->mCloth->setLiftCoefficient(0.2f); + + mSolver[index] = getSceneController()->getFactory()->createSolver(); + trackSolver(mSolver[index]); + trackClothActor(mClothActor[index]); + + // Add the cloth to the solver for simulation + addClothToSolver(mClothActor[index], mSolver[index]); +} + +void LocalGlobalScene::onInitialize() +{ + initializeCloth(1, physx::PxVec3(0.0f, 0.0f, 0.0f)); + initializeCloth(0, physx::PxVec3(0.0f, 0.0f, 0.0f)); + + mTime = 0.0f; + + { + IRenderMesh* mesh = getSceneController()->getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane); + Renderable* plane = getSceneController()->getRenderer().createRenderable(*mesh, *getSceneController()->getDefaultPlaneMaterial()); + plane->setTransform(PxTransform(PxVec3(0.f, 0.f, 0.f), PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f)))); + plane->setScale(PxVec3(1000.f)); + trackRenderable(plane); + } +} diff --git a/NvCloth/samples/SampleBase/scene/scenes/LocalGlobalScene.h b/NvCloth/samples/SampleBase/scene/scenes/LocalGlobalScene.h new file mode 100644 index 0000000..3b2bfa6 --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/LocalGlobalScene.h @@ -0,0 +1,40 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef LOCAL_GLOBAL_SCENE_H +#define LOCAL_GLOBAL_SCENE_H + +#include "scene/Scene.h" +#include <foundation/PxVec3.h> + +class LocalGlobalScene : public Scene +{ +public: + + LocalGlobalScene(SceneController* sceneController): Scene(sceneController) {} + + virtual void Animate(double dt) override; + void initializeCloth(int index, physx::PxVec3 offset); + virtual void onInitialize() override; + +private: + nv::cloth::Fabric* mFabric[2]; + nv::cloth::Solver* mSolver[2]; + ClothActor* mClothActor[2]; + + int mAttachmentVertices[2]; + physx::PxVec4 mAttachmentVertexOriginalPositions[2]; + + float mTime; + +}; + + +#endif
\ No newline at end of file diff --git a/NvCloth/samples/SampleBase/scene/scenes/MultiSolverScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/MultiSolverScene.cpp new file mode 100644 index 0000000..d708069 --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/MultiSolverScene.cpp @@ -0,0 +1,131 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "MultiSolverScene.h" +#include "Scene/SceneController.h" +#include <NvClothExt/ClothFabricCooker.h> +#include "ClothMeshGenerator.h" +#include <NvCloth/Fabric.h> +#include <NvCloth/Solver.h> +#include <NvCloth/Cloth.h> +#include <NvCloth/Factory.h> +#include "Renderer.h" +#include "renderer/RenderUtils.h" +#include "windows.h" +#include "utils/MeshGenerator.h" + +DECLARE_SCENE_NAME(MultiSolverScene, "Multi Solver Scene") + + +void MultiSolverScene::initializeCloth(int index, physx::PxVec3 offset) +{ + mSolver[index] = getSceneController()->getFactory()->createSolver(); + trackSolver(mSolver[index]); + + /////////////////////////////////////////////////////////////////////// + ClothMeshData clothMesh; + + physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f)+ offset, PxQuat(0, PxVec3(1.f, 0.f, 0.f))); + clothMesh.GeneratePlaneCloth(5.f, 6.f, 69, 79, false, transform); + clothMesh.AttachClothPlaneByAngles(69, 79); + clothMesh.SetInvMasses(0.5f + (float)index * 2.0f); + + mClothActor[index] = new ClothActor; + nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc(); + { + mClothActor[index]->mClothRenderMesh = new ClothRenderMesh(meshDesc); + mClothActor[index]->mClothRenderable = getSceneController()->getRenderer().createRenderable(*(static_cast<IRenderMesh*>(mClothActor[index]->mClothRenderMesh)), *getSceneController()->getDefaultMaterial()); + + float r, g, b; + r = index == 0 ? 1.0f : 0.3f; + g = index == 1 ? 1.0f : 0.3f; + b = index == 2 ? 1.0f : 0.3f; + + mClothActor[index]->mClothRenderable->setColor(DirectX::XMFLOAT4(r, g, b, 1.0f)); + } + + nv::cloth::Vector<int32_t>::Type phaseTypeInfo; + mFabric[index] = NvClothCookFabricFromMesh(getSceneController()->getFactory(), meshDesc, physx::PxVec3(0.0f, 0.0f, 1.0f), &phaseTypeInfo, false); + trackFabric(mFabric[index]); + + // Initialize start positions and masses for the actual cloth instance + // (note: the particle/vertex positions do not have to match the mesh description here. Set the positions to the initial shape of this cloth instance) + std::vector<physx::PxVec4> particlesCopy; + particlesCopy.resize(clothMesh.mVertices.size()); + + physx::PxVec3 clothOffset = transform.getPosition(); + for(int i = 0; i < (int)clothMesh.mVertices.size(); i++) + { + // To put attachment point closer to each other + if(clothMesh.mInvMasses[i] < 1e-6) + clothMesh.mVertices[i] = (clothMesh.mVertices[i] - clothOffset)*0.95f + clothOffset; + + particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles + } + + // Create the cloth from the initial positions/masses and the fabric + mClothActor[index]->mCloth = getSceneController()->getFactory()->createCloth(nv::cloth::Range<physx::PxVec4>(&particlesCopy[0], &particlesCopy[0] + particlesCopy.size()), *mFabric[index]); + particlesCopy.clear(); particlesCopy.shrink_to_fit(); + + mClothActor[index]->mCloth->setGravity(physx::PxVec3(0.0f, -9.8f, 0.0f)); + mClothActor[index]->mCloth->setDamping(physx::PxVec3(0.1f, 0.1f, 0.1f)); + mClothActor[index]->mCloth->setFriction(0.5f); + + + physx::PxVec4 spheres[2] = {physx::PxVec4(physx::PxVec3(0.f, 11.f, -2.f) + offset,1.5), + physx::PxVec4(physx::PxVec3(0.f, 11.f, 2.f) + offset,1.0)}; + + mClothActor[index]->mCloth->setSpheres(nv::cloth::Range<physx::PxVec4>(spheres, spheres + 2), 0, mClothActor[index]->mCloth->getNumSpheres()); + + uint32_t caps[4]; + caps[0] = 0; + caps[1] = 1; + + mClothActor[index]->mCloth->setCapsules(nv::cloth::Range<uint32_t>(caps, caps + 2), 0, mClothActor[index]->mCloth->getNumCapsules()); + + //create render mesh + auto mesh = MeshGenerator::generateCollisionCapsules(spheres,2,caps,2,-0.05f); + auto renderMesh = new MeshGenerator::MeshGeneratorRenderMesh(mesh); + Renderable* renderable = getSceneController()->getRenderer().createRenderable(*renderMesh, *getSceneController()->getDefaultMaterial()); + trackRenderable(renderable); + + // Setup phase configs + std::vector<nv::cloth::PhaseConfig> phases(mFabric[index]->getNumPhases()); + for(int i = 0; i < (int)phases.size(); i++) + { + phases[i].mPhaseIndex = i; + phases[i].mStiffness = 1.0f; + phases[i].mStiffnessMultiplier = 1.0f; + phases[i].mCompressionLimit = 1.0f; + phases[i].mStretchLimit = 1.0f; + } + mClothActor[index]->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back())); + mClothActor[index]->mCloth->setDragCoefficient(0.5f); + mClothActor[index]->mCloth->setLiftCoefficient(0.6f); + + trackClothActor(mClothActor[index]); + + // Add the cloth to the solver for simulation + addClothToSolver(mClothActor[index], mSolver[index]); +} + +void MultiSolverScene::onInitialize() +{ + initializeCloth(0, physx::PxVec3(-5.0f, 0.0f, 0.0f)); + initializeCloth(0, physx::PxVec3(5.0f, 0.0f, 0.0f)); + + { + IRenderMesh* mesh = getSceneController()->getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane); + Renderable* plane = getSceneController()->getRenderer().createRenderable(*mesh, *getSceneController()->getDefaultPlaneMaterial()); + plane->setTransform(PxTransform(PxVec3(0.f, 0.f, 0.f), PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f)))); + plane->setScale(PxVec3(1000.f)); + trackRenderable(plane); + } +} diff --git a/NvCloth/samples/SampleBase/scene/scenes/MultiSolverScene.h b/NvCloth/samples/SampleBase/scene/scenes/MultiSolverScene.h new file mode 100644 index 0000000..f95c54e --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/MultiSolverScene.h @@ -0,0 +1,33 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef MULTI_SOLVER_SCENE_H +#define MULTI_SOLVER_SCENE_H + +#include "scene/Scene.h" +#include <foundation/PxVec3.h> + +class MultiSolverScene : public Scene +{ +public: + + MultiSolverScene(SceneController* sceneController): Scene(sceneController) {} + + void initializeCloth(int index, physx::PxVec3 offset); + virtual void onInitialize() override; + +private: + nv::cloth::Fabric* mFabric[1]; + nv::cloth::Solver* mSolver[2]; + ClothActor* mClothActor[1]; +}; + + +#endif
\ No newline at end of file diff --git a/NvCloth/samples/SampleBase/scene/scenes/PlaneCollisionScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/PlaneCollisionScene.cpp new file mode 100644 index 0000000..fcf4060 --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/PlaneCollisionScene.cpp @@ -0,0 +1,135 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "PlaneCollisionScene.h" +#include "Scene/SceneController.h" +#include <NvClothExt/ClothFabricCooker.h> +#include "ClothMeshGenerator.h" +#include <NvCloth/Fabric.h> +#include <NvCloth/Solver.h> +#include <NvCloth/Cloth.h> +#include <NvCloth/Factory.h> +#include "Renderer.h" +#include "renderer/RenderUtils.h" +#include "windows.h" +#include "utils/MeshGenerator.h" + +DECLARE_SCENE_NAME(PlaneCollisionScene, "Plane Collision Scene") + + +void PlaneCollisionScene::initializeCloth(int index, physx::PxVec3 offset) +{ + /////////////////////////////////////////////////////////////////////// + ClothMeshData clothMesh; + + physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f)+ offset, PxQuat(0, PxVec3(1.f, 0.f, 0.f))); + clothMesh.GeneratePlaneCloth(5.f, 6.f, 59, 69, false, transform); + clothMesh.AttachClothPlaneByAngles(59, 69); + clothMesh.SetInvMasses(0.5f + (float)index * 2.0f); + + mClothActor[index] = new ClothActor; + nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc(); + { + mClothActor[index]->mClothRenderMesh = new ClothRenderMesh(meshDesc); + mClothActor[index]->mClothRenderable = getSceneController()->getRenderer().createRenderable(*(static_cast<IRenderMesh*>(mClothActor[index]->mClothRenderMesh)), *getSceneController()->getDefaultMaterial()); + + float r, g, b; + r = index == 0 ? 1.0f : 0.3f; + g = index == 1 ? 1.0f : 0.3f; + b = index == 2 ? 1.0f : 0.3f; + + mClothActor[index]->mClothRenderable->setColor(DirectX::XMFLOAT4(r, g, b, 1.0f)); + } + + nv::cloth::Vector<int32_t>::Type phaseTypeInfo; + mFabric[index] = NvClothCookFabricFromMesh(getSceneController()->getFactory(), meshDesc, physx::PxVec3(0.0f, 0.0f, 1.0f), &phaseTypeInfo, false); + trackFabric(mFabric[index]); + + // Initialize start positions and masses for the actual cloth instance + // (note: the particle/vertex positions do not have to match the mesh description here. Set the positions to the initial shape of this cloth instance) + std::vector<physx::PxVec4> particlesCopy; + particlesCopy.resize(clothMesh.mVertices.size()); + + physx::PxVec3 clothOffset = transform.getPosition(); + for(int i = 0; i < (int)clothMesh.mVertices.size(); i++) + { + // To put attachment point closer to each other + if(clothMesh.mInvMasses[i] < 1e-6) + clothMesh.mVertices[i] = (clothMesh.mVertices[i] - clothOffset)*0.95f + clothOffset; + + particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles + } + + // Create the cloth from the initial positions/masses and the fabric + mClothActor[index]->mCloth = getSceneController()->getFactory()->createCloth(nv::cloth::Range<physx::PxVec4>(&particlesCopy[0], &particlesCopy[0] + particlesCopy.size()), *mFabric[index]); + particlesCopy.clear(); particlesCopy.shrink_to_fit(); + + mClothActor[index]->mCloth->setGravity(physx::PxVec3(0.0f, -9.8f, 0.0f)); + mClothActor[index]->mCloth->setDamping(physx::PxVec3(0.1f, 0.1f, 0.1f)); + + //Generate collision planes + std::vector<physx::PxVec4> planes; + planes.push_back(physx::PxVec4(physx::PxVec3(-0.5f, 0.4f, 0.0f).getNormalized(), -4.0f)); + planes.push_back(physx::PxVec4(physx::PxVec3(0.0f, 0.4f, 0.5f).getNormalized(), -4.0f)); + uint32_t mask = 3; + MeshGenerator::Mesh mesh = MeshGenerator::generateCollisionConvex(planes.data(), mask, -0.01f, true); + + //assign as collision data + nv::cloth::Range<const physx::PxVec4> planesR(&planes[0], &planes[0] + planes.size()); + mClothActor[index]->mCloth->setPlanes(planesR, 0, mClothActor[index]->mCloth->getNumPlanes()); + + //assign convex indices + std::vector<uint32_t> indices; + indices.push_back(1); + indices.push_back(2); + nv::cloth::Range<uint32_t> cind(&indices[0], &indices[0] + indices.size()); + mClothActor[index]->mCloth->setConvexes(cind, 0, mClothActor[index]->mCloth->getNumConvexes()); + + //create render mesh + //scale down the render model to not intersect the triangles so much + auto renderMesh = new MeshGenerator::MeshGeneratorRenderMesh(mesh); + Renderable* renderable = getSceneController()->getRenderer().createRenderable(*renderMesh, *getSceneController()->getDefaultMaterial()); + trackRenderable(renderable); + + // Setup phase configs + std::vector<nv::cloth::PhaseConfig> phases(mFabric[index]->getNumPhases()); + for(int i = 0; i < (int)phases.size(); i++) + { + phases[i].mPhaseIndex = i; + phases[i].mStiffness = 1.0f; + phases[i].mStiffnessMultiplier = 1.0f; + phases[i].mCompressionLimit = 1.0f; + phases[i].mStretchLimit = 1.0f; + } + mClothActor[index]->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back())); + mClothActor[index]->mCloth->setDragCoefficient(0.1f); + mClothActor[index]->mCloth->setLiftCoefficient(0.1f); + + trackClothActor(mClothActor[index]); + + // Add the cloth to the solver for simulation + addClothToSolver(mClothActor[index], mSolver); +} + +void PlaneCollisionScene::onInitialize() +{ + mSolver = getSceneController()->getFactory()->createSolver(); + trackSolver(mSolver); + + initializeCloth(0, physx::PxVec3(0.0f, 0.0f, 0.0f)); + + { + IRenderMesh* mesh = getSceneController()->getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane); + Renderable* plane = getSceneController()->getRenderer().createRenderable(*mesh, *getSceneController()->getDefaultPlaneMaterial()); + plane->setTransform(PxTransform(PxVec3(0.f, 0.f, 0.f), PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f)))); + plane->setScale(PxVec3(1000.f)); + trackRenderable(plane); + } +} diff --git a/NvCloth/samples/SampleBase/scene/scenes/PlaneCollisionScene.h b/NvCloth/samples/SampleBase/scene/scenes/PlaneCollisionScene.h new file mode 100644 index 0000000..32ff35b --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/PlaneCollisionScene.h @@ -0,0 +1,33 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef PLANE_COLLISION_SCENE_H +#define PLANE_COLLISION_SCENE_H + +#include "scene/Scene.h" +#include <foundation/PxVec3.h> + +class PlaneCollisionScene : public Scene +{ +public: + + PlaneCollisionScene(SceneController* sceneController): Scene(sceneController) {} + + void initializeCloth(int index, physx::PxVec3 offset); + virtual void onInitialize() override; + +private: + nv::cloth::Fabric* mFabric[1]; + nv::cloth::Solver* mSolver; + ClothActor* mClothActor[1]; +}; + + +#endif
\ No newline at end of file diff --git a/NvCloth/samples/SampleBase/scene/scenes/SelfCollisionScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/SelfCollisionScene.cpp new file mode 100644 index 0000000..6a407ea --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/SelfCollisionScene.cpp @@ -0,0 +1,155 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "SelfCollisionScene.h" +#include "Scene/SceneController.h" +#include <NvClothExt/ClothFabricCooker.h> +#include "ClothMeshGenerator.h" +#include <NvCloth/Fabric.h> +#include <NvCloth/Solver.h> +#include <NvCloth/Cloth.h> +#include <NvCloth/Factory.h> +#include "Renderer.h" +#include "renderer/RenderUtils.h" + +DECLARE_SCENE_NAME(SelfCollisionScene, "Self Collision Scene") + +void SelfCollisionScene::initializeCloth(int index, physx::PxMat44 transform) +{ + /////////////////////////////////////////////////////////////////////// + + float w = 5.f - index; + float h = 6.f + index; + transform *= PxTransform(PxVec3(0.f, 13.f, 0.f), PxQuat(0, PxVec3(1.f, 0.f, 0.f))); + ClothMeshData clothMesh; + clothMesh.GeneratePlaneCloth(w, h, 5 * w, 5 * h, false, transform); + clothMesh.AttachClothPlaneByAngles(5 * w, 5 * h); + clothMesh.SetInvMasses(1.0f / (1000.0f / (5.0f*w*5.0f*h))); + + float w2 = w - 1.0f; + float h2 = h + 1.0f; + transform *= PxTransform(PxVec3(0.f, 0.8f, -0.2f), PxQuat(0, PxVec3(1.f, 0.f, 0.f))); + ClothMeshData clothMesh2; + clothMesh2.GeneratePlaneCloth(w2, h2, 5 * w2, 5 * h2, false, transform); + clothMesh2.AttachClothPlaneByAngles(5 * w2, 5 * h2); + clothMesh2.SetInvMasses(1.0f / (1000.0f / (5.0f*w2*5.0f*h2))); + int FirstParticleIndexCloth2 = (int)clothMesh.mVertices.size(); + clothMesh.Merge(clothMesh2); + + mClothActor[index] = new ClothActor; + nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc(); + { + mClothActor[index]->mClothRenderMesh = new ClothRenderMesh(meshDesc); + mClothActor[index]->mClothRenderable = getSceneController()->getRenderer().createRenderable(*(static_cast<IRenderMesh*>(mClothActor[index]->mClothRenderMesh)), *getSceneController()->getDefaultMaterial()); + mClothActor[index]->mClothRenderable->setColor(getRandomPastelColor()); + } + + nv::cloth::Vector<int32_t>::Type phaseTypeInfo; + mFabric[index] = NvClothCookFabricFromMesh(getSceneController()->getFactory(), meshDesc, physx::PxVec3(0.0f, 0.0f, 1.0f), &phaseTypeInfo, false); + trackFabric(mFabric[index]); + + // Initialize start positions and masses for the actual cloth instance + // (note: the particle/vertex positions do not have to match the mesh description here. Set the positions to the initial shape of this cloth instance) + std::vector<physx::PxVec4> particlesCopy; + particlesCopy.resize(clothMesh.mVertices.size()); + + physx::PxVec3 clothOffset = transform.getPosition(); + for(int i = 0; i < (int)clothMesh.mVertices.size(); i++) + { + // To put attachment point closer to each other + if(clothMesh.mInvMasses[i] < 1e-6) + clothMesh.mVertices[i] = (clothMesh.mVertices[i] - clothOffset)*0.9f + clothOffset; + + particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles + } + + // Create the cloth from the initial positions/masses and the fabric + mClothActor[index]->mCloth = getSceneController()->getFactory()->createCloth(nv::cloth::Range<physx::PxVec4>(&particlesCopy[0], &particlesCopy[0] + particlesCopy.size()), *mFabric[index]); + particlesCopy.clear(); particlesCopy.shrink_to_fit(); + + std::vector<physx::PxVec4> planes; + planes.push_back(physx::PxVec4(physx::PxVec3(0.0f, 1.f, 0.0f), -0.01f)); + + nv::cloth::Range<const physx::PxVec4> planesR(&planes[0], &planes[0] + planes.size()); + mClothActor[index]->mCloth->setPlanes(planesR, 0, mClothActor[index]->mCloth->getNumPlanes()); + std::vector<uint32_t> indices; + indices.resize(planes.size()); + for(int i = 0; i < (int)indices.size(); i++) + indices[i] = 1 << i; + nv::cloth::Range<uint32_t> cind(&indices[0], &indices[0] + indices.size()); + mClothActor[index]->mCloth->setConvexes(cind, 0, mClothActor[index]->mCloth->getNumConvexes()); + + mClothActor[index]->mCloth->setGravity(physx::PxVec3(0.0f, -1.0f, 0.0f)); + mClothActor[index]->mCloth->setFriction(0.1); + mClothActor[index]->mCloth->setDragCoefficient(0.1f); + mClothActor[index]->mCloth->setLiftCoefficient(0.1f); + mClothActor[index]->mCloth->setSolverFrequency(120.0f); + mClothActor[index]->mCloth->setSelfCollisionDistance(0.26); + mClothActor[index]->mCloth->setSelfCollisionStiffness(0.95); + + std::vector<uint32_t> selfCollisionIndices; + + //only enable every other particle for self collision + for(int y = 0; y < 5*h + 1; y++) + for(int x = 0; x < 5 * w + 1; x++) + if((x & 1) ^ (y & 1)) selfCollisionIndices.push_back(x + y*(5 * w + 1)); + + for(int y = 0; y < 5 * h2 + 1; y++) + for(int x = 0; x < 5 * w2 + 1; x++) + if((x & 1) ^ (y & 1)) selfCollisionIndices.push_back(FirstParticleIndexCloth2 + x + y*(5 * w2 + 1)); + + nv::cloth::Range<uint32_t> selfCollisionIndicesRange (&selfCollisionIndices[0], &selfCollisionIndices[0] + selfCollisionIndices.size()); + mClothActor[index]->mCloth->setSelfCollisionIndices(selfCollisionIndicesRange); + + // Setup phase configs + std::vector<nv::cloth::PhaseConfig> phases(mFabric[index]->getNumPhases()); + for(int i = 0; i < (int)phases.size(); i++) + { + phases[i].mPhaseIndex = i; + phases[i].mStiffness = 1.0f; + phases[i].mStiffnessMultiplier = 1.0f; + phases[i].mCompressionLimit = 1.0f; + phases[i].mStretchLimit = 1.0f; + } + mClothActor[index]->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back())); + + trackClothActor(mClothActor[index]); + + // Add the cloth to the solver for simulation + addClothToSolver(mClothActor[index], mSolver); +} + +void SelfCollisionScene::onInitialize() +{ + mSolver = getSceneController()->getFactory()->createSolver(); + /*mSolver->setInterCollisionNbIterations(8); + mSolver->setInterCollisionDistance(0.4f); + mSolver->setInterCollisionStiffness(0.95f); + mSolver->setInterCollisionFilter( + [](void* a, void* b) { + return true; + } + );*/ + trackSolver(mSolver); + + physx::PxMat44 posTrans(physx::PxIdentity); + posTrans.setPosition(physx::PxVec3(0.0f, 0.f, -1.0f)); + initializeCloth(0, posTrans); + //posTrans.setPosition(physx::PxVec3(0.0f, 0.8f, -1.2f)); + //initializeCloth(1, posTrans); + + { + IRenderMesh* mesh = getSceneController()->getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane); + Renderable* plane = getSceneController()->getRenderer().createRenderable(*mesh, *getSceneController()->getDefaultPlaneMaterial()); + plane->setTransform(PxTransform(PxVec3(0.f, 0.f, 0.f), PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f)))); + plane->setScale(PxVec3(1000.f)); + trackRenderable(plane); + } +} diff --git a/NvCloth/samples/SampleBase/scene/scenes/SelfCollisionScene.h b/NvCloth/samples/SampleBase/scene/scenes/SelfCollisionScene.h new file mode 100644 index 0000000..0fcdeac --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/SelfCollisionScene.h @@ -0,0 +1,33 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef SELF_COLLISION_SCENE_H +#define SELF_COLLISION_SCENE_H + +#include "scene/Scene.h" +#include <foundation/PxVec3.h> + +class SelfCollisionScene : public Scene +{ +public: + + SelfCollisionScene(SceneController* sceneController): Scene(sceneController) {} + + void initializeCloth(int index, physx::PxMat44 transform); + virtual void onInitialize() override; + +private: + nv::cloth::Fabric* mFabric[2]; + nv::cloth::Solver* mSolver; + ClothActor* mClothActor[2]; +}; + + +#endif
\ No newline at end of file diff --git a/NvCloth/samples/SampleBase/scene/scenes/SimpleScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/SimpleScene.cpp index 68bb96b..b52e2c6 100644 --- a/NvCloth/samples/SampleBase/scene/scenes/SimpleScene.cpp +++ b/NvCloth/samples/SampleBase/scene/scenes/SimpleScene.cpp @@ -26,9 +26,9 @@ void SimpleScene::onInitialize() /////////////////////////////////////////////////////////////////////// ClothMeshData clothMesh; - physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f), PxQuat(PxPi / 6.f, PxVec3(1.f, 0.f, 0.f))); - clothMesh.GeneratePlaneCloth(6.f, 7.f, 59, 69, false, transform); - clothMesh.AttachClothPlaneByAngles(59, 69); + physx::PxMat44 transform = PxTransform(PxVec3(-2.f, 13.f, 0.f), PxQuat(PxPi / 6.f, PxVec3(1.f, 0.f, 0.f))); + clothMesh.GeneratePlaneCloth(6.f, 7.f, 49, 59, false, transform); + clothMesh.AttachClothPlaneByAngles(49, 59); mClothActor = new ClothActor; nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc(); @@ -48,11 +48,11 @@ void SimpleScene::onInitialize() particlesCopy.resize(clothMesh.mVertices.size()); physx::PxVec3 center = transform.transform(physx::PxVec3(0.0f, 0.0f, 0.0f)); - for(int i = 0; i < (int)clothMesh.mVertices.size(); i++) + for (int i = 0; i < (int)clothMesh.mVertices.size(); i++) { // To put attachment point closer to each other if(clothMesh.mInvMasses[i] < 1e-6) - clothMesh.mVertices[i] = (clothMesh.mVertices[i] - center)*0.85f + center; + clothMesh.mVertices[i] = (clothMesh.mVertices[i] - center) * 0.85f + center; particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles } @@ -65,7 +65,7 @@ void SimpleScene::onInitialize() // Setup phase configs std::vector<nv::cloth::PhaseConfig> phases(mFabric->getNumPhases()); - for(int i = 0; i < (int)phases.size(); i++) + for (int i = 0; i < (int)phases.size(); i++) { phases[i].mPhaseIndex = i; phases[i].mStiffness = 1.0f; @@ -74,8 +74,8 @@ void SimpleScene::onInitialize() phases[i].mStretchLimit = 1.0f; } mClothActor->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back())); - mClothActor->mCloth->setDragCoefficient(0.5f); - mClothActor->mCloth->setDragCoefficient(0.5f); + mClothActor->mCloth->setDragCoefficient(0.1f); + mClothActor->mCloth->setDragCoefficient(0.1f); mSolver = getSceneController()->getFactory()->createSolver(); trackSolver(mSolver); diff --git a/NvCloth/samples/SampleBase/scene/scenes/SphereScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/SphereScene.cpp new file mode 100644 index 0000000..d963011 --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/SphereScene.cpp @@ -0,0 +1,121 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "SphereScene.h" +#include "Scene/SceneController.h" +#include <NvClothExt/ClothFabricCooker.h> +#include "ClothMeshGenerator.h" +#include <NvCloth/Fabric.h> +#include <NvCloth/Solver.h> +#include <NvCloth/Cloth.h> +#include <NvCloth/Factory.h> +#include "Renderer.h" +#include "renderer/RenderUtils.h" +#include "windows.h" + +DECLARE_SCENE_NAME(SphereScene, "Sphere Scene") + + +void SphereScene::initializeCloth(int index, physx::PxVec3 offset) +{ + /////////////////////////////////////////////////////////////////////// + ClothMeshData clothMesh; + + physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f)+ offset, PxQuat(0, PxVec3(1.f, 0.f, 0.f))); + clothMesh.GeneratePlaneCloth(5.f, 6.f, 69, 79, false, transform); + clothMesh.AttachClothPlaneByAngles(69, 79); + clothMesh.SetInvMasses(0.5f + (float)index * 2.0f); + + mClothActor[index] = new ClothActor; + nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc(); + { + mClothActor[index]->mClothRenderMesh = new ClothRenderMesh(meshDesc); + mClothActor[index]->mClothRenderable = getSceneController()->getRenderer().createRenderable(*(static_cast<IRenderMesh*>(mClothActor[index]->mClothRenderMesh)), *getSceneController()->getDefaultMaterial()); + + float r, g, b; + r = index == 0 ? 1.0f : 0.3f; + g = index == 1 ? 1.0f : 0.3f; + b = index == 2 ? 1.0f : 0.3f; + + mClothActor[index]->mClothRenderable->setColor(DirectX::XMFLOAT4(r, g, b, 1.0f)); + } + + nv::cloth::Vector<int32_t>::Type phaseTypeInfo; + mFabric[index] = NvClothCookFabricFromMesh(getSceneController()->getFactory(), meshDesc, physx::PxVec3(0.0f, 0.0f, 1.0f), &phaseTypeInfo, false); + trackFabric(mFabric[index]); + + // Initialize start positions and masses for the actual cloth instance + // (note: the particle/vertex positions do not have to match the mesh description here. Set the positions to the initial shape of this cloth instance) + std::vector<physx::PxVec4> particlesCopy; + particlesCopy.resize(clothMesh.mVertices.size()); + + physx::PxVec3 clothOffset = transform.getPosition(); + for(int i = 0; i < (int)clothMesh.mVertices.size(); i++) + { + // To put attachment point closer to each other + if(clothMesh.mInvMasses[i] < 1e-6) + clothMesh.mVertices[i] = (clothMesh.mVertices[i] - clothOffset)*0.95f + clothOffset; + + particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles + } + + // Create the cloth from the initial positions/masses and the fabric + mClothActor[index]->mCloth = getSceneController()->getFactory()->createCloth(nv::cloth::Range<physx::PxVec4>(&particlesCopy[0], &particlesCopy[0] + particlesCopy.size()), *mFabric[index]); + particlesCopy.clear(); particlesCopy.shrink_to_fit(); + + mClothActor[index]->mCloth->setGravity(physx::PxVec3(0.0f, -9.8f, 0.0f)); + mClothActor[index]->mCloth->setDamping(physx::PxVec3(0.1f, 0.1f, 0.1f)); + + + { + IRenderMesh* mesh = getSceneController()->getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Sphere); + Renderable* sphere = getSceneController()->getRenderer().createRenderable(*mesh, *getSceneController()->getDefaultMaterial()); + sphere->setTransform(PxTransform(PxVec3(0.f, 10.f, -1.f) + offset, PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f)))); + sphere->setScale(PxVec3(1.5f)); + trackRenderable(sphere); + } + + physx::PxVec4 spheres[1] = {physx::PxVec4(physx::PxVec3(0.f, 10.f, -1.f) + offset,1.5)}; + + mClothActor[index]->mCloth->setSpheres(nv::cloth::Range<physx::PxVec4>(spheres, spheres + 1), 0, mClothActor[index]->mCloth->getNumSpheres()); + + // Setup phase configs + std::vector<nv::cloth::PhaseConfig> phases(mFabric[index]->getNumPhases()); + for(int i = 0; i < (int)phases.size(); i++) + { + phases[i].mPhaseIndex = i; + phases[i].mStiffness = 1.0f; + phases[i].mStiffnessMultiplier = 1.0f; + phases[i].mCompressionLimit = 1.0f; + phases[i].mStretchLimit = 1.0f; + } + mClothActor[index]->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back())); + + trackClothActor(mClothActor[index]); + + // Add the cloth to the solver for simulation + addClothToSolver(mClothActor[index], mSolver); +} + +void SphereScene::onInitialize() +{ + mSolver = getSceneController()->getFactory()->createSolver(); + trackSolver(mSolver); + + initializeCloth(0, physx::PxVec3(0.0f, 0.0f, 0.0f)); + + { + IRenderMesh* mesh = getSceneController()->getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane); + Renderable* plane = getSceneController()->getRenderer().createRenderable(*mesh, *getSceneController()->getDefaultPlaneMaterial()); + plane->setTransform(PxTransform(PxVec3(0.f, 0.f, 0.f), PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f)))); + plane->setScale(PxVec3(1000.f)); + trackRenderable(plane); + } +} diff --git a/NvCloth/samples/SampleBase/scene/scenes/SphereScene.h b/NvCloth/samples/SampleBase/scene/scenes/SphereScene.h new file mode 100644 index 0000000..60d7753 --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/SphereScene.h @@ -0,0 +1,33 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef SPHERE_SCENE_H +#define SPHERE_SCENE_H + +#include "scene/Scene.h" +#include <foundation/PxVec3.h> + +class SphereScene : public Scene +{ +public: + + SphereScene(SceneController* sceneController): Scene(sceneController) {} + + void initializeCloth(int index, physx::PxVec3 offset); + virtual void onInitialize() override; + +private: + nv::cloth::Fabric* mFabric[1]; + nv::cloth::Solver* mSolver; + ClothActor* mClothActor[1]; +}; + + +#endif
\ No newline at end of file diff --git a/NvCloth/samples/SampleBase/scene/scenes/StiffnessPerConstraintScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/StiffnessPerConstraintScene.cpp new file mode 100644 index 0000000..4ed9daf --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/StiffnessPerConstraintScene.cpp @@ -0,0 +1,122 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "StiffnessPerConstraintScene.h" +#include "Scene/SceneController.h" +#include <NvClothExt/ClothFabricCooker.h> +#include "ClothMeshGenerator.h" +#include <NvCloth/Fabric.h> +#include <NvCloth/Solver.h> +#include <NvCloth/Cloth.h> +#include <NvCloth/Factory.h> +#include "Renderer.h" +#include "renderer/RenderUtils.h" +#include "windows.h" + +DECLARE_SCENE_NAME(StiffnessPerConstraintScene, "Stiffness per constraint scene") + +void StiffnessPerConstraintScene::initializeCloth(int index, physx::PxVec3 offset) +{ + /////////////////////////////////////////////////////////////////////// + ClothMeshData clothMesh; + + physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f)+ offset, PxQuat(0, PxVec3(1.f, 0.f, 0.f))); + clothMesh.GeneratePlaneCloth(5.f, 6.25f, 39, 49, false, transform); + clothMesh.AttachClothPlaneByAngles(39, 39); + clothMesh.SetInvMasses(0.5f + (float)index * 2.0f); + + mClothActor[index] = new ClothActor; + nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc(); + { + meshDesc.pointsStiffness.stride = sizeof(float); + meshDesc.pointsStiffness.count = meshDesc.points.count; + float* stiffnessValues = new float[meshDesc.pointsStiffness.count]; + meshDesc.pointsStiffness.data = stiffnessValues; + for(int y = 0; y<50; y++) + for(int x = 0; x<40; x++) + { + stiffnessValues[x + y * 40] = ((y) % 8)<5 ? 1.0f : 0.1f; + } + + mClothActor[index]->mClothRenderMesh = new ClothRenderMesh(meshDesc); + mClothActor[index]->mClothRenderable = getSceneController()->getRenderer().createRenderable(*(static_cast<IRenderMesh*>(mClothActor[index]->mClothRenderMesh)), *getSceneController()->getDefaultMaterial()); + + float r, g, b; + r = index == 0 ? 1.0f : 0.3f; + g = index == 1 ? 1.0f : 0.3f; + b = index == 2 ? 1.0f : 0.3f; + + mClothActor[index]->mClothRenderable->setColor(DirectX::XMFLOAT4(r, g, b, 1.0f)); + } + + nv::cloth::Vector<int32_t>::Type phaseTypeInfo; + mFabric[index] = NvClothCookFabricFromMesh(getSceneController()->getFactory(), meshDesc, physx::PxVec3(0.0f, 0.0f, 1.0f), &phaseTypeInfo, false); + trackFabric(mFabric[index]); + + // Initialize start positions and masses for the actual cloth instance + // (note: the particle/vertex positions do not have to match the mesh description here. Set the positions to the initial shape of this cloth instance) + std::vector<physx::PxVec4> particlesCopy; + particlesCopy.resize(clothMesh.mVertices.size()); + + physx::PxVec3 clothOffset = transform.getPosition(); + for(int i = 0; i < (int)clothMesh.mVertices.size(); i++) + { + // To put attachment point closer to each other + if(clothMesh.mInvMasses[i] < 1e-6) + clothMesh.mVertices[i] = (clothMesh.mVertices[i] - clothOffset)*0.95f + clothOffset; + + particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles + } + + // Create the cloth from the initial positions/masses and the fabric + mClothActor[index]->mCloth = getSceneController()->getFactory()->createCloth(nv::cloth::Range<physx::PxVec4>(&particlesCopy[0], &particlesCopy[0] + particlesCopy.size()), *mFabric[index]); + particlesCopy.clear(); particlesCopy.shrink_to_fit(); + + mClothActor[index]->mCloth->setGravity(physx::PxVec3(0.0f, -9.8f, 0.0f)); + mClothActor[index]->mCloth->setTetherConstraintStiffness(0.1); + mClothActor[index]->mCloth->setTetherConstraintScale(1.5f); + mClothActor[index]->mCloth->setDragCoefficient(0.1); + mClothActor[index]->mCloth->setSolverFrequency(120.0f); + mClothActor[index]->mCloth->setDamping(physx::PxVec3(0.1f, 0.1f, 0.1f)); + + // Setup phase configs + std::vector<nv::cloth::PhaseConfig> phases(mFabric[index]->getNumPhases()); + for(int i = 0; i < (int)phases.size(); i++) + { + phases[i].mPhaseIndex = i; + phases[i].mStiffness = 1.0f; + phases[i].mStiffnessMultiplier = 1.0f; + phases[i].mCompressionLimit = 1.0f; + phases[i].mStretchLimit = 1.0f; + } + mClothActor[index]->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back())); + mClothActor[index]->mCloth->setDragCoefficient(0.1f); + mClothActor[index]->mCloth->setLiftCoefficient(0.1f); + + trackClothActor(mClothActor[index]); + + // Add the cloth to the solver for simulation + addClothToSolver(mClothActor[index], mSolver); +} + +void StiffnessPerConstraintScene::onInitialize() +{ + mSolver = getSceneController()->getFactory()->createSolver(); + trackSolver(mSolver); + initializeCloth(0, physx::PxVec3(0.0f, 0.0f, 0.0f)); + + { + IRenderMesh* mesh = getSceneController()->getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane); + Renderable* plane = getSceneController()->getRenderer().createRenderable(*mesh, *getSceneController()->getDefaultPlaneMaterial()); + plane->setTransform(PxTransform(PxVec3(0.f, 0.f, 0.f), PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f)))); + plane->setScale(PxVec3(1000.f)); + trackRenderable(plane); + } +} diff --git a/NvCloth/samples/SampleBase/scene/scenes/StiffnessPerConstraintScene.h b/NvCloth/samples/SampleBase/scene/scenes/StiffnessPerConstraintScene.h new file mode 100644 index 0000000..bd56a13 --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/StiffnessPerConstraintScene.h @@ -0,0 +1,33 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef STIFFNESS_PER_CONSTRAINT_SCENE_H +#define STIFFNESS_PER_CONSTRAINT_SCENE_H + +#include "scene/Scene.h" +#include <foundation/PxVec3.h> + +class StiffnessPerConstraintScene : public Scene +{ +public: + + StiffnessPerConstraintScene(SceneController* sceneController): Scene(sceneController) {} + + void initializeCloth(int index, physx::PxVec3 offset); + virtual void onInitialize() override; + +private: + nv::cloth::Fabric* mFabric[1]; + nv::cloth::Solver* mSolver; + ClothActor* mClothActor[1]; +}; + + +#endif
\ No newline at end of file diff --git a/NvCloth/samples/SampleBase/scene/scenes/TetherScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/TetherScene.cpp index b818d9d..ecd0850 100644 --- a/NvCloth/samples/SampleBase/scene/scenes/TetherScene.cpp +++ b/NvCloth/samples/SampleBase/scene/scenes/TetherScene.cpp @@ -23,12 +23,11 @@ DECLARE_SCENE_NAME(TetherScene, "Tether Scene") void TetherScene::initializeCloth(int index, physx::PxVec3 offset, float tetherStiffness) { - /////////////////////////////////////////////////////////////////////// ClothMeshData clothMesh; - physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f)+ offset, PxQuat(PxPi / 6.f, PxVec3(1.f, 0.f, 0.f))); - clothMesh.GeneratePlaneCloth(6.f, 7.5f, 39, 59, false, transform); - clothMesh.AttachClothPlaneByAngles(39, 59); + physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f) + offset, PxQuat(PxPi / 6.f, PxVec3(1.f, 0.f, 0.f))); + clothMesh.GeneratePlaneCloth(6.f, 7.f, 49, 59, false, transform); + clothMesh.AttachClothPlaneByAngles(49, 59); mClothActor[index] = new ClothActor; nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc(); @@ -48,13 +47,13 @@ void TetherScene::initializeCloth(int index, physx::PxVec3 offset, float tetherS particlesCopy.resize(clothMesh.mVertices.size()); physx::PxVec3 clothOffset = transform.getPosition(); - for(int i = 0; i < (int)clothMesh.mVertices.size(); i++) + for (int i = 0; i < (int)clothMesh.mVertices.size(); i++) { // To put attachment point closer to each other if(clothMesh.mInvMasses[i] < 1e-6) - clothMesh.mVertices[i] = (clothMesh.mVertices[i] - clothOffset)*0.8f + clothOffset; + clothMesh.mVertices[i] = (clothMesh.mVertices[i] - clothOffset) * 0.8f + clothOffset; - particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles + particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], 0.5f * clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles } // Create the cloth from the initial positions/masses and the fabric @@ -66,19 +65,16 @@ void TetherScene::initializeCloth(int index, physx::PxVec3 offset, float tetherS // Setup phase configs std::vector<nv::cloth::PhaseConfig> phases(mFabric[index]->getNumPhases()); - for(int i = 0; i < (int)phases.size(); i++) + for (int i = 0; i < (int)phases.size(); i++) { phases[i].mPhaseIndex = i; - phases[i].mStiffness = 1.0f; + phases[i].mStiffness = 0.75f; phases[i].mStiffnessMultiplier = 1.0f; phases[i].mCompressionLimit = 1.0f; phases[i].mStretchLimit = 1.0f; } mClothActor[index]->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back())); - - mSolver = getSceneController()->getFactory()->createSolver(); - trackSolver(mSolver); trackClothActor(mClothActor[index]); // Add the cloth to the solver for simulation @@ -87,9 +83,11 @@ void TetherScene::initializeCloth(int index, physx::PxVec3 offset, float tetherS void TetherScene::onInitialize() { + mSolver = getSceneController()->getFactory()->createSolver(); + trackSolver(mSolver); - initializeCloth(0,physx::PxVec3(-5.0f,0.0f,0.0f),0); - initializeCloth(1, physx::PxVec3(4.0f, 0.0f, 0.0f),1); + initializeCloth(0,physx::PxVec3(-7.0f, 2.0f, 0.0f), 0.0f); + initializeCloth(1, physx::PxVec3(2.0f, 2.0f, 0.0f), 1.0f); mTime = 0.0f; diff --git a/NvCloth/samples/SampleBase/scene/scenes/TriangleScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/TriangleScene.cpp new file mode 100644 index 0000000..1822bb9 --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/TriangleScene.cpp @@ -0,0 +1,131 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "TriangleScene.h" +#include "Scene/SceneController.h" +#include <NvClothExt/ClothFabricCooker.h> +#include "ClothMeshGenerator.h" +#include <NvCloth/Fabric.h> +#include <NvCloth/Solver.h> +#include <NvCloth/Cloth.h> +#include <NvCloth/Factory.h> +#include "Renderer.h" +#include "renderer/RenderUtils.h" +#include "windows.h" +#include "utils/MeshGenerator.h" + +DECLARE_SCENE_NAME(TriangleScene, "Triangle Scene") + + +void TriangleScene::initializeCloth(int index, physx::PxVec3 offset) +{ + /////////////////////////////////////////////////////////////////////// + ClothMeshData clothMesh; + + physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f)+ offset, PxQuat(0, PxVec3(1.f, 0.f, 0.f))); + clothMesh.GeneratePlaneCloth(5.f, 6.f, 39, 44, false, transform); + clothMesh.AttachClothPlaneByAngles(39, 44); + clothMesh.SetInvMasses(0.5f + (float)index * 2.0f); + + mClothActor[index] = new ClothActor; + nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc(); + { + mClothActor[index]->mClothRenderMesh = new ClothRenderMesh(meshDesc); + mClothActor[index]->mClothRenderable = getSceneController()->getRenderer().createRenderable(*(static_cast<IRenderMesh*>(mClothActor[index]->mClothRenderMesh)), *getSceneController()->getDefaultMaterial()); + + float r, g, b; + r = index == 0 ? 1.0f : 0.3f; + g = index == 1 ? 1.0f : 0.3f; + b = index == 2 ? 1.0f : 0.3f; + + mClothActor[index]->mClothRenderable->setColor(DirectX::XMFLOAT4(r, g, b, 1.0f)); + } + + nv::cloth::Vector<int32_t>::Type phaseTypeInfo; + mFabric[index] = NvClothCookFabricFromMesh(getSceneController()->getFactory(), meshDesc, physx::PxVec3(0.0f, 0.0f, 1.0f), &phaseTypeInfo, false); + trackFabric(mFabric[index]); + + // Initialize start positions and masses for the actual cloth instance + // (note: the particle/vertex positions do not have to match the mesh description here. Set the positions to the initial shape of this cloth instance) + std::vector<physx::PxVec4> particlesCopy; + particlesCopy.resize(clothMesh.mVertices.size()); + + physx::PxVec3 clothOffset = transform.getPosition(); + for(int i = 0; i < (int)clothMesh.mVertices.size(); i++) + { + // To put attachment point closer to each other + if(clothMesh.mInvMasses[i] < 1e-6) + clothMesh.mVertices[i] = (clothMesh.mVertices[i] - clothOffset)*0.95f + clothOffset; + + particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles + } + + // Create the cloth from the initial positions/masses and the fabric + mClothActor[index]->mCloth = getSceneController()->getFactory()->createCloth(nv::cloth::Range<physx::PxVec4>(&particlesCopy[0], &particlesCopy[0] + particlesCopy.size()), *mFabric[index]); + particlesCopy.clear(); particlesCopy.shrink_to_fit(); + + mClothActor[index]->mCloth->setGravity(physx::PxVec3(0.0f, -9.8f, 0.0f)); + mClothActor[index]->mCloth->setDamping(physx::PxVec3(0.1f, 0.1f, 0.1f)); + + //Generate triangle sphere + physx::PxVec3 meshOffset(0.0f, 11.0f, -1.0f); + auto mesh = MeshGenerator::generateIcosahedron(1.5f,1); + mesh.applyTransfom(physx::PxMat44(physx::PxTransform(meshOffset))); + + //assign as collision data + physx::PxVec3* collisionTriangles; + int vertexCount = mesh.generateTriangleList(&collisionTriangles); + nv::cloth::Range<const physx::PxVec3> trRange(&collisionTriangles[0], &collisionTriangles[0] + vertexCount); + mClothActor[index]->mCloth->setTriangles(trRange, 0, 0); + + //create render mesh + //scale down the render model to not intersect the triangles so much + mesh.applyTransfom( physx::PxMat44(physx::PxTransform(meshOffset))* //redo translation + physx::PxMat44(physx::PxVec4(0.97,0.97,0.97,1.0f))* //scale + physx::PxMat44(physx::PxTransform(-meshOffset))); //undo translation + auto renderMesh = new MeshGenerator::MeshGeneratorRenderMesh(mesh); + Renderable* renderable = getSceneController()->getRenderer().createRenderable(*renderMesh, *getSceneController()->getDefaultMaterial()); + trackRenderable(renderable); + + // Setup phase configs + std::vector<nv::cloth::PhaseConfig> phases(mFabric[index]->getNumPhases()); + for(int i = 0; i < (int)phases.size(); i++) + { + phases[i].mPhaseIndex = i; + phases[i].mStiffness = 1.0f; + phases[i].mStiffnessMultiplier = 1.0f; + phases[i].mCompressionLimit = 1.0f; + phases[i].mStretchLimit = 1.0f; + } + mClothActor[index]->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back())); + mClothActor[index]->mCloth->setDragCoefficient(0.1f); + mClothActor[index]->mCloth->setLiftCoefficient(0.1f); + + trackClothActor(mClothActor[index]); + + // Add the cloth to the solver for simulation + addClothToSolver(mClothActor[index], mSolver); +} + +void TriangleScene::onInitialize() +{ + mSolver = getSceneController()->getFactory()->createSolver(); + trackSolver(mSolver); + + initializeCloth(0, physx::PxVec3(0.0f, 0.0f, 0.0f)); + + { + IRenderMesh* mesh = getSceneController()->getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane); + Renderable* plane = getSceneController()->getRenderer().createRenderable(*mesh, *getSceneController()->getDefaultPlaneMaterial()); + plane->setTransform(PxTransform(PxVec3(0.f, 0.f, 0.f), PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f)))); + plane->setScale(PxVec3(1000.f)); + trackRenderable(plane); + } +} diff --git a/NvCloth/samples/SampleBase/scene/scenes/TriangleScene.h b/NvCloth/samples/SampleBase/scene/scenes/TriangleScene.h new file mode 100644 index 0000000..35cfefb --- /dev/null +++ b/NvCloth/samples/SampleBase/scene/scenes/TriangleScene.h @@ -0,0 +1,33 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef TRIANGLE_SCENE_H +#define TRIANGLE_SCENE_H + +#include "scene/Scene.h" +#include <foundation/PxVec3.h> + +class TriangleScene : public Scene +{ +public: + + TriangleScene(SceneController* sceneController): Scene(sceneController) {} + + void initializeCloth(int index, physx::PxVec3 offset); + virtual void onInitialize() override; + +private: + nv::cloth::Fabric* mFabric[1]; + nv::cloth::Solver* mSolver; + ClothActor* mClothActor[1]; +}; + + +#endif
\ No newline at end of file diff --git a/NvCloth/samples/SampleBase/scene/scenes/WindScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/WindScene.cpp index 51cec1d..232316d 100644 --- a/NvCloth/samples/SampleBase/scene/scenes/WindScene.cpp +++ b/NvCloth/samples/SampleBase/scene/scenes/WindScene.cpp @@ -18,6 +18,7 @@ #include <NvCloth/Factory.h> #include "Renderer.h" #include "renderer/RenderUtils.h" +#include "windows.h" DECLARE_SCENE_NAME(WindScene, "Wind Scene") @@ -27,17 +28,19 @@ void WindScene::Animate(double dt) if(mTime > 3.7f) { - float dvx = 3.f * cos(25.f * mTime); - float vy = max(0.f, 0.9f * cos(11.f * mTime)); - float dvz = 1.5f * sin(19.f * mTime); + float dvx = 6.0f * sin((1.f + sinf(mTime) * 0.5f) * mTime); + float vy = 0; + float dvz = 50.0f + 7.0f * sin((9.f + sinf(mTime + 2.0f) * 0.5f) * mTime) + + 4.0f * sin((7.f + sinf(mTime * 0.9f) * 0.5f) * mTime); + for(int i = 0; i < 3; i++) { - physx::PxVec3 wind = physx::PxVec3(2.5f + dvx, vy, 15.f + dvz); + physx::PxVec3 wind = physx::PxVec3(dvx, vy, dvz); mClothActor[i]->mCloth->setWindVelocity(wind); } } - doSimulationStep(dt); + Scene::Animate(dt); } void WindScene::initializeCloth(int index, physx::PxVec3 offset) @@ -45,17 +48,23 @@ void WindScene::initializeCloth(int index, physx::PxVec3 offset) /////////////////////////////////////////////////////////////////////// ClothMeshData clothMesh; - physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f)+ offset, PxQuat(PxPi / 6.f, PxVec3(1.f, 0.f, 0.f))); - clothMesh.GeneratePlaneCloth(5.f, 6.f, 49, 59, false, transform); - clothMesh.AttachClothPlaneByAngles(49, 59); - clothMesh.SetInvMasses(0.2f + (float)index * 1.4f); + physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f) + offset, PxQuat(PxPi / 2.0f, PxVec3(1.f, 0.f, 0.f))); + clothMesh.GeneratePlaneCloth(5.f, 6.f, 69, 79, false, transform); + clothMesh.AttachClothPlaneByAngles(69, 79); + clothMesh.SetInvMasses(0.5f + (float)index * 2.0f); mClothActor[index] = new ClothActor; nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc(); { mClothActor[index]->mClothRenderMesh = new ClothRenderMesh(meshDesc); mClothActor[index]->mClothRenderable = getSceneController()->getRenderer().createRenderable(*(static_cast<IRenderMesh*>(mClothActor[index]->mClothRenderMesh)), *getSceneController()->getDefaultMaterial()); - mClothActor[index]->mClothRenderable->setColor(getRandomPastelColor()); + + float r, g, b; + r = index == 0 ? 1.0f : 0.3f; + g = index == 1 ? 1.0f : 0.3f; + b = index == 2 ? 1.0f : 0.3f; + + mClothActor[index]->mClothRenderable->setColor(DirectX::XMFLOAT4(r, g, b, 1.0f)); } nv::cloth::Vector<int32_t>::Type phaseTypeInfo; @@ -68,11 +77,11 @@ void WindScene::initializeCloth(int index, physx::PxVec3 offset) particlesCopy.resize(clothMesh.mVertices.size()); physx::PxVec3 clothOffset = transform.getPosition(); - for(int i = 0; i < (int)clothMesh.mVertices.size(); i++) + for (int i = 0; i < (int)clothMesh.mVertices.size(); i++) { // To put attachment point closer to each other if(clothMesh.mInvMasses[i] < 1e-6) - clothMesh.mVertices[i] = (clothMesh.mVertices[i] - clothOffset)*0.9f + clothOffset; + clothMesh.mVertices[i] = (clothMesh.mVertices[i] - clothOffset)*0.95f + clothOffset; particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles } @@ -85,7 +94,7 @@ void WindScene::initializeCloth(int index, physx::PxVec3 offset) // Setup phase configs std::vector<nv::cloth::PhaseConfig> phases(mFabric[index]->getNumPhases()); - for(int i = 0; i < (int)phases.size(); i++) + for (int i = 0; i < (int)phases.size(); i++) { phases[i].mPhaseIndex = i; phases[i].mStiffness = 1.0f; @@ -94,11 +103,9 @@ void WindScene::initializeCloth(int index, physx::PxVec3 offset) phases[i].mStretchLimit = 1.0f; } mClothActor[index]->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back())); - mClothActor[index]->mCloth->setDragCoefficient(0.5f); - mClothActor[index]->mCloth->setLiftCoefficient(0.6f); + mClothActor[index]->mCloth->setDragCoefficient(0.4f); + mClothActor[index]->mCloth->setLiftCoefficient(0.25f); - mSolver = getSceneController()->getFactory()->createSolver(); - trackSolver(mSolver); trackClothActor(mClothActor[index]); // Add the cloth to the solver for simulation @@ -107,10 +114,12 @@ void WindScene::initializeCloth(int index, physx::PxVec3 offset) void WindScene::onInitialize() { + mSolver = getSceneController()->getFactory()->createSolver(); + trackSolver(mSolver); - initializeCloth(2,physx::PxVec3(-9.0f,0.0f,0.0f)); - initializeCloth(1, physx::PxVec3(-2.0f, 0.0f, 0.0f)); - initializeCloth(0, physx::PxVec3(5.0f, 0.0f, 0.0f)); + initializeCloth(2,physx::PxVec3(-11.0f, 0.0f,0.0f)); + initializeCloth(1, physx::PxVec3(-4.0f, 0.0f, 0.0f)); + initializeCloth(0, physx::PxVec3(3.0f, 0.0f, 0.0f)); mTime = 0.0f; |