fix(merge branch): fix confilt

This commit is contained in:
thinkinggis 2019-10-25 10:25:54 +08:00
commit f223f2965c
78 changed files with 3253 additions and 2126 deletions

View File

@ -28,10 +28,10 @@ ClearPass -> PixelPickingPass -> RenderPass -> [ ...其他后处理 Pass ] -> Co
```
PixelPickingPass 分解步骤如下:
1. 逐要素编码idx -> color传入 attributes 渲染 Layer 到纹理。
1. `ENCODE` 阶段。逐要素编码idx -> color传入 attributes 渲染 Layer 到纹理。
2. 获取鼠标在视口中的位置。由于目前 L7 与地图结合的方案为双 Canvas 而非共享 WebGL Context事件监听注册在地图底图上。
3. 读取纹理在指定位置的颜色进行解码color -> idx),查找对应要素,作为 Layer `onHover/onClick` 回调参数传入。
4. (可选)将待高亮要素对应的颜色传入 Vertex Shader 用于每个 Vertex 判断自身是否被选中,如果被选中,在 Fragment Shader 中将高亮颜色与计算颜色混合。
4. `HIGHLIGHT` 阶段(可选)将待高亮要素对应的颜色传入 Vertex Shader 用于每个 Vertex 判断自身是否被选中,如果被选中,在 Fragment Shader 中将高亮颜色与计算颜色混合。
## 使用方法
@ -69,7 +69,8 @@ const layer = new PolygonLayer({
```typescript
const layer = new PolygonLayer({
enablePicking: true, // 开启拾取
highlightColor: 'red', // 设置高亮颜色
enableHighlight: true, // 开启高亮
highlightColor: [0, 0, 1, 1], // 设置高亮颜色为蓝色
});
```
@ -132,7 +133,7 @@ void main() {
void main() {
// 必须在末尾,保证后续不会再对 gl_FragColor 进行修改
gl_FragColor = highlightPickingColor(gl_FragColor);
gl_FragColor = filterPickingColor(gl_FragColor);
}
```
@ -141,7 +142,7 @@ void main() {
| 方法名 | 应用 shader | 说明 |
| -------- | --- | ------------- |
| `setPickingColor` | `vertex` | 比较自身颜色编码与高亮颜色,判断是否被选中,传递结果给 fragment |
| `highlightPickingColor` | `fragment` | 当前 fragment 被选中则使用高亮颜色混合,否则直接输出原始计算结果 |
| `filterPickingColor` | `fragment` | 当前 fragment 被选中则使用高亮颜色混合,否则直接输出原始计算结果 |
## 参考资料

View File

@ -44,7 +44,7 @@ export default class Marker {
this.mapservice = mapService;
mapService.getMarkerContainer().appendChild(element as HTMLElement);
mapService.on('camerachange', this.update);
this.setDraggable(draggable);
// this.setDraggable(draggable);
this.update();
return this;
}

View File

@ -1,5 +1,4 @@
import container, { lazyInject } from './inversify.config';
import IconService from './services/asset/IconService';
import ClearPass from './services/renderer/passes/ClearPass';
import MultiPassRenderer from './services/renderer/passes/MultiPassRenderer';
import PixelPickingPass from './services/renderer/passes/PixelPickingPass';
@ -28,7 +27,6 @@ export {
* Service
*/
SceneService,
IconService,
packCircleVertex,
/** pass */
MultiPassRenderer,
@ -41,8 +39,8 @@ export {
};
/** 暴露服务接口供其他 packages 实现 */
export * from './services/layer/ILayerStyleService';
export * from './services/layer/ILayerService';
export * from './services/layer/IStyleAttributeService';
export * from './services/source/ISourceService';
export * from './services/map/IMapService';
export * from './services/coordinate/ICoordinateSystemService';
@ -56,6 +54,7 @@ export * from './services/asset/IFontService';
export * from './services/component/IControlService';
export * from './services/component/IMarkerService';
export * from './services/component/IPopUpService';
export * from './services/log/ILogService';
/** 全部渲染服务接口 */
export * from './services/renderer/IAttribute';

View File

@ -14,6 +14,7 @@ import { IGlobalConfigService } from './services/config/IConfigService';
import { ICoordinateSystemService } from './services/coordinate/ICoordinateSystemService';
import { IInteractionService } from './services/interaction/IInteractionService';
import { ILayerService } from './services/layer/ILayerService';
import { IStyleAttributeService } from './services/layer/IStyleAttributeService';
import { ILogService } from './services/log/ILogService';
import { IShaderModuleService } from './services/shader/IShaderModuleService';
@ -26,7 +27,7 @@ import GlobalConfigService from './services/config/ConfigService';
import CoordinateSystemService from './services/coordinate/CoordinateSystemService';
import InteractionService from './services/interaction/InteractionService';
import LayerService from './services/layer/LayerService';
import LayerStyleService from './services/layer/LayerStyleService';
import StyleAttributeService from './services/layer/StyleAttributeService';
import LogService from './services/log/LogService';
import ShaderModuleService from './services/shader/ShaderModuleService';
// @see https://github.com/inversify/InversifyJS/blob/master/wiki/container_api.md#defaultscope
@ -43,6 +44,9 @@ container
.bind<ILayerService>(TYPES.ILayerService)
.to(LayerService)
.inSingletonScope();
container
.bind<IStyleAttributeService>(TYPES.IStyleAttributeService)
.to(StyleAttributeService);
container
.bind<ICameraService>(TYPES.ICameraService)
.to(CameraService)
@ -79,9 +83,9 @@ container
// @see https://github.com/inversify/InversifyJS/blob/master/wiki/inheritance.md#what-can-i-do-when-my-base-class-is-provided-by-a-third-party-module
decorate(injectable(), EventEmitter);
// 支持 L7 使用 new 而非容器实例化的场景
// @see https://github.com/inversify/inversify-inject-decorators
const DECORATORS = getDecorators(container);
// 支持 L7 使用 new 而非容器实例化的场景,同时禁止 lazyInject cache
// @see https://github.com/inversify/inversify-inject-decorators#caching-vs-non-caching-behaviour
const DECORATORS = getDecorators(container, false);
interface IBabelPropertyDescriptor extends PropertyDescriptor {
initializer(): any;

View File

@ -23,6 +23,7 @@ const defaultGlobalConfig: Partial<IGlobalConfig> = {
],
size: 10000,
shape: 'circle',
scales: {},
};
@injectable()
@ -38,7 +39,8 @@ export default class GlobalConfigService implements IGlobalConfigService {
...this.config,
...config,
};
// TODO: validate config
// TODO: validate config with JSON schema
// @see https://github.com/webpack/schema-utils
return true;
}

View File

@ -1,4 +1,13 @@
export enum InteractionEvent {
Hover = 'hover',
Click = 'click',
}
export interface IInteractionService {
init(): void;
destroy(): void;
on(
eventName: InteractionEvent,
callback: (params: { x: number; y: number }) => void,
): void;
}

View File

@ -1,14 +1,20 @@
import EventEmitter from 'eventemitter3';
import Hammer from 'hammerjs';
import { inject, injectable } from 'inversify';
import { TYPES } from '../../types';
import { ILogService } from '../log/ILogService';
import { IRendererService } from '../renderer/IRendererService';
import { IInteractionService } from './IInteractionService';
import { IMapService } from '../map/IMapService';
import { IInteractionService, InteractionEvent } from './IInteractionService';
/**
* L7 canvas WebGL Context
* L7 canvas
*/
@injectable()
export default class InteractionService implements IInteractionService {
@inject(TYPES.IRendererService)
private readonly rendererService: IRendererService;
export default class InteractionService extends EventEmitter
implements IInteractionService {
@inject(TYPES.IMapService)
private readonly mapService: IMapService;
@inject(TYPES.ILogService)
private readonly logger: ILogService;
@ -16,45 +22,46 @@ export default class InteractionService implements IInteractionService {
private hammertime: HammerManager;
public init() {
const $containter = this.rendererService.getContainer();
if ($containter) {
const hammertime = new Hammer($containter);
hammertime.get('pan').set({ direction: Hammer.DIRECTION_ALL });
hammertime.get('pinch').set({ enable: true });
// hammertime.on('panstart', this.onPanstart);
hammertime.on('panmove', this.onPanmove);
// hammertime.on('panend', this.onPanend);
// hammertime.on('pinch', this.onPinch);
// $containter.addEventListener('wheel', this.onMousewheel);
this.hammertime = hammertime;
}
// 注册事件在地图底图上
this.addEventListenerOnMap();
}
public destroy() {
if (this.hammertime) {
this.hammertime.destroy();
}
const $containter = this.rendererService.getContainer();
this.removeEventListenerOnMap();
this.off(InteractionEvent.Hover);
}
private addEventListenerOnMap() {
const $containter = this.mapService.getMapContainer();
if ($containter) {
// $containter.removeEventListener('wheel', this.onMousewheel);
const hammertime = new Hammer($containter);
hammertime.get('pan').set({ direction: Hammer.DIRECTION_ALL });
hammertime.get('pinch').set({ enable: true });
// hammertime.on('panstart', this.onPanstart);
// hammertime.on('panmove', this.onPanmove);
// hammertime.on('panend', this.onPanend);
// hammertime.on('pinch', this.onPinch);
$containter.addEventListener('mousemove', this.onHover);
this.hammertime = hammertime;
// TODO: 根据场景注册事件到 L7 canvas 上
this.logger.info('add event listeners on canvas');
}
}
private onPanmove = (e: HammerInput) => {
// @ts-ignore
// this.logger.info(e);
// if (this.isMoving) {
// this.deltaX = e.center.x - this.lastX;
// this.deltaY = e.center.y - this.lastY;
// this.lastX = e.center.x;
// this.lastY = e.center.y;
// this.emit(Mouse.MOVE_EVENT, {
// deltaX: this.deltaX,
// deltaY: this.deltaY,
// deltaZ: this.deltaZ
// });
// }
private removeEventListenerOnMap() {
const $containter = this.mapService.getMapContainer();
if ($containter) {
$containter.removeEventListener('mousemove', this.onHover);
}
}
private onHover = ({ x, y }: MouseEvent) => {
this.emit(InteractionEvent.Hover, { x, y });
};
}

View File

@ -2,62 +2,33 @@ import { AsyncParallelHook, SyncHook } from 'tapable';
import { IModel } from '../renderer/IModel';
import { IMultiPassRenderer } from '../renderer/IMultiPassRenderer';
import { ISource, ISourceCFG } from '../source/ISourceService';
import { ILayerStyleOptions } from './ILayerStyleService';
export enum ScaleTypes {
LINEAR = 'linear',
POWER = 'power',
LOG = 'log',
IDENTITY = 'identity',
TIME = 'time',
QUANTILE = 'quantile',
QUANTIZE = 'quantize',
THRESHOLD = 'threshold',
CAT = 'cat',
}
export enum StyleScaleType {
CONSTANT = 'constant',
VARIABLE = 'variable',
}
export interface IScaleOption {
field?: string;
type: ScaleTypes;
ticks?: any[];
nice?: boolean;
format?: () => any;
domain?: any[];
}
export interface IStyleScale {
scale: any;
field: string;
type: StyleScaleType;
option: IScaleOption;
}
import {
IEncodeFeature,
IScale,
IStyleAttributeService,
StyleAttrField,
StyleAttributeOption,
} from './IStyleAttributeService';
export interface ILayerGlobalConfig {
colors: string[];
size: number;
shape: string;
scales: {
[key: string]: IScaleOption;
[key: string]: IScale;
};
}
type CallBack = (...args: any[]) => any;
export type StyleAttributeField = string | string[];
export type StyleAttributeOption = string | number | boolean | any[] | CallBack;
export type StyleAttrField = string | string[] | number | number[];
export interface ILayerStyleAttribute {
type: string;
names: string[];
field: StyleAttributeField;
values?: any[];
scales?: IStyleScale[];
setScales: (scales: IStyleScale[]) => void;
callback?: (...args: any[]) => [];
mapping?(...params: unknown[]): unknown[];
export interface IPickedFeature {
x: number;
y: number;
lnglat?: { lng: number; lat: number };
feature?: unknown;
}
export interface ILayer {
name: string;
id: string; // 一个场景中同一类型 Layer 可能存在多个
name: string; // 代表 Layer 的类型
// visible: boolean;
// zIndex: number;
// type: string;
@ -67,16 +38,20 @@ export interface ILayer {
init: SyncHook<unknown>;
beforeRender: SyncHook<unknown>;
afterRender: SyncHook<unknown>;
beforePickingEncode: SyncHook<unknown>;
afterPickingEncode: SyncHook<unknown>;
beforeHighlight: SyncHook<unknown>;
afterHighlight: SyncHook<unknown>;
beforeDestroy: SyncHook<unknown>;
afterDestroy: SyncHook<unknown>;
};
models: IModel[];
styleAttributes: {
[attributeName: string]: ILayerStyleAttribute;
};
sourceOption: {
data: any;
options?: ISourceCFG;
};
multiPassRenderer: IMultiPassRenderer;
styleAttributeService: IStyleAttributeService;
init(): ILayer;
size(field: StyleAttrField, value?: StyleAttributeOption): ILayer;
color(field: StyleAttrField, value?: StyleAttributeOption): ILayer;
@ -84,7 +59,7 @@ export interface ILayer {
// pattern(field: string, value: StyleAttributeOption): ILayer;
// filter(field: string, value: StyleAttributeOption): ILayer;
// active(option: ActiveOption): ILayer;
style(options: ILayerStyleOptions): ILayer;
style(options: unknown): ILayer;
// hide(): ILayer;
// show(): ILayer;
// animate(field: string, option: any): ILayer;
@ -94,11 +69,15 @@ export interface ILayer {
addPlugin(plugin: ILayerPlugin): ILayer;
getSource(): ISource;
setSource(source: ISource): void;
setEncodedData(encodedData: Array<{ [key: string]: unknown }>): void;
getEncodedData(): Array<{ [key: string]: unknown }>;
getInitializationOptions(): Partial<ILayerInitializationOptions>;
setEncodedData(encodedData: IEncodeFeature[]): void;
getEncodedData(): IEncodeFeature[];
getStyleOptions(): Partial<ILayerInitializationOptions>;
isDirty(): boolean;
}
/**
* Layer
*/
export interface ILayerPlugin {
apply(layer: ILayer): void;
}
@ -108,8 +87,22 @@ export interface ILayerPlugin {
*/
export interface ILayerInitializationOptions {
enableMultiPassRenderer: boolean;
enablePicking: boolean;
passes: Array<string | [string, { [key: string]: unknown }]>;
/**
*
*/
enablePicking: boolean;
/**
*
*/
enableHighlight: boolean;
/**
*
*/
highlightColor: string | number[];
onHover(pickedFeature: IPickedFeature): void;
onClick(pickedFeature: IPickedFeature): void;
}
/**
@ -119,5 +112,5 @@ export interface ILayerService {
add(layer: ILayer): void;
initLayers(): void;
renderLayers(): void;
clean(): void;
destroy(): void;
}

View File

@ -1,31 +0,0 @@
/**
* 1. Layer
* 2. Layer style()
* Layer dirty uniform Buffer
* @see https://yuque.antfin-inc.com/yuqi.pyq/fgetpa/qfuzg8
*/
type OptionType = 'attribute' | 'uniform';
type Color = [number, number, number];
type StyleOption =
| {
type: OptionType;
value: string | number | number[] | Color;
dirty: boolean;
}
| string
| number
| number[]
| Color;
export interface ILayerStyleOptions {
[key: string]: StyleOption;
}
// tslint:disable-next-line:no-empty-interface
export default interface ILayerStyleService {
registerDefaultStyleOptions(
layerName: string,
options: ILayerStyleOptions,
): void;
}

View File

@ -0,0 +1,157 @@
import {
IAttribute,
IAttributeInitializationOptions,
} from '../renderer/IAttribute';
import { IBufferInitializationOptions } from '../renderer/IBuffer';
import { IElements } from '../renderer/IElements';
import { ILayer } from './ILayerService';
/**
* 1. Layer
* 2. Layer style()
* Layer dirty uniform Buffer
* @see https://yuque.antfin-inc.com/yuqi.pyq/fgetpa/qfuzg8
*/
export enum ScaleTypes {
LINEAR = 'linear',
POWER = 'power',
LOG = 'log',
IDENTITY = 'identity',
TIME = 'time',
QUANTILE = 'quantile',
QUANTIZE = 'quantize',
THRESHOLD = 'threshold',
CAT = 'cat',
}
export interface IScale {
type: ScaleTypes;
ticks?: any[];
nice?: boolean;
format?: () => any;
domain?: any[];
}
export enum AttributeType {
Attribute,
InstancedAttribute,
Uniform,
}
export interface IEncodeFeature {
color?: Color;
size?: number | number[];
shape?: string | number;
pattern?: string;
id?: number;
coordinates: Position[][];
}
export interface IVertexAttributeDescriptor
extends Omit<IAttributeInitializationOptions, 'buffer'> {
/**
* attribute name in vertex shader
*/
name: string;
/**
* buffer
*/
buffer: IBufferInitializationOptions;
update?: (
feature: IEncodeFeature,
featureIdx: number,
vertex: number[],
) => number[];
}
type Position = number[];
type Color = [number, number, number, number];
type CallBack = (...args: any[]) => any;
export type StyleAttributeField = string | string[];
export type StyleAttributeOption = string | number | boolean | any[] | CallBack;
export type StyleAttrField = string | string[] | number | number[];
export interface IStyleAttributeInitializationOptions {
name: string;
type: AttributeType;
scale?: {
field: StyleAttributeField;
values: unknown[];
callback?: (...args: any[]) => [];
scalers?: Array<{
field: string;
func: unknown;
}>;
};
descriptor: IVertexAttributeDescriptor;
}
export interface IFeatureRange {
startIndex: number;
endIndex: number;
}
export interface IStyleAttribute extends IStyleAttributeInitializationOptions {
needRescale: boolean;
needRemapping: boolean;
needRegenerateVertices: boolean;
featureRange: IFeatureRange;
/**
* IAttribute
*/
vertexAttribute: IAttribute;
mapping?(...params: unknown[]): unknown[];
setProps(props: Partial<IStyleAttributeInitializationOptions>): void;
}
export type Triangulation = (
feature: IEncodeFeature,
) => {
vertices: number[];
indices: number[];
size: number;
};
export interface IStyleAttributeUpdateOptions {
featureRange: IFeatureRange;
}
export interface IStyleAttributeService {
// registerDefaultStyleOptions(
// layerName: string,
// options: ILayerStyleOptions,
// ): void;
registerStyleAttribute(
options: Partial<IStyleAttributeInitializationOptions>,
): IStyleAttribute;
updateStyleAttribute(
attributeName: string,
attributeOptions: Partial<IStyleAttributeInitializationOptions>,
updateOptions: IStyleAttributeUpdateOptions,
): void;
getLayerStyleAttributes(): IStyleAttribute[] | undefined;
getLayerStyleAttribute(attributeName: string): IStyleAttribute | undefined;
createAttributesAndIndices(
encodedFeatures: IEncodeFeature[],
triangulation: Triangulation,
): {
attributes: {
[attributeName: string]: IAttribute;
};
elements: IElements;
};
/**
* feature range
*/
updateAttributeByFeatureRange(
attributeName: string,
features: IEncodeFeature[],
startFeatureIdx?: number,
endFeatureIdx?: number,
): void;
/**
*
*/
clearAllAttributes(): void;
}

View File

@ -30,16 +30,19 @@ export default class LayerService implements ILayerService {
}
public renderLayers() {
this.layers.forEach((layer) => {
// trigger hooks
layer.hooks.beforeRender.call(layer);
layer.render();
layer.hooks.afterRender.call(layer);
});
// TODO脏检查只渲染发生改变的 Layer
this.layers
// .filter((layer) => layer.isDirty())
.forEach((layer) => {
// trigger hooks
layer.hooks.beforeRender.call(layer);
layer.render();
layer.hooks.afterRender.call(layer);
});
}
public clean() {
// TODO: destroy every layer first
public destroy() {
this.layers.forEach((layer) => layer.destroy());
this.layers = [];
}
}

View File

@ -1,18 +0,0 @@
import { inject, injectable } from 'inversify';
import ILayerStyleService, { ILayerStyleOptions } from './ILayerStyleService';
@injectable()
export default class LayerStyleService implements ILayerStyleService {
private registry: {
[layerName: string]: ILayerStyleOptions;
} = {};
public registerDefaultStyleOptions(
layerName: string,
options: ILayerStyleOptions,
) {
if (!this.registry[layerName]) {
this.registry[layerName] = options;
}
}
}

View File

@ -0,0 +1,80 @@
import { isNil } from 'lodash';
import { IStyleAttribute } from '../layer/IStyleAttributeService';
import { IAttribute } from '../renderer/IAttribute';
import { IBuffer } from '../renderer/IBuffer';
import {
AttributeType,
IEncodeFeature,
IFeatureRange,
IStyleAttributeInitializationOptions,
IVertexAttributeDescriptor,
} from './IStyleAttributeService';
export default class StyleAttribute implements IStyleAttribute {
public name: string;
public type: AttributeType;
public scale?: {
field: string | string[];
values: unknown[];
callback?: (...args: any[]) => [];
scalers?: Array<{
field: string;
func: unknown;
}>;
};
public descriptor: IVertexAttributeDescriptor;
public featureBufferLayout: Array<{
feature: IEncodeFeature;
featureIdx: number;
bufferOffset: number;
length: number;
}> = [];
public needRescale: boolean = false;
public needRemapping: boolean = false;
public needRegenerateVertices: boolean = false;
public featureRange: IFeatureRange = {
startIndex: 0,
endIndex: Infinity,
};
public vertexAttribute: IAttribute;
private buffer: IBuffer;
constructor(options: Partial<IStyleAttributeInitializationOptions>) {
this.setProps(options);
}
public setProps(options: Partial<IStyleAttributeInitializationOptions>) {
Object.assign(this, options);
}
public mapping(params: unknown[]): unknown[] {
/**
* callback null , callback
*/
if (this!.scale!.callback) {
// 使用用户返回的值处理
const ret = this!.scale!.callback(params);
if (!isNil(ret)) {
return [ret];
}
}
// 没有 callback 或者用户 callback 返回值为空,则使用默认的逻辑处理
return this.defaultCallback(params);
}
private defaultCallback = (params: unknown[]): unknown[] => {
// 没有 params 的情况,是指没有指定 fields直接返回配置的 values 常量
if (params.length === 0) {
return this!.scale!.values;
}
return params.map((param, idx) => {
const scaleFunc = this.scale!.scalers![idx].func;
// @ts-ignore
const value = scaleFunc(param);
return value;
});
};
}

View File

@ -0,0 +1,268 @@
import { inject, injectable } from 'inversify';
import { TYPES } from '../../types';
import { gl } from '../renderer/gl';
import { IAttribute } from '../renderer/IAttribute';
import { IElements } from '../renderer/IElements';
import { IRendererService } from '../renderer/IRendererService';
import { ILayer } from './ILayerService';
import {
IEncodeFeature,
IStyleAttribute,
IStyleAttributeInitializationOptions,
IStyleAttributeService,
IStyleAttributeUpdateOptions,
IVertexAttributeDescriptor,
Triangulation,
} from './IStyleAttributeService';
import StyleAttribute from './StyleAttribute';
const bytesPerElementMap = {
[gl.FLOAT]: 4,
[gl.UNSIGNED_BYTE]: 1,
[gl.UNSIGNED_SHORT]: 2,
};
/**
* Layer
*/
@injectable()
export default class StyleAttributeService implements IStyleAttributeService {
@inject(TYPES.IRendererService)
private readonly rendererService: IRendererService;
private attributes: IStyleAttribute[] = [];
private featureLayout: {
sizePerElement: number;
elements: Array<{
featureIdx: number;
vertices: number[];
offset: number;
}>;
} = {
sizePerElement: 0,
elements: [],
};
public registerStyleAttribute(
options: Partial<IStyleAttributeInitializationOptions>,
) {
let attributeToUpdate =
options.name && this.getLayerStyleAttribute(options.name);
if (attributeToUpdate) {
attributeToUpdate.setProps(options);
} else {
attributeToUpdate = new StyleAttribute(options);
this.attributes.push(attributeToUpdate);
}
return attributeToUpdate;
}
public updateStyleAttribute(
attributeName: string,
options: Partial<IStyleAttributeInitializationOptions>,
updateOptions?: Partial<IStyleAttributeUpdateOptions>,
) {
let attributeToUpdate = this.getLayerStyleAttribute(attributeName);
if (!attributeToUpdate) {
attributeToUpdate = this.registerStyleAttribute({
...options,
name: attributeName,
});
}
const { scale } = options;
if (scale) {
// TODO: 需要比较新旧值确定是否需要 rescale
// 需要重新 scale肯定也需要重新进行数据映射
attributeToUpdate.scale = scale;
attributeToUpdate.needRescale = true;
attributeToUpdate.needRemapping = true;
attributeToUpdate.needRegenerateVertices = true;
if (updateOptions && updateOptions.featureRange) {
attributeToUpdate.featureRange = updateOptions.featureRange;
}
}
}
public getLayerStyleAttributes(): IStyleAttribute[] | undefined {
return this.attributes;
}
public getLayerStyleAttribute(
attributeName: string,
): IStyleAttribute | undefined {
return this.attributes.find(
(attribute) => attribute.name === attributeName,
);
}
public updateAttributeByFeatureRange(
attributeName: string,
features: IEncodeFeature[],
startFeatureIdx: number = 0,
endFeatureIdx?: number,
) {
const attributeToUpdate = this.attributes.find(
(attribute) => attribute.name === attributeName,
);
if (attributeToUpdate) {
const { descriptor } = attributeToUpdate;
const { update, buffer, size = 0 } = descriptor;
const bytesPerElement = bytesPerElementMap[buffer.type || gl.FLOAT];
if (update) {
const { elements, sizePerElement } = this.featureLayout;
// 截取待更新的 feature 范围
const featuresToUpdate = elements.slice(startFeatureIdx, endFeatureIdx);
// [n, n] 中断更新
if (!featuresToUpdate.length) {
return;
}
const { offset } = featuresToUpdate[0];
// 以 byte 为单位计算 buffer 中的偏移
const bufferOffsetInBytes = offset * size * bytesPerElement;
const updatedBufferData = featuresToUpdate
.map(({ featureIdx, vertices }) => {
const verticesNumForCurrentFeature =
vertices.length / sizePerElement;
const featureData: number[] = [];
for (
let vertexIdx = 0;
vertexIdx < verticesNumForCurrentFeature;
vertexIdx++
) {
featureData.push(
...update(
features[featureIdx],
featureIdx,
vertices.slice(
vertexIdx * sizePerElement,
vertexIdx * sizePerElement + sizePerElement,
),
),
);
}
return featureData;
})
.reduce((prev, cur) => {
prev.push(...cur);
return prev;
}, []);
// 更新底层 IAttribute 中包含的 IBuffer使用 subdata
attributeToUpdate.vertexAttribute.updateBuffer({
data: updatedBufferData,
offset: bufferOffsetInBytes,
});
}
}
}
public createAttributesAndIndices(
features: IEncodeFeature[],
triangulation: Triangulation,
): {
attributes: {
[attributeName: string]: IAttribute;
};
elements: IElements;
} {
const descriptors = this.attributes.map((attr) => attr.descriptor);
let verticesNum = 0;
const vertices: number[] = [];
const indices: number[] = [];
let size = 3;
features.forEach((feature, featureIdx) => {
// 逐 feature 进行三角化
const {
indices: indicesForCurrentFeature,
vertices: verticesForCurrentFeature,
size: vertexSize,
} = triangulation(feature);
indices.push(...indicesForCurrentFeature.map((i) => i + verticesNum));
vertices.push(...verticesForCurrentFeature);
size = vertexSize;
const verticesNumForCurrentFeature =
verticesForCurrentFeature.length / vertexSize;
// 记录三角化结果,用于后续精确更新指定 feature
this.featureLayout.sizePerElement = size;
this.featureLayout.elements.push({
featureIdx,
vertices: verticesForCurrentFeature,
offset: verticesNum,
});
verticesNum += verticesNumForCurrentFeature;
// 根据 position 顶点生成其他顶点数据
for (
let vertexIdx = 0;
vertexIdx < verticesNumForCurrentFeature;
vertexIdx++
) {
descriptors.forEach((descriptor, attributeIdx) => {
if (descriptor.update) {
(descriptor.buffer.data as number[]).push(
...descriptor.update(
feature,
featureIdx,
verticesForCurrentFeature.slice(
vertexIdx * vertexSize,
vertexIdx * vertexSize + vertexSize,
),
// TODO: 传入顶点索引 vertexIdx
),
);
}
});
}
});
const {
createAttribute,
createBuffer,
createElements,
} = this.rendererService;
const attributes: {
[attributeName: string]: IAttribute;
} = {};
descriptors.forEach((descriptor, attributeIdx) => {
// IAttribute 参数透传
const { buffer, update, name, ...rest } = descriptor;
const vertexAttribute = createAttribute({
// IBuffer 参数透传
buffer: createBuffer(buffer),
...rest,
});
attributes[descriptor.name || ''] = vertexAttribute;
// 在 StyleAttribute 上保存对 VertexAttribute 的引用
this.attributes[attributeIdx].vertexAttribute = vertexAttribute;
});
const elements = createElements({
data: indices,
type: gl.UNSIGNED_INT,
count: indices.length,
});
return {
attributes,
elements,
};
}
public clearAllAttributes() {
// 销毁关联的 vertex attribute buffer objects
this.attributes.forEach((attribute) => {
attribute.vertexAttribute.destroy();
});
this.attributes = [];
}
}

View File

@ -13,6 +13,7 @@ export interface IPoint {
export interface IMapService {
map: AMap.Map | Map;
init(config: Partial<IMapConfig>): void;
destroy(): void;
onCameraChanged(callback: (viewport: IViewport) => void): void;
// init map
addMarkerContainer(): void;
@ -27,14 +28,16 @@ export interface IMapService {
// get map status method
getMinZoom(): number;
getMaxZoom(): number;
// get map params
getType(): MapType;
getZoom(): number;
getCenter(): ILngLat;
getPitch(): number;
getRotation(): number;
getBounds(): Bounds;
getMapContainer(): HTMLElement | null;
// set Map status
// control with raw map
setRotation(rotation: number): void;
zoomIn(): void;
zoomOut(): void;
@ -44,12 +47,13 @@ export interface IMapService {
setZoomAndCenter(zoom: number, center: Point): void;
setMapStyle(style: string): void;
// conversion Method
// coordinates methods
pixelToLngLat(pixel: Point): ILngLat;
lngLatToPixel(lnglat: Point): IPoint;
containerToLngLat(pixel: Point): ILngLat;
lngLatToContainer(lnglat: Point): IPoint;
}
export enum MapType {
amap = 'amap',
mapbox = 'mapbox',

View File

@ -26,5 +26,11 @@ export interface IAttributeInitializationOptions {
}
export interface IAttribute {
updateBuffer(options: {
// 用于替换的数据
data: number[] | number[][] | Uint8Array | Uint16Array | Uint32Array;
// 原 Buffer 替换位置,单位为 byte
offset: number;
}): void;
destroy(): void;
}

View File

@ -21,6 +21,7 @@ export interface IElementsInitializationOptions {
| gl.TRIANGLES
| gl.TRIANGLE_STRIP
| gl.TRIANGLE_FAN;
count?: number;
}
export interface IElements {

View File

@ -29,6 +29,16 @@ export interface IClearOptions {
framebuffer?: IFramebuffer | null;
}
export interface IReadPixelsOptions {
x: number;
y: number;
width: number;
height: number;
// gl.bindFrameBuffer
framebuffer?: IFramebuffer;
data?: Uint8Array;
}
export interface IRendererService {
init($container: HTMLDivElement): Promise<void>;
clear(options: IClearOptions): void;
@ -38,11 +48,13 @@ export interface IRendererService {
createElements(options: IElementsInitializationOptions): IElements;
createTexture2D(options: ITexture2DInitializationOptions): ITexture2D;
createFramebuffer(options: IFramebufferInitializationOptions): IFramebuffer;
renderToFramebuffer(
useFramebuffer(
framebuffer: IFramebuffer | null,
drawCommands: () => void,
): void;
getViewportSize(): { width: number; height: number };
getContainer(): HTMLElement | null;
viewport(size: { x: number; y: number; width: number; height: number }): void;
readPixels(options: IReadPixelsOptions): Uint8Array;
destroy(): void;
}

View File

@ -81,9 +81,9 @@ export default class BasePostProcessingPass<InitializationOptions = {}>
public render(layer: ILayer) {
const postProcessor = layer.multiPassRenderer.getPostProcessor();
const { renderToFramebuffer } = this.rendererService;
const { useFramebuffer } = this.rendererService;
renderToFramebuffer(
useFramebuffer(
this.renderToScreen ? null : postProcessor.getWriteFBO(),
() => {
this.model.draw({

View File

@ -1,28 +1,63 @@
import { inject, injectable } from 'inversify';
import { lazyInject } from '../../../index';
import { TYPES } from '../../../types';
import {
IInteractionService,
InteractionEvent,
} from '../../interaction/IInteractionService';
import { ILayer, ILayerService } from '../../layer/ILayerService';
import { ILogService } from '../../log/ILogService';
import { gl } from '../gl';
import { IFramebuffer } from '../IFramebuffer';
import { IPass, PassType } from '../IMultiPassRenderer';
import { IRendererService } from '../IRendererService';
function decodePickingColor(color: Uint8Array): number {
const [i1, i2, i3] = color;
// 1 was added to seperate from no selection
const index = i1 + i2 * 256 + i3 * 65536 - 1;
return index;
}
/**
* PixelPickingPass based on
* color-based PixelPickingPass
* @see https://github.com/antvis/L7/blob/next/dev-docs/PixelPickingEngine.md
*/
@injectable()
export default class PixelPickingPass implements IPass {
@lazyInject(TYPES.IRendererService)
protected readonly rendererService: IRendererService;
@lazyInject(TYPES.IInteractionService)
protected readonly interactionService: IInteractionService;
@lazyInject(TYPES.ILogService)
protected readonly logger: ILogService;
/**
* picking framebuffer attributes
*/
private pickingFBO: IFramebuffer;
/**
* layer
*/
private layer: ILayer;
/**
* throttle hover picking framebuffer
*/
private alreadyInRendering: boolean = false;
public getType() {
return PassType.Normal;
}
public init(layer: ILayer) {
this.layer = layer;
const { createTexture2D, createFramebuffer } = this.rendererService;
// 创建 picking framebuffer后续实时 resize
this.pickingFBO = createFramebuffer({
color: createTexture2D({
width: 1,
@ -31,16 +66,147 @@ export default class PixelPickingPass implements IPass {
wrapT: gl.CLAMP_TO_EDGE,
}),
});
// 监听 hover 事件
this.interactionService.on(InteractionEvent.Hover, this.pickFromPickingFBO);
}
public render(layer: ILayer) {
const { getViewportSize, renderToFramebuffer } = this.rendererService;
this.pickingFBO.resize(getViewportSize());
if (this.alreadyInRendering) {
return;
}
renderToFramebuffer(this.pickingFBO, () => {
layer.multiPassRenderer.setRenderFlag(false);
const { getViewportSize, useFramebuffer, clear } = this.rendererService;
const { width, height } = getViewportSize();
// throttled
this.alreadyInRendering = true;
// resize first, fbo can't be resized in use
this.pickingFBO.resize({ width, height });
useFramebuffer(this.pickingFBO, () => {
clear({
framebuffer: this.pickingFBO,
color: [0, 0, 0, 0],
stencil: 0,
depth: 1,
});
this.logger.info(`picking fbo cleared ${width} ${height}`);
/**
* picking pass multipass
* 1. clear ClearPass
* 2. RenderPass
* 3. pass
*/
const originRenderFlag = this.layer.multiPassRenderer.getRenderFlag();
this.layer.multiPassRenderer.setRenderFlag(false);
// trigger hooks
layer.hooks.beforeRender.call(layer);
layer.render();
layer.multiPassRenderer.setRenderFlag(true);
layer.hooks.afterRender.call(layer);
this.layer.multiPassRenderer.setRenderFlag(originRenderFlag);
this.alreadyInRendering = false;
});
}
/**
*
* TODO
*/
private pickFromPickingFBO = ({ x, y }: { x: number; y: number }) => {
const {
getViewportSize,
readPixels,
useFramebuffer,
} = this.rendererService;
const { width, height } = getViewportSize();
const { enableHighlight } = this.layer.getStyleOptions();
let pickedColors: Uint8Array | undefined;
useFramebuffer(this.pickingFBO, () => {
// avoid realloc
pickedColors = readPixels({
x: Math.round(x * window.devicePixelRatio),
// 视口坐标系原点在左上,而 WebGL 在左下,需要翻转 Y 轴
y: Math.round(height - (y + 1) * window.devicePixelRatio),
width: 1,
height: 1,
data: new Uint8Array(1 * 1 * 4),
framebuffer: this.pickingFBO,
});
this.logger.info('try to picking');
if (
pickedColors[0] !== 0 ||
pickedColors[1] !== 0 ||
pickedColors[2] !== 0
) {
this.logger.info('picked');
const pickedFeatureIdx = decodePickingColor(pickedColors);
const rawFeature = this.layer.getSource()!.data!.dataArray[
pickedFeatureIdx
];
// trigger onHover/Click callback on layer
this.triggerHoverOnLayer({ x, y, feature: rawFeature });
}
});
if (enableHighlight) {
this.highlightPickedFeature(pickedColors);
}
};
private triggerHoverOnLayer({
x,
y,
feature,
}: {
x: number;
y: number;
feature: unknown;
}) {
// TODO: onClick
const { onHover, onClick } = this.layer.getStyleOptions();
if (onHover) {
onHover({
x,
y,
feature,
});
}
}
/**
* highlight feature buffer
* 1.
* 2. alpha
* shader
* @example
* this.layer.color('name', ['#000000'], {
* featureRange: {
* startIndex: pickedFeatureIdx,
* endIndex: pickedFeatureIdx + 1,
* },
* });
*/
private highlightPickedFeature(pickedColors: Uint8Array | undefined) {
const [r, g, b] = pickedColors;
// TODO: highlight pass 需要 multipass
const originRenderFlag = this.layer.multiPassRenderer.getRenderFlag();
this.layer.multiPassRenderer.setRenderFlag(false);
this.layer.hooks.beforeRender.call(this.layer);
// @ts-ignore
this.layer.hooks.beforeHighlight.call(this.layer, [r, g, b]);
this.layer.render();
this.layer.hooks.afterHighlight.call(this.layer);
this.layer.hooks.afterRender.call(this.layer);
this.layer.multiPassRenderer.setRenderFlag(originRenderFlag);
}
}

View File

@ -22,9 +22,9 @@ export default class RenderPass implements IPass {
}
public render(layer: ILayer) {
const { renderToFramebuffer, clear } = this.rendererService;
const { useFramebuffer, clear } = this.rendererService;
const readFBO = layer.multiPassRenderer.getPostProcessor().getReadFBO();
renderToFramebuffer(readFBO, () => {
useFramebuffer(readFBO, () => {
clear({
color: [0, 0, 0, 0],
depth: 1,

View File

@ -140,7 +140,6 @@ export default class Scene extends EventEmitter implements ISceneService {
// 初始化 container 上的交互
this.interactionService.init();
// TODOinit renderer
this.logger.info('renderer loaded');
});
@ -169,13 +168,15 @@ export default class Scene extends EventEmitter implements ISceneService {
}
public destroy() {
this.emit('destory');
this.emit('destroy');
this.inited = false;
this.layerService.clean();
this.layerService.destroy();
this.configService.reset();
this.interactionService.destroy();
this.controlService.destroy();
this.removeAllListeners();
this.rendererService.destroy();
this.map.destroy();
window.removeEventListener('resize', this.handleWindowResized, false);
}
private handleWindowResized = () => {

View File

@ -5,6 +5,8 @@ import { IModuleParams, IShaderModuleService } from './IShaderModuleService';
import decode from '../../shaders/decode.glsl';
import lighting from '../../shaders/lighting.glsl';
import pickingFrag from '../../shaders/picking.frag.glsl';
import pickingVert from '../../shaders/picking.vert.glsl';
import projection from '../../shaders/projection.glsl';
import sdf2d from '../../shaders/sdf_2d.glsl';
@ -23,6 +25,7 @@ export default class ShaderModuleService implements IShaderModuleService {
this.registerModule('projection', { vs: projection, fs: '' });
this.registerModule('sdf_2d', { vs: '', fs: sdf2d });
this.registerModule('lighting', { vs: lighting, fs: '' });
this.registerModule('picking', { vs: pickingVert, fs: pickingFrag });
}
public registerModule(moduleName: string, moduleParams: IModuleParams) {

View File

@ -0,0 +1,46 @@
varying vec4 v_PickingResult;
uniform vec4 u_HighlightColor : [0, 0, 0, 0];
uniform float u_PickingStage : 0.0;
#define PICKING_NONE 0.0
#define PICKING_ENCODE 1.0
#define PICKING_HIGHLIGHT 2.0
#define COLOR_SCALE 1. / 255.
/*
* Returns highlight color if this item is selected.
*/
vec4 filterHighlightColor(vec4 color) {
bool selected = bool(v_PickingResult.a);
if (selected) {
vec4 highLightColor = u_HighlightColor * COLOR_SCALE;
float highLightAlpha = highLightColor.a;
float highLightRatio = highLightAlpha / (highLightAlpha + color.a * (1.0 - highLightAlpha));
vec3 resultRGB = mix(color.rgb, highLightColor.rgb, highLightRatio);
return vec4(resultRGB, color.a);
} else {
return color;
}
}
/*
* Returns picking color if picking enabled else unmodified argument.
*/
vec4 filterPickingColor(vec4 color) {
vec3 pickingColor = v_PickingResult.rgb;
if (u_PickingStage == PICKING_ENCODE && length(pickingColor) < 0.001) {
discard;
}
return u_PickingStage == PICKING_ENCODE ? vec4(pickingColor, 1.0) : color;
}
/*
* Returns picking color if picking is enabled if not
* highlight color if this item is selected, otherwise unmodified argument.
*/
vec4 filterColor(vec4 color) {
return filterPickingColor(filterHighlightColor(color));
}

View File

@ -0,0 +1,27 @@
attribute vec3 a_PickingColor;
varying vec4 v_PickingResult;
uniform vec3 u_PickingColor : [0, 0, 0];
uniform vec4 u_HighlightColor : [0, 0, 0, 0];
uniform float u_PickingStage : 0.0;
uniform float u_PickingThreshold : 1.0;
#define PICKING_NONE 0.0
#define PICKING_ENCODE 1.0
#define PICKING_HIGHLIGHT 2.0
#define COLOR_SCALE 1. / 255.
bool isVertexPicked(vec3 vertexColor) {
return
abs(vertexColor.r - u_PickingColor.r) < u_PickingThreshold &&
abs(vertexColor.g - u_PickingColor.g) < u_PickingThreshold &&
abs(vertexColor.b - u_PickingColor.b) < u_PickingThreshold;
}
void setPickingColor(vec3 pickingColor) {
// compares only in highlight stage
v_PickingResult.a = float((u_PickingStage == PICKING_HIGHLIGHT) && isVertexPicked(pickingColor));
// Stores the picking color so that the fragment shader can render it during picking
v_PickingResult.rgb = pickingColor * COLOR_SCALE;
}

View File

@ -13,6 +13,7 @@ const TYPES = {
IFontService: Symbol.for('IFontService'),
IInteractionService: Symbol.for('IInteractionService'),
IControlService: Symbol.for('IControlService'),
IStyleAttributeService: Symbol.for('IStyleAttributeService'),
/** multi-pass */
ClearPass: Symbol.for('ClearPass'),

View File

@ -8,7 +8,7 @@ export function createRendererContainer(domId: string): HTMLDivElement | null {
$container.style.cssText += `
position: absolute;
top: 0;
z-index:10;
z-index:2;
height: 100%;
width: 100%;
pointer-events: none;

View File

@ -1,193 +0,0 @@
import { IICONMap, ILayerStyleOptions } from '@l7/core';
import { lngLatToMeters } from '@l7/utils';
import { vec3 } from 'gl-matrix';
import { IExtrudeGeomety } from '../point/shape/extrude';
interface IBufferCfg {
data: unknown[];
iconMap?: IICONMap;
style?: ILayerStyleOptions;
}
export type Position = number[];
type Color = [number, number, number, number];
export interface IBufferInfo {
vertices?: any;
indexArray?: any;
indexOffset: any;
verticesOffset: number;
faceNum?: any;
dimensions: number;
[key: string]: any;
}
export interface IEncodeFeature {
color?: Color;
size?: number | number[];
shape?: string | number;
pattern?: string;
id?: number;
coordinates: unknown;
bufferInfo: unknown;
}
export default class Buffer {
public attributes: {
[key: string]: Float32Array;
} = {};
public verticesCount: number = 0;
public indexArray: Uint32Array = new Uint32Array(0);
public indexCount: number = 0;
public instanceGeometry: IExtrudeGeomety;
protected data: unknown[];
protected iconMap: IICONMap;
protected style: any;
constructor({ data, iconMap, style }: IBufferCfg) {
this.data = data;
this.iconMap = iconMap as IICONMap;
this.style = style;
this.init();
}
public computeVertexNormals(
field: string = 'positions',
flag: boolean = true,
) {
const normals = (this.attributes.normals = new Float32Array(
this.verticesCount * 3,
));
const indexArray = this.indexArray;
const positions = this.attributes[field];
let vA;
let vB;
let vC;
const cb = vec3.create();
const ab = vec3.create();
const normal = vec3.create();
for (let i = 0, li = indexArray.length; i < li; i += 3) {
vA = indexArray[i + 0] * 3;
vB = indexArray[i + 1] * 3;
vC = indexArray[i + 2] * 3;
const [ax, ay] = flag
? lngLatToMeters([positions[vA], positions[vA + 1]])
: [positions[vA], positions[vA + 1]];
const pA = vec3.fromValues(ax, ay, positions[vA + 2]);
const [bx, by] = flag
? lngLatToMeters([positions[vB], positions[vB + 1]])
: [positions[vB], positions[vB + 1]];
const pB = vec3.fromValues(bx, by, positions[vB + 2]);
const [cx, cy] = flag
? lngLatToMeters([positions[vC], positions[vC + 1]])
: [positions[vC], positions[vC + 1]];
const pC = vec3.fromValues(cx, cy, positions[vC + 2]);
vec3.sub(cb, pC, pB);
vec3.sub(ab, pA, pB);
vec3.cross(normal, cb, ab);
normals[vA] += cb[0];
normals[vA + 1] += cb[1];
normals[vA + 2] += cb[2];
normals[vB] += cb[0];
normals[vB + 1] += cb[1];
normals[vB + 2] += cb[2];
normals[vC] += cb[0];
normals[vC + 1] += cb[1];
normals[vC + 2] += cb[2];
}
this.normalizeNormals();
}
// 计算每个要素顶点个数,记录索引位置
protected calculateFeatures() {
throw new Error('Method not implemented.');
}
protected buildFeatures() {
throw new Error('Method not implemented.');
}
protected checkIsClosed(points: Position[][]) {
const p1 = points[0][0];
const p2 = points[0][points[0].length - 1];
return p1[0] === p2[0] && p1[1] === p2[1];
}
protected concat(arrayType: Float32Array, arrays: any) {
let totalLength = 0;
for (const arr of arrays) {
totalLength += arr.length;
}
const arrayBuffer = new ArrayBuffer(
totalLength * arrayType.BYTES_PER_ELEMENT,
);
let offset = 0;
// @ts-ignore
const result = new arrayType(arrayBuffer);
for (const arr of arrays) {
result.set(arr, offset);
offset += arr.length;
}
return result;
}
protected encodeArray(feature: IEncodeFeature, num: number) {
const { color, id, pattern, size } = feature;
const bufferInfo = feature.bufferInfo as IBufferInfo;
const { verticesOffset } = bufferInfo;
const imagePos = this.iconMap;
const start1 = verticesOffset;
for (let i = 0; i < num; i++) {
if (color) {
this.attributes.colors[start1 * 4 + i * 4] = color[0];
this.attributes.colors[start1 * 4 + i * 4 + 1] = color[1];
this.attributes.colors[start1 * 4 + i * 4 + 2] = color[2];
this.attributes.colors[start1 * 4 + i * 4 + 3] = color[3];
}
if (id) {
this.attributes.pickingIds[start1 + i] = id;
}
if (size) {
let size2: number[] = [];
if (Array.isArray(size) && size.length === 2) {
// TODO 多维size支持
size2 = [size[0], size[0], size[1]];
}
if (!Array.isArray(size)) {
size2 = [size];
}
this.attributes.sizes.set(size2, (start1 + i) * size2.length);
}
if (pattern) {
// @ts-ignore
const patternPos = imagePos[pattern] || { x: 0, y: 0 };
this.attributes.patterns[start1 * 2 + i * 2] = patternPos.x;
this.attributes.patterns[start1 * 2 + i * 2 + 1] = patternPos.y;
}
}
}
protected initAttributes() {
this.attributes.positions = new Float32Array(this.verticesCount * 3);
this.attributes.colors = new Float32Array(this.verticesCount * 4);
this.attributes.pickingIds = new Float32Array(this.verticesCount);
this.attributes.sizes = new Float32Array(this.verticesCount);
this.attributes.pickingIds = new Float32Array(this.verticesCount);
this.indexArray = new Uint32Array(this.indexCount);
}
private init() {
// 1. 计算 attribute 长度
this.calculateFeatures();
// 2. 初始化 attribute
this.initAttributes();
// 3. 拼接attribute
this.buildFeatures();
}
private normalizeNormals() {
const { normals } = this.attributes;
for (let i = 0, li = normals.length; i < li; i += 3) {
const normal = vec3.fromValues(
normals[i],
normals[i + 1],
normals[i + 2],
);
const newNormal = vec3.create();
vec3.normalize(newNormal, normal);
normals.set(newNormal, i);
}
}
}

View File

@ -1,33 +1,63 @@
import {
IEncodeFeature,
IFontService,
IGlobalConfigService,
IIconService,
ILayer,
ILayerInitializationOptions,
ILayerPlugin,
ILayerStyleAttribute,
ILayerStyleOptions,
IMapService,
IModel,
IMultiPassRenderer,
IRendererService,
ISource,
IShaderModuleService,
ISourceCFG,
IStyleAttributeService,
IStyleAttributeUpdateOptions,
lazyInject,
StyleAttributeField,
StyleAttributeOption,
Triangulation,
TYPES,
} from '@l7/core';
import Source from '@l7/source';
import { isFunction } from 'lodash';
import { SyncHook } from 'tapable';
import DataEncodePlugin from '../plugins/DataEncodePlugin';
import DataMappingPlugin from '../plugins/DataMappingPlugin';
import DataSourcePlugin from '../plugins/DataSourcePlugin';
import FeatureScalePlugin from '../plugins/FeatureScalePlugin';
import MultiPassRendererPlugin from '../plugins/MultiPassRendererPlugin';
import PixelPickingPlugin from '../plugins/PixelPickingPlugin';
import RegisterStyleAttributePlugin from '../plugins/RegisterStyleAttributePlugin';
import ShaderUniformPlugin from '../plugins/ShaderUniformPlugin';
import StyleAttribute from './StyleAttribute';
import UpdateStyleAttributePlugin from '../plugins/UpdateStyleAttributePlugin';
export default class BaseLayer implements ILayer {
export interface ILayerModelInitializationOptions {
moduleName: string;
vertexShader: string;
fragmentShader: string;
triangulation: Triangulation;
}
/**
* layer id
*/
let layerIdCounter = 0;
/**
* Layer
*/
const defaultLayerInitializationOptions: Partial<
ILayerInitializationOptions
> = {
enableMultiPassRenderer: true,
enablePicking: false,
enableHighlight: false,
highlightColor: 'red',
};
export default class BaseLayer<ChildLayerStyleOptions = {}> implements ILayer {
public id: string = `${layerIdCounter++}`;
public name: string;
// 生命周期钩子
@ -35,6 +65,13 @@ export default class BaseLayer implements ILayer {
init: new SyncHook(['layer']),
beforeRender: new SyncHook(['layer']),
afterRender: new SyncHook(['layer']),
beforePickingEncode: new SyncHook(['layer']),
afterPickingEncode: new SyncHook(['layer']),
// @ts-ignore
beforeHighlight: new SyncHook(['layer', 'pickedColor']),
afterHighlight: new SyncHook(['layer']),
beforeDestroy: new SyncHook(['layer']),
afterDestroy: new SyncHook(['layer']),
};
// 待渲染 model 列表
@ -46,21 +83,42 @@ export default class BaseLayer implements ILayer {
// 插件集
public plugins: ILayerPlugin[] = [
new DataSourcePlugin(),
new DataEncodePlugin(),
/**
* StyleAttribute VertexAttribute
*/
new RegisterStyleAttributePlugin(),
/**
* Source Scale
*/
new FeatureScalePlugin(),
/**
* 使 Scale
*/
new DataMappingPlugin(),
/**
*
*/
new UpdateStyleAttributePlugin(),
/**
* Multi Pass 线
*/
new MultiPassRendererPlugin(),
/**
*
*/
new ShaderUniformPlugin(),
/**
* Encode Highlight
*/
new PixelPickingPlugin(),
];
public sourceOption: {
data: any;
options?: ISourceCFG;
};
public styleOption: ILayerStyleOptions = {
opacity: 1.0,
};
// 样式属性
public styleAttributes: {
[key: string]: Required<ILayerStyleAttribute>;
} = {};
@lazyInject(TYPES.IStyleAttributeService)
public styleAttributeService: IStyleAttributeService;
@lazyInject(TYPES.IIconService)
protected readonly iconService: IIconService;
@ -70,20 +128,34 @@ export default class BaseLayer implements ILayer {
protected layerSource: Source;
private encodedData: Array<{ [key: string]: unknown }>;
private initializationOptions: Partial<ILayerInitializationOptions>;
private encodedData: IEncodeFeature[];
/**
*
*/
private styleOptions: Partial<
ILayerInitializationOptions & ChildLayerStyleOptions
>;
@lazyInject(TYPES.IGlobalConfigService)
private readonly configService: IGlobalConfigService;
@lazyInject(TYPES.IShaderModuleService)
private readonly shaderModuleService: IShaderModuleService;
@lazyInject(TYPES.IRendererService)
private readonly rendererService: IRendererService;
@lazyInject(TYPES.IMapService)
private readonly map: IMapService;
constructor(initializationOptions: Partial<ILayerInitializationOptions>) {
this.initializationOptions = initializationOptions;
constructor(
styleOptions: Partial<ILayerInitializationOptions & ChildLayerStyleOptions>,
) {
this.styleOptions = {
...defaultLayerInitializationOptions,
...styleOptions,
};
}
public addPlugin(plugin: ILayerPlugin) {
@ -101,42 +173,36 @@ export default class BaseLayer implements ILayer {
this.buildModels();
return this;
}
public color(
field: StyleAttributeField,
values?: StyleAttributeOption,
): ILayer {
this.createStyleAttribute(
updateOptions?: Partial<IStyleAttributeUpdateOptions>,
) {
this.styleAttributeService.updateStyleAttribute(
'color',
field,
values,
this.configService.getConfig().colors,
{
// @ts-ignore
scale: {
field,
...this.splitValuesAndCallbackInAttribute(
// @ts-ignore
values,
this.configService.getConfig().colors,
),
},
},
// @ts-ignore
updateOptions,
);
return this;
}
public size(
field: StyleAttributeField,
values?: StyleAttributeOption,
): ILayer {
this.createStyleAttribute(
'size',
field,
values,
this.configService.getConfig().size,
);
public size(field: StyleAttributeField, values?: StyleAttributeOption) {
return this;
}
public shape(
field: StyleAttributeField,
values?: StyleAttributeOption,
): ILayer {
this.createStyleAttribute(
'shape',
field,
values,
this.configService.getConfig().shape,
);
public shape(field: StyleAttributeField, values?: StyleAttributeOption) {
return this;
}
@ -147,8 +213,12 @@ export default class BaseLayer implements ILayer {
};
return this;
}
public style(options: ILayerStyleOptions): ILayer {
this.styleOption = options;
public style(options: unknown): ILayer {
// @ts-ignore
this.styleOptions = {
...this.styleOptions,
...options,
};
return this;
}
public render(): ILayer {
@ -169,13 +239,25 @@ export default class BaseLayer implements ILayer {
}
public destroy() {
this.hooks.beforeDestroy.call(this);
// 清除所有属性以及关联的 vao
this.styleAttributeService.clearAllAttributes();
// 销毁所有 model
this.models.forEach((model) => model.destroy());
this.hooks.afterDestroy.call(this);
}
public getStyleAttributes(): {
[key: string]: Required<ILayerStyleAttribute>;
} {
return this.styleAttributes;
public isDirty() {
return !!(
this.styleAttributeService.getLayerStyleAttributes() || []
).filter(
(attribute) =>
attribute.needRescale ||
attribute.needRemapping ||
attribute.needRegenerateVertices,
).length;
}
public setSource(source: Source) {
@ -185,18 +267,43 @@ export default class BaseLayer implements ILayer {
return this.layerSource;
}
public getInitializationOptions() {
return this.initializationOptions;
public getStyleOptions() {
return this.styleOptions;
}
public setEncodedData(encodedData: Array<{ [key: string]: unknown }>) {
public setEncodedData(encodedData: IEncodeFeature[]) {
this.encodedData = encodedData;
}
public getEncodedData() {
return this.encodedData;
}
protected buildLayerModel(options: ILayerModelInitializationOptions): IModel {
const { moduleName, vertexShader, fragmentShader, triangulation } = options;
this.shaderModuleService.registerModule(moduleName, {
vs: vertexShader,
fs: fragmentShader,
});
const { vs, fs, uniforms } = this.shaderModuleService.getModule(moduleName);
const { createModel } = this.rendererService;
const {
attributes,
elements,
} = this.styleAttributeService.createAttributesAndIndices(
this.encodedData,
triangulation,
);
return createModel({
attributes,
uniforms,
fs,
vs,
elements,
});
}
protected buildModels() {
throw new Error('Method not implemented.');
}
@ -205,18 +312,15 @@ export default class BaseLayer implements ILayer {
throw new Error('Method not implemented.');
}
private createStyleAttribute(
attributeName: string,
field: StyleAttributeField,
valuesOrCallback: any,
defaultValues: any,
private splitValuesAndCallbackInAttribute(
valuesOrCallback?: unknown[],
defaultValues?: unknown[],
) {
this.styleAttributes[attributeName] = new StyleAttribute({
field,
return {
values: isFunction(valuesOrCallback)
? undefined
: valuesOrCallback || defaultValues,
callback: isFunction(valuesOrCallback) ? valuesOrCallback : undefined,
});
};
}
}

View File

@ -1,113 +0,0 @@
import {
IScaleOption,
IStyleScale,
ScaleTypes,
StyleScaleType,
} from '@l7/core';
import { extent } from 'd3-array';
import * as d3 from 'd3-scale';
import { isNil, isNumber, isString, uniq } from 'lodash';
const dateRegex = /^(?:(?!0000)[0-9]{4}([-/.]+)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-/.]?)0?2\2(?:29))(\s+([01]|([01][0-9]|2[0-3])):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9]))?$/;
const scaleMap = {
[ScaleTypes.LINEAR]: d3.scaleLinear,
[ScaleTypes.POWER]: d3.scalePow,
[ScaleTypes.LOG]: d3.scaleLog,
[ScaleTypes.IDENTITY]: d3.scaleIdentity,
[ScaleTypes.TIME]: d3.scaleTime,
[ScaleTypes.QUANTILE]: d3.scaleQuantile,
[ScaleTypes.QUANTIZE]: d3.scaleQuantize,
[ScaleTypes.THRESHOLD]: d3.scaleThreshold,
[ScaleTypes.CAT]: d3.scaleOrdinal,
};
export default class ScaleController {
private scaleOptions: {
[field: string]: IScaleOption;
};
constructor(cfg: { [field: string]: IScaleOption }) {
this.scaleOptions = cfg;
}
public createScale(field: string, data?: any[]): IStyleScale {
let scaleOption: IScaleOption = this.scaleOptions[field];
const scale: IStyleScale = {
field,
scale: undefined,
type: StyleScaleType.VARIABLE,
option: scaleOption,
};
if (!data || !data.length) {
// 数据为空
if (scaleOption && scaleOption.type) {
scale.scale = this.generateScale(scaleOption.type, scaleOption);
} else {
scale.scale = d3.scaleOrdinal([field]);
scale.type = StyleScaleType.CONSTANT;
}
return scale;
}
let firstValue = null;
data.some((item) => {
if (!isNil(item[field])) {
firstValue = item[field];
return true;
}
firstValue = null;
});
// 常量 Scale
if (isNumber(field) || (isNil(firstValue) && !scaleOption)) {
scale.scale = d3.scaleOrdinal([field]);
scale.type = StyleScaleType.CONSTANT;
} else {
// 根据数据类型判断 默认等分位,时间,和枚举类型
const type =
scaleOption && scaleOption.type
? scaleOption.type
: this.getDefaultType(field, firstValue);
const cfg = this.getScaleCfg(type, field, data);
Object.assign(cfg, scaleOption);
scaleOption = cfg; // 更新scale配置
scale.scale = this.generateScale(type, cfg);
scale.option = scaleOption;
}
return scale;
}
private getDefaultType(field: string, firstValue: any) {
let type = ScaleTypes.QUANTIZE;
if (dateRegex.test(firstValue)) {
type = ScaleTypes.TIME;
} else if (isString(firstValue)) {
type = ScaleTypes.CAT;
}
return type;
}
private getScaleCfg(type: ScaleTypes, field: string, data: any[]) {
const cfg: IScaleOption = {
field,
type,
};
const values = data.map((item) => item[field]);
// 默认类型为 Quantile Scales https://github.com/d3/d3-scale#quantile-scales
if (type !== ScaleTypes.CAT && type !== ScaleTypes.QUANTILE) {
cfg.domain = extent(values);
} else if (type === ScaleTypes.CAT) {
cfg.domain = uniq(values);
}
return cfg;
}
private generateScale(type: ScaleTypes, scaleOption: IScaleOption) {
// @ts-ignore
let scale = scaleMap[type]();
if (scaleOption.hasOwnProperty('domain')) {
// 处理同一字段映射不同视觉通道的问题
scale = scale.copy().domain(scaleOption.domain);
}
// TODO 其他属性支持
return scale;
}
}

View File

@ -1,90 +0,0 @@
import { ILayerStyleAttribute, IStyleScale, StyleScaleType } from '@l7/core';
import { isNil, isString } from 'lodash';
type CallBack = (...args: any[]) => any;
export default class StyleAttribute implements ILayerStyleAttribute {
public type: StyleScaleType;
public names: string[];
public scales: IStyleScale[] = [];
public values: any[] = [];
public field: string;
constructor(cfg: any) {
const {
type = StyleScaleType.CONSTANT,
scales = [],
values = [],
callback,
field,
} = cfg;
this.field = field;
this.type = type;
this.scales = scales;
this.values = values;
this.names = this.parseFields(field) || [];
if (callback) {
this.type = StyleScaleType.VARIABLE;
}
this.callback = (...params: any[]): any[] => {
/**
* callback null , callback
*/
if (callback) {
// 使用用户返回的值处理
const ret = callback(...params);
if (!isNil(ret)) {
return [ret];
}
}
// 没有 callback 或者用户 callback 返回值为空,则使用默认的逻辑处理
return this.defaultCallback.apply(this, params);
};
}
public callback: CallBack = () => [];
public setScales(scales: IStyleScale[]): void {
if (scales.some((scale) => scale.type === StyleScaleType.VARIABLE)) {
this.type = StyleScaleType.VARIABLE;
scales.forEach((scale) => {
if (this.values.length > 0) {
scale.scale.range(this.values);
}
});
} else {
// 设置attribute 常量值
this.values = scales.map((scale, index) => {
return scale.scale(this.names[index]);
});
}
this.scales = scales;
}
public mapping(...params: unknown[]): unknown[] {
return this.callback.apply(this, params);
}
private defaultCallback(...params: unknown[]): unknown[] {
// 没有 params 的情况,是指没有指定 fields直接返回配置的 values 常量
if (params.length === 0) {
return this.values;
}
return params.map((param, idx) => {
const scale = this.scales[idx];
const value = scale.scale(param);
return value;
});
}
/**
* @example
* 'w*h' => ['w', 'h']
* 'w' => ['w']
*/
private parseFields(field: string[] | string): string[] {
if (Array.isArray(field)) {
return field;
}
if (isString(field)) {
return field.split('*');
}
return [field];
}
}

View File

@ -1,46 +1,46 @@
import BufferBase, { IEncodeFeature, Position } from '../../core/BaseBuffer';
import extrudePolygon, {
fillPolygon,
IExtrudeGeomety,
} from '../../point/shape/extrude';
import {
geometryShape,
ShapeType2D,
ShapeType3D,
} from '../../point/shape/Path';
export default class GridHeatMapBuffer extends BufferBase {
private verticesOffset: number = 0;
protected buildFeatures() {
this.verticesOffset = 0;
const layerData = this.data as IEncodeFeature[];
layerData.forEach((feature: IEncodeFeature) => {
this.calculateFill(feature);
});
}
protected calculateFeatures() {
const layerData = this.data as IEncodeFeature[];
const shape = layerData[0].shape as ShapeType3D | ShapeType2D;
this.verticesCount = layerData.length;
this.indexCount = 0;
this.instanceGeometry = this.getGeometry(shape as
| ShapeType2D
| ShapeType3D);
}
protected calculateFill(feature: IEncodeFeature) {
feature.bufferInfo = { verticesOffset: this.verticesOffset };
const coordinates = feature.coordinates as Position;
this.encodeArray(feature, 1);
this.attributes.positions.set([...coordinates, 1], this.verticesOffset * 3);
this.verticesOffset++;
}
private getGeometry(shape: ShapeType2D | ShapeType3D): IExtrudeGeomety {
const path = geometryShape[shape]
? geometryShape[shape]()
: geometryShape.circle();
// const geometry = ShapeType2D[str as ShapeType2D]
// ? fillPolygon([path])
// : extrudePolygon([path]);
const geometry = fillPolygon([path]);
return geometry;
}
}
// import BufferBase, { IEncodeFeature, Position } from '../../core/BaseBuffer';
// import extrudePolygon, {
// fillPolygon,
// IExtrudeGeomety,
// } from '../../point/shape/extrude';
// import {
// geometryShape,
// ShapeType2D,
// ShapeType3D,
// } from '../../point/shape/Path';
// export default class GridHeatMapBuffer extends BufferBase {
// private verticesOffset: number = 0;
// protected buildFeatures() {
// this.verticesOffset = 0;
// const layerData = this.data as IEncodeFeature[];
// layerData.forEach((feature: IEncodeFeature) => {
// this.calculateFill(feature);
// });
// }
// protected calculateFeatures() {
// const layerData = this.data as IEncodeFeature[];
// const shape = layerData[0].shape as ShapeType3D | ShapeType2D;
// this.verticesCount = layerData.length;
// this.indexCount = 0;
// this.instanceGeometry = this.getGeometry(shape as
// | ShapeType2D
// | ShapeType3D);
// }
// protected calculateFill(feature: IEncodeFeature) {
// feature.bufferInfo = { verticesOffset: this.verticesOffset };
// const coordinates = feature.coordinates as Position;
// this.encodeArray(feature, 1);
// this.attributes.positions.set([...coordinates, 1], this.verticesOffset * 3);
// this.verticesOffset++;
// }
// private getGeometry(shape: ShapeType2D | ShapeType3D): IExtrudeGeomety {
// const path = geometryShape[shape]
// ? geometryShape[shape]()
// : geometryShape.circle();
// // const geometry = ShapeType2D[str as ShapeType2D]
// // ? fillPolygon([path])
// // : extrudePolygon([path]);
// const geometry = fillPolygon([path]);
// return geometry;
// }
// }

View File

@ -1,112 +1,112 @@
import {
gl,
IRendererService,
IShaderModuleService,
lazyInject,
TYPES,
} from '@l7/core';
import BaseLayer from '../core/BaseLayer';
import GridHeatMapBuffer from './buffers/GridBuffer';
import hexagon_frag from './shaders/hexagon_frag.glsl';
import hexagon_vert from './shaders/hexagon_vert.glsl';
// import {
// gl,
// IRendererService,
// IShaderModuleService,
// lazyInject,
// TYPES,
// } from '@l7/core';
// import BaseLayer from '../core/BaseLayer';
// import GridHeatMapBuffer from './buffers/GridBuffer';
// import hexagon_frag from './shaders/hexagon_frag.glsl';
// import hexagon_vert from './shaders/hexagon_vert.glsl';
export default class HeatMapLayer extends BaseLayer {
public name: string = 'HeatMapLayer';
// export default class HeatMapLayer extends BaseLayer {
// public name: string = 'HeatMapLayer';
@lazyInject(TYPES.IShaderModuleService)
private readonly shaderModule: IShaderModuleService;
// @lazyInject(TYPES.IShaderModuleService)
// private readonly shaderModule: IShaderModuleService;
@lazyInject(TYPES.IRendererService)
private readonly renderer: IRendererService;
// @lazyInject(TYPES.IRendererService)
// private readonly renderer: IRendererService;
protected renderModels() {
this.models.forEach((model) =>
model.draw({
uniforms: {
u_ModelMatrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
},
}),
);
return this;
}
// protected renderModels() {
// this.models.forEach((model) =>
// model.draw({
// uniforms: {
// u_ModelMatrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
// },
// }),
// );
// return this;
// }
protected buildModels(): void {
this.shaderModule.registerModule('grid', {
vs: hexagon_vert,
fs: hexagon_frag,
});
this.models = [];
const { vs, fs, uniforms } = this.shaderModule.getModule('grid');
const buffer = new GridHeatMapBuffer({
data: this.getEncodedData(),
});
const {
createAttribute,
createBuffer,
createElements,
createModel,
} = this.renderer;
// protected buildModels(): void {
// this.shaderModule.registerModule('grid', {
// vs: hexagon_vert,
// fs: hexagon_frag,
// });
// this.models = [];
// const { vs, fs, uniforms } = this.shaderModule.getModule('grid');
// const buffer = new GridHeatMapBuffer({
// data: this.getEncodedData(),
// });
// const {
// createAttribute,
// createBuffer,
// createElements,
// createModel,
// } = this.renderer;
this.models.push(
createModel({
attributes: {
a_miter: createAttribute({
buffer: createBuffer({
data: buffer.instanceGeometry.positions,
type: gl.FLOAT,
}),
size: 3,
divisor: 0,
}),
// a_normal: createAttribute({
// buffer: createBuffer({
// data: buffer.attributes.normals,
// type: gl.FLOAT,
// }),
// size: 3,
// }),
a_color: createAttribute({
buffer: createBuffer({
data: buffer.attributes.colors,
type: gl.FLOAT,
}),
size: 4,
divisor: 1,
}),
// a_size: createAttribute({
// buffer: createBuffer({
// data: buffer.attributes.sizes,
// type: gl.FLOAT,
// }),
// size: 1,
// divisor: 1,
// }),
a_Position: createAttribute({
buffer: createBuffer({
data: buffer.attributes.positions,
type: gl.FLOAT,
}),
size: 3,
divisor: 1,
}),
},
uniforms: {
...uniforms,
u_opacity: (this.styleOption.opacity as number) || 1.0,
u_radius: [
this.getSource().data.xOffset,
this.getSource().data.yOffset,
],
},
fs,
vs,
count: buffer.instanceGeometry.index.length,
instances: buffer.verticesCount,
elements: createElements({
data: buffer.instanceGeometry.index,
type: gl.UNSIGNED_INT,
}),
}),
);
}
}
// this.models.push(
// createModel({
// attributes: {
// a_miter: createAttribute({
// buffer: createBuffer({
// data: buffer.instanceGeometry.positions,
// type: gl.FLOAT,
// }),
// size: 3,
// divisor: 0,
// }),
// // a_normal: createAttribute({
// // buffer: createBuffer({
// // data: buffer.attributes.normals,
// // type: gl.FLOAT,
// // }),
// // size: 3,
// // }),
// a_color: createAttribute({
// buffer: createBuffer({
// data: buffer.attributes.colors,
// type: gl.FLOAT,
// }),
// size: 4,
// divisor: 1,
// }),
// // a_size: createAttribute({
// // buffer: createBuffer({
// // data: buffer.attributes.sizes,
// // type: gl.FLOAT,
// // }),
// // size: 1,
// // divisor: 1,
// // }),
// a_Position: createAttribute({
// buffer: createBuffer({
// data: buffer.attributes.positions,
// type: gl.FLOAT,
// }),
// size: 3,
// divisor: 1,
// }),
// },
// uniforms: {
// ...uniforms,
// u_opacity: (this.styleOption.opacity as number) || 1.0,
// u_radius: [
// this.getSource().data.xOffset,
// this.getSource().data.yOffset,
// ],
// },
// fs,
// vs,
// count: buffer.instanceGeometry.index.length,
// instances: buffer.verticesCount,
// elements: createElements({
// data: buffer.instanceGeometry.index,
// type: gl.UNSIGNED_INT,
// }),
// }),
// );
// }
// }

View File

@ -1,16 +1,16 @@
import BaseLayer from './core/BaseLayer';
import HeatMapLayer from './heatmap';
import Line from './line';
import PointLayer from './point';
import Point from './point/point';
// import HeatMapLayer from './heatmap';
// import Line from './line';
// import PointLayer from './point';
// import Point from './point/point';
import PolygonLayer from './polygon';
import ImageLayer from './raster';
// import ImageLayer from './raster';
export {
BaseLayer,
PointLayer,
// PointLayer,
PolygonLayer,
Point,
Line,
ImageLayer,
HeatMapLayer,
// Point,
// Line,
// ImageLayer,
// HeatMapLayer,
};

View File

@ -1,95 +1,95 @@
import { lngLatToMeters, Point } from '@l7/utils';
import BufferBase, { IEncodeFeature, Position } from '../../core/BaseBuffer';
import getNormals from '../../utils/polylineNormal';
interface IBufferInfo {
normals: number[];
arrayIndex: number[];
positions: number[];
attrDistance: number[];
miters: number[];
verticesOffset: number;
indexOffset: number;
}
export default class LineBuffer extends BufferBase {
private hasPattern: boolean;
protected buildFeatures() {
const layerData = this.data as IEncodeFeature[];
layerData.forEach((feature: IEncodeFeature) => {
this.calculateLine(feature);
delete feature.bufferInfo;
});
this.hasPattern = layerData.some((feature: IEncodeFeature) => {
return feature.pattern;
});
}
protected initAttributes() {
super.initAttributes();
this.attributes.dashArray = new Float32Array(this.verticesCount);
this.attributes.attrDistance = new Float32Array(this.verticesCount);
this.attributes.totalDistances = new Float32Array(this.verticesCount);
this.attributes.patterns = new Float32Array(this.verticesCount * 2);
this.attributes.miters = new Float32Array(this.verticesCount);
this.attributes.normals = new Float32Array(this.verticesCount * 3);
}
protected calculateFeatures() {
const layerData = this.data as IEncodeFeature[];
// 计算长
layerData.forEach((feature: IEncodeFeature, index: number) => {
let coordinates = feature.coordinates as Position[] | Position[][];
if (Array.isArray(coordinates[0][0])) {
coordinates = coordinates[0] as Position[];
}
// @ts-ignore
const projectCoord: number[][] = coordinates.map((item: Position[]) => {
// @ts-ignore
const p: Point = [...item];
return lngLatToMeters(p);
});
const { normals, attrIndex, attrPos, attrDistance, miters } = getNormals(
coordinates as number[][],
false,
this.verticesCount,
);
const bufferInfo: IBufferInfo = {
normals,
arrayIndex: attrIndex,
positions: attrPos,
attrDistance,
miters,
verticesOffset: this.verticesCount,
indexOffset: this.indexCount,
};
this.verticesCount += attrPos.length / 3;
this.indexCount += attrIndex.length;
feature.bufferInfo = bufferInfo;
});
}
private calculateLine(feature: IEncodeFeature) {
const bufferInfo = feature.bufferInfo as IBufferInfo;
const {
normals,
arrayIndex,
positions,
attrDistance,
miters,
verticesOffset,
indexOffset,
} = bufferInfo;
const { dashArray = 200 } = this.style;
// import { lngLatToMeters, Point } from '@l7/utils';
// import BufferBase, { IEncodeFeature, Position } from '../../core/BaseBuffer';
// import getNormals from '../../utils/polylineNormal';
// interface IBufferInfo {
// normals: number[];
// arrayIndex: number[];
// positions: number[];
// attrDistance: number[];
// miters: number[];
// verticesOffset: number;
// indexOffset: number;
// }
// export default class LineBuffer extends BufferBase {
// private hasPattern: boolean;
// protected buildFeatures() {
// const layerData = this.data as IEncodeFeature[];
// layerData.forEach((feature: IEncodeFeature) => {
// this.calculateLine(feature);
// delete feature.bufferInfo;
// });
// this.hasPattern = layerData.some((feature: IEncodeFeature) => {
// return feature.pattern;
// });
// }
// protected initAttributes() {
// super.initAttributes();
// this.attributes.dashArray = new Float32Array(this.verticesCount);
// this.attributes.attrDistance = new Float32Array(this.verticesCount);
// this.attributes.totalDistances = new Float32Array(this.verticesCount);
// this.attributes.patterns = new Float32Array(this.verticesCount * 2);
// this.attributes.miters = new Float32Array(this.verticesCount);
// this.attributes.normals = new Float32Array(this.verticesCount * 3);
// }
// protected calculateFeatures() {
// const layerData = this.data as IEncodeFeature[];
// // 计算长
// layerData.forEach((feature: IEncodeFeature, index: number) => {
// let coordinates = feature.coordinates as Position[] | Position[][];
// if (Array.isArray(coordinates[0][0])) {
// coordinates = coordinates[0] as Position[];
// }
// // @ts-ignore
// const projectCoord: number[][] = coordinates.map((item: Position[]) => {
// // @ts-ignore
// const p: Point = [...item];
// return lngLatToMeters(p);
// });
// const { normals, attrIndex, attrPos, attrDistance, miters } = getNormals(
// coordinates as number[][],
// false,
// this.verticesCount,
// );
// const bufferInfo: IBufferInfo = {
// normals,
// arrayIndex: attrIndex,
// positions: attrPos,
// attrDistance,
// miters,
// verticesOffset: this.verticesCount,
// indexOffset: this.indexCount,
// };
// this.verticesCount += attrPos.length / 3;
// this.indexCount += attrIndex.length;
// feature.bufferInfo = bufferInfo;
// });
// }
// private calculateLine(feature: IEncodeFeature) {
// const bufferInfo = feature.bufferInfo as IBufferInfo;
// const {
// normals,
// arrayIndex,
// positions,
// attrDistance,
// miters,
// verticesOffset,
// indexOffset,
// } = bufferInfo;
// const { dashArray = 200 } = this.style;
this.encodeArray(feature, positions.length / 3);
const totalLength = attrDistance[attrDistance.length - 1];
// 增加长度
const totalDistances = Array(positions.length / 3).fill(totalLength);
// 虚线比例
const ratio = dashArray / totalLength;
const dashArrays = Array(positions.length / 3).fill(ratio);
this.attributes.positions.set(positions, verticesOffset * 3);
this.indexArray.set(arrayIndex, indexOffset);
this.attributes.miters.set(miters, verticesOffset);
this.attributes.normals.set(normals, verticesOffset * 3);
this.attributes.attrDistance.set(attrDistance, verticesOffset);
this.attributes.totalDistances.set(totalDistances, verticesOffset);
this.attributes.dashArray.set(dashArrays, verticesOffset);
}
}
// this.encodeArray(feature, positions.length / 3);
// const totalLength = attrDistance[attrDistance.length - 1];
// // 增加长度
// const totalDistances = Array(positions.length / 3).fill(totalLength);
// // 虚线比例
// const ratio = dashArray / totalLength;
// const dashArrays = Array(positions.length / 3).fill(ratio);
// this.attributes.positions.set(positions, verticesOffset * 3);
// this.indexArray.set(arrayIndex, indexOffset);
// this.attributes.miters.set(miters, verticesOffset);
// this.attributes.normals.set(normals, verticesOffset * 3);
// this.attributes.attrDistance.set(attrDistance, verticesOffset);
// this.attributes.totalDistances.set(totalDistances, verticesOffset);
// this.attributes.dashArray.set(dashArrays, verticesOffset);
// }
// }

View File

@ -1,102 +1,102 @@
import {
gl,
IRendererService,
IShaderModuleService,
lazyInject,
TYPES,
} from '@l7/core';
import BaseLayer from '../core/BaseLayer';
import LineBuffer from './buffers/line';
import line_frag from './shaders/line_frag.glsl';
import line_vert from './shaders/line_vert.glsl';
export default class LineLayer extends BaseLayer {
public name: string = 'LineLayer';
@lazyInject(TYPES.IShaderModuleService)
private readonly shaderModule: IShaderModuleService;
// import {
// gl,
// IRendererService,
// IShaderModuleService,
// lazyInject,
// TYPES,
// } from '@l7/core';
// import BaseLayer from '../core/BaseLayer';
// import LineBuffer from './buffers/line';
// import line_frag from './shaders/line_frag.glsl';
// import line_vert from './shaders/line_vert.glsl';
// export default class LineLayer extends BaseLayer {
// public name: string = 'LineLayer';
// @lazyInject(TYPES.IShaderModuleService)
// private readonly shaderModule: IShaderModuleService;
@lazyInject(TYPES.IRendererService)
private readonly renderer: IRendererService;
// @lazyInject(TYPES.IRendererService)
// private readonly renderer: IRendererService;
protected renderModels() {
this.models.forEach((model) =>
model.draw({
uniforms: {
u_ModelMatrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
},
}),
);
return this;
}
protected buildModels(): void {
this.shaderModule.registerModule('line', {
vs: line_vert,
fs: line_frag,
});
// protected renderModels() {
// this.models.forEach((model) =>
// model.draw({
// uniforms: {
// u_ModelMatrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
// },
// }),
// );
// return this;
// }
// protected buildModels(): void {
// this.shaderModule.registerModule('line', {
// vs: line_vert,
// fs: line_frag,
// });
this.models = [];
const { vs, fs, uniforms } = this.shaderModule.getModule('line');
const buffer = new LineBuffer({
data: this.getEncodedData(),
style: this.styleOption,
});
const {
createAttribute,
createBuffer,
createElements,
createModel,
} = this.renderer;
// this.models = [];
// const { vs, fs, uniforms } = this.shaderModule.getModule('line');
// const buffer = new LineBuffer({
// data: this.getEncodedData(),
// style: this.styleOption,
// });
// const {
// createAttribute,
// createBuffer,
// createElements,
// createModel,
// } = this.renderer;
this.models.push(
createModel({
attributes: {
a_Position: createAttribute({
buffer: createBuffer({
data: buffer.attributes.positions,
type: gl.FLOAT,
}),
size: 3,
}),
a_normal: createAttribute({
buffer: createBuffer({
data: buffer.attributes.normals,
type: gl.FLOAT,
}),
size: 3,
}),
a_color: createAttribute({
buffer: createBuffer({
data: buffer.attributes.colors,
type: gl.FLOAT,
}),
size: 4,
}),
a_size: createAttribute({
buffer: createBuffer({
data: buffer.attributes.sizes,
type: gl.FLOAT,
}),
size: 1,
}),
a_miter: createAttribute({
buffer: createBuffer({
data: buffer.attributes.miters,
type: gl.FLOAT,
}),
size: 1,
}),
},
uniforms: {
...uniforms,
u_opacity: this.styleOption.opacity as number,
},
fs,
vs,
count: buffer.indexArray.length,
elements: createElements({
data: buffer.indexArray,
type: gl.UNSIGNED_INT,
}),
}),
);
}
}
// this.models.push(
// createModel({
// attributes: {
// a_Position: createAttribute({
// buffer: createBuffer({
// data: buffer.attributes.positions,
// type: gl.FLOAT,
// }),
// size: 3,
// }),
// a_normal: createAttribute({
// buffer: createBuffer({
// data: buffer.attributes.normals,
// type: gl.FLOAT,
// }),
// size: 3,
// }),
// a_color: createAttribute({
// buffer: createBuffer({
// data: buffer.attributes.colors,
// type: gl.FLOAT,
// }),
// size: 4,
// }),
// a_size: createAttribute({
// buffer: createBuffer({
// data: buffer.attributes.sizes,
// type: gl.FLOAT,
// }),
// size: 1,
// }),
// a_miter: createAttribute({
// buffer: createBuffer({
// data: buffer.attributes.miters,
// type: gl.FLOAT,
// }),
// size: 1,
// }),
// },
// uniforms: {
// ...uniforms,
// u_opacity: this.styleOption.opacity as number,
// },
// fs,
// vs,
// count: buffer.indexArray.length,
// elements: createElements({
// data: buffer.indexArray,
// type: gl.UNSIGNED_INT,
// }),
// }),
// );
// }
// }

View File

@ -1,125 +0,0 @@
import {
IGlobalConfigService,
ILayer,
ILayerPlugin,
ILayerStyleAttribute,
IParseDataItem,
IStyleScale,
lazyInject,
StyleScaleType,
TYPES,
} from '@l7/core';
import { isString } from 'lodash';
import ScaleController from '../core/ScaleController';
import { rgb2arr } from '../utils/color';
export default class DataEncodePlugin implements ILayerPlugin {
@lazyInject(TYPES.IGlobalConfigService)
private readonly configService: IGlobalConfigService;
private scaleController: ScaleController;
private scaleCache: {
[fieldName: string]: IStyleScale;
} = {};
public apply(layer: ILayer) {
layer.hooks.init.tap('DataEncodePlugin', () => {
const source = layer.getSource();
const dataArray = source && source.data && source.data.dataArray;
if (!dataArray) {
return;
}
this.scaleController = new ScaleController(
this.configService.getConfig().scales || {},
);
// create scales by source data & config
Object.keys(layer.styleAttributes).forEach((attributeName) => {
const attribute = layer.styleAttributes[attributeName];
const scales: any[] = [];
attribute.names.forEach((field: string) => {
scales.push(this.getOrCreateScale(attribute, field, dataArray));
});
attribute.setScales(scales);
});
// mapping with source data
layer.setEncodedData(this.mapping(layer.styleAttributes, dataArray));
});
// TODO: remapping before render
// layer.hooks.beforeRender.tap()
}
private getOrCreateScale(
attribute: ILayerStyleAttribute,
field: string,
data: any[],
): IStyleScale {
let scale = this.scaleCache[field as string];
if (!scale) {
scale = this.scaleController.createScale(field as string, data);
if (
scale.type === StyleScaleType.VARIABLE &&
attribute.values &&
attribute.values.length > 0
) {
scale.scale.range(attribute.values);
}
this.scaleCache[field as string] = scale;
}
return {
...scale,
scale: scale.scale.copy(), // 存在相同字段映射不同通道的情况
};
}
private mapping(
attributes: {
[attributeName: string]: ILayerStyleAttribute;
},
data: IParseDataItem[],
): Array<{ [key: string]: unknown }> {
return data.map((record: IParseDataItem) => {
const encodeRecord: { [key: string]: unknown } = {
id: record._id,
coordinates: record.coordinates,
};
Object.keys(attributes).forEach((attributeName: string) => {
const attribute = attributes[attributeName];
// const { type } = attribute;
// if (type === StyleScaleType.CONSTANT) {
// return;
// }
let values = this.getAttrValue(attribute, record);
if (attributeName === 'color') {
values = values.map((c: unknown) => {
return rgb2arr(c as string);
});
}
encodeRecord[attributeName] =
Array.isArray(values) && values.length === 1 ? values[0] : values;
});
return encodeRecord;
});
}
private getAttrValue(
attribute: ILayerStyleAttribute,
record: { [key: string]: unknown },
) {
const scales = attribute.scales || [];
const params: unknown[] = [];
scales.forEach((scale) => {
const { field, type } = scale;
if (type === StyleScaleType.CONSTANT) {
params.push(scale.field);
} else {
params.push(record[field]);
}
});
return attribute.mapping ? attribute.mapping(...params) : [];
}
}

View File

@ -0,0 +1,98 @@
import {
IEncodeFeature,
IGlobalConfigService,
ILayer,
ILayerPlugin,
ILogService,
IParseDataItem,
IStyleAttribute,
lazyInject,
TYPES,
} from '@l7/core';
import { rgb2arr } from '../utils/color';
export default class DataMappingPlugin implements ILayerPlugin {
@lazyInject(TYPES.IGlobalConfigService)
private readonly configService: IGlobalConfigService;
@lazyInject(TYPES.ILogService)
private readonly logger: ILogService;
public apply(layer: ILayer) {
layer.hooks.init.tap('DataMappingPlugin', () => {
const attributes =
layer.styleAttributeService.getLayerStyleAttributes() || [];
const { dataArray } = layer.getSource().data;
// TODO: FIXME
if (!dataArray) {
return;
}
// mapping with source data
layer.setEncodedData(this.mapping(attributes, dataArray));
});
// remapping before render
layer.hooks.beforeRender.tap('DataMappingPlugin', () => {
const attributes =
layer.styleAttributeService.getLayerStyleAttributes() || [];
const { dataArray } = layer.getSource().data;
const attributesToRemapping = attributes.filter(
(attribute) => attribute.needRemapping,
);
if (attributesToRemapping.length) {
layer.setEncodedData(this.mapping(attributesToRemapping, dataArray));
this.logger.info('remapping finished');
}
});
}
private mapping(
attributes: IStyleAttribute[],
data: IParseDataItem[],
): IEncodeFeature[] {
return data.map((record: IParseDataItem) => {
const encodeRecord: IEncodeFeature = {
id: record._id,
coordinates: record.coordinates,
};
// TODO 数据过滤
attributes.forEach((attribute: IStyleAttribute) => {
let values = this.applyAttributeMapping(attribute, record);
attribute.needRemapping = false;
// TODO: 支持每个属性配置 postprocess
if (attribute.name === 'color') {
values = values.map((c: unknown) => {
return rgb2arr(c as string);
});
}
// @ts-ignore
encodeRecord[attribute.name] =
Array.isArray(values) && values.length === 1 ? values[0] : values;
});
return encodeRecord;
}) as IEncodeFeature[];
}
private applyAttributeMapping(
attribute: IStyleAttribute,
record: { [key: string]: unknown },
) {
if (!attribute.scale) {
return [];
}
const scalers = attribute!.scale!.scalers || [];
const params: unknown[] = [];
scalers.forEach(({ field }) => {
if (record[field]) {
params.push(record[field]);
}
});
return attribute.mapping ? attribute.mapping(params) : [];
}
}

View File

@ -1,15 +1,4 @@
import {
IGlobalConfigService,
ILayer,
ILayerPlugin,
ILayerStyleAttribute,
IParseDataItem,
ISourceCFG,
IStyleScale,
lazyInject,
StyleScaleType,
TYPES,
} from '@l7/core';
import { ILayer, ILayerPlugin } from '@l7/core';
import Source from '@l7/source';
export default class DataSourcePlugin implements ILayerPlugin {
public apply(layer: ILayer) {

View File

@ -0,0 +1,174 @@
import {
IGlobalConfigService,
ILayer,
ILayerPlugin,
ILogService,
IScale,
IStyleAttribute,
lazyInject,
ScaleTypes,
TYPES,
} from '@l7/core';
import { IParseDataItem } from '@l7/source';
import { extent } from 'd3-array';
import * as d3 from 'd3-scale';
import { isNil, isNumber, isString, uniq } from 'lodash';
const dateRegex = /^(?:(?!0000)[0-9]{4}([-/.]+)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-/.]?)0?2\2(?:29))(\s+([01]|([01][0-9]|2[0-3])):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9]))?$/;
const scaleMap = {
[ScaleTypes.LINEAR]: d3.scaleLinear,
[ScaleTypes.POWER]: d3.scalePow,
[ScaleTypes.LOG]: d3.scaleLog,
[ScaleTypes.IDENTITY]: d3.scaleIdentity,
[ScaleTypes.TIME]: d3.scaleTime,
[ScaleTypes.QUANTILE]: d3.scaleQuantile,
[ScaleTypes.QUANTIZE]: d3.scaleQuantize,
[ScaleTypes.THRESHOLD]: d3.scaleThreshold,
[ScaleTypes.CAT]: d3.scaleOrdinal,
};
/**
* Source Scale StyleAttribute 使
*/
export default class FeatureScalePlugin implements ILayerPlugin {
@lazyInject(TYPES.IGlobalConfigService)
private readonly configService: IGlobalConfigService;
@lazyInject(TYPES.ILogService)
private readonly logger: ILogService;
private scaleCache: {
[field: string]: unknown;
} = {};
public apply(layer: ILayer) {
layer.hooks.init.tap('FeatureScalePlugin', () => {
const attributes = layer.styleAttributeService.getLayerStyleAttributes();
const { dataArray } = layer.getSource().data;
this.caculateScalesForAttributes(attributes || [], dataArray);
});
layer.hooks.beforeRender.tap('FeatureScalePlugin', () => {
const attributes = layer.styleAttributeService.getLayerStyleAttributes();
if (attributes) {
const { dataArray } = layer.getSource().data;
const attributesToRescale = attributes.filter(
(attribute) => attribute.needRescale,
);
if (attributesToRescale.length) {
this.caculateScalesForAttributes(attributesToRescale, dataArray);
this.logger.info('rescale finished');
}
}
});
}
private caculateScalesForAttributes(
attributes: IStyleAttribute[],
dataArray: IParseDataItem[],
) {
this.scaleCache = {};
attributes.forEach((attribute) => {
if (attribute.scale) {
attribute!.scale!.scalers = this.parseFields(
attribute!.scale!.field || '',
).map((field: string) => ({
field,
func: this.getOrCreateScale(field, attribute, dataArray),
}));
attribute.needRescale = false;
}
});
}
private getOrCreateScale(
field: string,
attribute: IStyleAttribute,
dataArray: IParseDataItem[],
) {
if (this.scaleCache[field]) {
return this.scaleCache[field];
}
this.scaleCache[field] = this.createScale(field, dataArray);
(this.scaleCache[field] as {
range: (c: unknown[]) => void;
}).range(attribute!.scale!.values);
return this.scaleCache[field];
}
/**
* @example
* 'w*h' => ['w', 'h']
* 'w' => ['w']
*/
private parseFields(field: string[] | string): string[] {
if (Array.isArray(field)) {
return field;
}
if (isString(field)) {
return field.split('*');
}
return [field];
}
private createScale(field: string, data?: IParseDataItem[]): unknown {
// 首先查找全局默认配置例如 color
const scaleOption: IScale = this.configService.getConfig()!.scales![field];
if (!data || !data.length) {
// 数据为空
return scaleOption && scaleOption.type
? this.createDefaultScale(scaleOption)
: d3.scaleOrdinal([field]);
}
const firstValue = data!.find((d) => !isNil(d[field]))![field];
// 常量 Scale
if (isNumber(field) || (isNil(firstValue) && !scaleOption)) {
return d3.scaleOrdinal([field]);
} else {
// 根据数据类型判断 默认等分位,时间,和枚举类型
const type =
(scaleOption && scaleOption.type) || this.getDefaultType(firstValue);
return this.createDefaultScale(
this.createDefaultScaleConfig(type, field, data),
);
}
}
private getDefaultType(firstValue: unknown) {
let type = ScaleTypes.QUANTIZE;
if (typeof firstValue === 'string') {
type = dateRegex.test(firstValue) ? ScaleTypes.TIME : ScaleTypes.CAT;
}
return type;
}
private createDefaultScaleConfig(
type: ScaleTypes,
field: string,
data?: IParseDataItem[],
) {
const cfg: IScale = {
type,
};
const values = data!.map((item) => item[field]);
// 默认类型为 Quantile Scales https://github.com/d3/d3-scale#quantile-scales
if (type !== ScaleTypes.CAT && type !== ScaleTypes.QUANTILE) {
cfg.domain = extent(values);
} else if (type === ScaleTypes.CAT) {
cfg.domain = uniq(values);
}
return cfg;
}
private createDefaultScale({ type, domain }: IScale) {
// @ts-ignore
const scale = scaleMap[type]();
if (domain) {
scale.domain(domain);
}
// TODO 其他属性支持
return scale;
}
}

View File

@ -58,10 +58,7 @@ export default class MultiPassRendererPlugin implements ILayerPlugin {
public apply(layer: ILayer) {
layer.hooks.init.tap('MultiPassRendererPlugin', () => {
const {
enableMultiPassRenderer,
passes = [],
} = layer.getInitializationOptions();
const { enableMultiPassRenderer, passes = [] } = layer.getStyleOptions();
// SceneConfig 的 enableMultiPassRenderer 配置项可以统一关闭
this.enabled =
@ -103,13 +100,12 @@ export default class MultiPassRendererPlugin implements ILayerPlugin {
multiPassRenderer.add(new ClearPass());
if (layer.getInitializationOptions().enablePicking) {
if (layer.getStyleOptions().enablePicking) {
multiPassRenderer.add(new PixelPickingPass());
}
multiPassRenderer.add(new RenderPass());
// post processing
// TODO: pass initialization params
normalizePasses(passes).forEach(
(pass: [string, { [key: string]: unknown }]) => {
const PostProcessingPassClazz = builtinPostProcessingPassMap[pass[0]];

View File

@ -0,0 +1,92 @@
import {
AttributeType,
gl,
IEncodeFeature,
ILayer,
ILayerPlugin,
IRendererService,
lazyInject,
TYPES,
} from '@l7/core';
import { rgb2arr } from '../utils/color';
function encodePickingColor(featureIdx: number): [number, number, number] {
return [
(featureIdx + 1) & 255,
((featureIdx + 1) >> 8) & 255,
(((featureIdx + 1) >> 8) >> 8) & 255,
];
}
const PickingStage = {
NONE: 0.0,
ENCODE: 1.0,
HIGHLIGHT: 2.0,
};
export default class PixelPickingPlugin implements ILayerPlugin {
@lazyInject(TYPES.IRendererService)
private readonly rendererService: IRendererService;
public apply(layer: ILayer) {
// TODO: 由于 Shader 目前无法根据是否开启拾取进行内容修改,因此即使不开启也需要生成 a_PickingColor
layer.hooks.init.tap('PixelPickingPlugin', () => {
const { enablePicking } = layer.getStyleOptions();
layer.styleAttributeService.registerStyleAttribute({
name: 'pickingColor',
type: AttributeType.Attribute,
descriptor: {
name: 'a_PickingColor',
buffer: {
data: [],
type: gl.FLOAT,
},
size: 3,
// TODO: 固定 feature range 范围内的 pickingColor 都是固定的,可以生成 cache
update: (feature: IEncodeFeature, featureIdx: number) =>
// 只有开启拾取才需要 encode
enablePicking ? encodePickingColor(featureIdx) : [0, 0, 0],
},
});
});
// 必须要与 PixelPickingPass 结合使用,因此必须开启 multiPassRenderer
// if (layer.multiPassRenderer) {
layer.hooks.beforeRender.tap('PixelPickingPlugin', () => {
layer.models.forEach((model) =>
model.addUniforms({
u_PickingStage: PickingStage.ENCODE,
}),
);
});
layer.hooks.afterRender.tap('PixelPickingPlugin', () => {
layer.models.forEach((model) =>
model.addUniforms({
u_PickingStage: PickingStage.NONE,
u_PickingColor: [0, 0, 0],
u_HighlightColor: [0, 0, 0, 0],
}),
);
});
layer.hooks.beforeHighlight.tap(
'PixelPickingPlugin',
// @ts-ignore
(l: unknown, pickedColor: unknown) => {
const { highlightColor } = layer.getStyleOptions();
const highlightColorInArray =
typeof highlightColor === 'string'
? rgb2arr(highlightColor)
: highlightColor || [1, 0, 0, 1];
layer.models.forEach((model) =>
model.addUniforms({
u_PickingStage: PickingStage.HIGHLIGHT,
u_PickingColor: pickedColor as number[],
u_HighlightColor: highlightColorInArray.map((c) => c * 255),
}),
);
},
);
// }
}
}

View File

@ -0,0 +1,66 @@
import {
AttributeType,
gl,
IEncodeFeature,
ILayer,
ILayerPlugin,
ILogService,
IStyleAttributeService,
lazyInject,
TYPES,
} from '@l7/core';
/**
* Layer indices attribute
*/
export default class RegisterStyleAttributePlugin implements ILayerPlugin {
@lazyInject(TYPES.ILogService)
private readonly logger: ILogService;
public apply(layer: ILayer) {
layer.hooks.init.tap('RegisterStyleAttributePlugin', () => {
this.registerBuiltinAttributes(layer);
});
}
private registerBuiltinAttributes(layer: ILayer) {
layer.styleAttributeService.registerStyleAttribute({
name: 'position',
type: AttributeType.Attribute,
descriptor: {
name: 'a_Position',
buffer: {
data: [],
type: gl.FLOAT,
},
size: 3,
update: (
feature: IEncodeFeature,
featureIdx: number,
vertex: number[],
) => {
return [vertex[0], vertex[1], 0];
},
},
});
layer.styleAttributeService.registerStyleAttribute({
name: 'color',
type: AttributeType.Attribute,
descriptor: {
name: 'a_Color',
buffer: {
// give the WebGL driver a hint that this buffer may change
usage: gl.DYNAMIC_DRAW,
data: [],
type: gl.FLOAT,
},
size: 4,
update: (feature: IEncodeFeature, featureIdx: number) => {
const { color } = feature;
return !color || !color.length ? [0, 0, 0, 0] : color;
},
},
});
}
}

View File

@ -53,6 +53,7 @@ export default class ShaderUniformPlugin implements ILayerPlugin {
// 其他参数例如视口大小、DPR 等
u_ViewportSize: [width, height],
u_DevicePixelRatio: window.devicePixelRatio,
u_ModelMatrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
}),
);

View File

@ -0,0 +1,31 @@
import { ILayer, ILayerPlugin, ILogService, lazyInject, TYPES } from '@l7/core';
/**
* Layer indices attribute
*/
export default class UpdateStyleAttributePlugin implements ILayerPlugin {
@lazyInject(TYPES.ILogService)
private readonly logger: ILogService;
public apply(layer: ILayer) {
layer.hooks.beforeRender.tap('UpdateStyleAttributePlugin', () => {
const attributes =
layer.styleAttributeService.getLayerStyleAttributes() || [];
attributes
.filter((attribute) => attribute.needRegenerateVertices)
.forEach((attribute) => {
// 精确更新某个/某些 feature(s),需要传入 featureIdx
layer.styleAttributeService.updateAttributeByFeatureRange(
attribute.name,
layer.getEncodedData(), // 获取经过 mapping 最新的数据
attribute.featureRange.startIndex,
attribute.featureRange.endIndex,
);
attribute.needRegenerateVertices = false;
this.logger.info(
`regenerate vertex attributes: ${attribute.name} finished`,
);
});
});
}
}

View File

@ -1,79 +1,79 @@
import BaseBuffer, {
IBufferInfo,
IEncodeFeature,
Position,
} from '../../core/BaseBuffer';
import extrudePolygon, { IExtrudeGeomety } from '../shape/extrude';
import { geometryShape, ShapeType2D, ShapeType3D } from '../shape/Path';
interface IGeometryCache {
[key: string]: IExtrudeGeomety;
}
export default class ExtrudeBuffer extends BaseBuffer {
private indexOffset: number = 0;
private verticesOffset: number = 0;
private geometryCache: IGeometryCache;
public buildFeatures() {
const layerData = this.data as IEncodeFeature[];
layerData.forEach((feature: IEncodeFeature) => {
this.calculateFill(feature);
});
}
// import BaseBuffer, {
// IBufferInfo,
// IEncodeFeature,
// Position,
// } from '../../core/BaseBuffer';
// import extrudePolygon, { IExtrudeGeomety } from '../shape/extrude';
// import { geometryShape, ShapeType2D, ShapeType3D } from '../shape/Path';
// interface IGeometryCache {
// [key: string]: IExtrudeGeomety;
// }
// export default class ExtrudeBuffer extends BaseBuffer {
// private indexOffset: number = 0;
// private verticesOffset: number = 0;
// private geometryCache: IGeometryCache;
// public buildFeatures() {
// const layerData = this.data as IEncodeFeature[];
// layerData.forEach((feature: IEncodeFeature) => {
// this.calculateFill(feature);
// });
// }
protected calculateFeatures() {
const layerData = this.data as IEncodeFeature[];
this.geometryCache = {};
this.verticesOffset = 0;
this.indexOffset = 0;
layerData.forEach((feature: IEncodeFeature) => {
const { shape } = feature;
const { positions, index } = this.getGeometry(shape as ShapeType3D);
this.verticesCount += positions.length / 3;
this.indexCount += index.length;
});
}
protected initAttributes() {
super.initAttributes();
this.attributes.miters = new Float32Array(this.verticesCount * 3);
this.attributes.normals = new Float32Array(this.verticesCount * 3);
this.attributes.sizes = new Float32Array(this.verticesCount * 3);
}
private calculateFill(feature: IEncodeFeature) {
const { coordinates, shape } = feature;
const instanceGeometry = this.getGeometry(shape as ShapeType3D);
const numPoint = instanceGeometry.positions.length / 3;
feature.bufferInfo = {
verticesOffset: this.verticesOffset,
indexOffset: this.indexOffset,
dimensions: 3,
};
this.encodeArray(feature, numPoint);
this.attributes.miters.set(
instanceGeometry.positions,
this.verticesOffset * 3,
);
const indexArray = instanceGeometry.index.map((v) => {
return v + this.verticesOffset;
});
this.indexArray.set(indexArray, this.indexOffset);
const position: number[] = [];
for (let i = 0; i < numPoint; i++) {
const coor = coordinates as Position;
position.push(coor[0], coor[1], coor[2] || 0);
}
this.attributes.positions.set(position, this.verticesOffset * 3);
this.verticesOffset += numPoint;
this.indexOffset += indexArray.length;
}
// protected calculateFeatures() {
// const layerData = this.data as IEncodeFeature[];
// this.geometryCache = {};
// this.verticesOffset = 0;
// this.indexOffset = 0;
// layerData.forEach((feature: IEncodeFeature) => {
// const { shape } = feature;
// const { positions, index } = this.getGeometry(shape as ShapeType3D);
// this.verticesCount += positions.length / 3;
// this.indexCount += index.length;
// });
// }
// protected initAttributes() {
// super.initAttributes();
// this.attributes.miters = new Float32Array(this.verticesCount * 3);
// this.attributes.normals = new Float32Array(this.verticesCount * 3);
// this.attributes.sizes = new Float32Array(this.verticesCount * 3);
// }
// private calculateFill(feature: IEncodeFeature) {
// const { coordinates, shape } = feature;
// const instanceGeometry = this.getGeometry(shape as ShapeType3D);
// const numPoint = instanceGeometry.positions.length / 3;
// feature.bufferInfo = {
// verticesOffset: this.verticesOffset,
// indexOffset: this.indexOffset,
// dimensions: 3,
// };
// this.encodeArray(feature, numPoint);
// this.attributes.miters.set(
// instanceGeometry.positions,
// this.verticesOffset * 3,
// );
// const indexArray = instanceGeometry.index.map((v) => {
// return v + this.verticesOffset;
// });
// this.indexArray.set(indexArray, this.indexOffset);
// const position: number[] = [];
// for (let i = 0; i < numPoint; i++) {
// const coor = coordinates as Position;
// position.push(coor[0], coor[1], coor[2] || 0);
// }
// this.attributes.positions.set(position, this.verticesOffset * 3);
// this.verticesOffset += numPoint;
// this.indexOffset += indexArray.length;
// }
private getGeometry(shape: ShapeType3D): IExtrudeGeomety {
if (this.geometryCache && this.geometryCache[shape]) {
return this.geometryCache[shape];
}
const path = geometryShape[shape]
? geometryShape[shape]()
: geometryShape.cylinder();
const geometry = extrudePolygon([path]);
this.geometryCache[shape] = geometry;
return geometry;
}
}
// private getGeometry(shape: ShapeType3D): IExtrudeGeomety {
// if (this.geometryCache && this.geometryCache[shape]) {
// return this.geometryCache[shape];
// }
// const path = geometryShape[shape]
// ? geometryShape[shape]()
// : geometryShape.cylinder();
// const geometry = extrudePolygon([path]);
// this.geometryCache[shape] = geometry;
// return geometry;
// }
// }

View File

@ -1,22 +1,22 @@
import BaseBuffer, { IEncodeFeature, Position } from '../../core/BaseBuffer';
export default class ImageBuffer extends BaseBuffer {
protected calculateFeatures() {
const layerData = this.data as IEncodeFeature[];
this.verticesCount = layerData.length;
this.indexCount = layerData.length;
}
protected buildFeatures() {
const layerData = this.data as IEncodeFeature[];
this.attributes.uv = new Float32Array(this.verticesCount * 2);
layerData.forEach((item: IEncodeFeature, index: number) => {
const { color = [0, 0, 0, 0], size, id, shape, coordinates } = item;
const { x, y } = this.iconMap[shape as string] || { x: 0, y: 0 };
const coor = coordinates as Position;
this.attributes.positions.set(coor, index * 3);
this.attributes.colors.set(color, index * 4);
this.attributes.pickingIds.set([id as number], index);
this.attributes.sizes.set([size as number], index); //
this.attributes.uv.set([x, y], index * 2);
});
}
}
// import BaseBuffer, { IEncodeFeature, Position } from '../../core/BaseBuffer';
// export default class ImageBuffer extends BaseBuffer {
// protected calculateFeatures() {
// const layerData = this.data as IEncodeFeature[];
// this.verticesCount = layerData.length;
// this.indexCount = layerData.length;
// }
// protected buildFeatures() {
// const layerData = this.data as IEncodeFeature[];
// this.attributes.uv = new Float32Array(this.verticesCount * 2);
// layerData.forEach((item: IEncodeFeature, index: number) => {
// const { color = [0, 0, 0, 0], size, id, shape, coordinates } = item;
// const { x, y } = this.iconMap[shape as string] || { x: 0, y: 0 };
// const coor = coordinates as Position;
// this.attributes.positions.set(coor, index * 3);
// this.attributes.colors.set(color, index * 4);
// this.attributes.pickingIds.set([id as number], index);
// this.attributes.sizes.set([size as number], index); //
// this.attributes.uv.set([x, y], index * 2);
// });
// }
// }

View File

@ -1,216 +1,216 @@
import {
gl,
ILayer,
IRendererService,
IShaderModuleService,
lazyInject,
packCircleVertex,
TYPES,
} from '@l7/core';
import { featureEach } from '@turf/meta';
import BaseLayer from '../core/BaseLayer';
import circleFrag from './shaders/circle_frag.glsl';
import circleVert from './shaders/circle_vert.glsl';
// import {
// gl,
// ILayer,
// IRendererService,
// IShaderModuleService,
// lazyInject,
// packCircleVertex,
// TYPES,
// } from '@l7/core';
// import { featureEach } from '@turf/meta';
// import BaseLayer from '../core/BaseLayer';
// import circleFrag from './shaders/circle_frag.glsl';
// import circleVert from './shaders/circle_vert.glsl';
export interface IPointLayerStyleOptions {
pointShape: string;
pointColor: [number, number, number];
pointRadius: number;
pointOpacity: number;
strokeWidth: number;
strokeColor: [number, number, number];
strokeOpacity: number;
}
// export interface IPointLayerStyleOptions {
// pointShape: string;
// pointColor: [number, number, number];
// pointRadius: number;
// pointOpacity: number;
// strokeWidth: number;
// strokeColor: [number, number, number];
// strokeOpacity: number;
// }
interface IPointFeature {
coordinates: [number, number] | [number, number];
[key: string]: any;
}
// interface IPointFeature {
// coordinates: [number, number] | [number, number];
// [key: string]: any;
// }
/**
* PointLayer
*/
export default class PointLayer extends BaseLayer {
public styleOptions: IPointLayerStyleOptions = {
pointShape: 'circle',
pointColor: [81, 187, 214],
pointRadius: 10,
pointOpacity: 1,
strokeWidth: 2,
strokeColor: [255, 255, 255],
strokeOpacity: 1,
};
// /**
// * PointLayer
// */
// export default class PointLayer extends BaseLayer {
// public styleOptions: IPointLayerStyleOptions = {
// pointShape: 'circle',
// pointColor: [81, 187, 214],
// pointRadius: 10,
// pointOpacity: 1,
// strokeWidth: 2,
// strokeColor: [255, 255, 255],
// strokeOpacity: 1,
// };
public name: string = 'pointLayer';
// public name: string = 'pointLayer';
@lazyInject(TYPES.IShaderModuleService)
private readonly shaderModule: IShaderModuleService;
// @lazyInject(TYPES.IShaderModuleService)
// private readonly shaderModule: IShaderModuleService;
@lazyInject(TYPES.IRendererService)
private readonly renderer: IRendererService;
// @lazyInject(TYPES.IRendererService)
// private readonly renderer: IRendererService;
private pointFeatures: IPointFeature[] = [];
// private pointFeatures: IPointFeature[] = [];
// public style(options: Partial<IPointLayerStyleOptions>) {
// // this.layerStyleService.update(options);
// // this.styleOptions = {
// // ...this.styleOptions,
// // ...options,
// // };
// }
// // public style(options: Partial<IPointLayerStyleOptions>) {
// // // this.layerStyleService.update(options);
// // // this.styleOptions = {
// // // ...this.styleOptions,
// // // ...options,
// // // };
// // }
public render(): ILayer {
this.models.forEach((model) =>
model.draw({
uniforms: {
u_ModelMatrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
u_stroke_width: 1,
u_blur: 0,
u_opacity: 1,
u_stroke_color: [1, 1, 1, 1],
u_stroke_opacity: 1,
},
}),
);
return this;
}
// public render(): ILayer {
// this.models.forEach((model) =>
// model.draw({
// uniforms: {
// u_ModelMatrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
// u_stroke_width: 1,
// u_blur: 0,
// u_opacity: 1,
// u_stroke_color: [1, 1, 1, 1],
// u_stroke_opacity: 1,
// },
// }),
// );
// return this;
// }
protected buildModels() {
this.shaderModule.registerModule('circle', {
vs: circleVert,
fs: circleFrag,
});
// protected buildModels() {
// this.shaderModule.registerModule('circle', {
// vs: circleVert,
// fs: circleFrag,
// });
this.models = [];
const { vs, fs, uniforms } = this.shaderModule.getModule('circle');
// TODO: fix me
const source = this.getSource();
featureEach(
// @ts-ignore
source.originData,
({ geometry: { coordinates }, properties }) => {
this.pointFeatures.push({
coordinates,
});
},
);
// this.models = [];
// const { vs, fs, uniforms } = this.shaderModule.getModule('circle');
// // TODO: fix me
// const source = this.getSource();
// featureEach(
// // @ts-ignore
// source.originData,
// ({ geometry: { coordinates }, properties }) => {
// this.pointFeatures.push({
// coordinates,
// });
// },
// );
const {
packedBuffer,
packedBuffer2,
packedBuffer3,
indexBuffer,
positionBuffer,
// @ts-ignore
} = this.buildPointBuffers(this.pointFeatures);
// const {
// packedBuffer,
// packedBuffer2,
// packedBuffer3,
// indexBuffer,
// positionBuffer,
// // @ts-ignore
// } = this.buildPointBuffers(this.pointFeatures);
const {
createAttribute,
createBuffer,
createElements,
createModel,
} = this.renderer;
// const {
// createAttribute,
// createBuffer,
// createElements,
// createModel,
// } = this.renderer;
this.models.push(
createModel({
attributes: {
a_Position: createAttribute({
buffer: createBuffer({
data: positionBuffer,
type: gl.FLOAT,
}),
}),
a_packed_data: createAttribute({
buffer: createBuffer({
data: packedBuffer,
type: gl.FLOAT,
}),
}),
},
uniforms,
fs,
vs,
count: indexBuffer.length,
primitive: gl.TRIANGLES,
elements: createElements({
data: indexBuffer,
type: gl.UNSIGNED_INT,
}),
depth: { enable: false },
blend: {
enable: true,
func: {
srcRGB: gl.SRC_ALPHA,
srcAlpha: 1,
dstRGB: gl.ONE_MINUS_SRC_ALPHA,
dstAlpha: 1,
},
},
}),
);
}
// this.models.push(
// createModel({
// attributes: {
// a_Position: createAttribute({
// buffer: createBuffer({
// data: positionBuffer,
// type: gl.FLOAT,
// }),
// }),
// a_packed_data: createAttribute({
// buffer: createBuffer({
// data: packedBuffer,
// type: gl.FLOAT,
// }),
// }),
// },
// uniforms,
// fs,
// vs,
// count: indexBuffer.length,
// primitive: gl.TRIANGLES,
// elements: createElements({
// data: indexBuffer,
// type: gl.UNSIGNED_INT,
// }),
// depth: { enable: false },
// blend: {
// enable: true,
// func: {
// srcRGB: gl.SRC_ALPHA,
// srcAlpha: 1,
// dstRGB: gl.ONE_MINUS_SRC_ALPHA,
// dstAlpha: 1,
// },
// },
// }),
// );
// }
private buildPointBuffers(pointFeatures: IPointFeature[]) {
const packedBuffer: number[][] = [];
const packedBuffer2: number[][] = [];
const packedBuffer3: number[][] = [];
const positionBuffer: number[][] = [];
const indexBuffer: Array<[number, number, number]> = [];
// private buildPointBuffers(pointFeatures: IPointFeature[]) {
// const packedBuffer: number[][] = [];
// const packedBuffer2: number[][] = [];
// const packedBuffer3: number[][] = [];
// const positionBuffer: number[][] = [];
// const indexBuffer: Array<[number, number, number]> = [];
const {
pointColor,
pointRadius,
pointShape,
pointOpacity,
strokeColor,
strokeWidth,
strokeOpacity,
} = this.styleOptions;
// const {
// pointColor,
// pointRadius,
// pointShape,
// pointOpacity,
// strokeColor,
// strokeWidth,
// strokeOpacity,
// } = this.styleOptions;
let i = 0;
pointFeatures.forEach((pointFeature) => {
// TODO: 判断是否使用瓦片坐标
const [tileX, tileY] = pointFeature.coordinates;
// let i = 0;
// pointFeatures.forEach((pointFeature) => {
// // TODO: 判断是否使用瓦片坐标
// const [tileX, tileY] = pointFeature.coordinates;
// 压缩顶点数据
// TODO: 某些变量通过 uniform 而非 vertex attribute 传入
const {
packedBuffer: packed1,
packedBuffer2: packed2,
packedBuffer3: packed3,
} = packCircleVertex({
color: [...pointColor, 255],
radius: pointRadius,
tileX: 0,
tileY: 0,
shape: pointShape,
opacity: pointOpacity,
strokeColor: [...strokeColor, 255],
strokeOpacity,
strokeWidth,
});
packedBuffer.push(...packed1);
packedBuffer2.push(...packed2);
packedBuffer3.push(...packed3);
// // 压缩顶点数据
// // TODO: 某些变量通过 uniform 而非 vertex attribute 传入
// const {
// packedBuffer: packed1,
// packedBuffer2: packed2,
// packedBuffer3: packed3,
// } = packCircleVertex({
// color: [...pointColor, 255],
// radius: pointRadius,
// tileX: 0,
// tileY: 0,
// shape: pointShape,
// opacity: pointOpacity,
// strokeColor: [...strokeColor, 255],
// strokeOpacity,
// strokeWidth,
// });
// packedBuffer.push(...packed1);
// packedBuffer2.push(...packed2);
// packedBuffer3.push(...packed3);
// 经纬度坐标
positionBuffer.push([tileX, tileY]);
positionBuffer.push([tileX, tileY]);
positionBuffer.push([tileX, tileY]);
positionBuffer.push([tileX, tileY]);
// // 经纬度坐标
// positionBuffer.push([tileX, tileY]);
// positionBuffer.push([tileX, tileY]);
// positionBuffer.push([tileX, tileY]);
// positionBuffer.push([tileX, tileY]);
// 构造 index
indexBuffer.push([0 + i, 1 + i, 2 + i]);
indexBuffer.push([2 + i, 3 + i, 0 + i]);
i += 4;
});
// // 构造 index
// indexBuffer.push([0 + i, 1 + i, 2 + i]);
// indexBuffer.push([2 + i, 3 + i, 0 + i]);
// i += 4;
// });
return {
packedBuffer,
packedBuffer2,
packedBuffer3,
indexBuffer,
positionBuffer,
};
}
}
// return {
// packedBuffer,
// packedBuffer2,
// packedBuffer3,
// indexBuffer,
// positionBuffer,
// };
// }
// }

View File

@ -1,147 +1,130 @@
import {
gl,
IIconService,
IRendererService,
IShaderModuleService,
lazyInject,
TYPES,
} from '@l7/core';
import BaseLayer from '../core/BaseLayer';
import ExtrudeBuffer from './buffers/ExtrudeBuffer';
import ImageBuffer from './buffers/ImageBuffer';
import extrude_frag from './shaders/extrude_frag.glsl';
import extrude_vert from './shaders/extrude_vert.glsl';
import image_frag from './shaders/image_frag.glsl';
import image_vert from './shaders/image_vert.glsl';
// import {
// gl,
// IIconService,
// IRendererService,
// IShaderModuleService,
// lazyInject,
// TYPES,
// } from '@l7/core';
// import BaseLayer from '../core/BaseLayer';
// import ExtrudeBuffer from './buffers/ExtrudeBuffer';
// import ImageBuffer from './buffers/ImageBuffer';
// import extrude_frag from './shaders/extrude_frag.glsl';
// import extrude_vert from './shaders/extrude_vert.glsl';
// import image_frag from './shaders/image_frag.glsl';
// import image_vert from './shaders/image_vert.glsl';
export default class PointLayer extends BaseLayer {
public name: string = 'PointLayer';
// export default class PointLayer extends BaseLayer {
// public name: string = 'PointLayer';
@lazyInject(TYPES.IShaderModuleService)
private readonly shaderModule: IShaderModuleService;
// @lazyInject(TYPES.IShaderModuleService)
// private readonly shaderModule: IShaderModuleService;
@lazyInject(TYPES.IRendererService)
private readonly renderer: IRendererService;
// @lazyInject(TYPES.IRendererService)
// private readonly renderer: IRendererService;
protected renderModels() {
this.models.forEach((model) =>
model.draw({
uniforms: {
u_ModelMatrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
},
}),
);
return this;
}
// protected renderModels() {
// this.models.forEach((model) =>
// model.draw({
// uniforms: {
// u_ModelMatrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
// },
// }),
// );
// return this;
// }
protected buildModels(): void {
this.shaderModule.registerModule('point', {
vs: extrude_vert,
fs: extrude_frag,
});
this.shaderModule.registerModule('pointImage', {
vs: image_vert,
fs: image_frag,
});
// protected buildModels(): void {
// this.shaderModule.registerModule('point', {
// vs: extrude_vert,
// fs: extrude_frag,
// });
// this.shaderModule.registerModule('pointImage', {
// vs: image_vert,
// fs: image_frag,
// });
this.models = [];
const { vs, fs, uniforms } = this.shaderModule.getModule('pointImage');
// const buffer = new ExtrudeBuffer({
// data: this.getEncodedData(),
// });
// buffer.computeVertexNormals('miters', false);
const {
createAttribute,
createBuffer,
createElements,
createTexture2D,
createModel,
} = this.renderer;
const buffer = new ImageBuffer({
data: this.getEncodedData(),
iconMap: this.iconService.getIconMap(),
});
this.fontService.setFontOptions({
characterSet: ['人', '之', '初'],
fontFamily: 'sans-serif',
fontWeight: 'normal',
});
this.iconService.on('imageUpdate', () => {
this.models.push(
createModel({
attributes: {
a_Position: createAttribute({
buffer: createBuffer({
data: buffer.attributes.positions,
type: gl.FLOAT,
}),
size: 3,
}),
a_normal: createAttribute({
buffer: createBuffer({
data: buffer.attributes.normals,
type: gl.FLOAT,
}),
size: 3,
}),
a_color: createAttribute({
buffer: createBuffer({
data: buffer.attributes.colors,
type: gl.FLOAT,
}),
size: 4,
}),
a_size: createAttribute({
buffer: createBuffer({
data: buffer.attributes.sizes,
type: gl.FLOAT,
}),
size: 1,
}),
a_uv: createAttribute({
buffer: createBuffer({
data: buffer.attributes.uv,
type: gl.FLOAT,
}),
size: 2,
}),
// a_shape: createAttribute({
// buffer: createBuffer({
// data: buffer.attributes.miters,
// type: gl.FLOAT,
// }),
// size: 3,
// }),
},
uniforms: {
...uniforms,
u_opacity: this.styleOption.opacity as number,
u_texture: createTexture2D({
data: this.iconService.getCanvas(),
width: 1024,
height: this.iconService.canvasHeight,
}),
},
fs,
vs,
depth: { enable: false },
blend: {
enable: true,
func: {
srcRGB: gl.SRC_ALPHA,
srcAlpha: 1,
dstRGB: gl.ONE_MINUS_SRC_ALPHA,
dstAlpha: 1,
},
},
primitive: gl.POINTS,
count: buffer.verticesCount,
// elements: createElements({
// data: buffer.indexArray,
// type: gl.UNSIGNED_INT,
// }),
}),
);
});
}
}
// this.models = [];
// const { vs, fs, uniforms } = this.shaderModule.getModule('pointImage');
// // const buffer = new ExtrudeBuffer({
// // data: this.getEncodedData(),
// // });
// // buffer.computeVertexNormals('miters', false);
// const {
// createAttribute,
// createBuffer,
// createElements,
// createTexture2D,
// createModel,
// } = this.renderer;
// const buffer = new ImageBuffer({
// data: this.getEncodedData(),
// iconMap: this.iconService.getIconMap(),
// });
// this.models.push(
// createModel({
// attributes: {
// a_Position: createAttribute({
// buffer: createBuffer({
// data: buffer.attributes.positions,
// type: gl.FLOAT,
// }),
// size: 3,
// }),
// a_normal: createAttribute({
// buffer: createBuffer({
// data: buffer.attributes.normals,
// type: gl.FLOAT,
// }),
// size: 3,
// }),
// a_color: createAttribute({
// buffer: createBuffer({
// data: buffer.attributes.colors,
// type: gl.FLOAT,
// }),
// size: 4,
// }),
// a_size: createAttribute({
// buffer: createBuffer({
// data: buffer.attributes.sizes,
// type: gl.FLOAT,
// }),
// size: 1,
// }),
// a_uv: createAttribute({
// buffer: createBuffer({
// data: buffer.attributes.uv,
// type: gl.FLOAT,
// }),
// size: 2,
// }),
// // a_shape: createAttribute({
// // buffer: createBuffer({
// // data: buffer.attributes.miters,
// // type: gl.FLOAT,
// // }),
// // size: 3,
// // }),
// },
// uniforms: {
// ...uniforms,
// u_opacity: this.styleOption.opacity as number,
// u_texture: createTexture2D({
// data: this.iconService.getCanvas(),
// width: 1024,
// height: this.iconService.canvasHeight,
// }),
// },
// fs,
// vs,
// primitive: gl.POINTS,
// count: buffer.verticesCount,
// // elements: createElements({
// // data: buffer.indexArray,
// // type: gl.UNSIGNED_INT,
// // }),
// }),
// );
// }
// }

View File

@ -0,0 +1,16 @@
import { IEncodeFeature } from '@l7/core';
import { polygonTriangulation } from '..';
describe('PolygonTriangulation', () => {
it('should do triangulation with a polygon correctly', () => {
const mockFeature: IEncodeFeature = {
coordinates: [[[0, 0], [1, 0], [1, 1], [0, 1]]],
color: [1, 0, 0, 0],
};
const { indices, vertices, size } = polygonTriangulation(mockFeature);
expect(indices).toEqual([2, 3, 0, 0, 1, 2]);
expect(vertices).toEqual([0, 0, 1, 0, 1, 1, 0, 1]);
expect(size).toEqual(2);
});
});

View File

@ -1,137 +0,0 @@
import earcut from 'earcut';
import BufferBase, {
IBufferInfo,
IEncodeFeature,
Position,
} from '../../core/BaseBuffer';
export default class ExtrudeBuffer extends BufferBase {
public buildFeatures() {
const layerData = this.data as IEncodeFeature[];
layerData.forEach((feature: IEncodeFeature) => {
this.calculateTop(feature);
this.calculateWall(feature);
delete feature.bufferInfo;
});
}
public calculateFeatures() {
const layerData = this.data as IEncodeFeature[];
// 计算长
layerData.forEach((feature: IEncodeFeature) => {
const coordinates = feature.coordinates as Position[][];
const flattengeo = earcut.flatten(coordinates);
const n = this.checkIsClosed(coordinates)
? coordinates[0].length - 1
: coordinates[0].length;
const { vertices, dimensions, holes } = flattengeo;
const indexArray = earcut(vertices, holes, dimensions).map(
(v) => this.verticesCount + v,
);
const bufferInfo: IBufferInfo = {
dimensions,
vertices,
indexArray,
verticesOffset: this.verticesCount + 0,
indexOffset: this.indexCount + 0,
faceNum: n,
};
this.indexCount += indexArray.length + n * 6;
this.verticesCount += vertices.length / dimensions + n * 4;
feature.bufferInfo = bufferInfo;
});
}
protected calculateWall(feature: IEncodeFeature) {
const size = feature.size || 0;
const bufferInfo = feature.bufferInfo as IBufferInfo;
const {
vertices,
indexOffset,
verticesOffset,
faceNum,
dimensions,
} = bufferInfo;
this.encodeArray(feature, faceNum * 4);
for (let i = 0; i < faceNum; i++) {
const prePoint = vertices.slice(i * dimensions, (i + 1) * dimensions);
const nextPoint = vertices.slice(
(i + 1) * dimensions,
(i + 2) * dimensions,
);
this.calculateExtrudeFace(
prePoint,
nextPoint,
verticesOffset + i * 4,
indexOffset + i * 6,
size as number,
);
bufferInfo.verticesOffset += 4;
bufferInfo.indexOffset += 6;
feature.bufferInfo = bufferInfo;
}
}
private calculateTop(feature: IEncodeFeature) {
const size = feature.size || 1;
const bufferInfo = feature.bufferInfo as IBufferInfo;
const {
indexArray,
vertices,
indexOffset,
verticesOffset,
dimensions,
} = bufferInfo;
const pointCount = vertices.length / dimensions;
this.encodeArray(feature, vertices.length / dimensions);
// 添加顶点
for (let i = 0; i < pointCount; i++) {
this.attributes.positions.set(
[vertices[i * dimensions], vertices[i * dimensions + 1], size],
(verticesOffset + i) * 3,
);
// 顶部文理坐标计算
// if (this.uv) {
// // TODO 用过BBox计算纹理坐标
// this.attributes.uv.set([-1, -1], (verticesOffset + i) * 2);
// }
}
bufferInfo.verticesOffset += pointCount;
// 添加顶点索引
this.indexArray.set(indexArray, indexOffset); // 顶部坐标
bufferInfo.indexOffset += indexArray.length;
feature.bufferInfo = bufferInfo;
}
private calculateExtrudeFace(
prePoint: number[],
nextPoint: number[],
positionOffset: number,
indexOffset: number | undefined,
size: number,
) {
this.attributes.positions.set(
[
prePoint[0],
prePoint[1],
size,
nextPoint[0],
nextPoint[1],
size,
prePoint[0],
prePoint[1],
0,
nextPoint[0],
nextPoint[1],
0,
],
positionOffset * 3,
);
const indexArray = [1, 2, 0, 3, 2, 1].map((v) => {
return v + positionOffset;
});
// if (this.uv) {
// this.attributes.uv.set(
// [0.1, 0, 0, 0, 0.1, size / 2000, 0, size / 2000],
// positionOffset * 2,
// );
// }
this.indexArray.set(indexArray, indexOffset);
}
}

View File

@ -1,69 +0,0 @@
import earcut from 'earcut';
import BufferBase, {
IBufferInfo,
IEncodeFeature,
Position,
} from '../../core/BaseBuffer';
export default class FillBuffer extends BufferBase {
protected buildFeatures() {
const layerData = this.data as IEncodeFeature[];
layerData.forEach((feature: IEncodeFeature) => {
this.calculateFill(feature);
delete feature.bufferInfo;
});
}
protected calculateFeatures() {
const layerData = this.data as IEncodeFeature[];
// 计算长
layerData.forEach((feature: IEncodeFeature) => {
const { coordinates } = feature;
const flattengeo = earcut.flatten(coordinates as Position[][]);
const { vertices, dimensions, holes } = flattengeo;
const indexArray = earcut(vertices, holes, dimensions).map(
(v) => this.verticesCount + v,
);
const bufferInfo: IBufferInfo = {
vertices,
indexArray,
verticesOffset: this.verticesCount + 0,
indexOffset: this.indexCount + 0,
dimensions,
};
this.indexCount += indexArray.length;
this.verticesCount += vertices.length / dimensions;
feature.bufferInfo = bufferInfo;
});
}
private calculateFill(feature: IEncodeFeature) {
const bufferInfo = feature.bufferInfo as IBufferInfo;
const {
indexArray,
vertices,
indexOffset,
verticesOffset,
dimensions = 3,
} = bufferInfo;
const pointCount = vertices.length / dimensions;
this.encodeArray(feature, pointCount);
// 添加顶点
for (let i = 0; i < pointCount; i++) {
this.attributes.positions.set(
[vertices[i * dimensions], vertices[i * dimensions + 1], 0],
(verticesOffset + i) * 3,
);
// if (this.uv) {
// // TODO 用过BBox计算纹理坐标
// this.attributes.uv.set(
// [0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0],
// (verticesOffset + i) * 3,
// );
// }
}
bufferInfo.verticesOffset += pointCount;
feature.bufferInfo = bufferInfo;
// 添加顶点索引
this.indexArray.set(indexArray, indexOffset); // 顶部坐标
}
}

View File

@ -1,95 +1,48 @@
import {
gl,
IRendererService,
IShaderModuleService,
lazyInject,
TYPES,
} from '@l7/core';
import { IEncodeFeature } from '@l7/core';
import earcut from 'earcut';
import BaseLayer from '../core/BaseLayer';
import ExtrudeBuffer from './buffers/ExtrudeBuffer';
import FillBuffer from './buffers/FillBuffer';
import polygon_frag from './shaders/polygon_frag.glsl';
import polygon_vert from './shaders/polygon_vert.glsl';
export default class PolygonLayer extends BaseLayer {
interface IPolygonLayerStyleOptions {
opacity: number;
}
export function polygonTriangulation(feature: IEncodeFeature) {
const { coordinates } = feature;
const flattengeo = earcut.flatten(coordinates);
const { vertices, dimensions, holes } = flattengeo;
return {
indices: earcut(vertices, holes, dimensions),
vertices,
size: dimensions,
};
}
export default class PolygonLayer extends BaseLayer<IPolygonLayerStyleOptions> {
public name: string = 'PolygonLayer';
@lazyInject(TYPES.IShaderModuleService)
private readonly shaderModule: IShaderModuleService;
@lazyInject(TYPES.IRendererService)
private readonly renderer: IRendererService;
protected renderModels() {
const { opacity } = this.getStyleOptions();
this.models.forEach((model) =>
model.draw({
uniforms: {
u_ModelMatrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
u_Opacity: opacity || 0,
},
}),
);
return this;
}
protected buildModels(): void {
this.shaderModule.registerModule('polygon', {
vs: polygon_vert,
fs: polygon_frag,
});
this.models = [];
const { vs, fs, uniforms } = this.shaderModule.getModule('polygon');
// const buffer = new ExtrudeBuffer({
// data: this.getEncodedData(),
// });
// buffer.computeVertexNormals();
const buffer = new FillBuffer({
data: this.getEncodedData(),
});
const {
createAttribute,
createBuffer,
createElements,
createModel,
} = this.renderer;
this.models.push(
createModel({
attributes: {
a_Position: createAttribute({
buffer: createBuffer({
data: buffer.attributes.positions,
type: gl.FLOAT,
}),
size: 3,
}),
a_normal: createAttribute({
buffer: createBuffer({
data: buffer.attributes.normals,
type: gl.FLOAT,
}),
size: 3,
}),
a_color: createAttribute({
buffer: createBuffer({
data: buffer.attributes.colors,
type: gl.FLOAT,
}),
size: 4,
}),
},
uniforms: {
...uniforms,
u_opacity: this.styleOption.opacity as number,
},
fs,
vs,
count: buffer.indexArray.length,
elements: createElements({
data: buffer.indexArray,
type: gl.UNSIGNED_INT,
}),
protected buildModels() {
this.models = [
this.buildLayerModel({
moduleName: 'polygon',
vertexShader: polygon_vert,
fragmentShader: polygon_frag,
triangulation: polygonTriangulation,
}),
);
];
}
}

View File

@ -1,6 +1,10 @@
varying vec4 v_color;
uniform float u_opacity: 1.0;
uniform float u_Opacity: 1.0;
varying vec4 v_Color;
#pragma include "picking"
void main() {
gl_FragColor = v_color;
gl_FragColor.a *= u_opacity;
gl_FragColor = v_Color;
gl_FragColor.a *= u_Opacity;
gl_FragColor = filterColor(gl_FragColor);
}

View File

@ -1,14 +1,18 @@
attribute vec4 a_color;
attribute vec4 a_Color;
attribute vec3 a_Position;
attribute vec3 a_normal;
// attribute vec3 a_normal;
uniform mat4 u_ModelMatrix;
varying vec4 v_color;
varying vec4 v_Color;
#pragma include "projection"
#pragma include "picking"
void main() {
v_color = a_color;
v_Color = a_Color;
vec4 project_pos = project_position(vec4(a_Position, 1.0));
gl_Position = project_common_position_to_clipspace(vec4(project_pos.xyz, 1.0));
setPickingColor(a_PickingColor);
}

View File

@ -1,33 +1,33 @@
import BaseBuffer, { IEncodeFeature, Position } from '../../core/BaseBuffer';
interface IImageFeature extends IEncodeFeature {
images: any[];
}
export default class ImageBuffer extends BaseBuffer {
protected calculateFeatures() {
this.verticesCount = 6;
this.indexCount = 6;
}
protected buildFeatures() {
this.attributes.uv = new Float32Array(this.verticesCount * 2);
const layerData = this.data as IImageFeature[];
const coordinates = layerData[0].coordinates as Position[];
const positions: number[] = [
...coordinates[0],
0,
coordinates[1][0],
coordinates[0][1],
0,
...coordinates[1],
0,
...coordinates[0],
0,
...coordinates[1],
0,
coordinates[0][0],
coordinates[1][1],
0,
];
this.attributes.positions.set(positions, 0);
this.attributes.uv.set([0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0], 0);
}
}
// import BaseBuffer, { IEncodeFeature, Position } from '../../core/BaseBuffer';
// interface IImageFeature extends IEncodeFeature {
// images: any[];
// }
// export default class ImageBuffer extends BaseBuffer {
// protected calculateFeatures() {
// this.verticesCount = 6;
// this.indexCount = 6;
// }
// protected buildFeatures() {
// this.attributes.uv = new Float32Array(this.verticesCount * 2);
// const layerData = this.data as IImageFeature[];
// const coordinates = layerData[0].coordinates as Position[];
// const positions: number[] = [
// ...coordinates[0],
// 0,
// coordinates[1][0],
// coordinates[0][1],
// 0,
// ...coordinates[1],
// 0,
// ...coordinates[0],
// 0,
// ...coordinates[1],
// 0,
// coordinates[0][0],
// coordinates[1][1],
// 0,
// ];
// this.attributes.positions.set(positions, 0);
// this.attributes.uv.set([0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0], 0);
// }
// }

View File

@ -1,87 +1,87 @@
import {
gl,
IRendererService,
IShaderModuleService,
ITexture2D,
lazyInject,
TYPES,
} from '@l7/core';
import BaseLayer from '../core/BaseLayer';
import ImageBuffer from './buffers/ImageBuffer';
import image_frag from './shaders/image_frag.glsl';
import image_vert from './shaders/image_vert.glsl';
export default class ImageLayer extends BaseLayer {
public name: string = 'imageLayer';
@lazyInject(TYPES.IShaderModuleService)
private readonly shaderModule: IShaderModuleService;
// import {
// gl,
// IRendererService,
// IShaderModuleService,
// ITexture2D,
// lazyInject,
// TYPES,
// } from '@l7/core';
// import BaseLayer from '../core/BaseLayer';
// import ImageBuffer from './buffers/ImageBuffer';
// import image_frag from './shaders/image_frag.glsl';
// import image_vert from './shaders/image_vert.glsl';
// export default class ImageLayer extends BaseLayer {
// public name: string = 'imageLayer';
// @lazyInject(TYPES.IShaderModuleService)
// private readonly shaderModule: IShaderModuleService;
@lazyInject(TYPES.IRendererService)
private readonly renderer: IRendererService;
// @lazyInject(TYPES.IRendererService)
// private readonly renderer: IRendererService;
protected renderModels() {
this.models.forEach((model) =>
model.draw({
uniforms: {
u_ModelMatrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
},
}),
);
return this;
}
protected buildModels() {
const {
createAttribute,
createBuffer,
createElements,
createTexture2D,
createModel,
} = this.renderer;
this.shaderModule.registerModule('image', {
vs: image_vert,
fs: image_frag,
});
// protected renderModels() {
// this.models.forEach((model) =>
// model.draw({
// uniforms: {
// u_ModelMatrix: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
// },
// }),
// );
// return this;
// }
// protected buildModels() {
// const {
// createAttribute,
// createBuffer,
// createElements,
// createTexture2D,
// createModel,
// } = this.renderer;
// this.shaderModule.registerModule('image', {
// vs: image_vert,
// fs: image_frag,
// });
this.models = [];
const { vs, fs, uniforms } = this.shaderModule.getModule('image');
const source = this.getSource();
// const imageData = await source.data.images;
const buffer = new ImageBuffer({
data: this.getEncodedData(),
});
source.data.images.then((imageData: HTMLImageElement[]) => {
const texture: ITexture2D = createTexture2D({
data: imageData[0],
width: imageData[0].width,
height: imageData[0].height,
});
this.models.push(
createModel({
attributes: {
a_Position: createAttribute({
buffer: createBuffer({
data: buffer.attributes.positions,
type: gl.FLOAT,
}),
size: 3,
}),
a_uv: createAttribute({
buffer: createBuffer({
data: buffer.attributes.uv,
type: gl.FLOAT,
}),
size: 2,
}),
},
uniforms: {
...uniforms,
u_texture: texture,
u_opacity: 1.0,
},
fs,
vs,
count: buffer.verticesCount,
}),
);
});
}
}
// this.models = [];
// const { vs, fs, uniforms } = this.shaderModule.getModule('image');
// const source = this.getSource();
// // const imageData = await source.data.images;
// const buffer = new ImageBuffer({
// data: this.getEncodedData(),
// });
// source.data.images.then((imageData: HTMLImageElement[]) => {
// const texture: ITexture2D = createTexture2D({
// data: imageData[0],
// width: imageData[0].width,
// height: imageData[0].height,
// });
// this.models.push(
// createModel({
// attributes: {
// a_Position: createAttribute({
// buffer: createBuffer({
// data: buffer.attributes.positions,
// type: gl.FLOAT,
// }),
// size: 3,
// }),
// a_uv: createAttribute({
// buffer: createBuffer({
// data: buffer.attributes.uv,
// type: gl.FLOAT,
// }),
// size: 2,
// }),
// },
// uniforms: {
// ...uniforms,
// u_texture: texture,
// u_opacity: 1.0,
// },
// fs,
// vs,
// count: buffer.verticesCount,
// }),
// );
// });
// }
// }

View File

@ -6,16 +6,16 @@ import {
CoordinateSystem,
ICoordinateSystemService,
ILngLat,
IMapCamera,
IMapConfig,
IMapService,
IPoint,
IViewport,
Point,
MapType,
TYPES,
} from '@l7/core';
import { DOM } from '@l7/utils';
import { inject, injectable } from 'inversify';
import { IAMapEvent, IAMapInstance } from '../../typings/index';
import Viewport from './Viewport';
const AMAP_API_KEY: string = '15cd8a57710d40c9b7c0e3cc120f1200';
@ -33,6 +33,9 @@ export default class AMapService implements IMapService {
private readonly coordinateSystemService: ICoordinateSystemService;
private markerContainer: HTMLElement;
private $mapContainer: HTMLElement | null;
private $jsapi: HTMLScriptElement;
private viewport: Viewport;
private cameraChangedCallback: (viewport: IViewport) => void;
@ -69,6 +72,9 @@ export default class AMapService implements IMapService {
return [size.getWidth(), size.getHeight()];
}
public getType() {
return MapType.amap;
}
public getZoom(): number {
return this.map.getZoom();
}
@ -164,11 +170,13 @@ export default class AMapService implements IMapService {
public async init(mapConfig: IMapConfig): Promise<void> {
const { id, style, ...rest } = mapConfig;
this.$mapContainer = document.getElementById(id);
// tslint:disable-next-line:typedef
await new Promise((resolve) => {
// 异步加载高德地图
// @see https://lbs.amap.com/api/javascript-api/guide/abc/load
window.onLoad = (): void => {
window.onload = (): void => {
// @ts-ignore
this.map = new AMap.Map(id, {
mapStyle: style,
@ -182,15 +190,24 @@ export default class AMapService implements IMapService {
};
const url: string = `https://webapi.amap.com/maps?v=${AMAP_VERSION}&key=${AMAP_API_KEY}&plugin=Map3D&callback=onLoad`;
const jsapi: HTMLScriptElement = document.createElement('script');
jsapi.charset = 'utf-8';
jsapi.src = url;
document.head.appendChild(jsapi);
this.$jsapi = document.createElement('script');
this.$jsapi.charset = 'utf-8';
this.$jsapi.src = url;
document.head.appendChild(this.$jsapi);
});
this.viewport = new Viewport();
}
public destroy() {
this.map.destroy();
document.head.removeChild(this.$jsapi);
}
public getMapContainer() {
return this.$mapContainer;
}
public onCameraChanged(callback: (viewport: IViewport) => void): void {
this.cameraChangedCallback = callback;
}

View File

@ -10,11 +10,13 @@ import {
IMapService,
IPoint,
IViewport,
MapType,
TYPES,
} from '@l7/core';
import { DOM } from '@l7/utils';
import { inject, injectable } from 'inversify';
import mapboxgl, { IControl, Map } from 'mapbox-gl';
import { IMapboxInstance } from '../../typings/index';
import Viewport from './Viewport';
const EventMap: {
[key: string]: any;
@ -27,6 +29,8 @@ mapboxgl.accessToken =
'pk.eyJ1IjoieGlhb2l2ZXIiLCJhIjoiY2pxcmc5OGNkMDY3cjQzbG42cXk5NTl3YiJ9.hUC5Chlqzzh0FFd_aEc-uQ';
const LNGLAT_OFFSET_ZOOM_THRESHOLD = 12;
let counter = 1;
/**
* AMapService
*/
@ -38,6 +42,8 @@ export default class MapboxService implements IMapService {
private viewport: Viewport;
private markerContainer: HTMLElement;
private cameraChangedCallback: (viewport: IViewport) => void;
private $mapContainer: HTMLElement | null;
private $link: HTMLLinkElement;
// init
public addMarkerContainer(): void {
@ -67,6 +73,9 @@ export default class MapboxService implements IMapService {
}
// get mapStatus method
public getType() {
return MapType.mapbox;
}
public getZoom(): number {
return this.map.getZoom();
}
@ -156,6 +165,9 @@ export default class MapboxService implements IMapService {
public async init(mapConfig: IMapConfig): Promise<void> {
const { id, attributionControl = false, ...rest } = mapConfig;
this.$mapContainer = document.getElementById(id);
this.$mapContainer!.classList.add(`${counter++}`);
this.viewport = new Viewport();
/**
@ -173,12 +185,22 @@ export default class MapboxService implements IMapService {
// 不同于高德地图,需要手动触发首次渲染
this.handleCameraChanged();
const $link: HTMLLinkElement = document.createElement('link');
$link.href =
this.$link = document.createElement('link');
this.$link.href =
'https://api.tiles.mapbox.com/mapbox-gl-js/v1.2.1/mapbox-gl.css';
$link.rel = 'stylesheet';
document.head.appendChild($link);
this.removeLogoControl();
this.$link.rel = 'stylesheet';
document.head.appendChild(this.$link);
}
public destroy() {
document.head.removeChild(this.$link);
this.$mapContainer = null;
this.map.remove();
}
public getMapContainer() {
return this.$mapContainer;
}
public onCameraChanged(callback: (viewport: IViewport) => void): void {

View File

@ -1,4 +1,4 @@
import { IAttribute, IAttributeInitializationOptions } from '@l7/core';
import { IAttribute, IAttributeInitializationOptions, IBuffer } from '@l7/core';
import regl from 'regl';
import ReglBuffer from './ReglBuffer';
@ -7,9 +7,11 @@ import ReglBuffer from './ReglBuffer';
*/
export default class ReglAttribute implements IAttribute {
private attribute: regl.Attribute;
private buffer: IBuffer;
constructor(gl: regl.Regl, options: IAttributeInitializationOptions) {
const { buffer, offset, stride, normalized, size, divisor } = options;
this.buffer = buffer;
this.attribute = {
buffer: (buffer as ReglBuffer).get(),
offset: offset || 0,
@ -27,7 +29,16 @@ export default class ReglAttribute implements IAttribute {
return this.attribute;
}
public updateBuffer(options: {
// 用于替换的数据
data: number[] | number[][] | Uint8Array | Uint16Array | Uint32Array;
// 原 Buffer 替换位置,单位为 byte
offset: number;
}) {
this.buffer.subData(options);
}
public destroy() {
// TODO: destroy buffer?
this.buffer.destroy();
}
}

View File

@ -9,7 +9,7 @@ export default class ReglElements implements IElements {
private elements: regl.Elements;
constructor(reGl: regl.Regl, options: IElementsInitializationOptions) {
const { data, usage, type } = options;
const { data, usage, type, count } = options;
this.elements = reGl.elements({
data,
@ -18,6 +18,7 @@ export default class ReglElements implements IElements {
| 'uint8'
| 'uint16'
| 'uint32',
count,
});
}

View File

@ -67,12 +67,16 @@ export default class ReglModel implements IModel {
vert: vs,
primitive:
primitiveMap[primitive === undefined ? gl.TRIANGLES : primitive],
count,
};
if (instances) {
drawParams.instances = instances;
}
// elements 中可能包含 count此时不应传入
if (count) {
drawParams.count = count;
}
if (elements) {
drawParams.elements = (elements as ReglElements).get();
}
@ -133,9 +137,7 @@ export default class ReglModel implements IModel {
}
public destroy() {
// release all resources
// @see https://github.com/regl-project/regl/blob/gh-pages/API.md#clean-up
this.reGl.destroy();
// don't need do anything since we will call `rendererService.cleanup()`
}
/**

View File

@ -14,6 +14,7 @@ import {
IFramebufferInitializationOptions,
IModel,
IModelInitializationOptions,
IReadPixelsOptions,
IRendererService,
ITexture2D,
ITexture2DInitializationOptions,
@ -92,19 +93,13 @@ export default class ReglRendererService implements IRendererService {
public createFramebuffer = (options: IFramebufferInitializationOptions) =>
new ReglFramebuffer(this.gl, options);
public renderToFramebuffer = (
public useFramebuffer = (
framebuffer: IFramebuffer | null,
drawCommands: () => void,
) => {
const useFramebuffer = this.gl({
// since post-processor will swap read/write fbos, we must retrieve it dynamically
framebuffer: framebuffer
? () => (framebuffer as ReglFramebuffer).get()
: null,
});
// TODO: pass other options
useFramebuffer({}, drawCommands);
this.gl({
framebuffer: framebuffer ? (framebuffer as ReglFramebuffer).get() : null,
})(drawCommands);
};
public clear = (options: IClearOptions) => {
@ -141,6 +136,20 @@ export default class ReglRendererService implements IRendererService {
this.gl._refresh();
};
public readPixels = (options: IReadPixelsOptions) => {
const { framebuffer, x, y, width, height } = options;
const readPixelsOptions: regl.ReadOptions = {
x,
y,
width,
height,
};
if (framebuffer) {
readPixelsOptions.framebuffer = (framebuffer as ReglFramebuffer).get();
}
return this.gl.read(readPixelsOptions);
};
public getViewportSize = () => {
return {
width: this.gl._gl.drawingBufferWidth,
@ -151,4 +160,9 @@ export default class ReglRendererService implements IRendererService {
public getContainer = () => {
return this.$container;
};
public destroy = () => {
// @see https://github.com/regl-project/regl/blob/gh-pages/API.md#clean-up
this.gl.destroy();
};
}

View File

@ -1,7 +1,6 @@
import {
Bounds,
container,
IconService,
IControl,
IControlService,
IIconService,
@ -31,6 +30,10 @@ container
.bind<IRendererService>(TYPES.IRendererService)
.to(ReglRendererService)
.inSingletonScope();
// 缓存当前地图类型,便于 DEMO 中切换底图时动态绑定
let mapType: MapType;
/**
* Scene API
*
@ -45,36 +48,37 @@ container
*/
class Scene {
public map: AMap.Map | Map;
protected readonly iconService: IIconService;
private sceneService: ISceneService;
private mapService: IMapService;
private controlService: IControlService;
private iconService: IIconService;
// private mapType: MapType;
public constructor(config: IMapConfig & IRenderConfig) {
const { type = MapType.amap } = config;
// 根据用户传入参数绑定地图服务
let mapService: new (...args: any[]) => IMapService;
let mapServiceImpl: new (...args: any[]) => IMapService;
if (type === MapType.mapbox) {
mapService = MapboxService;
mapServiceImpl = MapboxService;
} else if (type === MapType.amap) {
mapService = AMapService;
mapServiceImpl = AMapService;
} else {
throw new Error('不支持的地图服务');
}
// this.mapService = mapService;
// DEMO 中切换底图实现时,需要重新绑定底图服务
// @see https://github.com/inversify/InversifyJS/blob/master/wiki/container_api.md#containerrebindserviceidentifier-serviceidentifier
if (container.isBound(TYPES.IMapService)) {
container
.rebind<IMapService>(TYPES.IMapService)
.to(mapService)
.inSingletonScope();
} else {
if (!container.isBound(TYPES.IMapService)) {
container
.bind<IMapService>(TYPES.IMapService)
.to(mapService)
.to(mapServiceImpl)
.inSingletonScope();
} else if (type !== mapType) {
container
.rebind<IMapService>(TYPES.IMapService)
.to(mapServiceImpl)
.inSingletonScope();
}
@ -85,10 +89,12 @@ class Scene {
this.iconService = container.get<IIconService>(TYPES.IIconService);
this.controlService = container.get<IControlService>(TYPES.IControlService);
this.map = this.mapService.map; // 暴露原生map方法
mapType = this.mapService.getType();
}
public getMapService(): IMapService {
return this.mapService;
//
}
public addLayer(layer: ILayer): void {
@ -213,6 +219,7 @@ class Scene {
public destroy() {
this.sceneService.destroy();
// TODO: 清理其他 Service 例如 IconService
}
// 资源管理

View File

@ -8,7 +8,7 @@ export function createRendererContainer(domId: string): HTMLDivElement | null {
$container.style.cssText += `
position: absolute;
top: 0;
z-index:10;
z-index:2;
height: 100%;
width: 100%;
pointer-events: none;

View File

@ -0,0 +1,5 @@
import { storiesOf } from '@storybook/react';
import * as React from 'react';
import Polygon from './components/Polygon';
storiesOf('动画', module).add('动态更新指定 feature(s)', () => <Polygon />);

View File

@ -0,0 +1,101 @@
// @ts-ignore
import { PolygonLayer } from '@l7/layers';
// @ts-ignore
import { Scene } from '@l7/scene';
import * as dat from 'dat.gui';
import * as React from 'react';
function convertRGB2Hex(rgb: number[]) {
return (
'#' + rgb.map((r) => ('0' + Math.floor(r).toString(16)).slice(-2)).join('')
);
}
export default class Mapbox extends React.Component {
private gui: dat.GUI;
private $stats: Node;
private scene: Scene;
public componentWillUnmount() {
if (this.gui) {
this.gui.destroy();
}
if (this.$stats) {
document.body.removeChild(this.$stats);
}
this.scene.destroy();
}
public async componentDidMount() {
const response = await fetch(
'https://gw.alipayobjects.com/os/basement_prod/d2e0e930-fd44-4fca-8872-c1037b0fee7b.json',
);
const scene = new Scene({
id: 'map',
type: 'mapbox',
style: 'mapbox://styles/mapbox/streets-v9',
center: [110.19382669582967, 50.258134],
pitch: 0,
zoom: 3,
});
this.scene = scene;
const layer = new PolygonLayer({
enablePicking: false,
});
layer
.source(await response.json())
.size('name', [0, 10000, 50000, 30000, 100000])
.color('name', [
'#2E8AE6',
'#69D1AB',
'#DAF291',
'#FFD591',
'#FF7A45',
'#CF1D49',
])
.shape('fill')
.style({
opacity: 0.8,
});
scene.addLayer(layer);
scene.render();
/*** 运行时修改样式属性 ***/
const gui = new dat.GUI();
this.gui = gui;
const styleOptions = {
color: [0, 0, 0],
featureRange: {
startIndex: 0,
endIndex: Infinity,
},
};
const pointFolder = gui.addFolder('精确更新 feature');
pointFolder.add(styleOptions.featureRange, 'startIndex', 0, 100, 1);
pointFolder.add(styleOptions.featureRange, 'endIndex', 0, 100, 1);
pointFolder.addColor(styleOptions, 'color').onChange((color: number[]) => {
layer.color('name', [convertRGB2Hex(color)], {
featureRange: {
startIndex: styleOptions.featureRange.startIndex,
endIndex: styleOptions.featureRange.endIndex,
},
});
scene.render();
});
}
public render() {
return (
<div
id="map"
style={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
}}
/>
);
}
}

View File

@ -2,25 +2,25 @@ import { storiesOf } from '@storybook/react';
import * as React from 'react';
import AMap from './components/AMap';
import Mapbox from './components/Mapbox';
import Polygon from './components/Polygon';
import Point3D from './components/Point3D';
import Line from './components/Line';
import ImageLayer from './components/Image';
import GridHeatMap from './components/GridHeatmap';
import PointImage from './components/pointImage';
// import Polygon from './components/Polygon';
// import Point3D from './components/Point3D';
// import Line from './components/Line';
// import ImageLayer from './components/Image';
// import GridHeatMap from './components/GridHeatmap';
// import PointImage from './components/pointImage';
// @ts-ignore
import notes from './Map.md';
storiesOf('地图底图测试', module)
storiesOf('地图底图', module)
.add('高德地图', () => <AMap />, {
notes: { markdown: notes },
})
.add('Mapbox', () => <Mapbox />, {
notes: { markdown: notes },
})
.add('Polygon', () => <Polygon />)
.add('Point3D', () => <Point3D />)
.add('Line', () => <Line />)
.add('GridHeatMap', () => <GridHeatMap />)
.add('Image', () => <ImageLayer />)
.add('pointImage', () => <PointImage />);
});
// .add('Polygon', () => <Polygon />);
// .add('Point3D', () => <Point3D />)
// .add('Line', () => <Line />)
// .add('GridHeatMap', () => <GridHeatMap />)
// .add('Image', () => <ImageLayer />)
// .add('pointImage', () => <PointImage />);

View File

@ -1,7 +1,8 @@
import { PointLayer } from '@l7/layers';
// @ts-ignore
import { PolygonLayer } from '@l7/layers';
// @ts-ignore
import { Scene } from '@l7/scene';
import * as React from 'react';
import data from './data.json';
export default class AMap extends React.Component {
private scene: Scene;
@ -10,18 +11,36 @@ export default class AMap extends React.Component {
this.scene.destroy();
}
public componentDidMount() {
public async componentDidMount() {
const response = await fetch(
'https://gw.alipayobjects.com/os/basement_prod/d2e0e930-fd44-4fca-8872-c1037b0fee7b.json',
);
const scene = new Scene({
center: [120.19382669582967, 30.258134],
center: [110.19382669582967, 50.258134],
id: 'map',
pitch: 0,
style: 'dark',
type: 'amap',
zoom: 1,
zoom: 3,
});
const pointLayer = new PointLayer({});
pointLayer.source(data);
scene.addLayer(pointLayer);
const layer = new PolygonLayer({});
layer
.source(await response.json())
.size('name', [0, 10000, 50000, 30000, 100000])
.color('name', [
'#2E8AE6',
'#69D1AB',
'#DAF291',
'#FFD591',
'#FF7A45',
'#CF1D49',
])
.shape('fill')
.style({
opacity: 0.8,
});
scene.addLayer(layer);
scene.render();
this.scene = scene;
}

View File

@ -1,73 +1,77 @@
import { PointLayer } from '@l7/layers';
// @ts-ignore
import '!style-loader!css-loader!./css/l7.css';
import { Marker, Popup, Scale, Zoom } from '@l7/component';
import { PolygonLayer } from '@l7/layers';
// @ts-ignore
import { Scene } from '@l7/scene';
import * as dat from 'dat.gui';
import * as React from 'react';
import data from './data2.json';
export default class Mapbox extends React.Component {
private gui: dat.GUI;
private $stats: Node;
private scene: Scene;
public componentWillUnmount() {
if (this.gui) {
this.gui.destroy();
}
if (this.$stats) {
document.body.removeChild(this.$stats);
}
this.scene.destroy();
}
public async componentDidMount() {
const response = await fetch(
'https://gw.alipayobjects.com/os/basement_prod/d2e0e930-fd44-4fca-8872-c1037b0fee7b.json',
);
const scene = new Scene({
id: 'map',
type: 'mapbox',
style: 'mapbox://styles/mapbox/streets-v9',
center: [120.19382669582967, 30.258134],
center: [110.19382669582967, 30.258134],
pitch: 0,
zoom: 2,
zoom: 3,
});
const pointLayer = new PointLayer({});
// TODO: new GeoJSONSource()
pointLayer.source(data);
// .size('mag', [2, 10])
// .color('mag', [
// '#2E8AE6',
// '#69D1AB',
// '#DAF291',
// '#FFD591',
// '#FF7A45',
// '#CF1D49',
// ]);
scene.addLayer(pointLayer);
scene.render();
this.scene = scene;
const layer = new PolygonLayer({});
/*** 运行时修改样式属性 ***/
const gui = new dat.GUI();
this.gui = gui;
const pointFolder = gui.addFolder('Point 样式属性');
pointFolder
.addColor(pointLayer.styleOptions, 'pointColor')
.onChange((pointColor: [number, number, number]) => {
pointLayer.style({
pointColor,
});
scene.render();
layer
.source(await response.json())
.size('name', [0, 10000, 50000, 30000, 100000])
.color('name', [
'#2E8AE6',
'#69D1AB',
'#DAF291',
'#FFD591',
'#FF7A45',
'#CF1D49',
])
.shape('fill')
.style({
opacity: 0.8,
});
pointFolder
.add(pointLayer.styleOptions, 'strokeWidth', 1, 10, 0.1)
.onChange((strokeWidth: number) => {
pointLayer.style({
strokeWidth,
});
scene.render();
scene.addLayer(layer);
scene.render();
scene.on('loaded', () => {
const zoomControl = new Zoom({
position: 'bottomright',
});
const scaleControl = new Scale();
const popup = new Popup({
offsets: [0, 20],
})
.setLnglat({
lng: 120.19382669582967,
lat: 30.258134,
})
.setText('hello')
.addTo(scene);
const maker = new Marker();
maker
.setLnglat({
lng: 120.19382669582967,
lat: 30.258134,
})
.addTo(scene);
scene.addControl(zoomControl);
scene.addControl(scaleControl);
console.log(layer);
// layer.fitBounds();
});
}
public render() {

View File

@ -1,8 +1,16 @@
// @ts-ignore
import { PolygonLayer } from '@l7/layers';
// @ts-ignore
import { Scene } from '@l7/scene';
import * as dat from 'dat.gui';
import * as React from 'react';
function convertRGB2Hex(rgb: number[]) {
return (
'#' + rgb.map((r) => ('0' + Math.floor(r).toString(16)).slice(-2)).join('')
);
}
export default class Mapbox extends React.Component {
private gui: dat.GUI;
private $stats: Node;
@ -22,29 +30,6 @@ export default class Mapbox extends React.Component {
const response = await fetch(
'https://gw.alipayobjects.com/os/basement_prod/d2e0e930-fd44-4fca-8872-c1037b0fee7b.json',
);
const data = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
properties: {
name: 'test',
},
geometry: {
type: 'Polygon',
coordinates: [
[
[108.28125, 40.17887331434696],
[114.78515624999999, 24.367113562651262],
[119.88281249999999, 31.952162238024975],
[108.28125, 40.17887331434696],
],
],
},
},
],
};
// data.features = data.features.slice(1, 12);
const scene = new Scene({
id: 'map',
type: 'mapbox',
@ -53,10 +38,8 @@ export default class Mapbox extends React.Component {
pitch: 0,
zoom: 3,
});
const layer = new PolygonLayer({
enableMultiPassRenderer: true,
passes: [],
});
this.scene = scene;
const layer = new PolygonLayer({});
// TODO: new GeoJSONSource()
layer
@ -72,22 +55,32 @@ export default class Mapbox extends React.Component {
])
.shape('fill')
.style({
opacity: 0.8,
opacity: 0.2,
});
scene.addLayer(layer);
scene.render();
/*** 运行时修改样式属性 ***/
// const gui = new dat.GUI();
// this.gui = gui;
// const pointFolder = gui.addFolder('Polygon 样式属性');
// pointFolder
// .add(layer.styleOptions, 'opacity')
// .onChange((opacity: number) => {
// layer.style({
// opacity,
// });
// scene.render();
// });
const gui = new dat.GUI();
this.gui = gui;
const styleOptions = {
color: [0, 0, 0],
featureRange: {
startIndex: 0,
endIndex: Infinity,
},
};
const pointFolder = gui.addFolder('精确更新 feature');
pointFolder.add(styleOptions.featureRange, 'startIndex', 0, 100, 1);
pointFolder.add(styleOptions.featureRange, 'endIndex', 0, 100, 1);
pointFolder.addColor(styleOptions, 'color').onChange((color: number[]) => {
layer.color('name', [convertRGB2Hex(color)], {
featureRange: {
startIndex: styleOptions.featureRange.startIndex,
endIndex: styleOptions.featureRange.endIndex,
},
});
scene.render();
});
}
public render() {

View File

@ -0,0 +1,5 @@
import { storiesOf } from '@storybook/react';
import * as React from 'react';
import Polygon from './components/Polygon';
storiesOf('MultiPassRenderer', module).add('blur', () => <Polygon />);

View File

@ -0,0 +1,85 @@
// @ts-ignore
import { PolygonLayer } from '@l7/layers';
// @ts-ignore
import { Scene } from '@l7/scene';
import * as dat from 'dat.gui';
import * as React from 'react';
export default class Mapbox extends React.Component {
private gui: dat.GUI;
private $stats: Node;
private scene: Scene;
public componentWillUnmount() {
if (this.gui) {
this.gui.destroy();
}
if (this.$stats) {
document.body.removeChild(this.$stats);
}
this.scene.destroy();
}
public async componentDidMount() {
const response = await fetch(
'https://gw.alipayobjects.com/os/basement_prod/d2e0e930-fd44-4fca-8872-c1037b0fee7b.json',
);
const data = await response.json();
const scene = new Scene({
id: 'map',
type: 'mapbox',
style: 'mapbox://styles/mapbox/streets-v9',
center: [110.19382669582967, 50.258134],
pitch: 0,
zoom: 3,
});
const layer = new PolygonLayer({
enablePicking: false,
passes: [
'blurH',
[
'blurV',
{
blurRadius: 8,
},
],
],
});
layer
.source(data)
.size('name', [0, 10000, 50000, 30000, 100000])
.color('name', [
'#2E8AE6',
'#69D1AB',
'#DAF291',
'#FFD591',
'#FF7A45',
'#CF1D49',
])
.shape('fill')
.style({
opacity: 0.8,
});
scene.addLayer(layer);
scene.render();
this.scene = scene;
}
public render() {
return (
<div
id="map"
style={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
}}
/>
);
}
}

View File

@ -0,0 +1,10 @@
import { storiesOf } from '@storybook/react';
import * as React from 'react';
import AdvancedAPI from './components/AdvancedAPI';
import Highlight from './components/Highlight';
import Tooltip from './components/Tooltip';
storiesOf('交互', module)
.add('拾取 & 高亮', () => <Highlight />)
.add('拾取 & Tooltip', () => <Tooltip />)
.add('高级拾取 API', () => <AdvancedAPI />);

View File

@ -0,0 +1,117 @@
// @ts-ignore
import { PolygonLayer } from '@l7/layers';
// @ts-ignore
import { Scene } from '@l7/scene';
import * as dat from 'dat.gui';
import * as React from 'react';
export default class AdvancedAPI extends React.Component {
private gui: dat.GUI;
private $stats: Node;
private scene: Scene;
public componentWillUnmount() {
if (this.gui) {
this.gui.destroy();
}
if (this.$stats) {
document.body.removeChild(this.$stats);
}
this.scene.destroy();
}
public async componentDidMount() {
const response = await fetch(
'https://gw.alipayobjects.com/os/basement_prod/d2e0e930-fd44-4fca-8872-c1037b0fee7b.json',
);
const scene = new Scene({
id: 'map',
type: 'mapbox',
style: 'mapbox://styles/mapbox/streets-v9',
center: [110.19382669582967, 50.258134],
pitch: 0,
zoom: 3,
});
const layer = new PolygonLayer({
enablePicking: true,
enableHighlight: true,
highlightColor: [0, 0, 1, 1],
onHover: (pickedFeature) => {
// tslint:disable-next-line:no-console
console.log(pickedFeature);
},
});
layer
.source(await response.json())
.size('name', [0, 10000, 50000, 30000, 100000])
.color('name', [
'#2E8AE6',
'#69D1AB',
'#DAF291',
'#FFD591',
'#FF7A45',
'#CF1D49',
])
.shape('fill')
.style({
opacity: 0.8,
});
scene.addLayer(layer);
scene.render();
this.scene = scene;
/*** 运行时修改样式属性 ***/
const gui = new dat.GUI();
this.gui = gui;
const styleOptions = {
enablePicking: true,
enableHighlight: true,
highlightColor: [0, 0, 255],
};
const pointFolder = gui.addFolder('拾取 & 高亮');
// pointFolder
// .add(styleOptions, 'enablePicking')
// .onChange((enablePicking: boolean) => {
// // FIXME: 该配置项会影响到初始化阶段 PixelPickingPass 的添加,暂不支持在运行时更改
// layer.style({
// enablePicking,
// });
// scene.render();
// });
pointFolder
.add(styleOptions, 'enableHighlight')
.onChange((enableHighlight: boolean) => {
layer.style({
enableHighlight,
});
scene.render();
});
pointFolder
.addColor(styleOptions, 'highlightColor')
.onChange((highlightColor: number[]) => {
const [r, g, b] = highlightColor.map((c) => c / 255);
layer.style({
highlightColor: [r, g, b, 1],
});
scene.render();
});
pointFolder.open();
}
public render() {
return (
<div
id="map"
style={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
}}
/>
);
}
}

View File

@ -0,0 +1,117 @@
// @ts-ignore
import { PolygonLayer } from '@l7/layers';
// @ts-ignore
import { Scene } from '@l7/scene';
import * as dat from 'dat.gui';
import * as React from 'react';
export default class Highlight extends React.Component {
private gui: dat.GUI;
private $stats: Node;
private scene: Scene;
public componentWillUnmount() {
if (this.gui) {
this.gui.destroy();
}
if (this.$stats) {
document.body.removeChild(this.$stats);
}
this.scene.destroy();
}
public async componentDidMount() {
const response = await fetch(
'https://gw.alipayobjects.com/os/basement_prod/d2e0e930-fd44-4fca-8872-c1037b0fee7b.json',
);
const scene = new Scene({
id: 'map',
type: 'mapbox',
style: 'mapbox://styles/mapbox/streets-v9',
center: [110.19382669582967, 50.258134],
pitch: 0,
zoom: 3,
});
const layer = new PolygonLayer({
enablePicking: true,
enableHighlight: true,
highlightColor: [0, 0, 1, 1],
onHover: (pickedFeature) => {
// tslint:disable-next-line:no-console
console.log(pickedFeature);
},
});
layer
.source(await response.json())
.size('name', [0, 10000, 50000, 30000, 100000])
.color('name', [
'#2E8AE6',
'#69D1AB',
'#DAF291',
'#FFD591',
'#FF7A45',
'#CF1D49',
])
.shape('fill')
.style({
opacity: 1.0,
});
scene.addLayer(layer);
scene.render();
this.scene = scene;
/*** 运行时修改样式属性 ***/
const gui = new dat.GUI();
this.gui = gui;
const styleOptions = {
enablePicking: true,
enableHighlight: true,
highlightColor: [0, 0, 255],
};
const pointFolder = gui.addFolder('拾取 & 高亮');
// pointFolder
// .add(styleOptions, 'enablePicking')
// .onChange((enablePicking: boolean) => {
// // FIXME: 该配置项会影响到初始化阶段 PixelPickingPass 的添加,暂不支持在运行时更改
// layer.style({
// enablePicking,
// });
// scene.render();
// });
pointFolder
.add(styleOptions, 'enableHighlight')
.onChange((enableHighlight: boolean) => {
layer.style({
enableHighlight,
});
scene.render();
});
pointFolder
.addColor(styleOptions, 'highlightColor')
.onChange((highlightColor: number[]) => {
const [r, g, b] = highlightColor.map((c) => c / 255);
layer.style({
highlightColor: [r, g, b, 1],
});
scene.render();
});
pointFolder.open();
}
public render() {
return (
<div
id="map"
style={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
}}
/>
);
}
}

View File

@ -0,0 +1,71 @@
// @ts-ignore
import { PolygonLayer } from '@l7/layers';
// @ts-ignore
import { Scene } from '@l7/scene';
import * as dat from 'dat.gui';
import * as React from 'react';
export default class Mapbox extends React.Component {
private scene: Scene;
public componentWillUnmount() {
this.scene.destroy();
}
public async componentDidMount() {
const response = await fetch(
'https://gw.alipayobjects.com/os/basement_prod/d2e0e930-fd44-4fca-8872-c1037b0fee7b.json',
);
const scene = new Scene({
id: 'map',
type: 'mapbox',
style: 'mapbox://styles/mapbox/streets-v9',
center: [110.19382669582967, 50.258134],
pitch: 0,
zoom: 3,
});
const layer = new PolygonLayer({
enablePicking: true,
enableHighlight: false,
onHover: (pickedFeature) => {
// tslint:disable-next-line:no-console
console.log(pickedFeature);
},
});
layer
.source(await response.json())
.size('name', [0, 10000, 50000, 30000, 100000])
.color('name', [
'#2E8AE6',
'#69D1AB',
'#DAF291',
'#FFD591',
'#FF7A45',
'#CF1D49',
])
.shape('fill')
.style({
opacity: 0.8,
});
scene.addLayer(layer);
scene.render();
this.scene = scene;
}
public render() {
return (
<div
id="map"
style={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
}}
/>
);
}
}

View File

@ -3,6 +3,7 @@
"noEmit": true,
"strict": true,
"strictPropertyInitialization": false,
"downlevelIteration": true,
"jsx": "react",
"target": "es5",
"lib": ["es6", "dom"],