maplibre-gl-nightlayer 1.0.0-alpha.9 → 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.
- package/README.md +28 -7
- package/dist/main.d.ts +138 -120
- package/dist/main.js +47 -38
- package/dist/nightlayer.min.js +81 -0
- package/package.json +19 -17
package/README.md
CHANGED
|
@@ -3,13 +3,14 @@
|
|
|
3
3
|
Adds a simple night shadow (Earth's shadow) on the map.
|
|
4
4
|

|
|
5
5
|
|
|
6
|
-
##
|
|
6
|
+
## Demo
|
|
7
|
+
[example](https://kikuchan.github.io/maplibre-gl-nightlayer/)
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
### Module
|
|
7
11
|
```sh
|
|
8
12
|
$ npm install --save-dev maplibre-gl-nightlayer
|
|
9
13
|
```
|
|
10
|
-
|
|
11
|
-
## Usage
|
|
12
|
-
|
|
13
14
|
```js
|
|
14
15
|
import { NightLayer } from 'maplibre-gl-nightlayer';
|
|
15
16
|
|
|
@@ -21,9 +22,25 @@ map.on('load', () => {
|
|
|
21
22
|
date: null,
|
|
22
23
|
opacity: 0.5,
|
|
23
24
|
color: [0, 0, 0, 255],
|
|
24
|
-
daytimeColor: [0, 0, 0, 0],
|
|
25
|
+
daytimeColor: [0, 0, 0, 0],
|
|
25
26
|
twilightSteps: 0,
|
|
26
27
|
twilightAttenuation: 0.5,
|
|
28
|
+
updateInterval: 10000,
|
|
29
|
+
}));
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### via CDN
|
|
34
|
+
|
|
35
|
+
Load MapLibre GL JS first so that `maplibregl` is available globally.
|
|
36
|
+
|
|
37
|
+
```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({
|
|
43
|
+
:
|
|
27
44
|
}));
|
|
28
45
|
});
|
|
29
46
|
```
|
|
@@ -41,6 +58,7 @@ type Options = {
|
|
|
41
58
|
daytimeColor?: Color;
|
|
42
59
|
twilightSteps?: number;
|
|
43
60
|
twilightAttenuation?: number;
|
|
61
|
+
updateInterval?: number;
|
|
44
62
|
};
|
|
45
63
|
|
|
46
64
|
export class NightLayer implements CustomLayerInterface {
|
|
@@ -61,7 +79,7 @@ export class NightLayer implements CustomLayerInterface {
|
|
|
61
79
|
setColor(color: Color): void;
|
|
62
80
|
|
|
63
81
|
getDaytimeColor(): Color4;
|
|
64
|
-
setDaytimeColor(color: Color);
|
|
82
|
+
setDaytimeColor(color: Color): void;
|
|
65
83
|
|
|
66
84
|
getTwilightSteps(): number;
|
|
67
85
|
setTwilightSteps(steps: number): void;
|
|
@@ -69,10 +87,13 @@ export class NightLayer implements CustomLayerInterface {
|
|
|
69
87
|
getTwilightAttenuation(): number;
|
|
70
88
|
setTwilightAttenuation(attenuation: number): void;
|
|
71
89
|
|
|
90
|
+
getUpdateInterval(): number;
|
|
91
|
+
setUpdateInterval(interval: number): void;
|
|
92
|
+
|
|
72
93
|
:
|
|
73
94
|
};
|
|
74
95
|
|
|
75
|
-
export function getSubsolarPoint(date?: Date): {
|
|
96
|
+
export function getSubsolarPoint(date?: Date | null): {
|
|
76
97
|
lng: number;
|
|
77
98
|
lat: number;
|
|
78
99
|
};
|
package/dist/main.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { CustomLayerInterface, CustomRenderMethodInput, Map } from "maplibre-gl";
|
|
2
|
+
//#region src/main.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* Get the subsolar point (longitude and latitude) at the given date.
|
|
5
5
|
* @param date If null, the current date is used.
|
|
6
6
|
* @returns The subsolar point.
|
|
7
7
|
*/
|
|
8
|
-
export declare function getSubsolarPoint(date?: Date): {
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
export declare function getSubsolarPoint(date?: Date | null): {
|
|
9
|
+
lng: number;
|
|
10
|
+
lat: number;
|
|
11
11
|
};
|
|
12
12
|
type Color3 = [number, number, number];
|
|
13
13
|
type Color4 = [number, number, number, number];
|
|
@@ -16,124 +16,142 @@ type Color = Color3 | Color4;
|
|
|
16
16
|
* Options for the NightLayer.
|
|
17
17
|
*/
|
|
18
18
|
type Options = {
|
|
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
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Date for the shadow.
|
|
21
|
+
* If null, the current date is used.
|
|
22
|
+
*/
|
|
23
|
+
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
|
+
*/
|
|
29
|
+
opacity?: number;
|
|
30
|
+
/**
|
|
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
|
|
58
|
+
*/
|
|
59
|
+
updateInterval?: number;
|
|
52
60
|
};
|
|
53
61
|
/**
|
|
54
62
|
* A custom Maplibre GL JS layer that renders the night side of the Earth.
|
|
55
63
|
*/
|
|
56
64
|
export declare class NightLayer implements CustomLayerInterface {
|
|
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
|
-
|
|
65
|
+
#private;
|
|
66
|
+
id: string;
|
|
67
|
+
type: "custom";
|
|
68
|
+
renderingMode: "2d";
|
|
69
|
+
/**
|
|
70
|
+
* Create a new NightLayer instance. Intended to be passed to `map.addLayer`.
|
|
71
|
+
* @param opts The options.
|
|
72
|
+
*/
|
|
73
|
+
constructor(opts?: Options);
|
|
74
|
+
/**
|
|
75
|
+
* Get the current subsolar point (longitude and latitude)
|
|
76
|
+
* @returns The subsolar point.
|
|
77
|
+
*/
|
|
78
|
+
getSubsolarPoint(): {
|
|
79
|
+
lng: number;
|
|
80
|
+
lat: number;
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Get the date for the shadow.
|
|
84
|
+
* @returns The date. If null, the current date is used.
|
|
85
|
+
*/
|
|
86
|
+
getDate(): Date | null;
|
|
87
|
+
/**
|
|
88
|
+
* Set the date for the shadow.
|
|
89
|
+
* @param date If null, the current date is used.
|
|
90
|
+
*/
|
|
91
|
+
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
|
+
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
|
+
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
|
+
*/
|
|
146
|
+
getUpdateInterval(): number;
|
|
147
|
+
/**
|
|
148
|
+
* Set the update interval.
|
|
149
|
+
* @param interval The interval in milliseconds..
|
|
150
|
+
*/
|
|
151
|
+
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;
|
|
138
155
|
}
|
|
139
|
-
|
|
156
|
+
//#endregion
|
|
157
|
+
export { NightLayer as default };
|
package/dist/main.js
CHANGED
|
@@ -1,72 +1,81 @@
|
|
|
1
|
-
|
|
1
|
+
import{createTileMesh as e}from"maplibre-gl";const t=Array.from({length:15},(e,t)=>Math.sin(n(t===0?-.8333333:-6*t)));function n(e){return e*Math.PI/180}function r(e){return e/Math.PI*180}function i(e){let[t,n,r,i]=e.map(e=>e/255);return[t*i,n*i,r*i,i]}function a(e){if(e??=/* @__PURE__ */ new Date,!Number.isFinite(e.getTime()))throw RangeError(`Invalid date`);let t=e.getUTCFullYear(),i=e.getUTCMonth(),a=+(i>1&&t%4==0&&(t%100!=0||t%400==0)),o=[0,31,59,90,120,151,181,212,243,273,304,334][i]+e.getUTCDate()+a,s=e.getUTCHours()+e.getUTCMinutes()/60+e.getUTCSeconds()/3600+e.getUTCMilliseconds()/36e5,c=o+s/24,l=2*Math.PI/365.24,u=n(23.44),d=(c+9)*l,f=d+.0334*Math.sin((c-3)*l),p=(d-Math.atan2(Math.sin(f),Math.cos(f)*Math.cos(u)))/Math.PI,m=720*(p-Math.trunc(p+.5));return{lng:-15*(s-12+m/60),lat:r(Math.asin(Math.sin(-u)*Math.cos(f)))}}var o=class{id=`nightlayer`;type=`custom`;renderingMode=`2d`;#e;#t;#n;#r;#i;#a;#o;#s;#c;#l;#u;constructor(e={}){this.#t=e.opacity??.5,this.#o=e.twilightSteps??0,this.#s=e.twilightAttenuation??.5,this.#a=e.updateInterval??1e4,this.setColor(e.color??[0,0,0,255]),this.setDaytimeColor(e.daytimeColor??[0,0,0,0]),this.setDate(e.date??null)}#d(){let e=this.#u!==void 0&&this.#e===null;this.#i&&=(clearInterval(this.#i),void 0),e&&this.#a>0&&(this.#i=setInterval(()=>this.#u?.triggerRepaint(),this.#a))}getSubsolarPoint(){return a(this.#e)}getDate(){return this.#e?new Date(this.#e.getTime()):null}setDate(e){if(e!==null&&!Number.isFinite(e.getTime()))throw RangeError(`Invalid date`);this.#e=e===null?null:new Date(e.getTime()),this.#d(),this.#u?.triggerRepaint()}getOpacity(){return this.#t}setOpacity(e){this.#t=e,this.#u?.triggerRepaint()}getColor(){return this.#n}setColor(e){this.#n=[...e.slice(0,3),e?.[3]??255],this.#u?.triggerRepaint()}getDaytimeColor(){return this.#r}setDaytimeColor(e){this.#r=[...e.slice(0,3),e?.[3]??255],this.#u?.triggerRepaint()}getTwilightSteps(){return this.#o}setTwilightSteps(e){this.#o=e,this.#u?.triggerRepaint()}getTwilightAttenuation(){return this.#s}setTwilightAttenuation(e){this.#s=e,this.#u?.triggerRepaint()}getUpdateInterval(){return this.#a}setUpdateInterval(e){this.#a=e,this.#d()}onAdd(e,t){this.#c=/* @__PURE__ */ new Map,this.#l=/* @__PURE__ */ new Map,this.#u=e,this.#d()}onRemove(e,t){if(this.#i!==void 0&&(clearInterval(this.#i),this.#i=void 0),this.#c)for(let e of this.#c.values())t.deleteProgram(e);if(this.#l)for(let e of this.#l.values())t.deleteBuffer(e.vertices),t.deleteBuffer(e.indices);this.#c=void 0,this.#l=void 0,this.#u=void 0}#f(e,t){let r=`#version 300 es
|
|
2
2
|
precision highp float;
|
|
3
3
|
|
|
4
4
|
in vec2 a_position;
|
|
5
5
|
out vec2 v_position;
|
|
6
|
+
out float v_pole;
|
|
6
7
|
|
|
7
|
-
${
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
vec4 projectTile(vec2 pos) {
|
|
11
|
-
return u_matrix * vec4(pos, 0., 1.);
|
|
12
|
-
}
|
|
13
|
-
`:`
|
|
14
|
-
${i.vertexShaderPrelude}
|
|
15
|
-
${i.define}
|
|
16
|
-
`}
|
|
8
|
+
${t.vertexShaderPrelude}
|
|
9
|
+
${t.define}
|
|
17
10
|
|
|
18
11
|
void main() {
|
|
19
|
-
gl_Position = projectTile(a_position);
|
|
20
|
-
v_position = a_position;
|
|
12
|
+
gl_Position = projectTile(a_position, a_position);
|
|
13
|
+
v_position = vec2(a_position.x, clamp(a_position.y, 0.0, 8192.0)) / 8192.0 - 0.5;
|
|
14
|
+
v_pole = a_position.y < -32767.5 ? 1.0 : (a_position.y > 32766.5 ? -1.0 : 0.0);
|
|
21
15
|
}
|
|
22
|
-
`,
|
|
16
|
+
`,i=`#version 300 es
|
|
23
17
|
precision highp float;
|
|
24
18
|
|
|
25
19
|
in vec2 v_position;
|
|
20
|
+
in float v_pole;
|
|
26
21
|
out vec4 fragColor;
|
|
27
22
|
|
|
28
|
-
uniform
|
|
23
|
+
uniform vec4 u_tile; // exp(mercator latitude), sin(latitude), cos(latitude), tile size
|
|
24
|
+
uniform vec3 u_sun; // sin(solar latitude), cos(solar latitude) * cos/sin(relative longitude)
|
|
25
|
+
uniform float u_center_height;
|
|
26
|
+
uniform float u_thresholds[15];
|
|
29
27
|
uniform float u_opacity;
|
|
30
28
|
uniform float u_twilight_steps;
|
|
31
29
|
uniform float u_twilight_attenuation;
|
|
32
30
|
uniform vec4 u_daytime_color;
|
|
33
31
|
uniform vec4 u_night_color;
|
|
34
32
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
float y = mercator.y;
|
|
39
|
-
|
|
40
|
-
float lng = x * 360.0 - 180.0;
|
|
41
|
-
float lat = degrees(2.0 * atan(exp(${Math.PI} * (1.0 - 2.0 * y))) - ${Math.PI/2});
|
|
42
|
-
|
|
43
|
-
return vec2(lng, lat);
|
|
33
|
+
float expMinusOne(float x) {
|
|
34
|
+
if (abs(x) < 0.001) return x * (1.0 + x * (0.5 + x * (1.0 / 6.0 + x / 24.0)));
|
|
35
|
+
return exp(x) - 1.0;
|
|
44
36
|
}
|
|
45
37
|
|
|
46
38
|
void main() {
|
|
47
|
-
|
|
39
|
+
// Work with small tile-local differences, not rounded global coordinates.
|
|
40
|
+
float longitudeDelta = v_position.x * u_tile.w * ${2*Math.PI};
|
|
41
|
+
float e = expMinusOne(-v_position.y * u_tile.w * ${2*Math.PI});
|
|
42
|
+
float t = u_tile.x;
|
|
43
|
+
float denominator = t * t + 1.0;
|
|
44
|
+
float deltaSquared = t * t * e * (2.0 + e);
|
|
45
|
+
float product = denominator * (denominator + deltaSquared);
|
|
46
|
+
float sinDelta = 2.0 * deltaSquared / product;
|
|
47
|
+
float cosDelta = 2.0 * t * (e * denominator - deltaSquared) / product;
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
float
|
|
52
|
-
float
|
|
53
|
-
float
|
|
49
|
+
float poleAngle = v_pole * ${n(4.948871220193396)};
|
|
50
|
+
float poleCosDelta = -2.0 * pow(sin(poleAngle * 0.5), 2.0);
|
|
51
|
+
float poleSin = sin(poleAngle);
|
|
52
|
+
float latitudeSin = u_tile.y + sinDelta;
|
|
53
|
+
float latitudeCos = u_tile.z + cosDelta;
|
|
54
|
+
sinDelta += latitudeSin * poleCosDelta + latitudeCos * poleSin;
|
|
55
|
+
cosDelta += latitudeCos * poleCosDelta - latitudeSin * poleSin;
|
|
56
|
+
latitudeCos = u_tile.z + cosDelta;
|
|
54
57
|
|
|
58
|
+
float longitudeCosDelta = -2.0 * pow(sin(longitudeDelta * 0.5), 2.0);
|
|
59
|
+
float heightDelta = u_sun.x * sinDelta + u_sun.y * cosDelta
|
|
60
|
+
+ latitudeCos * (u_sun.y * longitudeCosDelta + u_sun.z * sin(longitudeDelta));
|
|
55
61
|
float twilightStepAngle = 6.0;
|
|
56
|
-
float twilightBegins = -0.8333333;
|
|
57
62
|
float twilightSteps = u_twilight_steps;
|
|
58
63
|
|
|
59
64
|
// Attenuation for each twilightStepAngle
|
|
60
65
|
float att = u_twilight_attenuation;
|
|
61
|
-
float twilightLevel
|
|
66
|
+
float twilightLevel;
|
|
62
67
|
if (twilightSteps > 0.) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
twilightLevel = 0.;
|
|
68
|
+
// Threshold residuals are computed in float64 on the CPU before upload.
|
|
69
|
+
twilightLevel = 1.0;
|
|
70
|
+
for (int i = 1; i < 15; i++) {
|
|
71
|
+
if (u_thresholds[i] + heightDelta < 0.0) twilightLevel += 1.0;
|
|
68
72
|
}
|
|
73
|
+
twilightLevel = ceil(min(twilightLevel, twilightSteps));
|
|
74
|
+
if (u_thresholds[0] + heightDelta > 0.0) twilightLevel = 0.0;
|
|
75
|
+
} else {
|
|
76
|
+
float altitude = degrees(asin(clamp(u_center_height + heightDelta, -1.0, 1.0)));
|
|
77
|
+
twilightLevel = -altitude / twilightStepAngle;
|
|
69
78
|
}
|
|
70
79
|
float brightness = clamp(pow(clamp(1. - att, 0., 1.), twilightLevel), 0., 1.);
|
|
71
80
|
fragColor = mix(u_night_color, u_daytime_color, brightness) * u_opacity;
|
|
72
|
-
}`,e
|
|
81
|
+
}`,a=e.createShader(e.VERTEX_SHADER);if(!a)throw Error(`failed to create vertex shader`);let o=e.createShader(e.FRAGMENT_SHADER);if(!o)throw e.deleteShader(a),Error(`failed to create fragment shader`);let s=null;try{if(e.shaderSource(a,r),e.compileShader(a),e.shaderSource(o,i),e.compileShader(o),s=e.createProgram(),!s)throw Error(`failed to create program`);if(e.attachShader(s,a),e.attachShader(s,o),e.linkProgram(s),!e.getProgramParameter(s,e.LINK_STATUS))throw Error(e.getProgramInfoLog(s)||`failed to link program`);return s}catch(t){throw s&&e.deleteProgram(s),s=null,t}finally{s&&(e.detachShader(s,a),e.detachShader(s,o)),e.deleteShader(a),e.deleteShader(o)}}#p(t,n,r,i){let a=`${n}:${r}:${i}`,o=this.#l.get(a);if(o)return o;let s=e({granularity:n,generateBorders:!1,extendToNorthPole:r,extendToSouthPole:i},`16bit`),c=t.createBuffer(),l=t.createBuffer();if(!c||!l)throw c&&t.deleteBuffer(c),l&&t.deleteBuffer(l),Error(`failed to create tile mesh buffers`);t.bindBuffer(t.ARRAY_BUFFER,c),t.bufferData(t.ARRAY_BUFFER,s.vertices,t.STATIC_DRAW),t.bindBuffer(t.ELEMENT_ARRAY_BUFFER,l),t.bufferData(t.ELEMENT_ARRAY_BUFFER,s.indices,t.STATIC_DRAW);let u={vertices:c,indices:l,count:s.indices.byteLength/2};return this.#l.set(a,u),u}render(e,r){if(!this.#c||!this.#l||!this.#u)return;let a=r.shaderData,o=a.variantName;this.#c.has(o)||this.#c.set(o,this.#f(e,a));let s=this.#c.get(o);if(!s)return;let{lng:c,lat:l}=this.getSubsolarPoint(),u=n(c),d=n(l),f=Math.sin(d),p=Math.cos(d);e.useProgram(s),e.uniform1f(e.getUniformLocation(s,`u_opacity`),this.#t),e.uniform4fv(e.getUniformLocation(s,`u_night_color`),i(this.#n)),e.uniform4fv(e.getUniformLocation(s,`u_daytime_color`),i(this.#r)),e.uniform1f(e.getUniformLocation(s,`u_twilight_steps`),this.#o),e.uniform1f(e.getUniformLocation(s,`u_twilight_attenuation`),this.#s);let m=r.defaultProjectionData.projectionTransition>0,h=this.#u.coveringTiles({tileSize:256});e.disable(e.SCISSOR_TEST),e.disable(e.DEPTH_TEST),e.disable(e.STENCIL_TEST),e.enable(e.CULL_FACE),e.frontFace(e.CCW),e.cullFace(e.BACK),e.enable(e.BLEND),e.blendFunc(e.ONE,e.ONE_MINUS_SRC_ALPHA);for(let i of h){let{x:a,y:o,z:c}=i.canonical,l=2**-c,d=r.getProjectionData({tileID:{canonical:{x:a,y:o,z:c},wrap:i.wrap},applyGlobeMatrix:!0});e.uniformMatrix4fv(e.getUniformLocation(s,`u_projection_matrix`),!1,d.mainMatrix),e.uniformMatrix4fv(e.getUniformLocation(s,`u_projection_fallback_matrix`),!1,d.fallbackMatrix),e.uniform4fv(e.getUniformLocation(s,`u_projection_tile_mercator_coords`),d.tileMercatorCoords),e.uniform4fv(e.getUniformLocation(s,`u_projection_clipping_plane`),d.clippingPlane),e.uniform1f(e.getUniformLocation(s,`u_projection_transition`),d.projectionTransition);let h=Math.exp(Math.PI*(1-2*(o+.5)*l)),g=(h*h-1)/(h*h+1),_=2*h/(h*h+1),v=u-n((a+.5)*l*360-180),y=[f,p*Math.cos(v),p*Math.sin(v)],b=y[0]*g+y[1]*_,x=t.map(e=>b-e);e.uniform4fv(e.getUniformLocation(s,`u_tile`),[h,g,_,l]),e.uniform3fv(e.getUniformLocation(s,`u_sun`),y),e.uniform1f(e.getUniformLocation(s,`u_center_height`),b),e.uniform1fv(e.getUniformLocation(s,`u_thresholds[0]`),x);let S=m?Math.max(Math.floor(128*l),32):1,C=this.#p(e,S,m&&o===0,m&&o===2**c-1);e.bindBuffer(e.ARRAY_BUFFER,C.vertices),e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,C.indices);let w=e.getAttribLocation(s,`a_position`);e.enableVertexAttribArray(w),e.vertexAttribPointer(w,2,e.SHORT,!1,0,0),e.drawElements(e.TRIANGLES,C.count,e.UNSIGNED_SHORT,0)}}};export{o as NightLayer,o as default,a as getSubsolarPoint};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
var nightLayer=(function(e,t){Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});let n=Array.from({length:15},(e,t)=>Math.sin(r(t===0?-.8333333:-6*t)));function r(e){return e*Math.PI/180}function i(e){return e/Math.PI*180}function a(e){let[t,n,r,i]=e.map(e=>e/255);return[t*i,n*i,r*i,i]}function o(e){if(e??=/* @__PURE__ */ new Date,!Number.isFinite(e.getTime()))throw RangeError(`Invalid date`);let t=e.getUTCFullYear(),n=e.getUTCMonth(),a=+(n>1&&t%4==0&&(t%100!=0||t%400==0)),o=[0,31,59,90,120,151,181,212,243,273,304,334][n]+e.getUTCDate()+a,s=e.getUTCHours()+e.getUTCMinutes()/60+e.getUTCSeconds()/3600+e.getUTCMilliseconds()/36e5,c=o+s/24,l=2*Math.PI/365.24,u=r(23.44),d=(c+9)*l,f=d+.0334*Math.sin((c-3)*l),p=(d-Math.atan2(Math.sin(f),Math.cos(f)*Math.cos(u)))/Math.PI,m=720*(p-Math.trunc(p+.5));return{lng:-15*(s-12+m/60),lat:i(Math.asin(Math.sin(-u)*Math.cos(f)))}}var s=class{id=`nightlayer`;type=`custom`;renderingMode=`2d`;#e;#t;#n;#r;#i;#a;#o;#s;#c;#l;#u;constructor(e={}){this.#t=e.opacity??.5,this.#o=e.twilightSteps??0,this.#s=e.twilightAttenuation??.5,this.#a=e.updateInterval??1e4,this.setColor(e.color??[0,0,0,255]),this.setDaytimeColor(e.daytimeColor??[0,0,0,0]),this.setDate(e.date??null)}#d(){let e=this.#u!==void 0&&this.#e===null;this.#i&&=(clearInterval(this.#i),void 0),e&&this.#a>0&&(this.#i=setInterval(()=>this.#u?.triggerRepaint(),this.#a))}getSubsolarPoint(){return o(this.#e)}getDate(){return this.#e?new Date(this.#e.getTime()):null}setDate(e){if(e!==null&&!Number.isFinite(e.getTime()))throw RangeError(`Invalid date`);this.#e=e===null?null:new Date(e.getTime()),this.#d(),this.#u?.triggerRepaint()}getOpacity(){return this.#t}setOpacity(e){this.#t=e,this.#u?.triggerRepaint()}getColor(){return this.#n}setColor(e){this.#n=[...e.slice(0,3),e?.[3]??255],this.#u?.triggerRepaint()}getDaytimeColor(){return this.#r}setDaytimeColor(e){this.#r=[...e.slice(0,3),e?.[3]??255],this.#u?.triggerRepaint()}getTwilightSteps(){return this.#o}setTwilightSteps(e){this.#o=e,this.#u?.triggerRepaint()}getTwilightAttenuation(){return this.#s}setTwilightAttenuation(e){this.#s=e,this.#u?.triggerRepaint()}getUpdateInterval(){return this.#a}setUpdateInterval(e){this.#a=e,this.#d()}onAdd(e,t){this.#c=/* @__PURE__ */ new Map,this.#l=/* @__PURE__ */ new Map,this.#u=e,this.#d()}onRemove(e,t){if(this.#i!==void 0&&(clearInterval(this.#i),this.#i=void 0),this.#c)for(let e of this.#c.values())t.deleteProgram(e);if(this.#l)for(let e of this.#l.values())t.deleteBuffer(e.vertices),t.deleteBuffer(e.indices);this.#c=void 0,this.#l=void 0,this.#u=void 0}#f(e,t){let n=`#version 300 es
|
|
2
|
+
precision highp float;
|
|
3
|
+
|
|
4
|
+
in vec2 a_position;
|
|
5
|
+
out vec2 v_position;
|
|
6
|
+
out float v_pole;
|
|
7
|
+
|
|
8
|
+
${t.vertexShaderPrelude}
|
|
9
|
+
${t.define}
|
|
10
|
+
|
|
11
|
+
void main() {
|
|
12
|
+
gl_Position = projectTile(a_position, a_position);
|
|
13
|
+
v_position = vec2(a_position.x, clamp(a_position.y, 0.0, 8192.0)) / 8192.0 - 0.5;
|
|
14
|
+
v_pole = a_position.y < -32767.5 ? 1.0 : (a_position.y > 32766.5 ? -1.0 : 0.0);
|
|
15
|
+
}
|
|
16
|
+
`,i=`#version 300 es
|
|
17
|
+
precision highp float;
|
|
18
|
+
|
|
19
|
+
in vec2 v_position;
|
|
20
|
+
in float v_pole;
|
|
21
|
+
out vec4 fragColor;
|
|
22
|
+
|
|
23
|
+
uniform vec4 u_tile; // exp(mercator latitude), sin(latitude), cos(latitude), tile size
|
|
24
|
+
uniform vec3 u_sun; // sin(solar latitude), cos(solar latitude) * cos/sin(relative longitude)
|
|
25
|
+
uniform float u_center_height;
|
|
26
|
+
uniform float u_thresholds[15];
|
|
27
|
+
uniform float u_opacity;
|
|
28
|
+
uniform float u_twilight_steps;
|
|
29
|
+
uniform float u_twilight_attenuation;
|
|
30
|
+
uniform vec4 u_daytime_color;
|
|
31
|
+
uniform vec4 u_night_color;
|
|
32
|
+
|
|
33
|
+
float expMinusOne(float x) {
|
|
34
|
+
if (abs(x) < 0.001) return x * (1.0 + x * (0.5 + x * (1.0 / 6.0 + x / 24.0)));
|
|
35
|
+
return exp(x) - 1.0;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
void main() {
|
|
39
|
+
// Work with small tile-local differences, not rounded global coordinates.
|
|
40
|
+
float longitudeDelta = v_position.x * u_tile.w * ${2*Math.PI};
|
|
41
|
+
float e = expMinusOne(-v_position.y * u_tile.w * ${2*Math.PI});
|
|
42
|
+
float t = u_tile.x;
|
|
43
|
+
float denominator = t * t + 1.0;
|
|
44
|
+
float deltaSquared = t * t * e * (2.0 + e);
|
|
45
|
+
float product = denominator * (denominator + deltaSquared);
|
|
46
|
+
float sinDelta = 2.0 * deltaSquared / product;
|
|
47
|
+
float cosDelta = 2.0 * t * (e * denominator - deltaSquared) / product;
|
|
48
|
+
|
|
49
|
+
float poleAngle = v_pole * ${r(4.948871220193396)};
|
|
50
|
+
float poleCosDelta = -2.0 * pow(sin(poleAngle * 0.5), 2.0);
|
|
51
|
+
float poleSin = sin(poleAngle);
|
|
52
|
+
float latitudeSin = u_tile.y + sinDelta;
|
|
53
|
+
float latitudeCos = u_tile.z + cosDelta;
|
|
54
|
+
sinDelta += latitudeSin * poleCosDelta + latitudeCos * poleSin;
|
|
55
|
+
cosDelta += latitudeCos * poleCosDelta - latitudeSin * poleSin;
|
|
56
|
+
latitudeCos = u_tile.z + cosDelta;
|
|
57
|
+
|
|
58
|
+
float longitudeCosDelta = -2.0 * pow(sin(longitudeDelta * 0.5), 2.0);
|
|
59
|
+
float heightDelta = u_sun.x * sinDelta + u_sun.y * cosDelta
|
|
60
|
+
+ latitudeCos * (u_sun.y * longitudeCosDelta + u_sun.z * sin(longitudeDelta));
|
|
61
|
+
float twilightStepAngle = 6.0;
|
|
62
|
+
float twilightSteps = u_twilight_steps;
|
|
63
|
+
|
|
64
|
+
// Attenuation for each twilightStepAngle
|
|
65
|
+
float att = u_twilight_attenuation;
|
|
66
|
+
float twilightLevel;
|
|
67
|
+
if (twilightSteps > 0.) {
|
|
68
|
+
// Threshold residuals are computed in float64 on the CPU before upload.
|
|
69
|
+
twilightLevel = 1.0;
|
|
70
|
+
for (int i = 1; i < 15; i++) {
|
|
71
|
+
if (u_thresholds[i] + heightDelta < 0.0) twilightLevel += 1.0;
|
|
72
|
+
}
|
|
73
|
+
twilightLevel = ceil(min(twilightLevel, twilightSteps));
|
|
74
|
+
if (u_thresholds[0] + heightDelta > 0.0) twilightLevel = 0.0;
|
|
75
|
+
} else {
|
|
76
|
+
float altitude = degrees(asin(clamp(u_center_height + heightDelta, -1.0, 1.0)));
|
|
77
|
+
twilightLevel = -altitude / twilightStepAngle;
|
|
78
|
+
}
|
|
79
|
+
float brightness = clamp(pow(clamp(1. - att, 0., 1.), twilightLevel), 0., 1.);
|
|
80
|
+
fragColor = mix(u_night_color, u_daytime_color, brightness) * u_opacity;
|
|
81
|
+
}`,a=e.createShader(e.VERTEX_SHADER);if(!a)throw Error(`failed to create vertex shader`);let o=e.createShader(e.FRAGMENT_SHADER);if(!o)throw e.deleteShader(a),Error(`failed to create fragment shader`);let s=null;try{if(e.shaderSource(a,n),e.compileShader(a),e.shaderSource(o,i),e.compileShader(o),s=e.createProgram(),!s)throw Error(`failed to create program`);if(e.attachShader(s,a),e.attachShader(s,o),e.linkProgram(s),!e.getProgramParameter(s,e.LINK_STATUS))throw Error(e.getProgramInfoLog(s)||`failed to link program`);return s}catch(t){throw s&&e.deleteProgram(s),s=null,t}finally{s&&(e.detachShader(s,a),e.detachShader(s,o)),e.deleteShader(a),e.deleteShader(o)}}#p(e,n,r,i){let a=`${n}:${r}:${i}`,o=this.#l.get(a);if(o)return o;let s=(0,t.createTileMesh)({granularity:n,generateBorders:!1,extendToNorthPole:r,extendToSouthPole:i},`16bit`),c=e.createBuffer(),l=e.createBuffer();if(!c||!l)throw c&&e.deleteBuffer(c),l&&e.deleteBuffer(l),Error(`failed to create tile mesh buffers`);e.bindBuffer(e.ARRAY_BUFFER,c),e.bufferData(e.ARRAY_BUFFER,s.vertices,e.STATIC_DRAW),e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,l),e.bufferData(e.ELEMENT_ARRAY_BUFFER,s.indices,e.STATIC_DRAW);let u={vertices:c,indices:l,count:s.indices.byteLength/2};return this.#l.set(a,u),u}render(e,t){if(!this.#c||!this.#l||!this.#u)return;let i=t.shaderData,o=i.variantName;this.#c.has(o)||this.#c.set(o,this.#f(e,i));let s=this.#c.get(o);if(!s)return;let{lng:c,lat:l}=this.getSubsolarPoint(),u=r(c),d=r(l),f=Math.sin(d),p=Math.cos(d);e.useProgram(s),e.uniform1f(e.getUniformLocation(s,`u_opacity`),this.#t),e.uniform4fv(e.getUniformLocation(s,`u_night_color`),a(this.#n)),e.uniform4fv(e.getUniformLocation(s,`u_daytime_color`),a(this.#r)),e.uniform1f(e.getUniformLocation(s,`u_twilight_steps`),this.#o),e.uniform1f(e.getUniformLocation(s,`u_twilight_attenuation`),this.#s);let m=t.defaultProjectionData.projectionTransition>0,h=this.#u.coveringTiles({tileSize:256});e.disable(e.SCISSOR_TEST),e.disable(e.DEPTH_TEST),e.disable(e.STENCIL_TEST),e.enable(e.CULL_FACE),e.frontFace(e.CCW),e.cullFace(e.BACK),e.enable(e.BLEND),e.blendFunc(e.ONE,e.ONE_MINUS_SRC_ALPHA);for(let i of h){let{x:a,y:o,z:c}=i.canonical,l=2**-c,d=t.getProjectionData({tileID:{canonical:{x:a,y:o,z:c},wrap:i.wrap},applyGlobeMatrix:!0});e.uniformMatrix4fv(e.getUniformLocation(s,`u_projection_matrix`),!1,d.mainMatrix),e.uniformMatrix4fv(e.getUniformLocation(s,`u_projection_fallback_matrix`),!1,d.fallbackMatrix),e.uniform4fv(e.getUniformLocation(s,`u_projection_tile_mercator_coords`),d.tileMercatorCoords),e.uniform4fv(e.getUniformLocation(s,`u_projection_clipping_plane`),d.clippingPlane),e.uniform1f(e.getUniformLocation(s,`u_projection_transition`),d.projectionTransition);let h=Math.exp(Math.PI*(1-2*(o+.5)*l)),g=(h*h-1)/(h*h+1),_=2*h/(h*h+1),v=u-r((a+.5)*l*360-180),y=[f,p*Math.cos(v),p*Math.sin(v)],b=y[0]*g+y[1]*_,x=n.map(e=>b-e);e.uniform4fv(e.getUniformLocation(s,`u_tile`),[h,g,_,l]),e.uniform3fv(e.getUniformLocation(s,`u_sun`),y),e.uniform1f(e.getUniformLocation(s,`u_center_height`),b),e.uniform1fv(e.getUniformLocation(s,`u_thresholds[0]`),x);let S=m?Math.max(Math.floor(128*l),32):1,C=this.#p(e,S,m&&o===0,m&&o===2**c-1);e.bindBuffer(e.ARRAY_BUFFER,C.vertices),e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,C.indices);let w=e.getAttribLocation(s,`a_position`);e.enableVertexAttribArray(w),e.vertexAttribPointer(w,2,e.SHORT,!1,0,0),e.drawElements(e.TRIANGLES,C.count,e.UNSIGNED_SHORT,0)}}};return e.NightLayer=s,e.default=s,e.getSubsolarPoint=o,e})({},maplibregl);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "maplibre-gl-nightlayer",
|
|
3
|
-
"version": "1.0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "A Night Layer for MapLibre GL JS",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -10,15 +10,11 @@
|
|
|
10
10
|
"dist/"
|
|
11
11
|
],
|
|
12
12
|
"scripts": {
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"lint:fix": "biome lint ./src --write",
|
|
19
|
-
"format": "biome format ./src",
|
|
20
|
-
"format:fix": "biome format ./src --write",
|
|
21
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
13
|
+
"build": "tsdown",
|
|
14
|
+
"typecheck": "tsc --noEmit",
|
|
15
|
+
"lint": "oxlint ./src tsdown.config.ts",
|
|
16
|
+
"format": "oxfmt --check ./src tsdown.config.ts",
|
|
17
|
+
"format:fix": "oxfmt --write ./src tsdown.config.ts"
|
|
22
18
|
},
|
|
23
19
|
"keywords": [
|
|
24
20
|
"maplibre",
|
|
@@ -29,13 +25,19 @@
|
|
|
29
25
|
],
|
|
30
26
|
"author": "kikuchan98@gmail.com",
|
|
31
27
|
"homepage": "https://github.com/kikuchan/maplibre-gl-nightlayer#readme",
|
|
28
|
+
"repository": {
|
|
29
|
+
"url": "https://github.com/kikuchan/maplibre-gl-nightlayer"
|
|
30
|
+
},
|
|
32
31
|
"license": "MIT",
|
|
33
32
|
"devDependencies": {
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"typescript": "^
|
|
39
|
-
|
|
40
|
-
|
|
33
|
+
"maplibre-gl": "^6.13.0",
|
|
34
|
+
"oxfmt": "^0.72.0",
|
|
35
|
+
"oxlint": "^1.87.0",
|
|
36
|
+
"tsdown": "^0.23.0",
|
|
37
|
+
"typescript": "^7.0.2"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"maplibre-gl": "^6.13.0"
|
|
41
|
+
},
|
|
42
|
+
"packageManager": "pnpm@12.10.1"
|
|
41
43
|
}
|