15832144755
2022-01-06 7b4c8991dca9cf2a809a95e239d144697d3afb56
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
import { Cartesian3, defined } from "../../Source/Cesium.js";
import { ComputeEngine } from "../../Source/Cesium.js";
import { Pass } from "../../Source/Cesium.js";
import { OctahedralProjectedCubeMap } from "../../Source/Cesium.js";
import createContext from "../createContext.js";
import createFrameState from "../createFrameState.js";
import pollToPromise from "../pollToPromise.js";
 
describe(
  "Scene/OctahedralProjectedCubeMap",
  function () {
    var context;
    var computeEngine;
    var octahedralMap;
 
    var environmentMapUrl =
      "./Data/EnvironmentMap/kiara_6_afternoon_2k_ibl.ktx2";
    var fsOctahedralMap =
      "uniform sampler2D projectedMap;" +
      "uniform vec2 textureSize;" +
      "uniform vec3 direction;" +
      "uniform float lod;" +
      "uniform float maxLod;" +
      "void main() {" +
      "   vec3 color = czm_sampleOctahedralProjection(projectedMap, textureSize, direction, lod, maxLod);" +
      "   gl_FragColor = vec4(color, 1.0);" +
      "}";
 
    var fsCubeMap =
      "uniform samplerCube cubeMap;" +
      "uniform vec3 direction;" +
      "void main() {" +
      "   vec4 rgba = textureCube(cubeMap, direction);" +
      "   gl_FragColor = vec4(rgba.rgb, 1.0);" +
      "}";
 
    beforeAll(function () {
      context = createContext();
      computeEngine = new ComputeEngine(context);
    });
 
    afterAll(function () {
      context.destroyForSpecs();
      computeEngine.destroy();
    });
 
    afterEach(function () {
      octahedralMap = octahedralMap && octahedralMap.destroy();
      context.textureCache.destroyReleasedTextures();
    });
 
    function executeCommands(frameState) {
      var length = frameState.commandList.length;
      for (var i = 0; i < length; ++i) {
        var command = frameState.commandList[i];
        if (command.pass === Pass.COMPUTE) {
          command.execute(computeEngine);
        } else {
          command.execute(context);
        }
      }
      frameState.commandList.length = 0;
    }
 
    function sampleOctahedralMap(octahedralMap, direction, lod, callback) {
      expect({
        context: context,
        fragmentShader: fsOctahedralMap,
        uniformMap: {
          projectedMap: function () {
            return octahedralMap.texture;
          },
          textureSize: function () {
            return octahedralMap.texture.dimensions;
          },
          direction: function () {
            return direction;
          },
          lod: function () {
            return lod;
          },
          maxLod: function () {
            return octahedralMap.maximumMipmapLevel;
          },
        },
      }).contextToRenderAndCall(callback);
    }
 
    function sampleCubeMap(cubeMap, direction, callback) {
      expect({
        context: context,
        fragmentShader: fsCubeMap,
        uniformMap: {
          cubeMap: function () {
            return cubeMap;
          },
          direction: function () {
            return direction;
          },
        },
      }).contextToRenderAndCall(callback);
    }
 
    function expectCubeMapAndOctahedralMapEqual(octahedralMap, direction, lod) {
      return sampleCubeMap(octahedralMap._cubeMaps[lod], direction, function (
        cubeMapColor
      ) {
        var directionFlipY = direction.clone();
        directionFlipY.y *= -1;
 
        sampleOctahedralMap(octahedralMap, directionFlipY, lod, function (
          octahedralMapColor
        ) {
          return expect(cubeMapColor).toEqualEpsilon(octahedralMapColor, 6);
        });
      });
    }
 
    it("creates a packed texture with the right dimensions", function () {
      if (!OctahedralProjectedCubeMap.isSupported(context)) {
        return;
      }
 
      octahedralMap = new OctahedralProjectedCubeMap(environmentMapUrl);
      var frameState = createFrameState(context);
 
      return pollToPromise(function () {
        octahedralMap.update(frameState);
        return octahedralMap.ready;
      }).then(function () {
        expect(octahedralMap.texture.width).toEqual(770);
        expect(octahedralMap.texture.height).toEqual(512);
        expect(octahedralMap.maximumMipmapLevel).toEqual(5);
      });
    });
 
    it("correctly projects the given cube map and all mip levels", function () {
      if (!OctahedralProjectedCubeMap.isSupported(context)) {
        return;
      }
 
      octahedralMap = new OctahedralProjectedCubeMap(environmentMapUrl);
      var frameState = createFrameState(context);
 
      return pollToPromise(function () {
        // We manually call update and execute the commands
        // because calling scene.renderForSpecs does not
        // actually execute these commands, and we need
        // to get the output of the texture.
        octahedralMap.update(frameState);
        executeCommands(frameState);
 
        return octahedralMap.ready;
      }).then(function () {
        var directions = {
          positiveX: new Cartesian3(1, 0, 0),
          negativeX: new Cartesian3(-1, 0, 0),
          positiveY: new Cartesian3(0, 1, 0),
          negativeY: new Cartesian3(0, -1, 0),
          positiveZ: new Cartesian3(0, 0, 1),
          negativeZ: new Cartesian3(0, 0, -1),
        };
 
        for (
          var mipLevel = 0;
          mipLevel < octahedralMap.maximumMipmapLevel;
          mipLevel++
        ) {
          for (var key in directions) {
            if (directions.hasOwnProperty(key)) {
              var direction = directions[key];
 
              expectCubeMapAndOctahedralMapEqual(
                octahedralMap,
                direction,
                mipLevel
              );
            }
          }
        }
      });
    });
 
    it("caches projected textures", function () {
      if (!OctahedralProjectedCubeMap.isSupported(context)) {
        return;
      }
 
      var projection = new OctahedralProjectedCubeMap(environmentMapUrl);
      var frameState = createFrameState(context);
 
      return pollToPromise(function () {
        projection.update(frameState);
        return projection.ready;
      })
        .then(function () {
          var projection2 = new OctahedralProjectedCubeMap(environmentMapUrl);
          projection2.update(frameState);
          expect(projection2.ready).toEqual(true);
          expect(projection.texture).toEqual(projection2.texture);
          projection2.destroy();
        })
        .always(function () {
          projection.destroy();
        });
    });
 
    it("rejects when environment map fails to load.", function () {
      if (!OctahedralProjectedCubeMap.isSupported(context)) {
        return;
      }
 
      var projection = new OctahedralProjectedCubeMap("http://invalid.url");
      var frameState = createFrameState(context);
      var error;
 
      projection.readyPromise
        .then(function () {
          fail("Should not resolve.");
        })
        .otherwise(function (e) {
          error = e;
          expect(error).toBeDefined();
          expect(projection.ready).toEqual(false);
        });
 
      return pollToPromise(function () {
        projection.update(frameState);
        return defined(error);
      });
    });
  },
  "WebGL"
);