MeshCost.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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 System.IO.Compression;
  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. public int highLODsides;
  111. // normalized fee based on compressed data sizes
  112. public float costFee;
  113. // physics cost
  114. public float physicsCost;
  115. }
  116. // calculates a mesh model costs
  117. // returns false on error, with a reason on parameter error
  118. // resources input LLSD request
  119. // basicCost input region assets upload cost
  120. // totalcost returns model total upload fee
  121. // meshcostdata returns detailed costs for viewer
  122. // avatarSkeleton if mesh includes a avatar skeleton
  123. // useAvatarCollider if we should use physics mesh for avatar
  124. public bool MeshModelCost(LLSDAssetResource resources, int basicCost, out int totalcost,
  125. LLSDAssetUploadResponseData meshcostdata, out string error, ref string warning, out int[] meshesSides)
  126. {
  127. totalcost = 0;
  128. error = string.Empty;
  129. meshesSides = null;
  130. bool avatarSkeleton = false;
  131. if (resources == null ||
  132. resources.instance_list == null ||
  133. resources.instance_list.Array.Count == 0)
  134. {
  135. error = "missing model information.";
  136. return false;
  137. }
  138. int numberInstances = resources.instance_list.Array.Count;
  139. if (ObjectLinkedPartsMax != 0 && numberInstances > ObjectLinkedPartsMax)
  140. {
  141. error = "Model would have more than " + ObjectLinkedPartsMax.ToString() + " linked prims";
  142. return false;
  143. }
  144. meshcostdata.model_streaming_cost = 0.0;
  145. meshcostdata.simulation_cost = 0.0;
  146. meshcostdata.physics_cost = 0.0;
  147. meshcostdata.resource_cost = 0.0;
  148. meshcostdata.upload_price_breakdown.mesh_instance = 0;
  149. meshcostdata.upload_price_breakdown.mesh_physics = 0;
  150. meshcostdata.upload_price_breakdown.mesh_streaming = 0;
  151. meshcostdata.upload_price_breakdown.model = 0;
  152. int itmp;
  153. // textures cost
  154. if (resources.texture_list != null && resources.texture_list.Array.Count > 0)
  155. {
  156. float textures_cost = (float)(resources.texture_list.Array.Count * basicCost);
  157. textures_cost *= ModelTextureCostFactor;
  158. itmp = (int)(textures_cost + 0.5f); // round
  159. meshcostdata.upload_price_breakdown.texture = itmp;
  160. totalcost += itmp;
  161. }
  162. // meshs assets cost
  163. float meshsfee = 0;
  164. int numberMeshs = 0;
  165. bool haveMeshs = false;
  166. bool curskeleton;
  167. bool curAvatarPhys;
  168. List<ameshCostParam> meshsCosts = new List<ameshCostParam>();
  169. if (resources.mesh_list != null && resources.mesh_list.Array.Count > 0)
  170. {
  171. numberMeshs = resources.mesh_list.Array.Count;
  172. meshesSides = new int[numberMeshs];
  173. for (int i = 0; i < numberMeshs; i++)
  174. {
  175. ameshCostParam curCost = new ameshCostParam();
  176. byte[] data = (byte[])resources.mesh_list.Array[i];
  177. if (!MeshCost(data, curCost, out curskeleton, out curAvatarPhys, out error))
  178. {
  179. return false;
  180. }
  181. if (curskeleton)
  182. {
  183. if (avatarSkeleton)
  184. {
  185. error = "model can only contain a avatar skeleton";
  186. return false;
  187. }
  188. avatarSkeleton = true;
  189. }
  190. meshsCosts.Add(curCost);
  191. meshsfee += curCost.costFee;
  192. meshesSides[i] = curCost.highLODsides;
  193. }
  194. haveMeshs = true;
  195. }
  196. // instances (prims) cost
  197. int mesh;
  198. int skipedSmall = 0;
  199. for (int i = 0; i < numberInstances; i++)
  200. {
  201. Hashtable inst = (Hashtable)resources.instance_list.Array[i];
  202. ArrayList ascale = (ArrayList)inst["scale"];
  203. Vector3 scale;
  204. double tmp;
  205. tmp = (double)ascale[0];
  206. scale.X = (float)tmp;
  207. tmp = (double)ascale[1];
  208. scale.Y = (float)tmp;
  209. tmp = (double)ascale[2];
  210. scale.Z = (float)tmp;
  211. if (scale.X < PrimScaleMin || scale.Y < PrimScaleMin || scale.Z < PrimScaleMin)
  212. {
  213. skipedSmall++;
  214. continue;
  215. }
  216. if (scale.X > NonPhysicalPrimScaleMax || scale.Y > NonPhysicalPrimScaleMax || scale.Z > NonPhysicalPrimScaleMax)
  217. {
  218. error = "Model contains parts with sides larger than " + NonPhysicalPrimScaleMax.ToString() + "m. Please ajust scale";
  219. return false;
  220. }
  221. if (haveMeshs && inst.ContainsKey("mesh"))
  222. {
  223. mesh = (int)inst["mesh"];
  224. if (mesh >= numberMeshs)
  225. {
  226. error = "Incoerent model information.";
  227. return false;
  228. }
  229. // streamming cost
  230. float sqdiam = scale.LengthSquared();
  231. ameshCostParam curCost = meshsCosts[mesh];
  232. float mesh_streaming = streamingCost(curCost, sqdiam);
  233. meshcostdata.model_streaming_cost += mesh_streaming;
  234. meshcostdata.physics_cost += curCost.physicsCost;
  235. }
  236. else // instance as no mesh ??
  237. {
  238. // to do later if needed
  239. meshcostdata.model_streaming_cost += 0.5f;
  240. meshcostdata.physics_cost += 1.0f;
  241. }
  242. // assume unscripted and static prim server cost
  243. meshcostdata.simulation_cost += 0.5f;
  244. // charge for prims creation
  245. meshsfee += primCreationCost;
  246. }
  247. if (skipedSmall > 0)
  248. {
  249. if (skipedSmall > numberInstances / 2)
  250. {
  251. error = "Model contains too many prims smaller than " + PrimScaleMin.ToString() +
  252. "m minimum allowed size. Please check scalling";
  253. return false;
  254. }
  255. else
  256. warning += skipedSmall.ToString() + " of the requested " +numberInstances.ToString() +
  257. " model prims will not upload because they are smaller than " + PrimScaleMin.ToString() +
  258. "m minimum allowed size. Please check scalling ";
  259. }
  260. if (meshcostdata.physics_cost <= meshcostdata.model_streaming_cost)
  261. meshcostdata.resource_cost = meshcostdata.model_streaming_cost;
  262. else
  263. meshcostdata.resource_cost = meshcostdata.physics_cost;
  264. if (meshcostdata.resource_cost < meshcostdata.simulation_cost)
  265. meshcostdata.resource_cost = meshcostdata.simulation_cost;
  266. // scale cost
  267. // at this point a cost of 1.0 whould mean basic cost
  268. meshsfee *= ModelMeshCostFactor;
  269. if (meshsfee < ModelMinCostFactor)
  270. meshsfee = ModelMinCostFactor;
  271. // actually scale it to basic cost
  272. meshsfee *= (float)basicCost;
  273. meshsfee += 0.5f; // rounding
  274. totalcost += (int)meshsfee;
  275. // breakdown prices
  276. // don't seem to be in use so removed code for now
  277. return true;
  278. }
  279. // single mesh asset cost
  280. private bool MeshCost(byte[] data, ameshCostParam cost,out bool skeleton, out bool avatarPhys, out string error)
  281. {
  282. cost.highLODSize = 0;
  283. cost.highLODsides = 0;
  284. cost.medLODSize = 0;
  285. cost.lowLODSize = 0;
  286. cost.lowestLODSize = 0;
  287. cost.physicsCost = 0.0f;
  288. cost.costFee = 0.0f;
  289. error = string.Empty;
  290. skeleton = false;
  291. avatarPhys = false;
  292. if (data == null || data.Length == 0)
  293. {
  294. error = "Missing model information.";
  295. return false;
  296. }
  297. OSD meshOsd = null;
  298. int start = 0;
  299. error = "Invalid model data";
  300. using (MemoryStream ms = new MemoryStream(data))
  301. {
  302. try
  303. {
  304. OSD osd = OSDParser.DeserializeLLSDBinary(ms);
  305. if (osd is OSDMap)
  306. meshOsd = (OSDMap)osd;
  307. else
  308. return false;
  309. }
  310. catch
  311. {
  312. return false;
  313. }
  314. start = (int)ms.Position;
  315. }
  316. OSDMap map = (OSDMap)meshOsd;
  317. OSDMap tmpmap;
  318. int highlod_size = 0;
  319. int medlod_size = 0;
  320. int lowlod_size = 0;
  321. int lowestlod_size = 0;
  322. int skin_size = 0;
  323. int hulls_size = 0;
  324. int phys_nhulls;
  325. int phys_hullsvertices = 0;
  326. int physmesh_size = 0;
  327. int phys_ntriangles = 0;
  328. int submesh_offset = -1;
  329. if (map.ContainsKey("skeleton"))
  330. {
  331. tmpmap = (OSDMap)map["skeleton"];
  332. if (tmpmap.ContainsKey("offset") && tmpmap.ContainsKey("size"))
  333. {
  334. int sksize = tmpmap["size"].AsInteger();
  335. if(sksize > 0)
  336. skeleton = true;
  337. }
  338. }
  339. if (map.ContainsKey("physics_convex"))
  340. {
  341. tmpmap = (OSDMap)map["physics_convex"];
  342. if (tmpmap.ContainsKey("offset"))
  343. submesh_offset = tmpmap["offset"].AsInteger() + start;
  344. if (tmpmap.ContainsKey("size"))
  345. hulls_size = tmpmap["size"].AsInteger();
  346. }
  347. if (submesh_offset < 0 || hulls_size == 0)
  348. {
  349. error = "Missing physics_convex block";
  350. return false;
  351. }
  352. if (!hulls(data, submesh_offset, hulls_size, out phys_hullsvertices, out phys_nhulls))
  353. {
  354. error = "Bad physics_convex block";
  355. return false;
  356. }
  357. submesh_offset = -1;
  358. int nsides = 0;
  359. int lod_ntriangles = 0;
  360. if (map.ContainsKey("high_lod"))
  361. {
  362. tmpmap = (OSDMap)map["high_lod"];
  363. // see at least if there is a offset for this one
  364. if (tmpmap.ContainsKey("offset"))
  365. submesh_offset = tmpmap["offset"].AsInteger() + start;
  366. if (tmpmap.ContainsKey("size"))
  367. highlod_size = tmpmap["size"].AsInteger();
  368. if (submesh_offset >= 0 && highlod_size > 0)
  369. {
  370. if (!submesh(data, submesh_offset, highlod_size, out lod_ntriangles, out nsides))
  371. {
  372. error = "Model data parsing error";
  373. return false;
  374. }
  375. }
  376. }
  377. if (submesh_offset < 0 || highlod_size <= 0)
  378. {
  379. error = "Missing high_lod block";
  380. return false;
  381. }
  382. bool haveprev = true;
  383. if (map.ContainsKey("medium_lod"))
  384. {
  385. tmpmap = (OSDMap)map["medium_lod"];
  386. if (tmpmap.ContainsKey("size"))
  387. medlod_size = tmpmap["size"].AsInteger();
  388. else
  389. haveprev = false;
  390. }
  391. if (haveprev && map.ContainsKey("low_lod"))
  392. {
  393. tmpmap = (OSDMap)map["low_lod"];
  394. if (tmpmap.ContainsKey("size"))
  395. lowlod_size = tmpmap["size"].AsInteger();
  396. else
  397. haveprev = false;
  398. }
  399. if (haveprev && map.ContainsKey("lowest_lod"))
  400. {
  401. tmpmap = (OSDMap)map["lowest_lod"];
  402. if (tmpmap.ContainsKey("size"))
  403. lowestlod_size = tmpmap["size"].AsInteger();
  404. }
  405. if (map.ContainsKey("skin"))
  406. {
  407. tmpmap = (OSDMap)map["skin"];
  408. if (tmpmap.ContainsKey("size"))
  409. skin_size = tmpmap["size"].AsInteger();
  410. }
  411. cost.highLODSize = highlod_size;
  412. cost.highLODsides = nsides;
  413. cost.medLODSize = medlod_size;
  414. cost.lowLODSize = lowlod_size;
  415. cost.lowestLODSize = lowestlod_size;
  416. submesh_offset = -1;
  417. tmpmap = null;
  418. if(map.ContainsKey("physics_mesh"))
  419. tmpmap = (OSDMap)map["physics_mesh"];
  420. else if (map.ContainsKey("physics_shape")) // old naming
  421. tmpmap = (OSDMap)map["physics_shape"];
  422. int phys_nsides = 0;
  423. if(tmpmap != null)
  424. {
  425. if (tmpmap.ContainsKey("offset"))
  426. submesh_offset = tmpmap["offset"].AsInteger() + start;
  427. if (tmpmap.ContainsKey("size"))
  428. physmesh_size = tmpmap["size"].AsInteger();
  429. if (submesh_offset >= 0 && physmesh_size > 0)
  430. {
  431. if (!submesh(data, submesh_offset, physmesh_size, out phys_ntriangles, out phys_nsides))
  432. {
  433. error = "Model data parsing error";
  434. return false;
  435. }
  436. }
  437. }
  438. // upload is done in convex shape type so only one hull
  439. phys_hullsvertices++;
  440. cost.physicsCost = 0.04f * phys_hullsvertices;
  441. float sfee;
  442. sfee = data.Length; // start with total compressed data size
  443. // penalize lod meshs that should be more builder optimized
  444. sfee += medSizeWth * medlod_size;
  445. sfee += lowSizeWth * lowlod_size;
  446. sfee += lowestSizeWth * lowlod_size;
  447. // physics
  448. // favor potencial optimized meshs versus automatic decomposition
  449. if (physmesh_size != 0)
  450. sfee += physMeshSizeWth * (physmesh_size + hulls_size / 4); // reduce cost of mandatory convex hull
  451. else
  452. sfee += physHullSizeWth * hulls_size;
  453. // bytes to money
  454. sfee *= bytecost;
  455. cost.costFee = sfee;
  456. return true;
  457. }
  458. // parses a LOD or physics mesh component
  459. private bool submesh(byte[] data, int offset, int size, out int ntriangles, out int nsides)
  460. {
  461. ntriangles = 0;
  462. nsides = 0;
  463. OSD decodedMeshOsd = new OSD();
  464. try
  465. {
  466. using (MemoryStream outMs = new MemoryStream())
  467. {
  468. using (MemoryStream inMs = new MemoryStream(data, offset, size))
  469. {
  470. using (DeflateStream decompressionStream = new DeflateStream(inMs, CompressionMode.Decompress))
  471. {
  472. byte[] readBuffer = new byte[2048];
  473. inMs.Read(readBuffer, 0, 2); // skip first 2 bytes in header
  474. int readLen = 0;
  475. while ((readLen = decompressionStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
  476. outMs.Write(readBuffer, 0, readLen);
  477. }
  478. }
  479. outMs.Seek(0, SeekOrigin.Begin);
  480. decodedMeshOsd = OSDParser.DeserializeLLSDBinary(outMs);
  481. }
  482. }
  483. catch
  484. {
  485. return false;
  486. }
  487. OSDArray decodedMeshOsdArray = null;
  488. byte[] dummy;
  489. decodedMeshOsdArray = (OSDArray)decodedMeshOsd;
  490. foreach (OSD subMeshOsd in decodedMeshOsdArray)
  491. {
  492. if (subMeshOsd is OSDMap)
  493. {
  494. OSDMap subtmpmap = (OSDMap)subMeshOsd;
  495. if (subtmpmap.ContainsKey("NoGeometry") && ((OSDBoolean)subtmpmap["NoGeometry"]))
  496. continue;
  497. if (!subtmpmap.ContainsKey("Position"))
  498. return false;
  499. if (subtmpmap.ContainsKey("TriangleList"))
  500. {
  501. dummy = subtmpmap["TriangleList"].AsBinary();
  502. ntriangles += dummy.Length / bytesPerCoord;
  503. }
  504. else
  505. return false;
  506. nsides++;
  507. }
  508. }
  509. return true;
  510. }
  511. // parses convex hulls component
  512. private bool hulls(byte[] data, int offset, int size, out int nvertices, out int nhulls)
  513. {
  514. nvertices = 0;
  515. nhulls = 1;
  516. OSD decodedMeshOsd = new OSD();
  517. try
  518. {
  519. using (MemoryStream outMs = new MemoryStream(4 * size))
  520. {
  521. using (MemoryStream inMs = new MemoryStream(data, offset, size))
  522. {
  523. using (DeflateStream decompressionStream = new DeflateStream(inMs, CompressionMode.Decompress))
  524. {
  525. byte[] readBuffer = new byte[8192];
  526. inMs.Read(readBuffer, 0, 2); // skip first 2 bytes in header
  527. int readLen = 0;
  528. while ((readLen = decompressionStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
  529. outMs.Write(readBuffer, 0, readLen);
  530. }
  531. }
  532. outMs.Seek(0, SeekOrigin.Begin);
  533. decodedMeshOsd = OSDParser.DeserializeLLSDBinary(outMs);
  534. }
  535. }
  536. catch
  537. {
  538. return false;
  539. }
  540. OSDMap cmap = (OSDMap)decodedMeshOsd;
  541. if (cmap == null)
  542. return false;
  543. byte[] dummy;
  544. // must have one of this
  545. if (cmap.ContainsKey("BoundingVerts"))
  546. {
  547. dummy = cmap["BoundingVerts"].AsBinary();
  548. nvertices = dummy.Length / bytesPerCoord;
  549. }
  550. else
  551. return false;
  552. /* upload is done with convex shape type
  553. if (cmap.ContainsKey("HullList"))
  554. {
  555. dummy = cmap["HullList"].AsBinary();
  556. nhulls += dummy.Length;
  557. }
  558. if (cmap.ContainsKey("Positions"))
  559. {
  560. dummy = cmap["Positions"].AsBinary();
  561. nvertices = dummy.Length / bytesPerCoord;
  562. }
  563. */
  564. return true;
  565. }
  566. // returns streaming cost from on mesh LODs sizes in curCost and square of prim size length
  567. private float streamingCost(ameshCostParam curCost, float sqdiam)
  568. {
  569. // compute efective areas
  570. float ma = 262144f;
  571. float mh = sqdiam * highLodFactor;
  572. if (mh > ma)
  573. mh = ma;
  574. float mm = sqdiam * midLodFactor;
  575. if (mm > ma)
  576. mm = ma;
  577. float ml = sqdiam * lowLodFactor;
  578. if (ml > ma)
  579. ml = ma;
  580. float mlst = ma;
  581. mlst -= ml;
  582. ml -= mm;
  583. mm -= mh;
  584. if (mlst < 1.0f)
  585. mlst = 1.0f;
  586. if (ml < 1.0f)
  587. ml = 1.0f;
  588. if (mm < 1.0f)
  589. mm = 1.0f;
  590. if (mh < 1.0f)
  591. mh = 1.0f;
  592. ma = mlst + ml + mm + mh;
  593. // get LODs compressed sizes
  594. int lst = curCost.lowestLODSize;
  595. int l = curCost.lowLODSize;
  596. int m = curCost.medLODSize;
  597. int h = curCost.highLODSize;
  598. // use previous higher LOD size on missing ones
  599. if (m <= 0)
  600. m = h;
  601. if (l <= 0)
  602. l = m;
  603. if (lst <= 0)
  604. lst = l;
  605. // giving 384 bytes bonus
  606. lst -= 384;
  607. l -= 384;
  608. m -= 384;
  609. h -= 384;
  610. // force minumum sizes
  611. if (lst < 16)
  612. lst = 16;
  613. if (l < 16)
  614. l = 16;
  615. if (m < 16)
  616. m = 16;
  617. if (h < 16)
  618. h = 16;
  619. // compute cost weighted by relative effective areas
  620. float cost = lst * mlst + l * ml + m * mm + h * mh;
  621. cost /= ma;
  622. cost *= 0.004f; // overall tunning parameter
  623. return cost;
  624. }
  625. }
  626. }