maplibre-gl-nightlayer 1.0.0-alpha.8 → 1.0.0-alpha.9

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
@@ -20,9 +20,9 @@ map.on('load', () => {
20
20
  // These are the default values
21
21
  date: null,
22
22
  opacity: 0.5,
23
- color: [0, 0, 0],
23
+ color: [0, 0, 0, 255],
24
+ daytimeColor: [0, 0, 0, 0], // transparent
24
25
  twilightSteps: 0,
25
- twilightStepAngle: 6,
26
26
  twilightAttenuation: 0.5,
27
27
  }));
28
28
  });
@@ -30,14 +30,16 @@ map.on('load', () => {
30
30
 
31
31
  ## API
32
32
  ```ts
33
- type Color = [number, number, number];
33
+ type Color3 = [number, number, number];
34
+ type Color4 = [number, number, number, number];
35
+ type Color = Color3 | Color4;
34
36
 
35
37
  type Options = {
36
38
  date?: Date | null;
37
39
  opacity?: number;
38
40
  color?: Color;
41
+ daytimeColor?: Color;
39
42
  twilightSteps?: number;
40
- twilightStepAngle?: number;
41
43
  twilightAttenuation?: number;
42
44
  };
43
45
 
@@ -55,15 +57,15 @@ export class NightLayer implements CustomLayerInterface {
55
57
  getOpacity(): number;
56
58
  setOpacity(opacity: number): void;
57
59
 
58
- getColor(): Color;
60
+ getColor(): Color4;
59
61
  setColor(color: Color): void;
60
62
 
63
+ getDaytimeColor(): Color4;
64
+ setDaytimeColor(color: Color);
65
+
61
66
  getTwilightSteps(): number;
62
67
  setTwilightSteps(steps: number): void;
63
68
 
64
- getTwilightStepAngle(): number;
65
- setTwilightStepAngle(angle: number): void;
66
-
67
69
  getTwilightAttenuation(): number;
68
70
  setTwilightAttenuation(attenuation: number): void;
69
71
 
package/dist/main.d.ts CHANGED
@@ -9,7 +9,9 @@ export declare function getSubsolarPoint(date?: Date): {
9
9
  lng: number;
10
10
  lat: number;
11
11
  };
12
- type Color = [number, number, number];
12
+ type Color3 = [number, number, number];
13
+ type Color4 = [number, number, number, number];
14
+ type Color = Color3 | Color4;
13
15
  /**
14
16
  * Options for the NightLayer.
15
17
  */
@@ -20,24 +22,28 @@ type Options = {
20
22
  */
21
23
  date?: Date | null;
22
24
  /**
23
- * Opacity of the shadow.
25
+ * Opacity of the layer.
24
26
  * 0.0 means fully transparent, 1.0 means fully opaque
27
+ * default: 0.5
25
28
  */
26
29
  opacity?: number;
27
30
  /**
28
- * Color of the shadow.
31
+ * Color of the shadow: [r, g, b, a]
29
32
  * Each value should be in the range [0, 255].
33
+ * default: [0, 0, 0, 255]
30
34
  */
31
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;
32
42
  /**
33
43
  * Number of twilight steps.
34
44
  * 0 means no steps (gradation), 1 means one step (day/night), etc.
35
45
  */
36
46
  twilightSteps?: number;
37
- /**
38
- * Angle for each twilight step in degrees.
39
- */
40
- twilightStepAngle?: number;
41
47
  /**
42
48
  * Attenuation factor for each twilight step.
43
49
  * 0.0 means no attenuation, 1.0 means full attenuation
@@ -89,12 +95,22 @@ export declare class NightLayer implements CustomLayerInterface {
89
95
  * Get the color of the shadow.
90
96
  * @returns The color. Each value is in the range [0, 255].
91
97
  */
92
- getColor(): Color;
98
+ getColor(): Color4;
93
99
  /**
94
100
  * Set the color of the shadow.
95
101
  * @param color Each value should be in the range [0, 255].
96
102
  */
97
103
  setColor(color: Color): void;
104
+ /**
105
+ * Get the color of the daytime.
106
+ * @returns The color [r, g, b, a]. Each value is in the range [0, 255].
107
+ */
108
+ getDaytimeColor(): Color4;
109
+ /**
110
+ * Set the color of the daytime.
111
+ * @param color Each value should be in the range [0, 255].
112
+ */
113
+ setDaytimeColor(color: Color): void;
98
114
  /**
99
115
  * Get the number of twilight steps.
100
116
  * @returns The steps. 0 means no steps (gradation), 1 means one step (day/night), etc.
@@ -105,16 +121,6 @@ export declare class NightLayer implements CustomLayerInterface {
105
121
  * @param steps 0 means no steps (gradation), 1 means one step (day/night), etc.
106
122
  */
107
123
  setTwilightSteps(steps: number): void;
108
- /**
109
- * Get the angle for each twilight step.
110
- * @returns The angle in degrees.
111
- */
112
- getTwilightStepAngle(): number;
113
- /**
114
- * Set the angle for each twilight step.
115
- * @param angle in degrees.
116
- */
117
- setTwilightStepAngle(angle: number): void;
118
124
  /**
119
125
  * Get the attenuation factor for each twilight step.
120
126
  * @returns The attenuation. 0.0 means no attenuation, 1.0 means full attenuation.
package/dist/main.js CHANGED
@@ -1,18 +1,18 @@
1
- "use strict";import{MercatorCoordinate as f,createTileMesh as m}from"maplibre-gl";function p(o){return o*Math.PI/180}function d(o){return o/Math.PI*180}export function getSubsolarPoint(o){o=o||new Date;const t=(o.getTime()-Date.UTC(o.getUTCFullYear(),0,0))/864e5,r=2*Math.PI/365.24,u=p(23.44),s=.0167,n=(t+9)*r,e=n+2*s*Math.sin((t-3)*r),i=(n-Math.atan2(Math.sin(e),Math.cos(e)*Math.cos(u)))/Math.PI,a=720*(i-Math.trunc(i+.5)),h=-15*(o.getUTCHours()+o.getUTCMinutes()/60+o.getUTCSeconds()/3600-12+a/60),l=d(Math.asin(Math.sin(-u)*Math.cos(e)));return{lng:h,lat:l}}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(){return getSubsolarPoint(this.#n||new Date)}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 s=`#version 300 es
1
+ "use strict";import{MercatorCoordinate as f,createTileMesh as d}from"maplibre-gl";function p(a){return a*Math.PI/180}function g(a){return a/Math.PI*180}function l(a){const[t,i,u,o]=a.map(n=>n/255);return[t*o,i*o,u*o,o]}export function getSubsolarPoint(a){a=a||new Date;const t=(a.getTime()-Date.UTC(a.getUTCFullYear(),0,0))/864e5,i=2*Math.PI/365.24,u=p(23.44),o=.0167,n=(t+9)*i,e=n+2*o*Math.sin((t-3)*i),r=(n-Math.atan2(Math.sin(e),Math.cos(e)*Math.cos(u)))/Math.PI,s=720*(r-Math.trunc(r+.5)),c=-15*(a.getUTCHours()+a.getUTCMinutes()/60+a.getUTCSeconds()/3600-12+s/60),m=g(Math.asin(Math.sin(-u)*Math.cos(e)));return{lng:c,lat:m}}export class NightLayer{id="nightlayer";type="custom";renderingMode="2d";#o;#n;#u;#c;#a;#s;#e;#t;#r;#i;#h;#f;constructor(t={}){this.#o=t.date??null,this.#n=t.opacity??.5,this.#a=t.twilightSteps??0,this.#s=t.twilightAttenuation??.5,this.setColor(t.color??[0,0,0,255]),this.setDaytimeColor(t.daytimeColor??[0,0,0,0])}getSubsolarPoint(){return getSubsolarPoint(this.#o||new Date)}getDate(){return this.#o}setDate(t){this.#o=t,this.#t?.triggerRepaint()}getOpacity(){return this.#n}setOpacity(t){this.#n=t,this.#t?.triggerRepaint()}getColor(){return this.#u}setColor(t){this.#u=[...t.slice(0,3),t?.[3]??255],this.#t?.triggerRepaint()}getDaytimeColor(){return this.#c}setDaytimeColor(t){this.#c=[...t.slice(0,3),t?.[3]??255],this.#t?.triggerRepaint()}getTwilightSteps(){return this.#a}setTwilightSteps(t){this.#a=t,this.#t?.triggerRepaint()}getTwilightAttenuation(){return this.#s}setTwilightAttenuation(t){this.#s=t,this.#t?.triggerRepaint()}onAdd(t,i){this.#e=new Map,this.#r=i.createBuffer(),this.#i=i.createBuffer(),this.#t=t}onRemove(){this.#e=void 0,this.#t=void 0,this.#r=void 0,this.#i=void 0}#l(t,i){const o=`#version 300 es
2
2
  precision highp float;
3
3
 
4
4
  in vec2 a_position;
5
5
  out vec2 v_position;
6
6
 
7
- ${!r.vertexShaderPrelude?`
7
+ ${!i.vertexShaderPrelude?`
8
8
  uniform mat4 u_matrix;
9
9
 
10
10
  vec4 projectTile(vec2 pos) {
11
11
  return u_matrix * vec4(pos, 0., 1.);
12
12
  }
13
13
  `:`
14
- ${r.vertexShaderPrelude}
15
- ${r.define}
14
+ ${i.vertexShaderPrelude}
15
+ ${i.define}
16
16
  `}
17
17
 
18
18
  void main() {
@@ -27,10 +27,10 @@ ${!r.vertexShaderPrelude?`
27
27
 
28
28
  uniform vec2 u_subsolar;
29
29
  uniform float u_opacity;
30
- uniform float u_twilight_step_angle;
31
30
  uniform float u_twilight_steps;
32
31
  uniform float u_twilight_attenuation;
33
- uniform vec3 u_color;
32
+ uniform vec4 u_daytime_color;
33
+ uniform vec4 u_night_color;
34
34
 
35
35
  vec2 mercatorToLngLat(vec2 mercator) {
36
36
  // 0 <= x <= 1, 0 <= y <= 1
@@ -51,7 +51,9 @@ ${!r.vertexShaderPrelude?`
51
51
  float A = sin(observer.y) * sin(subsolar.y);
52
52
  float B = cos(observer.y) * cos(subsolar.y) * cos(subsolar.x - observer.x);
53
53
  float altitude = degrees(asin(A + B));
54
- float twilightStepAngle = u_twilight_step_angle;
54
+
55
+ float twilightStepAngle = 6.0;
56
+ float twilightBegins = -0.8333333;
55
57
  float twilightSteps = u_twilight_steps;
56
58
 
57
59
  // Attenuation for each twilightStepAngle
@@ -59,10 +61,12 @@ ${!r.vertexShaderPrelude?`
59
61
  float twilightLevel = -altitude / twilightStepAngle;
60
62
  if (twilightSteps > 0.) {
61
63
  twilightLevel = ceil(clamp(twilightLevel, 0., twilightSteps));
64
+
65
+ if (altitude > twilightBegins) {
66
+ // XXX: Adjust for sunset/sunrise line
67
+ twilightLevel = 0.;
68
+ }
62
69
  }
63
70
  float brightness = clamp(pow(clamp(1. - att, 0., 1.), twilightLevel), 0., 1.);
64
- float darkness = (1. - brightness);
65
-
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,s),t.compileShader(e);const i=t.createShader(t.FRAGMENT_SHADER);if(!i)throw new Error("failed to create fragment shader");t.shaderSource(i,n),t.compileShader(i);const a=t.createProgram();if(!a)throw new Error("failed to create program");return t.attachShader(a,e),t.attachShader(a,i),t.linkProgram(a),a}#m(t){if(!this.#t||!this.#i||!this.#r)return;const[r,u]=this.#t.getBounds().toArray(),s=Math.floor(f.fromLngLat(r).x),n=Math.ceil(f.fromLngLat(u).x),e=this.#t.style?.projection?.name==="globe"?"globe":[s,n].join(":");if(e===this.#f)return;let i;e==="globe"?(i=m({granularity:100,generateBorders:!1,extendToNorthPole:!0,extendToSouthPole:!0},"16bit"),i.vertices=new Float32Array(new Int16Array(i.vertices)).map(a=>a/8192).buffer):i={vertices:new Float32Array([s,0,n,0,s,1,s,1,n,0,n,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,u){if(u||(u=r),!this.#e||!this.#i||!this.#r||!this.#t)return;const s=u.shaderData||{},n=s.variantName||"default";this.#e.has(n)||this.#e.set(n,this.#l(t,s));const e=this.#e.get(n);if(!e)return;const{lng:i,lat:a}=this.getSubsolarPoint();if(t.useProgram(e),t.uniform2fv(t.getUniformLocation(e,"u_subsolar"),[i,a]),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 h=this.#t.transform.getProjectionDataForCustomLayer(!0);t.uniformMatrix4fv(t.getUniformLocation(e,"u_projection_fallback_matrix"),!1,h.fallbackMatrix),t.uniformMatrix4fv(t.getUniformLocation(e,"u_projection_matrix"),!1,h.mainMatrix),t.uniform4f(t.getUniformLocation(e,"u_projection_tile_mercator_coords"),...h.tileMercatorCoords),t.uniform4f(t.getUniformLocation(e,"u_projection_clipping_plane"),...h.clippingPlane),t.uniform1f(t.getUniformLocation(e,"u_projection_transition"),h.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;
71
+ fragColor = mix(u_night_color, u_daytime_color, brightness) * u_opacity;
72
+ }`,e=t.createShader(t.VERTEX_SHADER);if(!e)throw new Error("failed to create vertex shader");t.shaderSource(e,o),t.compileShader(e);const r=t.createShader(t.FRAGMENT_SHADER);if(!r)throw new Error("failed to create fragment shader");t.shaderSource(r,n),t.compileShader(r);const s=t.createProgram();if(!s)throw new Error("failed to create program");return t.attachShader(s,e),t.attachShader(s,r),t.linkProgram(s),s}#m(t){if(!this.#t||!this.#r||!this.#i)return;const[i,u]=this.#t.getBounds().toArray(),o=Math.floor(f.fromLngLat(i).x),n=Math.ceil(f.fromLngLat(u).x),e=this.#t.style?.projection?.name==="globe"?"globe":[o,n].join(":");if(e===this.#f)return;let r;e==="globe"?(r=d({granularity:100,generateBorders:!1,extendToNorthPole:!0,extendToSouthPole:!0},"16bit"),r.vertices=new Float32Array(new Int16Array(r.vertices)).map(s=>s/8192).buffer):r={vertices:new Float32Array([o,0,n,0,o,1,o,1,n,0,n,1]).buffer,indices:new Uint16Array([0,1,2,3,4,5]).buffer,uses32bitIndices:!1},t.bindBuffer(t.ARRAY_BUFFER,this.#r),t.bufferData(t.ARRAY_BUFFER,r.vertices,t.DYNAMIC_DRAW),t.bindBuffer(t.ARRAY_BUFFER,null),t.bindBuffer(t.ELEMENT_ARRAY_BUFFER,this.#i),t.bufferData(t.ELEMENT_ARRAY_BUFFER,r.indices,t.DYNAMIC_DRAW),t.bindBuffer(t.ELEMENT_ARRAY_BUFFER,null),this.#h=r.indices.byteLength/2,this.#f=e}render(t,i,u){if(u||(u=i),!this.#e||!this.#r||!this.#i||!this.#t)return;const o=u.shaderData||{},n=o.variantName||"default";this.#e.has(n)||this.#e.set(n,this.#l(t,o));const e=this.#e.get(n);if(!e)return;const{lng:r,lat:s}=this.getSubsolarPoint();if(t.useProgram(e),t.uniform2fv(t.getUniformLocation(e,"u_subsolar"),[r,s]),t.uniform1f(t.getUniformLocation(e,"u_opacity"),this.#n),t.uniform4fv(t.getUniformLocation(e,"u_night_color"),l(this.#u)),t.uniform4fv(t.getUniformLocation(e,"u_daytime_color"),l(this.#c)),t.uniform1f(t.getUniformLocation(e,"u_twilight_steps"),this.#a),t.uniform1f(t.getUniformLocation(e,"u_twilight_attenuation"),this.#s),"getProjectionDataForCustomLayer"in this.#t.transform){const c=this.#t.transform.getProjectionDataForCustomLayer(!0);t.uniformMatrix4fv(t.getUniformLocation(e,"u_projection_fallback_matrix"),!1,c.fallbackMatrix),t.uniformMatrix4fv(t.getUniformLocation(e,"u_projection_matrix"),!1,c.mainMatrix),t.uniform4f(t.getUniformLocation(e,"u_projection_tile_mercator_coords"),...c.tileMercatorCoords),t.uniform4f(t.getUniformLocation(e,"u_projection_clipping_plane"),...c.clippingPlane),t.uniform1f(t.getUniformLocation(e,"u_projection_transition"),c.projectionTransition)}else t.uniformMatrix4fv(t.getUniformLocation(e,"u_matrix"),!1,i);this.#m(t),t.bindBuffer(t.ARRAY_BUFFER,this.#r),t.bindBuffer(t.ELEMENT_ARRAY_BUFFER,this.#i);const h=t.getAttribLocation(e,"a_position");t.enableVertexAttribArray(h),t.vertexAttribPointer(h,2,t.FLOAT,!1,0,0),t.drawElements(t.TRIANGLES,this.#h,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.8",
3
+ "version": "1.0.0-alpha.9",
4
4
  "description": "A Night Layer for MapLibre GL JS",
5
5
  "type": "module",
6
6
  "main": "dist/main.js",
@@ -10,7 +10,7 @@
10
10
  "dist/"
11
11
  ],
12
12
  "scripts": {
13
- "dev": "vite playground/",
13
+ "dev": "vite playground/v5",
14
14
  "build": "pnpm run build:esm && pnpm run build:types",
15
15
  "build:esm": "esbuild src/main.ts --minify --outfile=dist/main.js",
16
16
  "build:types": "tsc --declaration --emitDeclarationOnly --declarationDir './dist'",
@@ -32,10 +32,10 @@
32
32
  "license": "MIT",
33
33
  "devDependencies": {
34
34
  "@biomejs/biome": "^1.9.4",
35
- "esbuild": "^0.24.0",
35
+ "esbuild": "^0.24.2",
36
36
  "gl-matrix": "^3.4.3",
37
37
  "maplibre-gl": "5.0.0-pre.10",
38
38
  "typescript": "^5.7.2",
39
- "vite": "^6.0.3"
39
+ "vite": "^6.0.5"
40
40
  }
41
41
  }