maplibre-gl-nightlayer 1.0.0-alpha.5 → 1.0.0-alpha.7
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/dist/main.d.ts +4 -3
- package/dist/main.js +21 -11
- package/package.json +4 -4
package/dist/main.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CustomLayerInterface, Map as MaplibreMap } from 'maplibre-gl';
|
|
1
|
+
import type { CustomLayerInterface, CustomRenderMethodInput, Map as MaplibreMap } from 'maplibre-gl';
|
|
2
2
|
import type { mat4 } from 'gl-matrix';
|
|
3
3
|
type Color = [number, number, number];
|
|
4
4
|
/**
|
|
@@ -116,8 +116,9 @@ export declare class NightLayer implements CustomLayerInterface {
|
|
|
116
116
|
* @param attenuation 0.0 means no attenuation, 1.0 means full attenuation.
|
|
117
117
|
*/
|
|
118
118
|
setTwilightAttenuation(attenuation: number): void;
|
|
119
|
-
onAdd(map: MaplibreMap, gl: WebGLRenderingContext): void;
|
|
119
|
+
onAdd(map: MaplibreMap, gl: WebGLRenderingContext | WebGL2RenderingContext): void;
|
|
120
120
|
onRemove(): void;
|
|
121
|
-
render(gl: WebGLRenderingContext, matrix: mat4): void;
|
|
121
|
+
render(gl: WebGLRenderingContext | WebGL2RenderingContext, matrix: mat4, options: CustomRenderMethodInput): void;
|
|
122
|
+
render(gl: WebGLRenderingContext | WebGL2RenderingContext, options: CustomRenderMethodInput): void;
|
|
122
123
|
}
|
|
123
124
|
export default NightLayer;
|
package/dist/main.js
CHANGED
|
@@ -1,19 +1,30 @@
|
|
|
1
|
-
"use strict";import{MercatorCoordinate as
|
|
1
|
+
"use strict";import{MercatorCoordinate as f,createTileMesh as p}from"maplibre-gl";function d(h){return h*Math.PI/180}function g(h){return h/Math.PI*180}export class NightLayer{id="nightlayer";type="custom";renderingMode="2d";#n;#o;#a;#s;#u;#h;#e;#t;#i;#r;#c;#f;constructor(t={}){this.#n=t.date??null,this.#o=t.opacity??.5,this.#a=t.color??[0,0,0],this.#s=t.twilightSteps??0,this.#u=t.twilightStepAngle??6,this.#h=t.twilightAttenuation??.5}getSubsolarPoint(t){const r=(t.getTime()-Date.UTC(t.getUTCFullYear(),0,0))/864e5,s=2*Math.PI/365.24,o=d(23.44),a=.0167,e=(r+9)*s,i=e+2*a*Math.sin((r-3)*s),n=(e-Math.atan2(Math.sin(i),Math.cos(i)*Math.cos(o)))/Math.PI,c=720*(n-Math.trunc(n+.5)),l=-15*(t.getUTCHours()+t.getUTCMinutes()/60+t.getUTCSeconds()/3600-12+c/60),m=g(Math.asin(Math.sin(-o)*Math.cos(i)));return{subsolarLng:l,subsolarLat:m}}getDate(){return this.#n}setDate(t){this.#n=t,this.#t?.triggerRepaint()}getOpacity(){return this.#o}setOpacity(t){this.#o=t,this.#t?.triggerRepaint()}getColor(){return this.#a}setColor(t){this.#a=t,this.#t?.triggerRepaint()}getTwilightSteps(){return this.#s}setTwilightSteps(t){this.#s=t,this.#t?.triggerRepaint()}getTwilightStepAngle(){return this.#u}setTwilightStepAngle(t){this.#u=t,this.#t?.triggerRepaint()}getTwilightAttenuation(){return this.#h}setTwilightAttenuation(t){this.#h=t,this.#t?.triggerRepaint()}onAdd(t,r){this.#e=new Map,this.#i=r.createBuffer(),this.#r=r.createBuffer(),this.#t=t}onRemove(){this.#e=void 0,this.#t=void 0,this.#i=void 0,this.#r=void 0}#l(t,r){const o=`#version 300 es
|
|
2
2
|
precision highp float;
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
in vec2 a_position;
|
|
5
|
+
out vec2 v_position;
|
|
5
6
|
|
|
7
|
+
${!r.vertexShaderPrelude?`
|
|
6
8
|
uniform mat4 u_matrix;
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
vec4 projectTile(vec2 pos) {
|
|
11
|
+
return u_matrix * vec4(pos, 0., 1.);
|
|
12
|
+
}
|
|
13
|
+
`:`
|
|
14
|
+
${r.vertexShaderPrelude}
|
|
15
|
+
${r.define}
|
|
16
|
+
`}
|
|
9
17
|
|
|
10
18
|
void main() {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
19
|
+
gl_Position = projectTile(a_position);
|
|
20
|
+
v_position = a_position;
|
|
21
|
+
}
|
|
22
|
+
`,a=`#version 300 es
|
|
15
23
|
precision highp float;
|
|
16
24
|
|
|
25
|
+
in vec2 v_position;
|
|
26
|
+
out vec4 fragColor;
|
|
27
|
+
|
|
17
28
|
uniform vec2 u_subsolar;
|
|
18
29
|
uniform float u_opacity;
|
|
19
30
|
uniform float u_twilight_step_angle;
|
|
@@ -21,8 +32,6 @@
|
|
|
21
32
|
uniform float u_twilight_attenuation;
|
|
22
33
|
uniform vec3 u_color;
|
|
23
34
|
|
|
24
|
-
varying vec2 v_position;
|
|
25
|
-
|
|
26
35
|
vec2 mercatorToLngLat(vec2 mercator) {
|
|
27
36
|
// 0 <= x <= 1, 0 <= y <= 1
|
|
28
37
|
float x = mercator.x;
|
|
@@ -54,5 +63,6 @@
|
|
|
54
63
|
float brightness = clamp(pow(clamp(1. - att, 0., 1.), twilightLevel), 0., 1.);
|
|
55
64
|
float darkness = (1. - brightness);
|
|
56
65
|
|
|
57
|
-
|
|
58
|
-
|
|
66
|
+
fragColor = vec4(v_position, 0.0, 1.0);
|
|
67
|
+
fragColor = vec4(u_color / 255., 1.0) * darkness * u_opacity;
|
|
68
|
+
}`,e=t.createShader(t.VERTEX_SHADER);if(!e)throw new Error("failed to create vertex shader");t.shaderSource(e,o),t.compileShader(e);const i=t.createShader(t.FRAGMENT_SHADER);if(!i)throw new Error("failed to create fragment shader");t.shaderSource(i,a),t.compileShader(i);const n=t.createProgram();if(!n)throw new Error("failed to create program");return t.attachShader(n,e),t.attachShader(n,i),t.linkProgram(n),n}#m(t){if(!this.#t||!this.#i||!this.#r)return;const[r,s]=this.#t.getBounds().toArray(),o=Math.floor(f.fromLngLat(r).x),a=Math.ceil(f.fromLngLat(s).x),e=this.#t.style?.projection?.name==="globe"?"globe":[o,a].join(":");if(e===this.#f)return;let i;e==="globe"?(i=p({granularity:100,generateBorders:!1,extendToNorthPole:!0,extendToSouthPole:!0},"16bit"),i.vertices=new Float32Array(new Int16Array(i.vertices)).map(n=>n/8192).buffer):i={vertices:new Float32Array([o,0,a,0,o,1,o,1,a,0,a,1]).buffer,indices:new Uint16Array([0,1,2,3,4,5]).buffer,uses32bitIndices:!1},t.bindBuffer(t.ARRAY_BUFFER,this.#i),t.bufferData(t.ARRAY_BUFFER,i.vertices,t.DYNAMIC_DRAW),t.bindBuffer(t.ARRAY_BUFFER,null),t.bindBuffer(t.ELEMENT_ARRAY_BUFFER,this.#r),t.bufferData(t.ELEMENT_ARRAY_BUFFER,i.indices,t.DYNAMIC_DRAW),t.bindBuffer(t.ELEMENT_ARRAY_BUFFER,null),this.#c=i.indices.byteLength/2,this.#f=e}render(t,r,s){if(s||(s=r),!this.#e||!this.#i||!this.#r||!this.#t)return;const o=s.shaderData||{},a=o.variantName||"default";this.#e.has(a)||this.#e.set(a,this.#l(t,o));const e=this.#e.get(a);if(!e)return;const{subsolarLng:i,subsolarLat:n}=this.getSubsolarPoint(this.#n||new Date);if(t.useProgram(e),t.uniform2fv(t.getUniformLocation(e,"u_subsolar"),[i,n]),t.uniform1f(t.getUniformLocation(e,"u_opacity"),this.#o),t.uniform3fv(t.getUniformLocation(e,"u_color"),this.#a),t.uniform1f(t.getUniformLocation(e,"u_twilight_steps"),this.#s),t.uniform1f(t.getUniformLocation(e,"u_twilight_step_angle"),this.#u),t.uniform1f(t.getUniformLocation(e,"u_twilight_attenuation"),this.#h),"getProjectionDataForCustomLayer"in this.#t.transform){const u=this.#t.transform.getProjectionDataForCustomLayer(!0);t.uniformMatrix4fv(t.getUniformLocation(e,"u_projection_fallback_matrix"),!1,u.fallbackMatrix),t.uniformMatrix4fv(t.getUniformLocation(e,"u_projection_matrix"),!1,u.mainMatrix),t.uniform4f(t.getUniformLocation(e,"u_projection_tile_mercator_coords"),...u.tileMercatorCoords),t.uniform4f(t.getUniformLocation(e,"u_projection_clipping_plane"),...u.clippingPlane),t.uniform1f(t.getUniformLocation(e,"u_projection_transition"),u.projectionTransition)}else t.uniformMatrix4fv(t.getUniformLocation(e,"u_matrix"),!1,r);this.#m(t),t.bindBuffer(t.ARRAY_BUFFER,this.#i),t.bindBuffer(t.ELEMENT_ARRAY_BUFFER,this.#r);const c=t.getAttribLocation(e,"a_position");t.enableVertexAttribArray(c),t.vertexAttribPointer(c,2,t.FLOAT,!1,0,0),t.drawElements(t.TRIANGLES,this.#c,t.UNSIGNED_SHORT,0)}}export default NightLayer;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "maplibre-gl-nightlayer",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.7",
|
|
4
4
|
"description": "A Night Layer for MapLibre GL JS",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"@biomejs/biome": "^1.9.4",
|
|
35
35
|
"esbuild": "^0.24.0",
|
|
36
36
|
"gl-matrix": "^3.4.3",
|
|
37
|
-
"maplibre-gl": "
|
|
38
|
-
"typescript": "^5.
|
|
39
|
-
"vite": "^
|
|
37
|
+
"maplibre-gl": "5.0.0-pre.9",
|
|
38
|
+
"typescript": "^5.7.2",
|
|
39
|
+
"vite": "^6.0.3"
|
|
40
40
|
}
|
|
41
41
|
}
|