@mapcatch/util 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/.eslintrc.js +53 -0
  2. package/.prettierrc +4 -0
  3. package/CHANGELOG.md +0 -0
  4. package/README.md +44 -0
  5. package/docs/Catolog.md +24 -0
  6. package/docs/Util.md +170 -0
  7. package/package.json +30 -0
  8. package/src/constants/cameras.js +5 -0
  9. package/src/constants/index.js +1 -0
  10. package/src/event.js +205 -0
  11. package/src/exif/exif.js +37 -0
  12. package/src/exif/gps_tags.js +33 -0
  13. package/src/exif/ifd1_tags.js +22 -0
  14. package/src/exif/index.js +16 -0
  15. package/src/exif/iptc_field_map.js +12 -0
  16. package/src/exif/parse_image.js +446 -0
  17. package/src/exif/string_values.js +137 -0
  18. package/src/exif/tags.js +75 -0
  19. package/src/exif/tiff_tags.js +35 -0
  20. package/src/exif/util.js +108 -0
  21. package/src/gl-operations/constants.js +11 -0
  22. package/src/gl-operations/default_options.js +98 -0
  23. package/src/gl-operations/index.js +594 -0
  24. package/src/gl-operations/reglCommands/contours.js +27 -0
  25. package/src/gl-operations/reglCommands/default.js +44 -0
  26. package/src/gl-operations/reglCommands/hillshading.js +332 -0
  27. package/src/gl-operations/reglCommands/index.js +6 -0
  28. package/src/gl-operations/reglCommands/multiLayers.js +301 -0
  29. package/src/gl-operations/reglCommands/transitions.js +109 -0
  30. package/src/gl-operations/reglCommands/util.js +71 -0
  31. package/src/gl-operations/renderer.js +193 -0
  32. package/src/gl-operations/shaders/fragment/convertDem.js +26 -0
  33. package/src/gl-operations/shaders/fragment/convolutionSmooth.js +55 -0
  34. package/src/gl-operations/shaders/fragment/diffCalc.js +34 -0
  35. package/src/gl-operations/shaders/fragment/drawResult.js +42 -0
  36. package/src/gl-operations/shaders/fragment/hillshading/hsAdvAmbientShadows.js +79 -0
  37. package/src/gl-operations/shaders/fragment/hillshading/hsAdvDirect.js +55 -0
  38. package/src/gl-operations/shaders/fragment/hillshading/hsAdvFinalBaselayer.js +31 -0
  39. package/src/gl-operations/shaders/fragment/hillshading/hsAdvFinalColorscale.js +56 -0
  40. package/src/gl-operations/shaders/fragment/hillshading/hsAdvMergeAndScaleTiles.js +27 -0
  41. package/src/gl-operations/shaders/fragment/hillshading/hsAdvNormals.js +26 -0
  42. package/src/gl-operations/shaders/fragment/hillshading/hsAdvSmooth.js +54 -0
  43. package/src/gl-operations/shaders/fragment/hillshading/hsAdvSoftShadows.js +81 -0
  44. package/src/gl-operations/shaders/fragment/hillshading/hsPregen.js +50 -0
  45. package/src/gl-operations/shaders/fragment/interpolateColor.js +63 -0
  46. package/src/gl-operations/shaders/fragment/interpolateColorOnly.js +47 -0
  47. package/src/gl-operations/shaders/fragment/interpolateValue.js +124 -0
  48. package/src/gl-operations/shaders/fragment/multiAnalyze1Calc.js +36 -0
  49. package/src/gl-operations/shaders/fragment/multiAnalyze2Calc.js +46 -0
  50. package/src/gl-operations/shaders/fragment/multiAnalyze3Calc.js +54 -0
  51. package/src/gl-operations/shaders/fragment/multiAnalyze4Calc.js +62 -0
  52. package/src/gl-operations/shaders/fragment/multiAnalyze5Calc.js +70 -0
  53. package/src/gl-operations/shaders/fragment/multiAnalyze6Calc.js +78 -0
  54. package/src/gl-operations/shaders/fragment/single.js +88 -0
  55. package/src/gl-operations/shaders/transform.js +22 -0
  56. package/src/gl-operations/shaders/util/computeColor.glsl +84 -0
  57. package/src/gl-operations/shaders/util/getTexelValue.glsl +10 -0
  58. package/src/gl-operations/shaders/util/isCloseEnough.glsl +9 -0
  59. package/src/gl-operations/shaders/util/rgbaToFloat.glsl +18 -0
  60. package/src/gl-operations/shaders/vertex/double.js +17 -0
  61. package/src/gl-operations/shaders/vertex/multi3.js +20 -0
  62. package/src/gl-operations/shaders/vertex/multi4.js +23 -0
  63. package/src/gl-operations/shaders/vertex/multi5.js +26 -0
  64. package/src/gl-operations/shaders/vertex/multi6.js +29 -0
  65. package/src/gl-operations/shaders/vertex/single.js +13 -0
  66. package/src/gl-operations/shaders/vertex/singleNotTransformed.js +12 -0
  67. package/src/gl-operations/texture_manager.js +141 -0
  68. package/src/gl-operations/util.js +336 -0
  69. package/src/index.js +10 -0
  70. package/src/util.js +332 -0
  71. package/vite.config.js +52 -0
@@ -0,0 +1,109 @@
1
+ import vertSingle from '../shaders/vertex/single.js';
2
+ import vertDouble from '../shaders/vertex/double.js';
3
+
4
+ import fragInterpolateColor from '../shaders/fragment/interpolateColor.js';
5
+ import fragInterpolateColorOnly from '../shaders/fragment/interpolateColorOnly.js';
6
+ import fragInterpolateValue from '../shaders/fragment/interpolateValue.js';
7
+
8
+ import * as util from '../util';
9
+
10
+ /**
11
+ * The DrawCommand output by this function interpolates, for each pixel, between two values, one
12
+ * from `textureA` and one from `textureB`. The same color scale / sentinel values are applied to
13
+ * both.
14
+ */
15
+ export function createDrawTileInterpolateValueCommand(
16
+ regl,
17
+ commonConfig,
18
+ fragMacros,
19
+ ) {
20
+ return regl({
21
+ ...commonConfig,
22
+ vert: vertDouble,
23
+ frag: util.defineMacros(fragInterpolateValue, fragMacros),
24
+ uniforms: {
25
+ ...commonConfig.uniforms,
26
+ scaleLength: regl.prop('scaleLength'),
27
+ sentinelLength: regl.prop('sentinelLength'),
28
+ scaleColormap: regl.prop('scaleColormap'),
29
+ sentinelColormap: regl.prop('sentinelColormap'),
30
+ textureA: (_, { textureA }) => textureA,
31
+ textureB: (_, { textureB }) => textureB,
32
+ interpolationFraction: (_, { interpolationFraction }) => interpolationFraction,
33
+ },
34
+ attributes: {
35
+ ...commonConfig.attributes,
36
+ texCoordA: (_, { textureBoundsA }) => util.getTexCoordVerticesTriangleStripQuad(textureBoundsA),
37
+ texCoordB: (_, { textureBoundsB }) => util.getTexCoordVerticesTriangleStripQuad(textureBoundsB),
38
+ },
39
+ });
40
+ }
41
+
42
+ /**
43
+ * The behavior of this DrawCommand is similar to the one above, except that pixels from `textureA`
44
+ * are colorized with one color scale / set of sentinel values, while pixels from `textureB` use a
45
+ * different color scale / set of sentinel values.
46
+ */
47
+ export function createDrawTileInterpolateColorCommand(
48
+ regl,
49
+ commonConfig,
50
+ fragMacros,
51
+ ) {
52
+ return regl({
53
+ ...commonConfig,
54
+ vert: vertDouble,
55
+ frag: util.defineMacros(fragInterpolateColor, fragMacros),
56
+ uniforms: {
57
+ ...commonConfig.uniforms,
58
+ scaleLengthA: regl.prop('scaleLengthA'),
59
+ sentinelLengthA: regl.prop('sentinelLengthA'),
60
+ scaleColormapA: regl.prop('scaleColormapA'),
61
+ sentinelColormapA: regl.prop('sentinelColormapA'),
62
+ scaleLengthB: regl.prop('scaleLengthB'),
63
+ sentinelLengthB: regl.prop('sentinelLengthB'),
64
+ scaleColormapB: regl.prop('scaleColormapB'),
65
+ sentinelColormapB: regl.prop('sentinelColormapB'),
66
+ textureA: (_, { textureA }) => textureA,
67
+ textureB: (_, { textureB }) => textureB,
68
+ interpolationFraction: (_, { interpolationFraction }) => interpolationFraction,
69
+ },
70
+ attributes: {
71
+ ...commonConfig.attributes,
72
+ texCoordA: (_, { textureBoundsA }) => util.getTexCoordVerticesTriangleStripQuad(textureBoundsA),
73
+ texCoordB: (_, { textureBoundsB }) => util.getTexCoordVerticesTriangleStripQuad(textureBoundsB),
74
+ },
75
+ });
76
+ }
77
+
78
+ /**
79
+ * The behavior of this DrawCommand is similar to the one above, except that the pixel values
80
+ * are the same. Only the colorscale changes.
81
+ */
82
+ export function createDrawTileInterpolateColorOnlyCommand(
83
+ regl,
84
+ commonConfig,
85
+ fragMacros,
86
+ ) {
87
+ return regl({
88
+ ...commonConfig,
89
+ vert: vertSingle,
90
+ frag: util.defineMacros(fragInterpolateColorOnly, fragMacros),
91
+ uniforms: {
92
+ ...commonConfig.uniforms,
93
+ scaleLengthA: regl.prop('scaleLengthA'),
94
+ sentinelLengthA: regl.prop('sentinelLengthA'),
95
+ scaleColormapA: regl.prop('scaleColormapA'),
96
+ sentinelColormapA: regl.prop('sentinelColormapA'),
97
+ scaleLengthB: regl.prop('scaleLengthB'),
98
+ sentinelLengthB: regl.prop('sentinelLengthB'),
99
+ scaleColormapB: regl.prop('scaleColormapB'),
100
+ sentinelColormapB: regl.prop('sentinelColormapB'),
101
+ texture: (_, { texture }) => texture,
102
+ interpolationFraction: (_, { interpolationFraction }) => interpolationFraction,
103
+ },
104
+ attributes: {
105
+ ...commonConfig.attributes,
106
+ texCoord: (_, { textureBounds }) => util.getTexCoordVerticesTriangleStripQuad(textureBounds),
107
+ },
108
+ });
109
+ }
@@ -0,0 +1,71 @@
1
+ import vertSingle from '../shaders/vertex/single.js';
2
+
3
+ import fragConvertDem from '../shaders/fragment/convertDem.js';
4
+
5
+ import * as util from '../util';
6
+
7
+ const littleEndian = util.machineIsLittleEndian();
8
+
9
+ /**
10
+ * The object generated by this function should be merged into the DrawConfig for each Regl
11
+ * command in the application.
12
+ */
13
+ export function getCommonDrawConfiguration(
14
+ tileSize,
15
+ nodataValue,
16
+ ) {
17
+ return {
18
+ uniforms: {
19
+ nodataValue,
20
+ littleEndian,
21
+ transformMatrix: ({ viewportWidth, viewportHeight }) => (
22
+ util.getTransformMatrix(viewportWidth, viewportHeight)
23
+ ),
24
+ },
25
+ attributes: {
26
+ position: (_, { canvasCoordinates }) => {
27
+ const [left, top] = canvasCoordinates;
28
+ const [right, bottom] = [left + tileSize, top + tileSize];
29
+ return [
30
+ [left, top ],
31
+ [right, top ],
32
+ [left, bottom],
33
+ [right, bottom],
34
+ ];
35
+ },
36
+ },
37
+ // We don't need the depth buffer for 2D drawing. Leaving it enabled (and failing to clear it
38
+ // between draw calls) results in visual artifacts.
39
+ depth: { enable: false },
40
+ primitive: 'triangle strip',
41
+ count: 4,
42
+ viewport: (_, { canvasSize: [width, height] }) => ({ width, height }),
43
+ };
44
+ }
45
+
46
+ /**
47
+ * The command output by this function converts a tile in DEM format
48
+ * to float32 packed as rgba.
49
+ */
50
+ export function createConvertDemCommand(
51
+ regl,
52
+ commonConfig
53
+ ) {
54
+ return regl({
55
+ ...commonConfig,
56
+ vert: vertSingle,
57
+ frag: fragConvertDem,
58
+ depth: {
59
+ enable: false
60
+ },
61
+ uniforms: {
62
+ ...commonConfig.uniforms,
63
+ texture: (_, { texture }) => texture,
64
+ },
65
+ attributes: {
66
+ ...commonConfig.attributes,
67
+ texCoord: [[0, 1], [1, 1], [0, 0], [1, 0]],
68
+ },
69
+ framebuffer: regl.prop("fbo"),
70
+ });
71
+ }
@@ -0,0 +1,193 @@
1
+ import REGL, { Framebuffer2D } from 'regl';
2
+ import {
3
+ CLEAR_COLOR,
4
+ MAX_TEXTURE_DIMENSION,
5
+ EARTH_SUN_DISTANCE,
6
+ SUN_RADIUS,
7
+ } from './constants';
8
+
9
+ import TextureManager from './texture_manager';
10
+ import * as util from './util';
11
+ import { vec3 } from "gl-matrix";
12
+ import * as reglCommands from './reglCommands';
13
+
14
+
15
+ export default class Renderer {
16
+ constructor(gloperations, tileSize, nodataValue, scaleInput, sentinelInput, colorscaleMaxLength, sentinelMaxLength) {
17
+ const canvas = document.createElement('canvas');
18
+ let maxTextureDimension = MAX_TEXTURE_DIMENSION;
19
+ let normalTextureDimension = 1024;
20
+
21
+ const regl = REGL({
22
+ canvas: canvas,
23
+ // profile: true,
24
+ // extension only used for advanced hillshading
25
+ // TODO: add fallback to rgba if writing to float fails
26
+ optionalExtensions: ["OES_texture_float", "WEBGL_color_buffer_float"],
27
+ onDone: function (err, regl) {
28
+ if (err) {
29
+ console.log(err);
30
+ return;
31
+ } else {
32
+ // cannot use this directly as it seems to cause some issues
33
+ // maxTextureDimension = regl.limits.maxTextureSize;
34
+ if (regl.limits.maxTextureSize > 2048) {
35
+ maxTextureDimension = 2048;
36
+ }
37
+ if (regl.limits.maxTextureSize > 4096) {
38
+ maxTextureDimension = 4096;
39
+ }
40
+ if (regl.limits.maxTextureSize > 8192) {
41
+ maxTextureDimension = 8192;
42
+ }
43
+
44
+ // TODO: use lower normal dimension where relevant
45
+ // Currently set to same due to a bug
46
+ normalTextureDimension = maxTextureDimension;
47
+ }
48
+ // TODO: Improve software rendering detection
49
+ if (regl.limits.maxFragmentUniforms === 261) {
50
+ console.warn("Software rendering detected. Many features of this plugin will fail.\
51
+ If you have a GPU, check if drivers are installed ok?");
52
+ }
53
+ }
54
+ });
55
+
56
+ const commonDrawConfig = reglCommands.getCommonDrawConfiguration(tileSize, nodataValue);
57
+ const fragMacros = {
58
+ SCALE_MAX_LENGTH: colorscaleMaxLength,
59
+ SENTINEL_MAX_LENGTH: sentinelMaxLength,
60
+ };
61
+
62
+ // Assign object "instance" properties.
63
+ Object.assign(this, {
64
+ gloperations: gloperations,
65
+ canvas: canvas,
66
+ regl: regl,
67
+ tileSize: tileSize,
68
+ nodataValue: nodataValue,
69
+ maxTextureDimension: maxTextureDimension,
70
+ normalTextureDimension: normalTextureDimension,
71
+ scaleInput: scaleInput,
72
+ sentinelInput: sentinelInput,
73
+ scaleColormap: util.createColormapTexture(scaleInput, regl),
74
+ sentinelColormap: util.createColormapTexture(sentinelInput, regl),
75
+ textureManager: new TextureManager(regl, tileSize, normalTextureDimension, false),
76
+ drawTile: reglCommands.createDrawTileCommand(regl, commonDrawConfig, fragMacros)
77
+ });
78
+ }
79
+
80
+ renderTile({ coords, pixelData }, _hillshadeOptions, zoom) {
81
+ const {
82
+ regl,
83
+ textureManager,
84
+ tileSize,
85
+ } = this;
86
+ this.setCanvasSize(tileSize, tileSize);
87
+ // Add image to the texture and retrieve its texture coordinates.
88
+ const textureBounds = textureManager.addTile(coords, pixelData);
89
+
90
+ regl.clear({ color: CLEAR_COLOR });
91
+
92
+ const zoomdelta = _hillshadeOptions.hsSimpleZoomdelta || 0;
93
+
94
+ const offset_pixels = Math.max(0.5, 2 ** (zoom + zoomdelta) / 2048);
95
+ const offset_texcoords = offset_pixels / textureManager.texture.width;
96
+
97
+ if (_hillshadeOptions.hillshadeType === "none") {
98
+ this.drawTile({
99
+ canvasSize: [tileSize, tileSize],
100
+ canvasCoordinates: [0, 0],
101
+ textureBounds,
102
+ texture: textureManager.texture,
103
+ scaleLength: this.scaleInput.length,
104
+ sentinelLength: this.sentinelInput.length,
105
+ scaleColormap: this.scaleColormap,
106
+ sentinelColormap: this.sentinelColormap,
107
+ enableSimpleHillshade: false,
108
+ });
109
+ } else if (_hillshadeOptions.hillshadeType === "simple") {
110
+ this.drawTileHsSimple({
111
+ scaleLength: this.scaleInput.length,
112
+ sentinelLength: this.sentinelInput.length,
113
+ scaleColormap: this.scaleColormap,
114
+ sentinelColormap: this.sentinelColormap,
115
+ canvasSize: [tileSize, tileSize],
116
+ canvasCoordinates: [0, 0],
117
+ textureBounds: textureBounds,
118
+ texture: textureManager.texture,
119
+ textureSize: textureManager.texture.width,
120
+ tileSize: tileSize,
121
+ offset: offset_texcoords,
122
+ enableSimpleHillshade: true,
123
+ // elevationScale: _hillshadeOptions.hsElevationScale,
124
+ azimuth: _hillshadeOptions.hsSimpleAzimuth,
125
+ altitude: _hillshadeOptions.hsSimpleAltitude,
126
+ slopescale: _hillshadeOptions.hsSimpleSlopescale,
127
+ });
128
+ }
129
+
130
+ // Since the tile will fill the whole canvas, the offset is simply [0, 0].
131
+ return [0, 0];
132
+ }
133
+
134
+ setCanvasSize(width, height) {
135
+ Object.assign(this.canvas, { width, height });
136
+ }
137
+
138
+ generateAmbientDirections(iterations) {
139
+ const ambientDirections = [];
140
+ for (let i = 0; i < iterations; i++) {
141
+ const direction = vec3.random(vec3.create(), Math.random());
142
+ ambientDirections.push(direction);
143
+ }
144
+ this.ambientDirections = ambientDirections;
145
+ }
146
+
147
+ generateSunDirections(iterations, sunRadiusMultiplier) {
148
+ const sunDirections = [];
149
+ for (let i = 0; i < iterations; i++) {
150
+ const direction = vec3.normalize(
151
+ vec3.create(),
152
+ vec3.add(
153
+ vec3.create(),
154
+ vec3.scale(
155
+ vec3.create(),
156
+ vec3.normalize(vec3.create(), [1, 1, 1]),
157
+ EARTH_SUN_DISTANCE
158
+ ),
159
+ vec3.random(vec3.create(), SUN_RADIUS * sunRadiusMultiplier)
160
+ )
161
+ );
162
+ sunDirections.push(direction);
163
+ }
164
+ this.sunDirections = sunDirections;
165
+ }
166
+
167
+ updateColorscale(scaleInput) {
168
+ this.scaleInputPrevious = this.scaleInput;
169
+ this.scaleInput = scaleInput;
170
+ this.scaleColormapPrevious = this.scaleColormap;
171
+ this.scaleColormap = util.createColormapTexture(scaleInput, this.regl);
172
+ }
173
+ updateSentinels(sentinelInput) {
174
+ this.sentinelInputPrevious = this.sentinelInput;
175
+ this.sentinelInput = sentinelInput;
176
+ this.sentinelColormapPrevious = this.sentinelColormap;
177
+ this.sentinelColormap = util.createColormapTexture(sentinelInput, this.regl);
178
+ }
179
+ setMaxTextureDimension(newMaxTextureDimension) {
180
+ const {
181
+ textureManager,
182
+ tileSize,
183
+ regl,
184
+ } = this;
185
+
186
+ textureManager.destroy();
187
+
188
+ Object.assign(this, {
189
+ maxTextureDimension: newMaxTextureDimension,
190
+ textureManager: new TextureManager(regl, tileSize, newMaxTextureDimension, false),
191
+ });
192
+ }
193
+ }
@@ -0,0 +1,26 @@
1
+ import glsl from 'glslify'
2
+
3
+ export default glsl`#ifdef GL_FRAGMENT_PRECISION_HIGH
4
+ precision highp float;
5
+ #else
6
+ precision mediump float;
7
+ #endif
8
+
9
+ #pragma glslify: floatToRgba = require(glsl-float-to-rgba)
10
+
11
+ uniform float nodataValue;
12
+ uniform bool littleEndian;
13
+ uniform sampler2D texture;
14
+ varying vec2 vTexCoord;
15
+
16
+ void main() {
17
+ //sample the texture
18
+ vec3 rgb = texture2D(texture, vTexCoord).rgb;
19
+
20
+ // Convert the red, green, and blue channels into a float
21
+ float f = -10000.0 + ((rgb.r * 255.0 * 256.0 * 256.0 + rgb.g * 255.0 * 256.0 + rgb.b * 255.0) * 0.1);
22
+
23
+ // convert to rgba and write to framebuffer
24
+ gl_FragColor = floatToRgba(f, littleEndian);
25
+ }
26
+ `
@@ -0,0 +1,55 @@
1
+ import glsl from 'glslify'
2
+
3
+ export default glsl`#ifdef GL_FRAGMENT_PRECISION_HIGH
4
+ precision highp float;
5
+ #else
6
+ precision mediump float;
7
+ #endif
8
+
9
+ #pragma glslify: rgbaToFloat = require(glsl-rgba-to-float)
10
+ #pragma glslify: floatToRgba = require(glsl-float-to-rgba)
11
+ #pragma glslify: isCloseEnough = require(../util/isCloseEnough.glsl)
12
+ #pragma glslify: getTexelValue = require(../util/getTexelValue.glsl)
13
+
14
+ uniform sampler2D texture;
15
+ uniform float textureSize;
16
+ uniform bool littleEndian;
17
+ uniform float nodataValue;
18
+ varying vec2 vTexCoord;
19
+ uniform int kernelSize;
20
+
21
+ int kernelEnd = int(kernelSize/2);
22
+ int kernelStart = kernelEnd * -1;
23
+
24
+ float runConvKernel(vec2 pos, vec2 onePixel) {
25
+ float convKernelWeight = 0.0;
26
+ float sum = 0.0;
27
+
28
+ for (int i = -20; i < 20; i ++) {
29
+ if (i < kernelStart) continue;
30
+ if (i > kernelEnd) break;
31
+ for (int j = -20; j < 20; j ++) {
32
+ if (j < kernelStart) continue;
33
+ if (j > kernelEnd) break;
34
+ float texelValue = getTexelValue(texture, pos + onePixel * vec2(i, j), littleEndian);
35
+ if (!isCloseEnough(texelValue, nodataValue)) {
36
+ sum = sum + texelValue;
37
+ convKernelWeight = convKernelWeight + 1.0;
38
+ }
39
+ }
40
+ }
41
+ return (sum / convKernelWeight);
42
+ }
43
+
44
+
45
+ void main() {
46
+ float texelFloat = getTexelValue(texture, vTexCoord, littleEndian);
47
+ if (isCloseEnough(texelFloat, nodataValue)) {
48
+ gl_FragColor = floatToRgba(nodataValue, littleEndian);
49
+ } else {
50
+ vec2 onePixel = vec2(1.0, 1.0) / textureSize;
51
+ float texelFloatSmoothed = runConvKernel(vTexCoord, onePixel);
52
+ gl_FragColor = floatToRgba(texelFloatSmoothed, littleEndian);
53
+ }
54
+ }
55
+ `
@@ -0,0 +1,34 @@
1
+ import glsl from 'glslify'
2
+
3
+ export default glsl`#ifdef GL_FRAGMENT_PRECISION_HIGH
4
+ precision highp float;
5
+ #else
6
+ precision mediump float;
7
+ #endif
8
+
9
+ #pragma glslify: floatToRgba = require(glsl-float-to-rgba)
10
+ #pragma glslify: isCloseEnough = require(../util/isCloseEnough.glsl)
11
+ #pragma glslify: getTexelValue = require(../util/getTexelValue.glsl)
12
+
13
+ uniform float nodataValue;
14
+ uniform bool littleEndian;
15
+ uniform sampler2D textureA;
16
+ uniform sampler2D textureB;
17
+
18
+ varying vec2 vTexCoordA;
19
+ varying vec2 vTexCoordB;
20
+
21
+ void main() {
22
+ float texelFloatA = getTexelValue(textureA, vTexCoordA, littleEndian);
23
+ float texelFloatB = getTexelValue(textureB, vTexCoordB, littleEndian);
24
+ bool aIsNodata = isCloseEnough(texelFloatA, nodataValue);
25
+ bool bIsNodata = isCloseEnough(texelFloatB, nodataValue);
26
+
27
+ if (aIsNodata || bIsNodata) {
28
+ gl_FragColor = floatToRgba(nodataValue, littleEndian);
29
+ } else {
30
+ float diff = texelFloatB - texelFloatA;
31
+ gl_FragColor = floatToRgba(diff, littleEndian);
32
+ }
33
+ }
34
+ `
@@ -0,0 +1,42 @@
1
+ import glsl from 'glslify'
2
+
3
+ export default glsl`#ifdef GL_FRAGMENT_PRECISION_HIGH
4
+ precision highp float;
5
+ #else
6
+ precision mediump float;
7
+ #endif
8
+
9
+ #define TRANSPARENT vec4(0.0)
10
+
11
+ #pragma glslify: computeColor = require(../util/computeColor.glsl)
12
+ #pragma glslify: isCloseEnough = require(../util/isCloseEnough.glsl)
13
+ #pragma glslify: getTexelValue = require(../util/getTexelValue.glsl)
14
+
15
+ uniform int scaleLength;
16
+ uniform int sentinelLength;
17
+ uniform sampler2D scaleColormap;
18
+ uniform sampler2D sentinelColormap;
19
+
20
+ uniform float nodataValue;
21
+ uniform sampler2D texture;
22
+ uniform bool littleEndian;
23
+
24
+ varying vec2 vTexCoord;
25
+
26
+ void main() {
27
+ float f = getTexelValue(texture, vTexCoord, littleEndian);
28
+
29
+ if (isCloseEnough(f, nodataValue)) {
30
+ gl_FragColor = TRANSPARENT;
31
+ } else {
32
+ gl_FragColor = computeColor(
33
+ f,
34
+ scaleColormap,
35
+ sentinelColormap,
36
+ scaleLength,
37
+ sentinelLength,
38
+ littleEndian
39
+ );
40
+ }
41
+ }
42
+ `
@@ -0,0 +1,79 @@
1
+ import glsl from 'glslify'
2
+
3
+ export default glsl`#ifdef GL_FRAGMENT_PRECISION_HIGH
4
+ precision highp float;
5
+ #else
6
+ precision mediump float;
7
+ #endif
8
+
9
+ uniform sampler2D tInput;
10
+ uniform sampler2D tNormal;
11
+ uniform sampler2D tSrc;
12
+ uniform vec3 direction;
13
+ uniform vec2 resolution;
14
+ uniform float pixelScale;
15
+ uniform float ambientIterations;
16
+ varying vec2 vTexCoord;
17
+
18
+ void main() {
19
+ vec2 ires = 1.0 / resolution;
20
+ vec3 src = texture2D(tSrc, gl_FragCoord.xy * ires).rgb;
21
+ vec4 e0 = texture2D(tInput, vTexCoord);
22
+ vec3 n0 = texture2D(tNormal, vTexCoord).rgb;
23
+
24
+ vec3 sr3d = normalize(n0 + direction); // ray direction
25
+ vec2 sr = normalize(sr3d.xy); // get 2D ray direction
26
+
27
+ // initialize pixel traversal algorithm
28
+ vec2 p0 = vTexCoord * resolution;
29
+ vec2 p = floor(p0); // pixel we are starting in
30
+ vec2 stp = sign(sr);
31
+
32
+ // how far we need to travel in our ray direction to intersect the next pixel
33
+ // in the x- and y- directions
34
+ vec2 tMax = step(0.0, sr) * (1.0 - fract(p0)) + (1.0 - step(0.0, sr)) * fract(p0);
35
+ tMax /= abs(sr);
36
+ // how far must we travel along our ray to cover the width and height of a pixel
37
+ vec2 tDelta = 1.0 / abs(sr);
38
+
39
+ // pixel traversal routine
40
+ for (int i = 0; i < 65536; i++) {
41
+ if (tMax.x < tMax.y) {
42
+ tMax.x += tDelta.x;
43
+ p.x += stp.x;
44
+ } else {
45
+ tMax.y += tDelta.y;
46
+ p.y += stp.y;
47
+ }
48
+
49
+ // normalized texture coordinate for the center of the current pixel
50
+ vec2 ptex = ires * (p + 0.5);
51
+
52
+ // If we left the tile: add some illumination to the pixel for this iteration and stop traversing
53
+ if (ptex.x < 0.0 || ptex.x > 1.0 || ptex.y < 0.0 || ptex.y > 1.0) {
54
+ // illumination of this single ray
55
+ vec3 illumination = vec3(1.0/ambientIterations);
56
+ // add illumination of this ray to result of previous iteration
57
+ gl_FragColor = vec4(src + illumination, 1.0);
58
+ return;
59
+ }
60
+
61
+ // elevation/value at current pixel
62
+ vec4 e = texture2D(tInput, ptex);
63
+ // time we have traveled along the 2D ray
64
+ float time = distance(p + 0.5, p0);
65
+ // elevation along our original 3D ray at the current point
66
+ float z = e0.r + time * pixelScale * sr3d.z;
67
+ // If we did not exit tile, have we hit the terrain?
68
+ if (e.r > z) {
69
+ // We hit terrain. Do not add illumination for this iteration
70
+ gl_FragColor = vec4(src, 1.0);
71
+ return;
72
+ }
73
+ }
74
+
75
+ // Should have hit terrain or left the tile by this point, so should never need this
76
+ // If we finish the loop somehow, let’s pretend it’s been illuminated
77
+ vec3 illumination = vec3(1.0/ambientIterations);
78
+ gl_FragColor = vec4(src + illumination, 1.0);
79
+ }`
@@ -0,0 +1,55 @@
1
+ import glsl from 'glslify'
2
+
3
+ export default glsl`#ifdef GL_FRAGMENT_PRECISION_HIGH
4
+ precision highp float;
5
+ #else
6
+ precision mediump float;
7
+ #endif
8
+
9
+ #pragma glslify: computeColor = require(../../util/computeColor.glsl)
10
+ #pragma glslify: isCloseEnough = require(../../util/isCloseEnough.glsl)
11
+
12
+ uniform sampler2D tInput;
13
+ uniform sampler2D tNormal;
14
+ uniform float floatScale;
15
+ uniform vec3 sunDirection;
16
+ uniform float nodataValue;
17
+ uniform bool littleEndian;
18
+ varying vec2 vTexCoord;
19
+
20
+ uniform int scaleLength;
21
+ uniform int sentinelLength;
22
+ uniform sampler2D scaleColormap;
23
+ uniform sampler2D sentinelColormap;
24
+
25
+ void main() {
26
+ float f = texture2D(tInput, vTexCoord).r;
27
+
28
+ if (isCloseEnough(f, nodataValue)) {
29
+ // discard;
30
+ gl_FragColor = vec4(0.0);
31
+ } else {
32
+ vec4 clr = computeColor(
33
+ f / floatScale,
34
+ scaleColormap,
35
+ sentinelColormap,
36
+ scaleLength,
37
+ sentinelLength,
38
+ littleEndian
39
+ );
40
+
41
+ vec3 n = texture2D(tNormal, vTexCoord).rgb;
42
+
43
+ // light intensity that varies on the range [−1..1]
44
+ float light = dot(n, sunDirection);
45
+ // transform the light intensity to the range [0..1]
46
+ light = 0.5 * light + 0.5;
47
+
48
+ // gl_FragColor = vec4(l, l, l, 1.0); // to show light
49
+
50
+ vec3 color = light * pow(clr.rgb, vec3(2.0));
51
+ color = pow(color, vec3(1.0/2.2));
52
+ gl_FragColor = vec4(color, 1.0);
53
+ }
54
+ }
55
+ `
@@ -0,0 +1,31 @@
1
+ import glsl from 'glslify'
2
+
3
+ export default glsl`#ifdef GL_FRAGMENT_PRECISION_HIGH
4
+ precision highp float;
5
+ #else
6
+ precision mediump float;
7
+ #endif
8
+
9
+ uniform sampler2D tBase;
10
+ uniform sampler2D tSoftShadow;
11
+ uniform sampler2D tAmbient;
12
+ uniform float finalSoftMultiplier;
13
+ uniform float finalAmbientMultiplier;
14
+ uniform float nodataValue;
15
+ uniform bool littleEndian;
16
+ varying vec2 vTexCoordA;
17
+ varying vec2 vTexCoordB;
18
+
19
+ void main() {
20
+ vec3 baselayer = texture2D(tBase, vTexCoordA).rgb;
21
+
22
+ float softShadow = texture2D(tSoftShadow, vTexCoordB).r;
23
+ float ambient = texture2D(tAmbient, vTexCoordB).r;
24
+ // Add up the lighting
25
+ float light = finalSoftMultiplier * softShadow + finalAmbientMultiplier * ambient;
26
+ //Deepen the original color a bit by applying a curve, and multiply it by the light
27
+ vec3 color = light * pow(baselayer.rgb, vec3(2.0));
28
+ // apply gamma correction
29
+ color = pow(color, vec3(1.0/2.2));
30
+ gl_FragColor = vec4(color, 1.0);
31
+ }`