ViewerEnvironment.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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.Collections.Generic;
  29. using System.Runtime.CompilerServices;
  30. using OpenMetaverse;
  31. using OpenMetaverse.StructuredData;
  32. namespace OpenSim.Framework
  33. {
  34. // legacy lightshare
  35. public class RegionLightShareData
  36. {
  37. public Vector3 waterColor = new Vector3(4.0f, 38.0f, 64.0f);
  38. public float waterFogDensityExponent = 4.0f;
  39. public float underwaterFogModifier = 0.25f;
  40. public Vector3 reflectionWaveletScale = new Vector3(2.0f, 2.0f, 2.0f);
  41. public float fresnelScale = 0.40f;
  42. public float fresnelOffset = 0.50f;
  43. public float refractScaleAbove = 0.03f;
  44. public float refractScaleBelow = 0.20f;
  45. public float blurMultiplier = 0.040f;
  46. public Vector2 bigWaveDirection = new Vector2(1.05f, -0.42f);
  47. public Vector2 littleWaveDirection = new Vector2(1.11f, -1.16f);
  48. public UUID normalMapTexture = new UUID("822ded49-9a6c-f61c-cb89-6df54f42cdf4");
  49. public Vector4 horizon = new Vector4(0.25f, 0.25f, 0.32f, 0.32f);
  50. public float hazeHorizon = 0.19f;
  51. public Vector4 blueDensity = new Vector4(0.12f, 0.22f, 0.38f, 0.38f);
  52. public float hazeDensity = 0.70f;
  53. public float densityMultiplier = 0.18f;
  54. public float distanceMultiplier = 0.8f;
  55. public UInt16 maxAltitude = 1605;
  56. public Vector4 sunMoonColor = new Vector4(0.24f, 0.26f, 0.30f, 0.30f);
  57. public float sunMoonPosition = 0.317f;
  58. public Vector4 ambient = new Vector4(0.35f, 0.35f, 0.35f, 0.35f);
  59. public float eastAngle = 0.0f;
  60. public float sunGlowFocus = 0.10f;
  61. public float sunGlowSize = 1.75f;
  62. public float sceneGamma = 1.0f;
  63. public float starBrightness = 0.0f;
  64. public Vector4 cloudColor = new Vector4(0.41f, 0.41f, 0.41f, 0.41f);
  65. public Vector3 cloudXYDensity = new Vector3(1.00f, 0.53f, 1.00f);
  66. public float cloudCoverage = 0.27f;
  67. public float cloudScale = 0.42f;
  68. public Vector3 cloudDetailXYDensity = new Vector3(1.00f, 0.53f, 0.12f);
  69. public float cloudScrollX = 0.20f;
  70. public bool cloudScrollXLock = false;
  71. public float cloudScrollY = 0.01f;
  72. public bool cloudScrollYLock = false;
  73. public bool drawClassicClouds = true;
  74. }
  75. public class ViewerEnvironment
  76. {
  77. DayCycle Cycle = new DayCycle();
  78. public int DayLength = 14400;
  79. public int DayOffset = 57600;
  80. public int Flags = 0;
  81. public float[] Altitudes = new float[3] {1000f, 2000f, 3000f };
  82. //DayHash;
  83. public bool IsLegacy = false;
  84. public string DayCycleName;
  85. public int version = 0;
  86. public void FromWLOSD(OSD osd)
  87. {
  88. OSDArray array = osd as OSDArray;
  89. if(osd != null)
  90. {
  91. Cycle = new DayCycle();
  92. Cycle.FromWLOSD(array);
  93. }
  94. InvalidateCaches();
  95. }
  96. public OSD ToWLOSD(UUID message, UUID region)
  97. {
  98. OSDArray array = new OSDArray(4) { null, null, null, null };
  99. array[0] = new OSDMap { {"messageID", message }, { "regionID", region } };
  100. Cycle.ToWLOSD(ref array);
  101. return array;
  102. }
  103. private static Quaternion AzAlToRot(float az, float al)
  104. {
  105. if (Utils.ApproxEqual(al, 0, 1e-3f) || Utils.ApproxEqual(Math.Abs(al), Utils.TWO_PI, 1e-3f))
  106. {
  107. az *= 0.5f;
  108. return new Quaternion(0, 0, (float)Math.Sin(az), (float)Math.Cos(az));
  109. }
  110. if (Utils.ApproxEqual(az, 0, 1e-3f) || Utils.ApproxEqual(Math.Abs(az), Utils.TWO_PI, 1e-3f))
  111. {
  112. al *= 0.5f;
  113. return new Quaternion(0, -(float)Math.Sin(al), 0, (float)Math.Cos(al));
  114. }
  115. az *= 0.5f;
  116. float sz = (float)Math.Sin(az);
  117. float cz = (float)Math.Cos(az);
  118. al *= 0.5f;
  119. float sl = (float)Math.Sin(al);
  120. float cl = (float)Math.Cos(al);
  121. Quaternion rot = new Quaternion(sl * sz, -sl * cz, cl * sz, cl * cz);
  122. rot.Normalize();
  123. return rot;
  124. }
  125. public static void convertFromAngles(SkyData sky, float sun_angle, float east_angle)
  126. {
  127. float az = -east_angle;
  128. float al = sun_angle;
  129. sky.sun_rotation = AzAlToRot(az, al);
  130. sky.moon_rotation = AzAlToRot(az, al + (float)Math.PI);
  131. }
  132. public static Vector3 Xrot(Quaternion rot)
  133. {
  134. rot.Normalize(); // just in case
  135. return new Vector3(2 * (rot.X * rot.X + rot.W * rot.W) - 1,
  136. 2 * (rot.X * rot.Y + rot.Z * rot.W),
  137. 2 * (rot.X * rot.Z - rot.Y * rot.W));
  138. }
  139. public static void convertToAngles(SkyData sky, out float sun_angle, out float east_angle, out Vector4 lightnorm)
  140. {
  141. Vector3 v = Xrot(sky.sun_rotation);
  142. v.Normalize();
  143. if(v.Z >= 0)
  144. lightnorm = new Vector4(v.Y, v.Z, v.X, 1);
  145. else if (v.Z > -0.12)
  146. {
  147. float m = v.Y * v.Y + v.Z * v.Z;
  148. m = 1/(float)Math.Sqrt(m);
  149. lightnorm = new Vector4(v.Y * m, 0, v.X * m, 1);
  150. }
  151. else
  152. lightnorm = new Vector4(-v.Y, -v.Z, -v.X, 1);
  153. sun_angle = (float)Math.Asin(v.Z);
  154. east_angle = -(float)Math.Atan2(v.Y, v.X);
  155. if (Math.Abs(east_angle) < 1e-6)
  156. east_angle = 0;
  157. else if (east_angle < 0)
  158. east_angle = Utils.TWO_PI + east_angle;
  159. // this is just a case on one example daycyles, as wrong as any
  160. /*
  161. if (Utils.ApproxEqual(east_angle, Utils.PI, 1e-4f))
  162. {
  163. east_angle = 0;
  164. sun_angle = Utils.PI - sun_angle;
  165. }
  166. */
  167. if (Math.Abs(sun_angle) < 1e-6)
  168. sun_angle = 0;
  169. else if (sun_angle < 0)
  170. sun_angle = Utils.TWO_PI + sun_angle;
  171. }
  172. public void FromLightShare(RegionLightShareData ls)
  173. {
  174. WaterData water = new WaterData();
  175. water.waterFogColor = ls.waterColor / 256f;
  176. water.waterFogDensity = (float)Math.Pow(2.0f, ls.waterFogDensityExponent);
  177. //water.waterFogDensity = ls.waterFogDensityExponent;
  178. water.underWaterFogMod = ls.underwaterFogModifier;
  179. water.normScale = ls.reflectionWaveletScale;
  180. water.fresnelScale = ls.fresnelScale;
  181. water.fresnelOffset = ls.fresnelOffset;
  182. water.scaleAbove = ls.refractScaleAbove;
  183. water.scaleBelow = ls.refractScaleBelow;
  184. water.blurMultiplier = ls.blurMultiplier;
  185. water.wave1Dir = ls.littleWaveDirection;
  186. water.wave2Dir = ls.bigWaveDirection;
  187. water.normalMap = ls.normalMapTexture;
  188. water.Name = "LightshareWater";
  189. SkyData sky = new SkyData();
  190. convertFromAngles(sky, 2.0f * (float)Math.PI * ls.sunMoonPosition, 2.0f * (float)Math.PI * ls.eastAngle);
  191. sky.sunlight_color = ls.sunMoonColor * 3.0f;
  192. sky.ambient = new Vector3(ls.ambient.X * 3.0f, ls.ambient.Y * 3.0f, ls.ambient.Z * 3.0f);
  193. sky.blue_horizon = new Vector3(ls.horizon.X * 2.0f, ls.horizon.Y * 2.0f, ls.horizon.Z * 2.0f);
  194. sky.blue_density = new Vector3(ls.blueDensity.X * 2.0f, ls.blueDensity.Y * 2.0f, ls.blueDensity.Z * 2.0f);;
  195. sky.haze_horizon = ls.hazeHorizon;
  196. sky.haze_density = ls.hazeDensity;
  197. sky.cloud_shadow = ls.cloudCoverage;
  198. sky.density_multiplier = ls.densityMultiplier / 1000.0f;
  199. sky.distance_multiplier = ls.distanceMultiplier;
  200. sky.max_y = ls.maxAltitude;
  201. sky.cloud_color = new Vector3(ls.cloudColor.X, ls.cloudColor.Y, ls.cloudColor.Z);
  202. sky.cloud_pos_density1 = ls.cloudXYDensity;
  203. sky.cloud_pos_density2 = ls.cloudDetailXYDensity;
  204. sky.cloud_scale = ls.cloudScale;
  205. sky.gamma=ls.sceneGamma;
  206. sky.glow = new Vector3((2f - ls.sunGlowSize) * 20f, 0f, -ls.sunGlowFocus * 5f);
  207. sky.cloud_scroll_rate = new Vector2(ls.cloudScrollX, ls.cloudScrollY);
  208. if (ls.cloudScrollXLock)
  209. sky.cloud_scroll_rate.X = 0;
  210. if (ls.cloudScrollYLock)
  211. sky.cloud_scroll_rate.Y = 0;
  212. sky.star_brightness = ls.starBrightness * 250f;
  213. sky.Name = "LightshareSky";
  214. Cycle = new DayCycle();
  215. Cycle.Name = "Lightshare";
  216. Cycle.waterframes.Add(water.Name, water);
  217. DayCycle.TrackEntry track = new DayCycle.TrackEntry(-1, water.Name);
  218. Cycle.waterTrack.Add(track);
  219. Cycle.skyframes.Add(sky.Name, sky);
  220. track = new DayCycle.TrackEntry(-1, sky.Name);
  221. Cycle.skyTrack0.Add(track);
  222. InvalidateCaches();
  223. }
  224. public RegionLightShareData ToLightShare()
  225. {
  226. RegionLightShareData ls = new RegionLightShareData();
  227. DayCycle.TrackEntry te;
  228. if (Cycle.waterTrack.Count > 0)
  229. {
  230. te = Cycle.waterTrack[0];
  231. if (Cycle.waterframes.TryGetValue(te.frameName, out WaterData water))
  232. {
  233. ls.waterColor = water.waterFogColor * 256f;
  234. ls.waterFogDensityExponent = (float)Math.Sqrt(water.waterFogDensity);
  235. //ls.waterFogDensityExponent = water.waterFogDensity;
  236. ls.underwaterFogModifier = water.underWaterFogMod;
  237. ls.reflectionWaveletScale = water.normScale;
  238. ls.fresnelScale = water.fresnelScale;
  239. ls.fresnelOffset = water.fresnelOffset;
  240. ls.refractScaleAbove = water.scaleAbove;
  241. ls.refractScaleBelow = water.scaleBelow;
  242. ls.blurMultiplier = water.blurMultiplier;
  243. ls.littleWaveDirection = water.wave1Dir;
  244. ls.bigWaveDirection = water.wave2Dir;
  245. ls.normalMapTexture = water.normalMap;
  246. }
  247. }
  248. if (Cycle.skyTrack0.Count > 0)
  249. {
  250. te = Cycle.skyTrack0[0];
  251. if (Cycle.skyframes.TryGetValue(te.frameName, out SkyData sky))
  252. {
  253. Vector4 lightnorm;
  254. convertToAngles(sky, out ls.sunMoonPosition, out ls.eastAngle, out lightnorm);
  255. ls.sunMoonPosition *= 0.5f / (float)Math.PI;
  256. ls.eastAngle *= 0.5f / (float)Math.PI;
  257. ls.sunMoonColor = sky.sunlight_color / 3f;
  258. ls.ambient = new Vector4(sky.ambient.X / 3.0f, sky.ambient.Y / 3.0f, sky.ambient.Z / 3.0f, 1);
  259. ls.horizon = new Vector4(sky.blue_horizon.X / 2.0f, sky.blue_horizon.Y / 2.0f, sky.blue_horizon.Z / 2.0f, 1);
  260. ls.blueDensity = new Vector4(sky.blue_density.X / 2.0f, sky.blue_density.Y / 2.0f, sky.blue_density.Z / 2.0f, 1);
  261. ls.hazeHorizon = sky.haze_horizon;
  262. ls.hazeDensity = sky.haze_density;
  263. ls.cloudCoverage = sky.cloud_shadow;
  264. ls.densityMultiplier = 1000f * sky.density_multiplier;
  265. ls.distanceMultiplier = sky.distance_multiplier;
  266. ls.maxAltitude = (ushort)sky.max_y;
  267. ls.cloudColor = new Vector4(sky.cloud_color.X, sky.cloud_color.Y, sky.cloud_color.Z, 1);
  268. ls.cloudXYDensity = sky.cloud_pos_density1;
  269. ls.cloudDetailXYDensity = sky.cloud_pos_density2;
  270. ls.cloudScale = sky.cloud_scale;
  271. ls.sceneGamma = sky.gamma;
  272. ls.sunGlowSize = (2f - sky.glow.X) / 20f;
  273. ls.sunGlowFocus = -sky.glow.Z / 5f;
  274. ls.cloudScrollX = sky.cloud_scroll_rate.X;
  275. ls.cloudScrollY = sky.cloud_scroll_rate.Y;
  276. ls.cloudScrollXLock = ls.cloudScrollX == 0f;
  277. ls.cloudScrollYLock = ls.cloudScrollY == 0f;
  278. ls.starBrightness = sky.star_brightness / 250f;
  279. }
  280. }
  281. return ls;
  282. }
  283. public void FromOSD(OSD osd)
  284. {
  285. OSDMap map = osd as OSDMap;
  286. if (map == null)
  287. return;
  288. OSD otmp;
  289. if (map.TryGetValue("day_cycle", out otmp) && otmp is OSDMap)
  290. {
  291. Cycle = new DayCycle();
  292. Cycle.FromOSD(otmp as OSDMap);
  293. }
  294. if (Cycle == null)
  295. Cycle = new DayCycle();
  296. if (map.TryGetValue("day_length", out otmp))
  297. DayLength = otmp;
  298. if (map.TryGetValue("day_offset", out otmp))
  299. DayOffset = otmp;
  300. if (map.TryGetValue("flags", out otmp))
  301. Flags = otmp;
  302. if (map.TryGetValue("env_version", out otmp))
  303. version = otmp;
  304. else
  305. ++version;
  306. if (map.TryGetValue("track_altitudes", out otmp) && otmp is OSDArray)
  307. {
  308. OSDArray alt = otmp as OSDArray;
  309. for(int i = 0; i < alt.Count && i < 3; ++i)
  310. Altitudes[i] = alt[i];
  311. SortAltitudes();
  312. }
  313. IsLegacy = false;
  314. InvalidateCaches();
  315. }
  316. public void SortAltitudes()
  317. {
  318. for (int i = 0; i < 2; ++i)
  319. {
  320. float h = Altitudes[i];
  321. for (int j = i + 1; j < 3; ++j)
  322. {
  323. if (h > Altitudes[j])
  324. {
  325. Altitudes[i] = Altitudes[j];
  326. Altitudes[j] = h;
  327. List<DayCycle.TrackEntry> tet = Cycle.skyTracks[i];
  328. Cycle.skyTracks[i] = Cycle.skyTracks[j];
  329. Cycle.skyTracks[j] = tet;
  330. h = Altitudes[i];
  331. }
  332. }
  333. }
  334. }
  335. public bool CycleFromOSD(OSD osd)
  336. {
  337. OSDMap map = osd as OSDMap;
  338. if (map == null)
  339. return false;
  340. if (!map.TryGetValue("type", out OSD tmp))
  341. return false;
  342. string type = tmp.AsString();
  343. if (type != "daycycle")
  344. return false;
  345. Cycle = new DayCycle();
  346. Cycle.FromOSD(map);
  347. InvalidateCaches();
  348. return true;
  349. }
  350. public bool FromAssetOSD(string name, OSD osd)
  351. {
  352. OSDMap map = osd as OSDMap;
  353. if (map == null)
  354. return false;
  355. if (!map.TryGetValue("type", out OSD tmp))
  356. return false;
  357. string type = tmp.AsString();
  358. bool ok = false;
  359. if (type == "water")
  360. {
  361. if (Cycle == null)
  362. Cycle = new DayCycle();
  363. ok = Cycle.replaceWaterFromOSD(name, map);
  364. }
  365. else
  366. {
  367. if (type == "daycycle")
  368. {
  369. Cycle = new DayCycle();
  370. Cycle.FromOSD(map);
  371. ok = true;
  372. }
  373. else if(type == "sky")
  374. {
  375. if (Cycle == null)
  376. Cycle = new DayCycle();
  377. ok = Cycle.replaceSkyFromOSD(name, map);
  378. }
  379. }
  380. if(ok && !string.IsNullOrWhiteSpace(name))
  381. Cycle.Name = name;
  382. InvalidateCaches();
  383. return ok;
  384. }
  385. public OSD ToOSD()
  386. {
  387. OSDMap env = new OSDMap();
  388. env["day_cycle"] = Cycle.ToOSD();
  389. env["day_length"] = DayLength;
  390. env["day_offset"] = DayOffset;
  391. env["flags"] = Flags;
  392. env["env_version"] = version;
  393. OSDArray alt = new OSDArray();
  394. alt.Add(Altitudes[0]);
  395. alt.Add(Altitudes[1]);
  396. alt.Add(Altitudes[2]);
  397. env["track_altitudes"] = alt;
  398. return env;
  399. }
  400. public readonly object m_cachedbytesLock = new object();
  401. public byte[] m_cachedbytes = null;
  402. public byte[] m_cachedWLbytes = null;
  403. public void InvalidateCaches()
  404. {
  405. lock (m_cachedbytesLock)
  406. {
  407. m_cachedbytes = null;
  408. m_cachedWLbytes = null;
  409. }
  410. }
  411. public byte[] ToCapBytes(UUID regionID, int parcelID)
  412. {
  413. //byte[] ret = m_cachedbytes;
  414. //if(ret != null)
  415. // return ret;
  416. lock (m_cachedbytesLock)
  417. {
  418. byte[] ret = m_cachedbytes;
  419. if (ret == null)
  420. {
  421. OSDMap map = new OSDMap();
  422. OSDMap cenv = (OSDMap)ToOSD();
  423. cenv["parcel_id"] = parcelID;
  424. cenv["region_id"] = regionID;
  425. map["environment"] = cenv;
  426. map["parcel_id"] = parcelID;
  427. map["success"] = true;
  428. ret = OSDParser.SerializeLLSDXmlToBytes(map);
  429. m_cachedbytes = ret;
  430. }
  431. return ret;
  432. }
  433. }
  434. public byte[] ToCapWLBytes(UUID messageID, UUID regionID)
  435. {
  436. //byte[] ret = m_cachedWLbytes;
  437. //if (ret != null)
  438. // return ret;
  439. lock (m_cachedbytesLock)
  440. {
  441. byte[] ret = m_cachedWLbytes;
  442. if (ret == null)
  443. {
  444. OSD d = ToWLOSD(messageID, regionID);
  445. ret = OSDParser.SerializeLLSDXmlToBytes(d);
  446. m_cachedWLbytes = ret;
  447. }
  448. return ret;
  449. }
  450. }
  451. public static ViewerEnvironment FromOSDString(string s)
  452. {
  453. try
  454. {
  455. OSD eosd = OSDParser.Deserialize(s);
  456. ViewerEnvironment VEnv = new ViewerEnvironment();
  457. VEnv.FromOSD(eosd);
  458. return VEnv;
  459. }
  460. catch
  461. {
  462. }
  463. return null;
  464. }
  465. public static string ToOSDString(ViewerEnvironment VEnv, bool xml = false)
  466. {
  467. try
  468. {
  469. OSD eosd= VEnv.ToOSD();
  470. if(xml)
  471. return OSDParser.SerializeLLSDXmlString(eosd);
  472. else
  473. return OSDParser.SerializeLLSDNotationFull(eosd);
  474. }
  475. catch {}
  476. return String.Empty;
  477. }
  478. public ViewerEnvironment Clone()
  479. {
  480. // im lazy need to proper clone later
  481. OSD osd = ToOSD();
  482. ViewerEnvironment VEnv = new ViewerEnvironment();
  483. VEnv.FromOSD(osd);
  484. return VEnv;
  485. }
  486. public static OSD DefaultToOSD(UUID regionID, int parcel)
  487. {
  488. OSDMap top = new OSDMap();
  489. OSDMap env = new OSDMap();
  490. env["is_default"] = true;
  491. if (parcel >= 0)
  492. env["parcel_id"] = parcel;
  493. env["region_id"] = regionID;
  494. OSDArray alt = new OSDArray();
  495. alt.Add(1000f);
  496. alt.Add(2000f);
  497. alt.Add(3000f);
  498. env["track_altitudes"] = alt;
  499. top["environment"] = env;
  500. if (parcel >= 0)
  501. top["parcel_id"] = parcel;
  502. top["success"] = true;
  503. return top;
  504. }
  505. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  506. public List<DayCycle.TrackEntry> FindTrack(float altitude)
  507. {
  508. if (altitude < Altitudes[0])
  509. return Cycle.skyTrack0;
  510. int altindx = 1;
  511. for (; altindx < Altitudes.Length; ++altindx)
  512. {
  513. if (Altitudes[altindx] > altitude)
  514. break;
  515. }
  516. List<DayCycle.TrackEntry> track = null;
  517. while (--altindx >= 0)
  518. {
  519. track = Cycle.skyTracks[altindx];
  520. if (track != null && track.Count > 0)
  521. break;
  522. }
  523. return track;
  524. }
  525. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  526. public bool FindSkies(List<DayCycle.TrackEntry> track, float dayfrac, out float skyfrac, out SkyData sky1, out SkyData sky2)
  527. {
  528. sky1 = null;
  529. sky2 = null;
  530. skyfrac = dayfrac;
  531. if (track.Count == 1 || track[0].time < 0)
  532. {
  533. if (!Cycle.skyframes.TryGetValue(track[0].frameName, out sky1) || sky1 == null)
  534. return false;
  535. return true;
  536. }
  537. int i = 0;
  538. while (i < track.Count)
  539. {
  540. if (track[i].time > dayfrac)
  541. break;
  542. ++i;
  543. }
  544. float firstFrac;
  545. float secondFrac;
  546. string first;
  547. string second;
  548. int ntracks = track.Count;
  549. if (i == 0 || i == ntracks)
  550. {
  551. --ntracks;
  552. firstFrac = track[ntracks].time;
  553. first = track[ntracks].frameName;
  554. secondFrac = track[0].time + 1f;
  555. second = track[0].frameName;
  556. }
  557. else
  558. {
  559. secondFrac = track[i].time;
  560. second = track[i].frameName;
  561. --i;
  562. firstFrac = track[i].time;
  563. first = track[i].frameName;
  564. }
  565. if (!Cycle.skyframes.TryGetValue(first, out sky1) || sky1 == null)
  566. firstFrac = -1;
  567. if (!Cycle.skyframes.TryGetValue(second, out sky2) || sky2 == null)
  568. secondFrac = -1;
  569. if (firstFrac < 0)
  570. {
  571. if (secondFrac < 0)
  572. return false;
  573. sky1 = sky2;
  574. sky2 = null;
  575. return true;
  576. }
  577. if (secondFrac < 0 || secondFrac == firstFrac)
  578. {
  579. sky2 = null;
  580. return true;
  581. }
  582. dayfrac -= firstFrac;
  583. secondFrac -= firstFrac;
  584. dayfrac /= secondFrac;
  585. skyfrac = Utils.Clamp(dayfrac, 0, 1f);
  586. return true;
  587. }
  588. public bool getPositions(float altitude, float dayfrac, out Vector3 sundir, out Vector3 moondir,
  589. out Quaternion sunrot, out Quaternion moonrot)
  590. {
  591. sundir = Vector3.Zero;
  592. moondir = Vector3.Zero;
  593. sunrot = Quaternion.Identity;
  594. moonrot = Quaternion.Identity;
  595. List<DayCycle.TrackEntry> track = FindTrack(altitude);
  596. if (track == null || track.Count == 0)
  597. return false;
  598. if (!FindSkies(track, dayfrac, out dayfrac, out SkyData sky1, out SkyData sky2))
  599. return false;
  600. if (sky2 == null)
  601. {
  602. moonrot = sky1.moon_rotation;
  603. moondir = Xrot(moonrot);
  604. sunrot = sky1.sun_rotation;
  605. sundir = Xrot(sunrot);
  606. return true;
  607. }
  608. moonrot = Quaternion.Slerp(sky1.moon_rotation, sky2.moon_rotation, dayfrac);
  609. moondir = Xrot(moonrot);
  610. sunrot = Quaternion.Slerp(sky1.sun_rotation, sky2.sun_rotation, dayfrac);
  611. sundir = Xrot(sunrot);
  612. return true;
  613. }
  614. /* not needed for wl viewers
  615. public bool getWLPositions(float altitude, float dayfrac, out Vector3 sundir)
  616. {
  617. sundir = Vector3.Zero;
  618. List<DayCycle.TrackEntry> track = track = FindTrack(altitude);
  619. if (track == null || track.Count == 0)
  620. return false;
  621. if (!FindSkies(track, dayfrac, out dayfrac, out SkyData sky1, out SkyData sky2))
  622. return false;
  623. Quaternion sunrot;
  624. if (sky2 == null)
  625. {
  626. sunrot = sky1.sun_rotation;
  627. sundir = Xrot(sunrot);
  628. return true;
  629. }
  630. sunrot = Quaternion.Slerp(sky1.sun_rotation, sky2.sun_rotation, dayfrac);
  631. sundir = Xrot(sunrot);
  632. return true;
  633. }
  634. */
  635. public void GatherAssets(Dictionary<UUID, sbyte> uuids)
  636. {
  637. if (Cycle != null)
  638. Cycle.GatherAssets(uuids);
  639. }
  640. }
  641. }