three-base 2.0.108 → 2.0.109
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/FPScontrol.d.ts +2 -2
- package/FPScontrol.js +33 -30
- package/Fire/FireMaterial.js +1 -1
- package/InstancedMeshBase.d.ts +1 -1
- package/PipeMesh/PathGeometry.d.ts +44 -0
- package/PipeMesh/PathGeometry.js +300 -0
- package/PipeMesh/PathPoint.d.ts +17 -0
- package/PipeMesh/PathPoint.js +31 -0
- package/PipeMesh/PathPointList.d.ts +31 -0
- package/PipeMesh/PathPointList.js +208 -0
- package/PipeMesh/PathTubeGeometry.d.ts +25 -0
- package/PipeMesh/PathTubeGeometry.js +130 -0
- package/PipeMesh/PipeMesh.d.ts +23 -0
- package/PipeMesh/PipeMesh.js +61 -0
- package/PipeMesh/index.d.ts +1 -0
- package/PipeMesh/index.js +1 -0
- package/ThreeBase.js +2 -2
- package/keyContorls.d.ts +1 -1
- package/package.json +1 -1
package/FPScontrol.d.ts
CHANGED
|
@@ -16,7 +16,8 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
16
16
|
STEPS_PER_FRAME: number;
|
|
17
17
|
speed: number;
|
|
18
18
|
camera: Camera;
|
|
19
|
-
|
|
19
|
+
octrees: Array<Octree>;
|
|
20
|
+
addOctree(obj: Object3D): void;
|
|
20
21
|
playerCollider: Capsule;
|
|
21
22
|
clock: Clock;
|
|
22
23
|
playerVelocity: Vector3;
|
|
@@ -33,7 +34,6 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
33
34
|
mousedown: () => void;
|
|
34
35
|
mouseup: () => void;
|
|
35
36
|
mousemove: (event: MouseEvent) => void;
|
|
36
|
-
fromGraphNode(object3d: Object3D): void;
|
|
37
37
|
getForwardVector: () => Vector3;
|
|
38
38
|
getSideVector: () => Vector3;
|
|
39
39
|
controls: (deltaTime: number) => void;
|
package/FPScontrol.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @Author: xiaosihan
|
|
3
3
|
* @Date: 2022-05-04 23:01:36
|
|
4
4
|
* @Last Modified by: xiaosihan
|
|
5
|
-
* @Last Modified time:
|
|
5
|
+
* @Last Modified time: 2024-11-07 21:57:39
|
|
6
6
|
*/
|
|
7
7
|
import { Clock, EventDispatcher, Vector3 } from "three";
|
|
8
8
|
import { Capsule } from "three/examples/jsm/math/Capsule";
|
|
@@ -21,7 +21,7 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
21
21
|
super();
|
|
22
22
|
// 相机对象
|
|
23
23
|
this.camera = camera;
|
|
24
|
-
this.camera.rotation.order =
|
|
24
|
+
this.camera.rotation.order = "YXZ";
|
|
25
25
|
this.canvas = canvas;
|
|
26
26
|
this.init(canvas);
|
|
27
27
|
}
|
|
@@ -31,8 +31,13 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
31
31
|
STEPS_PER_FRAME = 5; // 每帧碰撞计算的次数
|
|
32
32
|
speed = 1; // 速度
|
|
33
33
|
camera;
|
|
34
|
-
//
|
|
35
|
-
|
|
34
|
+
// 八叉树数组
|
|
35
|
+
octrees = [];
|
|
36
|
+
addOctree(obj) {
|
|
37
|
+
const octree = new Octree();
|
|
38
|
+
octree.fromGraphNode(obj);
|
|
39
|
+
this.octrees.push(octree);
|
|
40
|
+
}
|
|
36
41
|
// 用户自己的碰撞体积
|
|
37
42
|
playerCollider = new Capsule(new Vector3(0, 0.35, 0), new Vector3(0, 1, 0), 0.35);
|
|
38
43
|
// 时钟对象
|
|
@@ -45,11 +50,11 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
45
50
|
keyStates = {};
|
|
46
51
|
init(canvas) {
|
|
47
52
|
this.playerCollider.end.set(0, 1, 0);
|
|
48
|
-
document.addEventListener(
|
|
49
|
-
document.addEventListener(
|
|
50
|
-
canvas.addEventListener(
|
|
51
|
-
document.addEventListener(
|
|
52
|
-
document.body.addEventListener(
|
|
53
|
+
document.addEventListener("keydown", this.keydown);
|
|
54
|
+
document.addEventListener("keyup", this.keyup);
|
|
55
|
+
canvas.addEventListener("pointerdown", this.mousedown);
|
|
56
|
+
document.addEventListener("pointerup", this.mouseup);
|
|
57
|
+
document.body.addEventListener("pointermove", this.mousemove);
|
|
53
58
|
this.enable = true;
|
|
54
59
|
}
|
|
55
60
|
keydown = (event) => {
|
|
@@ -71,10 +76,6 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
71
76
|
this.camera.rotation.x -= event.movementY / 500;
|
|
72
77
|
}
|
|
73
78
|
};
|
|
74
|
-
// 设置 八叉树 节点
|
|
75
|
-
fromGraphNode(object3d) {
|
|
76
|
-
this.worldOctree.fromGraphNode(object3d);
|
|
77
|
-
}
|
|
78
79
|
// 获取前进的向量
|
|
79
80
|
getForwardVector = () => {
|
|
80
81
|
this.camera.getWorldDirection(this.playerDirection);
|
|
@@ -93,20 +94,20 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
93
94
|
// 玩家移动控制器
|
|
94
95
|
controls = (deltaTime) => {
|
|
95
96
|
const speedDelta = deltaTime * (this.playerOnFloor ? 25 : 8) * this.speed;
|
|
96
|
-
if (this.keyStates[
|
|
97
|
+
if (this.keyStates["KeyW"]) {
|
|
97
98
|
this.playerVelocity.add(this.getForwardVector().multiplyScalar(speedDelta));
|
|
98
99
|
}
|
|
99
|
-
if (this.keyStates[
|
|
100
|
+
if (this.keyStates["KeyS"]) {
|
|
100
101
|
this.playerVelocity.add(this.getForwardVector().multiplyScalar(-speedDelta));
|
|
101
102
|
}
|
|
102
|
-
if (this.keyStates[
|
|
103
|
+
if (this.keyStates["KeyA"]) {
|
|
103
104
|
this.playerVelocity.add(this.getSideVector().multiplyScalar(-speedDelta));
|
|
104
105
|
}
|
|
105
|
-
if (this.keyStates[
|
|
106
|
+
if (this.keyStates["KeyD"]) {
|
|
106
107
|
this.playerVelocity.add(this.getSideVector().multiplyScalar(speedDelta));
|
|
107
108
|
}
|
|
108
109
|
if (this.playerOnFloor) {
|
|
109
|
-
if (this.keyStates[
|
|
110
|
+
if (this.keyStates["Space"]) {
|
|
110
111
|
this.playerVelocity.y = 15;
|
|
111
112
|
}
|
|
112
113
|
}
|
|
@@ -128,14 +129,16 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
128
129
|
};
|
|
129
130
|
// 玩家的碰撞检测
|
|
130
131
|
playerCollisions() {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
132
|
+
for (let octree of this.octrees) {
|
|
133
|
+
const result = octree.capsuleIntersect(this.playerCollider);
|
|
134
|
+
this.playerOnFloor = false;
|
|
135
|
+
if (result) {
|
|
136
|
+
this.playerOnFloor = result.normal.y > 0;
|
|
137
|
+
if (!this.playerOnFloor) {
|
|
138
|
+
this.playerVelocity.addScaledVector(result.normal, -result.normal.dot(this.playerVelocity));
|
|
139
|
+
}
|
|
140
|
+
this.playerCollider.translate(result.normal.multiplyScalar(result.depth));
|
|
137
141
|
}
|
|
138
|
-
this.playerCollider.translate(result.normal.multiplyScalar(result.depth));
|
|
139
142
|
}
|
|
140
143
|
}
|
|
141
144
|
// 更新控制器的位置
|
|
@@ -162,10 +165,10 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
162
165
|
// 销毁事件
|
|
163
166
|
disposer() {
|
|
164
167
|
this.enable = false;
|
|
165
|
-
document.removeEventListener(
|
|
166
|
-
document.removeEventListener(
|
|
167
|
-
this.canvas.removeEventListener(
|
|
168
|
-
document.removeEventListener(
|
|
169
|
-
document.body.removeEventListener(
|
|
168
|
+
document.removeEventListener("keydown", this.keydown);
|
|
169
|
+
document.removeEventListener("keyup", this.keyup);
|
|
170
|
+
this.canvas.removeEventListener("pointerdown", this.mousedown);
|
|
171
|
+
document.removeEventListener("pointerup", this.mouseup);
|
|
172
|
+
document.body.removeEventListener("pointermove", this.mousemove);
|
|
170
173
|
}
|
|
171
174
|
}
|
package/Fire/FireMaterial.js
CHANGED
package/InstancedMeshBase.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ export default class InstancedMeshBase extends MeshBase {
|
|
|
18
18
|
instanceColor: InstancedBufferAttribute;
|
|
19
19
|
reset(...object: Object3D[]): this;
|
|
20
20
|
loadModel(url: string | File | Array<File>, filename?: string): Promise<void>;
|
|
21
|
-
computeBoundingSphere: import("lodash").
|
|
21
|
+
computeBoundingSphere: import("lodash").DebouncedFuncLeading<() => void>;
|
|
22
22
|
setCount(count?: number): Promise<void>;
|
|
23
23
|
setMatrixAt(index: number, matrix?: Matrix4): Promise<void>;
|
|
24
24
|
setTRS(index: number, { position, rotation, scale, }: {
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Usage } from 'three/src/constants.js';
|
|
2
|
+
import type { PathPointList } from './PathPointList.js';
|
|
3
|
+
import { BufferGeometry } from 'three/src/core/BufferGeometry.js';
|
|
4
|
+
export interface Options {
|
|
5
|
+
radius?: number;
|
|
6
|
+
width?: number;
|
|
7
|
+
progress?: number;
|
|
8
|
+
radialSegments?: number;
|
|
9
|
+
startRad?: number;
|
|
10
|
+
arrow?: boolean;
|
|
11
|
+
side?: 'both' | 'left' | 'right';
|
|
12
|
+
}
|
|
13
|
+
export interface PathGeometryParam {
|
|
14
|
+
pathPointList: PathPointList;
|
|
15
|
+
options: Options;
|
|
16
|
+
usage: Usage;
|
|
17
|
+
transparent: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* PathGeometry
|
|
21
|
+
*/
|
|
22
|
+
declare class PathGeometry extends BufferGeometry {
|
|
23
|
+
/**
|
|
24
|
+
* @param {Object|Number} initData - If initData is number, geometry init by empty data and set it as the max vertex. If initData is Object, it contains pathPointList and options.
|
|
25
|
+
* @param {Boolean} [generateUv2=false]
|
|
26
|
+
*/
|
|
27
|
+
constructor(initData: PathGeometryParam, generateUv2?: boolean);
|
|
28
|
+
_initByMaxVertex(maxVertex: number, generateUv2: boolean): void;
|
|
29
|
+
_initByData(pathPointList: PathPointList, options: Options, usage: Usage, generateUv2: boolean): void;
|
|
30
|
+
/**
|
|
31
|
+
* Update geometry by PathPointList instance
|
|
32
|
+
* @param {PathPointList} pathPointList
|
|
33
|
+
* @param {Object} options
|
|
34
|
+
* @param {Number} [options.width=0.1]
|
|
35
|
+
* @param {Number} [options.progress=1]
|
|
36
|
+
* @param {Boolean} [options.arrow=true]
|
|
37
|
+
* @param {String} [options.side='both'] - "left"/"right"/"both"
|
|
38
|
+
*/
|
|
39
|
+
update(pathPointList: PathPointList, options: Options): void;
|
|
40
|
+
_resizeAttribute(name: string, len: number): void;
|
|
41
|
+
_resizeIndex(len: number): void;
|
|
42
|
+
_updateAttributes(position: Array<number>, normal: Array<number>, uv: Array<number>, uv2: Array<number> | null, indices: Array<number>): void;
|
|
43
|
+
}
|
|
44
|
+
export { PathGeometry };
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import { DynamicDrawUsage, StaticDrawUsage } from 'three/src/constants.js';
|
|
2
|
+
import { PathPoint } from './PathPoint.js';
|
|
3
|
+
import { BufferGeometry } from 'three/src/core/BufferGeometry.js';
|
|
4
|
+
import { BufferAttribute } from 'three/src/core/BufferAttribute.js';
|
|
5
|
+
import { Vector3 } from 'three/src/math/Vector3.js';
|
|
6
|
+
/**
|
|
7
|
+
* PathGeometry
|
|
8
|
+
*/
|
|
9
|
+
class PathGeometry extends BufferGeometry {
|
|
10
|
+
/**
|
|
11
|
+
* @param {Object|Number} initData - If initData is number, geometry init by empty data and set it as the max vertex. If initData is Object, it contains pathPointList and options.
|
|
12
|
+
* @param {Boolean} [generateUv2=false]
|
|
13
|
+
*/
|
|
14
|
+
constructor(initData, generateUv2 = false) {
|
|
15
|
+
super();
|
|
16
|
+
this._initByData(initData.pathPointList, initData.options, initData.usage, generateUv2);
|
|
17
|
+
}
|
|
18
|
+
_initByMaxVertex(maxVertex, generateUv2) {
|
|
19
|
+
this.setAttribute('position', new BufferAttribute(new Float32Array(maxVertex * 3), 3).setUsage(DynamicDrawUsage));
|
|
20
|
+
this.setAttribute('normal', new BufferAttribute(new Float32Array(maxVertex * 3), 3).setUsage(DynamicDrawUsage));
|
|
21
|
+
this.setAttribute('uv', new BufferAttribute(new Float32Array(maxVertex * 2), 2).setUsage(DynamicDrawUsage));
|
|
22
|
+
if (generateUv2) {
|
|
23
|
+
this.setAttribute('uv2', new BufferAttribute(new Float32Array(maxVertex * 2), 2).setUsage(DynamicDrawUsage));
|
|
24
|
+
}
|
|
25
|
+
this.drawRange.start = 0;
|
|
26
|
+
this.drawRange.count = 0;
|
|
27
|
+
this.setIndex(new Array(maxVertex * 3));
|
|
28
|
+
}
|
|
29
|
+
_initByData(pathPointList, options, usage, generateUv2) {
|
|
30
|
+
const vertexData = generatePathVertexData(pathPointList, options, generateUv2);
|
|
31
|
+
if (vertexData && vertexData.count !== 0) {
|
|
32
|
+
this.setAttribute('position', new BufferAttribute(new Float32Array(vertexData.position), 3).setUsage(usage || StaticDrawUsage));
|
|
33
|
+
this.setAttribute('normal', new BufferAttribute(new Float32Array(vertexData.normal), 3).setUsage(usage || StaticDrawUsage));
|
|
34
|
+
this.setAttribute('uv', new BufferAttribute(new Float32Array(vertexData.uv), 2).setUsage(usage || StaticDrawUsage));
|
|
35
|
+
if (generateUv2) {
|
|
36
|
+
this.setAttribute('uv2', new BufferAttribute(new Float32Array(vertexData.uv2), 2).setUsage(usage || StaticDrawUsage));
|
|
37
|
+
}
|
|
38
|
+
this.setIndex(vertexData.indices);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
this._initByMaxVertex(2, generateUv2);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Update geometry by PathPointList instance
|
|
46
|
+
* @param {PathPointList} pathPointList
|
|
47
|
+
* @param {Object} options
|
|
48
|
+
* @param {Number} [options.width=0.1]
|
|
49
|
+
* @param {Number} [options.progress=1]
|
|
50
|
+
* @param {Boolean} [options.arrow=true]
|
|
51
|
+
* @param {String} [options.side='both'] - "left"/"right"/"both"
|
|
52
|
+
*/
|
|
53
|
+
update(pathPointList, options) {
|
|
54
|
+
const generateUv2 = !!this.getAttribute('uv2');
|
|
55
|
+
const vertexData = generatePathVertexData(pathPointList, options, generateUv2);
|
|
56
|
+
if (vertexData) {
|
|
57
|
+
this._updateAttributes(vertexData.position, vertexData.normal, vertexData.uv, generateUv2 ? vertexData.uv2 : null, vertexData.indices);
|
|
58
|
+
this.drawRange.count = vertexData.count;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
this.drawRange.count = 0;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
_resizeAttribute(name, len) {
|
|
65
|
+
let attribute = this.getAttribute(name);
|
|
66
|
+
while (attribute.array.length < len) {
|
|
67
|
+
const oldLength = attribute.array.length;
|
|
68
|
+
const newAttribute = new BufferAttribute(new Float32Array(oldLength * 2), attribute.itemSize, attribute.normalized);
|
|
69
|
+
newAttribute.name = attribute.name;
|
|
70
|
+
newAttribute.usage = attribute.usage;
|
|
71
|
+
this.setAttribute(name, newAttribute);
|
|
72
|
+
attribute = newAttribute;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
_resizeIndex(len) {
|
|
76
|
+
let index = this.getIndex();
|
|
77
|
+
while (index.array.length < len) {
|
|
78
|
+
const oldLength = index.array.length;
|
|
79
|
+
const newIndex = new BufferAttribute(oldLength * 2 > 65535 ? new Uint32Array(oldLength * 2) : new Uint16Array(oldLength * 2), 1);
|
|
80
|
+
newIndex.name = index.name;
|
|
81
|
+
newIndex.usage = index.usage;
|
|
82
|
+
this.setIndex(newIndex);
|
|
83
|
+
index = newIndex;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
_updateAttributes(position, normal, uv, uv2, indices) {
|
|
87
|
+
this._resizeAttribute('position', position.length);
|
|
88
|
+
const positionAttribute = this.getAttribute('position');
|
|
89
|
+
positionAttribute.array.set(position, 0);
|
|
90
|
+
positionAttribute.updateRange.count = position.length;
|
|
91
|
+
positionAttribute.needsUpdate = true;
|
|
92
|
+
this._resizeAttribute('normal', normal.length);
|
|
93
|
+
const normalAttribute = this.getAttribute('normal');
|
|
94
|
+
normalAttribute.array.set(normal, 0);
|
|
95
|
+
normalAttribute.updateRange.count = normal.length;
|
|
96
|
+
normalAttribute.needsUpdate = true;
|
|
97
|
+
this._resizeAttribute('uv', uv.length);
|
|
98
|
+
const uvAttribute = this.getAttribute('uv');
|
|
99
|
+
uvAttribute.array.set(uv, 0);
|
|
100
|
+
uvAttribute.updateRange.count = uv.length;
|
|
101
|
+
uvAttribute.needsUpdate = true;
|
|
102
|
+
if (uv2) {
|
|
103
|
+
this._resizeAttribute('uv2', uv2.length);
|
|
104
|
+
const uv2Attribute = this.getAttribute('uv2');
|
|
105
|
+
uv2Attribute.array.set(uv2, 0);
|
|
106
|
+
uv2Attribute.updateRange.count = uv2.length;
|
|
107
|
+
uv2Attribute.needsUpdate = true;
|
|
108
|
+
}
|
|
109
|
+
this._resizeIndex(indices.length);
|
|
110
|
+
const indexAttribute = this.getIndex();
|
|
111
|
+
indexAttribute.set(indices, 0);
|
|
112
|
+
indexAttribute.updateRange.count = indices.length;
|
|
113
|
+
indexAttribute.needsUpdate = true;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
export { PathGeometry };
|
|
117
|
+
// Vertex Data Generate Functions
|
|
118
|
+
function generatePathVertexData(pathPointList, options, generateUv2 = false) {
|
|
119
|
+
const width = options.width || 0.1;
|
|
120
|
+
const progress = options.progress !== undefined ? options.progress : 1;
|
|
121
|
+
const arrow = options.arrow !== undefined ? options.arrow : true;
|
|
122
|
+
const side = options.side !== undefined ? options.side : 'both';
|
|
123
|
+
const halfWidth = width / 2;
|
|
124
|
+
const sideWidth = side !== 'both' ? width / 2 : width;
|
|
125
|
+
const totalDistance = pathPointList.distance();
|
|
126
|
+
const progressDistance = progress * totalDistance;
|
|
127
|
+
if (totalDistance == 0) {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
const sharpUvOffset = halfWidth / sideWidth;
|
|
131
|
+
const sharpUvOffset2 = halfWidth / totalDistance;
|
|
132
|
+
let count = 0;
|
|
133
|
+
// modify data
|
|
134
|
+
const position = [];
|
|
135
|
+
const normal = [];
|
|
136
|
+
const uv = [];
|
|
137
|
+
const uv2 = [];
|
|
138
|
+
const indices = [];
|
|
139
|
+
let verticesCount = 0;
|
|
140
|
+
const right = new Vector3();
|
|
141
|
+
const left = new Vector3();
|
|
142
|
+
// for sharp corners
|
|
143
|
+
const leftOffset = new Vector3();
|
|
144
|
+
const rightOffset = new Vector3();
|
|
145
|
+
const tempPoint1 = new Vector3();
|
|
146
|
+
const tempPoint2 = new Vector3();
|
|
147
|
+
function addVertices(pathPoint) {
|
|
148
|
+
const first = position.length === 0;
|
|
149
|
+
const sharpCorner = pathPoint.sharp && !first;
|
|
150
|
+
const uvDist = pathPoint.dist / sideWidth;
|
|
151
|
+
const uvDist2 = pathPoint.dist / totalDistance;
|
|
152
|
+
const dir = pathPoint.dir;
|
|
153
|
+
const up = pathPoint.up;
|
|
154
|
+
const _right = pathPoint.right;
|
|
155
|
+
if (side !== 'left') {
|
|
156
|
+
right.copy(_right).multiplyScalar(halfWidth * pathPoint.widthScale);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
right.set(0, 0, 0);
|
|
160
|
+
}
|
|
161
|
+
if (side !== 'right') {
|
|
162
|
+
left.copy(_right).multiplyScalar(-halfWidth * pathPoint.widthScale);
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
left.set(0, 0, 0);
|
|
166
|
+
}
|
|
167
|
+
right.add(pathPoint.pos);
|
|
168
|
+
left.add(pathPoint.pos);
|
|
169
|
+
if (sharpCorner) {
|
|
170
|
+
leftOffset.fromArray(position, position.length - 6).sub(left);
|
|
171
|
+
rightOffset.fromArray(position, position.length - 3).sub(right);
|
|
172
|
+
const leftDist = leftOffset.length();
|
|
173
|
+
const rightDist = rightOffset.length();
|
|
174
|
+
const sideOffset = leftDist - rightDist;
|
|
175
|
+
let longerOffset, longEdge;
|
|
176
|
+
if (sideOffset > 0) {
|
|
177
|
+
longerOffset = leftOffset;
|
|
178
|
+
longEdge = left;
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
longerOffset = rightOffset;
|
|
182
|
+
longEdge = right;
|
|
183
|
+
}
|
|
184
|
+
tempPoint1.copy(longerOffset).setLength(Math.abs(sideOffset)).add(longEdge);
|
|
185
|
+
let _cos = tempPoint2.copy(longEdge).sub(tempPoint1).normalize().dot(dir);
|
|
186
|
+
let _len = tempPoint2.copy(longEdge).sub(tempPoint1).length();
|
|
187
|
+
let _dist = _cos * _len * 2;
|
|
188
|
+
tempPoint2.copy(dir).setLength(_dist).add(tempPoint1);
|
|
189
|
+
if (sideOffset > 0) {
|
|
190
|
+
position.push(tempPoint1.x, tempPoint1.y, tempPoint1.z, // 6
|
|
191
|
+
right.x, right.y, right.z, // 5
|
|
192
|
+
left.x, left.y, left.z, // 4
|
|
193
|
+
right.x, right.y, right.z, // 3
|
|
194
|
+
tempPoint2.x, tempPoint2.y, tempPoint2.z, // 2
|
|
195
|
+
right.x, right.y, right.z // 1
|
|
196
|
+
);
|
|
197
|
+
verticesCount += 6;
|
|
198
|
+
indices.push(verticesCount - 6, verticesCount - 8, verticesCount - 7, verticesCount - 6, verticesCount - 7, verticesCount - 5, verticesCount - 4, verticesCount - 6, verticesCount - 5, verticesCount - 2, verticesCount - 4, verticesCount - 1);
|
|
199
|
+
count += 12;
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
position.push(left.x, left.y, left.z, // 6
|
|
203
|
+
tempPoint1.x, tempPoint1.y, tempPoint1.z, // 5
|
|
204
|
+
left.x, left.y, left.z, // 4
|
|
205
|
+
right.x, right.y, right.z, // 3
|
|
206
|
+
left.x, left.y, left.z, // 2
|
|
207
|
+
tempPoint2.x, tempPoint2.y, tempPoint2.z // 1
|
|
208
|
+
);
|
|
209
|
+
verticesCount += 6;
|
|
210
|
+
indices.push(verticesCount - 6, verticesCount - 8, verticesCount - 7, verticesCount - 6, verticesCount - 7, verticesCount - 5, verticesCount - 6, verticesCount - 5, verticesCount - 3, verticesCount - 2, verticesCount - 3, verticesCount - 1);
|
|
211
|
+
count += 12;
|
|
212
|
+
}
|
|
213
|
+
normal.push(up.x, up.y, up.z, up.x, up.y, up.z, up.x, up.y, up.z, up.x, up.y, up.z, up.x, up.y, up.z, up.x, up.y, up.z);
|
|
214
|
+
uv.push(uvDist - sharpUvOffset, 0, uvDist - sharpUvOffset, 1, uvDist, 0, uvDist, 1, uvDist + sharpUvOffset, 0, uvDist + sharpUvOffset, 1);
|
|
215
|
+
if (generateUv2) {
|
|
216
|
+
uv2.push(uvDist2 - sharpUvOffset2, 0, uvDist2 - sharpUvOffset2, 1, uvDist2, 0, uvDist2, 1, uvDist2 + sharpUvOffset2, 0, uvDist2 + sharpUvOffset2, 1);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
position.push(left.x, left.y, left.z, right.x, right.y, right.z);
|
|
221
|
+
normal.push(up.x, up.y, up.z, up.x, up.y, up.z);
|
|
222
|
+
uv.push(uvDist, 0, uvDist, 1);
|
|
223
|
+
if (generateUv2) {
|
|
224
|
+
uv2.push(uvDist2, 0, uvDist2, 1);
|
|
225
|
+
}
|
|
226
|
+
verticesCount += 2;
|
|
227
|
+
if (!first) {
|
|
228
|
+
indices.push(verticesCount - 2, verticesCount - 4, verticesCount - 3, verticesCount - 2, verticesCount - 3, verticesCount - 1);
|
|
229
|
+
count += 6;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
const sharp = new Vector3();
|
|
234
|
+
function addStart(pathPoint) {
|
|
235
|
+
const dir = pathPoint.dir;
|
|
236
|
+
const up = pathPoint.up;
|
|
237
|
+
const _right = pathPoint.right;
|
|
238
|
+
const uvDist = pathPoint.dist / sideWidth;
|
|
239
|
+
const uvDist2 = pathPoint.dist / totalDistance;
|
|
240
|
+
if (side !== 'left') {
|
|
241
|
+
right.copy(_right).multiplyScalar(halfWidth * 2);
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
right.set(0, 0, 0);
|
|
245
|
+
}
|
|
246
|
+
if (side !== 'right') {
|
|
247
|
+
left.copy(_right).multiplyScalar(-halfWidth * 2);
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
left.set(0, 0, 0);
|
|
251
|
+
}
|
|
252
|
+
sharp.copy(dir).setLength(halfWidth * 3);
|
|
253
|
+
right.add(pathPoint.pos);
|
|
254
|
+
left.add(pathPoint.pos);
|
|
255
|
+
sharp.add(pathPoint.pos);
|
|
256
|
+
position.push(left.x, left.y, left.z, right.x, right.y, right.z, sharp.x, sharp.y, sharp.z);
|
|
257
|
+
normal.push(up.x, up.y, up.z, up.x, up.y, up.z, up.x, up.y, up.z);
|
|
258
|
+
uv.push(uvDist, side !== 'both' ? (side !== 'right' ? -2 : 0) : -0.5, uvDist, side !== 'both' ? (side !== 'left' ? 2 : 0) : 1.5, uvDist + 1.5, side !== 'both' ? 0 : 0.5);
|
|
259
|
+
if (generateUv2) {
|
|
260
|
+
uv2.push(uvDist2, side !== 'both' ? (side !== 'right' ? -2 : 0) : -0.5, uvDist2, side !== 'both' ? (side !== 'left' ? 2 : 0) : 1.5, uvDist2 + (1.5 * width) / totalDistance, side !== 'both' ? 0 : 0.5);
|
|
261
|
+
}
|
|
262
|
+
verticesCount += 3;
|
|
263
|
+
indices.push(verticesCount - 1, verticesCount - 3, verticesCount - 2);
|
|
264
|
+
count += 3;
|
|
265
|
+
}
|
|
266
|
+
let lastPoint;
|
|
267
|
+
if (progressDistance > 0) {
|
|
268
|
+
for (let i = 0; i < pathPointList.count; i++) {
|
|
269
|
+
const pathPoint = pathPointList.array[i];
|
|
270
|
+
if (pathPoint.dist > progressDistance) {
|
|
271
|
+
const prevPoint = pathPointList.array[i - 1];
|
|
272
|
+
lastPoint = new PathPoint();
|
|
273
|
+
// linear lerp for progress
|
|
274
|
+
const alpha = (progressDistance - prevPoint.dist) / (pathPoint.dist - prevPoint.dist);
|
|
275
|
+
lastPoint.lerpPathPoints(prevPoint, pathPoint, alpha);
|
|
276
|
+
addVertices(lastPoint);
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
addVertices(pathPoint);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
lastPoint = pathPointList.array[0];
|
|
286
|
+
}
|
|
287
|
+
// build arrow geometry
|
|
288
|
+
if (arrow) {
|
|
289
|
+
lastPoint = lastPoint || pathPointList.array[pathPointList.count - 1];
|
|
290
|
+
addStart(lastPoint);
|
|
291
|
+
}
|
|
292
|
+
return {
|
|
293
|
+
position,
|
|
294
|
+
normal,
|
|
295
|
+
uv,
|
|
296
|
+
uv2,
|
|
297
|
+
indices,
|
|
298
|
+
count,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Vector3 } from 'three/src/math/Vector3';
|
|
2
|
+
/**
|
|
3
|
+
* PathPoint
|
|
4
|
+
*/
|
|
5
|
+
declare class PathPoint {
|
|
6
|
+
pos: Vector3;
|
|
7
|
+
dir: Vector3;
|
|
8
|
+
right: Vector3;
|
|
9
|
+
up: Vector3;
|
|
10
|
+
dist: number;
|
|
11
|
+
widthScale: number;
|
|
12
|
+
sharp: boolean;
|
|
13
|
+
constructor();
|
|
14
|
+
lerpPathPoints(p1: PathPoint, p2: PathPoint, alpha: number): void;
|
|
15
|
+
copy(source: PathPoint): void;
|
|
16
|
+
}
|
|
17
|
+
export { PathPoint };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Vector3 } from 'three/src/math/Vector3';
|
|
2
|
+
/**
|
|
3
|
+
* PathPoint
|
|
4
|
+
*/
|
|
5
|
+
class PathPoint {
|
|
6
|
+
pos = new Vector3();
|
|
7
|
+
dir = new Vector3();
|
|
8
|
+
right = new Vector3();
|
|
9
|
+
up = new Vector3(); // normal
|
|
10
|
+
dist = 0; // distance from start
|
|
11
|
+
widthScale = 1; // for corner
|
|
12
|
+
sharp = false; // marks as sharp corner
|
|
13
|
+
constructor() { }
|
|
14
|
+
lerpPathPoints(p1, p2, alpha) {
|
|
15
|
+
this.pos.lerpVectors(p1.pos, p2.pos, alpha);
|
|
16
|
+
this.dir.lerpVectors(p1.dir, p2.dir, alpha);
|
|
17
|
+
this.up.lerpVectors(p1.up, p2.up, alpha);
|
|
18
|
+
this.right.lerpVectors(p1.right, p2.right, alpha);
|
|
19
|
+
this.dist = (p2.dist - p1.dist) * alpha + p1.dist;
|
|
20
|
+
this.widthScale = (p2.widthScale - p1.widthScale) * alpha + p1.widthScale;
|
|
21
|
+
}
|
|
22
|
+
copy(source) {
|
|
23
|
+
this.pos.copy(source.pos);
|
|
24
|
+
this.dir.copy(source.dir);
|
|
25
|
+
this.up.copy(source.up);
|
|
26
|
+
this.right.copy(source.right);
|
|
27
|
+
this.dist = source.dist;
|
|
28
|
+
this.widthScale = source.widthScale;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export { PathPoint };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Vector3 } from 'three/src/math/Vector3.js';
|
|
2
|
+
import { PathPoint } from './PathPoint.js';
|
|
3
|
+
/**
|
|
4
|
+
* PathPointList
|
|
5
|
+
* input points to generate a PathPoint list
|
|
6
|
+
*/
|
|
7
|
+
declare class PathPointList {
|
|
8
|
+
array: Array<PathPoint>;
|
|
9
|
+
count: number;
|
|
10
|
+
constructor();
|
|
11
|
+
/**
|
|
12
|
+
* Set points
|
|
13
|
+
* @param {Vector3[]} points key points array
|
|
14
|
+
* @param {number} cornerRadius? the corner radius. set 0 to disable round corner. default is 0.1
|
|
15
|
+
* @param {number} cornerSplit? the corner split. default is 10.
|
|
16
|
+
* @param {number} up? force up. default is auto up (calculate by tangent).
|
|
17
|
+
* @param {boolean} close? close path. default is false.
|
|
18
|
+
*/
|
|
19
|
+
set(points: Array<Vector3>, cornerRadius?: number, cornerSplit?: number, up?: Vector3, close?: boolean): void;
|
|
20
|
+
/**
|
|
21
|
+
* Get distance of this path
|
|
22
|
+
* @return {number}
|
|
23
|
+
*/
|
|
24
|
+
distance(): number;
|
|
25
|
+
_getByIndex(index: number): PathPoint;
|
|
26
|
+
_start(current: Vector3, next: Vector3, up?: Vector3): void;
|
|
27
|
+
_end(current: Vector3): void;
|
|
28
|
+
_corner(current: Vector3, next: Vector3, cornerRadius: number, cornerSplit: number, up?: Vector3): void;
|
|
29
|
+
_sharpCorner(current: Vector3, next: Vector3, up?: Vector3, dirType?: 0 | 1 | 2, sharp?: boolean): void;
|
|
30
|
+
}
|
|
31
|
+
export { PathPointList };
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { Vector3 } from 'three/src/math/Vector3.js';
|
|
2
|
+
import { PathPoint } from './PathPoint.js';
|
|
3
|
+
import { Matrix4 } from 'three/src/math/Matrix4.js';
|
|
4
|
+
import { QuadraticBezierCurve3 } from 'three/src/extras/curves/QuadraticBezierCurve3.js';
|
|
5
|
+
const helpVec3_1 = new Vector3();
|
|
6
|
+
const helpVec3_2 = new Vector3();
|
|
7
|
+
const helpVec3_3 = new Vector3();
|
|
8
|
+
const helpMat4 = new Matrix4();
|
|
9
|
+
const helpCurve = new QuadraticBezierCurve3(new Vector3(), new Vector3(), new Vector3());
|
|
10
|
+
function _getCornerBezierCurve(last, current, next, cornerRadius, firstCorner, out) {
|
|
11
|
+
const lastDir = helpVec3_1.subVectors(current, last);
|
|
12
|
+
const nextDir = helpVec3_2.subVectors(next, current);
|
|
13
|
+
const lastDirLength = lastDir.length();
|
|
14
|
+
const nextDirLength = nextDir.length();
|
|
15
|
+
lastDir.normalize();
|
|
16
|
+
nextDir.normalize();
|
|
17
|
+
// cornerRadius can not bigger then lineDistance / 2, auto fix this
|
|
18
|
+
const v0Dist = Math.min((firstCorner ? lastDirLength / 2 : lastDirLength) * 0.999999, cornerRadius);
|
|
19
|
+
out.v0.copy(current).sub(lastDir.multiplyScalar(v0Dist));
|
|
20
|
+
out.v1.copy(current);
|
|
21
|
+
const v2Dist = Math.min((nextDirLength / 2) * 0.999999, cornerRadius);
|
|
22
|
+
out.v2.copy(current).add(nextDir.multiplyScalar(v2Dist));
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* PathPointList
|
|
27
|
+
* input points to generate a PathPoint list
|
|
28
|
+
*/
|
|
29
|
+
class PathPointList {
|
|
30
|
+
array = []; // path point array
|
|
31
|
+
count = 0;
|
|
32
|
+
constructor() { }
|
|
33
|
+
/**
|
|
34
|
+
* Set points
|
|
35
|
+
* @param {Vector3[]} points key points array
|
|
36
|
+
* @param {number} cornerRadius? the corner radius. set 0 to disable round corner. default is 0.1
|
|
37
|
+
* @param {number} cornerSplit? the corner split. default is 10.
|
|
38
|
+
* @param {number} up? force up. default is auto up (calculate by tangent).
|
|
39
|
+
* @param {boolean} close? close path. default is false.
|
|
40
|
+
*/
|
|
41
|
+
set(points, cornerRadius = 0.1, cornerSplit = 10, up, close = false) {
|
|
42
|
+
points = points.slice(0);
|
|
43
|
+
if (points.length < 2) {
|
|
44
|
+
console.warn('PathPointList: points length less than 2.');
|
|
45
|
+
this.count = 0;
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
// Auto close
|
|
49
|
+
if (close && !points[0].equals(points[points.length - 1])) {
|
|
50
|
+
points.push(new Vector3().copy(points[0]));
|
|
51
|
+
}
|
|
52
|
+
// Generate path point list
|
|
53
|
+
for (let i = 0, l = points.length; i < l; i++) {
|
|
54
|
+
if (i === 0) {
|
|
55
|
+
this._start(points[i], points[i + 1], up);
|
|
56
|
+
}
|
|
57
|
+
else if (i === l - 1) {
|
|
58
|
+
if (close) {
|
|
59
|
+
// Connect end point and start point
|
|
60
|
+
this._corner(points[i], points[1], cornerRadius, cornerSplit, up);
|
|
61
|
+
// Fix start point
|
|
62
|
+
const dist = this.array[0].dist; // should not copy dist
|
|
63
|
+
this.array[0].copy(this.array[this.count - 1]);
|
|
64
|
+
this.array[0].dist = dist;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
this._end(points[i]);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
this._corner(points[i], points[i + 1], cornerRadius, cornerSplit, up);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Get distance of this path
|
|
77
|
+
* @return {number}
|
|
78
|
+
*/
|
|
79
|
+
distance() {
|
|
80
|
+
if (this.count > 0) {
|
|
81
|
+
return this.array[this.count - 1].dist;
|
|
82
|
+
}
|
|
83
|
+
return 0;
|
|
84
|
+
}
|
|
85
|
+
_getByIndex(index) {
|
|
86
|
+
if (!this.array[index]) {
|
|
87
|
+
this.array[index] = new PathPoint();
|
|
88
|
+
}
|
|
89
|
+
return this.array[index];
|
|
90
|
+
}
|
|
91
|
+
_start(current, next, up) {
|
|
92
|
+
this.count = 0;
|
|
93
|
+
const point = this._getByIndex(this.count);
|
|
94
|
+
point.pos.copy(current);
|
|
95
|
+
point.dir.subVectors(next, current);
|
|
96
|
+
// init start up dir
|
|
97
|
+
if (up) {
|
|
98
|
+
point.up.copy(up);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
// select an initial normal vector perpendicular to the first tangent vector
|
|
102
|
+
let min = Number.MAX_VALUE;
|
|
103
|
+
const tx = Math.abs(point.dir.x);
|
|
104
|
+
const ty = Math.abs(point.dir.y);
|
|
105
|
+
const tz = Math.abs(point.dir.z);
|
|
106
|
+
if (tx < min) {
|
|
107
|
+
min = tx;
|
|
108
|
+
point.up.set(1, 0, 0);
|
|
109
|
+
}
|
|
110
|
+
if (ty < min) {
|
|
111
|
+
min = ty;
|
|
112
|
+
point.up.set(0, 1, 0);
|
|
113
|
+
}
|
|
114
|
+
if (tz < min) {
|
|
115
|
+
point.up.set(0, 0, 1);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
point.right.crossVectors(point.dir, point.up).normalize();
|
|
119
|
+
point.up.crossVectors(point.right, point.dir).normalize();
|
|
120
|
+
point.dist = 0;
|
|
121
|
+
point.widthScale = 1;
|
|
122
|
+
point.sharp = false;
|
|
123
|
+
point.dir.normalize();
|
|
124
|
+
this.count++;
|
|
125
|
+
}
|
|
126
|
+
_end(current) {
|
|
127
|
+
const lastPoint = this.array[this.count - 1];
|
|
128
|
+
const point = this._getByIndex(this.count);
|
|
129
|
+
point.pos.copy(current);
|
|
130
|
+
point.dir.subVectors(current, lastPoint.pos);
|
|
131
|
+
const dist = point.dir.length();
|
|
132
|
+
point.dir.normalize();
|
|
133
|
+
point.up.copy(lastPoint.up); // copy last up
|
|
134
|
+
const vec = helpVec3_1.crossVectors(lastPoint.dir, point.dir);
|
|
135
|
+
if (vec.length() > Number.EPSILON) {
|
|
136
|
+
vec.normalize();
|
|
137
|
+
const theta = Math.acos(Math.min(Math.max(lastPoint.dir.dot(point.dir), -1), 1)); // clamp for floating pt errors
|
|
138
|
+
point.up.applyMatrix4(helpMat4.makeRotationAxis(vec, theta));
|
|
139
|
+
}
|
|
140
|
+
point.right.crossVectors(point.dir, point.up).normalize();
|
|
141
|
+
point.dist = lastPoint.dist + dist;
|
|
142
|
+
point.widthScale = 1;
|
|
143
|
+
point.sharp = false;
|
|
144
|
+
this.count++;
|
|
145
|
+
}
|
|
146
|
+
_corner(current, next, cornerRadius, cornerSplit, up) {
|
|
147
|
+
if (cornerRadius > 0 && cornerSplit > 0) {
|
|
148
|
+
const lastPoint = this.array[this.count - 1];
|
|
149
|
+
const curve = _getCornerBezierCurve(lastPoint.pos, current, next, cornerRadius, this.count - 1 === 0, helpCurve);
|
|
150
|
+
const samplerPoints = curve.getPoints(cornerSplit);
|
|
151
|
+
for (let f = 0; f < cornerSplit; f++) {
|
|
152
|
+
this._sharpCorner(samplerPoints[f], samplerPoints[f + 1], up, f === 0 ? 1 : 0);
|
|
153
|
+
}
|
|
154
|
+
if (!samplerPoints[cornerSplit].equals(next)) {
|
|
155
|
+
this._sharpCorner(samplerPoints[cornerSplit], next, up, 2);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
this._sharpCorner(current, next, up, 0, true);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
// dirType: 0 - use middle dir / 1 - use last dir / 2- use next dir
|
|
163
|
+
_sharpCorner(current, next, up, dirType = 0, sharp = false) {
|
|
164
|
+
const lastPoint = this.array[this.count - 1];
|
|
165
|
+
const point = this._getByIndex(this.count);
|
|
166
|
+
const lastDir = helpVec3_1.subVectors(current, lastPoint.pos);
|
|
167
|
+
const nextDir = helpVec3_2.subVectors(next, current);
|
|
168
|
+
const lastDirLength = lastDir.length();
|
|
169
|
+
lastDir.normalize();
|
|
170
|
+
nextDir.normalize();
|
|
171
|
+
point.pos.copy(current);
|
|
172
|
+
if (dirType === 1) {
|
|
173
|
+
point.dir.copy(lastDir);
|
|
174
|
+
}
|
|
175
|
+
else if (dirType === 2) {
|
|
176
|
+
point.dir.copy(nextDir);
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
point.dir.addVectors(lastDir, nextDir);
|
|
180
|
+
point.dir.normalize();
|
|
181
|
+
}
|
|
182
|
+
if (up) {
|
|
183
|
+
if (point.dir.dot(up) === 1) {
|
|
184
|
+
point.right.crossVectors(nextDir, up).normalize();
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
point.right.crossVectors(point.dir, up).normalize();
|
|
188
|
+
}
|
|
189
|
+
point.up.crossVectors(point.right, point.dir).normalize();
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
point.up.copy(lastPoint.up);
|
|
193
|
+
const vec = helpVec3_3.crossVectors(lastPoint.dir, point.dir);
|
|
194
|
+
if (vec.length() > Number.EPSILON) {
|
|
195
|
+
vec.normalize();
|
|
196
|
+
const theta = Math.acos(Math.min(Math.max(lastPoint.dir.dot(point.dir), -1), 1)); // clamp for floating pt errors
|
|
197
|
+
point.up.applyMatrix4(helpMat4.makeRotationAxis(vec, theta));
|
|
198
|
+
}
|
|
199
|
+
point.right.crossVectors(point.dir, point.up).normalize();
|
|
200
|
+
}
|
|
201
|
+
point.dist = lastPoint.dist + lastDirLength;
|
|
202
|
+
const _cos = lastDir.dot(nextDir);
|
|
203
|
+
point.widthScale = Math.min(1 / Math.sqrt((1 + _cos) / 2), 1.415) || 1;
|
|
204
|
+
point.sharp = Math.abs(_cos - 1) > 0.05 && sharp;
|
|
205
|
+
this.count++;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
export { PathPointList };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Usage } from 'three/src/constants.js';
|
|
2
|
+
import { PathGeometry, type Options, type PathGeometryParam } from './PathGeometry.js';
|
|
3
|
+
import type { PathPointList } from './PathPointList.js';
|
|
4
|
+
/**
|
|
5
|
+
* PathTubeGeometry
|
|
6
|
+
*/
|
|
7
|
+
declare class PathTubeGeometry extends PathGeometry {
|
|
8
|
+
/**
|
|
9
|
+
* @param {Object|Number} initData - If initData is number, geometry init by empty data and set it as the max vertex. If initData is Object, it contains pathPointList and options.
|
|
10
|
+
* @param {Boolean} [generateUv2=false]
|
|
11
|
+
*/
|
|
12
|
+
constructor(initData: PathGeometryParam, generateUv2?: boolean);
|
|
13
|
+
_initByData(pathPointList: PathPointList, options: Options, usage: Usage, generateUv2: boolean): void;
|
|
14
|
+
/**
|
|
15
|
+
* Update geometry by PathPointList instance
|
|
16
|
+
* @param {PathPointList} pathPointList
|
|
17
|
+
* @param {Object} options
|
|
18
|
+
* @param {Number} [options.radius=0.1]
|
|
19
|
+
* @param {Number} [options.progress=1]
|
|
20
|
+
* @param {Boolean} [options.radialSegments=8]
|
|
21
|
+
* @param {String} [options.startRad=0]
|
|
22
|
+
*/
|
|
23
|
+
update(pathPointList: PathPointList, options: Options): void;
|
|
24
|
+
}
|
|
25
|
+
export { PathTubeGeometry };
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { StaticDrawUsage } from 'three/src/constants.js';
|
|
2
|
+
import { PathGeometry } from './PathGeometry.js';
|
|
3
|
+
import { PathPoint } from './PathPoint.js';
|
|
4
|
+
import { BufferAttribute } from 'three/src/core/BufferAttribute.js';
|
|
5
|
+
import { Vector3 } from 'three/src/math/Vector3.js';
|
|
6
|
+
/**
|
|
7
|
+
* PathTubeGeometry
|
|
8
|
+
*/
|
|
9
|
+
class PathTubeGeometry extends PathGeometry {
|
|
10
|
+
/**
|
|
11
|
+
* @param {Object|Number} initData - If initData is number, geometry init by empty data and set it as the max vertex. If initData is Object, it contains pathPointList and options.
|
|
12
|
+
* @param {Boolean} [generateUv2=false]
|
|
13
|
+
*/
|
|
14
|
+
constructor(initData, generateUv2 = false) {
|
|
15
|
+
super(initData, generateUv2);
|
|
16
|
+
}
|
|
17
|
+
_initByData(pathPointList, options = {}, usage, generateUv2) {
|
|
18
|
+
const vertexData = generateTubeVertexData(pathPointList, options, generateUv2);
|
|
19
|
+
if (vertexData && vertexData.count !== 0) {
|
|
20
|
+
this.setAttribute('position', new BufferAttribute(new Float32Array(vertexData.position), 3).setUsage(usage || StaticDrawUsage));
|
|
21
|
+
this.setAttribute('normal', new BufferAttribute(new Float32Array(vertexData.normal), 3).setUsage(usage || StaticDrawUsage));
|
|
22
|
+
this.setAttribute('uv', new BufferAttribute(new Float32Array(vertexData.uv), 2).setUsage(usage || StaticDrawUsage));
|
|
23
|
+
if (generateUv2) {
|
|
24
|
+
this.setAttribute('uv2', new BufferAttribute(new Float32Array(vertexData.uv2), 2).setUsage(usage || StaticDrawUsage));
|
|
25
|
+
}
|
|
26
|
+
this.setIndex(vertexData.indices);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
this._initByMaxVertex(2, generateUv2);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Update geometry by PathPointList instance
|
|
34
|
+
* @param {PathPointList} pathPointList
|
|
35
|
+
* @param {Object} options
|
|
36
|
+
* @param {Number} [options.radius=0.1]
|
|
37
|
+
* @param {Number} [options.progress=1]
|
|
38
|
+
* @param {Boolean} [options.radialSegments=8]
|
|
39
|
+
* @param {String} [options.startRad=0]
|
|
40
|
+
*/
|
|
41
|
+
update(pathPointList, options) {
|
|
42
|
+
const generateUv2 = !!this.getAttribute('uv2');
|
|
43
|
+
const vertexData = generateTubeVertexData(pathPointList, options, generateUv2);
|
|
44
|
+
if (vertexData) {
|
|
45
|
+
this._updateAttributes(vertexData.position, vertexData.normal, vertexData.uv, generateUv2 ? vertexData.uv2 : null, vertexData.indices);
|
|
46
|
+
this.drawRange.count = vertexData.count;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
this.drawRange.count = 0;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export { PathTubeGeometry };
|
|
54
|
+
// Vertex Data Generate Functions
|
|
55
|
+
function generateTubeVertexData(pathPointList, options, generateUv2 = false) {
|
|
56
|
+
const radius = options.radius || 0.1;
|
|
57
|
+
const progress = options.progress !== undefined ? options.progress : 1;
|
|
58
|
+
const radialSegments = Math.max(2, options.radialSegments || 8);
|
|
59
|
+
const startRad = options.startRad || 0;
|
|
60
|
+
const circum = radius * 2 * Math.PI;
|
|
61
|
+
const totalDistance = pathPointList.distance();
|
|
62
|
+
const progressDistance = progress * totalDistance;
|
|
63
|
+
if (progressDistance == 0) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
let count = 0;
|
|
67
|
+
// modify data
|
|
68
|
+
const position = [];
|
|
69
|
+
const normal = [];
|
|
70
|
+
const uv = [];
|
|
71
|
+
const uv2 = [];
|
|
72
|
+
const indices = [];
|
|
73
|
+
let verticesCount = 0;
|
|
74
|
+
const normalDir = new Vector3();
|
|
75
|
+
function addVertices(pathPoint, radius, radialSegments) {
|
|
76
|
+
const first = position.length === 0;
|
|
77
|
+
const uvDist = pathPoint.dist / circum;
|
|
78
|
+
const uvDist2 = pathPoint.dist / totalDistance;
|
|
79
|
+
for (let r = 0; r <= radialSegments; r++) {
|
|
80
|
+
let _r = r;
|
|
81
|
+
if (_r == radialSegments) {
|
|
82
|
+
_r = 0;
|
|
83
|
+
}
|
|
84
|
+
normalDir
|
|
85
|
+
.copy(pathPoint.up)
|
|
86
|
+
.applyAxisAngle(pathPoint.dir, startRad + (Math.PI * 2 * _r) / radialSegments)
|
|
87
|
+
.normalize();
|
|
88
|
+
position.push(pathPoint.pos.x + normalDir.x * radius * pathPoint.widthScale, pathPoint.pos.y + normalDir.y * radius * pathPoint.widthScale, pathPoint.pos.z + normalDir.z * radius * pathPoint.widthScale);
|
|
89
|
+
normal.push(normalDir.x, normalDir.y, normalDir.z);
|
|
90
|
+
uv.push(uvDist, r / radialSegments);
|
|
91
|
+
if (generateUv2) {
|
|
92
|
+
uv2.push(uvDist2, r / radialSegments);
|
|
93
|
+
}
|
|
94
|
+
verticesCount++;
|
|
95
|
+
}
|
|
96
|
+
if (!first) {
|
|
97
|
+
const begin1 = verticesCount - (radialSegments + 1) * 2;
|
|
98
|
+
const begin2 = verticesCount - (radialSegments + 1);
|
|
99
|
+
for (let i = 0; i < radialSegments; i++) {
|
|
100
|
+
indices.push(begin2 + i, begin1 + i, begin1 + i + 1, begin2 + i, begin1 + i + 1, begin2 + i + 1);
|
|
101
|
+
count += 6;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (progressDistance > 0) {
|
|
106
|
+
for (let i = 0; i < pathPointList.count; i++) {
|
|
107
|
+
const pathPoint = pathPointList.array[i];
|
|
108
|
+
if (pathPoint.dist > progressDistance) {
|
|
109
|
+
const prevPoint = pathPointList.array[i - 1];
|
|
110
|
+
const lastPoint = new PathPoint();
|
|
111
|
+
// linear lerp for progress
|
|
112
|
+
const alpha = (progressDistance - prevPoint.dist) / (pathPoint.dist - prevPoint.dist);
|
|
113
|
+
lastPoint.lerpPathPoints(prevPoint, pathPoint, alpha);
|
|
114
|
+
addVertices(lastPoint, radius, radialSegments);
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
addVertices(pathPoint, radius, radialSegments);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
position,
|
|
124
|
+
normal,
|
|
125
|
+
uv,
|
|
126
|
+
uv2,
|
|
127
|
+
indices,
|
|
128
|
+
count,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Material, Mesh, Vector3 } from "three";
|
|
2
|
+
import { PathPointList } from "./PathPointList";
|
|
3
|
+
import { PathTubeGeometry } from "./PathTubeGeometry";
|
|
4
|
+
export type PipeMeshProps = {
|
|
5
|
+
vector3s: Array<Vector3>;
|
|
6
|
+
radius: number;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* 创建基础的管道模型
|
|
10
|
+
*/
|
|
11
|
+
export default class PipeMesh extends Mesh<PathTubeGeometry, Material> {
|
|
12
|
+
constructor(props: PipeMeshProps);
|
|
13
|
+
type: string;
|
|
14
|
+
vector3s: Array<Vector3>;
|
|
15
|
+
radius: number;
|
|
16
|
+
pathPointList: PathPointList;
|
|
17
|
+
material: Material;
|
|
18
|
+
set(props: PipeMeshProps): void;
|
|
19
|
+
setVector(i: number, vector: Vector3): void;
|
|
20
|
+
getVector(i: number): Vector3;
|
|
21
|
+
setLatestVector(vector: Vector3): void;
|
|
22
|
+
getLatestVector(): Vector3;
|
|
23
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @Author: xiaosihan
|
|
3
|
+
* @Date: 2022-02-16 23:13:14
|
|
4
|
+
* @Last Modified by: xiaosihan
|
|
5
|
+
* @Last Modified time: 2024-11-23 13:06:54
|
|
6
|
+
*/
|
|
7
|
+
import { DoubleSide, DynamicDrawUsage, Mesh, MeshLambertMaterial, } from "three";
|
|
8
|
+
import { PathPointList } from "./PathPointList";
|
|
9
|
+
import { PathTubeGeometry } from "./PathTubeGeometry";
|
|
10
|
+
/**
|
|
11
|
+
* 创建基础的管道模型
|
|
12
|
+
*/
|
|
13
|
+
export default class PipeMesh extends Mesh {
|
|
14
|
+
constructor(props) {
|
|
15
|
+
super();
|
|
16
|
+
this.set(props);
|
|
17
|
+
}
|
|
18
|
+
type = "pipemesh";
|
|
19
|
+
vector3s = [];
|
|
20
|
+
radius = 1;
|
|
21
|
+
pathPointList = new PathPointList();
|
|
22
|
+
material = new MeshLambertMaterial({
|
|
23
|
+
side: DoubleSide,
|
|
24
|
+
color: "#fff",
|
|
25
|
+
});
|
|
26
|
+
// 设置数据
|
|
27
|
+
set(props) {
|
|
28
|
+
this.vector3s = props.vector3s;
|
|
29
|
+
this.radius = props.radius;
|
|
30
|
+
this.pathPointList.set(this.vector3s, props.radius * 2, 10);
|
|
31
|
+
this.geometry = new PathTubeGeometry({
|
|
32
|
+
pathPointList: this.pathPointList,
|
|
33
|
+
options: {
|
|
34
|
+
arrow: true,
|
|
35
|
+
progress: 1,
|
|
36
|
+
radius: props.radius,
|
|
37
|
+
radialSegments: 10,
|
|
38
|
+
},
|
|
39
|
+
usage: DynamicDrawUsage,
|
|
40
|
+
transparent: true,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
// 设置指定点的坐标
|
|
44
|
+
setVector(i, vector) {
|
|
45
|
+
this.vector3s[i].copy(vector);
|
|
46
|
+
this.pathPointList.set(this.vector3s, this.radius * 2, 10);
|
|
47
|
+
this.geometry.update(this.pathPointList, { radius: this.radius });
|
|
48
|
+
}
|
|
49
|
+
// 获取指定点的坐标
|
|
50
|
+
getVector(i) {
|
|
51
|
+
return this.vector3s[i];
|
|
52
|
+
}
|
|
53
|
+
// 设置最后一个点的坐标
|
|
54
|
+
setLatestVector(vector) {
|
|
55
|
+
this.setVector(this.vector3s.length - 1, vector);
|
|
56
|
+
}
|
|
57
|
+
// 回去最后一个点的坐标
|
|
58
|
+
getLatestVector() {
|
|
59
|
+
return this.getVector(this.vector3s.length - 1);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "./PipeMesh";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "./PipeMesh";
|
package/ThreeBase.js
CHANGED
|
@@ -82,8 +82,8 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
82
82
|
renderer = (() => {
|
|
83
83
|
const renderer = new WebGLRenderer({
|
|
84
84
|
alpha: true,
|
|
85
|
-
antialias:
|
|
86
|
-
precision: "
|
|
85
|
+
antialias: false,
|
|
86
|
+
precision: "lowp",
|
|
87
87
|
logarithmicDepthBuffer: true,
|
|
88
88
|
preserveDrawingBuffer: true,
|
|
89
89
|
stencil: false, // 模版
|
package/keyContorls.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ declare class KeyContorls extends EventDispatcher {
|
|
|
16
16
|
_listeners?: {
|
|
17
17
|
[key: string]: Array<any>;
|
|
18
18
|
};
|
|
19
|
-
removeEvent: import("lodash").
|
|
19
|
+
removeEvent: import("lodash").DebouncedFuncLeading<(type: string) => void>;
|
|
20
20
|
addEventListener<T extends string>(type: T, listener: EventListener<{}, T, this>): void;
|
|
21
21
|
addEvent(): void;
|
|
22
22
|
dispose(): void;
|