ViewerSky.cs 20 KB

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