maplibre-gl-nightlayer 1.0.0 → 2.0.0-beta.1

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.
package/README.md CHANGED
@@ -1,16 +1,24 @@
1
1
  # A Night Layer for MapLibre GL JS
2
2
 
3
- Adds a simple night shadow (Earth's shadow) on the map.
4
- ![image](https://github.com/user-attachments/assets/425f58e0-66f6-4779-8c03-ff543972c59e)
3
+ Night falls on your map: twilight, starlight and moonlight.
4
+
5
+ > [!WARNING]
6
+ > This package relies on MapLibre GL JS internals. Compatibility may break even in minor or patch releases.
7
+
8
+ <img width="1680" height="1280" alt="image" src="https://github.com/user-attachments/assets/ed8f5264-f997-4b3c-990c-35a2454a4e91" />
5
9
 
6
10
  ## Demo
11
+
7
12
  [example](https://kikuchan.github.io/maplibre-gl-nightlayer/)
8
13
 
9
14
  ## Usage
15
+
10
16
  ### Module
17
+
11
18
  ```sh
12
19
  $ npm install --save-dev maplibre-gl-nightlayer
13
20
  ```
21
+
14
22
  ```js
15
23
  import { NightLayer } from 'maplibre-gl-nightlayer';
16
24
 
@@ -18,47 +26,67 @@ import { NightLayer } from 'maplibre-gl-nightlayer';
18
26
 
19
27
  map.on('load', () => {
20
28
  map.addLayer(new NightLayer({
21
- // These are the default values
22
29
  date: null,
23
30
  opacity: 0.5,
24
- color: [0, 0, 0, 255],
25
- daytimeColor: [0, 0, 0, 0],
26
- twilightSteps: 0,
27
- twilightAttenuation: 0.5,
31
+ twilight: null,
28
32
  updateInterval: 10000,
33
+ atmosphere: false,
34
+ development: false,
29
35
  }));
30
36
  });
31
37
  ```
32
38
 
33
39
  ### via CDN
34
40
 
35
- Load MapLibre GL JS first so that `maplibregl` is available globally.
36
-
37
41
  ```html
38
- <script src="https://unpkg.com/maplibre-gl-nightlayer@latest/dist/nightlayer.min.js"></script>
39
- ```
40
- ```js
41
- map.on('load', () => {
42
- map.addLayer(new nightLayer.NightLayer({
42
+ <script type="module">
43
+ import { NightLayer } from 'https://unpkg.com/maplibre-gl-nightlayer';
44
+
43
45
  :
44
- }));
45
- });
46
+
47
+ map.on('load', () => {
48
+ map.addLayer(new NightLayer());
49
+ });
50
+ </script>
46
51
  ```
47
52
 
48
53
  ## API
54
+
49
55
  ```ts
50
56
  type Color3 = [number, number, number];
51
57
  type Color4 = [number, number, number, number];
52
58
  type Color = Color3 | Color4;
59
+ type TwilightSteps = 1 | 2 | 3 | 4;
60
+ type Twilight = { steps: TwilightSteps; color: Color4; attenuation: number };
61
+ type TwilightInput = TwilightSteps | {
62
+ steps: TwilightSteps;
63
+ color?: Color; // RGB/alpha 0..1; default [0,0,0,0.95]; RGB-only alpha defaults to 0.95
64
+ attenuation?: number; // positive, default 0.5
65
+ } | null;
66
+ type DevelopmentProcess = 'linear' | 'aces' | 'agx' | 'filmic';
67
+ type Development = {
68
+ process: DevelopmentProcess;
69
+ exposure: number; // -12..32 EV
70
+ autoExposure: boolean;
71
+ compensation: number; // -12..12 EV, AE only
72
+ gamma: number; // 0.1..4, post-development
73
+ };
74
+ type DevelopmentInput = false | DevelopmentProcess | Partial<Development>;
75
+ type Atmosphere = { sky: number; fog: number; stars: boolean };
76
+ type AtmosphereInput = boolean | number | {
77
+ sky?: boolean | number;
78
+ fog?: boolean | number;
79
+ stars?: boolean;
80
+ };
53
81
 
54
82
  type Options = {
55
83
  date?: Date | null;
56
84
  opacity?: number;
57
- color?: Color;
58
- daytimeColor?: Color;
59
- twilightSteps?: number;
60
- twilightAttenuation?: number;
85
+ twilight?: TwilightInput; // map-only stepped lighting; null/omission keeps continuous lighting
61
86
  updateInterval?: number;
87
+ atmosphere?: AtmosphereInput; // default false
88
+ development?: DevelopmentInput; // default false
89
+ lut?: string;
62
90
  };
63
91
 
64
92
  export class NightLayer implements CustomLayerInterface {
@@ -75,20 +103,20 @@ export class NightLayer implements CustomLayerInterface {
75
103
  getOpacity(): number;
76
104
  setOpacity(opacity: number): void;
77
105
 
78
- getColor(): Color4;
79
- setColor(color: Color): void;
106
+ getTwilight(): Twilight | null;
107
+ setTwilight(twilight?: TwilightInput): void;
80
108
 
81
- getDaytimeColor(): Color4;
82
- setDaytimeColor(color: Color): void;
109
+ getUpdateInterval(): number;
110
+ setUpdateInterval(interval: number): void;
83
111
 
84
- getTwilightSteps(): number;
85
- setTwilightSteps(steps: number): void;
112
+ getAtmosphere(): Atmosphere;
113
+ setAtmosphere(atmosphere: AtmosphereInput): void;
86
114
 
87
- getTwilightAttenuation(): number;
88
- setTwilightAttenuation(attenuation: number): void;
89
115
 
90
- getUpdateInterval(): number;
91
- setUpdateInterval(interval: number): void;
116
+ getDevelopment(): Development | false;
117
+ setDevelopment(development: DevelopmentInput): void;
118
+ setLUT(source?: string): Promise<void>;
119
+ captureEXR(options?: { exposure?: 'preview' | 'raw' }): Promise<Blob>;
92
120
 
93
121
  :
94
122
  };
@@ -0,0 +1,114 @@
1
+ The analytic AgX base-look and fitted ACES preview transforms are adapted from three.js:
2
+ https://github.com/mrdoob/three.js/blob/dev/src/renderers/shaders/ShaderChunk/tonemapping_pars_fragment.glsl.js
3
+ Its implementation follows Filament's Blender-derived Rec.2020 AgX transform
4
+ and the minimal AgX default-contrast polynomial approximation.
5
+ Adaptations: shared preview exposure/sRGB encoding, explicit zero-input guard,
6
+ no optional look, and no three.js dependency. This is an analytic approximation,
7
+ not Blender's complete OpenColorIO configuration.
8
+ The ACES transform uses Stephen Hill's fitted RRT/ODT and input/output matrices,
9
+ including three.js's 1/0.6 viewing-environment scale. Its rational fit is evaluated
10
+ in a scaled form to avoid HDR overflow. It is not the full ACES reference pipeline.
11
+ Original fitted transform: https://github.com/selfshadow/ltc_code/blob/master/webgl/shaders/ltc/ltc_blit.fs
12
+
13
+ The MIT License
14
+
15
+ Copyright © 2010-2026 three.js authors
16
+
17
+ Permission is hereby granted, free of charge, to any person obtaining a copy
18
+ of this software and associated documentation files (the "Software"), to deal
19
+ in the Software without restriction, including without limitation the rights
20
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21
+ copies of the Software, and to permit persons to whom the Software is
22
+ furnished to do so, subject to the following conditions:
23
+
24
+ The above copyright notice and this permission notice shall be included in
25
+ all copies or substantial portions of the Software.
26
+
27
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33
+ THE SOFTWARE.
34
+
35
+ The analytic Filmic preview is an independently implemented approximation to
36
+ Troy Sobotka's Filmic Blender OpenColorIO configuration and Base Contrast look:
37
+ https://github.com/sobotka/filmic-blender
38
+ Reference revision: 84bf836a4d9e130045c962c47ac4206395d4393b
39
+ The reference config, desat65cube.spi3d and Filmic_to_0-70_1-03.spi1d were used
40
+ for numerical comparisons. They are not included in the package. The intensity
41
+ compression is evaluated analytically, and an eighth-degree polynomial fitted
42
+ to Base Contrast replaces the 1D look. This is not the complete OCIO pipeline.
43
+ The default final log range is -12.473931188 to 4.026068812 (16.5 stops).
44
+ It uses the shared sRGB output encoder, not a second display encoding.
45
+
46
+ The atmospheric lighting LUT is generated using Eric Bruneton's
47
+ Precomputed Atmospheric Scattering implementation:
48
+ https://github.com/ebruneton/precomputed_atmospheric_scattering
49
+ Revision: 34f14e745cff948f4ca3157d1b62a445ffa7286f
50
+
51
+ Copyright (c) 2017 Eric Bruneton
52
+ All rights reserved.
53
+
54
+ Redistribution and use in source and binary forms, with or without
55
+ modification, are permitted provided that the following conditions are met:
56
+ 1. Redistributions of source code must retain the above copyright notice,
57
+ this list of conditions and the following disclaimer.
58
+ 2. Redistributions in binary form must reproduce the above copyright notice,
59
+ this list of conditions and the following disclaimer in the documentation
60
+ and/or other materials provided with the distribution.
61
+ 3. Neither the name of the copyright holders nor the names of its contributors
62
+ may be used to endorse or promote products derived from this software
63
+ without specific prior written permission.
64
+
65
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
66
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
69
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
70
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
71
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
72
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
73
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
74
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
75
+ POSSIBILITY OF SUCH DAMAGE.
76
+
77
+ The embedded diffuse Milky Way is adapted from Deep Star Maps 2020:
78
+ https://svs.gsfc.nasa.gov/4851/
79
+ Sources: milkyway_2020_4k.exr (texture), starmap_2020_4k.exr (photometric
80
+ reference), both in celestial ICRF/J2000 coordinates.
81
+ Credit: NASA/Goddard Space Flight Center Scientific Visualization Studio.
82
+ Gaia DR2: ESA/Gaia/DPAC.
83
+ Visualization by Ernie Wright (USRA).
84
+
85
+ Adaptations: linear-light reduction to 1024x512, sRGB encoding and lossy WebP
86
+ compression; empirical normalization from unsaturated catalogue stars.
87
+ No constellation figures or boundaries are included.
88
+ NASA media usage guidelines: https://www.nasa.gov/nasa-brand-center/images-and-media/
89
+ Gaia credit: https://gea.esac.esa.int/archive/documentation/GDR2/Miscellaneous/sec_credit_and_citation_instructions/
90
+
91
+ The embedded point-star dataset is derived from the HYG Stellar Database v4.1,
92
+ Copyright David Nash / Astronexus. It is licensed under Creative Commons
93
+ Attribution-ShareAlike 4.0 International (CC BY-SA 4.0):
94
+ https://creativecommons.org/licenses/by-sa/4.0/
95
+ Source: https://github.com/astronexus/HYG-Database
96
+ Revision: c7f7f883fe678cc7680169a50ccd7dcc49b060ce
97
+ Data: hyg/CURRENT/hygdata_v41.csv
98
+
99
+ Adaptations: select V <= 6.5; convert J2000 positions/proper motions and
100
+ V magnitudes/B-V colours to GPU point records, including blackbody-based
101
+ estimated angular diameters (not measured sizes); compress the
102
+ result as star-catalog.bin.gz. This adapted dataset retains CC BY-SA 4.0.
103
+ The library code is a separate work and retains the project's MIT license.
104
+
105
+ Photometric filter response: SVO Filter Profile Service, Generic/Johnson.V
106
+ https://svo2.cab.inta-csic.es/theory/fps/fps.php?ID=Generic/Johnson.V
107
+
108
+ Lunar orbital elements and main perturbation formulae: Paul Schlyter,
109
+ "How to compute planetary positions": https://stjarnhimlen.se/comp/ppcomp.html
110
+ Lunar visual phase-law reference: Krisciunas & Schaefer (1991),
111
+ "A Model of the Brightness of Moonlight", PASP 103, 1033.
112
+ Natural moonless-night horizontal illuminance reference (0.8 millilux):
113
+ U.S. National Park Service, "Metrics Guide to Night Skies Report":
114
+ https://www.nps.gov/subjects/nightskies/NSQmetrics.html
Binary file
package/dist/main.d.ts CHANGED
@@ -1,157 +1,103 @@
1
1
  import { CustomLayerInterface, CustomRenderMethodInput, Map } from "maplibre-gl";
2
- //#region src/main.d.ts
3
- /**
4
- * Get the subsolar point (longitude and latitude) at the given date.
5
- * @param date If null, the current date is used.
6
- * @returns The subsolar point.
7
- */
8
- export declare function getSubsolarPoint(date?: Date | null): {
9
- lng: number;
10
- lat: number;
2
+ type DepthSample = {
3
+ /** Straight-line range; infinity for sky. */
4
+ distanceMeters: number;
5
+ /** Optical-axis depth; infinity for sky or endpoints behind the camera. */
6
+ depthMeters: number;
7
+ groundCoverage: number;
11
8
  };
12
9
  type Color3 = [number, number, number];
13
10
  type Color4 = [number, number, number, number];
14
11
  type Color = Color3 | Color4;
15
- /**
16
- * Options for the NightLayer.
17
- */
12
+ type TwilightSteps = 1 | 2 | 3 | 4;
13
+ type Twilight = {
14
+ steps: TwilightSteps;
15
+ /** Linear RGB illumination multipliers and application strength, all 0..1. */
16
+ color: Color4;
17
+ attenuation: number;
18
+ };
19
+ type TwilightInput = TwilightSteps | {
20
+ steps: TwilightSteps;
21
+ color?: Color;
22
+ attenuation?: number;
23
+ } | null;
24
+ type Atmosphere = {
25
+ sky: number;
26
+ fog: number;
27
+ stars: boolean;
28
+ };
29
+ type AtmosphereInput = boolean | number | {
30
+ sky?: boolean | number;
31
+ fog?: boolean | number;
32
+ stars?: boolean;
33
+ };
34
+ type DevelopmentProcess = "linear" | "aces" | "agx" | "filmic";
35
+ type Development = {
36
+ process: DevelopmentProcess;
37
+ exposure: number;
38
+ autoExposure: boolean;
39
+ compensation: number;
40
+ gamma: number;
41
+ };
42
+ type DevelopmentInput = false | DevelopmentProcess | Partial<Development>;
18
43
  type Options = {
19
- /**
20
- * Date for the shadow.
21
- * If null, the current date is used.
22
- */
23
44
  date?: Date | null;
24
- /**
25
- * Opacity of the layer.
26
- * 0.0 means fully transparent, 1.0 means fully opaque
27
- * default: 0.5
28
- */
45
+ /** Whole-layer opacity, including atmospheric effects. Default: 0.5. */
29
46
  opacity?: number;
47
+ /** Map-only stepped surface lighting; null/omission keeps continuous lighting.
48
+ * Defaults: black with alpha 0.95, attenuation 0.5. Ignored by camera/physical export. */
49
+ twilight?: TwilightInput;
50
+ updateInterval?: number;
30
51
  /**
31
- * Color of the shadow: [r, g, b, a]
32
- * Each value should be in the range [0, 255].
33
- * default: [0, 0, 0, 255]
34
- */
35
- color?: Color;
36
- /**
37
- * Color of the daytime: [r, g, b, a]
38
- * Each value should be in the range [0, 255].
39
- * default: [0, 0, 0, 0]
40
- */
41
- daytimeColor?: Color;
42
- /**
43
- * Number of twilight steps.
44
- * 0 means no steps (gradation), 1 means one step (day/night), etc.
45
- * default: 0
46
- */
47
- twilightSteps?: number;
48
- /**
49
- * Attenuation factor for each twilight step.
50
- * 0.0 means no attenuation, 1.0 means full attenuation
51
- * default: 0.5
52
- */
53
- twilightAttenuation?: number;
54
- /**
55
- * Update interval in milliseconds.
56
- * Periodically triggers a repaint.
57
- * default: 10000
52
+ * Sky scattering, finite-path fog, and celestial backgrounds/disks. Default: false.
58
53
  */
59
- updateInterval?: number;
54
+ atmosphere?: AtmosphereInput;
55
+ /** False: fixed linear map presentation. A process string abbreviates an object. */
56
+ development?: DevelopmentInput;
57
+ /** Atmosphere LUT archive URL. Defaults to atmosphere-lut.zip next to the module. */
58
+ lut?: string;
59
+ };
60
+ /** Subsolar longitude/latitude; omitted or null date means now. */
61
+ export declare function getSubsolarPoint(date?: Date | null): {
62
+ lng: number;
63
+ lat: number;
60
64
  };
61
- /**
62
- * A custom Maplibre GL JS layer that renders the night side of the Earth.
63
- */
64
65
  export declare class NightLayer implements CustomLayerInterface {
65
66
  #private;
66
67
  id: string;
67
68
  type: "custom";
68
69
  renderingMode: "2d";
69
- /**
70
- * Create a new NightLayer instance. Intended to be passed to `map.addLayer`.
71
- * @param opts The options.
72
- */
73
70
  constructor(opts?: Options);
74
- /**
75
- * Get the current subsolar point (longitude and latitude)
76
- * @returns The subsolar point.
77
- */
78
71
  getSubsolarPoint(): {
79
72
  lng: number;
80
73
  lat: number;
81
74
  };
82
- /**
83
- * Get the date for the shadow.
84
- * @returns The date. If null, the current date is used.
85
- */
86
75
  getDate(): Date | null;
87
- /**
88
- * Set the date for the shadow.
89
- * @param date If null, the current date is used.
90
- */
91
76
  setDate(date: Date | null): void;
92
- /**
93
- * Get the opacity of the shadow.
94
- * @returns The opacity. 0.0 means fully transparent, 1.0 means fully opaque.
95
- */
96
77
  getOpacity(): number;
97
- /**
98
- * Set the opacity of the shadow.
99
- * @param opacity 0.0 means fully transparent, 1.0 means fully opaque.
100
- */
101
78
  setOpacity(opacity: number): void;
102
- /**
103
- * Get the color of the shadow.
104
- * @returns The color. Each value is in the range [0, 255].
105
- */
106
- getColor(): Color4;
107
- /**
108
- * Set the color of the shadow.
109
- * @param color Each value should be in the range [0, 255].
110
- */
111
- setColor(color: Color): void;
112
- /**
113
- * Get the color of the daytime.
114
- * @returns The color [r, g, b, a]. Each value is in the range [0, 255].
115
- */
116
- getDaytimeColor(): Color4;
117
- /**
118
- * Set the color of the daytime.
119
- * @param color Each value should be in the range [0, 255].
120
- */
121
- setDaytimeColor(color: Color): void;
122
- /**
123
- * Get the number of twilight steps.
124
- * @returns The steps. 0 means no steps (gradation), 1 means one step (day/night), etc.
125
- */
126
- getTwilightSteps(): number;
127
- /**
128
- * Set the number of twilight steps.
129
- * @param steps 0 means no steps (gradation), 1 means one step (day/night), etc.
130
- */
131
- setTwilightSteps(steps: number): void;
132
- /**
133
- * Get the attenuation factor for each twilight step.
134
- * @returns The attenuation. 0.0 means no attenuation, 1.0 means full attenuation.
135
- */
136
- getTwilightAttenuation(): number;
137
- /**
138
- * Set the attenuation factor for each twilight step.
139
- * @param attenuation 0.0 means no attenuation, 1.0 means full attenuation.
140
- */
141
- setTwilightAttenuation(attenuation: number): void;
142
- /**
143
- * Get the update interval.
144
- * @returns The interval in milliseconds.
145
- */
79
+ getTwilight(): Twilight | null;
80
+ setTwilight(twilight?: TwilightInput): void;
146
81
  getUpdateInterval(): number;
147
- /**
148
- * Set the update interval.
149
- * @param interval The interval in milliseconds..
150
- */
151
82
  setUpdateInterval(interval: number): void;
152
- onAdd(map: Map, _gl: WebGLRenderingContext | WebGL2RenderingContext): void;
153
- onRemove(_map: Map, gl: WebGLRenderingContext | WebGL2RenderingContext): void;
154
- render(gl: WebGLRenderingContext | WebGL2RenderingContext, options: CustomRenderMethodInput): void;
83
+ getAtmosphere(): Atmosphere;
84
+ setAtmosphere(atmosphere: AtmosphereInput): void;
85
+ getDevelopment(): Development | false;
86
+ setDevelopment(input: DevelopmentInput): void;
87
+ /** Last rendered logical pixel at canvas-relative CSS coordinates (top-left).
88
+ * Returns ground depth and coverage for each pixel.
89
+ * Returns null before a valid frame, outside the canvas, or for unsupported CRS. */
90
+ getDepthAt(x: number, y: number): DepthSample | null;
91
+ /** Capture linear FLOAT RGB + Z (axial metres) OpenEXR, without preview tone mapping.
92
+ * Pass { exposure: 'raw' } to preserve the unexposed receiver values. */
93
+ captureEXR(options?: {
94
+ exposure?: "preview" | "raw";
95
+ }): Promise<Blob>;
96
+ /** Replace the LUT after loading succeeds; omit the URL to restore the default atmosphere archive. */
97
+ setLUT(source?: string): Promise<void>;
98
+ onAdd(map: Map, context: WebGLRenderingContext | WebGL2RenderingContext): void;
99
+ onRemove(map: Map, context: WebGLRenderingContext | WebGL2RenderingContext): void;
100
+ prerender(gl: WebGL2RenderingContext, options: CustomRenderMethodInput): void;
101
+ render(context: WebGLRenderingContext | WebGL2RenderingContext, options: CustomRenderMethodInput): void;
155
102
  }
156
- //#endregion
157
- export { NightLayer as default };
103
+ export { type Atmosphere, type AtmosphereInput, type Color, type Color3, type Color4, type DepthSample, type Development, type DevelopmentInput, type DevelopmentProcess, NightLayer as default, type Twilight, type TwilightInput, type TwilightSteps };