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 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 = 0;
  143. public float moisture_level = 0;
  144. public float sky_bottom_radius = 6360;
  145. public float sky_top_radius = 6420;
  146. public float sun_arc_radians = 0.00045f;
  147. public Quaternion sun_rotation = new Quaternion(0, -0.3824995f, 0, 0.9239557f);
  148. public float sun_scale = 1;
  149. public float moon_brightness = 0.5f;
  150. public Quaternion moon_rotation = new Quaternion(0, 0.9239557f, 0, 0.3824995f);
  151. public float moon_scale = 1;
  152. public float planet_radius = 6360f;
  153. public void FromWLOSD(string name, OSD osd)
  154. {
  155. Vector4 v4tmp;
  156. OSDMap map = osd as OSDMap;
  157. v4tmp = map["ambient"];
  158. ambient = new Vector3(v4tmp.X, v4tmp.Y, v4tmp.Z);
  159. v4tmp = map["blue_density"];
  160. blue_density = new Vector3(v4tmp.X, v4tmp.Y, v4tmp.Z);
  161. v4tmp = map["blue_horizon"];
  162. blue_horizon = new Vector3(v4tmp.X, v4tmp.Y, v4tmp.Z);
  163. v4tmp = map["cloud_color"];
  164. cloud_color = new Vector3(v4tmp.X, v4tmp.Y, v4tmp.Z);
  165. v4tmp = map["cloud_pos_density1"];
  166. cloud_pos_density1 = new Vector3(v4tmp.X, v4tmp.Y, v4tmp.Z);
  167. v4tmp = map["cloud_pos_density2"];
  168. cloud_pos_density2 = new Vector3(v4tmp.X, v4tmp.Y, v4tmp.Z);
  169. v4tmp = map["cloud_scale"];
  170. cloud_scale = v4tmp.X;
  171. cloud_scroll_rate = map["cloud_scroll_rate"];
  172. cloud_scroll_rate.X -= 10f;
  173. cloud_scroll_rate.Y -= 10f;
  174. v4tmp = map["cloud_shadow"];
  175. cloud_shadow = v4tmp.X;
  176. v4tmp = map["density_multiplier"];
  177. density_multiplier = v4tmp.X;
  178. v4tmp = map["distance_multiplier"];
  179. distance_multiplier = v4tmp.X;
  180. Vector2 v2tmp = map["enable_cloud_scroll"];
  181. if (v2tmp.X == 0)
  182. cloud_scroll_rate.X = 0;
  183. if (v2tmp.Y == 0)
  184. cloud_scroll_rate.Y = 0;
  185. v4tmp = map["gamma"];
  186. gamma = v4tmp.X;
  187. v4tmp = map["glow"];
  188. glow = new Vector3(v4tmp.X, v4tmp.Y, v4tmp.Z);
  189. v4tmp = map["haze_density"];
  190. haze_density = v4tmp.X;
  191. v4tmp = map["haze_horizon"];
  192. haze_horizon = v4tmp.X;
  193. //lightnorm = map["lightnorm"];
  194. v4tmp = map["max_y"];
  195. max_y = v4tmp.X;
  196. star_brightness = map["star_brightness"] * 250.0f;
  197. sunlight_color = map["sunlight_color"];
  198. ViewerEnvironment.convertFromAngles(this, map["sun_angle"], map["east_angle"]);
  199. Name = name;
  200. }
  201. public OSD ToWLOSD()
  202. {
  203. OSDMap map = new OSDMap();
  204. float sun_angle;
  205. float east_angle;
  206. Vector4 lightnorm;
  207. ViewerEnvironment.convertToAngles(this, out sun_angle, out east_angle, out lightnorm);
  208. map["ambient"] = new Vector4(ambient.X, ambient.Y, ambient.Z, 1);
  209. map["blue_density"] = new Vector4(blue_density.X, blue_density.Y, blue_density.Z, 1);
  210. map["blue_horizon"] = new Vector4(blue_horizon.X, blue_horizon.Y, blue_horizon.Z, 1);
  211. map["cloud_color"] = new Vector4(cloud_color.X, cloud_color.Y, cloud_color.Z, 1);;
  212. map["cloud_pos_density1"] = new Vector4(cloud_pos_density1.X, cloud_pos_density1.Y, cloud_pos_density1.Z, 1);
  213. map["cloud_pos_density2"] = new Vector4(cloud_pos_density2.X, cloud_pos_density2.Y, cloud_pos_density2.Z, 1);
  214. map["cloud_scale"] = new Vector4(cloud_scale, 0, 0, 1);
  215. map["cloud_scroll_rate"] = new Vector2(cloud_scroll_rate.X + 10f, cloud_scroll_rate.Y + 10f);
  216. map["cloud_shadow"] = new Vector4(cloud_shadow, 0, 0, 1);
  217. map["density_multiplier"] = new Vector4(density_multiplier, 0, 0, 1);
  218. map["distance_multiplier"] = new Vector4(distance_multiplier, 0, 0, 1);
  219. map["east_angle"] = east_angle;
  220. map["enable_cloud_scroll"] = new OSDArray { cloud_scroll_rate.X != 0, cloud_scroll_rate.Y != 0 };
  221. map["gamma"] = new Vector4(gamma, 0, 0, 1);
  222. map["glow"] = new Vector4(glow.X, glow.Y, glow.Z, 1);
  223. map["haze_density"] = new Vector4(haze_density, 0, 0, 1);
  224. map["haze_horizon"] = new Vector4(haze_horizon, 0, 0, 1);
  225. map["lightnorm"] = lightnorm;
  226. map["max_y"] = new Vector4(max_y, 0, 0, 1);
  227. map["name"] = Name;
  228. map["star_brightness"] = star_brightness / 250.0f;
  229. map["sun_angle"] = sun_angle;
  230. map["sunlight_color"] = sunlight_color;
  231. return map;
  232. }
  233. public OSD ToOSD()
  234. {
  235. OSDMap map = new OSDMap(64);
  236. OSDArray abscfg = new OSDArray(2);
  237. abscfg.Add(abscoefA.ToOSD());
  238. abscfg.Add(abscoefB.ToOSD());
  239. map["absorption_config"] = abscfg;
  240. map["bloom_id"] = bloom_id;
  241. map["cloud_color"] = cloud_color;
  242. map["cloud_id"] = cloud_id;
  243. map["cloud_pos_density1"] = cloud_pos_density1;
  244. map["cloud_pos_density2"] = cloud_pos_density2;
  245. map["cloud_scale"] = cloud_scale;
  246. map["cloud_scroll_rate"] = cloud_scroll_rate;
  247. map["cloud_shadow"] = cloud_shadow;
  248. map["cloud_variance"] = cloud_variance;
  249. map["dome_offset"] = dome_offset;
  250. map["dome_radius"] = dome_radius;
  251. map["droplet_radius"] = droplet_radius;
  252. map["gamma"] = gamma;
  253. map["glow"] = glow;
  254. map["halo_id"] = halo_id;
  255. map["ice_level"] = ice_level;
  256. OSDMap lhaze = new OSDMap();
  257. lhaze["ambient"] = ambient;
  258. lhaze["blue_density"] = blue_density;
  259. lhaze["blue_horizon"] = blue_horizon;
  260. lhaze["density_multiplier"] = density_multiplier;
  261. lhaze["distance_multiplier"] = distance_multiplier;
  262. lhaze["haze_density"] = haze_density;
  263. lhaze["haze_horizon"] = haze_horizon;
  264. map["legacy_haze"] = lhaze;
  265. map["max_y"] = max_y;
  266. OSDArray miecfg = new OSDArray();
  267. miecfg.Add(mieconf.ToOSD());
  268. map["mie_config"] = miecfg;
  269. map["moisture_level"] = moisture_level;
  270. map["moon_brightness"] = moon_brightness;
  271. map["moon_id"] = moon_id;
  272. map["moon_rotation"] = moon_rotation;
  273. map["moon_scale"] = moon_scale;
  274. map["planet_radius"] = planet_radius;
  275. map["rainbow_id"] = rainbow_id;
  276. OSDArray rayl = new OSDArray();
  277. rayl.Add(rayleigh_config.ToOSD());
  278. map["rayleigh_config"] = rayl;
  279. map["sky_bottom_radius"] = sky_bottom_radius;
  280. map["sky_top_radius"] = sky_top_radius;
  281. map["star_brightness"] = star_brightness;
  282. map["sun_arc_radians"] = sun_arc_radians;
  283. map["sun_id"] = sun_id;
  284. map["sun_rotation"] = sun_rotation;
  285. map["sun_scale"] = sun_scale;
  286. map["sunlight_color"] = sunlight_color;
  287. map["type"] = "sky";
  288. return map;
  289. }
  290. public void FromOSD(string name, OSDMap map)
  291. {
  292. OSDArray tmpArray;
  293. OSD otmp;
  294. if (map.TryGetValue("absorption_config",out otmp) && otmp is OSDArray)
  295. {
  296. tmpArray = otmp as OSDArray;
  297. if (tmpArray.Count > 0)
  298. {
  299. abscoefA.FromOSD(tmpArray[0] as OSDMap);
  300. if (tmpArray.Count > 1)
  301. abscoefA.FromOSD(tmpArray[1] as OSDMap);
  302. }
  303. }
  304. if (map.TryGetValue("bloom_id", out otmp))
  305. bloom_id = otmp;
  306. if (map.TryGetValue("cloud_color", out otmp))
  307. cloud_color = otmp;
  308. if (map.TryGetValue("cloud_id", out otmp))
  309. cloud_id = otmp;
  310. if (map.TryGetValue("cloud_pos_density1", out otmp))
  311. cloud_pos_density1 = otmp;
  312. if (map.TryGetValue("cloud_pos_density2", out otmp))
  313. cloud_pos_density2 = otmp;
  314. if (map.TryGetValue("cloud_scale", out otmp))
  315. cloud_scale = otmp;
  316. if (map.TryGetValue("cloud_scroll_rate", out otmp))
  317. cloud_scroll_rate = otmp;
  318. if (map.TryGetValue("cloud_shadow", out otmp))
  319. cloud_shadow = otmp;
  320. if (map.TryGetValue("cloud_variance", out otmp))
  321. cloud_variance = otmp;
  322. if (map.TryGetValue("dome_offset", out otmp))
  323. dome_offset = otmp;
  324. if (map.TryGetValue("dome_radius", out otmp))
  325. dome_radius = otmp;
  326. if (map.TryGetValue("droplet_radius", out otmp))
  327. droplet_radius = otmp;
  328. if (map.TryGetValue("gamma", out otmp))
  329. gamma = otmp;
  330. if (map.TryGetValue("glow", out otmp))
  331. glow = otmp;
  332. if (map.TryGetValue("halo_id", out otmp))
  333. halo_id = otmp;
  334. if (map.TryGetValue("ice_level", out otmp))
  335. halo_id = otmp;
  336. if (map.TryGetValue("legacy_haze", out OSD tmp) && tmp is OSDMap)
  337. {
  338. OSDMap lHaze = tmp as OSDMap;
  339. if (lHaze.TryGetValue("ambient", out otmp))
  340. ambient = otmp;
  341. if (lHaze.TryGetValue("blue_density", out otmp))
  342. blue_density = otmp;
  343. if (lHaze.TryGetValue("blue_horizon", out otmp))
  344. blue_horizon = otmp;
  345. if (lHaze.TryGetValue("density_multiplier", out otmp))
  346. density_multiplier = otmp;
  347. if (lHaze.TryGetValue("distance_multiplier", out otmp))
  348. distance_multiplier = otmp;
  349. if (lHaze.TryGetValue("haze_density", out otmp))
  350. haze_density = otmp;
  351. if (lHaze.TryGetValue("haze_horizon", out otmp))
  352. haze_horizon = otmp;
  353. }
  354. if (map.TryGetValue("max_y", out otmp))
  355. max_y = otmp;
  356. if (map.TryGetValue("mie_config", out otmp) && otmp is OSDArray)
  357. {
  358. tmpArray = otmp as OSDArray;
  359. if (tmpArray.Count > 0)
  360. mieconf.FromOSD(tmpArray[0] as OSDMap);
  361. }
  362. if (map.TryGetValue("moisture_level", out otmp))
  363. moisture_level = otmp;
  364. if (map.TryGetValue("moon_brightness", out otmp))
  365. moon_brightness = otmp;
  366. if (map.TryGetValue("moon_id", out otmp))
  367. moon_id = otmp;
  368. if (map.TryGetValue("moon_rotation", out otmp))
  369. moon_rotation = otmp;
  370. if (map.TryGetValue("moon_scale", out otmp))
  371. moon_scale = otmp;
  372. if (map.TryGetValue("planet_radius", out otmp))
  373. planet_radius = otmp;
  374. if (map.TryGetValue("rainbow_id", out otmp))
  375. rainbow_id = otmp;
  376. if (map.TryGetValue("rayleigh_config", out otmp) && otmp is OSDArray)
  377. {
  378. tmpArray = otmp as OSDArray;
  379. if (tmpArray.Count > 0)
  380. rayleigh_config.FromOSD(tmpArray[0] as OSDMap);
  381. }
  382. if (map.TryGetValue("sky_bottom_radius", out otmp))
  383. sky_bottom_radius = otmp;
  384. if (map.TryGetValue("sky_top_radius", out otmp))
  385. sky_top_radius = otmp;
  386. if (map.TryGetValue("star_brightness", out otmp))
  387. star_brightness = otmp;
  388. if (map.TryGetValue("sun_arc_radians", out otmp))
  389. sun_arc_radians = otmp;
  390. if (map.TryGetValue("sun_id", out otmp))
  391. sun_id = otmp;
  392. if (map.TryGetValue("sun_rotation", out otmp))
  393. sun_rotation = otmp;
  394. if (map.TryGetValue("sun_scale", out otmp))
  395. sun_scale = otmp;
  396. if (map.TryGetValue("sunlight_color", out otmp) && otmp is OSDArray)
  397. {
  398. tmpArray = otmp as OSDArray;
  399. if(tmpArray.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. }