Image-Based Lighting.html 6.9 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
<!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="Use image-based lighting to light a model."
    />
    <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">
      <table>
        <tbody>
          <tr>
            <td>Luminance at Zenith</td>
            <td>
              <input
                type="range"
                min="0.0"
                max="2.0"
                step="0.01"
                data-bind="value: luminanceAtZenith, valueUpdate: 'input'"
              />
              <input
                type="text"
                size="4"
                data-bind="value: luminanceAtZenith"
              />
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <script id="cesium_sandcastle_script">
      function startup(Cesium) {
        "use strict";
        //Sandcastle_Begin
        var viewer = new Cesium.Viewer("cesiumContainer");

        if (!viewer.scene.specularEnvironmentMapsSupported) {
          window.alert(
            "This browser does not support specular environment maps."
          );
        }

        var environmentMapURL =
          "https://cesium.com/assets/kiara_6_afternoon_2k_ibl.ktx";
        var modelURL = "../../SampleData/models/Pawns/Pawns.glb";

        // This environment map was processed using Google's Filament project. To process your own:
        // 1 - Download the Filament release (https://github.com/google/filament/releases).
        // 2 - Run `cmgen --type=ktx --deploy=/path/to/output /path/to/image.hdr`. Other formats are also supported. Run `cmgen --help` for all options.
        // 3 - Take the generated coefficients and the KTX file and load them in CesiumJS as shown below.

        var L00 = new Cesium.Cartesian3(
          0.170455150831422,
          0.163151083190219,
          0.196966760289763
        );
        var L1_1 = new Cesium.Cartesian3(
          -0.066550267689383,
          -0.022088055746048,
          0.078835009246127
        );
        var L10 = new Cesium.Cartesian3(
          0.038364097478591,
          0.045714300098753,
          0.063498904606215
        );
        var L11 = new Cesium.Cartesian3(
          -0.01436536331281,
          -0.026490613715151,
          -0.05018940406602
        );
        var L2_2 = new Cesium.Cartesian3(
          -0.05153278691789,
          -0.050777795729986,
          -0.056449044453032
        );
        var L2_1 = new Cesium.Cartesian3(
          0.043454596136534,
          0.046672590104157,
          0.05753010764661
        );
        var L20 = new Cesium.Cartesian3(
          -0.00164046627411,
          0.001286638231156,
          0.007228908989616
        );
        var L21 = new Cesium.Cartesian3(
          -0.042260855700641,
          -0.046394335094707,
          -0.057562936365585
        );
        var L22 = new Cesium.Cartesian3(
          -0.004953478914091,
          -0.000479681664876,
          0.008508150106928
        );
        var coefficients = [L00, L1_1, L10, L11, L2_2, L2_1, L20, L21, L22];

        var height = 0.0;
        var hpr = new Cesium.HeadingPitchRoll(0.0, 0.0, 0.0);
        var origin = Cesium.Cartesian3.fromDegrees(
          -123.0744619,
          44.0503706,
          height
        );
        var modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(
          origin,
          hpr
        );

        var model = viewer.scene.primitives.add(
          Cesium.Model.fromGltf({
            url: modelURL,
            modelMatrix: modelMatrix,
            minimumPixelSize: 128,
          })
        );

        model.readyPromise
          .then(function (model) {
            var camera = viewer.camera;

            // Zoom to model
            var controller = viewer.scene.screenSpaceCameraController;
            var r =
              2.0 * Math.max(model.boundingSphere.radius, camera.frustum.near);
            controller.minimumZoomDistance = r * 0.5;

            var center = Cesium.Matrix4.multiplyByPoint(
              model.modelMatrix,
              model.boundingSphere.center,
              new Cesium.Cartesian3()
            );
            var heading = Cesium.Math.toRadians(230.0);
            var pitch = Cesium.Math.toRadians(-20.0);
            camera.lookAt(
              center,
              new Cesium.HeadingPitchRange(heading, pitch, r * 2.0)
            );
            camera.lookAtTransform(Cesium.Matrix4.IDENTITY);

            model.sphericalHarmonicCoefficients = coefficients;
            model.specularEnvironmentMaps = environmentMapURL;

            // The viewModel tracks the state of our mini application.
            var viewModel = {
              luminanceAtZenith: model.luminanceAtZenith,
            };
            // Convert the viewModel members into knockout observables.
            Cesium.knockout.track(viewModel);

            // Bind the viewModel to the DOM elements of the UI that call for it.
            var toolbar = document.getElementById("toolbar");
            Cesium.knockout.applyBindings(viewModel, toolbar);

            function subscribeParameter(name) {
              Cesium.knockout
                .getObservable(viewModel, name)
                .subscribe(function (newValue) {
                  model[name] = newValue;
                });
            }

            subscribeParameter("luminanceAtZenith");

            Sandcastle.addToggleButton(
              "Use procedural environment lighting",
              false,
              function (checked) {
                if (!checked) {
                  model.sphericalHarmonicCoefficients = coefficients;
                  model.specularEnvironmentMaps = environmentMapURL;
                } else {
                  model.sphericalHarmonicCoefficients = undefined;
                  model.specularEnvironmentMaps = undefined;
                }
              }
            );
          })
          .otherwise(function (error) {
            window.alert(error);
          });
        //Sandcastle_End
        Sandcastle.finishedLoading();
      }
      if (typeof Cesium !== "undefined") {
        window.startupCalled = true;
        startup(Cesium);
      }
    </script>
  </body>
</html>