maplibre-gl-nightlayer 1.0.0-alpha.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 +61 -0
- package/dist/main.d.ts +35 -0
- package/dist/main.js +58 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# A Night Layer for MapLibre GL JS
|
|
2
|
+
|
|
3
|
+
Adds a simple night shadow (Earth's shadow) on the map.
|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
```sh
|
|
8
|
+
$ npm install --save-dev maplibre-gl-nightlayer
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { NightLayer } from 'maplibre-gl-nightlayer';
|
|
15
|
+
|
|
16
|
+
:
|
|
17
|
+
|
|
18
|
+
map.on('load', () => {
|
|
19
|
+
map.addLayer(new NightLayer());
|
|
20
|
+
});
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## API
|
|
24
|
+
```ts
|
|
25
|
+
type Color = [number, number, number];
|
|
26
|
+
|
|
27
|
+
type Options = {
|
|
28
|
+
opacity?: number;
|
|
29
|
+
date?: Date | null;
|
|
30
|
+
color?: Color;
|
|
31
|
+
twilightSteps?: number;
|
|
32
|
+
twilightStepAngle?: number;
|
|
33
|
+
twilightAttenuation?: number;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
class NightLayer implements CustomLayerInterface {
|
|
37
|
+
constructor(opts?: Options);
|
|
38
|
+
|
|
39
|
+
getSubsolarPoint(date: Date): {
|
|
40
|
+
subsolarLng: number;
|
|
41
|
+
subsolarLat: number;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
getDate(): Date | null;
|
|
45
|
+
setDate(date: Date | null): void;
|
|
46
|
+
|
|
47
|
+
getColor(): Color;
|
|
48
|
+
setColor(v: Color): void;
|
|
49
|
+
|
|
50
|
+
getTwilightSteps(): number;
|
|
51
|
+
setTwilightSteps(v: number): void;
|
|
52
|
+
|
|
53
|
+
getTwilightStepAngle(): number;
|
|
54
|
+
setTwilightStepAngle(v: number): void;
|
|
55
|
+
|
|
56
|
+
getTwilightAttenuation(): number;
|
|
57
|
+
setTwilightAttenuation(v: number): void;
|
|
58
|
+
|
|
59
|
+
:
|
|
60
|
+
}
|
|
61
|
+
```
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { CustomLayerInterface, Map as MaplibreMap } from 'maplibre-gl';
|
|
2
|
+
import type { mat4 } from 'gl-matrix';
|
|
3
|
+
type Color = [number, number, number];
|
|
4
|
+
type Options = {
|
|
5
|
+
opacity?: number;
|
|
6
|
+
date?: Date | null;
|
|
7
|
+
color?: Color;
|
|
8
|
+
twilightSteps?: number;
|
|
9
|
+
twilightStepAngle?: number;
|
|
10
|
+
twilightAttenuation?: number;
|
|
11
|
+
};
|
|
12
|
+
export declare class NightLayer implements CustomLayerInterface {
|
|
13
|
+
#private;
|
|
14
|
+
id: string;
|
|
15
|
+
type: "custom";
|
|
16
|
+
renderingMode: "2d";
|
|
17
|
+
constructor(opts?: Options);
|
|
18
|
+
getSubsolarPoint(date: Date): {
|
|
19
|
+
subsolarLng: number;
|
|
20
|
+
subsolarLat: number;
|
|
21
|
+
};
|
|
22
|
+
getDate(): Date | null;
|
|
23
|
+
setDate(date: Date | null): void;
|
|
24
|
+
getColor(): Color;
|
|
25
|
+
setColor(v: Color): void;
|
|
26
|
+
getTwilightSteps(): number;
|
|
27
|
+
setTwilightSteps(v: number): void;
|
|
28
|
+
getTwilightStepAngle(): number;
|
|
29
|
+
setTwilightStepAngle(v: number): void;
|
|
30
|
+
getTwilightAttenuation(): number;
|
|
31
|
+
setTwilightAttenuation(v: number): void;
|
|
32
|
+
onAdd(map: MaplibreMap, gl: WebGLRenderingContext): void;
|
|
33
|
+
render(gl: WebGLRenderingContext, matrix: mat4): void;
|
|
34
|
+
}
|
|
35
|
+
export default NightLayer;
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
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";#e;#h;#i;#r;#o;#a;#t;#n;#s;constructor(t={}){this.#e=t.date??null,this.#h=t.opacity??.5,this.#i=t.color??[0,0,0],this.#r=t.twilightSteps??0,this.#o=t.twilightStepAngle??6,this.#a=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.#e}setDate(t){this.#e=t}getColor(){return this.#i}setColor(t){this.#i=t}getTwilightSteps(){return this.#r}setTwilightSteps(t){this.#r=t}getTwilightStepAngle(){return this.#o}setTwilightStepAngle(t){this.#o=t}getTwilightAttenuation(){return this.#a}setTwilightAttenuation(t){this.#a=t}onAdd(t,e){const a=`
|
|
2
|
+
precision highp float;
|
|
3
|
+
|
|
4
|
+
attribute vec2 a_position;
|
|
5
|
+
|
|
6
|
+
uniform mat4 u_matrix;
|
|
7
|
+
|
|
8
|
+
varying vec2 v_position;
|
|
9
|
+
|
|
10
|
+
void main() {
|
|
11
|
+
vec4 tmp = u_matrix * vec4(a_position, 0.0, 1.0);
|
|
12
|
+
gl_Position = tmp;
|
|
13
|
+
v_position = a_position;
|
|
14
|
+
}`,n=`
|
|
15
|
+
precision highp float;
|
|
16
|
+
|
|
17
|
+
uniform vec2 u_subsolar;
|
|
18
|
+
uniform float u_opacity;
|
|
19
|
+
uniform float u_twilight_step_angle;
|
|
20
|
+
uniform float u_twilight_steps;
|
|
21
|
+
uniform float u_twilight_attenuation;
|
|
22
|
+
uniform vec3 u_color;
|
|
23
|
+
|
|
24
|
+
varying vec2 v_position;
|
|
25
|
+
|
|
26
|
+
vec2 mercatorToLngLat(vec2 mercator) {
|
|
27
|
+
// 0 <= x <= 1, 0 <= y <= 1
|
|
28
|
+
float x = mercator.x;
|
|
29
|
+
float y = mercator.y;
|
|
30
|
+
|
|
31
|
+
float lng = x * 360.0 - 180.0;
|
|
32
|
+
float lat = degrees(2.0 * atan(exp(${Math.PI} * (1.0 - 2.0 * y))) - ${Math.PI/2});
|
|
33
|
+
|
|
34
|
+
return vec2(lng, lat);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
void main() {
|
|
38
|
+
vec2 lnglat = mercatorToLngLat(v_position);
|
|
39
|
+
|
|
40
|
+
vec2 observer = radians(lnglat);
|
|
41
|
+
vec2 subsolar = radians(u_subsolar);
|
|
42
|
+
float A = sin(observer.y) * sin(subsolar.y);
|
|
43
|
+
float B = cos(observer.y) * cos(subsolar.y) * cos(subsolar.x - observer.x);
|
|
44
|
+
float altitude = degrees(asin(A + B));
|
|
45
|
+
float twilightStepAngle = u_twilight_step_angle;
|
|
46
|
+
float twilightSteps = u_twilight_steps;
|
|
47
|
+
|
|
48
|
+
// Attenuation for each twilightStepAngle
|
|
49
|
+
float att = u_twilight_attenuation;
|
|
50
|
+
float twilightLevel = -altitude / twilightStepAngle;
|
|
51
|
+
if (twilightSteps > 0.) {
|
|
52
|
+
twilightLevel = ceil(clamp(twilightLevel, 0., twilightSteps));
|
|
53
|
+
}
|
|
54
|
+
float brightness = clamp(pow(att, twilightLevel), 0., 1.);
|
|
55
|
+
float darkness = (1. - brightness);
|
|
56
|
+
|
|
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.#s=e.createBuffer(),this.#n=t}render(t,e){if(!this.#s||!this.#t||!this.#n)return;const{subsolarLng:a,subsolarLat:n}=this.getSubsolarPoint(this.#e||new Date),[r,i]=this.#n.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.#h),t.uniform3fv(t.getUniformLocation(this.#t,"u_color"),this.#i),t.uniform1f(t.getUniformLocation(this.#t,"u_twilight_steps"),this.#r),t.uniform1f(t.getUniformLocation(this.#t,"u_twilight_step_angle"),this.#o),t.uniform1f(t.getUniformLocation(this.#t,"u_twilight_attenuation"),this.#a),t.bindBuffer(t.ARRAY_BUFFER,this.#s),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;
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "maplibre-gl-nightlayer",
|
|
3
|
+
"version": "1.0.0-alpha.0",
|
|
4
|
+
"description": "A Night Layer for MapLibre GL JS",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/main.js",
|
|
7
|
+
"module": "dist/main.js",
|
|
8
|
+
"types": "dist/main.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist/"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"dev": "vite playground/",
|
|
14
|
+
"build": "pnpm run build:esm && pnpm run build:types",
|
|
15
|
+
"build:esm": "esbuild src/main.ts --minify --outfile=dist/main.js",
|
|
16
|
+
"build:types": "tsc --declaration --emitDeclarationOnly --declarationDir './dist'",
|
|
17
|
+
"lint": "biome lint ./src",
|
|
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"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"maplibre",
|
|
25
|
+
"daynight",
|
|
26
|
+
"earth",
|
|
27
|
+
"shadow",
|
|
28
|
+
"terminator"
|
|
29
|
+
],
|
|
30
|
+
"author": "kikuchan98@gmail.com",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@biomejs/biome": "^1.9.4",
|
|
34
|
+
"esbuild": "^0.24.0",
|
|
35
|
+
"gl-matrix": "^3.4.3",
|
|
36
|
+
"maplibre-gl": "^4.7.1",
|
|
37
|
+
"typescript": "^5.6.3",
|
|
38
|
+
"vite": "^5.4.11"
|
|
39
|
+
}
|
|
40
|
+
}
|