forked from CesiumGS/cesium
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathGlobeSurfaceShaderSet.js
More file actions
495 lines (440 loc) · 16.1 KB
/
GlobeSurfaceShaderSet.js
File metadata and controls
495 lines (440 loc) · 16.1 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import defined from "../Core/defined.js";
import destroyObject from "../Core/destroyObject.js";
import TerrainQuantization from "../Core/TerrainQuantization.js";
import ShaderProgram from "../Renderer/ShaderProgram.js";
import getClippingFunction from "./getClippingFunction.js";
import SceneMode from "./SceneMode.js";
function GlobeSurfaceShader(
numberOfDayTextures,
flags,
material,
shaderProgram,
clippingShaderState,
clippingPolygonShaderState,
) {
this.numberOfDayTextures = numberOfDayTextures;
this.flags = flags;
this.material = material;
this.shaderProgram = shaderProgram;
this.clippingShaderState = clippingShaderState;
this.clippingPolygonShaderState = clippingPolygonShaderState;
}
/**
* Manages the shaders used to shade the surface of a {@link Globe}.
*
* @alias GlobeSurfaceShaderSet
* @private
*/
function GlobeSurfaceShaderSet() {
this.baseVertexShaderSource = undefined;
this.baseFragmentShaderSource = undefined;
this._shadersByTexturesFlags = [];
this.material = undefined;
}
function getPositionMode(sceneMode) {
const getPosition3DMode =
"vec4 getPosition(vec3 position, float height, vec2 textureCoordinates) { return getPosition3DMode(position, height, textureCoordinates); }";
const getPositionColumbusViewAnd2DMode =
"vec4 getPosition(vec3 position, float height, vec2 textureCoordinates) { return getPositionColumbusViewMode(position, height, textureCoordinates); }";
const getPositionMorphingMode =
"vec4 getPosition(vec3 position, float height, vec2 textureCoordinates) { return getPositionMorphingMode(position, height, textureCoordinates); }";
let positionMode;
switch (sceneMode) {
case SceneMode.SCENE3D:
positionMode = getPosition3DMode;
break;
case SceneMode.SCENE2D:
case SceneMode.COLUMBUS_VIEW:
positionMode = getPositionColumbusViewAnd2DMode;
break;
case SceneMode.MORPHING:
positionMode = getPositionMorphingMode;
break;
}
return positionMode;
}
function getPolygonClippingFunction(context) {
// return a noop for webgl1
if (!context.webgl2) {
return `void clipPolygons(highp sampler2D clippingDistance, int regionsLength, vec2 clippingPosition, int regionIndex) {
}`;
}
return `void clipPolygons(highp sampler2D clippingDistance, int regionsLength, vec2 clippingPosition, int regionIndex) {
czm_clipPolygons(clippingDistance, regionsLength, clippingPosition, regionIndex);
}`;
}
function getUnpackClippingFunction(context) {
// return a noop for webgl1
if (!context.webgl2) {
return `vec4 unpackClippingExtents(highp sampler2D extentsTexture, int index) {
return vec4();
}`;
}
return `vec4 unpackClippingExtents(highp sampler2D extentsTexture, int index) {
return czm_unpackClippingExtents(extentsTexture, index);
}`;
}
function get2DYPositionFraction(useWebMercatorProjection) {
const get2DYPositionFractionGeographicProjection =
"float get2DYPositionFraction(vec2 textureCoordinates) { return get2DGeographicYPositionFraction(textureCoordinates); }";
const get2DYPositionFractionMercatorProjection =
"float get2DYPositionFraction(vec2 textureCoordinates) { return get2DMercatorYPositionFraction(textureCoordinates); }";
return useWebMercatorProjection
? get2DYPositionFractionMercatorProjection
: get2DYPositionFractionGeographicProjection;
}
GlobeSurfaceShaderSet.prototype.getShaderProgram = function (options) {
const frameState = options.frameState;
const surfaceTile = options.surfaceTile;
const numberOfDayTextures = options.numberOfDayTextures;
const applyBrightness = options.applyBrightness;
const applyContrast = options.applyContrast;
const applyHue = options.applyHue;
const applySaturation = options.applySaturation;
const applyGamma = options.applyGamma;
const applyAlpha = options.applyAlpha;
const applyDayNightAlpha = options.applyDayNightAlpha;
const applySplit = options.applySplit;
const hasWaterMask = options.hasWaterMask;
const showReflectiveOcean = options.showReflectiveOcean;
const showOceanWaves = options.showOceanWaves;
const enableLighting = options.enableLighting;
const dynamicAtmosphereLighting = options.dynamicAtmosphereLighting;
const dynamicAtmosphereLightingFromSun =
options.dynamicAtmosphereLightingFromSun;
const showGroundAtmosphere = options.showGroundAtmosphere;
const perFragmentGroundAtmosphere = options.perFragmentGroundAtmosphere;
const hasVertexNormals = options.hasVertexNormals;
const useWebMercatorProjection = options.useWebMercatorProjection;
const enableFog = options.enableFog;
const enableClippingPlanes = options.enableClippingPlanes;
const clippingPlanes = options.clippingPlanes;
const enableClippingPolygons = options.enableClippingPolygons;
const clippingPolygons = options.clippingPolygons;
const clippedByBoundaries = options.clippedByBoundaries;
const hasImageryLayerCutout = options.hasImageryLayerCutout;
const colorCorrect = options.colorCorrect;
const highlightFillTile = options.highlightFillTile;
const colorToAlpha = options.colorToAlpha;
const hasGeodeticSurfaceNormals = options.hasGeodeticSurfaceNormals;
const hasExaggeration = options.hasExaggeration;
const showUndergroundColor = options.showUndergroundColor;
const translucent = options.translucent;
const splitTerrain = options.splitTerrain;
let quantization = 0;
let quantizationDefine = "";
const mesh = surfaceTile.renderedMesh;
const terrainEncoding = mesh.encoding;
const quantizationMode = terrainEncoding.quantization;
if (quantizationMode === TerrainQuantization.BITS12) {
quantization = 1;
quantizationDefine = "QUANTIZATION_BITS12";
}
let cartographicLimitRectangleFlag = 0;
let cartographicLimitRectangleDefine = "";
if (clippedByBoundaries) {
cartographicLimitRectangleFlag = 1;
cartographicLimitRectangleDefine = "TILE_LIMIT_RECTANGLE";
}
let imageryCutoutFlag = 0;
let imageryCutoutDefine = "";
if (hasImageryLayerCutout) {
imageryCutoutFlag = 1;
imageryCutoutDefine = "APPLY_IMAGERY_CUTOUT";
}
const sceneMode = frameState.mode;
// Bitwise OR uses 32-bit integers; bits 0-31 are packed below.
// Flags beyond bit 31 use arithmetic to avoid silent wrap-around
// (x << 32 === x << 0 in JavaScript).
const flags =
((sceneMode |
(applyBrightness << 2) |
(applyContrast << 3) |
(applyHue << 4) |
(applySaturation << 5) |
(applyGamma << 6) |
(applyAlpha << 7) |
(hasWaterMask << 8) |
(showReflectiveOcean << 9) |
(showOceanWaves << 10) |
(enableLighting << 11) |
(dynamicAtmosphereLighting << 12) |
(dynamicAtmosphereLightingFromSun << 13) |
(showGroundAtmosphere << 14) |
(perFragmentGroundAtmosphere << 15) |
(hasVertexNormals << 16) |
(useWebMercatorProjection << 17) |
(enableFog << 18) |
(quantization << 19) |
(applySplit << 20) |
(enableClippingPlanes << 21) |
(enableClippingPolygons << 22) |
(cartographicLimitRectangleFlag << 23) |
(imageryCutoutFlag << 24) |
(colorCorrect << 25) |
(highlightFillTile << 26) |
(colorToAlpha << 27) |
(hasGeodeticSurfaceNormals << 28) |
(hasExaggeration << 29) |
(showUndergroundColor << 30) |
(translucent << 31)) >>>
0) +
(applyDayNightAlpha ? 0x100000000 : 0) +
(splitTerrain ? 0x1000000000 : 0);
let currentClippingShaderState = 0;
if (defined(clippingPlanes) && clippingPlanes.length > 0) {
currentClippingShaderState = enableClippingPlanes
? clippingPlanes.clippingPlanesState
: 0;
}
let currentClippingPolygonsShaderState = 0;
if (defined(clippingPolygons) && clippingPolygons.length > 0) {
currentClippingPolygonsShaderState = enableClippingPolygons
? clippingPolygons.clippingPolygonsState
: 0;
}
let surfaceShader = surfaceTile.surfaceShader;
if (
defined(surfaceShader) &&
surfaceShader.numberOfDayTextures === numberOfDayTextures &&
surfaceShader.flags === flags &&
surfaceShader.material === this.material &&
surfaceShader.clippingShaderState === currentClippingShaderState &&
surfaceShader.clippingPolygonShaderState ===
currentClippingPolygonsShaderState
) {
return surfaceShader.shaderProgram;
}
// New tile, or tile changed number of textures, flags, or clipping planes
let shadersByFlags = this._shadersByTexturesFlags[numberOfDayTextures];
if (!defined(shadersByFlags)) {
shadersByFlags = this._shadersByTexturesFlags[numberOfDayTextures] = [];
}
surfaceShader = shadersByFlags[flags];
if (
!defined(surfaceShader) ||
surfaceShader.material !== this.material ||
surfaceShader.clippingShaderState !== currentClippingShaderState ||
surfaceShader.clippingPolygonShaderState !==
currentClippingPolygonsShaderState
) {
// Cache miss - we've never seen this combination of numberOfDayTextures and flags before.
const vs = this.baseVertexShaderSource.clone();
const fs = this.baseFragmentShaderSource.clone();
// Need to go before GlobeFS
if (currentClippingShaderState !== 0) {
fs.sources.unshift(
getClippingFunction(clippingPlanes, frameState.context),
);
}
// Need to go before GlobeFS
if (currentClippingPolygonsShaderState !== 0) {
fs.sources.unshift(getPolygonClippingFunction(frameState.context));
vs.sources.unshift(getUnpackClippingFunction(frameState.context));
}
vs.defines.push(quantizationDefine);
fs.defines.push(
`TEXTURE_UNITS ${numberOfDayTextures}`,
cartographicLimitRectangleDefine,
imageryCutoutDefine,
);
if (applyBrightness) {
fs.defines.push("APPLY_BRIGHTNESS");
}
if (applyContrast) {
fs.defines.push("APPLY_CONTRAST");
}
if (applyHue) {
fs.defines.push("APPLY_HUE");
}
if (applySaturation) {
fs.defines.push("APPLY_SATURATION");
}
if (applyGamma) {
fs.defines.push("APPLY_GAMMA");
}
if (applyAlpha) {
fs.defines.push("APPLY_ALPHA");
}
if (applyDayNightAlpha) {
fs.defines.push("APPLY_DAY_NIGHT_ALPHA");
}
if (hasWaterMask) {
fs.defines.push("HAS_WATER_MASK");
}
if (showReflectiveOcean) {
fs.defines.push("SHOW_REFLECTIVE_OCEAN");
vs.defines.push("SHOW_REFLECTIVE_OCEAN");
}
if (showOceanWaves) {
fs.defines.push("SHOW_OCEAN_WAVES");
}
if (colorToAlpha) {
fs.defines.push("APPLY_COLOR_TO_ALPHA");
}
if (showUndergroundColor) {
vs.defines.push("UNDERGROUND_COLOR");
fs.defines.push("UNDERGROUND_COLOR");
}
if (translucent) {
vs.defines.push("TRANSLUCENT");
fs.defines.push("TRANSLUCENT");
}
if (enableLighting) {
if (hasVertexNormals) {
vs.defines.push("ENABLE_VERTEX_LIGHTING");
fs.defines.push("ENABLE_VERTEX_LIGHTING");
} else {
vs.defines.push("ENABLE_DAYNIGHT_SHADING");
fs.defines.push("ENABLE_DAYNIGHT_SHADING");
}
}
if (dynamicAtmosphereLighting) {
vs.defines.push("DYNAMIC_ATMOSPHERE_LIGHTING");
fs.defines.push("DYNAMIC_ATMOSPHERE_LIGHTING");
if (dynamicAtmosphereLightingFromSun) {
vs.defines.push("DYNAMIC_ATMOSPHERE_LIGHTING_FROM_SUN");
fs.defines.push("DYNAMIC_ATMOSPHERE_LIGHTING_FROM_SUN");
}
}
if (showGroundAtmosphere) {
vs.defines.push("GROUND_ATMOSPHERE");
fs.defines.push("GROUND_ATMOSPHERE");
if (perFragmentGroundAtmosphere) {
vs.defines.push("PER_FRAGMENT_GROUND_ATMOSPHERE");
fs.defines.push("PER_FRAGMENT_GROUND_ATMOSPHERE");
}
}
vs.defines.push("INCLUDE_WEB_MERCATOR_Y");
fs.defines.push("INCLUDE_WEB_MERCATOR_Y");
if (enableFog) {
vs.defines.push("FOG");
fs.defines.push("FOG");
}
if (applySplit) {
fs.defines.push("APPLY_SPLIT");
}
if (enableClippingPlanes) {
fs.defines.push("ENABLE_CLIPPING_PLANES");
}
if (enableClippingPolygons) {
fs.defines.push("ENABLE_CLIPPING_POLYGONS");
vs.defines.push("ENABLE_CLIPPING_POLYGONS");
if (clippingPolygons.inverse) {
fs.defines.push("CLIPPING_INVERSE");
}
fs.defines.push(
`CLIPPING_POLYGON_REGIONS_LENGTH ${clippingPolygons.extentsCount}`,
);
vs.defines.push(
`CLIPPING_POLYGON_REGIONS_LENGTH ${clippingPolygons.extentsCount}`,
);
}
if (colorCorrect) {
fs.defines.push("COLOR_CORRECT");
}
if (highlightFillTile) {
fs.defines.push("HIGHLIGHT_FILL_TILE");
}
if (hasGeodeticSurfaceNormals) {
vs.defines.push("GEODETIC_SURFACE_NORMALS");
}
if (hasExaggeration) {
vs.defines.push("EXAGGERATION");
}
if (splitTerrain) {
fs.defines.push("SPLIT_TERRAIN");
}
let computeDayColor =
"\
vec4 computeDayColor(vec4 initialColor, vec3 textureCoordinates, float nightBlend)\n\
{\n\
vec4 color = initialColor;\n";
if (hasImageryLayerCutout) {
computeDayColor +=
"\
vec4 cutoutAndColorResult;\n\
bool texelUnclipped;\n";
}
for (let i = 0; i < numberOfDayTextures; ++i) {
if (hasImageryLayerCutout) {
computeDayColor += `\
cutoutAndColorResult = u_dayTextureCutoutRectangles[${i}];\n\
texelUnclipped = v_textureCoordinates.x < cutoutAndColorResult.x || cutoutAndColorResult.z < v_textureCoordinates.x || v_textureCoordinates.y < cutoutAndColorResult.y || cutoutAndColorResult.w < v_textureCoordinates.y;\n\
cutoutAndColorResult = sampleAndBlend(\n`;
} else {
computeDayColor +=
"\
color = sampleAndBlend(\n";
}
computeDayColor += `\
color,\n\
u_dayTextures[${i}],\n\
u_dayTextureUseWebMercatorT[${i}] ? textureCoordinates.xz : textureCoordinates.xy,\n\
u_dayTextureTexCoordsRectangle[${i}],\n\
u_dayTextureTranslationAndScale[${i}],\n\
${applyAlpha ? `u_dayTextureAlpha[${i}]` : "1.0"},\n\
${applyDayNightAlpha ? `u_dayTextureNightAlpha[${i}]` : "1.0"},\n\
${applyDayNightAlpha ? `u_dayTextureDayAlpha[${i}]` : "1.0"},\n\
${applyBrightness ? `u_dayTextureBrightness[${i}]` : "0.0"},\n\
${applyContrast ? `u_dayTextureContrast[${i}]` : "0.0"},\n\
${applyHue ? `u_dayTextureHue[${i}]` : "0.0"},\n\
${applySaturation ? `u_dayTextureSaturation[${i}]` : "0.0"},\n\
${applyGamma ? `u_dayTextureOneOverGamma[${i}]` : "0.0"},\n\
${applySplit ? `u_dayTextureSplit[${i}]` : "0.0"},\n\
${colorToAlpha ? `u_colorsToAlpha[${i}]` : "vec4(0.0)"},\n\
nightBlend\);\n`;
if (hasImageryLayerCutout) {
computeDayColor +=
"\
color = czm_branchFreeTernary(texelUnclipped, cutoutAndColorResult, color);\n";
}
}
computeDayColor +=
"\
return color;\n\
}";
fs.sources.push(computeDayColor);
vs.sources.push(getPositionMode(sceneMode));
vs.sources.push(get2DYPositionFraction(useWebMercatorProjection));
const shader = ShaderProgram.fromCache({
context: frameState.context,
vertexShaderSource: vs,
fragmentShaderSource: fs,
attributeLocations: terrainEncoding.getAttributeLocations(),
});
surfaceShader = shadersByFlags[flags] = new GlobeSurfaceShader(
numberOfDayTextures,
flags,
this.material,
shader,
currentClippingShaderState,
currentClippingPolygonsShaderState,
);
}
surfaceTile.surfaceShader = surfaceShader;
return surfaceShader.shaderProgram;
};
GlobeSurfaceShaderSet.prototype.destroy = function () {
let flags;
let shader;
const shadersByTexturesFlags = this._shadersByTexturesFlags;
for (const textureCount in shadersByTexturesFlags) {
if (shadersByTexturesFlags.hasOwnProperty(textureCount)) {
const shadersByFlags = shadersByTexturesFlags[textureCount];
if (!defined(shadersByFlags)) {
continue;
}
for (flags in shadersByFlags) {
if (shadersByFlags.hasOwnProperty(flags)) {
shader = shadersByFlags[flags];
if (defined(shader)) {
shader.shaderProgram.destroy();
}
}
}
}
}
return destroyObject(this);
};
export default GlobeSurfaceShaderSet;