@naivemap/mapbox-gl-image-layer 0.5.0 → 0.6.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/LICENSE +21 -21
- package/README.md +11 -97
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +61 -0
- package/dist/mapbox-gl-image-layer/src/ImageLayer.d.ts +46 -0
- package/dist/mapbox-gl-image-layer/src/index.d.ts +4 -0
- package/package.json +27 -41
- package/dist/es/index.js +0 -228
- package/dist/es/shaders/image.fragment.glsl.js +0 -1
- package/dist/es/shaders/image.vertex.glsl.js +0 -1
- package/dist/es/shaders/mask.fragment.glsl.js +0 -1
- package/dist/es/shaders/mask.vertex.glsl.js +0 -1
- package/dist/es/utils/arrugator.js +0 -39
- package/dist/es/utils/image.js +0 -19
- package/dist/es/utils/webgl.js +0 -64
- package/dist/js/index.d.ts +0 -60
- package/dist/js/index.js +0 -256
- package/dist/js/shaders/image.fragment.glsl.d.ts +0 -2
- package/dist/js/shaders/image.fragment.glsl.js +0 -3
- package/dist/js/shaders/image.vertex.glsl.d.ts +0 -2
- package/dist/js/shaders/image.vertex.glsl.js +0 -3
- package/dist/js/shaders/mask.fragment.glsl.d.ts +0 -2
- package/dist/js/shaders/mask.fragment.glsl.js +0 -3
- package/dist/js/shaders/mask.vertex.glsl.d.ts +0 -2
- package/dist/js/shaders/mask.vertex.glsl.js +0 -3
- package/dist/js/utils/arrugator.d.ts +0 -7
- package/dist/js/utils/arrugator.js +0 -46
- package/dist/js/utils/image.d.ts +0 -7
- package/dist/js/utils/image.js +0 -23
- package/dist/js/utils/webgl.d.ts +0 -8
- package/dist/js/utils/webgl.js +0 -68
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 Naive Map
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Naive Map
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,97 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
<b>Implements:</b> mapboxgl.CustomLayerInterface
|
|
14
|
-
|
|
15
|
-
## Constructors
|
|
16
|
-
|
|
17
|
-
| Constructor | Description |
|
|
18
|
-
| --- | --- |
|
|
19
|
-
| (constructor)(`id`: `string`, `option`: `ImageOption`) | Constructs a new instance of the <code>ImageLayer</code> class |
|
|
20
|
-
|
|
21
|
-
### Parameters
|
|
22
|
-
|
|
23
|
-
**id** `(string)` The ID of the layer.
|
|
24
|
-
|
|
25
|
-
**option** `(ImageOption)` The option of the image.
|
|
26
|
-
|
|
27
|
-
| Name | Description |
|
|
28
|
-
| --- | --- |
|
|
29
|
-
| **option.url** <br />(`string`) | URL that points to an image. |
|
|
30
|
-
| **option.projection** <br />(`string`) | Projection with EPSG code that points to the image. |
|
|
31
|
-
| **option.coordinates** <br />(`Array<Array<number>>`) | Corners of image specified in longitude, latitude pairs: top left, top right, bottom right, bottom left. ref: [coordinates](https://docs.mapbox.com/mapbox-gl-js/style-spec/sources/#image-coordinates) |
|
|
32
|
-
| **option.resampling** <br />(Optional `enum`. One of `"linear"`, `"nearest"`. Defaults to `"linear"`) | The resampling/interpolation method to use for overscaling, also known as texture magnification filter. ref: [raster-resampling](https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#paint-raster-raster-resampling) |
|
|
33
|
-
| **option.opacity** <br />(Optional `number` between 0 and 1 inclusive. Defaults to 1. | The opacity at which the image will be drawn. |
|
|
34
|
-
| **option.crossOrigin** <br />(`string`) | The crossOrigin attribute is a string which specifies the Cross-Origin Resource Sharing ([CORS](https://developer.mozilla.org/en-US/docs/Glossary/CORS)) setting to use when retrieving the image. |
|
|
35
|
-
| **option.mask** <br />(`MaskProperty`) | The polygonal mask or multipolygonal mask for the image. |
|
|
36
|
-
|
|
37
|
-
```ts
|
|
38
|
-
export type ImageOption = {
|
|
39
|
-
url: string
|
|
40
|
-
projection: string
|
|
41
|
-
coordinates: Coordinates
|
|
42
|
-
resampling?: 'linear' | 'nearest'
|
|
43
|
-
opacity?: number
|
|
44
|
-
crossOrigin?: string
|
|
45
|
-
mask?: MaskProperty
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// top left, top right, bottom right, bottom left.
|
|
49
|
-
export type Coordinates = [[number, number], [number, number], [number, number], [number, number]]
|
|
50
|
-
|
|
51
|
-
export type MaskProperty = {
|
|
52
|
-
type?: 'in' | 'out' // default: in
|
|
53
|
-
data: GeoJSON.Polygon | GeoJSON.MultiPolygon
|
|
54
|
-
}
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
### Methods
|
|
58
|
-
|
|
59
|
-
#### updateImage
|
|
60
|
-
|
|
61
|
-
Updates the URL, the projection, the coordinates, the opacity or the resampling of the image.
|
|
62
|
-
|
|
63
|
-
```ts
|
|
64
|
-
updateImage(option: {
|
|
65
|
-
url?: string
|
|
66
|
-
projection?: string
|
|
67
|
-
coordinates?: Coordinates
|
|
68
|
-
opacity?: number
|
|
69
|
-
resampling?: 'linear' | 'nearest'
|
|
70
|
-
}): this
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
#### updateMask
|
|
74
|
-
|
|
75
|
-
Updates the mask property.
|
|
76
|
-
|
|
77
|
-
```ts
|
|
78
|
-
updateMask(mask: Partial<MaskProperty>): this
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
## Example
|
|
82
|
-
|
|
83
|
-
```ts
|
|
84
|
-
const layer = new ImageLayer('layer-id', {
|
|
85
|
-
url: '/4326.png',
|
|
86
|
-
projection: 'EPSG:4326',
|
|
87
|
-
resampling: 'nearest',
|
|
88
|
-
coordinates: [
|
|
89
|
-
[105.289838, 32.204171], // top-left
|
|
90
|
-
[110.195632, 32.204171], // top-right
|
|
91
|
-
[110.195632, 28.164713], // bottom-right
|
|
92
|
-
[105.289838, 28.164713], // bottom-left
|
|
93
|
-
],
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
map.addLayer(layer)
|
|
97
|
-
```
|
|
1
|
+
# @naivemap/mapbox-gl-image-layer
|
|
2
|
+
|
|
3
|
+
ImageLayer ([@naivemap/mapbox-gl-image-layer](https://www.npmjs.com/package/@naivemap/mapbox-gl-image-layer)): A versatile layer for displaying georeferenced images with **various projections** on the map. It supports loading and correctly positioning images from different coordinate systems, making it ideal for integrating non-standard raster data, historical maps, or aerial imagery.
|
|
4
|
+
|
|
5
|
+
[API References](https://www.naivemap.com/map-gl-layers/api/image-layer/)
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @naivemap/mapbox-gl-image-layer proj4
|
|
11
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const o=require("@naivemap/image-layer-core"),a=require("@naivemap/map-gl-layer-adaptor");class d{id;type="custom";renderingMode="2d";core;adaptor;constructor(e,r){this.id=e,this.core=new o(r),this.adaptor=a.createMapboxLayerAdaptor(this.core,{id:e,renderingMode:this.renderingMode,prepareStencilMask:t=>a.clearTileClippingMask(t)})}onAdd(e,r){this.adaptor.onAdd(e,r)}onRemove(e,r){this.adaptor.onRemove(e,r)}render(e,r){this.adaptor.render(e,r)}updateImage(e){return this.core.updateImage(e)}updateMask(e){return this.core.updateMask(e)}}module.exports=d;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import t from "@naivemap/image-layer-core";
|
|
2
|
+
import { createMapboxLayerAdaptor as o, clearTileClippingMask as d } from "@naivemap/map-gl-layer-adaptor";
|
|
3
|
+
class n {
|
|
4
|
+
id;
|
|
5
|
+
/**
|
|
6
|
+
* @ignore
|
|
7
|
+
*/
|
|
8
|
+
type = "custom";
|
|
9
|
+
/**
|
|
10
|
+
* @ignore
|
|
11
|
+
*/
|
|
12
|
+
renderingMode = "2d";
|
|
13
|
+
core;
|
|
14
|
+
adaptor;
|
|
15
|
+
/**
|
|
16
|
+
* @param id - A unique layer id
|
|
17
|
+
* @param option - ImageLayer options
|
|
18
|
+
*/
|
|
19
|
+
constructor(e, r) {
|
|
20
|
+
this.id = e, this.core = new t(r), this.adaptor = o(this.core, {
|
|
21
|
+
id: e,
|
|
22
|
+
renderingMode: this.renderingMode,
|
|
23
|
+
prepareStencilMask: (a) => d(a)
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* @ignore
|
|
28
|
+
*/
|
|
29
|
+
onAdd(e, r) {
|
|
30
|
+
this.adaptor.onAdd(e, r);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* @ignore
|
|
34
|
+
*/
|
|
35
|
+
onRemove(e, r) {
|
|
36
|
+
this.adaptor.onRemove(e, r);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* @ignore
|
|
40
|
+
*/
|
|
41
|
+
render(e, r) {
|
|
42
|
+
this.adaptor.render(e, r);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Updates the URL, the projection, the coordinates, the opacity or the resampling of the image.
|
|
46
|
+
* @param {Object} option Options object.
|
|
47
|
+
*/
|
|
48
|
+
updateImage(e) {
|
|
49
|
+
return this.core.updateImage(e);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Updates the mask property of the image layer.
|
|
53
|
+
* @param mask
|
|
54
|
+
*/
|
|
55
|
+
updateMask(e) {
|
|
56
|
+
return this.core.updateMask(e);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export {
|
|
60
|
+
n as default
|
|
61
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { default as ImageLayerCore, ImageLayerOption, MaskProperty } from '../../image-layer-core/src/index.ts';
|
|
2
|
+
import { CustomLayerInterface, Map } from 'mapbox-gl';
|
|
3
|
+
export type { ImageLayerOption } from '../../image-layer-core/src/index.ts';
|
|
4
|
+
/**
|
|
5
|
+
* A custom Mapbox GL JS layer for rendering georeferenced images with arbitrary projections.
|
|
6
|
+
*/
|
|
7
|
+
export default class ImageLayer implements CustomLayerInterface {
|
|
8
|
+
id: string;
|
|
9
|
+
/**
|
|
10
|
+
* @ignore
|
|
11
|
+
*/
|
|
12
|
+
type: 'custom';
|
|
13
|
+
/**
|
|
14
|
+
* @ignore
|
|
15
|
+
*/
|
|
16
|
+
renderingMode?: '2d' | '3d' | undefined;
|
|
17
|
+
private core;
|
|
18
|
+
private adaptor;
|
|
19
|
+
/**
|
|
20
|
+
* @param id - A unique layer id
|
|
21
|
+
* @param option - ImageLayer options
|
|
22
|
+
*/
|
|
23
|
+
constructor(id: string, option: ImageLayerOption);
|
|
24
|
+
/**
|
|
25
|
+
* @ignore
|
|
26
|
+
*/
|
|
27
|
+
onAdd(map: Map, gl: WebGLRenderingContext): void;
|
|
28
|
+
/**
|
|
29
|
+
* @ignore
|
|
30
|
+
*/
|
|
31
|
+
onRemove(map: Map, gl: WebGLRenderingContext): void;
|
|
32
|
+
/**
|
|
33
|
+
* @ignore
|
|
34
|
+
*/
|
|
35
|
+
render(gl: WebGLRenderingContext, args: unknown): void;
|
|
36
|
+
/**
|
|
37
|
+
* Updates the URL, the projection, the coordinates, the opacity or the resampling of the image.
|
|
38
|
+
* @param {Object} option Options object.
|
|
39
|
+
*/
|
|
40
|
+
updateImage(option: Partial<Omit<ImageLayerOption, 'mask'>>): ImageLayerCore;
|
|
41
|
+
/**
|
|
42
|
+
* Updates the mask property of the image layer.
|
|
43
|
+
* @param mask
|
|
44
|
+
*/
|
|
45
|
+
updateMask(mask: Partial<MaskProperty>): ImageLayerCore;
|
|
46
|
+
}
|
package/package.json
CHANGED
|
@@ -1,61 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naivemap/mapbox-gl-image-layer",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Load a static image of any projection via Arrugator and Proj4js.",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"main": "dist/js/index.js",
|
|
9
|
-
"module": "dist/es/index.js",
|
|
3
|
+
"version": "0.6.0-alpha.0",
|
|
4
|
+
"description": "Load a static image of any projection via Arrugator and Proj4js for Mapbox GL JS.",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"type": "module",
|
|
10
8
|
"exports": {
|
|
11
|
-
"./package.json": "./package.json",
|
|
12
9
|
".": {
|
|
13
|
-
"
|
|
14
|
-
"
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.cjs"
|
|
15
13
|
}
|
|
16
14
|
},
|
|
17
|
-
"types": "dist/js/index.d.ts",
|
|
18
|
-
"sideEffects": false,
|
|
19
15
|
"files": [
|
|
20
16
|
"dist"
|
|
21
17
|
],
|
|
22
18
|
"keywords": [
|
|
23
|
-
"mapbox
|
|
24
|
-
"
|
|
25
|
-
"
|
|
19
|
+
"mapbox",
|
|
20
|
+
"image",
|
|
21
|
+
"layer",
|
|
22
|
+
"custom"
|
|
26
23
|
],
|
|
24
|
+
"author": "naivemap",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"repository": "https://github.com/naivemap/maplibre-gl-layers",
|
|
27
28
|
"publishConfig": {
|
|
28
29
|
"access": "public"
|
|
29
30
|
},
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
"scripts": {
|
|
35
|
-
"test": "echo \"Error: run tests from root\" && exit 1",
|
|
36
|
-
"build": "npm-run-all build:*",
|
|
37
|
-
"build:es": "tsc --outDir dist/es --module esnext --declaration false",
|
|
38
|
-
"build:js": "tsc",
|
|
39
|
-
"prepublishOnly": "npm-run-all build:*"
|
|
40
|
-
},
|
|
41
|
-
"bugs": {
|
|
42
|
-
"url": "https://github.com/naivemap/mapbox-gl-layers/issues"
|
|
31
|
+
"sideEffects": false,
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@naivemap/map-gl-layer-adaptor": "0.1.0",
|
|
34
|
+
"@naivemap/image-layer-core": "0.1.0"
|
|
43
35
|
},
|
|
44
36
|
"peerDependencies": {
|
|
37
|
+
"mapbox-gl": "^2.0.0 || ^3.0.0",
|
|
45
38
|
"proj4": "^2.8.0"
|
|
46
39
|
},
|
|
47
|
-
"dependencies": {
|
|
48
|
-
"arrugator": "^1.0.1",
|
|
49
|
-
"compare-versions": "^6.0.0-rc.1",
|
|
50
|
-
"earcut": "^2.2.4",
|
|
51
|
-
"twgl.js": "^5.3.1"
|
|
52
|
-
},
|
|
53
40
|
"devDependencies": {
|
|
54
|
-
"
|
|
55
|
-
"@types/mapbox-gl": "^2.7.2",
|
|
56
|
-
"@types/node": "^17.0.36",
|
|
57
|
-
"@types/proj4": "^2.5.2",
|
|
58
|
-
"npm-run-all": "^4.1.5"
|
|
41
|
+
"mapbox-gl": "^3.10.0"
|
|
59
42
|
},
|
|
60
|
-
"
|
|
61
|
-
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "vite build",
|
|
45
|
+
"typedocs": "pnpm typedoc"
|
|
46
|
+
}
|
|
47
|
+
}
|
package/dist/es/index.js
DELETED
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
import { satisfies } from 'compare-versions';
|
|
2
|
-
import earcut from 'earcut';
|
|
3
|
-
import mapboxgl from 'mapbox-gl';
|
|
4
|
-
import * as twgl from 'twgl.js';
|
|
5
|
-
import fs from './shaders/image.fragment.glsl';
|
|
6
|
-
import vs from './shaders/image.vertex.glsl';
|
|
7
|
-
import maskfs from './shaders/mask.fragment.glsl';
|
|
8
|
-
import maskvs from './shaders/mask.vertex.glsl';
|
|
9
|
-
import { initArrugator } from './utils/arrugator';
|
|
10
|
-
var ImageLayer = /** @class */ (function () {
|
|
11
|
-
function ImageLayer(id, option) {
|
|
12
|
-
this.type = 'custom';
|
|
13
|
-
this.renderingMode = '2d';
|
|
14
|
-
// mask
|
|
15
|
-
this.stencilChecked = true; // resetStencilClippingMasks 版本检查
|
|
16
|
-
this.id = id;
|
|
17
|
-
this.option = option;
|
|
18
|
-
this.loaded = false;
|
|
19
|
-
this.maskProperty = Object.assign({ type: 'in' }, option.mask);
|
|
20
|
-
// 检查 stencil 是否可用
|
|
21
|
-
this.stencilChecked = satisfies(mapboxgl.version, '>=2.7.0');
|
|
22
|
-
// 如果传了 mask 边界数据,且版本不符
|
|
23
|
-
if (this.maskProperty.data && !this.stencilChecked) {
|
|
24
|
-
throw new Error("\u5982\u679C\u9700\u8981\u906E\u7F69\uFF08\u63A9\u819C\uFF09\uFF0Cmapbox-gl \u7248\u672C\u5FC5\u987B\uFF1A>=2.7.0");
|
|
25
|
-
}
|
|
26
|
-
// 初始化 Arrugator
|
|
27
|
-
var projection = option.projection, coordinates = option.coordinates;
|
|
28
|
-
this.arrugado = initArrugator(projection, coordinates);
|
|
29
|
-
// this._map = null
|
|
30
|
-
// this._gl = null
|
|
31
|
-
// this._program = null
|
|
32
|
-
// this._texture = null
|
|
33
|
-
// this._positionBuffer = null
|
|
34
|
-
// this._uvBuffer = null
|
|
35
|
-
// this._verticesIndexBuffer = null
|
|
36
|
-
}
|
|
37
|
-
ImageLayer.prototype.onAdd = function (map, gl) {
|
|
38
|
-
this.map = map;
|
|
39
|
-
this.gl = gl;
|
|
40
|
-
// 主程序
|
|
41
|
-
this.programInfo = twgl.createProgramInfo(gl, [vs, fs]);
|
|
42
|
-
this.loadTexture(map, gl);
|
|
43
|
-
this.bufferInfo = twgl.createBufferInfoFromArrays(gl, {
|
|
44
|
-
a_pos: { numComponents: 2, data: this.arrugado.pos },
|
|
45
|
-
a_uv: { numComponents: 2, data: this.arrugado.uv },
|
|
46
|
-
indices: this.arrugado.trigs,
|
|
47
|
-
});
|
|
48
|
-
// 掩膜程序
|
|
49
|
-
if (this.maskProperty.data) {
|
|
50
|
-
var data = this.maskProperty.data;
|
|
51
|
-
if (data) {
|
|
52
|
-
this.maskProgramInfo = twgl.createProgramInfo(gl, [maskvs, maskfs]);
|
|
53
|
-
this.maskBufferInfo = this.getMaskBufferInfo(gl, data);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
ImageLayer.prototype.onRemove = function (map, gl) {
|
|
58
|
-
if (this.programInfo) {
|
|
59
|
-
gl.deleteProgram(this.programInfo.program);
|
|
60
|
-
}
|
|
61
|
-
if (this.maskProgramInfo) {
|
|
62
|
-
gl.deleteProgram(this.maskProgramInfo.program);
|
|
63
|
-
}
|
|
64
|
-
if (this.texture) {
|
|
65
|
-
gl.deleteTexture(this.texture);
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
ImageLayer.prototype.render = function (gl, matrix) {
|
|
69
|
-
/**
|
|
70
|
-
* 线图层在启用 stencil 会消失,参考: https://github.com/mapbox/mapbox-gl-js/issues/12213
|
|
71
|
-
* 临时解决方案: map.painter.resetStencilClippingMasks()
|
|
72
|
-
* 该方法在 mapboxgl version >=2.7.0 才能用
|
|
73
|
-
*/
|
|
74
|
-
var _a;
|
|
75
|
-
if (this.stencilChecked) {
|
|
76
|
-
// @ts-ignore
|
|
77
|
-
this.map.painter.resetStencilClippingMasks();
|
|
78
|
-
}
|
|
79
|
-
if (this.loaded && this.programInfo && this.bufferInfo) {
|
|
80
|
-
// blend
|
|
81
|
-
gl.enable(gl.BLEND);
|
|
82
|
-
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
83
|
-
if (this.maskProgramInfo && this.maskBufferInfo) {
|
|
84
|
-
// mask program
|
|
85
|
-
gl.useProgram(this.maskProgramInfo.program);
|
|
86
|
-
// stencil test
|
|
87
|
-
gl.enable(gl.STENCIL_TEST);
|
|
88
|
-
gl.stencilFunc(gl.ALWAYS, 1, 0xff);
|
|
89
|
-
gl.stencilOp(gl.REPLACE, gl.REPLACE, gl.REPLACE);
|
|
90
|
-
gl.stencilMask(0xff);
|
|
91
|
-
gl.clear(gl.STENCIL_BUFFER_BIT);
|
|
92
|
-
// matrix
|
|
93
|
-
twgl.setUniforms(this.maskProgramInfo, { u_matrix: matrix });
|
|
94
|
-
// pos & indices
|
|
95
|
-
twgl.setBuffersAndAttributes(gl, this.maskProgramInfo, this.maskBufferInfo);
|
|
96
|
-
// draw
|
|
97
|
-
var elementType = gl.UNSIGNED_SHORT;
|
|
98
|
-
if (this.maskBufferInfo.numElements / 3 > 65535) {
|
|
99
|
-
// 使 drawElements 支持 UNSIGNED_INT 类型
|
|
100
|
-
gl.getExtension('OES_element_index_uint');
|
|
101
|
-
elementType = gl.UNSIGNED_INT;
|
|
102
|
-
}
|
|
103
|
-
gl.drawElements(gl.TRIANGLES, this.maskBufferInfo.numElements, elementType, 0);
|
|
104
|
-
}
|
|
105
|
-
// texture program
|
|
106
|
-
gl.useProgram(this.programInfo.program);
|
|
107
|
-
if ((_a = this.maskProgramInfo) === null || _a === void 0 ? void 0 : _a.program) {
|
|
108
|
-
// stencil test
|
|
109
|
-
var ref = this.maskProperty.type === 'out' ? 0 : 1;
|
|
110
|
-
gl.stencilFunc(gl.EQUAL, ref, 0xff);
|
|
111
|
-
gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
|
|
112
|
-
}
|
|
113
|
-
// uniforms
|
|
114
|
-
twgl.setUniforms(this.programInfo, {
|
|
115
|
-
u_matrix: matrix,
|
|
116
|
-
u_opacity: this.option.opacity || 1,
|
|
117
|
-
u_sampler: this.texture,
|
|
118
|
-
});
|
|
119
|
-
// pos, uv & indices
|
|
120
|
-
twgl.setBuffersAndAttributes(gl, this.programInfo, this.bufferInfo);
|
|
121
|
-
// draw
|
|
122
|
-
gl.drawElements(gl.TRIANGLES, this.arrugado.trigs.length, gl.UNSIGNED_SHORT, 0);
|
|
123
|
-
gl.clear(gl.STENCIL_BUFFER_BIT);
|
|
124
|
-
gl.disable(gl.STENCIL_TEST);
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
/**
|
|
128
|
-
* Updates the URL, the projection, the coordinates, the opacity or the resampling of the image.
|
|
129
|
-
* @param {Object} option Options object.
|
|
130
|
-
* @param {string} [option.url] Image URL.
|
|
131
|
-
* @param {string} [option.projection] Projection with EPSG code that points to the image..
|
|
132
|
-
* @param {Array<Array<number>>} [option.coordinates] Four geographical coordinates,
|
|
133
|
-
* @param {number} [option.opacity] opacity of the image.
|
|
134
|
-
* @param {string} [option.resampling] The resampling/interpolation method to use for overscaling.
|
|
135
|
-
*/
|
|
136
|
-
ImageLayer.prototype.updateImage = function (option) {
|
|
137
|
-
var _a, _b, _c, _d, _e;
|
|
138
|
-
if (this.gl && this.map) {
|
|
139
|
-
this.option.opacity = (_a = option.opacity) !== null && _a !== void 0 ? _a : this.option.opacity;
|
|
140
|
-
if (option.projection || option.coordinates) {
|
|
141
|
-
this.option.projection = (_b = option.projection) !== null && _b !== void 0 ? _b : this.option.projection;
|
|
142
|
-
this.option.coordinates = (_c = option.coordinates) !== null && _c !== void 0 ? _c : this.option.coordinates;
|
|
143
|
-
// reinit arrugator
|
|
144
|
-
this.arrugado = initArrugator(this.option.projection, this.option.coordinates);
|
|
145
|
-
this.bufferInfo = twgl.createBufferInfoFromArrays(this.gl, {
|
|
146
|
-
a_pos: { numComponents: 2, data: this.arrugado.pos },
|
|
147
|
-
a_uv: { numComponents: 2, data: this.arrugado.uv },
|
|
148
|
-
indices: this.arrugado.trigs,
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
if (option.url || option.resampling) {
|
|
152
|
-
this.loaded = false;
|
|
153
|
-
this.option.url = (_d = option.url) !== null && _d !== void 0 ? _d : this.option.url;
|
|
154
|
-
this.option.resampling = (_e = option.resampling) !== null && _e !== void 0 ? _e : this.option.resampling;
|
|
155
|
-
// reload image
|
|
156
|
-
this.loadTexture(this.map, this.gl);
|
|
157
|
-
}
|
|
158
|
-
else {
|
|
159
|
-
this.map.triggerRepaint();
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
return this;
|
|
163
|
-
};
|
|
164
|
-
/**
|
|
165
|
-
* Updates the mask property
|
|
166
|
-
* @param {MaskProperty} mask The mask property.
|
|
167
|
-
*/
|
|
168
|
-
ImageLayer.prototype.updateMask = function (mask) {
|
|
169
|
-
if (this.gl && this.map && this.maskProgramInfo) {
|
|
170
|
-
this.maskProperty = Object.assign(this.maskProperty, mask);
|
|
171
|
-
this.maskBufferInfo = this.getMaskBufferInfo(this.gl, this.maskProperty.data);
|
|
172
|
-
this.map.triggerRepaint();
|
|
173
|
-
}
|
|
174
|
-
return this;
|
|
175
|
-
};
|
|
176
|
-
ImageLayer.prototype.loadTexture = function (map, gl) {
|
|
177
|
-
var _this = this;
|
|
178
|
-
// 创建纹理
|
|
179
|
-
var filter = this.option.resampling === 'nearest' ? gl.NEAREST : gl.LINEAR;
|
|
180
|
-
this.texture = twgl.createTexture(gl, {
|
|
181
|
-
src: this.option.url,
|
|
182
|
-
crossOrigin: this.option.crossOrigin,
|
|
183
|
-
minMag: filter,
|
|
184
|
-
flipY: 0,
|
|
185
|
-
}, function () {
|
|
186
|
-
_this.loaded = true;
|
|
187
|
-
map.triggerRepaint();
|
|
188
|
-
});
|
|
189
|
-
};
|
|
190
|
-
ImageLayer.prototype.getMaskBufferInfo = function (gl, data) {
|
|
191
|
-
var positions = [];
|
|
192
|
-
var triangles = [];
|
|
193
|
-
if (data.type === 'MultiPolygon') {
|
|
194
|
-
// type: 'MultiPolygon'
|
|
195
|
-
var polyCount = data.coordinates.length;
|
|
196
|
-
var triangleStartIndex_1 = 0;
|
|
197
|
-
for (var i = 0; i < polyCount; i++) {
|
|
198
|
-
var coordinates = data.coordinates[i];
|
|
199
|
-
var flatten = earcut.flatten(coordinates);
|
|
200
|
-
var vertices = flatten.vertices, holes = flatten.holes, dimensions = flatten.dimensions;
|
|
201
|
-
var triangle = earcut(vertices, holes, dimensions);
|
|
202
|
-
var triangleNew = triangle.map(function (item) { return item + triangleStartIndex_1; });
|
|
203
|
-
triangleStartIndex_1 += vertices.length / 2;
|
|
204
|
-
// positions.push(...vertices)
|
|
205
|
-
// triangles.push(...triangleNew)
|
|
206
|
-
for (var m = 0; m < vertices.length; m++) {
|
|
207
|
-
positions.push(vertices[m]);
|
|
208
|
-
}
|
|
209
|
-
for (var n = 0; n < triangleNew.length; n++) {
|
|
210
|
-
triangles.push(triangleNew[n]);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
else {
|
|
215
|
-
// type: 'Polygon'
|
|
216
|
-
var flatten = earcut.flatten(data.coordinates);
|
|
217
|
-
var vertices = flatten.vertices, holes = flatten.holes, dimensions = flatten.dimensions;
|
|
218
|
-
positions = vertices;
|
|
219
|
-
triangles = earcut(vertices, holes, dimensions);
|
|
220
|
-
}
|
|
221
|
-
return twgl.createBufferInfoFromArrays(gl, {
|
|
222
|
-
a_pos: { numComponents: 2, data: positions },
|
|
223
|
-
indices: triangles.length / 3 > 65535 ? new Uint32Array(triangles) : new Uint16Array(triangles),
|
|
224
|
-
});
|
|
225
|
-
};
|
|
226
|
-
return ImageLayer;
|
|
227
|
-
}());
|
|
228
|
-
export default ImageLayer;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default "\n#ifdef GL_ES\n precision highp int;\n precision mediump float;\n#endif\nuniform sampler2D u_sampler;\nuniform float u_opacity;\nvarying vec2 v_uv;\nvoid main() {\n vec4 color = texture2D(u_sampler, v_uv);\n gl_FragColor = color * u_opacity;\n}\n";
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default "\nuniform mat4 u_matrix;\nattribute vec2 a_pos;\nattribute vec2 a_uv;\nvarying vec2 v_uv;\nvoid main() {\n gl_Position = u_matrix * vec4(a_pos, 0.0, 1.0);\n v_uv = a_uv;\n}\n";
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default "\n#ifdef GL_ES\nprecision highp int;\nprecision mediump float;\n#endif\n\nvoid main() {\n gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);\n}\n";
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default "\nuniform mat4 u_matrix;\nuniform float u_alt;\nattribute vec2 a_pos;\n\nconst float PI = 3.1415926536;\nconst float earthRadius = 6371008.8;\nconst float earthCircumference = 2.0 * PI * earthRadius; // meters\n\nfloat circumferenceAtLatitude(float latitude) {\n return earthCircumference * cos(latitude * PI / 180.0);\n}\n\nfloat mercatorXfromLng(float lng) {\n return (180.0 + lng) / 360.0;\n}\n\nfloat mercatorYfromLat(float lat) {\n return (180.0 - (180.0 / PI * log(tan(PI / 4.0 + lat * PI / 360.0)))) / 360.0;\n}\n\nfloat mercatorZfromAltitude(float altitude, float lat) {\n return altitude / circumferenceAtLatitude(lat);\n}\n\nvec3 mercatorfromLngLat(vec2 lnglat, float alt) {\n return vec3(mercatorXfromLng(lnglat.x), mercatorYfromLat(lnglat.y), mercatorZfromAltitude(alt, lnglat.y));\n}\n\nvoid main() {\n vec3 mercator = mercatorfromLngLat(a_pos, 0.0);\n gl_Position = u_matrix * vec4(mercator, 1.0);\n // gl_Position = u_matrix * vec4(a_pos, 0.0, 1.0);\n}\n";
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
// @ts-ignore
|
|
2
|
-
import Arrugator from 'arrugator';
|
|
3
|
-
import proj4 from 'proj4';
|
|
4
|
-
export function initArrugator(fromProj, coordinates) {
|
|
5
|
-
// 墨卡托投影的左上角坐标,对应 mapbox 左上角起始坐标 [0,0]
|
|
6
|
-
var origin = [-20037508.342789244, 20037508.342789244];
|
|
7
|
-
// 坐标转换为 Arrugator 坐标 top-left, top-left, top-left, top-left)
|
|
8
|
-
var verts = [coordinates[0], coordinates[3], coordinates[1], coordinates[2]];
|
|
9
|
-
// 转换为 EPSG:3857
|
|
10
|
-
var projector = proj4(fromProj, 'EPSG:3857').forward;
|
|
11
|
-
// 改写坐标转换函数,因为 mapbox 的墨卡托坐标是 0-1,并且对应地理范围与标准 3857 不同
|
|
12
|
-
function forward(coors) {
|
|
13
|
-
// 墨卡托坐标
|
|
14
|
-
var coor_3857 = projector(coors);
|
|
15
|
-
// 墨卡托坐标转换到 0-1 区间,origin 对应 mapbox 0 0点
|
|
16
|
-
var mapbox_coor1 = Math.abs((coor_3857[0] - origin[0]) / (20037508.342789244 * 2));
|
|
17
|
-
var mapbox_coor2 = Math.abs((coor_3857[1] - origin[1]) / (20037508.342789244 * 2));
|
|
18
|
-
return [mapbox_coor1, mapbox_coor2];
|
|
19
|
-
}
|
|
20
|
-
var epsilon = 0.00000000001;
|
|
21
|
-
// 纹理uv坐标
|
|
22
|
-
var sourceUV = [
|
|
23
|
-
[0, 0],
|
|
24
|
-
[0, 1],
|
|
25
|
-
[1, 0],
|
|
26
|
-
[1, 1], // bottom-right
|
|
27
|
-
];
|
|
28
|
-
var arrugator = new Arrugator(forward, verts, sourceUV, [
|
|
29
|
-
[0, 1, 3],
|
|
30
|
-
[0, 3, 2],
|
|
31
|
-
]);
|
|
32
|
-
arrugator.lowerEpsilon(epsilon);
|
|
33
|
-
var arrugado = arrugator.output();
|
|
34
|
-
return {
|
|
35
|
-
pos: arrugado.projected.flat(),
|
|
36
|
-
uv: arrugado.uv.flat(),
|
|
37
|
-
trigs: arrugado.trigs.flat(), // 三角形索引
|
|
38
|
-
};
|
|
39
|
-
}
|
package/dist/es/utils/image.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* load image
|
|
3
|
-
* @param src
|
|
4
|
-
* @param crossOrigin
|
|
5
|
-
* @returns
|
|
6
|
-
*/
|
|
7
|
-
export function loadImage(src, crossOrigin) {
|
|
8
|
-
return new Promise(function (res, rej) {
|
|
9
|
-
var img = new Image();
|
|
10
|
-
img.crossOrigin = crossOrigin !== null && crossOrigin !== void 0 ? crossOrigin : '';
|
|
11
|
-
img.src = src;
|
|
12
|
-
img.onload = function () {
|
|
13
|
-
res(img);
|
|
14
|
-
};
|
|
15
|
-
img.onerror = function () {
|
|
16
|
-
rej('error');
|
|
17
|
-
};
|
|
18
|
-
});
|
|
19
|
-
}
|
package/dist/es/utils/webgl.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Create a shader object
|
|
3
|
-
* @param gl GL context
|
|
4
|
-
* @param type the type of the shader object to be created
|
|
5
|
-
* @param source shader program (string)
|
|
6
|
-
* @return created shader object, or null if the creation has failed.
|
|
7
|
-
*/
|
|
8
|
-
function createShader(gl, type, source) {
|
|
9
|
-
// Create shader object
|
|
10
|
-
var shader = gl.createShader(type);
|
|
11
|
-
if (shader == null) {
|
|
12
|
-
console.log('unable to create shader');
|
|
13
|
-
return null;
|
|
14
|
-
}
|
|
15
|
-
// Set the shader program
|
|
16
|
-
gl.shaderSource(shader, source);
|
|
17
|
-
// Compile the shader
|
|
18
|
-
gl.compileShader(shader);
|
|
19
|
-
// Check the result of compilation
|
|
20
|
-
var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
|
|
21
|
-
if (!compiled) {
|
|
22
|
-
var error = gl.getShaderInfoLog(shader);
|
|
23
|
-
console.log('Failed to compile shader: ' + error);
|
|
24
|
-
gl.deleteShader(shader);
|
|
25
|
-
return null;
|
|
26
|
-
}
|
|
27
|
-
return shader;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Create the linked program object
|
|
31
|
-
* @param gl GL context
|
|
32
|
-
* @param vshader a vertex shader program (string)
|
|
33
|
-
* @param fshader a fragment shader program (string)
|
|
34
|
-
* @return created program object, or null if the creation has failed
|
|
35
|
-
*/
|
|
36
|
-
export function createProgram(gl, vshader, fshader) {
|
|
37
|
-
// Create shader object
|
|
38
|
-
var vertexShader = createShader(gl, gl.VERTEX_SHADER, vshader);
|
|
39
|
-
var fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fshader);
|
|
40
|
-
if (!vertexShader || !fragmentShader) {
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
// Create a program object
|
|
44
|
-
var program = gl.createProgram();
|
|
45
|
-
if (!program) {
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
48
|
-
// Attach the shader objects
|
|
49
|
-
gl.attachShader(program, vertexShader);
|
|
50
|
-
gl.attachShader(program, fragmentShader);
|
|
51
|
-
// Link the program object
|
|
52
|
-
gl.linkProgram(program);
|
|
53
|
-
// Check the result of linking
|
|
54
|
-
var linked = gl.getProgramParameter(program, gl.LINK_STATUS);
|
|
55
|
-
if (!linked) {
|
|
56
|
-
var error = gl.getProgramInfoLog(program);
|
|
57
|
-
console.log('Failed to link program: ' + error);
|
|
58
|
-
gl.deleteProgram(program);
|
|
59
|
-
gl.deleteShader(fragmentShader);
|
|
60
|
-
gl.deleteShader(vertexShader);
|
|
61
|
-
return null;
|
|
62
|
-
}
|
|
63
|
-
return program;
|
|
64
|
-
}
|
package/dist/js/index.d.ts
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import mapboxgl from 'mapbox-gl';
|
|
2
|
-
import type { Coordinates } from './utils/arrugator';
|
|
3
|
-
export type { Coordinates } from './utils/arrugator';
|
|
4
|
-
export declare type MaskProperty = {
|
|
5
|
-
type?: 'in' | 'out';
|
|
6
|
-
data: GeoJSON.Polygon | GeoJSON.MultiPolygon;
|
|
7
|
-
};
|
|
8
|
-
export declare type ImageOption = {
|
|
9
|
-
url: string;
|
|
10
|
-
projection: string;
|
|
11
|
-
coordinates: Coordinates;
|
|
12
|
-
resampling?: 'linear' | 'nearest';
|
|
13
|
-
opacity?: number;
|
|
14
|
-
crossOrigin?: string;
|
|
15
|
-
mask?: MaskProperty;
|
|
16
|
-
};
|
|
17
|
-
export default class ImageLayer implements mapboxgl.CustomLayerInterface {
|
|
18
|
-
id: string;
|
|
19
|
-
type: 'custom';
|
|
20
|
-
renderingMode?: '2d' | '3d' | undefined;
|
|
21
|
-
private option;
|
|
22
|
-
private map?;
|
|
23
|
-
private gl?;
|
|
24
|
-
private loaded;
|
|
25
|
-
private arrugado;
|
|
26
|
-
private programInfo?;
|
|
27
|
-
private bufferInfo?;
|
|
28
|
-
private texture?;
|
|
29
|
-
private stencilChecked;
|
|
30
|
-
private maskProperty;
|
|
31
|
-
private maskProgramInfo?;
|
|
32
|
-
private maskBufferInfo?;
|
|
33
|
-
constructor(id: string, option: ImageOption);
|
|
34
|
-
onAdd(map: mapboxgl.Map, gl: WebGLRenderingContext): void;
|
|
35
|
-
onRemove(map: mapboxgl.Map, gl: WebGLRenderingContext): void;
|
|
36
|
-
render(gl: WebGLRenderingContext, matrix: number[]): void;
|
|
37
|
-
/**
|
|
38
|
-
* Updates the URL, the projection, the coordinates, the opacity or the resampling of the image.
|
|
39
|
-
* @param {Object} option Options object.
|
|
40
|
-
* @param {string} [option.url] Image URL.
|
|
41
|
-
* @param {string} [option.projection] Projection with EPSG code that points to the image..
|
|
42
|
-
* @param {Array<Array<number>>} [option.coordinates] Four geographical coordinates,
|
|
43
|
-
* @param {number} [option.opacity] opacity of the image.
|
|
44
|
-
* @param {string} [option.resampling] The resampling/interpolation method to use for overscaling.
|
|
45
|
-
*/
|
|
46
|
-
updateImage(option: {
|
|
47
|
-
url?: string;
|
|
48
|
-
projection?: string;
|
|
49
|
-
coordinates?: Coordinates;
|
|
50
|
-
opacity?: number;
|
|
51
|
-
resampling?: 'linear' | 'nearest';
|
|
52
|
-
}): this;
|
|
53
|
-
/**
|
|
54
|
-
* Updates the mask property
|
|
55
|
-
* @param {MaskProperty} mask The mask property.
|
|
56
|
-
*/
|
|
57
|
-
updateMask(mask: Partial<MaskProperty>): this;
|
|
58
|
-
private loadTexture;
|
|
59
|
-
private getMaskBufferInfo;
|
|
60
|
-
}
|
package/dist/js/index.js
DELETED
|
@@ -1,256 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
var compare_versions_1 = require("compare-versions");
|
|
30
|
-
var earcut_1 = __importDefault(require("earcut"));
|
|
31
|
-
var mapbox_gl_1 = __importDefault(require("mapbox-gl"));
|
|
32
|
-
var twgl = __importStar(require("twgl.js"));
|
|
33
|
-
var image_fragment_glsl_1 = __importDefault(require("./shaders/image.fragment.glsl"));
|
|
34
|
-
var image_vertex_glsl_1 = __importDefault(require("./shaders/image.vertex.glsl"));
|
|
35
|
-
var mask_fragment_glsl_1 = __importDefault(require("./shaders/mask.fragment.glsl"));
|
|
36
|
-
var mask_vertex_glsl_1 = __importDefault(require("./shaders/mask.vertex.glsl"));
|
|
37
|
-
var arrugator_1 = require("./utils/arrugator");
|
|
38
|
-
var ImageLayer = /** @class */ (function () {
|
|
39
|
-
function ImageLayer(id, option) {
|
|
40
|
-
this.type = 'custom';
|
|
41
|
-
this.renderingMode = '2d';
|
|
42
|
-
// mask
|
|
43
|
-
this.stencilChecked = true; // resetStencilClippingMasks 版本检查
|
|
44
|
-
this.id = id;
|
|
45
|
-
this.option = option;
|
|
46
|
-
this.loaded = false;
|
|
47
|
-
this.maskProperty = Object.assign({ type: 'in' }, option.mask);
|
|
48
|
-
// 检查 stencil 是否可用
|
|
49
|
-
this.stencilChecked = (0, compare_versions_1.satisfies)(mapbox_gl_1.default.version, '>=2.7.0');
|
|
50
|
-
// 如果传了 mask 边界数据,且版本不符
|
|
51
|
-
if (this.maskProperty.data && !this.stencilChecked) {
|
|
52
|
-
throw new Error("\u5982\u679C\u9700\u8981\u906E\u7F69\uFF08\u63A9\u819C\uFF09\uFF0Cmapbox-gl \u7248\u672C\u5FC5\u987B\uFF1A>=2.7.0");
|
|
53
|
-
}
|
|
54
|
-
// 初始化 Arrugator
|
|
55
|
-
var projection = option.projection, coordinates = option.coordinates;
|
|
56
|
-
this.arrugado = (0, arrugator_1.initArrugator)(projection, coordinates);
|
|
57
|
-
// this._map = null
|
|
58
|
-
// this._gl = null
|
|
59
|
-
// this._program = null
|
|
60
|
-
// this._texture = null
|
|
61
|
-
// this._positionBuffer = null
|
|
62
|
-
// this._uvBuffer = null
|
|
63
|
-
// this._verticesIndexBuffer = null
|
|
64
|
-
}
|
|
65
|
-
ImageLayer.prototype.onAdd = function (map, gl) {
|
|
66
|
-
this.map = map;
|
|
67
|
-
this.gl = gl;
|
|
68
|
-
// 主程序
|
|
69
|
-
this.programInfo = twgl.createProgramInfo(gl, [image_vertex_glsl_1.default, image_fragment_glsl_1.default]);
|
|
70
|
-
this.loadTexture(map, gl);
|
|
71
|
-
this.bufferInfo = twgl.createBufferInfoFromArrays(gl, {
|
|
72
|
-
a_pos: { numComponents: 2, data: this.arrugado.pos },
|
|
73
|
-
a_uv: { numComponents: 2, data: this.arrugado.uv },
|
|
74
|
-
indices: this.arrugado.trigs,
|
|
75
|
-
});
|
|
76
|
-
// 掩膜程序
|
|
77
|
-
if (this.maskProperty.data) {
|
|
78
|
-
var data = this.maskProperty.data;
|
|
79
|
-
if (data) {
|
|
80
|
-
this.maskProgramInfo = twgl.createProgramInfo(gl, [mask_vertex_glsl_1.default, mask_fragment_glsl_1.default]);
|
|
81
|
-
this.maskBufferInfo = this.getMaskBufferInfo(gl, data);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
ImageLayer.prototype.onRemove = function (map, gl) {
|
|
86
|
-
if (this.programInfo) {
|
|
87
|
-
gl.deleteProgram(this.programInfo.program);
|
|
88
|
-
}
|
|
89
|
-
if (this.maskProgramInfo) {
|
|
90
|
-
gl.deleteProgram(this.maskProgramInfo.program);
|
|
91
|
-
}
|
|
92
|
-
if (this.texture) {
|
|
93
|
-
gl.deleteTexture(this.texture);
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
ImageLayer.prototype.render = function (gl, matrix) {
|
|
97
|
-
/**
|
|
98
|
-
* 线图层在启用 stencil 会消失,参考: https://github.com/mapbox/mapbox-gl-js/issues/12213
|
|
99
|
-
* 临时解决方案: map.painter.resetStencilClippingMasks()
|
|
100
|
-
* 该方法在 mapboxgl version >=2.7.0 才能用
|
|
101
|
-
*/
|
|
102
|
-
var _a;
|
|
103
|
-
if (this.stencilChecked) {
|
|
104
|
-
// @ts-ignore
|
|
105
|
-
this.map.painter.resetStencilClippingMasks();
|
|
106
|
-
}
|
|
107
|
-
if (this.loaded && this.programInfo && this.bufferInfo) {
|
|
108
|
-
// blend
|
|
109
|
-
gl.enable(gl.BLEND);
|
|
110
|
-
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
111
|
-
if (this.maskProgramInfo && this.maskBufferInfo) {
|
|
112
|
-
// mask program
|
|
113
|
-
gl.useProgram(this.maskProgramInfo.program);
|
|
114
|
-
// stencil test
|
|
115
|
-
gl.enable(gl.STENCIL_TEST);
|
|
116
|
-
gl.stencilFunc(gl.ALWAYS, 1, 0xff);
|
|
117
|
-
gl.stencilOp(gl.REPLACE, gl.REPLACE, gl.REPLACE);
|
|
118
|
-
gl.stencilMask(0xff);
|
|
119
|
-
gl.clear(gl.STENCIL_BUFFER_BIT);
|
|
120
|
-
// matrix
|
|
121
|
-
twgl.setUniforms(this.maskProgramInfo, { u_matrix: matrix });
|
|
122
|
-
// pos & indices
|
|
123
|
-
twgl.setBuffersAndAttributes(gl, this.maskProgramInfo, this.maskBufferInfo);
|
|
124
|
-
// draw
|
|
125
|
-
var elementType = gl.UNSIGNED_SHORT;
|
|
126
|
-
if (this.maskBufferInfo.numElements / 3 > 65535) {
|
|
127
|
-
// 使 drawElements 支持 UNSIGNED_INT 类型
|
|
128
|
-
gl.getExtension('OES_element_index_uint');
|
|
129
|
-
elementType = gl.UNSIGNED_INT;
|
|
130
|
-
}
|
|
131
|
-
gl.drawElements(gl.TRIANGLES, this.maskBufferInfo.numElements, elementType, 0);
|
|
132
|
-
}
|
|
133
|
-
// texture program
|
|
134
|
-
gl.useProgram(this.programInfo.program);
|
|
135
|
-
if ((_a = this.maskProgramInfo) === null || _a === void 0 ? void 0 : _a.program) {
|
|
136
|
-
// stencil test
|
|
137
|
-
var ref = this.maskProperty.type === 'out' ? 0 : 1;
|
|
138
|
-
gl.stencilFunc(gl.EQUAL, ref, 0xff);
|
|
139
|
-
gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
|
|
140
|
-
}
|
|
141
|
-
// uniforms
|
|
142
|
-
twgl.setUniforms(this.programInfo, {
|
|
143
|
-
u_matrix: matrix,
|
|
144
|
-
u_opacity: this.option.opacity || 1,
|
|
145
|
-
u_sampler: this.texture,
|
|
146
|
-
});
|
|
147
|
-
// pos, uv & indices
|
|
148
|
-
twgl.setBuffersAndAttributes(gl, this.programInfo, this.bufferInfo);
|
|
149
|
-
// draw
|
|
150
|
-
gl.drawElements(gl.TRIANGLES, this.arrugado.trigs.length, gl.UNSIGNED_SHORT, 0);
|
|
151
|
-
gl.clear(gl.STENCIL_BUFFER_BIT);
|
|
152
|
-
gl.disable(gl.STENCIL_TEST);
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
/**
|
|
156
|
-
* Updates the URL, the projection, the coordinates, the opacity or the resampling of the image.
|
|
157
|
-
* @param {Object} option Options object.
|
|
158
|
-
* @param {string} [option.url] Image URL.
|
|
159
|
-
* @param {string} [option.projection] Projection with EPSG code that points to the image..
|
|
160
|
-
* @param {Array<Array<number>>} [option.coordinates] Four geographical coordinates,
|
|
161
|
-
* @param {number} [option.opacity] opacity of the image.
|
|
162
|
-
* @param {string} [option.resampling] The resampling/interpolation method to use for overscaling.
|
|
163
|
-
*/
|
|
164
|
-
ImageLayer.prototype.updateImage = function (option) {
|
|
165
|
-
var _a, _b, _c, _d, _e;
|
|
166
|
-
if (this.gl && this.map) {
|
|
167
|
-
this.option.opacity = (_a = option.opacity) !== null && _a !== void 0 ? _a : this.option.opacity;
|
|
168
|
-
if (option.projection || option.coordinates) {
|
|
169
|
-
this.option.projection = (_b = option.projection) !== null && _b !== void 0 ? _b : this.option.projection;
|
|
170
|
-
this.option.coordinates = (_c = option.coordinates) !== null && _c !== void 0 ? _c : this.option.coordinates;
|
|
171
|
-
// reinit arrugator
|
|
172
|
-
this.arrugado = (0, arrugator_1.initArrugator)(this.option.projection, this.option.coordinates);
|
|
173
|
-
this.bufferInfo = twgl.createBufferInfoFromArrays(this.gl, {
|
|
174
|
-
a_pos: { numComponents: 2, data: this.arrugado.pos },
|
|
175
|
-
a_uv: { numComponents: 2, data: this.arrugado.uv },
|
|
176
|
-
indices: this.arrugado.trigs,
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
if (option.url || option.resampling) {
|
|
180
|
-
this.loaded = false;
|
|
181
|
-
this.option.url = (_d = option.url) !== null && _d !== void 0 ? _d : this.option.url;
|
|
182
|
-
this.option.resampling = (_e = option.resampling) !== null && _e !== void 0 ? _e : this.option.resampling;
|
|
183
|
-
// reload image
|
|
184
|
-
this.loadTexture(this.map, this.gl);
|
|
185
|
-
}
|
|
186
|
-
else {
|
|
187
|
-
this.map.triggerRepaint();
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
return this;
|
|
191
|
-
};
|
|
192
|
-
/**
|
|
193
|
-
* Updates the mask property
|
|
194
|
-
* @param {MaskProperty} mask The mask property.
|
|
195
|
-
*/
|
|
196
|
-
ImageLayer.prototype.updateMask = function (mask) {
|
|
197
|
-
if (this.gl && this.map && this.maskProgramInfo) {
|
|
198
|
-
this.maskProperty = Object.assign(this.maskProperty, mask);
|
|
199
|
-
this.maskBufferInfo = this.getMaskBufferInfo(this.gl, this.maskProperty.data);
|
|
200
|
-
this.map.triggerRepaint();
|
|
201
|
-
}
|
|
202
|
-
return this;
|
|
203
|
-
};
|
|
204
|
-
ImageLayer.prototype.loadTexture = function (map, gl) {
|
|
205
|
-
var _this = this;
|
|
206
|
-
// 创建纹理
|
|
207
|
-
var filter = this.option.resampling === 'nearest' ? gl.NEAREST : gl.LINEAR;
|
|
208
|
-
this.texture = twgl.createTexture(gl, {
|
|
209
|
-
src: this.option.url,
|
|
210
|
-
crossOrigin: this.option.crossOrigin,
|
|
211
|
-
minMag: filter,
|
|
212
|
-
flipY: 0,
|
|
213
|
-
}, function () {
|
|
214
|
-
_this.loaded = true;
|
|
215
|
-
map.triggerRepaint();
|
|
216
|
-
});
|
|
217
|
-
};
|
|
218
|
-
ImageLayer.prototype.getMaskBufferInfo = function (gl, data) {
|
|
219
|
-
var positions = [];
|
|
220
|
-
var triangles = [];
|
|
221
|
-
if (data.type === 'MultiPolygon') {
|
|
222
|
-
// type: 'MultiPolygon'
|
|
223
|
-
var polyCount = data.coordinates.length;
|
|
224
|
-
var triangleStartIndex_1 = 0;
|
|
225
|
-
for (var i = 0; i < polyCount; i++) {
|
|
226
|
-
var coordinates = data.coordinates[i];
|
|
227
|
-
var flatten = earcut_1.default.flatten(coordinates);
|
|
228
|
-
var vertices = flatten.vertices, holes = flatten.holes, dimensions = flatten.dimensions;
|
|
229
|
-
var triangle = (0, earcut_1.default)(vertices, holes, dimensions);
|
|
230
|
-
var triangleNew = triangle.map(function (item) { return item + triangleStartIndex_1; });
|
|
231
|
-
triangleStartIndex_1 += vertices.length / 2;
|
|
232
|
-
// positions.push(...vertices)
|
|
233
|
-
// triangles.push(...triangleNew)
|
|
234
|
-
for (var m = 0; m < vertices.length; m++) {
|
|
235
|
-
positions.push(vertices[m]);
|
|
236
|
-
}
|
|
237
|
-
for (var n = 0; n < triangleNew.length; n++) {
|
|
238
|
-
triangles.push(triangleNew[n]);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
else {
|
|
243
|
-
// type: 'Polygon'
|
|
244
|
-
var flatten = earcut_1.default.flatten(data.coordinates);
|
|
245
|
-
var vertices = flatten.vertices, holes = flatten.holes, dimensions = flatten.dimensions;
|
|
246
|
-
positions = vertices;
|
|
247
|
-
triangles = (0, earcut_1.default)(vertices, holes, dimensions);
|
|
248
|
-
}
|
|
249
|
-
return twgl.createBufferInfoFromArrays(gl, {
|
|
250
|
-
a_pos: { numComponents: 2, data: positions },
|
|
251
|
-
indices: triangles.length / 3 > 65535 ? new Uint32Array(triangles) : new Uint16Array(triangles),
|
|
252
|
-
});
|
|
253
|
-
};
|
|
254
|
-
return ImageLayer;
|
|
255
|
-
}());
|
|
256
|
-
exports.default = ImageLayer;
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
declare const _default: "\n#ifdef GL_ES\n precision highp int;\n precision mediump float;\n#endif\nuniform sampler2D u_sampler;\nuniform float u_opacity;\nvarying vec2 v_uv;\nvoid main() {\n vec4 color = texture2D(u_sampler, v_uv);\n gl_FragColor = color * u_opacity;\n}\n";
|
|
2
|
-
export default _default;
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = "\n#ifdef GL_ES\n precision highp int;\n precision mediump float;\n#endif\nuniform sampler2D u_sampler;\nuniform float u_opacity;\nvarying vec2 v_uv;\nvoid main() {\n vec4 color = texture2D(u_sampler, v_uv);\n gl_FragColor = color * u_opacity;\n}\n";
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = "\nuniform mat4 u_matrix;\nattribute vec2 a_pos;\nattribute vec2 a_uv;\nvarying vec2 v_uv;\nvoid main() {\n gl_Position = u_matrix * vec4(a_pos, 0.0, 1.0);\n v_uv = a_uv;\n}\n";
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
declare const _default: "\nuniform mat4 u_matrix;\nuniform float u_alt;\nattribute vec2 a_pos;\n\nconst float PI = 3.1415926536;\nconst float earthRadius = 6371008.8;\nconst float earthCircumference = 2.0 * PI * earthRadius; // meters\n\nfloat circumferenceAtLatitude(float latitude) {\n return earthCircumference * cos(latitude * PI / 180.0);\n}\n\nfloat mercatorXfromLng(float lng) {\n return (180.0 + lng) / 360.0;\n}\n\nfloat mercatorYfromLat(float lat) {\n return (180.0 - (180.0 / PI * log(tan(PI / 4.0 + lat * PI / 360.0)))) / 360.0;\n}\n\nfloat mercatorZfromAltitude(float altitude, float lat) {\n return altitude / circumferenceAtLatitude(lat);\n}\n\nvec3 mercatorfromLngLat(vec2 lnglat, float alt) {\n return vec3(mercatorXfromLng(lnglat.x), mercatorYfromLat(lnglat.y), mercatorZfromAltitude(alt, lnglat.y));\n}\n\nvoid main() {\n vec3 mercator = mercatorfromLngLat(a_pos, 0.0);\n gl_Position = u_matrix * vec4(mercator, 1.0);\n // gl_Position = u_matrix * vec4(a_pos, 0.0, 1.0);\n}\n";
|
|
2
|
-
export default _default;
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = "\nuniform mat4 u_matrix;\nuniform float u_alt;\nattribute vec2 a_pos;\n\nconst float PI = 3.1415926536;\nconst float earthRadius = 6371008.8;\nconst float earthCircumference = 2.0 * PI * earthRadius; // meters\n\nfloat circumferenceAtLatitude(float latitude) {\n return earthCircumference * cos(latitude * PI / 180.0);\n}\n\nfloat mercatorXfromLng(float lng) {\n return (180.0 + lng) / 360.0;\n}\n\nfloat mercatorYfromLat(float lat) {\n return (180.0 - (180.0 / PI * log(tan(PI / 4.0 + lat * PI / 360.0)))) / 360.0;\n}\n\nfloat mercatorZfromAltitude(float altitude, float lat) {\n return altitude / circumferenceAtLatitude(lat);\n}\n\nvec3 mercatorfromLngLat(vec2 lnglat, float alt) {\n return vec3(mercatorXfromLng(lnglat.x), mercatorYfromLat(lnglat.y), mercatorZfromAltitude(alt, lnglat.y));\n}\n\nvoid main() {\n vec3 mercator = mercatorfromLngLat(a_pos, 0.0);\n gl_Position = u_matrix * vec4(mercator, 1.0);\n // gl_Position = u_matrix * vec4(a_pos, 0.0, 1.0);\n}\n";
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export declare type Coordinates = [[number, number], [number, number], [number, number], [number, number]];
|
|
2
|
-
export declare type ArrugadoFlat = {
|
|
3
|
-
pos: number[];
|
|
4
|
-
uv: number[];
|
|
5
|
-
trigs: number[];
|
|
6
|
-
};
|
|
7
|
-
export declare function initArrugator(fromProj: string, coordinates: Coordinates): ArrugadoFlat;
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.initArrugator = void 0;
|
|
7
|
-
// @ts-ignore
|
|
8
|
-
var arrugator_1 = __importDefault(require("arrugator"));
|
|
9
|
-
var proj4_1 = __importDefault(require("proj4"));
|
|
10
|
-
function initArrugator(fromProj, coordinates) {
|
|
11
|
-
// 墨卡托投影的左上角坐标,对应 mapbox 左上角起始坐标 [0,0]
|
|
12
|
-
var origin = [-20037508.342789244, 20037508.342789244];
|
|
13
|
-
// 坐标转换为 Arrugator 坐标 top-left, top-left, top-left, top-left)
|
|
14
|
-
var verts = [coordinates[0], coordinates[3], coordinates[1], coordinates[2]];
|
|
15
|
-
// 转换为 EPSG:3857
|
|
16
|
-
var projector = (0, proj4_1.default)(fromProj, 'EPSG:3857').forward;
|
|
17
|
-
// 改写坐标转换函数,因为 mapbox 的墨卡托坐标是 0-1,并且对应地理范围与标准 3857 不同
|
|
18
|
-
function forward(coors) {
|
|
19
|
-
// 墨卡托坐标
|
|
20
|
-
var coor_3857 = projector(coors);
|
|
21
|
-
// 墨卡托坐标转换到 0-1 区间,origin 对应 mapbox 0 0点
|
|
22
|
-
var mapbox_coor1 = Math.abs((coor_3857[0] - origin[0]) / (20037508.342789244 * 2));
|
|
23
|
-
var mapbox_coor2 = Math.abs((coor_3857[1] - origin[1]) / (20037508.342789244 * 2));
|
|
24
|
-
return [mapbox_coor1, mapbox_coor2];
|
|
25
|
-
}
|
|
26
|
-
var epsilon = 0.00000000001;
|
|
27
|
-
// 纹理uv坐标
|
|
28
|
-
var sourceUV = [
|
|
29
|
-
[0, 0],
|
|
30
|
-
[0, 1],
|
|
31
|
-
[1, 0],
|
|
32
|
-
[1, 1], // bottom-right
|
|
33
|
-
];
|
|
34
|
-
var arrugator = new arrugator_1.default(forward, verts, sourceUV, [
|
|
35
|
-
[0, 1, 3],
|
|
36
|
-
[0, 3, 2],
|
|
37
|
-
]);
|
|
38
|
-
arrugator.lowerEpsilon(epsilon);
|
|
39
|
-
var arrugado = arrugator.output();
|
|
40
|
-
return {
|
|
41
|
-
pos: arrugado.projected.flat(),
|
|
42
|
-
uv: arrugado.uv.flat(),
|
|
43
|
-
trigs: arrugado.trigs.flat(), // 三角形索引
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
exports.initArrugator = initArrugator;
|
package/dist/js/utils/image.d.ts
DELETED
package/dist/js/utils/image.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.loadImage = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* load image
|
|
6
|
-
* @param src
|
|
7
|
-
* @param crossOrigin
|
|
8
|
-
* @returns
|
|
9
|
-
*/
|
|
10
|
-
function loadImage(src, crossOrigin) {
|
|
11
|
-
return new Promise(function (res, rej) {
|
|
12
|
-
var img = new Image();
|
|
13
|
-
img.crossOrigin = crossOrigin !== null && crossOrigin !== void 0 ? crossOrigin : '';
|
|
14
|
-
img.src = src;
|
|
15
|
-
img.onload = function () {
|
|
16
|
-
res(img);
|
|
17
|
-
};
|
|
18
|
-
img.onerror = function () {
|
|
19
|
-
rej('error');
|
|
20
|
-
};
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
exports.loadImage = loadImage;
|
package/dist/js/utils/webgl.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Create the linked program object
|
|
3
|
-
* @param gl GL context
|
|
4
|
-
* @param vshader a vertex shader program (string)
|
|
5
|
-
* @param fshader a fragment shader program (string)
|
|
6
|
-
* @return created program object, or null if the creation has failed
|
|
7
|
-
*/
|
|
8
|
-
export declare function createProgram(gl: WebGLRenderingContext, vshader: string, fshader: string): WebGLProgram | null;
|
package/dist/js/utils/webgl.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createProgram = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Create a shader object
|
|
6
|
-
* @param gl GL context
|
|
7
|
-
* @param type the type of the shader object to be created
|
|
8
|
-
* @param source shader program (string)
|
|
9
|
-
* @return created shader object, or null if the creation has failed.
|
|
10
|
-
*/
|
|
11
|
-
function createShader(gl, type, source) {
|
|
12
|
-
// Create shader object
|
|
13
|
-
var shader = gl.createShader(type);
|
|
14
|
-
if (shader == null) {
|
|
15
|
-
console.log('unable to create shader');
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
// Set the shader program
|
|
19
|
-
gl.shaderSource(shader, source);
|
|
20
|
-
// Compile the shader
|
|
21
|
-
gl.compileShader(shader);
|
|
22
|
-
// Check the result of compilation
|
|
23
|
-
var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
|
|
24
|
-
if (!compiled) {
|
|
25
|
-
var error = gl.getShaderInfoLog(shader);
|
|
26
|
-
console.log('Failed to compile shader: ' + error);
|
|
27
|
-
gl.deleteShader(shader);
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
return shader;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Create the linked program object
|
|
34
|
-
* @param gl GL context
|
|
35
|
-
* @param vshader a vertex shader program (string)
|
|
36
|
-
* @param fshader a fragment shader program (string)
|
|
37
|
-
* @return created program object, or null if the creation has failed
|
|
38
|
-
*/
|
|
39
|
-
function createProgram(gl, vshader, fshader) {
|
|
40
|
-
// Create shader object
|
|
41
|
-
var vertexShader = createShader(gl, gl.VERTEX_SHADER, vshader);
|
|
42
|
-
var fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fshader);
|
|
43
|
-
if (!vertexShader || !fragmentShader) {
|
|
44
|
-
return null;
|
|
45
|
-
}
|
|
46
|
-
// Create a program object
|
|
47
|
-
var program = gl.createProgram();
|
|
48
|
-
if (!program) {
|
|
49
|
-
return null;
|
|
50
|
-
}
|
|
51
|
-
// Attach the shader objects
|
|
52
|
-
gl.attachShader(program, vertexShader);
|
|
53
|
-
gl.attachShader(program, fragmentShader);
|
|
54
|
-
// Link the program object
|
|
55
|
-
gl.linkProgram(program);
|
|
56
|
-
// Check the result of linking
|
|
57
|
-
var linked = gl.getProgramParameter(program, gl.LINK_STATUS);
|
|
58
|
-
if (!linked) {
|
|
59
|
-
var error = gl.getProgramInfoLog(program);
|
|
60
|
-
console.log('Failed to link program: ' + error);
|
|
61
|
-
gl.deleteProgram(program);
|
|
62
|
-
gl.deleteShader(fragmentShader);
|
|
63
|
-
gl.deleteShader(vertexShader);
|
|
64
|
-
return null;
|
|
65
|
-
}
|
|
66
|
-
return program;
|
|
67
|
-
}
|
|
68
|
-
exports.createProgram = createProgram;
|