diff options
Diffstat (limited to '')
-rwxr-xr-x | OpenSim/Region/Physics/BulletSPlugin/BSShapeCollection.cs | 1009 |
1 files changed, 0 insertions, 1009 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSShapeCollection.cs b/OpenSim/Region/Physics/BulletSPlugin/BSShapeCollection.cs deleted file mode 100755 index d361f18..0000000 --- a/OpenSim/Region/Physics/BulletSPlugin/BSShapeCollection.cs +++ /dev/null | |||
@@ -1,1009 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyrightD | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | using System; | ||
28 | using System.Collections.Generic; | ||
29 | using System.Text; | ||
30 | using OMV = OpenMetaverse; | ||
31 | using OpenSim.Framework; | ||
32 | using OpenSim.Region.Physics.Manager; | ||
33 | using OpenSim.Region.Physics.ConvexDecompositionDotNet; | ||
34 | |||
35 | namespace OpenSim.Region.Physics.BulletSPlugin | ||
36 | { | ||
37 | public sealed class BSShapeCollection : IDisposable | ||
38 | { | ||
39 | private static string LogHeader = "[BULLETSIM SHAPE COLLECTION]"; | ||
40 | |||
41 | private BSScene PhysicsScene { get; set; } | ||
42 | |||
43 | private Object m_collectionActivityLock = new Object(); | ||
44 | |||
45 | // Description of a Mesh | ||
46 | private struct MeshDesc | ||
47 | { | ||
48 | public BulletShape shape; | ||
49 | public int referenceCount; | ||
50 | public DateTime lastReferenced; | ||
51 | public UInt64 shapeKey; | ||
52 | } | ||
53 | |||
54 | // Description of a hull. | ||
55 | // Meshes and hulls have the same shape hash key but we only need hulls for efficient collision calculations. | ||
56 | private struct HullDesc | ||
57 | { | ||
58 | public BulletShape shape; | ||
59 | public int referenceCount; | ||
60 | public DateTime lastReferenced; | ||
61 | public UInt64 shapeKey; | ||
62 | } | ||
63 | |||
64 | // The sharable set of meshes and hulls. Indexed by their shape hash. | ||
65 | private Dictionary<System.UInt64, MeshDesc> Meshes = new Dictionary<System.UInt64, MeshDesc>(); | ||
66 | private Dictionary<System.UInt64, HullDesc> Hulls = new Dictionary<System.UInt64, HullDesc>(); | ||
67 | |||
68 | private bool DDetail = false; | ||
69 | |||
70 | public BSShapeCollection(BSScene physScene) | ||
71 | { | ||
72 | PhysicsScene = physScene; | ||
73 | // Set the next to 'true' for very detailed shape update detailed logging (detailed details?) | ||
74 | // While detailed debugging is still active, this is better than commenting out all the | ||
75 | // DetailLog statements. When debugging slows down, this and the protected logging | ||
76 | // statements can be commented/removed. | ||
77 | DDetail = true; | ||
78 | } | ||
79 | |||
80 | public void Dispose() | ||
81 | { | ||
82 | // TODO!!!!!!!!! | ||
83 | } | ||
84 | |||
85 | // Callbacks called just before either the body or shape is destroyed. | ||
86 | // Mostly used for changing bodies out from under Linksets. | ||
87 | // Useful for other cases where parameters need saving. | ||
88 | // Passing 'null' says no callback. | ||
89 | public delegate void ShapeDestructionCallback(BulletShape shape); | ||
90 | public delegate void BodyDestructionCallback(BulletBody body); | ||
91 | |||
92 | // Called to update/change the body and shape for an object. | ||
93 | // First checks the shape and updates that if necessary then makes | ||
94 | // sure the body is of the right type. | ||
95 | // Return 'true' if either the body or the shape changed. | ||
96 | // 'shapeCallback' and 'bodyCallback' are, if non-null, functions called just before | ||
97 | // the current shape or body is destroyed. This allows the caller to remove any | ||
98 | // higher level dependencies on the shape or body. Mostly used for LinkSets to | ||
99 | // remove the physical constraints before the body is destroyed. | ||
100 | // Called at taint-time!! | ||
101 | public bool GetBodyAndShape(bool forceRebuild, BulletWorld sim, BSPhysObject prim, | ||
102 | ShapeDestructionCallback shapeCallback, BodyDestructionCallback bodyCallback) | ||
103 | { | ||
104 | PhysicsScene.AssertInTaintTime("BSShapeCollection.GetBodyAndShape"); | ||
105 | |||
106 | bool ret = false; | ||
107 | |||
108 | // This lock could probably be pushed down lower but building shouldn't take long | ||
109 | lock (m_collectionActivityLock) | ||
110 | { | ||
111 | // Do we have the correct geometry for this type of object? | ||
112 | // Updates prim.BSShape with information/pointers to shape. | ||
113 | // Returns 'true' of BSShape is changed to a new shape. | ||
114 | bool newGeom = CreateGeom(forceRebuild, prim, shapeCallback); | ||
115 | // If we had to select a new shape geometry for the object, | ||
116 | // rebuild the body around it. | ||
117 | // Updates prim.BSBody with information/pointers to requested body | ||
118 | // Returns 'true' if BSBody was changed. | ||
119 | bool newBody = CreateBody((newGeom || forceRebuild), prim, PhysicsScene.World, | ||
120 | prim.PhysShape, bodyCallback); | ||
121 | ret = newGeom || newBody; | ||
122 | } | ||
123 | DetailLog("{0},BSShapeCollection.GetBodyAndShape,taintExit,force={1},ret={2},body={3},shape={4}", | ||
124 | prim.LocalID, forceRebuild, ret, prim.PhysBody, prim.PhysShape); | ||
125 | |||
126 | return ret; | ||
127 | } | ||
128 | |||
129 | public bool GetBodyAndShape(bool forceRebuild, BulletWorld sim, BSPhysObject prim) | ||
130 | { | ||
131 | return GetBodyAndShape(forceRebuild, sim, prim, null, null); | ||
132 | } | ||
133 | |||
134 | // Track another user of a body. | ||
135 | // We presume the caller has allocated the body. | ||
136 | // Bodies only have one user so the body is just put into the world if not already there. | ||
137 | public void ReferenceBody(BulletBody body, bool inTaintTime) | ||
138 | { | ||
139 | lock (m_collectionActivityLock) | ||
140 | { | ||
141 | if (DDetail) DetailLog("{0},BSShapeCollection.ReferenceBody,newBody,body={1}", body.ID, body); | ||
142 | PhysicsScene.TaintedObject(inTaintTime, "BSShapeCollection.ReferenceBody", delegate() | ||
143 | { | ||
144 | if (!PhysicsScene.PE.IsInWorld(PhysicsScene.World, body)) | ||
145 | { | ||
146 | PhysicsScene.PE.AddObjectToWorld(PhysicsScene.World, body); | ||
147 | if (DDetail) DetailLog("{0},BSShapeCollection.ReferenceBody,addedToWorld,ref={1}", body.ID, body); | ||
148 | } | ||
149 | }); | ||
150 | } | ||
151 | } | ||
152 | |||
153 | // Release the usage of a body. | ||
154 | // Called when releasing use of a BSBody. BSShape is handled separately. | ||
155 | public void DereferenceBody(BulletBody body, bool inTaintTime, BodyDestructionCallback bodyCallback ) | ||
156 | { | ||
157 | if (!body.HasPhysicalBody) | ||
158 | return; | ||
159 | |||
160 | lock (m_collectionActivityLock) | ||
161 | { | ||
162 | PhysicsScene.TaintedObject(inTaintTime, "BSShapeCollection.DereferenceBody", delegate() | ||
163 | { | ||
164 | if (DDetail) DetailLog("{0},BSShapeCollection.DereferenceBody,DestroyingBody,body={1},inTaintTime={2}", | ||
165 | body.ID, body, inTaintTime); | ||
166 | // If the caller needs to know the old body is going away, pass the event up. | ||
167 | if (bodyCallback != null) bodyCallback(body); | ||
168 | |||
169 | if (PhysicsScene.PE.IsInWorld(PhysicsScene.World, body)) | ||
170 | { | ||
171 | PhysicsScene.PE.RemoveObjectFromWorld(PhysicsScene.World, body); | ||
172 | if (DDetail) DetailLog("{0},BSShapeCollection.DereferenceBody,removingFromWorld. Body={1}", body.ID, body); | ||
173 | } | ||
174 | |||
175 | // Zero any reference to the shape so it is not freed when the body is deleted. | ||
176 | PhysicsScene.PE.SetCollisionShape(PhysicsScene.World, body, null); | ||
177 | PhysicsScene.PE.DestroyObject(PhysicsScene.World, body); | ||
178 | }); | ||
179 | } | ||
180 | } | ||
181 | |||
182 | // Track the datastructures and use count for a shape. | ||
183 | // When creating a hull, this is called first to reference the mesh | ||
184 | // and then again to reference the hull. | ||
185 | // Meshes and hulls for the same shape have the same hash key. | ||
186 | // NOTE that native shapes are not added to the mesh list or removed. | ||
187 | // Returns 'true' if this is the initial reference to the shape. Otherwise reused. | ||
188 | public bool ReferenceShape(BulletShape shape) | ||
189 | { | ||
190 | bool ret = false; | ||
191 | switch (shape.type) | ||
192 | { | ||
193 | case BSPhysicsShapeType.SHAPE_MESH: | ||
194 | MeshDesc meshDesc; | ||
195 | if (Meshes.TryGetValue(shape.shapeKey, out meshDesc)) | ||
196 | { | ||
197 | // There is an existing instance of this mesh. | ||
198 | meshDesc.referenceCount++; | ||
199 | if (DDetail) DetailLog("{0},BSShapeCollection.ReferenceShape,existingMesh,key={1},cnt={2}", | ||
200 | BSScene.DetailLogZero, shape.shapeKey.ToString("X"), meshDesc.referenceCount); | ||
201 | } | ||
202 | else | ||
203 | { | ||
204 | // This is a new reference to a mesh | ||
205 | meshDesc.shape = shape.Clone(); | ||
206 | meshDesc.shapeKey = shape.shapeKey; | ||
207 | // We keep a reference to the underlying IMesh data so a hull can be built | ||
208 | meshDesc.referenceCount = 1; | ||
209 | if (DDetail) DetailLog("{0},BSShapeCollection.ReferenceShape,newMesh,key={1},cnt={2}", | ||
210 | BSScene.DetailLogZero, shape.shapeKey.ToString("X"), meshDesc.referenceCount); | ||
211 | ret = true; | ||
212 | } | ||
213 | meshDesc.lastReferenced = System.DateTime.Now; | ||
214 | Meshes[shape.shapeKey] = meshDesc; | ||
215 | break; | ||
216 | case BSPhysicsShapeType.SHAPE_HULL: | ||
217 | HullDesc hullDesc; | ||
218 | if (Hulls.TryGetValue(shape.shapeKey, out hullDesc)) | ||
219 | { | ||
220 | // There is an existing instance of this hull. | ||
221 | hullDesc.referenceCount++; | ||
222 | if (DDetail) DetailLog("{0},BSShapeCollection.ReferenceShape,existingHull,key={1},cnt={2}", | ||
223 | BSScene.DetailLogZero, shape.shapeKey.ToString("X"), hullDesc.referenceCount); | ||
224 | } | ||
225 | else | ||
226 | { | ||
227 | // This is a new reference to a hull | ||
228 | hullDesc.shape = shape.Clone(); | ||
229 | hullDesc.shapeKey = shape.shapeKey; | ||
230 | hullDesc.referenceCount = 1; | ||
231 | if (DDetail) DetailLog("{0},BSShapeCollection.ReferenceShape,newHull,key={1},cnt={2}", | ||
232 | BSScene.DetailLogZero, shape.shapeKey.ToString("X"), hullDesc.referenceCount); | ||
233 | ret = true; | ||
234 | |||
235 | } | ||
236 | hullDesc.lastReferenced = System.DateTime.Now; | ||
237 | Hulls[shape.shapeKey] = hullDesc; | ||
238 | break; | ||
239 | case BSPhysicsShapeType.SHAPE_UNKNOWN: | ||
240 | break; | ||
241 | default: | ||
242 | // Native shapes are not tracked and they don't go into any list | ||
243 | break; | ||
244 | } | ||
245 | return ret; | ||
246 | } | ||
247 | |||
248 | // Release the usage of a shape. | ||
249 | public void DereferenceShape(BulletShape shape, bool inTaintTime, ShapeDestructionCallback shapeCallback) | ||
250 | { | ||
251 | if (!shape.HasPhysicalShape) | ||
252 | return; | ||
253 | |||
254 | PhysicsScene.TaintedObject(inTaintTime, "BSShapeCollection.DereferenceShape", delegate() | ||
255 | { | ||
256 | if (shape.HasPhysicalShape) | ||
257 | { | ||
258 | if (shape.isNativeShape) | ||
259 | { | ||
260 | // Native shapes are not tracked and are released immediately | ||
261 | if (DDetail) DetailLog("{0},BSShapeCollection.DereferenceShape,deleteNativeShape,ptr={1},taintTime={2}", | ||
262 | BSScene.DetailLogZero, shape.AddrString, inTaintTime); | ||
263 | if (shapeCallback != null) shapeCallback(shape); | ||
264 | PhysicsScene.PE.DeleteCollisionShape(PhysicsScene.World, shape); | ||
265 | } | ||
266 | else | ||
267 | { | ||
268 | switch (shape.type) | ||
269 | { | ||
270 | case BSPhysicsShapeType.SHAPE_HULL: | ||
271 | DereferenceHull(shape, shapeCallback); | ||
272 | break; | ||
273 | case BSPhysicsShapeType.SHAPE_MESH: | ||
274 | DereferenceMesh(shape, shapeCallback); | ||
275 | break; | ||
276 | case BSPhysicsShapeType.SHAPE_COMPOUND: | ||
277 | DereferenceCompound(shape, shapeCallback); | ||
278 | break; | ||
279 | case BSPhysicsShapeType.SHAPE_UNKNOWN: | ||
280 | break; | ||
281 | default: | ||
282 | break; | ||
283 | } | ||
284 | } | ||
285 | } | ||
286 | }); | ||
287 | } | ||
288 | |||
289 | // Count down the reference count for a mesh shape | ||
290 | // Called at taint-time. | ||
291 | private void DereferenceMesh(BulletShape shape, ShapeDestructionCallback shapeCallback) | ||
292 | { | ||
293 | MeshDesc meshDesc; | ||
294 | if (Meshes.TryGetValue(shape.shapeKey, out meshDesc)) | ||
295 | { | ||
296 | meshDesc.referenceCount--; | ||
297 | // TODO: release the Bullet storage | ||
298 | if (shapeCallback != null) shapeCallback(shape); | ||
299 | meshDesc.lastReferenced = System.DateTime.Now; | ||
300 | Meshes[shape.shapeKey] = meshDesc; | ||
301 | if (DDetail) DetailLog("{0},BSShapeCollection.DereferenceMesh,shape={1},refCnt={2}", | ||
302 | BSScene.DetailLogZero, shape, meshDesc.referenceCount); | ||
303 | |||
304 | } | ||
305 | } | ||
306 | |||
307 | // Count down the reference count for a hull shape | ||
308 | // Called at taint-time. | ||
309 | private void DereferenceHull(BulletShape shape, ShapeDestructionCallback shapeCallback) | ||
310 | { | ||
311 | HullDesc hullDesc; | ||
312 | if (Hulls.TryGetValue(shape.shapeKey, out hullDesc)) | ||
313 | { | ||
314 | hullDesc.referenceCount--; | ||
315 | // TODO: release the Bullet storage (aging old entries?) | ||
316 | |||
317 | // Tell upper layers that, if they have dependencies on this shape, this link is going away | ||
318 | if (shapeCallback != null) shapeCallback(shape); | ||
319 | |||
320 | hullDesc.lastReferenced = System.DateTime.Now; | ||
321 | Hulls[shape.shapeKey] = hullDesc; | ||
322 | if (DDetail) DetailLog("{0},BSShapeCollection.DereferenceHull,shape={1},refCnt={2}", | ||
323 | BSScene.DetailLogZero, shape, hullDesc.referenceCount); | ||
324 | } | ||
325 | } | ||
326 | |||
327 | // Remove a reference to a compound shape. | ||
328 | // Taking a compound shape apart is a little tricky because if you just delete the | ||
329 | // physical shape, it will free all the underlying children. We can't do that because | ||
330 | // they could be shared. So, this removes each of the children from the compound and | ||
331 | // dereferences them separately before destroying the compound collision object itself. | ||
332 | // Called at taint-time. | ||
333 | private void DereferenceCompound(BulletShape shape, ShapeDestructionCallback shapeCallback) | ||
334 | { | ||
335 | if (!PhysicsScene.PE.IsCompound(shape)) | ||
336 | { | ||
337 | // Failed the sanity check!! | ||
338 | PhysicsScene.Logger.ErrorFormat("{0} Attempt to free a compound shape that is not compound!! type={1}, ptr={2}", | ||
339 | LogHeader, shape.type, shape.AddrString); | ||
340 | if (DDetail) DetailLog("{0},BSShapeCollection.DereferenceCompound,notACompoundShape,type={1},ptr={2}", | ||
341 | BSScene.DetailLogZero, shape.type, shape.AddrString); | ||
342 | return; | ||
343 | } | ||
344 | |||
345 | int numChildren = PhysicsScene.PE.GetNumberOfCompoundChildren(shape); | ||
346 | if (DDetail) DetailLog("{0},BSShapeCollection.DereferenceCompound,shape={1},children={2}", BSScene.DetailLogZero, shape, numChildren); | ||
347 | |||
348 | for (int ii = numChildren - 1; ii >= 0; ii--) | ||
349 | { | ||
350 | BulletShape childShape = PhysicsScene.PE.RemoveChildShapeFromCompoundShapeIndex(shape, ii); | ||
351 | DereferenceAnonCollisionShape(childShape); | ||
352 | } | ||
353 | PhysicsScene.PE.DeleteCollisionShape(PhysicsScene.World, shape); | ||
354 | } | ||
355 | |||
356 | // Sometimes we have a pointer to a collision shape but don't know what type it is. | ||
357 | // Figure out type and call the correct dereference routine. | ||
358 | // Called at taint-time. | ||
359 | private void DereferenceAnonCollisionShape(BulletShape shapeInfo) | ||
360 | { | ||
361 | MeshDesc meshDesc; | ||
362 | HullDesc hullDesc; | ||
363 | |||
364 | if (TryGetMeshByPtr(shapeInfo, out meshDesc)) | ||
365 | { | ||
366 | shapeInfo.type = BSPhysicsShapeType.SHAPE_MESH; | ||
367 | shapeInfo.shapeKey = meshDesc.shapeKey; | ||
368 | } | ||
369 | else | ||
370 | { | ||
371 | if (TryGetHullByPtr(shapeInfo, out hullDesc)) | ||
372 | { | ||
373 | shapeInfo.type = BSPhysicsShapeType.SHAPE_HULL; | ||
374 | shapeInfo.shapeKey = hullDesc.shapeKey; | ||
375 | } | ||
376 | else | ||
377 | { | ||
378 | if (PhysicsScene.PE.IsCompound(shapeInfo)) | ||
379 | { | ||
380 | shapeInfo.type = BSPhysicsShapeType.SHAPE_COMPOUND; | ||
381 | } | ||
382 | else | ||
383 | { | ||
384 | if (PhysicsScene.PE.IsNativeShape(shapeInfo)) | ||
385 | { | ||
386 | shapeInfo.isNativeShape = true; | ||
387 | shapeInfo.type = BSPhysicsShapeType.SHAPE_BOX; // (technically, type doesn't matter) | ||
388 | } | ||
389 | } | ||
390 | } | ||
391 | } | ||
392 | |||
393 | if (DDetail) DetailLog("{0},BSShapeCollection.DereferenceAnonCollisionShape,shape={1}", BSScene.DetailLogZero, shapeInfo); | ||
394 | |||
395 | if (shapeInfo.type != BSPhysicsShapeType.SHAPE_UNKNOWN) | ||
396 | { | ||
397 | DereferenceShape(shapeInfo, true, null); | ||
398 | } | ||
399 | else | ||
400 | { | ||
401 | PhysicsScene.Logger.ErrorFormat("{0} Could not decypher shape type. Region={1}, addr={2}", | ||
402 | LogHeader, PhysicsScene.RegionName, shapeInfo.AddrString); | ||
403 | } | ||
404 | } | ||
405 | |||
406 | // Create the geometry information in Bullet for later use. | ||
407 | // The objects needs a hull if it's physical otherwise a mesh is enough. | ||
408 | // if 'forceRebuild' is true, the geometry is unconditionally rebuilt. For meshes and hulls, | ||
409 | // shared geometries will be used. If the parameters of the existing shape are the same | ||
410 | // as this request, the shape is not rebuilt. | ||
411 | // Info in prim.BSShape is updated to the new shape. | ||
412 | // Returns 'true' if the geometry was rebuilt. | ||
413 | // Called at taint-time! | ||
414 | private bool CreateGeom(bool forceRebuild, BSPhysObject prim, ShapeDestructionCallback shapeCallback) | ||
415 | { | ||
416 | bool ret = false; | ||
417 | bool haveShape = false; | ||
418 | |||
419 | if (!haveShape && prim.PreferredPhysicalShape == BSPhysicsShapeType.SHAPE_CAPSULE) | ||
420 | { | ||
421 | // an avatar capsule is close to a native shape (it is not shared) | ||
422 | GetReferenceToNativeShape(prim, BSPhysicsShapeType.SHAPE_CAPSULE, FixedShapeKey.KEY_CAPSULE, shapeCallback); | ||
423 | if (DDetail) DetailLog("{0},BSShapeCollection.CreateGeom,avatarCapsule,shape={1}", prim.LocalID, prim.PhysShape); | ||
424 | ret = true; | ||
425 | haveShape = true; | ||
426 | } | ||
427 | |||
428 | // Compound shapes are handled special as they are rebuilt from scratch. | ||
429 | // This isn't too great a hardship since most of the child shapes will have already been created. | ||
430 | if (!haveShape && prim.PreferredPhysicalShape == BSPhysicsShapeType.SHAPE_COMPOUND) | ||
431 | { | ||
432 | ret = GetReferenceToCompoundShape(prim, shapeCallback); | ||
433 | if (DDetail) DetailLog("{0},BSShapeCollection.CreateGeom,compoundShape,shape={1}", prim.LocalID, prim.PhysShape); | ||
434 | haveShape = true; | ||
435 | } | ||
436 | |||
437 | if (!haveShape) | ||
438 | { | ||
439 | ret = CreateGeomNonSpecial(forceRebuild, prim, shapeCallback); | ||
440 | } | ||
441 | |||
442 | return ret; | ||
443 | } | ||
444 | |||
445 | // Create a mesh/hull shape or a native shape if 'nativeShapePossible' is 'true'. | ||
446 | public bool CreateGeomNonSpecial(bool forceRebuild, BSPhysObject prim, ShapeDestructionCallback shapeCallback) | ||
447 | { | ||
448 | bool ret = false; | ||
449 | bool haveShape = false; | ||
450 | bool nativeShapePossible = true; | ||
451 | PrimitiveBaseShape pbs = prim.BaseShape; | ||
452 | |||
453 | // If the prim attributes are simple, this could be a simple Bullet native shape | ||
454 | if (!haveShape | ||
455 | && pbs != null | ||
456 | && nativeShapePossible | ||
457 | && ((pbs.SculptEntry && !BSParam.ShouldMeshSculptedPrim) | ||
458 | || (pbs.ProfileBegin == 0 && pbs.ProfileEnd == 0 | ||
459 | && pbs.ProfileHollow == 0 | ||
460 | && pbs.PathTwist == 0 && pbs.PathTwistBegin == 0 | ||
461 | && pbs.PathBegin == 0 && pbs.PathEnd == 0 | ||
462 | && pbs.PathTaperX == 0 && pbs.PathTaperY == 0 | ||
463 | && pbs.PathScaleX == 100 && pbs.PathScaleY == 100 | ||
464 | && pbs.PathShearX == 0 && pbs.PathShearY == 0) ) ) | ||
465 | { | ||
466 | // Get the scale of any existing shape so we can see if the new shape is same native type and same size. | ||
467 | OMV.Vector3 scaleOfExistingShape = OMV.Vector3.Zero; | ||
468 | if (prim.PhysShape.HasPhysicalShape) | ||
469 | scaleOfExistingShape = PhysicsScene.PE.GetLocalScaling(prim.PhysShape); | ||
470 | |||
471 | if (DDetail) DetailLog("{0},BSShapeCollection.CreateGeom,maybeNative,force={1},primScale={2},primSize={3},primShape={4}", | ||
472 | prim.LocalID, forceRebuild, prim.Scale, prim.Size, prim.PhysShape.type); | ||
473 | |||
474 | // It doesn't look like Bullet scales spheres so make sure the scales are all equal | ||
475 | if ((pbs.ProfileShape == ProfileShape.HalfCircle && pbs.PathCurve == (byte)Extrusion.Curve1) | ||
476 | && pbs.Scale.X == pbs.Scale.Y && pbs.Scale.Y == pbs.Scale.Z) | ||
477 | { | ||
478 | haveShape = true; | ||
479 | if (forceRebuild | ||
480 | || prim.Scale != scaleOfExistingShape | ||
481 | || prim.PhysShape.type != BSPhysicsShapeType.SHAPE_SPHERE | ||
482 | ) | ||
483 | { | ||
484 | ret = GetReferenceToNativeShape(prim, BSPhysicsShapeType.SHAPE_SPHERE, | ||
485 | FixedShapeKey.KEY_SPHERE, shapeCallback); | ||
486 | if (DDetail) DetailLog("{0},BSShapeCollection.CreateGeom,sphere,force={1},shape={2}", | ||
487 | prim.LocalID, forceRebuild, prim.PhysShape); | ||
488 | } | ||
489 | } | ||
490 | if (!haveShape && pbs.ProfileShape == ProfileShape.Square && pbs.PathCurve == (byte)Extrusion.Straight) | ||
491 | { | ||
492 | haveShape = true; | ||
493 | if (forceRebuild | ||
494 | || prim.Scale != scaleOfExistingShape | ||
495 | || prim.PhysShape.type != BSPhysicsShapeType.SHAPE_BOX | ||
496 | ) | ||
497 | { | ||
498 | ret = GetReferenceToNativeShape( prim, BSPhysicsShapeType.SHAPE_BOX, | ||
499 | FixedShapeKey.KEY_BOX, shapeCallback); | ||
500 | if (DDetail) DetailLog("{0},BSShapeCollection.CreateGeom,box,force={1},shape={2}", | ||
501 | prim.LocalID, forceRebuild, prim.PhysShape); | ||
502 | } | ||
503 | } | ||
504 | } | ||
505 | |||
506 | // If a simple shape is not happening, create a mesh and possibly a hull. | ||
507 | if (!haveShape && pbs != null) | ||
508 | { | ||
509 | ret = CreateGeomMeshOrHull(prim, shapeCallback); | ||
510 | } | ||
511 | |||
512 | return ret; | ||
513 | } | ||
514 | |||
515 | public bool CreateGeomMeshOrHull(BSPhysObject prim, ShapeDestructionCallback shapeCallback) | ||
516 | { | ||
517 | |||
518 | bool ret = false; | ||
519 | // Note that if it's a native shape, the check for physical/non-physical is not | ||
520 | // made. Native shapes work in either case. | ||
521 | if (prim.IsPhysical && BSParam.ShouldUseHullsForPhysicalObjects) | ||
522 | { | ||
523 | // Update prim.BSShape to reference a hull of this shape. | ||
524 | ret = GetReferenceToHull(prim,shapeCallback); | ||
525 | if (DDetail) DetailLog("{0},BSShapeCollection.CreateGeom,hull,shape={1},key={2}", | ||
526 | prim.LocalID, prim.PhysShape, prim.PhysShape.shapeKey.ToString("X")); | ||
527 | } | ||
528 | else | ||
529 | { | ||
530 | ret = GetReferenceToMesh(prim, shapeCallback); | ||
531 | if (DDetail) DetailLog("{0},BSShapeCollection.CreateGeom,mesh,shape={1},key={2}", | ||
532 | prim.LocalID, prim.PhysShape, prim.PhysShape.shapeKey.ToString("X")); | ||
533 | } | ||
534 | return ret; | ||
535 | } | ||
536 | |||
537 | // Creates a native shape and assignes it to prim.BSShape. | ||
538 | // "Native" shapes are never shared. they are created here and destroyed in DereferenceShape(). | ||
539 | private bool GetReferenceToNativeShape(BSPhysObject prim, | ||
540 | BSPhysicsShapeType shapeType, FixedShapeKey shapeKey, | ||
541 | ShapeDestructionCallback shapeCallback) | ||
542 | { | ||
543 | // release any previous shape | ||
544 | DereferenceShape(prim.PhysShape, true, shapeCallback); | ||
545 | |||
546 | BulletShape newShape = BuildPhysicalNativeShape(prim, shapeType, shapeKey); | ||
547 | |||
548 | // Don't need to do a 'ReferenceShape()' here because native shapes are not shared. | ||
549 | if (DDetail) DetailLog("{0},BSShapeCollection.AddNativeShapeToPrim,create,newshape={1},scale={2}", | ||
550 | prim.LocalID, newShape, prim.Scale); | ||
551 | |||
552 | // native shapes are scaled by Bullet | ||
553 | prim.PhysShape = newShape; | ||
554 | return true; | ||
555 | } | ||
556 | |||
557 | private BulletShape BuildPhysicalNativeShape(BSPhysObject prim, BSPhysicsShapeType shapeType, | ||
558 | FixedShapeKey shapeKey) | ||
559 | { | ||
560 | BulletShape newShape; | ||
561 | // Need to make sure the passed shape information is for the native type. | ||
562 | ShapeData nativeShapeData = new ShapeData(); | ||
563 | nativeShapeData.Type = shapeType; | ||
564 | nativeShapeData.ID = prim.LocalID; | ||
565 | nativeShapeData.Scale = prim.Scale; | ||
566 | nativeShapeData.Size = prim.Scale; // unneeded, I think. | ||
567 | nativeShapeData.MeshKey = (ulong)shapeKey; | ||
568 | nativeShapeData.HullKey = (ulong)shapeKey; | ||
569 | |||
570 | if (shapeType == BSPhysicsShapeType.SHAPE_CAPSULE) | ||
571 | { | ||
572 | |||
573 | newShape = PhysicsScene.PE.BuildCapsuleShape(PhysicsScene.World, 1f, 1f, prim.Scale); | ||
574 | if (DDetail) DetailLog("{0},BSShapeCollection.BuiletPhysicalNativeShape,capsule,scale={1}", prim.LocalID, prim.Scale); | ||
575 | } | ||
576 | else | ||
577 | { | ||
578 | // Native shapes are scaled in Bullet so set the scaling to the size | ||
579 | newShape = PhysicsScene.PE.BuildNativeShape(PhysicsScene.World, nativeShapeData); | ||
580 | |||
581 | } | ||
582 | if (!newShape.HasPhysicalShape) | ||
583 | { | ||
584 | PhysicsScene.Logger.ErrorFormat("{0} BuildPhysicalNativeShape failed. ID={1}, shape={2}", | ||
585 | LogHeader, prim.LocalID, shapeType); | ||
586 | } | ||
587 | newShape.shapeKey = (System.UInt64)shapeKey; | ||
588 | newShape.isNativeShape = true; | ||
589 | |||
590 | return newShape; | ||
591 | } | ||
592 | |||
593 | // Builds a mesh shape in the physical world and updates prim.BSShape. | ||
594 | // Dereferences previous shape in BSShape and adds a reference for this new shape. | ||
595 | // Returns 'true' of a mesh was actually built. Otherwise . | ||
596 | // Called at taint-time! | ||
597 | private bool GetReferenceToMesh(BSPhysObject prim, ShapeDestructionCallback shapeCallback) | ||
598 | { | ||
599 | BulletShape newShape = new BulletShape(); | ||
600 | |||
601 | float lod; | ||
602 | System.UInt64 newMeshKey = ComputeShapeKey(prim.Size, prim.BaseShape, out lod); | ||
603 | |||
604 | // if this new shape is the same as last time, don't recreate the mesh | ||
605 | if (newMeshKey == prim.PhysShape.shapeKey && prim.PhysShape.type == BSPhysicsShapeType.SHAPE_MESH) | ||
606 | return false; | ||
607 | |||
608 | if (DDetail) DetailLog("{0},BSShapeCollection.GetReferenceToMesh,create,oldKey={1},newKey={2}", | ||
609 | prim.LocalID, prim.PhysShape.shapeKey.ToString("X"), newMeshKey.ToString("X")); | ||
610 | |||
611 | // Since we're recreating new, get rid of the reference to the previous shape | ||
612 | DereferenceShape(prim.PhysShape, true, shapeCallback); | ||
613 | |||
614 | newShape = CreatePhysicalMesh(prim.PhysObjectName, newMeshKey, prim.BaseShape, prim.Size, lod); | ||
615 | // Take evasive action if the mesh was not constructed. | ||
616 | newShape = VerifyMeshCreated(newShape, prim); | ||
617 | |||
618 | ReferenceShape(newShape); | ||
619 | |||
620 | prim.PhysShape = newShape; | ||
621 | |||
622 | return true; // 'true' means a new shape has been added to this prim | ||
623 | } | ||
624 | |||
625 | private BulletShape CreatePhysicalMesh(string objName, System.UInt64 newMeshKey, PrimitiveBaseShape pbs, OMV.Vector3 size, float lod) | ||
626 | { | ||
627 | BulletShape newShape = new BulletShape(); | ||
628 | IMesh meshData = null; | ||
629 | |||
630 | MeshDesc meshDesc; | ||
631 | if (Meshes.TryGetValue(newMeshKey, out meshDesc)) | ||
632 | { | ||
633 | // If the mesh has already been built just use it. | ||
634 | newShape = meshDesc.shape.Clone(); | ||
635 | } | ||
636 | else | ||
637 | { | ||
638 | meshData = PhysicsScene.mesher.CreateMesh(objName, pbs, size, lod, true, false); | ||
639 | |||
640 | if (meshData != null) | ||
641 | { | ||
642 | int[] indices = meshData.getIndexListAsInt(); | ||
643 | List<OMV.Vector3> vertices = meshData.getVertexList(); | ||
644 | |||
645 | float[] verticesAsFloats = new float[vertices.Count * 3]; | ||
646 | int vi = 0; | ||
647 | foreach (OMV.Vector3 vv in vertices) | ||
648 | { | ||
649 | verticesAsFloats[vi++] = vv.X; | ||
650 | verticesAsFloats[vi++] = vv.Y; | ||
651 | verticesAsFloats[vi++] = vv.Z; | ||
652 | } | ||
653 | |||
654 | // m_log.DebugFormat("{0}: BSShapeCollection.CreatePhysicalMesh: calling CreateMesh. lid={1}, key={2}, indices={3}, vertices={4}", | ||
655 | // LogHeader, prim.LocalID, newMeshKey, indices.Length, vertices.Count); | ||
656 | |||
657 | newShape = PhysicsScene.PE.CreateMeshShape(PhysicsScene.World, | ||
658 | indices.GetLength(0), indices, vertices.Count, verticesAsFloats); | ||
659 | } | ||
660 | } | ||
661 | newShape.shapeKey = newMeshKey; | ||
662 | |||
663 | return newShape; | ||
664 | } | ||
665 | |||
666 | // See that hull shape exists in the physical world and update prim.BSShape. | ||
667 | // We could be creating the hull because scale changed or whatever. | ||
668 | private bool GetReferenceToHull(BSPhysObject prim, ShapeDestructionCallback shapeCallback) | ||
669 | { | ||
670 | BulletShape newShape; | ||
671 | |||
672 | float lod; | ||
673 | System.UInt64 newHullKey = ComputeShapeKey(prim.Size, prim.BaseShape, out lod); | ||
674 | |||
675 | // if the hull hasn't changed, don't rebuild it | ||
676 | if (newHullKey == prim.PhysShape.shapeKey && prim.PhysShape.type == BSPhysicsShapeType.SHAPE_HULL) | ||
677 | return false; | ||
678 | |||
679 | if (DDetail) DetailLog("{0},BSShapeCollection.GetReferenceToHull,create,oldKey={1},newKey={2}", | ||
680 | prim.LocalID, prim.PhysShape.shapeKey.ToString("X"), newHullKey.ToString("X")); | ||
681 | |||
682 | // Remove usage of the previous shape. | ||
683 | DereferenceShape(prim.PhysShape, true, shapeCallback); | ||
684 | |||
685 | newShape = CreatePhysicalHull(prim.PhysObjectName, newHullKey, prim.BaseShape, prim.Size, lod); | ||
686 | newShape = VerifyMeshCreated(newShape, prim); | ||
687 | |||
688 | ReferenceShape(newShape); | ||
689 | |||
690 | prim.PhysShape = newShape; | ||
691 | return true; // 'true' means a new shape has been added to this prim | ||
692 | } | ||
693 | |||
694 | List<ConvexResult> m_hulls; | ||
695 | private BulletShape CreatePhysicalHull(string objName, System.UInt64 newHullKey, PrimitiveBaseShape pbs, OMV.Vector3 size, float lod) | ||
696 | { | ||
697 | |||
698 | BulletShape newShape = new BulletShape(); | ||
699 | IntPtr hullPtr = IntPtr.Zero; | ||
700 | |||
701 | HullDesc hullDesc; | ||
702 | if (Hulls.TryGetValue(newHullKey, out hullDesc)) | ||
703 | { | ||
704 | // If the hull shape already is created, just use it. | ||
705 | newShape = hullDesc.shape.Clone(); | ||
706 | } | ||
707 | else | ||
708 | { | ||
709 | // Build a new hull in the physical world | ||
710 | // Pass true for physicalness as this creates some sort of bounding box which we don't need | ||
711 | IMesh meshData = PhysicsScene.mesher.CreateMesh(objName, pbs, size, lod, true, false); | ||
712 | if (meshData != null) | ||
713 | { | ||
714 | |||
715 | int[] indices = meshData.getIndexListAsInt(); | ||
716 | List<OMV.Vector3> vertices = meshData.getVertexList(); | ||
717 | |||
718 | //format conversion from IMesh format to DecompDesc format | ||
719 | List<int> convIndices = new List<int>(); | ||
720 | List<float3> convVertices = new List<float3>(); | ||
721 | for (int ii = 0; ii < indices.GetLength(0); ii++) | ||
722 | { | ||
723 | convIndices.Add(indices[ii]); | ||
724 | } | ||
725 | foreach (OMV.Vector3 vv in vertices) | ||
726 | { | ||
727 | convVertices.Add(new float3(vv.X, vv.Y, vv.Z)); | ||
728 | } | ||
729 | |||
730 | // setup and do convex hull conversion | ||
731 | m_hulls = new List<ConvexResult>(); | ||
732 | DecompDesc dcomp = new DecompDesc(); | ||
733 | dcomp.mIndices = convIndices; | ||
734 | dcomp.mVertices = convVertices; | ||
735 | ConvexBuilder convexBuilder = new ConvexBuilder(HullReturn); | ||
736 | // create the hull into the _hulls variable | ||
737 | convexBuilder.process(dcomp); | ||
738 | |||
739 | // Convert the vertices and indices for passing to unmanaged. | ||
740 | // The hull information is passed as a large floating point array. | ||
741 | // The format is: | ||
742 | // convHulls[0] = number of hulls | ||
743 | // convHulls[1] = number of vertices in first hull | ||
744 | // convHulls[2] = hull centroid X coordinate | ||
745 | // convHulls[3] = hull centroid Y coordinate | ||
746 | // convHulls[4] = hull centroid Z coordinate | ||
747 | // convHulls[5] = first hull vertex X | ||
748 | // convHulls[6] = first hull vertex Y | ||
749 | // convHulls[7] = first hull vertex Z | ||
750 | // convHulls[8] = second hull vertex X | ||
751 | // ... | ||
752 | // convHulls[n] = number of vertices in second hull | ||
753 | // convHulls[n+1] = second hull centroid X coordinate | ||
754 | // ... | ||
755 | // | ||
756 | // TODO: is is very inefficient. Someday change the convex hull generator to return | ||
757 | // data structures that do not need to be converted in order to pass to Bullet. | ||
758 | // And maybe put the values directly into pinned memory rather than marshaling. | ||
759 | int hullCount = m_hulls.Count; | ||
760 | int totalVertices = 1; // include one for the count of the hulls | ||
761 | foreach (ConvexResult cr in m_hulls) | ||
762 | { | ||
763 | totalVertices += 4; // add four for the vertex count and centroid | ||
764 | totalVertices += cr.HullIndices.Count * 3; // we pass just triangles | ||
765 | } | ||
766 | float[] convHulls = new float[totalVertices]; | ||
767 | |||
768 | convHulls[0] = (float)hullCount; | ||
769 | int jj = 1; | ||
770 | foreach (ConvexResult cr in m_hulls) | ||
771 | { | ||
772 | // copy vertices for index access | ||
773 | float3[] verts = new float3[cr.HullVertices.Count]; | ||
774 | int kk = 0; | ||
775 | foreach (float3 ff in cr.HullVertices) | ||
776 | { | ||
777 | verts[kk++] = ff; | ||
778 | } | ||
779 | |||
780 | // add to the array one hull's worth of data | ||
781 | convHulls[jj++] = cr.HullIndices.Count; | ||
782 | convHulls[jj++] = 0f; // centroid x,y,z | ||
783 | convHulls[jj++] = 0f; | ||
784 | convHulls[jj++] = 0f; | ||
785 | foreach (int ind in cr.HullIndices) | ||
786 | { | ||
787 | convHulls[jj++] = verts[ind].x; | ||
788 | convHulls[jj++] = verts[ind].y; | ||
789 | convHulls[jj++] = verts[ind].z; | ||
790 | } | ||
791 | } | ||
792 | // create the hull data structure in Bullet | ||
793 | newShape = PhysicsScene.PE.CreateHullShape(PhysicsScene.World, hullCount, convHulls); | ||
794 | } | ||
795 | } | ||
796 | |||
797 | newShape.shapeKey = newHullKey; | ||
798 | |||
799 | return newShape; | ||
800 | } | ||
801 | |||
802 | // Callback from convex hull creater with a newly created hull. | ||
803 | // Just add it to our collection of hulls for this shape. | ||
804 | private void HullReturn(ConvexResult result) | ||
805 | { | ||
806 | m_hulls.Add(result); | ||
807 | return; | ||
808 | } | ||
809 | |||
810 | // Compound shapes are always built from scratch. | ||
811 | // This shouldn't be to bad since most of the parts will be meshes that had been built previously. | ||
812 | private bool GetReferenceToCompoundShape(BSPhysObject prim, ShapeDestructionCallback shapeCallback) | ||
813 | { | ||
814 | // Remove reference to the old shape | ||
815 | // Don't need to do this as the shape is freed when the new root shape is created below. | ||
816 | // DereferenceShape(prim.PhysShape, true, shapeCallback); | ||
817 | |||
818 | |||
819 | BulletShape cShape = PhysicsScene.PE.CreateCompoundShape(PhysicsScene.World, false); | ||
820 | |||
821 | // Create the shape for the root prim and add it to the compound shape. Cannot be a native shape. | ||
822 | CreateGeomMeshOrHull(prim, shapeCallback); | ||
823 | PhysicsScene.PE.AddChildShapeToCompoundShape(cShape, prim.PhysShape, OMV.Vector3.Zero, OMV.Quaternion.Identity); | ||
824 | if (DDetail) DetailLog("{0},BSShapeCollection.GetReferenceToCompoundShape,addRootPrim,compShape={1},rootShape={2}", | ||
825 | prim.LocalID, cShape, prim.PhysShape); | ||
826 | |||
827 | prim.PhysShape = cShape; | ||
828 | |||
829 | return true; | ||
830 | } | ||
831 | |||
832 | // Create a hash of all the shape parameters to be used as a key | ||
833 | // for this particular shape. | ||
834 | private System.UInt64 ComputeShapeKey(OMV.Vector3 size, PrimitiveBaseShape pbs, out float retLod) | ||
835 | { | ||
836 | // level of detail based on size and type of the object | ||
837 | float lod = BSParam.MeshLOD; | ||
838 | if (pbs.SculptEntry) | ||
839 | lod = BSParam.SculptLOD; | ||
840 | |||
841 | // Mega prims usually get more detail because one can interact with shape approximations at this size. | ||
842 | float maxAxis = Math.Max(size.X, Math.Max(size.Y, size.Z)); | ||
843 | if (maxAxis > BSParam.MeshMegaPrimThreshold) | ||
844 | lod = BSParam.MeshMegaPrimLOD; | ||
845 | |||
846 | retLod = lod; | ||
847 | return pbs.GetMeshKey(size, lod); | ||
848 | } | ||
849 | // For those who don't want the LOD | ||
850 | private System.UInt64 ComputeShapeKey(OMV.Vector3 size, PrimitiveBaseShape pbs) | ||
851 | { | ||
852 | float lod; | ||
853 | return ComputeShapeKey(size, pbs, out lod); | ||
854 | } | ||
855 | |||
856 | // The creation of a mesh or hull can fail if an underlying asset is not available. | ||
857 | // There are two cases: 1) the asset is not in the cache and it needs to be fetched; | ||
858 | // and 2) the asset cannot be converted (like failed decompression of JPEG2000s). | ||
859 | // The first case causes the asset to be fetched. The second case requires | ||
860 | // us to not loop forever. | ||
861 | // Called after creating a physical mesh or hull. If the physical shape was created, | ||
862 | // just return. | ||
863 | private BulletShape VerifyMeshCreated(BulletShape newShape, BSPhysObject prim) | ||
864 | { | ||
865 | // If the shape was successfully created, nothing more to do | ||
866 | if (newShape.HasPhysicalShape) | ||
867 | return newShape; | ||
868 | |||
869 | // If this mesh has an underlying asset and we have not failed getting it before, fetch the asset | ||
870 | if (prim.BaseShape.SculptEntry && !prim.LastAssetBuildFailed && prim.BaseShape.SculptTexture != OMV.UUID.Zero) | ||
871 | { | ||
872 | prim.LastAssetBuildFailed = true; | ||
873 | BSPhysObject xprim = prim; | ||
874 | DetailLog("{0},BSShapeCollection.VerifyMeshCreated,fetchAsset,lID={1},lastFailed={2}", | ||
875 | LogHeader, prim.LocalID, prim.LastAssetBuildFailed); | ||
876 | Util.FireAndForget(delegate | ||
877 | { | ||
878 | RequestAssetDelegate assetProvider = PhysicsScene.RequestAssetMethod; | ||
879 | if (assetProvider != null) | ||
880 | { | ||
881 | BSPhysObject yprim = xprim; // probably not necessary, but, just in case. | ||
882 | assetProvider(yprim.BaseShape.SculptTexture, delegate(AssetBase asset) | ||
883 | { | ||
884 | if (!yprim.BaseShape.SculptEntry) | ||
885 | return; | ||
886 | if (yprim.BaseShape.SculptTexture.ToString() != asset.ID) | ||
887 | return; | ||
888 | |||
889 | yprim.BaseShape.SculptData = asset.Data; | ||
890 | // This will cause the prim to see that the filler shape is not the right | ||
891 | // one and try again to build the object. | ||
892 | // No race condition with the normal shape setting since the rebuild is at taint time. | ||
893 | yprim.ForceBodyShapeRebuild(false); | ||
894 | |||
895 | }); | ||
896 | } | ||
897 | }); | ||
898 | } | ||
899 | else | ||
900 | { | ||
901 | if (prim.LastAssetBuildFailed) | ||
902 | { | ||
903 | PhysicsScene.Logger.ErrorFormat("{0} Mesh failed to fetch asset. lID={1}, texture={2}", | ||
904 | LogHeader, prim.LocalID, prim.BaseShape.SculptTexture); | ||
905 | } | ||
906 | } | ||
907 | |||
908 | // While we figure out the real problem, stick a simple native shape on the object. | ||
909 | BulletShape fillinShape = | ||
910 | BuildPhysicalNativeShape(prim, BSPhysicsShapeType.SHAPE_BOX, FixedShapeKey.KEY_BOX); | ||
911 | |||
912 | return fillinShape; | ||
913 | } | ||
914 | |||
915 | // Create a body object in Bullet. | ||
916 | // Updates prim.BSBody with the information about the new body if one is created. | ||
917 | // Returns 'true' if an object was actually created. | ||
918 | // Called at taint-time. | ||
919 | private bool CreateBody(bool forceRebuild, BSPhysObject prim, BulletWorld sim, BulletShape shape, | ||
920 | BodyDestructionCallback bodyCallback) | ||
921 | { | ||
922 | bool ret = false; | ||
923 | |||
924 | // the mesh, hull or native shape must have already been created in Bullet | ||
925 | bool mustRebuild = !prim.PhysBody.HasPhysicalBody; | ||
926 | |||
927 | // If there is an existing body, verify it's of an acceptable type. | ||
928 | // If not a solid object, body is a GhostObject. Otherwise a RigidBody. | ||
929 | if (!mustRebuild) | ||
930 | { | ||
931 | CollisionObjectTypes bodyType = (CollisionObjectTypes)PhysicsScene.PE.GetBodyType(prim.PhysBody); | ||
932 | if (prim.IsSolid && bodyType != CollisionObjectTypes.CO_RIGID_BODY | ||
933 | || !prim.IsSolid && bodyType != CollisionObjectTypes.CO_GHOST_OBJECT) | ||
934 | { | ||
935 | // If the collisionObject is not the correct type for solidness, rebuild what's there | ||
936 | mustRebuild = true; | ||
937 | } | ||
938 | } | ||
939 | |||
940 | if (mustRebuild || forceRebuild) | ||
941 | { | ||
942 | // Free any old body | ||
943 | DereferenceBody(prim.PhysBody, true, bodyCallback); | ||
944 | |||
945 | BulletBody aBody; | ||
946 | if (prim.IsSolid) | ||
947 | { | ||
948 | aBody = PhysicsScene.PE.CreateBodyFromShape(sim, shape, prim.LocalID, prim.RawPosition, prim.RawOrientation); | ||
949 | if (DDetail) DetailLog("{0},BSShapeCollection.CreateBody,mesh,body={1}", prim.LocalID, aBody); | ||
950 | } | ||
951 | else | ||
952 | { | ||
953 | aBody = PhysicsScene.PE.CreateGhostFromShape(sim, shape, prim.LocalID, prim.RawPosition, prim.RawOrientation); | ||
954 | if (DDetail) DetailLog("{0},BSShapeCollection.CreateBody,ghost,body={1}", prim.LocalID, aBody); | ||
955 | } | ||
956 | |||
957 | ReferenceBody(aBody, true); | ||
958 | |||
959 | prim.PhysBody = aBody; | ||
960 | |||
961 | ret = true; | ||
962 | } | ||
963 | |||
964 | return ret; | ||
965 | } | ||
966 | |||
967 | private bool TryGetMeshByPtr(BulletShape shape, out MeshDesc outDesc) | ||
968 | { | ||
969 | bool ret = false; | ||
970 | MeshDesc foundDesc = new MeshDesc(); | ||
971 | foreach (MeshDesc md in Meshes.Values) | ||
972 | { | ||
973 | if (md.shape.ReferenceSame(shape)) | ||
974 | { | ||
975 | foundDesc = md; | ||
976 | ret = true; | ||
977 | break; | ||
978 | } | ||
979 | |||
980 | } | ||
981 | outDesc = foundDesc; | ||
982 | return ret; | ||
983 | } | ||
984 | |||
985 | private bool TryGetHullByPtr(BulletShape shape, out HullDesc outDesc) | ||
986 | { | ||
987 | bool ret = false; | ||
988 | HullDesc foundDesc = new HullDesc(); | ||
989 | foreach (HullDesc hd in Hulls.Values) | ||
990 | { | ||
991 | if (hd.shape.ReferenceSame(shape)) | ||
992 | { | ||
993 | foundDesc = hd; | ||
994 | ret = true; | ||
995 | break; | ||
996 | } | ||
997 | |||
998 | } | ||
999 | outDesc = foundDesc; | ||
1000 | return ret; | ||
1001 | } | ||
1002 | |||
1003 | private void DetailLog(string msg, params Object[] args) | ||
1004 | { | ||
1005 | if (PhysicsScene.PhysicsLogging.Enabled) | ||
1006 | PhysicsScene.DetailLog(msg, args); | ||
1007 | } | ||
1008 | } | ||
1009 | } | ||