Lighting.html 7.17 KB
Newer Older
Schaaf's avatar
Schaaf committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"
    />
    <meta name="description" content="Set custom light direction and color." />
    <meta name="cesium-sandcastle-labels" content="Showcases" />
    <title>Cesium Demo</title>
    <script type="text/javascript" src="../Sandcastle-header.js"></script>
    <script
      type="text/javascript"
      src="../../../Build/CesiumUnminified/Cesium.js"
      nomodule
    ></script>
    <script type="module" src="../load-cesium-es6.js"></script>
  </head>
  <body
    class="sandcastle-loading"
    data-sandcastle-bucket="bucket-requirejs.html"
  >
    <style>
      @import url(../templates/bucket.css);
    </style>
    <div id="cesiumContainer" class="fullSize"></div>
    <div id="loadingOverlay"><h1>Loading...</h1></div>
    <div id="toolbar"></div>
    <script id="cesium_sandcastle_script">
      function startup(Cesium) {
        "use strict";
        //Sandcastle_Begin
        var viewer = new Cesium.Viewer("cesiumContainer", {
          terrainProvider: Cesium.createWorldTerrain({
            requestWaterMask: true,
            requestVertexNormals: true,
          }),
        });

        var scene = viewer.scene;
        scene.globe.enableLighting = true;

        var scratchIcrfToFixed = new Cesium.Matrix3();
        var scratchMoonPosition = new Cesium.Cartesian3();
        var scratchMoonDirection = new Cesium.Cartesian3();

        function getMoonDirection(result) {
          result = Cesium.defined(result) ? result : new Cesium.Cartesian3();
          var icrfToFixed = scratchIcrfToFixed;
          var date = viewer.clock.currentTime;
          if (
            !Cesium.defined(
              Cesium.Transforms.computeIcrfToFixedMatrix(date, icrfToFixed)
            )
          ) {
            Cesium.Transforms.computeTemeToPseudoFixedMatrix(date, icrfToFixed);
          }
          var moonPosition = Cesium.Simon1994PlanetaryPositions.computeMoonPositionInEarthInertialFrame(
            date,
            scratchMoonPosition
          );
          Cesium.Matrix3.multiplyByVector(
            icrfToFixed,
            moonPosition,
            moonPosition
          );
          var moonDirection = Cesium.Cartesian3.normalize(
            moonPosition,
            scratchMoonDirection
          );
          return Cesium.Cartesian3.negate(moonDirection, result);
        }

        var directionalLight = new Cesium.DirectionalLight({
          direction: new Cesium.Cartesian3(
            0.2454278300540191,
            0.8842635425193919,
            0.39729481195458805
          ),
        });

        var flashlight = new Cesium.DirectionalLight({
          direction: scene.camera.directionWC, // Updated every frame
        });

        var moonLight = new Cesium.DirectionalLight({
          direction: getMoonDirection(), // Updated every frame
          color: new Cesium.Color(0.9, 0.925, 1.0),
          intensity: 0.5,
        });

        var sunLight = new Cesium.SunLight();

        var customColorLight = new Cesium.DirectionalLight({
          direction: new Cesium.Cartesian3(
            -0.2454278300540191,
            0.8842635425193919,
            0.39729481195458805
          ),
          color: Cesium.Color.fromCssColorString("#deca7c"),
        });

        scene.preRender.addEventListener(function (scene, time) {
          if (scene.light === flashlight) {
            scene.light.direction = Cesium.Cartesian3.clone(
              scene.camera.directionWC,
              scene.light.direction
            );
          } else if (scene.light === moonLight) {
            scene.light.direction = getMoonDirection(scene.light.direction);
          }
        });

        viewer.entities.add({
          position: Cesium.Cartesian3.fromRadians(
            -2.1463338399937277,
            0.6677959688982861,
            32.18991401746337
          ),
          model: {
            uri: "../../SampleData/models/CesiumBalloon/CesiumBalloon.glb",
            scale: 7.0,
          },
        });

        viewer.entities.add({
          position: Cesium.Cartesian3.fromRadians(
            -2.14633449752228,
            0.667796065242357,
            24.47647034111423
          ),
          cylinder: {
            length: 8.0,
            topRadius: 2.0,
            bottomRadius: 2.0,
            material: Cesium.Color.WHITE,
          },
        });

        viewer.entities.add({
          position: Cesium.Cartesian3.fromRadians(
            -2.1463332294173365,
            0.6677959755384729,
            26.2876064083145
          ),
          ellipsoid: {
            radii: new Cesium.Cartesian3(2.5, 2.5, 2.5),
            material: Cesium.Color.WHITE.withAlpha(0.5),
          },
        });

        function setTime(iso8601) {
          var currentTime = Cesium.JulianDate.fromIso8601(iso8601);
          var endTime = Cesium.JulianDate.addDays(
            currentTime,
            2,
            new Cesium.JulianDate()
          );

          viewer.clock.currentTime = currentTime;
          viewer.timeline.zoomTo(currentTime, endTime);
        }

        function reset() {
          // Set scene defaults
          scene.light = sunLight;
          scene.globe.dynamicAtmosphereLighting = true;
          scene.globe.dynamicAtmosphereLightingFromSun = false;
          setTime("2020-01-09T23:00:39.018261982600961346Z");
        }

        viewer.scene.camera.setView({
          destination: new Cesium.Cartesian3(
            -2729490.8390059783,
            -4206389.878855597,
            3928671.2763356343
          ),
          orientation: new Cesium.HeadingPitchRoll(
            2.2482480507178426,
            -0.20084951548781982,
            0.002593933673552762
          ),
          endTransform: Cesium.Matrix4.IDENTITY,
        });

        var options = [
          {
            text: "Fixed lighting",
            onselect: function () {
              reset();
              scene.light = directionalLight;
            },
          },
          {
            text: "Flashlight",
            onselect: function () {
              reset();
              scene.light = flashlight;
              scene.globe.dynamicAtmosphereLighting = false;
            },
          },
          {
            text: "Moonlight",
            onselect: function () {
              reset();
              scene.light = moonLight;
              scene.globe.dynamicAtmosphereLightingFromSun = true;
              setTime("2020-01-10T05:29:41.17946898164518643Z");
            },
          },
          {
            text: "Sunlight",
            onselect: function () {
              reset();
            },
          },
          {
            text: "Custom color",
            onselect: function () {
              reset();
              scene.light = customColorLight;
            },
          },
        ];

        Sandcastle.addToolbarMenu(options);
        //Sandcastle_End
        Sandcastle.finishedLoading();
      }
      if (typeof Cesium !== "undefined") {
        window.startupCalled = true;
        startup(Cesium);
      }
    </script>
  </body>
</html>