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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
#include "UnityPrefix.h"
#if ENABLE_PHYSICS
#include "MeshCollider.h"
#include "Runtime/Graphics/Transform.h"
#include "RigidBody.h"
#include "PhysicsManager.h"
#include "Runtime/Serialize/TransferFunctions/SerializeTransfer.h"
#include "Runtime/Filters/AABBUtility.h"
#include "Runtime/Utilities/Utility.h"
#include "Runtime/Filters/Mesh/LodMesh.h"
#include "Runtime/Filters/Mesh/LodMeshFilter.h"
#include "External/PhysX/builds/SDKs/Physics/include/NxPhysics.h"
#include "nxmemorystream.h"
#include "Runtime/BaseClasses/IsPlaying.h"
#include "External/PhysX/builds/SDKs/Cooking/include/NxCooking.h"
#include "Runtime/Misc/BuildSettings.h"
#define GET_CONVEX_SHAPE() ((class NxConvexShape*)m_Shape)
#define GET_MESH_SHAPE() ((class NxTriangleMeshShape*)m_Shape)
#if UNITY_EDITOR
const NxConvexMesh* MeshCollider::GetConvexMesh() const
{
if( !m_Shape )
return NULL;
if( !m_Shape->isConvexMesh() )
return NULL;
const NxConvexMesh& mesh = GET_CONVEX_SHAPE ()->getConvexMesh ();
return &mesh;
}
const NxTriangleMesh* MeshCollider::GetTriangleMesh() const
{
if( !m_Shape )
return NULL;
if( m_Shape->isConvexMesh() )
return NULL;
const NxTriangleMesh& mesh = GET_MESH_SHAPE ()->getTriangleMesh ();
return &mesh;
}
#endif
MeshCollider::MeshCollider (MemLabelId label, ObjectCreationMode mode)
: Super(label, mode)
, m_MeshNode(this)
{
m_Shared = false;
}
MeshCollider::~MeshCollider ()
{
}
void MeshCollider::AwakeFromLoad(AwakeFromLoadMode awakeMode)
{
if (m_Shape)
{
// Apply changed values
if (m_Convex != (m_Shape->isConvexMesh() != NULL))
SetConvex(m_Convex);
if (m_Shape)
{
bool smooth;
if (m_Shape->isConvexMesh())
{
NxConvexShapeDesc desc;
GET_CONVEX_SHAPE ()->saveToDesc(desc);
smooth = desc.meshFlags & NX_MESH_SMOOTH_SPHERE_COLLISIONS;
}
else
{
NxTriangleMeshShapeDesc desc;
GET_MESH_SHAPE ()->saveToDesc(desc);
smooth = desc.meshFlags & NX_MESH_SMOOTH_SPHERE_COLLISIONS;
}
if (smooth != m_SmoothSphereCollisions)
SetSmoothSphereCollisions(m_SmoothSphereCollisions);
SetSharedMesh(m_Mesh);
}
}
Super::AwakeFromLoad (awakeMode);
}
void MeshCollider::Cleanup ()
{
m_MeshNode.RemoveFromList();
if (m_Shape)
{
if (m_Shape->isConvexMesh())
{
NxConvexMesh& mesh = GET_CONVEX_SHAPE ()->getConvexMesh ();
Super::Cleanup ();
if (!m_Shared)
GetDynamicsSDK ().releaseConvexMesh (mesh);
}
else
{
NxTriangleMesh& mesh = GET_MESH_SHAPE ()->getTriangleMesh ();
Super::Cleanup ();
if (!m_Shared)
GetDynamicsSDK ().releaseTriangleMesh (mesh);
}
}
}
void MeshCollider::DidDeleteMesh ()
{
Cleanup ();
}
void MeshCollider::CreateShape( void* nxmesh, const Rigidbody* ignoreRigidbody )
{
if ( !nxmesh )
return;
if (m_Convex)
{
NxConvexShapeDesc shapeDesc;
shapeDesc.meshData = (NxConvexMesh*)nxmesh;
if (m_SmoothSphereCollisions)
shapeDesc.meshFlags |= NX_MESH_SMOOTH_SPHERE_COLLISIONS;
FinalizeCreate (shapeDesc, true, ignoreRigidbody);
}
else
{
NxTriangleMeshShapeDesc shapeDesc;
if (m_SmoothSphereCollisions)
shapeDesc.meshFlags |= NX_MESH_SMOOTH_SPHERE_COLLISIONS;
shapeDesc.meshData = (NxTriangleMesh*)nxmesh;
FinalizeCreate (shapeDesc, true, ignoreRigidbody);
}
}
//@TODO: Updating physics mesh when reimporting no longer works!
void MeshCollider::Create (const Rigidbody* ignoreRigidbody)
{
if (m_Shape)
Cleanup ();
Mesh* mesh = m_Mesh;
m_CachedMesh = mesh;
if (mesh == NULL)
return;
// do not create anything if have no vertices or less-than-one triangle
if (mesh->GetVertexCount() == 0 || mesh->GetPrimitiveCount() == 0)
return;
/*
NxCookingParams params;
params.hintCollisionSpeed = true;
NxSetCookingParams (params);
*/
Matrix4x4f scalematrix;
TransformType type = GetComponent (Transform).CalculateTransformMatrixScaleDelta (scalematrix);
void* nxmesh = NULL;
if (IsNoScaleTransform(type) && !mesh->IsSharedPhysicsMeshDirty())
{
m_Shared = true;
if (m_Convex)
{
nxmesh = mesh->GetSharedNxConvexMesh ();
}
else
{
nxmesh = mesh->GetSharedNxMesh ();
}
}
// For scaled meshes, create instance
else
{
m_Shared = false;
nxmesh = CreateNxMeshFromUnityMesh(mesh, m_Convex, scalematrix, type);
}
if (nxmesh == NULL)
return;
mesh->AddObjectUser( m_MeshNode );
CreateShape( nxmesh, ignoreRigidbody );
}
void MeshCollider::ReCreate()
{
if( !m_Shape )
return;
// Re-creating the full mesh collider is expensive, so we only re-create the shape and not the mesh.
if ( m_Shape->isConvexMesh() )
{
NxConvexMesh& mesh = GET_CONVEX_SHAPE ()->getConvexMesh ();
Super::Cleanup();
CreateShape( &mesh, NULL );
}
else
{
NxTriangleMesh& mesh = GET_MESH_SHAPE ()->getTriangleMesh ();
Super::Cleanup();
CreateShape( &mesh, NULL );
}
}
void MeshCollider::ScaleChanged ()
{
Create (NULL);
}
void MeshCollider::TransformChanged (int changeMask)
{
Super::TransformChanged (changeMask);
if (m_Shape)
{
if (m_Shape->getActor ().userData == NULL)
{
PROFILER_AUTO(gStaticColliderMove, this)
FetchPoseFromTransform ();
}
else
{
Rigidbody* body = (Rigidbody*)m_Shape->getActor ().userData;
Matrix4x4f matrix;
if (GetRelativeToParentPositionAndRotation (GetComponent (Transform), body->GetComponent (Transform), matrix))
{
NxMat34 shapeMatrix;
shapeMatrix.setColumnMajor44 (matrix.GetPtr ());
m_Shape->setLocalPose (shapeMatrix);
}
if (body->GetGameObjectPtr() != GetGameObjectPtr() || changeMask & Transform::kScaleChanged)
RigidbodyMassDistributionChanged ();
}
if (changeMask & Transform::kScaleChanged)
ScaleChanged ();
RefreshPhysicsInEditMode();
}
else if (IS_CONTENT_NEWER_OR_SAME(kUnityVersion4_0_a1) && IsActive() && GetEnabled())
Create (NULL);
}
void MeshCollider::InitializeClass ()
{
REGISTER_MESSAGE_VOID(MeshCollider, kDidDeleteMesh, DidDeleteMesh);
}
void MeshCollider::SetSharedMesh (const PPtr<Mesh> m)
{
if (m_CachedMesh != m)
{
SetDirty ();
m_Mesh = m;
if (IsActive())
Create(NULL);
}
}
PPtr<Mesh> MeshCollider::GetSharedMesh ()
{
return m_Mesh;
}
void MeshCollider::Reset ()
{
Super::Reset ();
if (GetGameObjectPtr ())
{
MeshFilter* filter = QueryComponent (MeshFilter);
if (filter && m_Mesh.GetInstanceID () == 0)
{
PPtr<Mesh> newMesh = filter->GetSharedMesh ();
if (newMesh != m_Mesh)
{
m_Mesh = newMesh;
if (IsActive())
Create(NULL);
}
}
}
m_SmoothSphereCollisions = false;
m_Convex = false;
}
template<class TransferFunction>
void MeshCollider::Transfer (TransferFunction& transfer)
{
Super::Transfer (transfer);
transfer.SetVersion (2);
TRANSFER (m_SmoothSphereCollisions);
transfer.Transfer (m_Convex, "m_Convex");
transfer.Align();
TRANSFER (m_Mesh);
}
void MeshCollider::SetConvex (bool convex)
{
m_Convex = convex;
if (m_Shape)
Create(NULL);
SetDirty();
RefreshPhysicsInEditMode();
}
void MeshCollider::SetSmoothSphereCollisions (bool smooth)
{
m_SmoothSphereCollisions = smooth;
if (m_Shape)
Create(NULL);
SetDirty();
RefreshPhysicsInEditMode();
}
IMPLEMENT_CLASS_HAS_INIT (MeshCollider)
IMPLEMENT_OBJECT_SERIALIZE (MeshCollider)
#endif //ENABLE_PHYSICS
|