-
Notifications
You must be signed in to change notification settings - Fork 633
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(layers): add polygon3d , pointimagelayer
- Loading branch information
Showing
21 changed files
with
886 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
export type IPosition = [number, number, number] | [number, number]; | ||
export type IPath = IPosition[]; | ||
export enum ShapeType3D { | ||
CYLINDER = 'cylinder', | ||
SQUARECOLUMN = 'squareColumn', | ||
TRIANGLECOLUMN = 'triangleColumn', | ||
HEXAGONCOLUMN = 'hexagonColumn', | ||
PENTAGONCOLUMN = 'pentagonColumn', | ||
} | ||
export enum ShapeType2D { | ||
CIRCLE = 'circle', | ||
SQUARE = 'square', | ||
TRIANGLE = 'triangle', | ||
HEXAGON = 'hexagon', | ||
PENTAGON = 'pentagon', | ||
} | ||
|
||
/** | ||
* 生成规则多边形顶点个数 | ||
* @param pointCount 顶点个数 3 => 三角形 | ||
* @param start 顶点起始角度 调整图形的方向 | ||
*/ | ||
export function polygonPath(pointCount: number, start: number = 0): IPath { | ||
const step = (Math.PI * 2) / pointCount; | ||
const line = []; | ||
for (let i = 0; i < pointCount; i++) { | ||
line.push(step * i + (start * Math.PI) / 12); | ||
} | ||
const path: IPath = line.map((t) => { | ||
const x = Math.sin(t + Math.PI / 4); | ||
const y = Math.cos(t + Math.PI / 4); | ||
return [x, y, 0]; | ||
}); | ||
// path.push(path[0]); | ||
return path; | ||
} | ||
|
||
export function circle(): IPath { | ||
return polygonPath(30); | ||
} | ||
export function square(): IPath { | ||
return polygonPath(4); | ||
} | ||
export function triangle(): IPath { | ||
return polygonPath(3); | ||
} | ||
export function hexagon(): IPath { | ||
return polygonPath(6); | ||
} | ||
export function pentagon(): IPath { | ||
return polygonPath(5); | ||
} | ||
|
||
export const geometryShape = { | ||
[ShapeType2D.CIRCLE]: circle, | ||
[ShapeType2D.HEXAGON]: hexagon, | ||
[ShapeType2D.TRIANGLE]: triangle, | ||
[ShapeType2D.SQUARE]: square, | ||
[ShapeType2D.PENTAGON]: pentagon, | ||
[ShapeType3D.CYLINDER]: circle, | ||
[ShapeType3D.HEXAGONCOLUMN]: hexagon, | ||
[ShapeType3D.TRIANGLECOLUMN]: triangle, | ||
[ShapeType3D.SQUARECOLUMN]: square, | ||
[ShapeType3D.PENTAGONCOLUMN]: pentagon, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import earcut from 'earcut'; | ||
import { IPath } from './Path'; | ||
export interface IExtrudeGeomety { | ||
positions: number[]; | ||
index: number[]; | ||
} | ||
/** | ||
* 拉伸多边形顶点,返回拉伸后的顶点信息 | ||
* @param paths 路径数据组 | ||
* @param extrude 是否拉伸 | ||
*/ | ||
export default function extrudePolygon(path: IPath[]): IExtrudeGeomety { | ||
const p1 = path[0][0]; | ||
const p2 = path[0][path[0].length - 1]; | ||
if (p1[0] === p2[0] && p1[1] === p2[1]) { | ||
path[0] = path[0].slice(0, path[0].length - 1); | ||
} | ||
|
||
const n = path[0].length; | ||
const flattengeo = earcut.flatten(path); | ||
const { vertices, dimensions } = flattengeo; | ||
const positions = []; | ||
const indexArray = []; | ||
// 设置顶部z值 | ||
for (let j = 0; j < vertices.length / dimensions; j++) { | ||
if (dimensions === 2) { | ||
positions.push(vertices[j * 2], vertices[j * 2 + 1], 1); | ||
} else { | ||
positions.push(vertices[j * 3], vertices[j * 3 + 1], 1); | ||
} | ||
} | ||
const triangles = earcut( | ||
flattengeo.vertices, | ||
flattengeo.holes, | ||
flattengeo.dimensions, | ||
); | ||
indexArray.push(...triangles); | ||
for (let i = 0; i < n; i++) { | ||
const prePoint = flattengeo.vertices.slice( | ||
i * dimensions, | ||
(i + 1) * dimensions, | ||
); | ||
let nextPoint = flattengeo.vertices.slice( | ||
(i + 2) * dimensions, | ||
(i + 3) * dimensions, | ||
); | ||
if (nextPoint.length === 0) { | ||
nextPoint = flattengeo.vertices.slice(0, dimensions); | ||
} | ||
const indexOffset = positions.length / 3; | ||
positions.push( | ||
prePoint[0], | ||
prePoint[1], | ||
1, | ||
nextPoint[0], | ||
nextPoint[1], | ||
1, | ||
prePoint[0], | ||
prePoint[1], | ||
0, | ||
nextPoint[0], | ||
nextPoint[1], | ||
0, | ||
); | ||
indexArray.push(...[1, 2, 0, 3, 2, 1].map((v) => v + indexOffset)); | ||
} | ||
return { | ||
positions, | ||
index: indexArray, | ||
}; | ||
} | ||
export function fillPolygon(points: IPath[]) { | ||
const flattengeo = earcut.flatten(points); | ||
const triangles = earcut( | ||
flattengeo.vertices, | ||
flattengeo.holes, | ||
flattengeo.dimensions, | ||
); | ||
return { | ||
positions: flattengeo.vertices, | ||
index: triangles, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.