1
0

BSShapes.cs 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  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 OpenSim.Framework;
  31. using OpenSim.Region.Physics.Manager;
  32. using OpenSim.Region.Physics.Meshing;
  33. using OpenSim.Region.Physics.ConvexDecompositionDotNet;
  34. using OMV = OpenMetaverse;
  35. namespace OpenSim.Region.Physics.BulletSPlugin
  36. {
  37. public abstract class BSShape
  38. {
  39. private static string LogHeader = "[BULLETSIM SHAPE]";
  40. public int referenceCount { get; set; }
  41. public DateTime lastReferenced { get; set; }
  42. public BulletShape physShapeInfo { get; set; }
  43. public BSShape()
  44. {
  45. referenceCount = 1;
  46. lastReferenced = DateTime.Now;
  47. physShapeInfo = new BulletShape();
  48. }
  49. public BSShape(BulletShape pShape)
  50. {
  51. referenceCount = 1;
  52. lastReferenced = DateTime.Now;
  53. physShapeInfo = pShape;
  54. }
  55. // Get another reference to this shape.
  56. public abstract BSShape GetReference(BSScene pPhysicsScene, BSPhysObject pPrim);
  57. // Called when this shape is being used again.
  58. // Used internally. External callers should call instance.GetReference() to properly copy/reference
  59. // the shape.
  60. protected virtual void IncrementReference()
  61. {
  62. referenceCount++;
  63. lastReferenced = DateTime.Now;
  64. }
  65. // Called when this shape is done being used.
  66. protected virtual void DecrementReference()
  67. {
  68. referenceCount--;
  69. lastReferenced = DateTime.Now;
  70. }
  71. // Release the use of a physical shape.
  72. public abstract void Dereference(BSScene physicsScene);
  73. // Return 'true' if there is an allocated physics physical shape under this class instance.
  74. public virtual bool HasPhysicalShape
  75. {
  76. get
  77. {
  78. if (physShapeInfo != null)
  79. return physShapeInfo.HasPhysicalShape;
  80. return false;
  81. }
  82. }
  83. public virtual BSPhysicsShapeType ShapeType
  84. {
  85. get
  86. {
  87. BSPhysicsShapeType ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
  88. if (physShapeInfo != null && physShapeInfo.HasPhysicalShape)
  89. ret = physShapeInfo.shapeType;
  90. return ret;
  91. }
  92. }
  93. // Returns a string for debugging that uniquily identifies the memory used by this instance
  94. public virtual string AddrString
  95. {
  96. get
  97. {
  98. if (physShapeInfo != null)
  99. return physShapeInfo.AddrString;
  100. return "unknown";
  101. }
  102. }
  103. public override string ToString()
  104. {
  105. StringBuilder buff = new StringBuilder();
  106. if (physShapeInfo == null)
  107. {
  108. buff.Append("<noPhys");
  109. }
  110. else
  111. {
  112. buff.Append("<phy=");
  113. buff.Append(physShapeInfo.ToString());
  114. }
  115. buff.Append(",c=");
  116. buff.Append(referenceCount.ToString());
  117. buff.Append(">");
  118. return buff.ToString();
  119. }
  120. #region Common shape routines
  121. // Create a hash of all the shape parameters to be used as a key for this particular shape.
  122. public static System.UInt64 ComputeShapeKey(OMV.Vector3 size, PrimitiveBaseShape pbs, out float retLod)
  123. {
  124. // level of detail based on size and type of the object
  125. float lod = BSParam.MeshLOD;
  126. if (pbs.SculptEntry)
  127. lod = BSParam.SculptLOD;
  128. // Mega prims usually get more detail because one can interact with shape approximations at this size.
  129. float maxAxis = Math.Max(size.X, Math.Max(size.Y, size.Z));
  130. if (maxAxis > BSParam.MeshMegaPrimThreshold)
  131. lod = BSParam.MeshMegaPrimLOD;
  132. retLod = lod;
  133. return pbs.GetMeshKey(size, lod);
  134. }
  135. // The creation of a mesh or hull can fail if an underlying asset is not available.
  136. // There are two cases: 1) the asset is not in the cache and it needs to be fetched;
  137. // and 2) the asset cannot be converted (like failed decompression of JPEG2000s).
  138. // The first case causes the asset to be fetched. The second case requires
  139. // us to not loop forever.
  140. // Called after creating a physical mesh or hull. If the physical shape was created,
  141. // just return.
  142. public static BulletShape VerifyMeshCreated(BSScene physicsScene, BulletShape newShape, BSPhysObject prim)
  143. {
  144. // If the shape was successfully created, nothing more to do
  145. if (newShape.HasPhysicalShape)
  146. return newShape;
  147. // VerifyMeshCreated is called after trying to create the mesh. If we think the asset had been
  148. // fetched but we end up here again, the meshing of the asset must have failed.
  149. // Prevent trying to keep fetching the mesh by declaring failure.
  150. if (prim.PrimAssetState == BSPhysObject.PrimAssetCondition.Fetched)
  151. {
  152. prim.PrimAssetState = BSPhysObject.PrimAssetCondition.FailedMeshing;
  153. physicsScene.Logger.WarnFormat("{0} Fetched asset would not mesh. prim={1}, texture={2}",
  154. LogHeader, UsefulPrimInfo(physicsScene, prim), prim.BaseShape.SculptTexture);
  155. physicsScene.DetailLog("{0},BSShape.VerifyMeshCreated,setFailed,prim={1},tex={2}",
  156. prim.LocalID, UsefulPrimInfo(physicsScene, prim), prim.BaseShape.SculptTexture);
  157. }
  158. else
  159. {
  160. // If this mesh has an underlying asset and we have not failed getting it before, fetch the asset
  161. if (prim.BaseShape.SculptEntry
  162. && prim.PrimAssetState != BSPhysObject.PrimAssetCondition.FailedAssetFetch
  163. && prim.PrimAssetState != BSPhysObject.PrimAssetCondition.FailedMeshing
  164. && prim.PrimAssetState != BSPhysObject.PrimAssetCondition.Waiting
  165. && prim.BaseShape.SculptTexture != OMV.UUID.Zero
  166. )
  167. {
  168. physicsScene.DetailLog("{0},BSShape.VerifyMeshCreated,fetchAsset,objNam={1},tex={2}",
  169. prim.LocalID, prim.PhysObjectName, prim.BaseShape.SculptTexture);
  170. // Multiple requestors will know we're waiting for this asset
  171. prim.PrimAssetState = BSPhysObject.PrimAssetCondition.Waiting;
  172. BSPhysObject xprim = prim;
  173. Util.FireAndForget(delegate
  174. {
  175. // physicsScene.DetailLog("{0},BSShape.VerifyMeshCreated,inFireAndForget", xprim.LocalID);
  176. RequestAssetDelegate assetProvider = physicsScene.RequestAssetMethod;
  177. if (assetProvider != null)
  178. {
  179. BSPhysObject yprim = xprim; // probably not necessary, but, just in case.
  180. assetProvider(yprim.BaseShape.SculptTexture, delegate(AssetBase asset)
  181. {
  182. // physicsScene.DetailLog("{0},BSShape.VerifyMeshCreated,assetProviderCallback", xprim.LocalID);
  183. bool assetFound = false;
  184. string mismatchIDs = String.Empty; // DEBUG DEBUG
  185. if (asset != null && yprim.BaseShape.SculptEntry)
  186. {
  187. if (yprim.BaseShape.SculptTexture.ToString() == asset.ID)
  188. {
  189. yprim.BaseShape.SculptData = asset.Data;
  190. // This will cause the prim to see that the filler shape is not the right
  191. // one and try again to build the object.
  192. // No race condition with the normal shape setting since the rebuild is at taint time.
  193. yprim.PrimAssetState = BSPhysObject.PrimAssetCondition.Fetched;
  194. yprim.ForceBodyShapeRebuild(false /* inTaintTime */);
  195. assetFound = true;
  196. }
  197. else
  198. {
  199. mismatchIDs = yprim.BaseShape.SculptTexture.ToString() + "/" + asset.ID;
  200. }
  201. }
  202. if (!assetFound)
  203. {
  204. yprim.PrimAssetState = BSPhysObject.PrimAssetCondition.FailedAssetFetch;
  205. }
  206. physicsScene.DetailLog("{0},BSShape.VerifyMeshCreated,fetchAssetCallback,found={1},isSculpt={2},ids={3}",
  207. yprim.LocalID, assetFound, yprim.BaseShape.SculptEntry, mismatchIDs );
  208. });
  209. }
  210. else
  211. {
  212. xprim.PrimAssetState = BSPhysObject.PrimAssetCondition.FailedAssetFetch;
  213. physicsScene.Logger.ErrorFormat("{0} Physical object requires asset but no asset provider. Name={1}",
  214. LogHeader, physicsScene.Name);
  215. }
  216. });
  217. }
  218. else
  219. {
  220. if (prim.PrimAssetState == BSPhysObject.PrimAssetCondition.FailedAssetFetch)
  221. {
  222. physicsScene.Logger.WarnFormat("{0} Mesh failed to fetch asset. prim={1}, texture={2}",
  223. LogHeader, UsefulPrimInfo(physicsScene, prim), prim.BaseShape.SculptTexture);
  224. physicsScene.DetailLog("{0},BSShape.VerifyMeshCreated,wasFailed,prim={1},tex={2}",
  225. prim.LocalID, UsefulPrimInfo(physicsScene, prim), prim.BaseShape.SculptTexture);
  226. }
  227. if (prim.PrimAssetState == BSPhysObject.PrimAssetCondition.FailedMeshing)
  228. {
  229. physicsScene.Logger.WarnFormat("{0} Mesh asset would not mesh. prim={1}, texture={2}",
  230. LogHeader, UsefulPrimInfo(physicsScene, prim), prim.BaseShape.SculptTexture);
  231. physicsScene.DetailLog("{0},BSShape.VerifyMeshCreated,wasFailedMeshing,prim={1},tex={2}",
  232. prim.LocalID, UsefulPrimInfo(physicsScene, prim), prim.BaseShape.SculptTexture);
  233. }
  234. }
  235. }
  236. // While we wait for the mesh defining asset to be loaded, stick in a simple box for the object.
  237. BSShape fillShape = BSShapeNative.GetReference(physicsScene, prim, BSPhysicsShapeType.SHAPE_BOX, FixedShapeKey.KEY_BOX);
  238. physicsScene.DetailLog("{0},BSShape.VerifyMeshCreated,boxTempShape", prim.LocalID);
  239. return fillShape.physShapeInfo;
  240. }
  241. public static String UsefulPrimInfo(BSScene pScene, BSPhysObject prim)
  242. {
  243. StringBuilder buff = new StringBuilder(prim.PhysObjectName);
  244. buff.Append("/pos=");
  245. buff.Append(prim.RawPosition.ToString());
  246. if (pScene != null)
  247. {
  248. buff.Append("/rgn=");
  249. buff.Append(pScene.Name);
  250. }
  251. return buff.ToString();
  252. }
  253. #endregion // Common shape routines
  254. }
  255. // ============================================================================================================
  256. public class BSShapeNull : BSShape
  257. {
  258. public BSShapeNull() : base()
  259. {
  260. }
  261. public static BSShape GetReference() { return new BSShapeNull(); }
  262. public override BSShape GetReference(BSScene pPhysicsScene, BSPhysObject pPrim) { return new BSShapeNull(); }
  263. public override void Dereference(BSScene physicsScene) { /* The magic of garbage collection will make this go away */ }
  264. }
  265. // ============================================================================================================
  266. public class BSShapeNative : BSShape
  267. {
  268. private static string LogHeader = "[BULLETSIM SHAPE NATIVE]";
  269. public BSShapeNative(BulletShape pShape) : base(pShape)
  270. {
  271. }
  272. public static BSShape GetReference(BSScene physicsScene, BSPhysObject prim,
  273. BSPhysicsShapeType shapeType, FixedShapeKey shapeKey)
  274. {
  275. // Native shapes are not shared and are always built anew.
  276. return new BSShapeNative(CreatePhysicalNativeShape(physicsScene, prim, shapeType, shapeKey));
  277. }
  278. public override BSShape GetReference(BSScene pPhysicsScene, BSPhysObject pPrim)
  279. {
  280. // Native shapes are not shared so we return a new shape.
  281. BSShape ret = null;
  282. lock (physShapeInfo)
  283. {
  284. ret = new BSShapeNative(CreatePhysicalNativeShape(pPhysicsScene, pPrim,
  285. physShapeInfo.shapeType, (FixedShapeKey)physShapeInfo.shapeKey));
  286. }
  287. return ret;
  288. }
  289. // Make this reference to the physical shape go away since native shapes are not shared.
  290. public override void Dereference(BSScene physicsScene)
  291. {
  292. // Native shapes are not tracked and are released immediately
  293. lock (physShapeInfo)
  294. {
  295. if (physShapeInfo.HasPhysicalShape)
  296. {
  297. physicsScene.DetailLog("{0},BSShapeNative.Dereference,deleteNativeShape,shape={1}", BSScene.DetailLogZero, this);
  298. physicsScene.PE.DeleteCollisionShape(physicsScene.World, physShapeInfo);
  299. }
  300. physShapeInfo.Clear();
  301. // Garbage collection will free up this instance.
  302. }
  303. }
  304. private static BulletShape CreatePhysicalNativeShape(BSScene physicsScene, BSPhysObject prim,
  305. BSPhysicsShapeType shapeType, FixedShapeKey shapeKey)
  306. {
  307. BulletShape newShape;
  308. ShapeData nativeShapeData = new ShapeData();
  309. nativeShapeData.Type = shapeType;
  310. nativeShapeData.ID = prim.LocalID;
  311. nativeShapeData.Scale = prim.Scale;
  312. nativeShapeData.Size = prim.Scale;
  313. nativeShapeData.MeshKey = (ulong)shapeKey;
  314. nativeShapeData.HullKey = (ulong)shapeKey;
  315. if (shapeType == BSPhysicsShapeType.SHAPE_CAPSULE)
  316. {
  317. newShape = physicsScene.PE.BuildCapsuleShape(physicsScene.World, 1f, 1f, prim.Scale);
  318. physicsScene.DetailLog("{0},BSShapeNative,capsule,scale={1}", prim.LocalID, prim.Scale);
  319. }
  320. else
  321. {
  322. newShape = physicsScene.PE.BuildNativeShape(physicsScene.World, nativeShapeData);
  323. }
  324. if (!newShape.HasPhysicalShape)
  325. {
  326. physicsScene.Logger.ErrorFormat("{0} BuildPhysicalNativeShape failed. ID={1}, shape={2}",
  327. LogHeader, prim.LocalID, shapeType);
  328. }
  329. newShape.shapeType = shapeType;
  330. newShape.isNativeShape = true;
  331. newShape.shapeKey = (UInt64)shapeKey;
  332. return newShape;
  333. }
  334. }
  335. // ============================================================================================================
  336. public class BSShapeMesh : BSShape
  337. {
  338. private static string LogHeader = "[BULLETSIM SHAPE MESH]";
  339. public static Dictionary<System.UInt64, BSShapeMesh> Meshes = new Dictionary<System.UInt64, BSShapeMesh>();
  340. public BSShapeMesh(BulletShape pShape) : base(pShape)
  341. {
  342. }
  343. public static BSShape GetReference(BSScene physicsScene, bool forceRebuild, BSPhysObject prim)
  344. {
  345. float lod;
  346. System.UInt64 newMeshKey = BSShape.ComputeShapeKey(prim.Size, prim.BaseShape, out lod);
  347. BSShapeMesh retMesh = null;
  348. lock (Meshes)
  349. {
  350. if (Meshes.TryGetValue(newMeshKey, out retMesh))
  351. {
  352. // The mesh has already been created. Return a new reference to same.
  353. retMesh.IncrementReference();
  354. }
  355. else
  356. {
  357. retMesh = new BSShapeMesh(new BulletShape());
  358. // An instance of this mesh has not been created. Build and remember same.
  359. BulletShape newShape = retMesh.CreatePhysicalMesh(physicsScene, prim, newMeshKey, prim.BaseShape, prim.Size, lod);
  360. // Check to see if mesh was created (might require an asset).
  361. newShape = VerifyMeshCreated(physicsScene, newShape, prim);
  362. if (!newShape.isNativeShape
  363. || prim.PrimAssetState == BSPhysObject.PrimAssetCondition.FailedMeshing
  364. || prim.PrimAssetState == BSPhysObject.PrimAssetCondition.FailedAssetFetch)
  365. {
  366. // If a mesh was what was created, remember the built shape for later sharing.
  367. // Also note that if meshing failed we put it in the mesh list as there is nothing else to do about the mesh.
  368. Meshes.Add(newMeshKey, retMesh);
  369. }
  370. retMesh.physShapeInfo = newShape;
  371. }
  372. }
  373. physicsScene.DetailLog("{0},BSShapeMesh,getReference,mesh={1},size={2},lod={3}", prim.LocalID, retMesh, prim.Size, lod);
  374. return retMesh;
  375. }
  376. public override BSShape GetReference(BSScene pPhysicsScene, BSPhysObject pPrim)
  377. {
  378. BSShape ret = null;
  379. // If the underlying shape is native, the actual shape has not been build (waiting for asset)
  380. // and we must create a copy of the native shape since they are never shared.
  381. if (physShapeInfo.HasPhysicalShape && physShapeInfo.isNativeShape)
  382. {
  383. // TODO: decide when the native shapes should be freed. Check in Dereference?
  384. ret = BSShapeNative.GetReference(pPhysicsScene, pPrim, BSPhysicsShapeType.SHAPE_BOX, FixedShapeKey.KEY_BOX);
  385. }
  386. else
  387. {
  388. // Another reference to this shape is just counted.
  389. IncrementReference();
  390. ret = this;
  391. }
  392. return ret;
  393. }
  394. public override void Dereference(BSScene physicsScene)
  395. {
  396. lock (Meshes)
  397. {
  398. this.DecrementReference();
  399. physicsScene.DetailLog("{0},BSShapeMesh.Dereference,shape={1}", BSScene.DetailLogZero, this);
  400. // TODO: schedule aging and destruction of unused meshes.
  401. }
  402. }
  403. // Loop through all the known meshes and return the description based on the physical address.
  404. public static bool TryGetMeshByPtr(BulletShape pShape, out BSShapeMesh outMesh)
  405. {
  406. bool ret = false;
  407. BSShapeMesh foundDesc = null;
  408. lock (Meshes)
  409. {
  410. foreach (BSShapeMesh sm in Meshes.Values)
  411. {
  412. if (sm.physShapeInfo.ReferenceSame(pShape))
  413. {
  414. foundDesc = sm;
  415. ret = true;
  416. break;
  417. }
  418. }
  419. }
  420. outMesh = foundDesc;
  421. return ret;
  422. }
  423. public delegate BulletShape CreateShapeCall(BulletWorld world, int indicesCount, int[] indices, int verticesCount, float[] vertices );
  424. private BulletShape CreatePhysicalMesh(BSScene physicsScene, BSPhysObject prim, System.UInt64 newMeshKey,
  425. PrimitiveBaseShape pbs, OMV.Vector3 size, float lod)
  426. {
  427. return BSShapeMesh.CreatePhysicalMeshShape(physicsScene, prim, newMeshKey, pbs, size, lod,
  428. (w, iC, i, vC, v) => physicsScene.PE.CreateMeshShape(w, iC, i, vC, v) );
  429. }
  430. // Code that uses the mesher to create the index/vertices info for a trimesh shape.
  431. // This is used by the passed 'makeShape' call to create the Bullet mesh shape.
  432. // The actual build call is passed so this logic can be used by several of the shapes that use a
  433. // simple mesh as their base shape.
  434. public static BulletShape CreatePhysicalMeshShape(BSScene physicsScene, BSPhysObject prim, System.UInt64 newMeshKey,
  435. PrimitiveBaseShape pbs, OMV.Vector3 size, float lod, CreateShapeCall makeShape)
  436. {
  437. BulletShape newShape = new BulletShape();
  438. IMesh meshData = null;
  439. lock (physicsScene.mesher)
  440. {
  441. meshData = physicsScene.mesher.CreateMesh(prim.PhysObjectName, pbs, size, lod,
  442. false, // say it is not physical so a bounding box is not built
  443. false // do not cache the mesh and do not use previously built versions
  444. );
  445. }
  446. if (meshData != null)
  447. {
  448. if (prim.PrimAssetState == BSPhysObject.PrimAssetCondition.Fetched)
  449. {
  450. // Release the fetched asset data once it has been used.
  451. pbs.SculptData = new byte[0];
  452. prim.PrimAssetState = BSPhysObject.PrimAssetCondition.Unknown;
  453. }
  454. int[] indices = meshData.getIndexListAsInt();
  455. int realIndicesIndex = indices.Length;
  456. float[] verticesAsFloats = meshData.getVertexListAsFloat();
  457. if (BSParam.ShouldRemoveZeroWidthTriangles)
  458. {
  459. // Remove degenerate triangles. These are triangles with two of the vertices
  460. // are the same. This is complicated by the problem that vertices are not
  461. // made unique in sculpties so we have to compare the values in the vertex.
  462. realIndicesIndex = 0;
  463. for (int tri = 0; tri < indices.Length; tri += 3)
  464. {
  465. // Compute displacements into vertex array for each vertex of the triangle
  466. int v1 = indices[tri + 0] * 3;
  467. int v2 = indices[tri + 1] * 3;
  468. int v3 = indices[tri + 2] * 3;
  469. // Check to see if any two of the vertices are the same
  470. if (!( ( verticesAsFloats[v1 + 0] == verticesAsFloats[v2 + 0]
  471. && verticesAsFloats[v1 + 1] == verticesAsFloats[v2 + 1]
  472. && verticesAsFloats[v1 + 2] == verticesAsFloats[v2 + 2])
  473. || ( verticesAsFloats[v2 + 0] == verticesAsFloats[v3 + 0]
  474. && verticesAsFloats[v2 + 1] == verticesAsFloats[v3 + 1]
  475. && verticesAsFloats[v2 + 2] == verticesAsFloats[v3 + 2])
  476. || ( verticesAsFloats[v1 + 0] == verticesAsFloats[v3 + 0]
  477. && verticesAsFloats[v1 + 1] == verticesAsFloats[v3 + 1]
  478. && verticesAsFloats[v1 + 2] == verticesAsFloats[v3 + 2]) )
  479. )
  480. {
  481. // None of the vertices of the triangles are the same. This is a good triangle;
  482. indices[realIndicesIndex + 0] = indices[tri + 0];
  483. indices[realIndicesIndex + 1] = indices[tri + 1];
  484. indices[realIndicesIndex + 2] = indices[tri + 2];
  485. realIndicesIndex += 3;
  486. }
  487. }
  488. }
  489. physicsScene.DetailLog("{0},BSShapeMesh.CreatePhysicalMesh,key={1},origTri={2},realTri={3},numVerts={4}",
  490. BSScene.DetailLogZero, newMeshKey.ToString("X"), indices.Length / 3, realIndicesIndex / 3, verticesAsFloats.Length / 3);
  491. if (realIndicesIndex != 0)
  492. {
  493. newShape = makeShape(physicsScene.World, realIndicesIndex, indices, verticesAsFloats.Length / 3, verticesAsFloats);
  494. }
  495. else
  496. {
  497. // Force the asset condition to 'failed' so we won't try to keep fetching and processing this mesh.
  498. prim.PrimAssetState = BSPhysObject.PrimAssetCondition.FailedMeshing;
  499. physicsScene.Logger.DebugFormat("{0} All mesh triangles degenerate. Prim={1}", LogHeader, UsefulPrimInfo(physicsScene, prim) );
  500. physicsScene.DetailLog("{0},BSShapeMesh.CreatePhysicalMesh,allDegenerate,key={1}", prim.LocalID, newMeshKey);
  501. }
  502. }
  503. newShape.shapeKey = newMeshKey;
  504. return newShape;
  505. }
  506. }
  507. // ============================================================================================================
  508. public class BSShapeHull : BSShape
  509. {
  510. private static string LogHeader = "[BULLETSIM SHAPE HULL]";
  511. public static Dictionary<System.UInt64, BSShapeHull> Hulls = new Dictionary<System.UInt64, BSShapeHull>();
  512. public BSShapeHull(BulletShape pShape) : base(pShape)
  513. {
  514. }
  515. public static BSShape GetReference(BSScene physicsScene, bool forceRebuild, BSPhysObject prim)
  516. {
  517. float lod;
  518. System.UInt64 newHullKey = BSShape.ComputeShapeKey(prim.Size, prim.BaseShape, out lod);
  519. BSShapeHull retHull = null;
  520. lock (Hulls)
  521. {
  522. if (Hulls.TryGetValue(newHullKey, out retHull))
  523. {
  524. // The mesh has already been created. Return a new reference to same.
  525. retHull.IncrementReference();
  526. }
  527. else
  528. {
  529. retHull = new BSShapeHull(new BulletShape());
  530. // An instance of this mesh has not been created. Build and remember same.
  531. BulletShape newShape = retHull.CreatePhysicalHull(physicsScene, prim, newHullKey, prim.BaseShape, prim.Size, lod);
  532. // Check to see if hull was created (might require an asset).
  533. newShape = VerifyMeshCreated(physicsScene, newShape, prim);
  534. if (!newShape.isNativeShape
  535. || prim.PrimAssetState == BSPhysObject.PrimAssetCondition.FailedMeshing
  536. || prim.PrimAssetState == BSPhysObject.PrimAssetCondition.FailedAssetFetch)
  537. {
  538. // If a mesh was what was created, remember the built shape for later sharing.
  539. Hulls.Add(newHullKey, retHull);
  540. }
  541. retHull.physShapeInfo = newShape;
  542. }
  543. }
  544. physicsScene.DetailLog("{0},BSShapeHull,getReference,hull={1},size={2},lod={3}", prim.LocalID, retHull, prim.Size, lod);
  545. return retHull;
  546. }
  547. public override BSShape GetReference(BSScene pPhysicsScene, BSPhysObject pPrim)
  548. {
  549. BSShape ret = null;
  550. // If the underlying shape is native, the actual shape has not been build (waiting for asset)
  551. // and we must create a copy of the native shape since they are never shared.
  552. if (physShapeInfo.HasPhysicalShape && physShapeInfo.isNativeShape)
  553. {
  554. // TODO: decide when the native shapes should be freed. Check in Dereference?
  555. ret = BSShapeNative.GetReference(pPhysicsScene, pPrim, BSPhysicsShapeType.SHAPE_BOX, FixedShapeKey.KEY_BOX);
  556. }
  557. else
  558. {
  559. // Another reference to this shape is just counted.
  560. IncrementReference();
  561. ret = this;
  562. }
  563. return ret;
  564. }
  565. public override void Dereference(BSScene physicsScene)
  566. {
  567. lock (Hulls)
  568. {
  569. this.DecrementReference();
  570. physicsScene.DetailLog("{0},BSShapeHull.Dereference,shape={1}", BSScene.DetailLogZero, this);
  571. // TODO: schedule aging and destruction of unused meshes.
  572. }
  573. }
  574. List<ConvexResult> m_hulls;
  575. private BulletShape CreatePhysicalHull(BSScene physicsScene, BSPhysObject prim, System.UInt64 newHullKey,
  576. PrimitiveBaseShape pbs, OMV.Vector3 size, float lod)
  577. {
  578. BulletShape newShape = new BulletShape();
  579. IMesh meshData = null;
  580. List<List<OMV.Vector3>> allHulls = null;
  581. lock (physicsScene.mesher)
  582. {
  583. // Pass true for physicalness as this prevents the creation of bounding box which is not needed
  584. meshData = physicsScene.mesher.CreateMesh(prim.PhysObjectName, pbs, size, lod, true /* isPhysical */, false /* shouldCache */);
  585. // If we should use the asset's hull info, fetch it out of the locked mesher
  586. if (meshData != null && BSParam.ShouldUseAssetHulls)
  587. {
  588. Meshmerizer realMesher = physicsScene.mesher as Meshmerizer;
  589. if (realMesher != null)
  590. {
  591. allHulls = realMesher.GetConvexHulls(size);
  592. }
  593. if (allHulls == null)
  594. {
  595. physicsScene.DetailLog("{0},BSShapeHull.CreatePhysicalHull,assetHulls,noAssetHull", prim.LocalID);
  596. }
  597. }
  598. }
  599. // If there is hull data in the mesh asset, build the hull from that
  600. if (allHulls != null && BSParam.ShouldUseAssetHulls)
  601. {
  602. int hullCount = allHulls.Count;
  603. int totalVertices = 1; // include one for the count of the hulls
  604. // Using the structure described for HACD hulls, create the memory sturcture
  605. // to pass the hull data to the creater.
  606. foreach (List<OMV.Vector3> hullVerts in allHulls)
  607. {
  608. totalVertices += 4; // add four for the vertex count and centroid
  609. totalVertices += hullVerts.Count * 3; // one vertex is three dimensions
  610. }
  611. float[] convHulls = new float[totalVertices];
  612. convHulls[0] = (float)hullCount;
  613. int jj = 1;
  614. foreach (List<OMV.Vector3> hullVerts in allHulls)
  615. {
  616. convHulls[jj + 0] = hullVerts.Count;
  617. convHulls[jj + 1] = 0f; // centroid x,y,z
  618. convHulls[jj + 2] = 0f;
  619. convHulls[jj + 3] = 0f;
  620. jj += 4;
  621. foreach (OMV.Vector3 oneVert in hullVerts)
  622. {
  623. convHulls[jj + 0] = oneVert.X;
  624. convHulls[jj + 1] = oneVert.Y;
  625. convHulls[jj + 2] = oneVert.Z;
  626. jj += 3;
  627. }
  628. }
  629. // create the hull data structure in Bullet
  630. newShape = physicsScene.PE.CreateHullShape(physicsScene.World, hullCount, convHulls);
  631. physicsScene.DetailLog("{0},BSShapeHull.CreatePhysicalHull,assetHulls,hulls={1},totVert={2},shape={3}",
  632. prim.LocalID, hullCount, totalVertices, newShape);
  633. }
  634. // If no hull specified in the asset and we should use Bullet's HACD approximation...
  635. if (!newShape.HasPhysicalShape && BSParam.ShouldUseBulletHACD)
  636. {
  637. // Build the hull shape from an existing mesh shape.
  638. // The mesh should have already been created in Bullet.
  639. physicsScene.DetailLog("{0},BSShapeHull.CreatePhysicalHull,bulletHACD,entry", prim.LocalID);
  640. BSShape meshShape = BSShapeMesh.GetReference(physicsScene, true, prim);
  641. if (meshShape.physShapeInfo.HasPhysicalShape)
  642. {
  643. HACDParams parms;
  644. parms.maxVerticesPerHull = BSParam.BHullMaxVerticesPerHull;
  645. parms.minClusters = BSParam.BHullMinClusters;
  646. parms.compacityWeight = BSParam.BHullCompacityWeight;
  647. parms.volumeWeight = BSParam.BHullVolumeWeight;
  648. parms.concavity = BSParam.BHullConcavity;
  649. parms.addExtraDistPoints = BSParam.NumericBool(BSParam.BHullAddExtraDistPoints);
  650. parms.addNeighboursDistPoints = BSParam.NumericBool(BSParam.BHullAddNeighboursDistPoints);
  651. parms.addFacesPoints = BSParam.NumericBool(BSParam.BHullAddFacesPoints);
  652. parms.shouldAdjustCollisionMargin = BSParam.NumericBool(BSParam.BHullShouldAdjustCollisionMargin);
  653. physicsScene.DetailLog("{0},BSShapeHull.CreatePhysicalHull,hullFromMesh,beforeCall", prim.LocalID, newShape.HasPhysicalShape);
  654. newShape = physicsScene.PE.BuildHullShapeFromMesh(physicsScene.World, meshShape.physShapeInfo, parms);
  655. physicsScene.DetailLog("{0},BSShapeHull.CreatePhysicalHull,hullFromMesh,shape={1}", prim.LocalID, newShape);
  656. // Now done with the mesh shape.
  657. meshShape.Dereference(physicsScene);
  658. }
  659. physicsScene.DetailLog("{0},BSShapeHull.CreatePhysicalHull,bulletHACD,exit,hasBody={1}", prim.LocalID, newShape.HasPhysicalShape);
  660. }
  661. // If no other hull specifications, use our HACD hull approximation.
  662. if (!newShape.HasPhysicalShape && meshData != null)
  663. {
  664. if (prim.PrimAssetState == BSPhysObject.PrimAssetCondition.Fetched)
  665. {
  666. // Release the fetched asset data once it has been used.
  667. pbs.SculptData = new byte[0];
  668. prim.PrimAssetState = BSPhysObject.PrimAssetCondition.Unknown;
  669. }
  670. int[] indices = meshData.getIndexListAsInt();
  671. List<OMV.Vector3> vertices = meshData.getVertexList();
  672. //format conversion from IMesh format to DecompDesc format
  673. List<int> convIndices = new List<int>();
  674. List<float3> convVertices = new List<float3>();
  675. for (int ii = 0; ii < indices.GetLength(0); ii++)
  676. {
  677. convIndices.Add(indices[ii]);
  678. }
  679. foreach (OMV.Vector3 vv in vertices)
  680. {
  681. convVertices.Add(new float3(vv.X, vv.Y, vv.Z));
  682. }
  683. uint maxDepthSplit = (uint)BSParam.CSHullMaxDepthSplit;
  684. if (BSParam.CSHullMaxDepthSplit != BSParam.CSHullMaxDepthSplitForSimpleShapes)
  685. {
  686. // Simple primitive shapes we know are convex so they are better implemented with
  687. // fewer hulls.
  688. // Check for simple shape (prim without cuts) and reduce split parameter if so.
  689. if (BSShapeCollection.PrimHasNoCuts(pbs))
  690. {
  691. maxDepthSplit = (uint)BSParam.CSHullMaxDepthSplitForSimpleShapes;
  692. }
  693. }
  694. // setup and do convex hull conversion
  695. m_hulls = new List<ConvexResult>();
  696. DecompDesc dcomp = new DecompDesc();
  697. dcomp.mIndices = convIndices;
  698. dcomp.mVertices = convVertices;
  699. dcomp.mDepth = maxDepthSplit;
  700. dcomp.mCpercent = BSParam.CSHullConcavityThresholdPercent;
  701. dcomp.mPpercent = BSParam.CSHullVolumeConservationThresholdPercent;
  702. dcomp.mMaxVertices = (uint)BSParam.CSHullMaxVertices;
  703. dcomp.mSkinWidth = BSParam.CSHullMaxSkinWidth;
  704. ConvexBuilder convexBuilder = new ConvexBuilder(HullReturn);
  705. // create the hull into the _hulls variable
  706. convexBuilder.process(dcomp);
  707. physicsScene.DetailLog("{0},BSShapeCollection.CreatePhysicalHull,key={1},inVert={2},inInd={3},split={4},hulls={5}",
  708. BSScene.DetailLogZero, newHullKey, indices.GetLength(0), vertices.Count, maxDepthSplit, m_hulls.Count);
  709. // Convert the vertices and indices for passing to unmanaged.
  710. // The hull information is passed as a large floating point array.
  711. // The format is:
  712. // convHulls[0] = number of hulls
  713. // convHulls[1] = number of vertices in first hull
  714. // convHulls[2] = hull centroid X coordinate
  715. // convHulls[3] = hull centroid Y coordinate
  716. // convHulls[4] = hull centroid Z coordinate
  717. // convHulls[5] = first hull vertex X
  718. // convHulls[6] = first hull vertex Y
  719. // convHulls[7] = first hull vertex Z
  720. // convHulls[8] = second hull vertex X
  721. // ...
  722. // convHulls[n] = number of vertices in second hull
  723. // convHulls[n+1] = second hull centroid X coordinate
  724. // ...
  725. //
  726. // TODO: is is very inefficient. Someday change the convex hull generator to return
  727. // data structures that do not need to be converted in order to pass to Bullet.
  728. // And maybe put the values directly into pinned memory rather than marshaling.
  729. int hullCount = m_hulls.Count;
  730. int totalVertices = 1; // include one for the count of the hulls
  731. foreach (ConvexResult cr in m_hulls)
  732. {
  733. totalVertices += 4; // add four for the vertex count and centroid
  734. totalVertices += cr.HullIndices.Count * 3; // we pass just triangles
  735. }
  736. float[] convHulls = new float[totalVertices];
  737. convHulls[0] = (float)hullCount;
  738. int jj = 1;
  739. foreach (ConvexResult cr in m_hulls)
  740. {
  741. // copy vertices for index access
  742. float3[] verts = new float3[cr.HullVertices.Count];
  743. int kk = 0;
  744. foreach (float3 ff in cr.HullVertices)
  745. {
  746. verts[kk++] = ff;
  747. }
  748. // add to the array one hull's worth of data
  749. convHulls[jj++] = cr.HullIndices.Count;
  750. convHulls[jj++] = 0f; // centroid x,y,z
  751. convHulls[jj++] = 0f;
  752. convHulls[jj++] = 0f;
  753. foreach (int ind in cr.HullIndices)
  754. {
  755. convHulls[jj++] = verts[ind].x;
  756. convHulls[jj++] = verts[ind].y;
  757. convHulls[jj++] = verts[ind].z;
  758. }
  759. }
  760. // create the hull data structure in Bullet
  761. newShape = physicsScene.PE.CreateHullShape(physicsScene.World, hullCount, convHulls);
  762. }
  763. newShape.shapeKey = newHullKey;
  764. return newShape;
  765. }
  766. // Callback from convex hull creater with a newly created hull.
  767. // Just add it to our collection of hulls for this shape.
  768. private void HullReturn(ConvexResult result)
  769. {
  770. m_hulls.Add(result);
  771. return;
  772. }
  773. // Loop through all the known hulls and return the description based on the physical address.
  774. public static bool TryGetHullByPtr(BulletShape pShape, out BSShapeHull outHull)
  775. {
  776. bool ret = false;
  777. BSShapeHull foundDesc = null;
  778. lock (Hulls)
  779. {
  780. foreach (BSShapeHull sh in Hulls.Values)
  781. {
  782. if (sh.physShapeInfo.ReferenceSame(pShape))
  783. {
  784. foundDesc = sh;
  785. ret = true;
  786. break;
  787. }
  788. }
  789. }
  790. outHull = foundDesc;
  791. return ret;
  792. }
  793. }
  794. // ============================================================================================================
  795. public class BSShapeCompound : BSShape
  796. {
  797. private static string LogHeader = "[BULLETSIM SHAPE COMPOUND]";
  798. public static Dictionary<string, BSShapeCompound> CompoundShapes = new Dictionary<string, BSShapeCompound>();
  799. public BSShapeCompound(BulletShape pShape) : base(pShape)
  800. {
  801. }
  802. public static BSShape GetReference(BSScene physicsScene)
  803. {
  804. // Base compound shapes are not shared so this returns a raw shape.
  805. // A built compound shape can be reused in linksets.
  806. BSShapeCompound ret = new BSShapeCompound(CreatePhysicalCompoundShape(physicsScene));
  807. CompoundShapes.Add(ret.AddrString, ret);
  808. return ret;
  809. }
  810. public override BSShape GetReference(BSScene physicsScene, BSPhysObject prim)
  811. {
  812. // Calling this reference means we want another handle to an existing compound shape
  813. // (usually linksets) so return this copy.
  814. IncrementReference();
  815. return this;
  816. }
  817. // Dereferencing a compound shape releases the hold on all the child shapes.
  818. public override void Dereference(BSScene physicsScene)
  819. {
  820. lock (physShapeInfo)
  821. {
  822. this.DecrementReference();
  823. physicsScene.DetailLog("{0},BSShapeCompound.Dereference,shape={1}", BSScene.DetailLogZero, this);
  824. if (referenceCount <= 0)
  825. {
  826. if (!physicsScene.PE.IsCompound(physShapeInfo))
  827. {
  828. // Failed the sanity check!!
  829. physicsScene.Logger.ErrorFormat("{0} Attempt to free a compound shape that is not compound!! type={1}, ptr={2}",
  830. LogHeader, physShapeInfo.shapeType, physShapeInfo.AddrString);
  831. physicsScene.DetailLog("{0},BSShapeCollection.DereferenceCompound,notACompoundShape,type={1},ptr={2}",
  832. BSScene.DetailLogZero, physShapeInfo.shapeType, physShapeInfo.AddrString);
  833. return;
  834. }
  835. int numChildren = physicsScene.PE.GetNumberOfCompoundChildren(physShapeInfo);
  836. physicsScene.DetailLog("{0},BSShapeCollection.DereferenceCompound,shape={1},children={2}",
  837. BSScene.DetailLogZero, physShapeInfo, numChildren);
  838. // Loop through all the children dereferencing each.
  839. for (int ii = numChildren - 1; ii >= 0; ii--)
  840. {
  841. BulletShape childShape = physicsScene.PE.RemoveChildShapeFromCompoundShapeIndex(physShapeInfo, ii);
  842. DereferenceAnonCollisionShape(physicsScene, childShape);
  843. }
  844. lock (CompoundShapes)
  845. CompoundShapes.Remove(physShapeInfo.AddrString);
  846. physicsScene.PE.DeleteCollisionShape(physicsScene.World, physShapeInfo);
  847. }
  848. }
  849. }
  850. public static bool TryGetCompoundByPtr(BulletShape pShape, out BSShapeCompound outCompound)
  851. {
  852. lock (CompoundShapes)
  853. {
  854. string addr = pShape.AddrString;
  855. return CompoundShapes.TryGetValue(addr, out outCompound);
  856. }
  857. }
  858. private static BulletShape CreatePhysicalCompoundShape(BSScene physicsScene)
  859. {
  860. BulletShape cShape = physicsScene.PE.CreateCompoundShape(physicsScene.World, false);
  861. return cShape;
  862. }
  863. // Sometimes we have a pointer to a collision shape but don't know what type it is.
  864. // Figure out type and call the correct dereference routine.
  865. // Called at taint-time.
  866. private void DereferenceAnonCollisionShape(BSScene physicsScene, BulletShape pShape)
  867. {
  868. // TODO: figure a better way to go through all the shape types and find a possible instance.
  869. physicsScene.DetailLog("{0},BSShapeCompound.DereferenceAnonCollisionShape,shape={1}",
  870. BSScene.DetailLogZero, pShape);
  871. BSShapeMesh meshDesc;
  872. if (BSShapeMesh.TryGetMeshByPtr(pShape, out meshDesc))
  873. {
  874. meshDesc.Dereference(physicsScene);
  875. // physicsScene.DetailLog("{0},BSShapeCompound.DereferenceAnonCollisionShape,foundMesh,shape={1}", BSScene.DetailLogZero, pShape);
  876. }
  877. else
  878. {
  879. BSShapeHull hullDesc;
  880. if (BSShapeHull.TryGetHullByPtr(pShape, out hullDesc))
  881. {
  882. hullDesc.Dereference(physicsScene);
  883. // physicsScene.DetailLog("{0},BSShapeCompound.DereferenceAnonCollisionShape,foundHull,shape={1}", BSScene.DetailLogZero, pShape);
  884. }
  885. else
  886. {
  887. BSShapeConvexHull chullDesc;
  888. if (BSShapeConvexHull.TryGetConvexHullByPtr(pShape, out chullDesc))
  889. {
  890. chullDesc.Dereference(physicsScene);
  891. // physicsScene.DetailLog("{0},BSShapeCompound.DereferenceAnonCollisionShape,foundConvexHull,shape={1}", BSScene.DetailLogZero, pShape);
  892. }
  893. else
  894. {
  895. BSShapeGImpact gImpactDesc;
  896. if (BSShapeGImpact.TryGetGImpactByPtr(pShape, out gImpactDesc))
  897. {
  898. gImpactDesc.Dereference(physicsScene);
  899. // physicsScene.DetailLog("{0},BSShapeCompound.DereferenceAnonCollisionShape,foundgImpact,shape={1}", BSScene.DetailLogZero, pShape);
  900. }
  901. else
  902. {
  903. // Didn't find it in the lists of specific types. It could be compound.
  904. BSShapeCompound compoundDesc;
  905. if (BSShapeCompound.TryGetCompoundByPtr(pShape, out compoundDesc))
  906. {
  907. compoundDesc.Dereference(physicsScene);
  908. // physicsScene.DetailLog("{0},BSShapeCompound.DereferenceAnonCollisionShape,recursiveCompoundShape,shape={1}", BSScene.DetailLogZero, pShape);
  909. }
  910. else
  911. {
  912. // If none of the above, maybe it is a simple native shape.
  913. if (physicsScene.PE.IsNativeShape(pShape))
  914. {
  915. // physicsScene.DetailLog("{0},BSShapeCompound.DereferenceAnonCollisionShape,assumingNative,shape={1}", BSScene.DetailLogZero, pShape);
  916. BSShapeNative nativeShape = new BSShapeNative(pShape);
  917. nativeShape.Dereference(physicsScene);
  918. }
  919. }
  920. }
  921. }
  922. }
  923. }
  924. }
  925. }
  926. // ============================================================================================================
  927. public class BSShapeConvexHull : BSShape
  928. {
  929. private static string LogHeader = "[BULLETSIM SHAPE CONVEX HULL]";
  930. public static Dictionary<System.UInt64, BSShapeConvexHull> ConvexHulls = new Dictionary<System.UInt64, BSShapeConvexHull>();
  931. public BSShapeConvexHull(BulletShape pShape) : base(pShape)
  932. {
  933. }
  934. public static BSShape GetReference(BSScene physicsScene, bool forceRebuild, BSPhysObject prim)
  935. {
  936. float lod;
  937. System.UInt64 newMeshKey = BSShape.ComputeShapeKey(prim.Size, prim.BaseShape, out lod);
  938. physicsScene.DetailLog("{0},BSShapeConvexHull,getReference,newKey={1},size={2},lod={3}",
  939. prim.LocalID, newMeshKey.ToString("X"), prim.Size, lod);
  940. BSShapeConvexHull retConvexHull = null;
  941. lock (ConvexHulls)
  942. {
  943. if (ConvexHulls.TryGetValue(newMeshKey, out retConvexHull))
  944. {
  945. // The mesh has already been created. Return a new reference to same.
  946. retConvexHull.IncrementReference();
  947. }
  948. else
  949. {
  950. retConvexHull = new BSShapeConvexHull(new BulletShape());
  951. BulletShape convexShape = null;
  952. // Get a handle to a mesh to build the hull from
  953. BSShape baseMesh = BSShapeMesh.GetReference(physicsScene, false /* forceRebuild */, prim);
  954. if (baseMesh.physShapeInfo.isNativeShape)
  955. {
  956. // We get here if the mesh was not creatable. Could be waiting for an asset from the disk.
  957. // In the short term, we return the native shape and a later ForceBodyShapeRebuild should
  958. // get back to this code with a buildable mesh.
  959. // TODO: not sure the temp native shape is freed when the mesh is rebuilt. When does this get freed?
  960. convexShape = baseMesh.physShapeInfo;
  961. }
  962. else
  963. {
  964. convexShape = physicsScene.PE.BuildConvexHullShapeFromMesh(physicsScene.World, baseMesh.physShapeInfo);
  965. convexShape.shapeKey = newMeshKey;
  966. ConvexHulls.Add(convexShape.shapeKey, retConvexHull);
  967. physicsScene.DetailLog("{0},BSShapeConvexHull.GetReference,addingNewlyCreatedShape,shape={1}",
  968. BSScene.DetailLogZero, convexShape);
  969. }
  970. // Done with the base mesh
  971. baseMesh.Dereference(physicsScene);
  972. retConvexHull.physShapeInfo = convexShape;
  973. }
  974. }
  975. return retConvexHull;
  976. }
  977. public override BSShape GetReference(BSScene physicsScene, BSPhysObject prim)
  978. {
  979. // Calling this reference means we want another handle to an existing shape
  980. // (usually linksets) so return this copy.
  981. IncrementReference();
  982. return this;
  983. }
  984. // Dereferencing a compound shape releases the hold on all the child shapes.
  985. public override void Dereference(BSScene physicsScene)
  986. {
  987. lock (ConvexHulls)
  988. {
  989. this.DecrementReference();
  990. physicsScene.DetailLog("{0},BSShapeConvexHull.Dereference,shape={1}", BSScene.DetailLogZero, this);
  991. // TODO: schedule aging and destruction of unused meshes.
  992. }
  993. }
  994. // Loop through all the known hulls and return the description based on the physical address.
  995. public static bool TryGetConvexHullByPtr(BulletShape pShape, out BSShapeConvexHull outHull)
  996. {
  997. bool ret = false;
  998. BSShapeConvexHull foundDesc = null;
  999. lock (ConvexHulls)
  1000. {
  1001. foreach (BSShapeConvexHull sh in ConvexHulls.Values)
  1002. {
  1003. if (sh.physShapeInfo.ReferenceSame(pShape))
  1004. {
  1005. foundDesc = sh;
  1006. ret = true;
  1007. break;
  1008. }
  1009. }
  1010. }
  1011. outHull = foundDesc;
  1012. return ret;
  1013. }
  1014. }
  1015. // ============================================================================================================
  1016. public class BSShapeGImpact : BSShape
  1017. {
  1018. private static string LogHeader = "[BULLETSIM SHAPE GIMPACT]";
  1019. public static Dictionary<System.UInt64, BSShapeGImpact> GImpacts = new Dictionary<System.UInt64, BSShapeGImpact>();
  1020. public BSShapeGImpact(BulletShape pShape) : base(pShape)
  1021. {
  1022. }
  1023. public static BSShape GetReference(BSScene physicsScene, bool forceRebuild, BSPhysObject prim)
  1024. {
  1025. float lod;
  1026. System.UInt64 newMeshKey = BSShape.ComputeShapeKey(prim.Size, prim.BaseShape, out lod);
  1027. physicsScene.DetailLog("{0},BSShapeGImpact,getReference,newKey={1},size={2},lod={3}",
  1028. prim.LocalID, newMeshKey.ToString("X"), prim.Size, lod);
  1029. BSShapeGImpact retGImpact = null;
  1030. lock (GImpacts)
  1031. {
  1032. if (GImpacts.TryGetValue(newMeshKey, out retGImpact))
  1033. {
  1034. // The mesh has already been created. Return a new reference to same.
  1035. retGImpact.IncrementReference();
  1036. }
  1037. else
  1038. {
  1039. retGImpact = new BSShapeGImpact(new BulletShape());
  1040. BulletShape newShape = retGImpact.CreatePhysicalGImpact(physicsScene, prim, newMeshKey, prim.BaseShape, prim.Size, lod);
  1041. // Check to see if mesh was created (might require an asset).
  1042. newShape = VerifyMeshCreated(physicsScene, newShape, prim);
  1043. newShape.shapeKey = newMeshKey;
  1044. if (!newShape.isNativeShape
  1045. || prim.PrimAssetState == BSPhysObject.PrimAssetCondition.FailedMeshing
  1046. || prim.PrimAssetState == BSPhysObject.PrimAssetCondition.FailedAssetFetch)
  1047. {
  1048. // If a mesh was what was created, remember the built shape for later sharing.
  1049. // Also note that if meshing failed we put it in the mesh list as there is nothing
  1050. // else to do about the mesh.
  1051. GImpacts.Add(newMeshKey, retGImpact);
  1052. }
  1053. retGImpact.physShapeInfo = newShape;
  1054. }
  1055. }
  1056. return retGImpact;
  1057. }
  1058. private BulletShape CreatePhysicalGImpact(BSScene physicsScene, BSPhysObject prim, System.UInt64 newMeshKey,
  1059. PrimitiveBaseShape pbs, OMV.Vector3 size, float lod)
  1060. {
  1061. return BSShapeMesh.CreatePhysicalMeshShape(physicsScene, prim, newMeshKey, pbs, size, lod,
  1062. (w, iC, i, vC, v) => physicsScene.PE.CreateGImpactShape(w, iC, i, vC, v) );
  1063. }
  1064. public override BSShape GetReference(BSScene pPhysicsScene, BSPhysObject pPrim)
  1065. {
  1066. BSShape ret = null;
  1067. // If the underlying shape is native, the actual shape has not been build (waiting for asset)
  1068. // and we must create a copy of the native shape since they are never shared.
  1069. if (physShapeInfo.HasPhysicalShape && physShapeInfo.isNativeShape)
  1070. {
  1071. // TODO: decide when the native shapes should be freed. Check in Dereference?
  1072. ret = BSShapeNative.GetReference(pPhysicsScene, pPrim, BSPhysicsShapeType.SHAPE_BOX, FixedShapeKey.KEY_BOX);
  1073. }
  1074. else
  1075. {
  1076. // Another reference to this shape is just counted.
  1077. IncrementReference();
  1078. ret = this;
  1079. }
  1080. return ret;
  1081. }
  1082. // Dereferencing a compound shape releases the hold on all the child shapes.
  1083. public override void Dereference(BSScene physicsScene)
  1084. {
  1085. lock (GImpacts)
  1086. {
  1087. this.DecrementReference();
  1088. physicsScene.DetailLog("{0},BSShapeGImpact.Dereference,shape={1}", BSScene.DetailLogZero, this);
  1089. // TODO: schedule aging and destruction of unused meshes.
  1090. }
  1091. }
  1092. // Loop through all the known hulls and return the description based on the physical address.
  1093. public static bool TryGetGImpactByPtr(BulletShape pShape, out BSShapeGImpact outHull)
  1094. {
  1095. bool ret = false;
  1096. BSShapeGImpact foundDesc = null;
  1097. lock (GImpacts)
  1098. {
  1099. foreach (BSShapeGImpact sh in GImpacts.Values)
  1100. {
  1101. if (sh.physShapeInfo.ReferenceSame(pShape))
  1102. {
  1103. foundDesc = sh;
  1104. ret = true;
  1105. break;
  1106. }
  1107. }
  1108. }
  1109. outHull = foundDesc;
  1110. return ret;
  1111. }
  1112. }
  1113. // ============================================================================================================
  1114. public class BSShapeAvatar : BSShape
  1115. {
  1116. private static string LogHeader = "[BULLETSIM SHAPE AVATAR]";
  1117. public BSShapeAvatar() : base()
  1118. {
  1119. }
  1120. public static BSShape GetReference(BSPhysObject prim)
  1121. {
  1122. return new BSShapeNull();
  1123. }
  1124. public override BSShape GetReference(BSScene pPhysicsScene, BSPhysObject pPrim)
  1125. {
  1126. return new BSShapeNull();
  1127. }
  1128. public override void Dereference(BSScene physicsScene) { }
  1129. // From the front:
  1130. // A---A
  1131. // / \
  1132. // B-------B
  1133. // / \ +Z
  1134. // C-----------C |
  1135. // \ / -Y --+-- +Y
  1136. // \ / |
  1137. // \ / -Z
  1138. // D-----D
  1139. // \ /
  1140. // E-E
  1141. // From the top A and E are just lines.
  1142. // B, C and D are hexagons:
  1143. //
  1144. // C1--C2 +X
  1145. // / \ |
  1146. // C0 C3 -Y --+-- +Y
  1147. // \ / |
  1148. // C5--C4 -X
  1149. // Zero goes directly through the middle so the offsets are from that middle axis
  1150. // and up and down from a middle horizon (A and E are the same distance from the zero).
  1151. // The height, width and depth is one. All scaling is done by the simulator.
  1152. // Z component -- how far the level is from the middle zero
  1153. private const float Aup = 0.5f;
  1154. private const float Bup = 0.4f;
  1155. private const float Cup = 0.3f;
  1156. private const float Dup = -0.4f;
  1157. private const float Eup = -0.5f;
  1158. // Y component -- distance from center to x0 and x3
  1159. private const float Awid = 0.25f;
  1160. private const float Bwid = 0.3f;
  1161. private const float Cwid = 0.5f;
  1162. private const float Dwid = 0.3f;
  1163. private const float Ewid = 0.2f;
  1164. // Y component -- distance from center to x1, x2, x4 and x5
  1165. private const float Afwid = 0.0f;
  1166. private const float Bfwid = 0.2f;
  1167. private const float Cfwid = 0.4f;
  1168. private const float Dfwid = 0.2f;
  1169. private const float Efwid = 0.0f;
  1170. // X component -- distance from zero to the front or back of a level
  1171. private const float Adep = 0f;
  1172. private const float Bdep = 0.3f;
  1173. private const float Cdep = 0.5f;
  1174. private const float Ddep = 0.2f;
  1175. private const float Edep = 0f;
  1176. private OMV.Vector3[] avatarVertices = {
  1177. new OMV.Vector3( 0.0f, -Awid, Aup), // A0
  1178. new OMV.Vector3( 0.0f, +Awid, Aup), // A3
  1179. new OMV.Vector3( 0.0f, -Bwid, Bup), // B0
  1180. new OMV.Vector3(+Bdep, -Bfwid, Bup), // B1
  1181. new OMV.Vector3(+Bdep, +Bfwid, Bup), // B2
  1182. new OMV.Vector3( 0.0f, +Bwid, Bup), // B3
  1183. new OMV.Vector3(-Bdep, +Bfwid, Bup), // B4
  1184. new OMV.Vector3(-Bdep, -Bfwid, Bup), // B5
  1185. new OMV.Vector3( 0.0f, -Cwid, Cup), // C0
  1186. new OMV.Vector3(+Cdep, -Cfwid, Cup), // C1
  1187. new OMV.Vector3(+Cdep, +Cfwid, Cup), // C2
  1188. new OMV.Vector3( 0.0f, +Cwid, Cup), // C3
  1189. new OMV.Vector3(-Cdep, +Cfwid, Cup), // C4
  1190. new OMV.Vector3(-Cdep, -Cfwid, Cup), // C5
  1191. new OMV.Vector3( 0.0f, -Dwid, Dup), // D0
  1192. new OMV.Vector3(+Ddep, -Dfwid, Dup), // D1
  1193. new OMV.Vector3(+Ddep, +Dfwid, Dup), // D2
  1194. new OMV.Vector3( 0.0f, +Dwid, Dup), // D3
  1195. new OMV.Vector3(-Ddep, +Dfwid, Dup), // D4
  1196. new OMV.Vector3(-Ddep, -Dfwid, Dup), // D5
  1197. new OMV.Vector3( 0.0f, -Ewid, Eup), // E0
  1198. new OMV.Vector3( 0.0f, +Ewid, Eup), // E3
  1199. };
  1200. // Offsets of the vertices in the vertices array
  1201. private enum Ind : int
  1202. {
  1203. A0, A3,
  1204. B0, B1, B2, B3, B4, B5,
  1205. C0, C1, C2, C3, C4, C5,
  1206. D0, D1, D2, D3, D4, D5,
  1207. E0, E3
  1208. }
  1209. // Comments specify trianges and quads in clockwise direction
  1210. private Ind[] avatarIndices = {
  1211. Ind.A0, Ind.B0, Ind.B1, // A0,B0,B1
  1212. Ind.A0, Ind.B1, Ind.B2, Ind.B2, Ind.A3, Ind.A0, // A0,B1,B2,A3
  1213. Ind.A3, Ind.B2, Ind.B3, // A3,B2,B3
  1214. Ind.A3, Ind.B3, Ind.B4, // A3,B3,B4
  1215. Ind.A3, Ind.B4, Ind.B5, Ind.B5, Ind.A0, Ind.A3, // A3,B4,B5,A0
  1216. Ind.A0, Ind.B5, Ind.B0, // A0,B5,B0
  1217. Ind.B0, Ind.C0, Ind.C1, Ind.C1, Ind.B1, Ind.B0, // B0,C0,C1,B1
  1218. Ind.B1, Ind.C1, Ind.C2, Ind.C2, Ind.B2, Ind.B1, // B1,C1,C2,B2
  1219. Ind.B2, Ind.C2, Ind.C3, Ind.C3, Ind.B3, Ind.B2, // B2,C2,C3,B3
  1220. Ind.B3, Ind.C3, Ind.C4, Ind.C4, Ind.B4, Ind.B3, // B3,C3,C4,B4
  1221. Ind.B4, Ind.C4, Ind.C5, Ind.C5, Ind.B5, Ind.B4, // B4,C4,C5,B5
  1222. Ind.B5, Ind.C5, Ind.C0, Ind.C0, Ind.B0, Ind.B5, // B5,C5,C0,B0
  1223. Ind.C0, Ind.D0, Ind.D1, Ind.D1, Ind.C1, Ind.C0, // C0,D0,D1,C1
  1224. Ind.C1, Ind.D1, Ind.D2, Ind.D2, Ind.C2, Ind.C1, // C1,D1,D2,C2
  1225. Ind.C2, Ind.D2, Ind.D3, Ind.D3, Ind.C3, Ind.C2, // C2,D2,D3,C3
  1226. Ind.C3, Ind.D3, Ind.D4, Ind.D4, Ind.C4, Ind.C3, // C3,D3,D4,C4
  1227. Ind.C4, Ind.D4, Ind.D5, Ind.D5, Ind.C5, Ind.C4, // C4,D4,D5,C5
  1228. Ind.C5, Ind.D5, Ind.D0, Ind.D0, Ind.C0, Ind.C5, // C5,D5,D0,C0
  1229. Ind.E0, Ind.D0, Ind.D1, // E0,D0,D1
  1230. Ind.E0, Ind.D1, Ind.D2, Ind.D2, Ind.E3, Ind.E0, // E0,D1,D2,E3
  1231. Ind.E3, Ind.D2, Ind.D3, // E3,D2,D3
  1232. Ind.E3, Ind.D3, Ind.D4, // E3,D3,D4
  1233. Ind.E3, Ind.D4, Ind.D5, Ind.D5, Ind.E0, Ind.E3, // E3,D4,D5,E0
  1234. Ind.E0, Ind.D5, Ind.D0, // E0,D5,D0
  1235. };
  1236. }
  1237. }