1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
/*
* 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 "VirtualParticleScene.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(VirtualParticleScene, "Virtual Particle Scene")
void VirtualParticleScene::Animate(double dt)
{
static float time = 0.0f;
time += dt*0.75;
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(-4.f,10.f,0.f) + mOffset),1.5),
physx::PxVec4(totalTransform.transform(physx::PxVec3( 4.f,10.f,0.f) + mOffset),1.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 VirtualParticleScene::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, 9, 1, false, transform);
clothMesh.AttachClothPlaneByAngles(8, 1);
//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, -50.0f, 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->setFriction(1.0f);
typedef uint32_t arraytype[4];
arraytype * virtualParticleIndices = new uint32_t[clothMesh.mTriangles.size()][4];
for(int i = 0; i < (int)clothMesh.mTriangles.size(); i++)
{
virtualParticleIndices[i][0] = clothMesh.mTriangles[i].a;
virtualParticleIndices[i][1] = clothMesh.mTriangles[i].b;
virtualParticleIndices[i][2] = clothMesh.mTriangles[i].c;
virtualParticleIndices[i][3] = 0;
}
physx::PxVec3 weights[1] =
{
physx::PxVec3(1.0f,1.0f,1.0f) / 3.0f
};
mClothActor[index]->mCloth->setVirtualParticles(nv::cloth::Range<const uint32_t[4]>(virtualParticleIndices, (virtualParticleIndices+ clothMesh.mTriangles.size())), nv::cloth::Range<physx::PxVec3>(weights, weights+1));
physx::PxVec4 spheres[2] = {physx::PxVec4(physx::PxVec3(-4.f,10.f,0.f) + mOffset,1.5),
physx::PxVec4(physx::PxVec3( 4.f,10.f,0.f) + mOffset,1.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 VirtualParticleScene::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);
}
}
|