MeshCost.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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 copyright
  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.IO;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.Text;
  32. using OpenMetaverse;
  33. using OpenMetaverse.StructuredData;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Framework;
  36. using OpenSim.Region.Framework.Scenes;
  37. using OpenSim.Framework.Capabilities;
  38. using ComponentAce.Compression.Libs.zlib;
  39. using OSDArray = OpenMetaverse.StructuredData.OSDArray;
  40. using OSDMap = OpenMetaverse.StructuredData.OSDMap;
  41. using Nini.Config;
  42. namespace OpenSim.Region.ClientStack.Linden
  43. {
  44. public struct ModelPrimLimits
  45. {
  46. }
  47. public class ModelCost
  48. {
  49. // upload fee defaults
  50. // fees are normalized to 1.0
  51. // this parameters scale them to basic cost ( so 1.0 translates to 10 )
  52. public float ModelMeshCostFactor = 0.0f; // scale total cost relative to basic (excluding textures)
  53. public float ModelTextureCostFactor = 1.0f; // scale textures fee to basic.
  54. public float ModelMinCostFactor = 0.0f; // 0.5f; // minimum total model free excluding textures
  55. // itens costs in normalized values
  56. // ie will be multiplied by basicCost and factors above
  57. public float primCreationCost = 0.002f; // extra cost for each prim creation overhead
  58. // weigthed size to normalized cost
  59. public float bytecost = 1e-5f;
  60. // mesh upload fees based on compressed data sizes
  61. // several data sections are counted more that once
  62. // to promote user optimization
  63. // following parameters control how many extra times they are added
  64. // to global size.
  65. // LOD meshs
  66. const float medSizeWth = 1f; // 2x
  67. const float lowSizeWth = 1.5f; // 2.5x
  68. const float lowestSizeWth = 2f; // 3x
  69. // favor potencially physical optimized meshs versus automatic decomposition
  70. const float physMeshSizeWth = 6f; // counts 7x
  71. const float physHullSizeWth = 8f; // counts 9x
  72. // stream cost area factors
  73. // more or less like SL
  74. const float highLodFactor = 17.36f;
  75. const float midLodFactor = 277.78f;
  76. const float lowLodFactor = 1111.11f;
  77. // physics cost is below, identical to SL, assuming shape type convex
  78. // server cost is below identical to SL assuming non scripted non physical object
  79. // internal
  80. const int bytesPerCoord = 6; // 3 coords, 2 bytes per each
  81. // control prims dimensions
  82. public float PrimScaleMin = 0.001f;
  83. public float NonPhysicalPrimScaleMax = 256f;
  84. public float PhysicalPrimScaleMax = 10f;
  85. public int ObjectLinkedPartsMax = 512;
  86. public ModelCost(Scene scene)
  87. {
  88. PrimScaleMin = scene.m_minNonphys;
  89. NonPhysicalPrimScaleMax = scene.m_maxNonphys;
  90. PhysicalPrimScaleMax = scene.m_maxPhys;
  91. ObjectLinkedPartsMax = scene.m_linksetCapacity;
  92. }
  93. public void Econfig(IConfig EconomyConfig)
  94. {
  95. ModelMeshCostFactor = EconomyConfig.GetFloat("MeshModelUploadCostFactor", ModelMeshCostFactor);
  96. ModelTextureCostFactor = EconomyConfig.GetFloat("MeshModelUploadTextureCostFactor", ModelTextureCostFactor);
  97. ModelMinCostFactor = EconomyConfig.GetFloat("MeshModelMinCostFactor", ModelMinCostFactor);
  98. // next 2 are normalized so final cost is afected by modelUploadFactor above and normal cost
  99. primCreationCost = EconomyConfig.GetFloat("ModelPrimCreationCost", primCreationCost);
  100. bytecost = EconomyConfig.GetFloat("ModelMeshByteCost", bytecost);
  101. }
  102. // storage for a single mesh asset cost parameters
  103. private class ameshCostParam
  104. {
  105. // LOD sizes for size dependent streaming cost
  106. public int highLODSize;
  107. public int medLODSize;
  108. public int lowLODSize;
  109. public int lowestLODSize;
  110. // normalized fee based on compressed data sizes
  111. public float costFee;
  112. // physics cost
  113. public float physicsCost;
  114. }
  115. // calculates a mesh model costs
  116. // returns false on error, with a reason on parameter error
  117. // resources input LLSD request
  118. // basicCost input region assets upload cost
  119. // totalcost returns model total upload fee
  120. // meshcostdata returns detailed costs for viewer
  121. // avatarSkeleton if mesh includes a avatar skeleton
  122. // useAvatarCollider if we should use physics mesh for avatar
  123. public bool MeshModelCost(LLSDAssetResource resources, int basicCost, out int totalcost,
  124. LLSDAssetUploadResponseData meshcostdata, out string error, ref string warning)
  125. {
  126. totalcost = 0;
  127. error = string.Empty;
  128. bool avatarSkeleton = false;
  129. if (resources == null ||
  130. resources.instance_list == null ||
  131. resources.instance_list.Array.Count == 0)
  132. {
  133. error = "missing model information.";
  134. return false;
  135. }
  136. int numberInstances = resources.instance_list.Array.Count;
  137. if (ObjectLinkedPartsMax != 0 && numberInstances > ObjectLinkedPartsMax)
  138. {
  139. error = "Model would have more than " + ObjectLinkedPartsMax.ToString() + " linked prims";
  140. return false;
  141. }
  142. meshcostdata.model_streaming_cost = 0.0;
  143. meshcostdata.simulation_cost = 0.0;
  144. meshcostdata.physics_cost = 0.0;
  145. meshcostdata.resource_cost = 0.0;
  146. meshcostdata.upload_price_breakdown.mesh_instance = 0;
  147. meshcostdata.upload_price_breakdown.mesh_physics = 0;
  148. meshcostdata.upload_price_breakdown.mesh_streaming = 0;
  149. meshcostdata.upload_price_breakdown.model = 0;
  150. int itmp;
  151. // textures cost
  152. if (resources.texture_list != null && resources.texture_list.Array.Count > 0)
  153. {
  154. float textures_cost = (float)(resources.texture_list.Array.Count * basicCost);
  155. textures_cost *= ModelTextureCostFactor;
  156. itmp = (int)(textures_cost + 0.5f); // round
  157. meshcostdata.upload_price_breakdown.texture = itmp;
  158. totalcost += itmp;
  159. }
  160. // meshs assets cost
  161. float meshsfee = 0;
  162. int numberMeshs = 0;
  163. bool haveMeshs = false;
  164. bool curskeleton;
  165. bool curAvatarPhys;
  166. List<ameshCostParam> meshsCosts = new List<ameshCostParam>();
  167. if (resources.mesh_list != null && resources.mesh_list.Array.Count > 0)
  168. {
  169. numberMeshs = resources.mesh_list.Array.Count;
  170. for (int i = 0; i < numberMeshs; i++)
  171. {
  172. ameshCostParam curCost = new ameshCostParam();
  173. byte[] data = (byte[])resources.mesh_list.Array[i];
  174. if (!MeshCost(data, curCost,out curskeleton, out curAvatarPhys, out error))
  175. {
  176. return false;
  177. }
  178. if (curskeleton)
  179. {
  180. if (avatarSkeleton)
  181. {
  182. error = "model can only contain a avatar skeleton";
  183. return false;
  184. }
  185. avatarSkeleton = true;
  186. }
  187. meshsCosts.Add(curCost);
  188. meshsfee += curCost.costFee;
  189. }
  190. haveMeshs = true;
  191. }
  192. // instances (prims) cost
  193. int mesh;
  194. int skipedSmall = 0;
  195. for (int i = 0; i < numberInstances; i++)
  196. {
  197. Hashtable inst = (Hashtable)resources.instance_list.Array[i];
  198. ArrayList ascale = (ArrayList)inst["scale"];
  199. Vector3 scale;
  200. double tmp;
  201. tmp = (double)ascale[0];
  202. scale.X = (float)tmp;
  203. tmp = (double)ascale[1];
  204. scale.Y = (float)tmp;
  205. tmp = (double)ascale[2];
  206. scale.Z = (float)tmp;
  207. if (scale.X < PrimScaleMin || scale.Y < PrimScaleMin || scale.Z < PrimScaleMin)
  208. {
  209. skipedSmall++;
  210. continue;
  211. }
  212. if (scale.X > NonPhysicalPrimScaleMax || scale.Y > NonPhysicalPrimScaleMax || scale.Z > NonPhysicalPrimScaleMax)
  213. {
  214. error = "Model contains parts with sides larger than " + NonPhysicalPrimScaleMax.ToString() + "m. Please ajust scale";
  215. return false;
  216. }
  217. if (haveMeshs && inst.ContainsKey("mesh"))
  218. {
  219. mesh = (int)inst["mesh"];
  220. if (mesh >= numberMeshs)
  221. {
  222. error = "Incoerent model information.";
  223. return false;
  224. }
  225. // streamming cost
  226. float sqdiam = scale.LengthSquared();
  227. ameshCostParam curCost = meshsCosts[mesh];
  228. float mesh_streaming = streamingCost(curCost, sqdiam);
  229. meshcostdata.model_streaming_cost += mesh_streaming;
  230. meshcostdata.physics_cost += curCost.physicsCost;
  231. }
  232. else // instance as no mesh ??
  233. {
  234. // to do later if needed
  235. meshcostdata.model_streaming_cost += 0.5f;
  236. meshcostdata.physics_cost += 1.0f;
  237. }
  238. // assume unscripted and static prim server cost
  239. meshcostdata.simulation_cost += 0.5f;
  240. // charge for prims creation
  241. meshsfee += primCreationCost;
  242. }
  243. if (skipedSmall > 0)
  244. {
  245. if (skipedSmall > numberInstances / 2)
  246. {
  247. error = "Model contains too many prims smaller than " + PrimScaleMin.ToString() +
  248. "m minimum allowed size. Please check scalling";
  249. return false;
  250. }
  251. else
  252. warning += skipedSmall.ToString() + " of the requested " +numberInstances.ToString() +
  253. " model prims will not upload because they are smaller than " + PrimScaleMin.ToString() +
  254. "m minimum allowed size. Please check scalling ";
  255. }
  256. if (meshcostdata.physics_cost <= meshcostdata.model_streaming_cost)
  257. meshcostdata.resource_cost = meshcostdata.model_streaming_cost;
  258. else
  259. meshcostdata.resource_cost = meshcostdata.physics_cost;
  260. if (meshcostdata.resource_cost < meshcostdata.simulation_cost)
  261. meshcostdata.resource_cost = meshcostdata.simulation_cost;
  262. // scale cost
  263. // at this point a cost of 1.0 whould mean basic cost
  264. meshsfee *= ModelMeshCostFactor;
  265. if (meshsfee < ModelMinCostFactor)
  266. meshsfee = ModelMinCostFactor;
  267. // actually scale it to basic cost
  268. meshsfee *= (float)basicCost;
  269. meshsfee += 0.5f; // rounding
  270. totalcost += (int)meshsfee;
  271. // breakdown prices
  272. // don't seem to be in use so removed code for now
  273. return true;
  274. }
  275. // single mesh asset cost
  276. private bool MeshCost(byte[] data, ameshCostParam cost,out bool skeleton, out bool avatarPhys, out string error)
  277. {
  278. cost.highLODSize = 0;
  279. cost.medLODSize = 0;
  280. cost.lowLODSize = 0;
  281. cost.lowestLODSize = 0;
  282. cost.physicsCost = 0.0f;
  283. cost.costFee = 0.0f;
  284. error = string.Empty;
  285. skeleton = false;
  286. avatarPhys = false;
  287. if (data == null || data.Length == 0)
  288. {
  289. error = "Missing model information.";
  290. return false;
  291. }
  292. OSD meshOsd = null;
  293. int start = 0;
  294. error = "Invalid model data";
  295. using (MemoryStream ms = new MemoryStream(data))
  296. {
  297. try
  298. {
  299. OSD osd = OSDParser.DeserializeLLSDBinary(ms);
  300. if (osd is OSDMap)
  301. meshOsd = (OSDMap)osd;
  302. else
  303. return false;
  304. }
  305. catch
  306. {
  307. return false;
  308. }
  309. start = (int)ms.Position;
  310. }
  311. OSDMap map = (OSDMap)meshOsd;
  312. OSDMap tmpmap;
  313. int highlod_size = 0;
  314. int medlod_size = 0;
  315. int lowlod_size = 0;
  316. int lowestlod_size = 0;
  317. int skin_size = 0;
  318. int hulls_size = 0;
  319. int phys_nhulls;
  320. int phys_hullsvertices = 0;
  321. int physmesh_size = 0;
  322. int phys_ntriangles = 0;
  323. int submesh_offset = -1;
  324. if (map.ContainsKey("skeleton"))
  325. {
  326. tmpmap = (OSDMap)map["skeleton"];
  327. if (tmpmap.ContainsKey("offset") && tmpmap.ContainsKey("size"))
  328. {
  329. int sksize = tmpmap["size"].AsInteger();
  330. if(sksize > 0)
  331. skeleton = true;
  332. }
  333. }
  334. if (map.ContainsKey("physics_convex"))
  335. {
  336. tmpmap = (OSDMap)map["physics_convex"];
  337. if (tmpmap.ContainsKey("offset"))
  338. submesh_offset = tmpmap["offset"].AsInteger() + start;
  339. if (tmpmap.ContainsKey("size"))
  340. hulls_size = tmpmap["size"].AsInteger();
  341. }
  342. if (submesh_offset < 0 || hulls_size == 0)
  343. {
  344. error = "Missing physics_convex block";
  345. return false;
  346. }
  347. if (!hulls(data, submesh_offset, hulls_size, out phys_hullsvertices, out phys_nhulls))
  348. {
  349. error = "Bad physics_convex block";
  350. return false;
  351. }
  352. submesh_offset = -1;
  353. // only look for LOD meshs sizes
  354. if (map.ContainsKey("high_lod"))
  355. {
  356. tmpmap = (OSDMap)map["high_lod"];
  357. // see at least if there is a offset for this one
  358. if (tmpmap.ContainsKey("offset"))
  359. submesh_offset = tmpmap["offset"].AsInteger() + start;
  360. if (tmpmap.ContainsKey("size"))
  361. highlod_size = tmpmap["size"].AsInteger();
  362. }
  363. if (submesh_offset < 0 || highlod_size <= 0)
  364. {
  365. error = "Missing high_lod block";
  366. return false;
  367. }
  368. bool haveprev = true;
  369. if (map.ContainsKey("medium_lod"))
  370. {
  371. tmpmap = (OSDMap)map["medium_lod"];
  372. if (tmpmap.ContainsKey("size"))
  373. medlod_size = tmpmap["size"].AsInteger();
  374. else
  375. haveprev = false;
  376. }
  377. if (haveprev && map.ContainsKey("low_lod"))
  378. {
  379. tmpmap = (OSDMap)map["low_lod"];
  380. if (tmpmap.ContainsKey("size"))
  381. lowlod_size = tmpmap["size"].AsInteger();
  382. else
  383. haveprev = false;
  384. }
  385. if (haveprev && map.ContainsKey("lowest_lod"))
  386. {
  387. tmpmap = (OSDMap)map["lowest_lod"];
  388. if (tmpmap.ContainsKey("size"))
  389. lowestlod_size = tmpmap["size"].AsInteger();
  390. }
  391. if (map.ContainsKey("skin"))
  392. {
  393. tmpmap = (OSDMap)map["skin"];
  394. if (tmpmap.ContainsKey("size"))
  395. skin_size = tmpmap["size"].AsInteger();
  396. }
  397. cost.highLODSize = highlod_size;
  398. cost.medLODSize = medlod_size;
  399. cost.lowLODSize = lowlod_size;
  400. cost.lowestLODSize = lowestlod_size;
  401. submesh_offset = -1;
  402. tmpmap = null;
  403. if(map.ContainsKey("physics_mesh"))
  404. tmpmap = (OSDMap)map["physics_mesh"];
  405. else if (map.ContainsKey("physics_shape")) // old naming
  406. tmpmap = (OSDMap)map["physics_shape"];
  407. if(tmpmap != null)
  408. {
  409. if (tmpmap.ContainsKey("offset"))
  410. submesh_offset = tmpmap["offset"].AsInteger() + start;
  411. if (tmpmap.ContainsKey("size"))
  412. physmesh_size = tmpmap["size"].AsInteger();
  413. if (submesh_offset >= 0 || physmesh_size > 0)
  414. {
  415. if (!submesh(data, submesh_offset, physmesh_size, out phys_ntriangles))
  416. {
  417. error = "Model data parsing error";
  418. return false;
  419. }
  420. }
  421. }
  422. // upload is done in convex shape type so only one hull
  423. phys_hullsvertices++;
  424. cost.physicsCost = 0.04f * phys_hullsvertices;
  425. float sfee;
  426. sfee = data.Length; // start with total compressed data size
  427. // penalize lod meshs that should be more builder optimized
  428. sfee += medSizeWth * medlod_size;
  429. sfee += lowSizeWth * lowlod_size;
  430. sfee += lowestSizeWth * lowlod_size;
  431. // physics
  432. // favor potencial optimized meshs versus automatic decomposition
  433. if (physmesh_size != 0)
  434. sfee += physMeshSizeWth * (physmesh_size + hulls_size / 4); // reduce cost of mandatory convex hull
  435. else
  436. sfee += physHullSizeWth * hulls_size;
  437. // bytes to money
  438. sfee *= bytecost;
  439. cost.costFee = sfee;
  440. return true;
  441. }
  442. // parses a LOD or physics mesh component
  443. private bool submesh(byte[] data, int offset, int size, out int ntriangles)
  444. {
  445. ntriangles = 0;
  446. OSD decodedMeshOsd = new OSD();
  447. byte[] meshBytes = new byte[size];
  448. System.Buffer.BlockCopy(data, offset, meshBytes, 0, size);
  449. try
  450. {
  451. using (MemoryStream inMs = new MemoryStream(meshBytes))
  452. {
  453. using (MemoryStream outMs = new MemoryStream())
  454. {
  455. using (ZOutputStream zOut = new ZOutputStream(outMs))
  456. {
  457. byte[] readBuffer = new byte[4096];
  458. int readLen = 0;
  459. while ((readLen = inMs.Read(readBuffer, 0, readBuffer.Length)) > 0)
  460. {
  461. zOut.Write(readBuffer, 0, readLen);
  462. }
  463. zOut.Flush();
  464. outMs.Seek(0, SeekOrigin.Begin);
  465. byte[] decompressedBuf = outMs.GetBuffer();
  466. decodedMeshOsd = OSDParser.DeserializeLLSDBinary(decompressedBuf);
  467. }
  468. }
  469. }
  470. }
  471. catch
  472. {
  473. return false;
  474. }
  475. OSDArray decodedMeshOsdArray = null;
  476. byte[] dummy;
  477. decodedMeshOsdArray = (OSDArray)decodedMeshOsd;
  478. foreach (OSD subMeshOsd in decodedMeshOsdArray)
  479. {
  480. if (subMeshOsd is OSDMap)
  481. {
  482. OSDMap subtmpmap = (OSDMap)subMeshOsd;
  483. if (subtmpmap.ContainsKey("NoGeometry") && ((OSDBoolean)subtmpmap["NoGeometry"]))
  484. continue;
  485. if (!subtmpmap.ContainsKey("Position"))
  486. return false;
  487. if (subtmpmap.ContainsKey("TriangleList"))
  488. {
  489. dummy = subtmpmap["TriangleList"].AsBinary();
  490. ntriangles += dummy.Length / bytesPerCoord;
  491. }
  492. else
  493. return false;
  494. }
  495. }
  496. return true;
  497. }
  498. // parses convex hulls component
  499. private bool hulls(byte[] data, int offset, int size, out int nvertices, out int nhulls)
  500. {
  501. nvertices = 0;
  502. nhulls = 1;
  503. OSD decodedMeshOsd = new OSD();
  504. byte[] meshBytes = new byte[size];
  505. System.Buffer.BlockCopy(data, offset, meshBytes, 0, size);
  506. try
  507. {
  508. using (MemoryStream inMs = new MemoryStream(meshBytes))
  509. {
  510. using (MemoryStream outMs = new MemoryStream())
  511. {
  512. using (ZOutputStream zOut = new ZOutputStream(outMs))
  513. {
  514. byte[] readBuffer = new byte[4096];
  515. int readLen = 0;
  516. while ((readLen = inMs.Read(readBuffer, 0, readBuffer.Length)) > 0)
  517. {
  518. zOut.Write(readBuffer, 0, readLen);
  519. }
  520. zOut.Flush();
  521. outMs.Seek(0, SeekOrigin.Begin);
  522. byte[] decompressedBuf = outMs.GetBuffer();
  523. decodedMeshOsd = OSDParser.DeserializeLLSDBinary(decompressedBuf);
  524. }
  525. }
  526. }
  527. }
  528. catch
  529. {
  530. return false;
  531. }
  532. OSDMap cmap = (OSDMap)decodedMeshOsd;
  533. if (cmap == null)
  534. return false;
  535. byte[] dummy;
  536. // must have one of this
  537. if (cmap.ContainsKey("BoundingVerts"))
  538. {
  539. dummy = cmap["BoundingVerts"].AsBinary();
  540. nvertices = dummy.Length / bytesPerCoord;
  541. }
  542. else
  543. return false;
  544. /* upload is done with convex shape type
  545. if (cmap.ContainsKey("HullList"))
  546. {
  547. dummy = cmap["HullList"].AsBinary();
  548. nhulls += dummy.Length;
  549. }
  550. if (cmap.ContainsKey("Positions"))
  551. {
  552. dummy = cmap["Positions"].AsBinary();
  553. nvertices = dummy.Length / bytesPerCoord;
  554. }
  555. */
  556. return true;
  557. }
  558. // returns streaming cost from on mesh LODs sizes in curCost and square of prim size length
  559. private float streamingCost(ameshCostParam curCost, float sqdiam)
  560. {
  561. // compute efective areas
  562. float ma = 262144f;
  563. float mh = sqdiam * highLodFactor;
  564. if (mh > ma)
  565. mh = ma;
  566. float mm = sqdiam * midLodFactor;
  567. if (mm > ma)
  568. mm = ma;
  569. float ml = sqdiam * lowLodFactor;
  570. if (ml > ma)
  571. ml = ma;
  572. float mlst = ma;
  573. mlst -= ml;
  574. ml -= mm;
  575. mm -= mh;
  576. if (mlst < 1.0f)
  577. mlst = 1.0f;
  578. if (ml < 1.0f)
  579. ml = 1.0f;
  580. if (mm < 1.0f)
  581. mm = 1.0f;
  582. if (mh < 1.0f)
  583. mh = 1.0f;
  584. ma = mlst + ml + mm + mh;
  585. // get LODs compressed sizes
  586. // giving 384 bytes bonus
  587. int lst = curCost.lowestLODSize - 384;
  588. int l = curCost.lowLODSize - 384;
  589. int m = curCost.medLODSize - 384;
  590. int h = curCost.highLODSize - 384;
  591. // use previus higher LOD size on missing ones
  592. if (m <= 0)
  593. m = h;
  594. if (l <= 0)
  595. l = m;
  596. if (lst <= 0)
  597. lst = l;
  598. // force minumum sizes
  599. if (lst < 16)
  600. lst = 16;
  601. if (l < 16)
  602. l = 16;
  603. if (m < 16)
  604. m = 16;
  605. if (h < 16)
  606. h = 16;
  607. // compute cost weighted by relative effective areas
  608. float cost = (float)lst * mlst + (float)l * ml + (float)m * mm + (float)h * mh;
  609. cost /= ma;
  610. cost *= 0.004f; // overall tunning parameter
  611. return cost;
  612. }
  613. }
  614. }