ODEMeshWorker.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. /*
  2. * AJLDuarte 2012
  3. */
  4. using System;
  5. using System.Threading;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Reflection;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. using OpenSim.Framework;
  12. using OpenSim.Region.PhysicsModules.SharedBase;
  13. using OdeAPI;
  14. using log4net;
  15. using Nini.Config;
  16. using OpenMetaverse;
  17. namespace OpenSim.Region.PhysicsModule.ubOde
  18. {
  19. public enum MeshState : byte
  20. {
  21. noNeed = 0,
  22. loadingAsset = 1,
  23. AssetOK = 0x0f, // 00001111
  24. NeedMask = 0x30, // 00110000
  25. needMesh = 0x10, // 00010000
  26. needAsset = 0x20, // 00100000
  27. FailMask = 0xC0, // 11000000
  28. AssetFailed = 0x40, // 01000000
  29. MeshFailed = 0x80, // 10000000
  30. MeshNoColide = FailMask | needAsset
  31. }
  32. public enum meshWorkerCmnds : byte
  33. {
  34. nop = 0,
  35. addnew,
  36. changefull,
  37. changesize,
  38. changeshapetype,
  39. getmesh,
  40. }
  41. public class ODEPhysRepData
  42. {
  43. public PhysicsActor actor;
  44. public PrimitiveBaseShape pbs;
  45. public IMesh mesh;
  46. public Vector3 size;
  47. public Vector3 OBB;
  48. public Vector3 OBBOffset;
  49. public float volume;
  50. public byte shapetype;
  51. public bool hasOBB;
  52. public bool hasMeshVolume;
  53. public bool isTooSmall;
  54. public MeshState meshState;
  55. public UUID? assetID;
  56. public meshWorkerCmnds comand;
  57. }
  58. public class ODEMeshWorker
  59. {
  60. private ILog m_log;
  61. private ODEScene m_scene;
  62. private IMesher m_mesher;
  63. public bool meshSculptedPrim = true;
  64. public float meshSculptLOD = 32;
  65. public float MeshSculptphysicalLOD = 32;
  66. public float MinSizeToMeshmerize = 0.1f;
  67. private OpenSim.Framework.BlockingQueue<ODEPhysRepData> workQueue = new OpenSim.Framework.BlockingQueue<ODEPhysRepData>();
  68. private bool m_running;
  69. private Thread m_thread;
  70. public ODEMeshWorker(ODEScene pScene, ILog pLog, IMesher pMesher, IConfig pConfig)
  71. {
  72. m_scene = pScene;
  73. m_log = pLog;
  74. m_mesher = pMesher;
  75. if (pConfig != null)
  76. {
  77. meshSculptedPrim = pConfig.GetBoolean("mesh_sculpted_prim", meshSculptedPrim);
  78. meshSculptLOD = pConfig.GetFloat("mesh_lod", meshSculptLOD);
  79. MinSizeToMeshmerize = pConfig.GetFloat("mesh_min_size", MinSizeToMeshmerize);
  80. MeshSculptphysicalLOD = pConfig.GetFloat("mesh_physical_lod", MeshSculptphysicalLOD);
  81. }
  82. m_running = true;
  83. m_thread = new Thread(DoWork);
  84. m_thread.Name = "OdeMeshWorker";
  85. m_thread.Start();
  86. }
  87. private void DoWork()
  88. {
  89. m_mesher.ExpireFileCache();
  90. while(m_running)
  91. {
  92. ODEPhysRepData nextRep = workQueue.Dequeue();
  93. if(!m_running)
  94. return;
  95. if (nextRep == null)
  96. continue;
  97. if (m_scene.haveActor(nextRep.actor))
  98. {
  99. switch (nextRep.comand)
  100. {
  101. case meshWorkerCmnds.changefull:
  102. case meshWorkerCmnds.changeshapetype:
  103. case meshWorkerCmnds.changesize:
  104. GetMesh(nextRep);
  105. if (CreateActorPhysRep(nextRep) && m_scene.haveActor(nextRep.actor))
  106. m_scene.AddChange(nextRep.actor, changes.PhysRepData, nextRep);
  107. break;
  108. case meshWorkerCmnds.getmesh:
  109. DoRepDataGetMesh(nextRep);
  110. break;
  111. }
  112. }
  113. }
  114. }
  115. public void Stop()
  116. {
  117. try
  118. {
  119. m_thread.Abort();
  120. workQueue.Clear();
  121. }
  122. catch
  123. {
  124. }
  125. }
  126. public void ChangeActorPhysRep(PhysicsActor actor, PrimitiveBaseShape pbs,
  127. Vector3 size, byte shapetype)
  128. {
  129. ODEPhysRepData repData = new ODEPhysRepData();
  130. repData.actor = actor;
  131. repData.pbs = pbs;
  132. repData.size = size;
  133. repData.shapetype = shapetype;
  134. CheckMesh(repData);
  135. CalcVolumeData(repData);
  136. m_scene.AddChange(actor, changes.PhysRepData, repData);
  137. return;
  138. }
  139. public ODEPhysRepData NewActorPhysRep(PhysicsActor actor, PrimitiveBaseShape pbs,
  140. Vector3 size, byte shapetype)
  141. {
  142. ODEPhysRepData repData = new ODEPhysRepData();
  143. repData.actor = actor;
  144. repData.pbs = pbs;
  145. repData.size = size;
  146. repData.shapetype = shapetype;
  147. CheckMesh(repData);
  148. CalcVolumeData(repData);
  149. m_scene.AddChange(actor, changes.AddPhysRep, repData);
  150. return repData;
  151. }
  152. public void RequestMesh(ODEPhysRepData repData)
  153. {
  154. repData.mesh = null;
  155. if (repData.meshState == MeshState.needAsset)
  156. {
  157. PrimitiveBaseShape pbs = repData.pbs;
  158. // check if we got outdated
  159. if (!pbs.SculptEntry || pbs.SculptTexture == UUID.Zero)
  160. {
  161. repData.meshState = MeshState.noNeed;
  162. return;
  163. }
  164. repData.assetID = pbs.SculptTexture;
  165. repData.meshState = MeshState.loadingAsset;
  166. repData.comand = meshWorkerCmnds.getmesh;
  167. workQueue.Enqueue(repData);
  168. }
  169. }
  170. // creates and prepares a mesh to use and calls parameters estimation
  171. public bool CreateActorPhysRep(ODEPhysRepData repData)
  172. {
  173. IMesh mesh = repData.mesh;
  174. if (mesh != null)
  175. {
  176. IntPtr vertices, indices;
  177. int vertexCount, indexCount;
  178. int vertexStride, triStride;
  179. mesh.getVertexListAsPtrToFloatArray(out vertices, out vertexStride, out vertexCount);
  180. mesh.getIndexListAsPtrToIntArray(out indices, out triStride, out indexCount);
  181. if (vertexCount == 0 || indexCount == 0)
  182. {
  183. m_log.WarnFormat("[PHYSICS]: Invalid mesh data on prim {0} mesh UUID {1}",
  184. repData.actor.Name, repData.pbs.SculptTexture.ToString());
  185. repData.meshState = MeshState.MeshFailed;
  186. repData.hasOBB = false;
  187. repData.mesh = null;
  188. m_scene.mesher.ReleaseMesh(mesh);
  189. }
  190. else
  191. {
  192. repData.OBBOffset = mesh.GetCentroid();
  193. repData.OBB = mesh.GetOBB();
  194. repData.hasOBB = true;
  195. mesh.releaseSourceMeshData();
  196. }
  197. }
  198. CalcVolumeData(repData);
  199. return true;
  200. }
  201. public void AssetLoaded(ODEPhysRepData repData)
  202. {
  203. if (m_scene.haveActor(repData.actor))
  204. {
  205. if (needsMeshing(repData)) // no need for pbs now?
  206. {
  207. repData.comand = meshWorkerCmnds.changefull;
  208. workQueue.Enqueue(repData);
  209. }
  210. }
  211. else
  212. repData.pbs.SculptData = Utils.EmptyBytes;
  213. }
  214. public void DoRepDataGetMesh(ODEPhysRepData repData)
  215. {
  216. if (!repData.pbs.SculptEntry)
  217. return;
  218. if (repData.meshState != MeshState.loadingAsset)
  219. return;
  220. if (repData.assetID == null || repData.assetID == UUID.Zero)
  221. return;
  222. if (repData.assetID != repData.pbs.SculptTexture)
  223. return;
  224. // check if it is in cache
  225. GetMesh(repData);
  226. if (repData.meshState != MeshState.needAsset)
  227. {
  228. CreateActorPhysRep(repData);
  229. m_scene.AddChange(repData.actor, changes.PhysRepData, repData);
  230. return;
  231. }
  232. RequestAssetDelegate assetProvider = m_scene.RequestAssetMethod;
  233. if (assetProvider == null)
  234. return;
  235. ODEAssetRequest asr = new ODEAssetRequest(this, assetProvider, repData, m_log);
  236. }
  237. /// <summary>
  238. /// Routine to figure out if we need to mesh this prim with our mesher
  239. /// </summary>
  240. /// <param name="pbs"></param>
  241. /// <returns></returns>
  242. public bool needsMeshing(ODEPhysRepData repData)
  243. {
  244. PrimitiveBaseShape pbs = repData.pbs;
  245. // check sculpts or meshs
  246. Vector3 scale = pbs.Scale;
  247. if(scale.X <= MinSizeToMeshmerize &&
  248. scale.Y <= MinSizeToMeshmerize &&
  249. scale.Z <= MinSizeToMeshmerize)
  250. {
  251. repData.isTooSmall = true;
  252. return false;
  253. }
  254. if (pbs.SculptEntry)
  255. {
  256. if (meshSculptedPrim)
  257. return true;
  258. if (pbs.SculptType == (byte)SculptType.Mesh) // always do meshs
  259. return true;
  260. return false;
  261. }
  262. // convex shapes have no holes
  263. ushort profilehollow = pbs.ProfileHollow;
  264. if(repData.shapetype == 2)
  265. profilehollow = 0;
  266. // if it's a standard box or sphere with no cuts, hollows, twist or top shear, return false since ODE can use an internal representation for the prim
  267. if ((pbs.ProfileShape == ProfileShape.Square && pbs.PathCurve == (byte)Extrusion.Straight)
  268. || (pbs.ProfileShape == ProfileShape.HalfCircle && pbs.PathCurve == (byte)Extrusion.Curve1
  269. && pbs.Scale.X == pbs.Scale.Y && pbs.Scale.Y == pbs.Scale.Z))
  270. {
  271. if (pbs.ProfileBegin == 0 && pbs.ProfileEnd == 0
  272. && profilehollow == 0
  273. && pbs.PathTwist == 0 && pbs.PathTwistBegin == 0
  274. && pbs.PathBegin == 0 && pbs.PathEnd == 0
  275. && pbs.PathTaperX == 0 && pbs.PathTaperY == 0
  276. && pbs.PathScaleX == 100 && pbs.PathScaleY == 100
  277. && pbs.PathShearX == 0 && pbs.PathShearY == 0)
  278. {
  279. return false;
  280. }
  281. }
  282. // following code doesn't give meshs to boxes and spheres ever
  283. // and it's odd.. so for now just return true if asked to force meshs
  284. // hopefully mesher will fail if doesn't suport so things still get basic boxes
  285. int iPropertiesNotSupportedDefault = 0;
  286. if (profilehollow != 0)
  287. iPropertiesNotSupportedDefault++;
  288. if ((pbs.PathBegin != 0) || pbs.PathEnd != 0)
  289. iPropertiesNotSupportedDefault++;
  290. if ((pbs.PathTwistBegin != 0) || (pbs.PathTwist != 0))
  291. iPropertiesNotSupportedDefault++;
  292. if ((pbs.ProfileBegin != 0) || pbs.ProfileEnd != 0)
  293. iPropertiesNotSupportedDefault++;
  294. if ((pbs.PathScaleX != 100) || (pbs.PathScaleY != 100))
  295. iPropertiesNotSupportedDefault++;
  296. if ((pbs.PathShearX != 0) || (pbs.PathShearY != 0))
  297. iPropertiesNotSupportedDefault++;
  298. if (pbs.ProfileShape == ProfileShape.Circle && pbs.PathCurve == (byte)Extrusion.Straight)
  299. iPropertiesNotSupportedDefault++;
  300. if (pbs.ProfileShape == ProfileShape.HalfCircle && pbs.PathCurve == (byte)Extrusion.Curve1 && (pbs.Scale.X != pbs.Scale.Y || pbs.Scale.Y != pbs.Scale.Z || pbs.Scale.Z != pbs.Scale.X))
  301. iPropertiesNotSupportedDefault++;
  302. if (pbs.ProfileShape == ProfileShape.HalfCircle && pbs.PathCurve == (byte)Extrusion.Curve1)
  303. iPropertiesNotSupportedDefault++;
  304. // test for torus
  305. if ((pbs.ProfileCurve & 0x07) == (byte)ProfileShape.Square)
  306. {
  307. if (pbs.PathCurve == (byte)Extrusion.Curve1)
  308. {
  309. iPropertiesNotSupportedDefault++;
  310. }
  311. }
  312. else if ((pbs.ProfileCurve & 0x07) == (byte)ProfileShape.Circle)
  313. {
  314. if (pbs.PathCurve == (byte)Extrusion.Straight)
  315. {
  316. iPropertiesNotSupportedDefault++;
  317. }
  318. // ProfileCurve seems to combine hole shape and profile curve so we need to only compare against the lower 3 bits
  319. else if (pbs.PathCurve == (byte)Extrusion.Curve1)
  320. {
  321. iPropertiesNotSupportedDefault++;
  322. }
  323. }
  324. else if ((pbs.ProfileCurve & 0x07) == (byte)ProfileShape.HalfCircle)
  325. {
  326. if (pbs.PathCurve == (byte)Extrusion.Curve1 || pbs.PathCurve == (byte)Extrusion.Curve2)
  327. {
  328. iPropertiesNotSupportedDefault++;
  329. }
  330. }
  331. else if ((pbs.ProfileCurve & 0x07) == (byte)ProfileShape.EquilateralTriangle)
  332. {
  333. if (pbs.PathCurve == (byte)Extrusion.Straight)
  334. {
  335. iPropertiesNotSupportedDefault++;
  336. }
  337. else if (pbs.PathCurve == (byte)Extrusion.Curve1)
  338. {
  339. iPropertiesNotSupportedDefault++;
  340. }
  341. }
  342. if (iPropertiesNotSupportedDefault == 0)
  343. {
  344. return false;
  345. }
  346. return true;
  347. }
  348. // see if we need a mesh and if so if we have a cached one
  349. // called with a new repData
  350. public void CheckMesh(ODEPhysRepData repData)
  351. {
  352. PhysicsActor actor = repData.actor;
  353. PrimitiveBaseShape pbs = repData.pbs;
  354. if (!needsMeshing(repData))
  355. {
  356. repData.meshState = MeshState.noNeed;
  357. repData.hasOBB = false;
  358. return;
  359. }
  360. IMesh mesh = null;
  361. Vector3 size = repData.size;
  362. int clod = (int)LevelOfDetail.High;
  363. byte shapetype = repData.shapetype;
  364. bool convex = shapetype == 2;
  365. mesh = m_mesher.GetMesh(actor.Name, pbs, size, clod, true, convex);
  366. if (mesh == null)
  367. {
  368. if (pbs.SculptEntry)
  369. {
  370. if (pbs.SculptTexture != null && pbs.SculptTexture != UUID.Zero)
  371. {
  372. repData.assetID = pbs.SculptTexture;
  373. repData.meshState = MeshState.needAsset;
  374. }
  375. else
  376. repData.meshState = MeshState.MeshFailed;
  377. return;
  378. }
  379. else
  380. {
  381. repData.meshState = MeshState.needMesh;
  382. mesh = m_mesher.CreateMesh(actor.Name, pbs, size, clod, true, convex, true);
  383. if (mesh == null)
  384. {
  385. repData.meshState = MeshState.MeshFailed;
  386. return;
  387. }
  388. }
  389. }
  390. repData.meshState = MeshState.AssetOK;
  391. repData.mesh = mesh;
  392. repData.OBB = mesh.GetOBB();
  393. repData.OBBOffset = mesh.GetCentroid();
  394. repData.hasOBB = true;
  395. if (pbs.SculptEntry)
  396. {
  397. repData.assetID = pbs.SculptTexture;
  398. }
  399. pbs.SculptData = Utils.EmptyBytes;
  400. return ;
  401. }
  402. public void GetMesh(ODEPhysRepData repData)
  403. {
  404. PhysicsActor actor = repData.actor;
  405. PrimitiveBaseShape pbs = repData.pbs;
  406. repData.mesh = null;
  407. repData.hasOBB = false;
  408. if (!needsMeshing(repData))
  409. {
  410. repData.meshState = MeshState.noNeed;
  411. return;
  412. }
  413. if (repData.meshState == MeshState.MeshFailed)
  414. return;
  415. if (pbs.SculptEntry)
  416. {
  417. if (repData.meshState == MeshState.AssetFailed)
  418. {
  419. if (pbs.SculptTexture == repData.assetID)
  420. return;
  421. }
  422. }
  423. repData.meshState = MeshState.noNeed;
  424. IMesh mesh = null;
  425. Vector3 size = repData.size;
  426. byte shapetype = repData.shapetype;
  427. bool convex;
  428. int clod = (int)LevelOfDetail.High;
  429. if (shapetype == 0)
  430. convex = false;
  431. else
  432. {
  433. convex = true;
  434. if (pbs.SculptType != (byte)SculptType.Mesh)
  435. clod = (int)LevelOfDetail.Low;
  436. }
  437. mesh = m_mesher.CreateMesh(actor.Name, pbs, size, clod, true, convex, true);
  438. if (mesh == null)
  439. {
  440. if (pbs.SculptEntry)
  441. {
  442. if (pbs.SculptTexture == UUID.Zero)
  443. return;
  444. repData.assetID = pbs.SculptTexture;
  445. if (pbs.SculptData == null || pbs.SculptData.Length == 0)
  446. {
  447. repData.meshState = MeshState.needAsset;
  448. return;
  449. }
  450. }
  451. }
  452. repData.mesh = mesh;
  453. repData.pbs.SculptData = Utils.EmptyBytes;
  454. if (mesh == null)
  455. {
  456. if (pbs.SculptEntry)
  457. repData.meshState = MeshState.AssetFailed;
  458. else
  459. repData.meshState = MeshState.MeshFailed;
  460. return;
  461. }
  462. repData.meshState = MeshState.AssetOK;
  463. return;
  464. }
  465. private void CalculateBasicPrimVolume(ODEPhysRepData repData)
  466. {
  467. Vector3 _size = repData.size;
  468. float volume = _size.X * _size.Y * _size.Z; // default
  469. if(repData.isTooSmall)
  470. {
  471. repData.volume = volume;
  472. return;
  473. }
  474. PrimitiveBaseShape _pbs = repData.pbs;
  475. float tmp;
  476. float hollowAmount = (float)_pbs.ProfileHollow * 2.0e-5f;
  477. float hollowVolume = hollowAmount * hollowAmount;
  478. switch (_pbs.ProfileShape)
  479. {
  480. case ProfileShape.Square:
  481. // default box
  482. if (_pbs.PathCurve == (byte)Extrusion.Straight)
  483. {
  484. if (hollowAmount > 0.0)
  485. {
  486. switch (_pbs.HollowShape)
  487. {
  488. case HollowShape.Square:
  489. case HollowShape.Same:
  490. break;
  491. case HollowShape.Circle:
  492. hollowVolume *= 0.78539816339f;
  493. break;
  494. case HollowShape.Triangle:
  495. hollowVolume *= (0.5f * .5f);
  496. break;
  497. default:
  498. hollowVolume = 0;
  499. break;
  500. }
  501. volume *= (1.0f - hollowVolume);
  502. }
  503. }
  504. else if (_pbs.PathCurve == (byte)Extrusion.Curve1)
  505. {
  506. //a tube
  507. volume *= 0.78539816339e-2f * (float)(200 - _pbs.PathScaleX);
  508. tmp = 1.0f - 2.0e-2f * (float)(200 - _pbs.PathScaleY);
  509. volume -= volume * tmp * tmp;
  510. if (hollowAmount > 0.0)
  511. {
  512. hollowVolume *= hollowAmount;
  513. switch (_pbs.HollowShape)
  514. {
  515. case HollowShape.Square:
  516. case HollowShape.Same:
  517. break;
  518. case HollowShape.Circle:
  519. hollowVolume *= 0.78539816339f;
  520. break;
  521. case HollowShape.Triangle:
  522. hollowVolume *= 0.5f * 0.5f;
  523. break;
  524. default:
  525. hollowVolume = 0;
  526. break;
  527. }
  528. volume *= (1.0f - hollowVolume);
  529. }
  530. }
  531. break;
  532. case ProfileShape.Circle:
  533. if (_pbs.PathCurve == (byte)Extrusion.Straight)
  534. {
  535. volume *= 0.78539816339f; // elipse base
  536. if (hollowAmount > 0.0)
  537. {
  538. switch (_pbs.HollowShape)
  539. {
  540. case HollowShape.Same:
  541. case HollowShape.Circle:
  542. break;
  543. case HollowShape.Square:
  544. hollowVolume *= 0.5f * 2.5984480504799f;
  545. break;
  546. case HollowShape.Triangle:
  547. hollowVolume *= .5f * 1.27323954473516f;
  548. break;
  549. default:
  550. hollowVolume = 0;
  551. break;
  552. }
  553. volume *= (1.0f - hollowVolume);
  554. }
  555. }
  556. else if (_pbs.PathCurve == (byte)Extrusion.Curve1)
  557. {
  558. volume *= 0.61685027506808491367715568749226e-2f * (float)(200 - _pbs.PathScaleX);
  559. tmp = 1.0f - .02f * (float)(200 - _pbs.PathScaleY);
  560. volume *= (1.0f - tmp * tmp);
  561. if (hollowAmount > 0.0)
  562. {
  563. // calculate the hollow volume by it's shape compared to the prim shape
  564. hollowVolume *= hollowAmount;
  565. switch (_pbs.HollowShape)
  566. {
  567. case HollowShape.Same:
  568. case HollowShape.Circle:
  569. break;
  570. case HollowShape.Square:
  571. hollowVolume *= 0.5f * 2.5984480504799f;
  572. break;
  573. case HollowShape.Triangle:
  574. hollowVolume *= .5f * 1.27323954473516f;
  575. break;
  576. default:
  577. hollowVolume = 0;
  578. break;
  579. }
  580. volume *= (1.0f - hollowVolume);
  581. }
  582. }
  583. break;
  584. case ProfileShape.HalfCircle:
  585. if (_pbs.PathCurve == (byte)Extrusion.Curve1)
  586. {
  587. volume *= 0.5236f;
  588. if (hollowAmount > 0.0)
  589. {
  590. hollowVolume *= hollowAmount;
  591. switch (_pbs.HollowShape)
  592. {
  593. case HollowShape.Circle:
  594. case HollowShape.Triangle: // diference in sl is minor and odd
  595. case HollowShape.Same:
  596. break;
  597. case HollowShape.Square:
  598. hollowVolume *= 0.909f;
  599. break;
  600. // case HollowShape.Triangle:
  601. // hollowVolume *= .827f;
  602. // break;
  603. default:
  604. hollowVolume = 0;
  605. break;
  606. }
  607. volume *= (1.0f - hollowVolume);
  608. }
  609. }
  610. break;
  611. case ProfileShape.EquilateralTriangle:
  612. if (_pbs.PathCurve == (byte)Extrusion.Straight)
  613. {
  614. volume *= 0.32475953f;
  615. if (hollowAmount > 0.0)
  616. {
  617. // calculate the hollow volume by it's shape compared to the prim shape
  618. switch (_pbs.HollowShape)
  619. {
  620. case HollowShape.Same:
  621. case HollowShape.Triangle:
  622. hollowVolume *= .25f;
  623. break;
  624. case HollowShape.Square:
  625. hollowVolume *= 0.499849f * 3.07920140172638f;
  626. break;
  627. case HollowShape.Circle:
  628. // Hollow shape is a perfect cyllinder in respect to the cube's scale
  629. // Cyllinder hollow volume calculation
  630. hollowVolume *= 0.1963495f * 3.07920140172638f;
  631. break;
  632. default:
  633. hollowVolume = 0;
  634. break;
  635. }
  636. volume *= (1.0f - hollowVolume);
  637. }
  638. }
  639. else if (_pbs.PathCurve == (byte)Extrusion.Curve1)
  640. {
  641. volume *= 0.32475953f;
  642. volume *= 0.01f * (float)(200 - _pbs.PathScaleX);
  643. tmp = 1.0f - .02f * (float)(200 - _pbs.PathScaleY);
  644. volume *= (1.0f - tmp * tmp);
  645. if (hollowAmount > 0.0)
  646. {
  647. hollowVolume *= hollowAmount;
  648. switch (_pbs.HollowShape)
  649. {
  650. case HollowShape.Same:
  651. case HollowShape.Triangle:
  652. hollowVolume *= .25f;
  653. break;
  654. case HollowShape.Square:
  655. hollowVolume *= 0.499849f * 3.07920140172638f;
  656. break;
  657. case HollowShape.Circle:
  658. hollowVolume *= 0.1963495f * 3.07920140172638f;
  659. break;
  660. default:
  661. hollowVolume = 0;
  662. break;
  663. }
  664. volume *= (1.0f - hollowVolume);
  665. }
  666. }
  667. break;
  668. default:
  669. break;
  670. }
  671. float taperX1;
  672. float taperY1;
  673. float taperX;
  674. float taperY;
  675. float pathBegin;
  676. float pathEnd;
  677. float profileBegin;
  678. float profileEnd;
  679. if (_pbs.PathCurve == (byte)Extrusion.Straight || _pbs.PathCurve == (byte)Extrusion.Flexible)
  680. {
  681. taperX1 = _pbs.PathScaleX * 0.01f;
  682. if (taperX1 > 1.0f)
  683. taperX1 = 2.0f - taperX1;
  684. taperX = 1.0f - taperX1;
  685. taperY1 = _pbs.PathScaleY * 0.01f;
  686. if (taperY1 > 1.0f)
  687. taperY1 = 2.0f - taperY1;
  688. taperY = 1.0f - taperY1;
  689. }
  690. else
  691. {
  692. taperX = _pbs.PathTaperX * 0.01f;
  693. if (taperX < 0.0f)
  694. taperX = -taperX;
  695. taperX1 = 1.0f - taperX;
  696. taperY = _pbs.PathTaperY * 0.01f;
  697. if (taperY < 0.0f)
  698. taperY = -taperY;
  699. taperY1 = 1.0f - taperY;
  700. }
  701. volume *= (taperX1 * taperY1 + 0.5f * (taperX1 * taperY + taperX * taperY1) + 0.3333333333f * taperX * taperY);
  702. pathBegin = (float)_pbs.PathBegin * 2.0e-5f;
  703. pathEnd = 1.0f - (float)_pbs.PathEnd * 2.0e-5f;
  704. volume *= (pathEnd - pathBegin);
  705. // this is crude aproximation
  706. profileBegin = (float)_pbs.ProfileBegin * 2.0e-5f;
  707. profileEnd = 1.0f - (float)_pbs.ProfileEnd * 2.0e-5f;
  708. volume *= (profileEnd - profileBegin);
  709. repData.volume = volume;
  710. }
  711. private void CalcVolumeData(ODEPhysRepData repData)
  712. {
  713. if (repData.hasOBB)
  714. {
  715. Vector3 OBB = repData.OBB;
  716. }
  717. else
  718. {
  719. Vector3 OBB = repData.size;
  720. OBB.X *= 0.5f;
  721. OBB.Y *= 0.5f;
  722. OBB.Z *= 0.5f;
  723. repData.OBB = OBB;
  724. repData.OBBOffset = Vector3.Zero;
  725. }
  726. CalculateBasicPrimVolume(repData);
  727. }
  728. }
  729. public class ODEAssetRequest
  730. {
  731. ODEMeshWorker m_worker;
  732. private ILog m_log;
  733. ODEPhysRepData repData;
  734. public ODEAssetRequest(ODEMeshWorker pWorker, RequestAssetDelegate provider,
  735. ODEPhysRepData pRepData, ILog plog)
  736. {
  737. m_worker = pWorker;
  738. m_log = plog;
  739. repData = pRepData;
  740. repData.meshState = MeshState.AssetFailed;
  741. if (provider == null)
  742. return;
  743. if (repData.assetID == null)
  744. return;
  745. UUID assetID = (UUID) repData.assetID;
  746. if (assetID == UUID.Zero)
  747. return;
  748. repData.meshState = MeshState.loadingAsset;
  749. provider(assetID, ODEassetReceived);
  750. }
  751. void ODEassetReceived(AssetBase asset)
  752. {
  753. repData.meshState = MeshState.AssetFailed;
  754. if (asset != null)
  755. {
  756. if (asset.Data != null && asset.Data.Length > 0)
  757. {
  758. repData.meshState = MeshState.noNeed;
  759. if (!repData.pbs.SculptEntry)
  760. return;
  761. if (repData.pbs.SculptTexture != repData.assetID)
  762. return;
  763. // repData.pbs.SculptData = new byte[asset.Data.Length];
  764. // asset.Data.CopyTo(repData.pbs.SculptData,0);
  765. repData.pbs.SculptData = asset.Data;
  766. repData.meshState = MeshState.AssetOK;
  767. m_worker.AssetLoaded(repData);
  768. }
  769. else
  770. m_log.WarnFormat("[PHYSICS]: asset provider returned invalid mesh data for prim {0} asset UUID {1}.",
  771. repData.actor.Name, asset.ID.ToString());
  772. }
  773. else
  774. m_log.WarnFormat("[PHYSICS]: asset provider returned null asset for mesh of prim {0}.",
  775. repData.actor.Name);
  776. }
  777. }
  778. }