1
0

ViewerSky.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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.Collections.Generic;
  28. using OpenMetaverse;
  29. using OpenMetaverse.StructuredData;
  30. namespace OpenSim.Framework
  31. {
  32. public class SkyData
  33. {
  34. public struct AbsCoefData
  35. {
  36. public float constant_term;
  37. public float exp_scale;
  38. public float exp_term;
  39. public float linear_term;
  40. public float width;
  41. public AbsCoefData(float w, float expt, float exps, float lin, float cons)
  42. {
  43. constant_term = cons;
  44. exp_scale = exps;
  45. exp_term = expt;
  46. linear_term = lin;
  47. width = w;
  48. }
  49. public OSDMap ToOSD()
  50. {
  51. OSDMap map = new()
  52. {
  53. ["constant_term"] = constant_term,
  54. ["exp_scale"] = exp_scale,
  55. ["exp_term"] = exp_term,
  56. ["linear_term"] = linear_term,
  57. ["width"] = width
  58. };
  59. return map;
  60. }
  61. public void FromOSD(OSDMap map)
  62. {
  63. constant_term = map["constant_term"];
  64. exp_scale = map["exp_scale"];
  65. exp_term = map["exp_term"];
  66. linear_term = map["linear_term"];
  67. width = map["width"];
  68. }
  69. }
  70. public struct mCoefData
  71. {
  72. public float anisotropy;
  73. public float constant_term;
  74. public float exp_scale;
  75. public float exp_term;
  76. public float linear_term;
  77. public float width;
  78. public mCoefData(float w, float expt, float exps, float lin, float cons, float ani)
  79. {
  80. anisotropy = ani;
  81. constant_term = cons;
  82. exp_scale = exps;
  83. exp_term = expt;
  84. linear_term = lin;
  85. width = w;
  86. }
  87. public OSDMap ToOSD()
  88. {
  89. OSDMap map = new()
  90. {
  91. ["anisotropy"] = anisotropy,
  92. ["constant_term"] = constant_term,
  93. ["exp_scale"] = exp_scale,
  94. ["exp_term"] = exp_term,
  95. ["linear_term"] = linear_term,
  96. ["width"] = width
  97. };
  98. return map;
  99. }
  100. public void FromOSD(OSDMap map)
  101. {
  102. anisotropy = map["anisotropy"];
  103. constant_term = map["constant_term"];
  104. exp_scale = map["exp_scale"];
  105. exp_term = map["exp_term"];
  106. linear_term = map["linear_term"];
  107. width = map["width"];
  108. }
  109. }
  110. //AbsCoefData(float w, float expt, float exps, float lin, float cons)
  111. public AbsCoefData abscoefA = new(25000f, 0, 0, 0, 0);
  112. public AbsCoefData abscoefB = new(0, 0, 0, -6.6666667e-5f, 1f);
  113. public AbsCoefData rayleigh_config = new(0, 1, -1.25e-4f, 0, 0);
  114. //mCoefData(float w, float expt, float exps, float lin, float cons, float ani)
  115. public mCoefData mieconf = new(0, 1f, -8.333333e-4f, 0, 0, 0.8f);
  116. UUID bloom_id = new("3c59f7fe-9dc8-47f9-8aaf-a9dd1fbc3bef");
  117. UUID cloud_id = new("1dc1368f-e8fe-f02d-a08d-9d9f11c1af6b");
  118. UUID halo_id = new("12149143-f599-91a7-77ac-b52a3c0f59cd");
  119. UUID moon_id = new("ec4b9f0b-d008-45c6-96a4-01dd947ac621");
  120. UUID rainbow_id = new("11b4c57c-56b3-04ed-1f82-2004363882e4");
  121. UUID sun_id = UUID.Zero;
  122. public Vector3 ambient = new(1.047f, 1.047f, 1.047f); //?
  123. public Vector3 blue_density = new(0.2447f, 0.4487f, 0.76f);
  124. public Vector3 blue_horizon = new(0.4954f, 0.4954f, 0.64f);
  125. public Vector3 cloud_color = new(0.41f, 0.41f, 0.41f);
  126. public Vector3 cloud_pos_density1 = new(1, 0.5260f, 1);
  127. public Vector3 cloud_pos_density2 = new(1, 0.5260f, 0.12f);
  128. public float cloud_scale = 0.42f;
  129. public Vector2 cloud_scroll_rate = new(0.2f, 0.011f);
  130. public float cloud_shadow = 0.27f;
  131. public float density_multiplier = 0.00018f;
  132. public float distance_multiplier = 0.8f;
  133. public float gamma = 1;
  134. public Vector3 glow = new(5, 0.0010f, -0.48f);
  135. public float haze_density = 0.7f;
  136. public float haze_horizon = 0.19f;
  137. public float max_y = 1605;
  138. public float star_brightness = 0f;
  139. //this is a vector3 now, but all viewers expect a vector4, so keeping like this for now
  140. public Vector4 sunlight_color = new(0.7342f, 0.7815f, 0.9f, 0.3f);
  141. public string Name = "DefaultSky";
  142. public float cloud_variance = 0;
  143. public float dome_offset = 0.96f;
  144. public float dome_radius = 15000f;
  145. public float droplet_radius = 800.0f;
  146. public float ice_level = 0f;
  147. public bool HasRefProbe = false;
  148. public float reflectionProbeAmbiance = 0f;
  149. public float moisture_level = 0;
  150. public float sky_bottom_radius = 6360;
  151. public float sky_top_radius = 6420;
  152. public float sun_arc_radians = 0.00045f;
  153. public Quaternion sun_rotation = new(0, -0.3824995f, 0, 0.9239557f);
  154. public float sun_scale = 1;
  155. public float moon_brightness = 0.5f;
  156. public Quaternion moon_rotation = new(0, 0.9239557f, 0, 0.3824995f);
  157. public float moon_scale = 1;
  158. public float planet_radius = 6360f;
  159. public void FromWLOSD(string name, OSD osd)
  160. {
  161. Vector4 v4tmp;
  162. OSDMap map = osd as OSDMap;
  163. v4tmp = map["ambient"];
  164. ambient = new Vector3(v4tmp.X, v4tmp.Y, v4tmp.Z);
  165. v4tmp = map["blue_density"];
  166. blue_density = new Vector3(v4tmp.X, v4tmp.Y, v4tmp.Z);
  167. v4tmp = map["blue_horizon"];
  168. blue_horizon = new Vector3(v4tmp.X, v4tmp.Y, v4tmp.Z);
  169. v4tmp = map["cloud_color"];
  170. cloud_color = new Vector3(v4tmp.X, v4tmp.Y, v4tmp.Z);
  171. v4tmp = map["cloud_pos_density1"];
  172. cloud_pos_density1 = new Vector3(v4tmp.X, v4tmp.Y, v4tmp.Z);
  173. v4tmp = map["cloud_pos_density2"];
  174. cloud_pos_density2 = new Vector3(v4tmp.X, v4tmp.Y, v4tmp.Z);
  175. v4tmp = map["cloud_scale"];
  176. cloud_scale = v4tmp.X;
  177. cloud_scroll_rate = map["cloud_scroll_rate"];
  178. cloud_scroll_rate.X -= 10f;
  179. cloud_scroll_rate.Y -= 10f;
  180. v4tmp = map["cloud_shadow"];
  181. cloud_shadow = v4tmp.X;
  182. v4tmp = map["density_multiplier"];
  183. density_multiplier = v4tmp.X;
  184. v4tmp = map["distance_multiplier"];
  185. distance_multiplier = v4tmp.X;
  186. Vector2 v2tmp = map["enable_cloud_scroll"];
  187. if (v2tmp.X == 0)
  188. cloud_scroll_rate.X = 0;
  189. if (v2tmp.Y == 0)
  190. cloud_scroll_rate.Y = 0;
  191. v4tmp = map["gamma"];
  192. gamma = v4tmp.X;
  193. v4tmp = map["glow"];
  194. glow = new Vector3(v4tmp.X, v4tmp.Y, v4tmp.Z);
  195. v4tmp = map["haze_density"];
  196. haze_density = v4tmp.X;
  197. v4tmp = map["haze_horizon"];
  198. haze_horizon = v4tmp.X;
  199. //lightnorm = map["lightnorm"];
  200. v4tmp = map["max_y"];
  201. max_y = v4tmp.X;
  202. star_brightness = map["star_brightness"] * 250.0f;
  203. sunlight_color = map["sunlight_color"];
  204. ViewerEnvironment.convertFromAngles(this, map["sun_angle"], map["east_angle"]);
  205. Name = name;
  206. }
  207. public OSD ToWLOSD()
  208. {
  209. OSDMap map = new();
  210. ViewerEnvironment.convertToAngles(this, out float sun_angle, out float east_angle, out Vector4 lightnorm);
  211. map["ambient"] = new Vector4(ambient.X, ambient.Y, ambient.Z, 1);
  212. map["blue_density"] = new Vector4(blue_density.X, blue_density.Y, blue_density.Z, 1);
  213. map["blue_horizon"] = new Vector4(blue_horizon.X, blue_horizon.Y, blue_horizon.Z, 1);
  214. map["cloud_color"] = new Vector4(cloud_color.X, cloud_color.Y, cloud_color.Z, 1);
  215. map["cloud_pos_density1"] = new Vector4(cloud_pos_density1.X, cloud_pos_density1.Y, cloud_pos_density1.Z, 1);
  216. map["cloud_pos_density2"] = new Vector4(cloud_pos_density2.X, cloud_pos_density2.Y, cloud_pos_density2.Z, 1);
  217. map["cloud_scale"] = new Vector4(cloud_scale, 0, 0, 1);
  218. map["cloud_scroll_rate"] = new Vector2(cloud_scroll_rate.X + 10f, cloud_scroll_rate.Y + 10f);
  219. map["cloud_shadow"] = new Vector4(cloud_shadow, 0, 0, 1);
  220. map["density_multiplier"] = new Vector4(density_multiplier, 0, 0, 1);
  221. map["distance_multiplier"] = new Vector4(distance_multiplier, 0, 0, 1);
  222. map["east_angle"] = east_angle;
  223. map["enable_cloud_scroll"] = new OSDArray { cloud_scroll_rate.X != 0, cloud_scroll_rate.Y != 0 };
  224. map["gamma"] = new Vector4(gamma, 0, 0, 1);
  225. map["glow"] = new Vector4(glow.X, glow.Y, glow.Z, 1);
  226. map["haze_density"] = new Vector4(haze_density, 0, 0, 1);
  227. map["haze_horizon"] = new Vector4(haze_horizon, 0, 0, 1);
  228. map["lightnorm"] = lightnorm;
  229. map["max_y"] = new Vector4(max_y, 0, 0, 1);
  230. map["name"] = Name;
  231. map["star_brightness"] = star_brightness / 250.0f;
  232. map["sun_angle"] = sun_angle;
  233. map["sunlight_color"] = sunlight_color;
  234. return map;
  235. }
  236. public OSD ToOSD()
  237. {
  238. OSDMap map = new(64)
  239. {
  240. ["absorption_config"] = new OSDArray() { abscoefA.ToOSD(), abscoefB.ToOSD() },
  241. ["bloom_id"] = bloom_id,
  242. ["cloud_color"] = cloud_color,
  243. ["cloud_id"] = cloud_id,
  244. ["cloud_pos_density1"] = cloud_pos_density1,
  245. ["cloud_pos_density2"] = cloud_pos_density2,
  246. ["cloud_scale"] = cloud_scale,
  247. ["cloud_scroll_rate"] = cloud_scroll_rate,
  248. ["cloud_shadow"] = cloud_shadow,
  249. ["cloud_variance"] = cloud_variance,
  250. ["dome_offset"] = dome_offset,
  251. ["dome_radius"] = dome_radius,
  252. ["droplet_radius"] = droplet_radius,
  253. ["gamma"] = gamma,
  254. ["glow"] = glow,
  255. ["halo_id"] = halo_id,
  256. ["ice_level"] = ice_level,
  257. ["legacy_haze"] = new OSDMap()
  258. {
  259. ["ambient"] = ambient,
  260. ["blue_density"] = blue_density,
  261. ["blue_horizon"] = blue_horizon,
  262. ["density_multiplier"] = density_multiplier,
  263. ["distance_multiplier"] = distance_multiplier,
  264. ["haze_density"] = haze_density,
  265. ["haze_horizon"] = haze_horizon
  266. },
  267. ["max_y"] = max_y,
  268. ["moisture_level"] = moisture_level,
  269. ["moon_brightness"] = moon_brightness,
  270. ["moon_id"] = moon_id,
  271. ["moon_rotation"] = moon_rotation,
  272. ["moon_scale"] = moon_scale,
  273. ["planet_radius"] = planet_radius,
  274. ["rainbow_id"] = rainbow_id,
  275. ["sky_bottom_radius"] = sky_bottom_radius,
  276. ["sky_top_radius"] = sky_top_radius,
  277. ["star_brightness"] = star_brightness,
  278. ["sun_arc_radians"] = sun_arc_radians,
  279. ["sun_id"] = sun_id,
  280. ["sun_rotation"] = sun_rotation,
  281. ["sun_scale"] = sun_scale,
  282. ["sunlight_color"] = sunlight_color,
  283. ["mie_config"] = new OSDArray() { mieconf.ToOSD() },
  284. ["rayleigh_config"] = new OSDArray() { rayleigh_config.ToOSD() },
  285. ["type"] = "sky"
  286. };
  287. if(HasRefProbe)
  288. map["reflection_probe_ambiance"] = reflectionProbeAmbiance;
  289. return map;
  290. }
  291. public void FromOSD(string name, OSDMap map)
  292. {
  293. OSD otmp;
  294. if (map.TryGetValue("absorption_config", out otmp) && otmp is OSDArray absorptionArray)
  295. {
  296. if (absorptionArray.Count > 0)
  297. {
  298. abscoefA.FromOSD(absorptionArray[0] as OSDMap);
  299. if (absorptionArray.Count > 1)
  300. abscoefA.FromOSD(absorptionArray[1] as OSDMap);
  301. }
  302. }
  303. if (map.TryGetValue("bloom_id", out otmp))
  304. bloom_id = otmp;
  305. if (map.TryGetValue("cloud_color", out otmp))
  306. cloud_color = otmp;
  307. if (map.TryGetValue("cloud_id", out otmp))
  308. cloud_id = otmp;
  309. if (map.TryGetValue("cloud_pos_density1", out otmp))
  310. cloud_pos_density1 = otmp;
  311. if (map.TryGetValue("cloud_pos_density2", out otmp))
  312. cloud_pos_density2 = otmp;
  313. if (map.TryGetValue("cloud_scale", out otmp))
  314. cloud_scale = otmp;
  315. if (map.TryGetValue("cloud_scroll_rate", out otmp))
  316. cloud_scroll_rate = otmp;
  317. if (map.TryGetValue("cloud_shadow", out otmp))
  318. cloud_shadow = otmp;
  319. if (map.TryGetValue("cloud_variance", out otmp))
  320. cloud_variance = otmp;
  321. if (map.TryGetValue("dome_offset", out otmp))
  322. dome_offset = otmp;
  323. if (map.TryGetValue("dome_radius", out otmp))
  324. dome_radius = otmp;
  325. if (map.TryGetValue("droplet_radius", out otmp))
  326. droplet_radius = otmp;
  327. if (map.TryGetValue("gamma", out otmp))
  328. gamma = otmp;
  329. if (map.TryGetValue("glow", out otmp))
  330. glow = otmp;
  331. if (map.TryGetValue("halo_id", out otmp))
  332. halo_id = otmp;
  333. if (map.TryGetValue("ice_level", out otmp))
  334. ice_level = otmp;
  335. if (map.TryGetValue("reflection_probe_ambiance", out otmp))
  336. {
  337. reflectionProbeAmbiance = otmp;
  338. HasRefProbe = true;
  339. }
  340. if (map.TryGetValue("legacy_haze", out OSD tmp) && tmp is OSDMap lHaze)
  341. {
  342. if (lHaze.TryGetValue("ambient", out otmp))
  343. ambient = otmp;
  344. if (lHaze.TryGetValue("blue_density", out otmp))
  345. blue_density = otmp;
  346. if (lHaze.TryGetValue("blue_horizon", out otmp))
  347. blue_horizon = otmp;
  348. if (lHaze.TryGetValue("density_multiplier", out otmp))
  349. density_multiplier = otmp;
  350. if (lHaze.TryGetValue("distance_multiplier", out otmp))
  351. distance_multiplier = otmp;
  352. if (lHaze.TryGetValue("haze_density", out otmp))
  353. haze_density = otmp;
  354. if (lHaze.TryGetValue("haze_horizon", out otmp))
  355. haze_horizon = otmp;
  356. }
  357. if (map.TryGetValue("max_y", out otmp))
  358. max_y = otmp;
  359. if (map.TryGetValue("mie_config", out otmp) && otmp is OSDArray mieArray)
  360. {
  361. if (mieArray.Count > 0)
  362. mieconf.FromOSD(mieArray[0] as OSDMap);
  363. }
  364. if (map.TryGetValue("moisture_level", out otmp))
  365. moisture_level = otmp;
  366. if (map.TryGetValue("moon_brightness", out otmp))
  367. moon_brightness = otmp;
  368. if (map.TryGetValue("moon_id", out otmp))
  369. moon_id = otmp;
  370. if (map.TryGetValue("moon_rotation", out otmp))
  371. moon_rotation = otmp;
  372. if (map.TryGetValue("moon_scale", out otmp))
  373. moon_scale = otmp;
  374. if (map.TryGetValue("planet_radius", out otmp))
  375. planet_radius = otmp;
  376. if (map.TryGetValue("rainbow_id", out otmp))
  377. rainbow_id = otmp;
  378. if (map.TryGetValue("rayleigh_config", out otmp) && otmp is OSDArray rayleighArray)
  379. {
  380. if (rayleighArray.Count > 0)
  381. rayleigh_config.FromOSD(rayleighArray[0] as OSDMap);
  382. }
  383. if (map.TryGetValue("sky_bottom_radius", out otmp))
  384. sky_bottom_radius = otmp;
  385. if (map.TryGetValue("sky_top_radius", out otmp))
  386. sky_top_radius = otmp;
  387. if (map.TryGetValue("star_brightness", out otmp))
  388. star_brightness = otmp;
  389. if (map.TryGetValue("sun_arc_radians", out otmp))
  390. sun_arc_radians = otmp;
  391. if (map.TryGetValue("sun_id", out otmp))
  392. sun_id = otmp;
  393. if (map.TryGetValue("sun_rotation", out otmp))
  394. sun_rotation = otmp;
  395. if (map.TryGetValue("sun_scale", out otmp))
  396. sun_scale = otmp;
  397. if (map.TryGetValue("sunlight_color", out otmp) && otmp is OSDArray sunlightArray)
  398. {
  399. if(sunlightArray.Count == 4)
  400. sunlight_color = otmp;
  401. else
  402. {
  403. Vector3 tv = otmp;
  404. sunlight_color = new Vector4(tv.X, tv.Y, tv.Z, 0);
  405. }
  406. }
  407. Name = name;
  408. }
  409. public void GatherAssets(Dictionary<UUID, sbyte> uuids)
  410. {
  411. Util.AddToGatheredIds(uuids, bloom_id, (sbyte)AssetType.Texture);
  412. Util.AddToGatheredIds(uuids, cloud_id, (sbyte)AssetType.Texture);
  413. Util.AddToGatheredIds(uuids, halo_id, (sbyte)AssetType.Texture);
  414. Util.AddToGatheredIds(uuids, moon_id, (sbyte)AssetType.Texture);
  415. Util.AddToGatheredIds(uuids, rainbow_id, (sbyte)AssetType.Texture);
  416. Util.AddToGatheredIds(uuids, sun_id, (sbyte)AssetType.Texture);
  417. }
  418. }
  419. }