maplibre-gl-nightlayer 1.0.0-alpha.1 → 1.0.0-alpha.2
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 +4 -1
- package/dist/main.d.ts +63 -1
- package/dist/main.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,8 +25,8 @@ map.on('load', () => {
|
|
|
25
25
|
type Color = [number, number, number];
|
|
26
26
|
|
|
27
27
|
type Options = {
|
|
28
|
-
opacity?: number;
|
|
29
28
|
date?: Date | null;
|
|
29
|
+
opacity?: number;
|
|
30
30
|
color?: Color;
|
|
31
31
|
twilightSteps?: number;
|
|
32
32
|
twilightStepAngle?: number;
|
|
@@ -44,6 +44,9 @@ class NightLayer implements CustomLayerInterface {
|
|
|
44
44
|
getDate(): Date | null;
|
|
45
45
|
setDate(date: Date | null): void;
|
|
46
46
|
|
|
47
|
+
getOpacity(): number;
|
|
48
|
+
setOpacity(v: number): void;
|
|
49
|
+
|
|
47
50
|
getColor(): Color;
|
|
48
51
|
setColor(v: Color): void;
|
|
49
52
|
|
package/dist/main.d.ts
CHANGED
|
@@ -2,34 +2,96 @@ import type { CustomLayerInterface, Map as MaplibreMap } from 'maplibre-gl';
|
|
|
2
2
|
import type { mat4 } from 'gl-matrix';
|
|
3
3
|
type Color = [number, number, number];
|
|
4
4
|
type Options = {
|
|
5
|
-
opacity?: number;
|
|
6
5
|
date?: Date | null;
|
|
6
|
+
opacity?: number;
|
|
7
7
|
color?: Color;
|
|
8
8
|
twilightSteps?: number;
|
|
9
9
|
twilightStepAngle?: number;
|
|
10
10
|
twilightAttenuation?: number;
|
|
11
11
|
};
|
|
12
|
+
/**
|
|
13
|
+
* A custom Maplibre GL JS layer that renders the night side of the Earth.
|
|
14
|
+
*/
|
|
12
15
|
export declare class NightLayer implements CustomLayerInterface {
|
|
13
16
|
#private;
|
|
14
17
|
id: string;
|
|
15
18
|
type: "custom";
|
|
16
19
|
renderingMode: "2d";
|
|
20
|
+
/**
|
|
21
|
+
* Create a new NightLayer instance. Intended to be passed to `map.addLayer`.
|
|
22
|
+
* @param opts Options.
|
|
23
|
+
*/
|
|
17
24
|
constructor(opts?: Options);
|
|
25
|
+
/**
|
|
26
|
+
* Get the subsolar point (longitude and latitude) at the given date.
|
|
27
|
+
* @param date Date. If null, the current date is used.
|
|
28
|
+
*/
|
|
18
29
|
getSubsolarPoint(date: Date): {
|
|
19
30
|
subsolarLng: number;
|
|
20
31
|
subsolarLat: number;
|
|
21
32
|
};
|
|
33
|
+
/**
|
|
34
|
+
* Get the date of the night layer.
|
|
35
|
+
* @returns Date. If null, the current date is used.
|
|
36
|
+
*/
|
|
22
37
|
getDate(): Date | null;
|
|
38
|
+
/**
|
|
39
|
+
* Set the date of the night layer.
|
|
40
|
+
* @param date Date. If null, the current date is used.
|
|
41
|
+
*/
|
|
23
42
|
setDate(date: Date | null): void;
|
|
43
|
+
/**
|
|
44
|
+
* Get the opacity of the night layer.
|
|
45
|
+
* @returns Opacity. 0.0 means fully transparent, 1.0 means fully opaque.
|
|
46
|
+
*/
|
|
47
|
+
getOpacity(): number;
|
|
48
|
+
/**
|
|
49
|
+
* Set the opacity of the night layer.
|
|
50
|
+
* @param v Opacity. 0.0 means fully transparent, 1.0 means fully opaque.
|
|
51
|
+
*/
|
|
52
|
+
setOpacity(v: number): void;
|
|
53
|
+
/**
|
|
54
|
+
* Get the color of the night layer.
|
|
55
|
+
* @returns RGB color. Each value is in the range [0, 255].
|
|
56
|
+
*/
|
|
24
57
|
getColor(): Color;
|
|
58
|
+
/**
|
|
59
|
+
* Set the color of the night layer.
|
|
60
|
+
* @param v RGB color. Each value should be in the range [0, 255].
|
|
61
|
+
*/
|
|
25
62
|
setColor(v: Color): void;
|
|
63
|
+
/**
|
|
64
|
+
* Get the number of twilight steps.
|
|
65
|
+
* @returns 0 means no steps (gradation), 1 means one step (day/night), etc.
|
|
66
|
+
*/
|
|
26
67
|
getTwilightSteps(): number;
|
|
68
|
+
/**
|
|
69
|
+
* Set the number of twilight steps.
|
|
70
|
+
* @param v 0 means no steps (gradation), 1 means one step (day/night), etc.
|
|
71
|
+
*/
|
|
27
72
|
setTwilightSteps(v: number): void;
|
|
73
|
+
/**
|
|
74
|
+
* Get the angle for each twilight step.
|
|
75
|
+
* @returns angle in degrees.
|
|
76
|
+
*/
|
|
28
77
|
getTwilightStepAngle(): number;
|
|
78
|
+
/**
|
|
79
|
+
* Set the angle for each twilight step.
|
|
80
|
+
* @param v angle in degrees.
|
|
81
|
+
*/
|
|
29
82
|
setTwilightStepAngle(v: number): void;
|
|
83
|
+
/**
|
|
84
|
+
* Get the attenuation factor for each twilight step.
|
|
85
|
+
* @returns 0.0 means no attenuation, 1.0 means full attenuation.
|
|
86
|
+
*/
|
|
30
87
|
getTwilightAttenuation(): number;
|
|
88
|
+
/**
|
|
89
|
+
* Set the attenuation factor for each twilight step.
|
|
90
|
+
* @param v 0.0 means no attenuation, 1.0 means full attenuation.
|
|
91
|
+
*/
|
|
31
92
|
setTwilightAttenuation(v: number): void;
|
|
32
93
|
onAdd(map: MaplibreMap, gl: WebGLRenderingContext): void;
|
|
94
|
+
onRemove(): void;
|
|
33
95
|
render(gl: WebGLRenderingContext, matrix: mat4): void;
|
|
34
96
|
}
|
|
35
97
|
export default NightLayer;
|
package/dist/main.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";import{MercatorCoordinate as l}from"maplibre-gl";function
|
|
1
|
+
"use strict";import{MercatorCoordinate as l}from"maplibre-gl";function m(h){return h*Math.PI/180}function g(h){return h/Math.PI*180}export class NightLayer{id="nightlayer";type="custom";renderingMode="2d";#i;#r;#o;#a;#n;#s;#t;#e;#h;constructor(t={}){this.#i=t.date??null,this.#r=t.opacity??.5,this.#o=t.color??[0,0,0],this.#a=t.twilightSteps??0,this.#n=t.twilightStepAngle??6,this.#s=t.twilightAttenuation??.5}getSubsolarPoint(t){const e=(t.getTime()-Date.UTC(t.getUTCFullYear(),0,0))/864e5,a=2*Math.PI/365.24,n=m(23.44),r=.0167,i=(e+9)*a,o=i+2*r*Math.sin((e-3)*a),s=(i-Math.atan2(Math.sin(o),Math.cos(o)*Math.cos(n)))/Math.PI,u=720*(s-Math.trunc(s+.5)),c=-15*(t.getUTCHours()+t.getUTCMinutes()/60+t.getUTCSeconds()/3600-12+u/60),f=g(Math.asin(Math.sin(-n)*Math.cos(o)));return{subsolarLng:c,subsolarLat:f}}getDate(){return this.#i}setDate(t){this.#i=t,this.#e?.triggerRepaint()}getOpacity(){return this.#r}setOpacity(t){this.#r=t,this.#e?.triggerRepaint()}getColor(){return this.#o}setColor(t){this.#o=t,this.#e?.triggerRepaint()}getTwilightSteps(){return this.#a}setTwilightSteps(t){this.#a=t,this.#e?.triggerRepaint()}getTwilightStepAngle(){return this.#n}setTwilightStepAngle(t){this.#n=t,this.#e?.triggerRepaint()}getTwilightAttenuation(){return this.#s}setTwilightAttenuation(t){this.#s=t,this.#e?.triggerRepaint()}onAdd(t,e){const a=`
|
|
2
2
|
precision highp float;
|
|
3
3
|
|
|
4
4
|
attribute vec2 a_position;
|
|
@@ -55,4 +55,4 @@
|
|
|
55
55
|
float darkness = (1. - brightness);
|
|
56
56
|
|
|
57
57
|
gl_FragColor = vec4(u_color / 255., 1.0) * darkness * u_opacity;
|
|
58
|
-
}`,r=e.createShader(e.VERTEX_SHADER);if(!r)throw new Error("failed to create vertex shader");e.shaderSource(r,a),e.compileShader(r);const i=e.createShader(e.FRAGMENT_SHADER);if(!i)throw new Error("failed to create fragment shader");if(e.shaderSource(i,n),e.compileShader(i),this.#t=e.createProgram(),!this.#t)throw new Error("failed to create program");e.attachShader(this.#t,r),e.attachShader(this.#t,i),e.linkProgram(this.#t),this.#
|
|
58
|
+
}`,r=e.createShader(e.VERTEX_SHADER);if(!r)throw new Error("failed to create vertex shader");e.shaderSource(r,a),e.compileShader(r);const i=e.createShader(e.FRAGMENT_SHADER);if(!i)throw new Error("failed to create fragment shader");if(e.shaderSource(i,n),e.compileShader(i),this.#t=e.createProgram(),!this.#t)throw new Error("failed to create program");e.attachShader(this.#t,r),e.attachShader(this.#t,i),e.linkProgram(this.#t),this.#h=e.createBuffer(),this.#e=t}onRemove(){this.#e=void 0,this.#h=void 0,this.#t=void 0}render(t,e){if(!this.#h||!this.#t||!this.#e)return;const{subsolarLng:a,subsolarLat:n}=this.getSubsolarPoint(this.#i||new Date),[r,i]=this.#e.getBounds().toArray(),o=Math.floor(l.fromLngLat(r).x),s=Math.ceil(l.fromLngLat(i).x);t.useProgram(this.#t),t.uniformMatrix4fv(t.getUniformLocation(this.#t,"u_matrix"),!1,e),t.uniform2fv(t.getUniformLocation(this.#t,"u_subsolar"),[a,n]),t.uniform1f(t.getUniformLocation(this.#t,"u_opacity"),this.#r),t.uniform3fv(t.getUniformLocation(this.#t,"u_color"),this.#o),t.uniform1f(t.getUniformLocation(this.#t,"u_twilight_steps"),this.#a),t.uniform1f(t.getUniformLocation(this.#t,"u_twilight_step_angle"),this.#n),t.uniform1f(t.getUniformLocation(this.#t,"u_twilight_attenuation"),this.#s),t.bindBuffer(t.ARRAY_BUFFER,this.#h),t.bufferData(t.ARRAY_BUFFER,new Float32Array([o,0,s,0,o,1,o,1,s,0,s,1]),t.DYNAMIC_DRAW);const u=t.getAttribLocation(this.#t,"a_position");t.enableVertexAttribArray(u),t.vertexAttribPointer(u,2,t.FLOAT,!1,0,0),t.drawArrays(t.TRIANGLES,0,6)}}export default NightLayer;
|