ODEMeshWorker.cs 31 KB

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