feat: 样式数据映射 1.1 调整代码,提取公共部分到 BaseModel

This commit is contained in:
2912401452 2021-06-21 18:13:36 +08:00
parent cb626e6f59
commit 535665ba31
6 changed files with 245 additions and 402 deletions

View File

@ -1010,12 +1010,6 @@ export default class BaseLayer<ChildLayerStyleOptions = {}> extends EventEmitter
valuesOrCallback?: unknown[], valuesOrCallback?: unknown[],
defaultValues?: unknown[], defaultValues?: unknown[],
) { ) {
// console.log({
// values: isFunction(valuesOrCallback)
// ? undefined
// : valuesOrCallback || defaultValues,
// callback: isFunction(valuesOrCallback) ? valuesOrCallback : undefined,
// })
return { return {
values: isFunction(valuesOrCallback) values: isFunction(valuesOrCallback)
? undefined ? undefined

View File

@ -24,17 +24,13 @@ import {
Triangulation, Triangulation,
TYPES, TYPES,
} from '@antv/l7-core'; } from '@antv/l7-core';
import { isColor, rgb2arr } from '@antv/l7-utils';
import { BlendTypes } from '../utils/blend'; import { BlendTypes } from '../utils/blend';
import { getSize } from '../utils/dataMappingStyle'; interface ICellProperty {
attr: string;
interface IDataLayout { count: number;
widthCount: number;
heightCount: number;
widthStep: number;
widthStart: number;
heightStep: number;
heightStart: number;
} }
export default class BaseModel<ChildLayerStyleOptions = {}> export default class BaseModel<ChildLayerStyleOptions = {}>
implements ILayerModel { implements ILayerModel {
public triangulation: Triangulation; public triangulation: Triangulation;
@ -43,17 +39,22 @@ export default class BaseModel<ChildLayerStyleOptions = {}>
public createTexture2D: ( public createTexture2D: (
options: ITexture2DInitializationOptions, options: ITexture2DInitializationOptions,
) => ITexture2D; ) => ITexture2D;
public dataLayout: IDataLayout = {
// 默认值
widthCount: 1024,
heightCount: 1,
widthStep: 1 / 1024,
widthStart: 1 / 2048,
heightStep: 1,
heightStart: 0.5,
};
protected layer: ILayer; protected layer: ILayer;
protected DATA_TEXTURE_WIDTH: number; // 默认有多少列(宽度)
protected rowCount: number; // 计算得到的当前数据纹理有多少行(高度)
protected curretnOpacity: any = ''; // 当前的 opacity 值
protected curretnStrokeOpacity: any = ''; // 当前的 strokeOpacity 值
protected currentStrokeColor: any = ''; // 当前的 strokeColor 值
protected currentStrokeWidth: any = ''; // 当前的 strokeWidth 值
protected currentOffsets: any = ''; // 当前的 strokeOffsets 值
protected cellLength: number; // 单个 cell 的长度
protected cellProperties: ICellProperty[]; // 需要进行数据映射的属性集合
protected hasOpacity: number = 0;
protected hasStrokeOpacity: number = 0;
protected hasStrokeWidth: number = 0;
protected hasStroke: number = 0;
protected hasOffsets: number = 0;
@lazyInject(TYPES.IGlobalConfigService) @lazyInject(TYPES.IGlobalConfigService)
protected readonly configService: IGlobalConfigService; protected readonly configService: IGlobalConfigService;
@ -75,10 +76,6 @@ export default class BaseModel<ChildLayerStyleOptions = {}>
protected cameraService: ICameraService; protected cameraService: ICameraService;
protected layerService: ILayerService; protected layerService: ILayerService;
protected opacityTexture: ITexture2D;
protected strokeOpacityTexture: ITexture2D;
protected strokeTexture: ITexture2D;
protected strokeWidthTexture: ITexture2D;
// style texture data mapping // style texture data mapping
constructor(layer: ILayer) { constructor(layer: ILayer) {
@ -110,25 +107,101 @@ export default class BaseModel<ChildLayerStyleOptions = {}>
const { createTexture2D } = this.rendererService; const { createTexture2D } = this.rendererService;
this.createTexture2D = createTexture2D; this.createTexture2D = createTexture2D;
// 根据数据长度构建样式数据映射到纹理上时需要到布局数值 为纹理贴图映射样式数据做准备工作 this.DATA_TEXTURE_WIDTH = 1024; // 数据纹理固定宽度
this.initEncodeDataLayout(this.layer.getEncodedData().length); this.rowCount = 1;
this.cellLength = 0;
this.cellProperties = [];
}
// style datatexture mapping
/**
*
* @param d
* @param count
*/
public patchMod(d: number[], count: number) {
for (let i = 0; i < count; i++) {
d.push(-1);
}
} }
/** /**
* *
* @param dataLength * @param d
* @param cellData
* @param cellPropertiesLayouts
*/ */
public initEncodeDataLayout(dataLength: number) { public patchData(d: number[], cellData: any, cellPropertiesLayouts: any) {
const { width: widthCount, height: heightCount } = getSize(dataLength); for (const layout of cellPropertiesLayouts) {
this.dataLayout.widthCount = widthCount; const { attr, count } = layout;
this.dataLayout.heightCount = heightCount; if (!cellData) {
if (attr === 'stroke') {
d.push(-1, -1, -1, -1);
} else if (attr === 'offsets') {
d.push(-1, -1);
} else {
d.push(-1);
}
} else {
const value = cellData[attr];
this.dataLayout.widthStep = 1 / widthCount; if (value) {
this.dataLayout.widthStart = this.dataLayout.widthStep / 2; // 数据中存在该属性
this.dataLayout.heightStep = 1 / heightCount; if (attr === 'stroke') {
this.dataLayout.heightStart = this.dataLayout.heightStep / 2; d.push(...rgb2arr(value));
} else if (attr === 'offsets') {
// d.push(...value)
d.push(-value[0], value[1]);
} else {
d.push(value);
}
} else {
// 若不存在时则补位
this.patchMod(d, count);
}
}
}
} }
/**
*
* @param cellLength
* @param encodeData
* @param cellPropertiesLayouts
* @returns
*/
public calDataFrame(
cellLength: number,
encodeData: any,
cellPropertiesLayouts: any,
): any {
if (cellLength > this.DATA_TEXTURE_WIDTH) {
// console.log('failed');
return false;
}
const encodeDatalength = encodeData.length;
const rowCount = Math.ceil(
(encodeDatalength * cellLength) / this.DATA_TEXTURE_WIDTH,
); // 有多少行
const totalLength = rowCount * this.DATA_TEXTURE_WIDTH;
const d: number[] = [];
for (let i = 0; i < encodeDatalength; i++) {
// 根据 encodeData 数据推入数据
const cellData = encodeData[i];
this.patchData(d, cellData, cellPropertiesLayouts);
}
for (let i = d.length; i < totalLength; i++) {
// 每行不足的部分用 -1 补足(数据纹理时 width * height 的矩形数据集合)
d.push(-1);
}
return { data: d, width: this.DATA_TEXTURE_WIDTH, height: rowCount };
}
// style datatexture mapping
public getBlend(): IBlendOptions { public getBlend(): IBlendOptions {
const { blend = 'normal' } = this.layer.getLayerConfig(); const { blend = 'normal' } = this.layer.getLayerConfig();
return BlendTypes[BlendType[blend]] as IBlendOptions; return BlendTypes[BlendType[blend]] as IBlendOptions;

View File

@ -17,12 +17,6 @@ import pointFillFrag from '../shaders/fill_frag.glsl';
import pointFillVert from '../shaders/fill_vert.glsl'; import pointFillVert from '../shaders/fill_vert.glsl';
import { isArray, isNumber, isString } from 'lodash'; import { isArray, isNumber, isString } from 'lodash';
import {
getSize,
getUvPosition,
initTextureFloatData,
initTextureVec4Data,
} from '../../utils/dataMappingStyle';
interface IPointLayerStyleOptions { interface IPointLayerStyleOptions {
opacity: any; opacity: any;
strokeWidth: number; strokeWidth: number;
@ -30,115 +24,10 @@ interface IPointLayerStyleOptions {
strokeOpacity: number; strokeOpacity: number;
offsets: [number, number]; offsets: [number, number];
} }
// 用于判断 opacity 的值是否发生该改变
const curretnOpacity: any = '';
const curretnStrokeOpacity: any = '';
const currentStrokeColor: any = '';
const currentStrokeWidth: any = '';
let testTexture = true;
const cellPropertiesLayout = [
{ attr: 'opacity', flag: true, count: 1 },
{ attr: 'strokeOpacity', flag: true, count: 1 },
{ attr: 'strokeWidth', flag: true, count: 1 },
{ attr: 'stroke', flag: true, count: 4 },
];
const WIDTH = 1024; // 数据纹理的固定长度
// let WIDTH = 10
let calHeight = 1;
/**
*
* @param d
* @param cellData
* @param cellPropertiesLayouts
*/
function patchData(d: number[], cellData: any, cellPropertiesLayouts: any) {
for (const layout of cellPropertiesLayouts) {
const { attr, count } = layout;
if (!cellData) {
if (attr === 'stroke') {
d.push(-1, -1, -1, -1);
} else if (attr === 'offsets') {
d.push(-1, -1);
} else {
d.push(-1);
}
} else {
const value = cellData[attr];
if (value) {
// 数据中存在该属性
if (attr === 'stroke') {
d.push(...rgb2arr(value));
} else if (attr === 'offsets') {
// d.push(...value)
d.push(-value[0], value[1]);
} else {
d.push(value);
}
} else {
// 若不存在时则补位
patchMod(d, count);
}
}
}
}
/**
*
* @param d
* @param count
*/
function patchMod(d: number[], count: number) {
for (let i = 0; i < count; i++) {
d.push(-1);
}
}
/**
*
* @param cellLength
* @param encodeData
* @param cellPropertiesLayouts
* @returns
*/
function calDataFrame(
cellLength: number,
encodeData: any,
cellPropertiesLayouts: any,
): any {
if (cellLength > WIDTH) {
// console.log('failed');
return false;
}
const encodeDatalength = encodeData.length;
// WIDTH 行数固定
const rowCount = Math.ceil((encodeDatalength * cellLength) / WIDTH); // 有多少行
const totalLength = rowCount * WIDTH;
const d: number[] = [];
for (let i = 0; i < encodeDatalength; i++) {
// 根据 encodeData 数据推入数据
const cellData = encodeData[i];
patchData(d, cellData, cellPropertiesLayouts);
}
for (let i = d.length; i < totalLength; i++) {
// 每行不足的部分用 -1 补足(数据纹理时 width * height 的矩形数据集合)
d.push(-1);
}
// console.log(d, rowCount)
return { data: d, width: WIDTH, height: rowCount };
}
// 判断当前使用的 style 中的变量属性是否需要进行数据映射 // 判断当前使用的 style 中的变量属性是否需要进行数据映射
let hasOpacity = 0;
let hasStrokeOpacity = 0;
let hasStrokeWidth = 0;
let hasStroke = 0;
let hasOffsets = 0;
export default class FillModel extends BaseModel { export default class FillModel extends BaseModel {
protected testDataTexture: ITexture2D; protected dataTexture: ITexture2D;
/** /**
* offsets * offsets
@ -158,100 +47,133 @@ export default class FillModel extends BaseModel {
} }
} }
public isColorStatic(stroke: any) { /**
if ( * - layer
isArray(stroke) && * @param opacity
stroke.length === 4 && * @param strokeOpacity
isNumber(stroke[0]) && * @param strokeWidth
isNumber(stroke[1]) && * @param stroke
isNumber(stroke[2]) && * @param offsets
isNumber(stroke[3]) * @returns
) { */
return true; public isDataTextureUpdate(
} else { opacity: any,
return false; strokeOpacity: any,
strokeWidth: any,
stroke: any,
offsets: any,
) {
let isUpdate = false;
if (this.curretnOpacity !== JSON.stringify(opacity)) {
// 判断 opacity 是否发生改变
isUpdate = true;
this.curretnOpacity = JSON.stringify(opacity);
} }
if (this.curretnStrokeOpacity !== JSON.stringify(strokeOpacity)) {
// 判断 strokeOpacity 是否发生改变
isUpdate = true;
this.curretnStrokeOpacity = JSON.stringify(strokeOpacity);
}
if (this.currentStrokeWidth !== JSON.stringify(strokeWidth)) {
// 判断 strokeWidth 是否发生改变
isUpdate = true;
this.currentStrokeWidth = JSON.stringify(strokeWidth);
}
if (this.currentStrokeColor !== JSON.stringify(stroke)) {
// 判断 stroke 是否发生改变
isUpdate = true;
this.currentStrokeColor = JSON.stringify(stroke);
}
if (this.currentOffsets !== JSON.stringify(offsets)) {
// 判断 offsets 是否发生改变
isUpdate = true;
this.currentOffsets = JSON.stringify(offsets);
}
if (this.dataTexture === undefined) {
isUpdate = true;
}
return isUpdate;
}
/**
* - layer
*/
public clearLastCalRes() {
this.cellProperties = []; // 清空上一次计算的需要进行数据映射的属性集合
this.cellLength = 0; // 清空上一次计算的 cell 的长度
this.hasOpacity = 0; // 清空上一次是否需要对 opacity 属性进行数据映射的判断
this.hasStrokeOpacity = 0; // 清空上一次是否需要对 strokeOpacity 属性进行数据映射的判断
this.hasStrokeWidth = 0; // 清空上一次是否需要对 strokeWidth 属性进行数据映射的判断
this.hasStroke = 0; // 清空上一次是否需要对 stroke 属性进行数据映射的判断
this.hasOffsets = 0; // 清空上一次是否需要对 offsets 属性进行数据映射的判断
} }
public getUninforms(): IModelUniform { public getUninforms(): IModelUniform {
const { const {
opacity = 1, opacity = 1,
strokeOpacity = 1, strokeOpacity = 1,
strokeWidth = 2, strokeWidth = 0,
stroke = [0, 0, 0, 0], stroke = 'rgba(0,0,0,0)',
offsets = [0, 0], offsets = [0, 0],
} = this.layer.getLayerConfig() as IPointLayerStyleOptions; } = this.layer.getLayerConfig() as IPointLayerStyleOptions;
if (testTexture) { if (
const cellProperties = []; // cell 的布局 this.isDataTextureUpdate(
let cellLength = 0; // cell 的长度 opacity,
strokeOpacity,
strokeWidth,
stroke,
offsets,
)
) {
this.clearLastCalRes(); // 清除上一次的计算结果
if (!isNumber(opacity) && opacity !== undefined) { if (!isNumber(opacity)) {
// 数据映射 // 数据映射
cellProperties.push({ attr: 'opacity', flag: true, count: 1 }); this.cellProperties.push({ attr: 'opacity', count: 1 });
hasOpacity = 1; this.hasOpacity = 1;
cellLength += 1; this.cellLength += 1;
} else {
// 常量
hasOpacity = 0;
} }
if (!isNumber(strokeOpacity) && strokeOpacity !== undefined) { if (!isNumber(strokeOpacity)) {
// 数据映射 // 数据映射
cellProperties.push({ attr: 'strokeOpacity', flag: true, count: 1 }); this.cellProperties.push({ attr: 'strokeOpacity', count: 1 });
hasStrokeOpacity = 1; this.hasStrokeOpacity = 1;
cellLength += 1; this.cellLength += 1;
} else {
// 常量
hasStrokeOpacity = 0;
} }
if (!isNumber(strokeWidth) && strokeWidth !== undefined) { if (!isNumber(strokeWidth)) {
// 数据映射 // 数据映射
cellProperties.push({ attr: 'strokeWidth', flag: true, count: 1 }); this.cellProperties.push({ attr: 'strokeWidth', count: 1 });
hasStrokeWidth = 1; this.hasStrokeWidth = 1;
cellLength += 1; this.cellLength += 1;
} else {
// 常量
hasStrokeWidth = 0;
} }
// console.log('stroke', stroke);
// if((!isString(stroke) || !isColor(stroke)) && stroke !== undefined) { // 数据映射 if (!isColor(stroke)) {
if (!this.isColorStatic(stroke)) {
// 数据映射 // 数据映射
cellProperties.push({ attr: 'stroke', flag: true, count: 4 }); this.cellProperties.push({ attr: 'stroke', count: 4 });
cellLength += 4; this.cellLength += 4;
hasStroke = 1; this.hasStroke = 1;
} else {
// 常量
hasStroke = 0;
} }
if (!this.isOffsetStatic(offsets)) { if (!this.isOffsetStatic(offsets)) {
cellProperties.push({ attr: 'offsets', flag: true, count: 2 }); // 数据映射
cellLength += 2; this.cellProperties.push({ attr: 'offsets', count: 2 });
hasOffsets = 1; this.cellLength += 2;
} else { this.hasOffsets = 1;
hasOffsets = 0;
} }
// console.log('cellProperties', cellProperties);
// console.log('cellLength', cellLength);
// console.log('hasStrokeOpacity', hasStrokeOpacity)
// console.log('hasStrokeWidth', hasStrokeWidth)
const encodeData = this.layer.getEncodedData(); const encodeData = this.layer.getEncodedData();
// console.log('encodeData', encodeData) if (this.cellLength > 0) {
// let {data, width, height } = calDataFrame(cellLength, encodeData, cellPropertiesLayout) // 需要构建数据纹理
if (cellLength > 0) { const { data, width, height } = this.calDataFrame(
const { data, width, height } = calDataFrame( this.cellLength,
cellLength,
encodeData, encodeData,
cellProperties, this.cellProperties,
); );
calHeight = height; this.rowCount = height; // 当前数据纹理有多少行
this.testDataTexture = this.createTexture2D({ this.dataTexture = this.createTexture2D({
flipY: true, flipY: true,
data, data,
format: gl.LUMINANCE, format: gl.LUMINANCE,
@ -260,7 +182,8 @@ export default class FillModel extends BaseModel {
height, height,
}); });
} else { } else {
this.testDataTexture = this.createTexture2D({ // 不需要构建数据纹理 - 构建一个空纹理
this.dataTexture = this.createTexture2D({
flipY: true, flipY: true,
data: [1], data: [1],
format: gl.LUMINANCE, format: gl.LUMINANCE,
@ -269,27 +192,24 @@ export default class FillModel extends BaseModel {
height: 1, height: 1,
}); });
} }
// console.log('strokeOpacity', strokeOpacity, isNumber(strokeOpacity)? strokeOpacity : 1.0)
testTexture = false;
} }
return { return {
u_testTexture: this.testDataTexture, // 数据纹理 - 有数据映射的时候纹理中带数据,若没有任何数据映射时纹理是 [1] u_dataTexture: this.dataTexture, // 数据纹理 - 有数据映射的时候纹理中带数据,若没有任何数据映射时纹理是 [1]
u_cellTypeLayout: [ u_cellTypeLayout: [
// 传递样式数据映射信息 - 当前纹理大小以及有哪些字段需要映射 // 传递样式数据映射信息 - 当前纹理大小以及有哪些字段需要映射
calHeight, this.rowCount, // 数据纹理有几行
WIDTH, this.DATA_TEXTURE_WIDTH, // 数据纹理有几列
0.0, 0.0,
0.0, // rowCount columnCount - 几行几列 0.0,
hasOpacity, this.hasOpacity, // cell 中是否存在 opacity
hasStrokeOpacity, this.hasStrokeOpacity, // cell 中是否存在 strokeOpacity
hasStrokeWidth, this.hasStrokeWidth, // cell 中是否存在 strokeWidth
hasStroke, // opacity strokeOpacity strokeWidth stroke this.hasStroke, // cell 中是否存在 stroke
hasOffsets, this.hasOffsets, // cell 中是否存在 offsets
0.0,
0.0, 0.0,
0.0, 0.0,
0.0, // offsets
0.0, 0.0,
0.0, 0.0,
0.0, 0.0,
@ -327,36 +247,6 @@ export default class FillModel extends BaseModel {
} }
public initModels(): IModel[] { public initModels(): IModel[] {
// if(cellLength < 1) {
// console.log('err')
// }
// let encodeData = this.layer.getEncodedData()
// dataFrameArr = []
// let mod = WIDTH%cellLength
// let rowCellCount = (WIDTH-mod)/cellLength
// let encodeDatalength = encodeData.length
// let heightCount = Math.ceil(encodeDatalength * cellLength / (WIDTH - mod))
// let heightStep = 1/heightCount
// let heightStart = heightStep/2
// let widthStart = (1/WIDTH)/2
// let widthStep = (1/WIDTH) * (cellLength - 1.0)
// for(let i = 0; i < heightCount; i++) { // 行
// for(let j = 0; j < rowCellCount; j++) {
// let startU = widthStart + widthStep * j
// let startV = 1 - (heightStart + heightStep * i)
// let endU = startU + widthStep
// let endV = startV
// dataFrameArr.push({ startU, startV, endU, endV })
// }
// }
// console.log('dataFrameArr', dataFrameArr)
return this.buildModels(); return this.buildModels();
} }

View File

@ -6,7 +6,7 @@ attribute float a_Shape;
attribute float a_vertexId; attribute float a_vertexId;
uniform mat4 u_cellTypeLayout; uniform mat4 u_cellTypeLayout;
uniform sampler2D u_testTexture; uniform sampler2D u_dataTexture;
varying mat4 styleMappingMat; varying mat4 styleMappingMat;
uniform mat4 u_ModelMatrix; uniform mat4 u_ModelMatrix;
@ -49,7 +49,7 @@ vec2 nextPos(float currentRow, float currentColumn, float columnCount, float nex
float pos2value(vec2 pos, float columnWidth, float rowHeight) { float pos2value(vec2 pos, float columnWidth, float rowHeight) {
float u = (pos.r - 1.0) * columnWidth + columnWidth/2.0; float u = (pos.r - 1.0) * columnWidth + columnWidth/2.0;
float v = 1.0 - ((pos.g - 1.0) * rowHeight + rowHeight/2.0); float v = 1.0 - ((pos.g - 1.0) * rowHeight + rowHeight/2.0);
return texture2D(u_testTexture, vec2(u, v)).r; return texture2D(u_dataTexture, vec2(u, v)).r;
} }
bool hasOpacity() { // 判断 cell 中是否存在 opacity 的数据 bool hasOpacity() { // 判断 cell 中是否存在 opacity 的数据

View File

@ -39,6 +39,11 @@ function registerStyleAttribute(
layer.updateStyleAttribute(fieldName, field, values, updateOptions); layer.updateStyleAttribute(fieldName, field, values, updateOptions);
} }
/**
*
* @param configToUpdate
* @param layer
*/
function handleStyleDataMapping(configToUpdate: IConfigToUpdate, layer: any) { function handleStyleDataMapping(configToUpdate: IConfigToUpdate, layer: any) {
if (configToUpdate.opacity) { if (configToUpdate.opacity) {
// 处理 style 中 opacity 属性的数据映射 // 处理 style 中 opacity 属性的数据映射
@ -64,12 +69,13 @@ function handleStyleDataMapping(configToUpdate: IConfigToUpdate, layer: any) {
} }
if (configToUpdate.offsets) { if (configToUpdate.offsets) {
// 处理 style 中 offsets 属性的数据映射
handleStyleOffsets('offsets', layer, configToUpdate.offsets); handleStyleOffsets('offsets', layer, configToUpdate.offsets);
} }
} }
/** /**
* opacity * float
*/ */
function handleStyleFloat(fieldName: string, layer: ILayer, styleFloat: any) { function handleStyleFloat(fieldName: string, layer: ILayer, styleFloat: any) {
if (isString(styleFloat)) { if (isString(styleFloat)) {
@ -99,7 +105,12 @@ function handleStyleFloat(fieldName: string, layer: ILayer, styleFloat: any) {
registerStyleAttribute(fieldName, layer, [1.0], undefined); registerStyleAttribute(fieldName, layer, [1.0], undefined);
} }
} }
/**
* offsets
* @param fieldName
* @param layer
* @param styleOffsets
*/
function handleStyleOffsets( function handleStyleOffsets(
fieldName: string, fieldName: string,
layer: ILayer, layer: ILayer,
@ -157,129 +168,4 @@ function handleStyleColor(fieldName: string, layer: ILayer, styleColor: any) {
} }
} }
/** export { handleStyleDataMapping, handleStyleFloat, handleStyleColor };
* feature
* @param encodeDatalength
* @returns
*/
function getSize(encodeDatalength: number) {
const width = WIDTH;
const height = Math.ceil(encodeDatalength / width);
return { width, height };
}
/**
* index feature uv
* @param widthStep
* @param widthStart
* @param heightStep
* @param heightStart
* @param id
* @returns
*/
function getUvPosition(
widthStep: number,
widthStart: number,
heightStep: number,
heightStart: number,
index: number,
) {
// index 从零开始
const row = Math.ceil((index + 1) / WIDTH); // 当前 index 所在的行
let column = (index + 1) % WIDTH;
if (column === 0) {
// 取余等于零
column = WIDTH;
}
const u = widthStart + (column - 1) * widthStep;
const v = 1 - (heightStart + (row - 1) * heightStep);
return [u, v];
}
/**
* 1 field originData style
* 2 heightCount WIDTH (float)
* 3 createTexture2D
* 4
* @param heightCount
* @param createTexture2D
* @param originData
* @param field
* @returns
*/
function initTextureFloatData(
heightCount: number,
createTexture2D: any,
originData: any,
field: string,
): ITexture2D {
const d = [];
for (let i = 0; i < WIDTH * heightCount; i++) {
if (originData[i] && originData[i][field]) {
const v = originData[i][field];
d.push(v);
} else {
d.push(0);
}
}
const texture = createTexture2D({
flipY: true,
data: d,
format: gl.LUMINANCE,
type: gl.FLOAT,
width: WIDTH,
height: heightCount,
});
return texture;
}
/**
* 1 field originData style
* 2 heightCount WIDTH (color)
* 3 createTexture2D
* @param heightCount
* @param createTexture2D
* @param originData
* @param field
* @returns
*/
function initTextureVec4Data(
heightCount: number,
createTexture2D: any,
originData: any,
field: string,
): ITexture2D {
const d = [];
for (let i = 0; i < WIDTH * heightCount; i++) {
if (originData[i] && originData[i][field]) {
const [r, g, b, a] = rgb2arr(originData[i][field]);
d.push(r * 255, g * 255, b * 255, a * 255);
} else {
d.push(0, 0, 0, 0);
}
}
const arr = new Uint8ClampedArray(d);
const imageData = new ImageData(arr, WIDTH, heightCount); // (arr, width, height)
const texture = createTexture2D({
flipY: true,
data: new Uint8Array(imageData.data),
width: imageData.width,
height: imageData.height,
});
return texture;
}
export {
handleStyleDataMapping,
handleStyleFloat,
getSize,
getUvPosition,
initTextureFloatData,
initTextureVec4Data,
handleStyleColor,
};

View File

@ -90,9 +90,9 @@ export default class Amap2demo extends React.Component {
.size(10) .size(10)
.style({ .style({
opacity: 'customOpacity', opacity: 'customOpacity',
strokeOpacity: 'customStrokeOpacity', // strokeOpacity: 'customStrokeOpacity',
strokeWidth: 'customStrokeWidth', // strokeWidth: 'customStrokeWidth',
stroke: 'customStroke', // stroke: 'customStroke',
}); });
scene.addLayer(layer); scene.addLayer(layer);
}); });
@ -114,7 +114,7 @@ export default class Amap2demo extends React.Component {
// .style({ // .style({
// // stroke: '#000', // // stroke: '#000',
// // stroke: 'rgba(0, 255, 0, 1)', // // stroke: 'rgba(0, 255, 0, 1)',
// // stroke: 'strokeColor', // stroke: 'strokeColor',
// // stroke: ['strokeColor', (d: any) => { // // stroke: ['strokeColor', (d: any) => {
// // return d // // return d
// // }], // // }],
@ -123,9 +123,9 @@ export default class Amap2demo extends React.Component {
// // strokeWidth: 4, // // strokeWidth: 4,
// // strokeWidth: "strokeWidth", // // strokeWidth: "strokeWidth",
// // strokeWidth: ["strokeWidth", [1, 2]], // // strokeWidth: ["strokeWidth", [1, 2]],
// // strokeWidth: ["strokeWidth", (d: any) => { // strokeWidth: ["strokeWidth", (d: any) => {
// // return d * 2 // return d * 2
// // }], // }],
// // strokeOpacity: 0.5, // // strokeOpacity: 0.5,
// // strokeOpacity: 'strokeOpacity2', // // strokeOpacity: 'strokeOpacity2',
@ -140,10 +140,10 @@ export default class Amap2demo extends React.Component {
// // strokeOpacity: ['opacity2', [0.2, 0.6]], // // strokeOpacity: ['opacity2', [0.2, 0.6]],
// // offsets: [100, 100], // // offsets: [100, 100],
// // offsets: 'offsets2', // offsets: 'offsets2',
// // offsets: ['offsets2', (d: any) => d], // // offsets: ['offsets2', (d: any) => d],
// // opacity: 'opacity2', // opacity: 'opacity2',
// // opacity: 0.2 // // opacity: 0.2
// // opacity: 0, // // opacity: 0,
// // opacity: ['opacity2', (d: any) => { // // opacity: ['opacity2', (d: any) => {