maplibre-gl-nightlayer 1.0.0-alpha.4 → 1.0.0-alpha.6

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
@@ -45,19 +45,19 @@ class NightLayer implements CustomLayerInterface {
45
45
  setDate(date: Date | null): void;
46
46
 
47
47
  getOpacity(): number;
48
- setOpacity(v: number): void;
48
+ setOpacity(opacity: number): void;
49
49
 
50
50
  getColor(): Color;
51
- setColor(v: Color): void;
51
+ setColor(color: Color): void;
52
52
 
53
53
  getTwilightSteps(): number;
54
- setTwilightSteps(v: number): void;
54
+ setTwilightSteps(steps: number): void;
55
55
 
56
56
  getTwilightStepAngle(): number;
57
- setTwilightStepAngle(v: number): void;
57
+ setTwilightStepAngle(angle: number): void;
58
58
 
59
59
  getTwilightAttenuation(): number;
60
- setTwilightAttenuation(v: number): void;
60
+ setTwilightAttenuation(attenuation: number): void;
61
61
 
62
62
  :
63
63
  }
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
  /**
@@ -6,18 +6,18 @@ type Color = [number, number, number];
6
6
  */
7
7
  type Options = {
8
8
  /**
9
- * Date.
9
+ * Date for the shadow.
10
10
  * If null, the current date is used.
11
11
  */
12
12
  date?: Date | null;
13
13
  /**
14
- * Opacity.
14
+ * Opacity of the shadow.
15
15
  * 0.0 means fully transparent, 1.0 means fully opaque
16
16
  */
17
17
  opacity?: number;
18
18
  /**
19
- * RGB color.
20
- * Each value is in the range [0, 255].
19
+ * Color of the shadow.
20
+ * Each value should be in the range [0, 255].
21
21
  */
22
22
  color?: Color;
23
23
  /**
@@ -45,79 +45,80 @@ export declare class NightLayer implements CustomLayerInterface {
45
45
  renderingMode: "2d";
46
46
  /**
47
47
  * Create a new NightLayer instance. Intended to be passed to `map.addLayer`.
48
- * @param opts Options.
48
+ * @param opts The options.
49
49
  */
50
50
  constructor(opts?: Options);
51
51
  /**
52
52
  * Get the subsolar point (longitude and latitude) at the given date.
53
- * @param date Date. If null, the current date is used.
53
+ * @param date If null, the current date is used.
54
54
  */
55
55
  getSubsolarPoint(date: Date): {
56
56
  subsolarLng: number;
57
57
  subsolarLat: number;
58
58
  };
59
59
  /**
60
- * Get the date of the night layer.
61
- * @returns Date. If null, the current date is used.
60
+ * Get the date for the shadow.
61
+ * @returns The date. If null, the current date is used.
62
62
  */
63
63
  getDate(): Date | null;
64
64
  /**
65
- * Set the date of the night layer.
66
- * @param date Date. If null, the current date is used.
65
+ * Set the date for the shadow.
66
+ * @param date If null, the current date is used.
67
67
  */
68
68
  setDate(date: Date | null): void;
69
69
  /**
70
- * Get the opacity of the night layer.
71
- * @returns Opacity. 0.0 means fully transparent, 1.0 means fully opaque.
70
+ * Get the opacity of the shadow.
71
+ * @returns The opacity. 0.0 means fully transparent, 1.0 means fully opaque.
72
72
  */
73
73
  getOpacity(): number;
74
74
  /**
75
- * Set the opacity of the night layer.
76
- * @param v Opacity. 0.0 means fully transparent, 1.0 means fully opaque.
75
+ * Set the opacity of the shadow.
76
+ * @param opacity 0.0 means fully transparent, 1.0 means fully opaque.
77
77
  */
78
- setOpacity(v: number): void;
78
+ setOpacity(opacity: number): void;
79
79
  /**
80
- * Get the color of the night layer.
81
- * @returns RGB color. Each value is in the range [0, 255].
80
+ * Get the color of the shadow.
81
+ * @returns The color. Each value is in the range [0, 255].
82
82
  */
83
83
  getColor(): Color;
84
84
  /**
85
- * Set the color of the night layer.
86
- * @param v RGB color. Each value should be in the range [0, 255].
85
+ * Set the color of the shadow.
86
+ * @param color Each value should be in the range [0, 255].
87
87
  */
88
- setColor(v: Color): void;
88
+ setColor(color: Color): void;
89
89
  /**
90
90
  * Get the number of twilight steps.
91
- * @returns 0 means no steps (gradation), 1 means one step (day/night), etc.
91
+ * @returns The steps. 0 means no steps (gradation), 1 means one step (day/night), etc.
92
92
  */
93
93
  getTwilightSteps(): number;
94
94
  /**
95
95
  * Set the number of twilight steps.
96
- * @param v 0 means no steps (gradation), 1 means one step (day/night), etc.
96
+ * @param steps 0 means no steps (gradation), 1 means one step (day/night), etc.
97
97
  */
98
- setTwilightSteps(v: number): void;
98
+ setTwilightSteps(steps: number): void;
99
99
  /**
100
100
  * Get the angle for each twilight step.
101
- * @returns angle in degrees.
101
+ * @returns The angle in degrees.
102
102
  */
103
103
  getTwilightStepAngle(): number;
104
104
  /**
105
105
  * Set the angle for each twilight step.
106
- * @param v angle in degrees.
106
+ * @param angle in degrees.
107
107
  */
108
- setTwilightStepAngle(v: number): void;
108
+ setTwilightStepAngle(angle: number): void;
109
109
  /**
110
110
  * Get the attenuation factor for each twilight step.
111
- * @returns 0.0 means no attenuation, 1.0 means full attenuation.
111
+ * @returns The attenuation. 0.0 means no attenuation, 1.0 means full attenuation.
112
112
  */
113
113
  getTwilightAttenuation(): number;
114
114
  /**
115
115
  * Set the attenuation factor for each twilight step.
116
- * @param v 0.0 means no attenuation, 1.0 means full attenuation.
116
+ * @param attenuation 0.0 means no attenuation, 1.0 means full attenuation.
117
117
  */
118
- setTwilightAttenuation(v: number): void;
118
+ setTwilightAttenuation(attenuation: number): void;
119
119
  onAdd(map: MaplibreMap, gl: WebGLRenderingContext): 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 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=`
1
+ "use strict";import{MercatorCoordinate as f,createTileMesh as p}from"maplibre-gl";function d(c){return c*Math.PI/180}function g(c){return c/Math.PI*180}export class NightLayer{id="nightlayer";type="custom";renderingMode="2d";#o;#n;#a;#s;#u;#h;#e;#t;#i;#r;#c;#f;constructor(t={}){this.#o=t.date??null,this.#n=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),n=.0167,e=(r+9)*s,i=e+2*n*Math.sin((r-3)*s),a=(e-Math.atan2(Math.sin(i),Math.cos(i)*Math.cos(o)))/Math.PI,u=720*(a-Math.trunc(a+.5)),l=-15*(t.getUTCHours()+t.getUTCMinutes()/60+t.getUTCSeconds()/3600-12+u/60),m=g(Math.asin(Math.sin(-o)*Math.cos(i)));return{subsolarLng:l,subsolarLat:m}}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.#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()}#l(t){if(!this.#t||!this.#i||!this.#r)return;const[r,s]=this.#t.getBounds().toArray(),o=Math.floor(f.fromLngLat(r).x),n=Math.ceil(f.fromLngLat(s).x),e=this.#t.style?.projection?.name==="globe"?"globe":[o,n].join(":");if(e===this.#f)return;let i;e==="globe"?i=p({granularity:100,generateBorders:!1,extendToNorthPole:!0,extendToSouthPole:!0},"16bit"):i={vertices:new Int16Array([o,0,n,0,o,1,o,1,n,0,n,1]).map(u=>u*8192).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}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}#m(t,r){const o=`#version 300 es
2
2
  precision highp float;
3
3
 
4
- attribute vec2 a_position;
4
+ in vec2 a_position;
5
+ out vec2 v_position;
5
6
 
7
+ ${!r.vertexShaderPrelude?`
6
8
  uniform mat4 u_matrix;
7
9
 
8
- varying vec2 v_position;
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
- vec4 tmp = u_matrix * vec4(a_position, 0.0, 1.0);
12
- gl_Position = tmp;
13
- v_position = a_position;
14
- }`,n=`
19
+ gl_Position = projectTile(a_position / 8192.); // scale to 0.0 - 1.0
20
+ v_position = a_position / 8192.;
21
+ }
22
+ `,n=`#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
- 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.#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;
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,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}render(t,r,s){if(s||(s=r),!this.#e||!this.#i||!this.#r||!this.#t)return;const o=s.shaderData||{},n=o.variantName||"default";this.#e.has(n)||this.#e.set(n,this.#m(t,o));const e=this.#e.get(n);if(!e)return;const{subsolarLng:i,subsolarLat:a}=this.getSubsolarPoint(this.#o||new Date);if(t.useProgram(e),t.uniform2fv(t.getUniformLocation(e,"u_subsolar"),[i,a]),t.uniform1f(t.getUniformLocation(e,"u_opacity"),this.#n),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.#l(t),t.bindBuffer(t.ARRAY_BUFFER,this.#i),t.bindBuffer(t.ELEMENT_ARRAY_BUFFER,this.#r);const u=t.getAttribLocation(e,"a_position");t.enableVertexAttribArray(u),t.vertexAttribPointer(u,2,t.SHORT,!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.4",
3
+ "version": "1.0.0-alpha.6",
4
4
  "description": "A Night Layer for MapLibre GL JS",
5
5
  "type": "module",
6
6
  "main": "dist/main.js",
@@ -28,13 +28,14 @@
28
28
  "terminator"
29
29
  ],
30
30
  "author": "kikuchan98@gmail.com",
31
+ "homepage": "https://github.com/kikuchan/maplibre-gl-nightlayer#readme",
31
32
  "license": "MIT",
32
33
  "devDependencies": {
33
34
  "@biomejs/biome": "^1.9.4",
34
35
  "esbuild": "^0.24.0",
35
36
  "gl-matrix": "^3.4.3",
36
- "maplibre-gl": "^4.7.1",
37
- "typescript": "^5.6.3",
38
- "vite": "^5.4.11"
37
+ "maplibre-gl": "5.0.0-pre.9",
38
+ "typescript": "^5.7.2",
39
+ "vite": "^6.0.3"
39
40
  }
40
41
  }