Phi: nicer scroll animation for METAR widget
[fg:fgdata.git] / Nasal / earthview.nas
1 ###########################################################
2 # Earthview orbital rendering
3 ###########################################################
4
5 var start = func() {
6
7 if (earthview_running_flag ==1) {return;}
8
9 earthview_running_flag = 1;
10
11 var lat = getprop("/position/latitude-deg");
12 var lon = getprop("/position/longitude-deg");
13
14 earth_model.node = earthview.place_earth_model("Models/Astro/earth.xml",lat, lon, 0.0, 0.0, 0.0, 0.0);
15 cloudsphere_model.node = earthview.place_earth_model("Models/Astro/cloudsphere.xml",lat, lon, 0.0, 0.0, 0.0, 0.0);
16
17 # set Basic Weather off
18 props.globals.getNode("/environment/config/enabled").setBoolValue(0);
19 props.globals.getNode("/environment/params/metar-updates-environment").setBoolValue(0);
20
21 # set some reasonable defaults
22
23 setprop("/environment/visibility-m", 80000.0);
24 setprop("/sim/rendering/mie", 0.0);
25 setprop("/sim/rendering/rayleigh", 0.00002);
26 setprop("/sim/rendering/dome-density", 1.0);
27
28 control_loop();
29
30 }
31
32 var stop = func () {
33
34 earth_model.node.remove();
35 cloudsphere_model.node.remove();
36 setprop("/earthview/control_loop_flag",0);
37
38 earthview_running_flag = 0;
39 }
40
41 var place_earth_model = func(path, lat, lon, alt, heading, pitch, roll) {
42
43
44
45 var m = props.globals.getNode("models", 1);
46                 for (var i = 0; 1; i += 1)
47                         if (m.getChild("model", i, 0) == nil)
48                                 break;
49 var model = m.getChild("model", i, 1);
50
51 var R1 = 5800000.0;
52 var R2 = 58000.0;
53
54 var altitude1 = getprop("/position/altitude-ft");
55 var altitude2 = R2/R1 * altitude1;
56 var model_alt = altitude1 - altitude2 - R2 * m_to_ft;
57
58 setprop("/earthview/latitude-deg", lat);
59 setprop("/earthview/longitude-deg", lon);
60 setprop("/earthview/elevation-ft", model_alt);
61 setprop("/earthview/heading-deg", heading);
62 setprop("/earthview/pitch-deg", pitch);
63 setprop("/earthview/roll-deg", roll);
64 setprop("/earthview/yaw-deg", 0.0);
65
66 var eview = props.globals.getNode("earthview", 1);
67 var latN = eview.getNode("latitude-deg",1);
68 var lonN = eview.getNode("longitude-deg",1);
69 var altN = eview.getNode("elevation-ft",1);
70 var headN = eview.getNode("heading-deg",1);
71 var pitchN = eview.getNode("pitch-deg",1);
72 var rollN = eview.getNode("roll-deg",1);
73
74
75
76 model.getNode("path", 1).setValue(path);
77 model.getNode("latitude-deg-prop", 1).setValue(latN.getPath());
78 model.getNode("longitude-deg-prop", 1).setValue(lonN.getPath());
79 model.getNode("elevation-ft-prop", 1).setValue(altN.getPath());
80 model.getNode("heading-deg-prop", 1).setValue(headN.getPath());
81 model.getNode("pitch-deg-prop", 1).setValue(pitchN.getPath());
82 model.getNode("roll-deg-prop", 1).setValue(rollN.getPath());
83 model.getNode("load", 1).remove();
84
85 setprop("/earthview/heading-deg",90);
86 setprop("/earthview/control_loop_flag",1);
87
88
89
90 return model;
91 }
92
93
94 var control_loop = func {
95
96 var R1 = 5800000.0;
97 var R2 = 58000.0;
98
99 var altitude1 = getprop("/position/altitude-ft");
100 var altitude2 = R2/R1 * altitude1;
101 var model_alt = altitude1 - altitude2 - R2 * m_to_ft;
102
103 setprop("/earthview/elevation-ft", model_alt);
104
105 var lat = getprop("/position/latitude-deg");
106 var lon = getprop("/position/longitude-deg");
107
108 setprop("/earthview/latitude-deg", lat);
109 setprop("/earthview/longitude-deg", lon);
110
111 setprop("/earthview/roll-deg", -(90-lat));
112 setprop("/earthview/yaw-deg", -lon);
113
114 # now set scattering paramaters
115
116 if (getprop("/earthview/mrd-flag") == 1)
117         {
118         var rayleigh = 0.0002;
119         var mie = 0.001;
120         var density = 1.0;
121         
122         if (altitude1 < 300000.0)
123                 {
124                 setprop("/sim/rendering/rayleigh",rayleigh);
125                 setprop("/sim/rendering/mie",mie);
126                 setprop("/sim/rendering/dome-density",density);
127                 }
128         else if (altitude1 < 650000.0)
129                 {
130                 rayleigh = rayleigh - 0.00018 * (altitude1-300000.0)/350000.0;
131                 mie = mie - 0.001 * (altitude1-300000.0)/350000.0;
132                 density = 1.0;
133                 setprop("/sim/rendering/rayleigh",rayleigh);
134                 setprop("/sim/rendering/mie",mie);
135                 setprop("/sim/rendering/dome-density",density);
136                 }
137         else
138                 {
139                 rayleigh = 0.00002;
140                 mie = 0.0;
141                 density = 1.0;
142                 setprop("/sim/rendering/rayleigh",rayleigh);
143                 setprop("/sim/rendering/mie",mie);
144                 setprop("/sim/rendering/dome-density",density);
145                 }
146         
147         }
148
149
150 if (getprop("/earthview/control_loop_flag") ==1) {settimer( func {control_loop(); },0);}
151 }
152
153
154 var ft_to_m = 0.30480;
155 var m_to_ft = 1.0/ft_to_m;
156 var earth_model = {};
157 var cloudsphere_model = {};
158 var earthview_running_flag = 0;
159
160