-
Notifications
You must be signed in to change notification settings - Fork 105
/
MapNodeGeometry.ts
215 lines (172 loc) · 5.4 KB
/
MapNodeGeometry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import {BufferGeometry, Float32BufferAttribute} from 'three';
/**
* Map node geometry is a geometry used to represent the map nodes.
*
* Consists of a XZ plane with normals facing +Y.
*
* The geometry points start in XZ plane that can be manipulated for example for height adjustment.
*
* Geometry can also include skirts to mask off missalignments between tiles.
*/
export class MapNodeGeometry extends BufferGeometry
{
/**
* Map node geometry constructor.
*
* @param width - Width of the node.
* @param height - Height of the node.
* @param widthSegments - Number of subdivisions along the width.
* @param heightSegments - Number of subdivisions along the height.
* @param skirt - Skirt around the plane to mask gaps between tiles.
*/
public constructor(width: number = 1.0, height: number = 1.0, widthSegments: number = 1.0, heightSegments: number = 1.0, skirt: boolean = false, skirtDepth: number = 10.0)
{
super();
// Buffers
const indices = [];
const vertices = [];
const normals = [];
const uvs = [];
// Build plane
MapNodeGeometry.buildPlane(width, height, widthSegments, heightSegments, indices, vertices, normals, uvs);
// Generate the skirt
if (skirt)
{
MapNodeGeometry.buildSkirt(width, height, widthSegments, heightSegments, skirtDepth, indices, vertices, normals, uvs);
}
this.setIndex(indices);
this.setAttribute('position', new Float32BufferAttribute(vertices, 3));
this.setAttribute('normal', new Float32BufferAttribute(normals, 3));
this.setAttribute('uv', new Float32BufferAttribute(uvs, 2));
}
public static buildPlane(width: number = 1.0, height: number = 1.0, widthSegments: number = 1.0, heightSegments: number = 1.0, indices: number[], vertices: number[], normals: number[], uvs: number[]): void
{
// Half width X
const widthHalf = width / 2;
// Half width Z
const heightHalf = height / 2;
// Size of the grid in X
const gridX = widthSegments + 1;
// Size of the grid in Z
const gridZ = heightSegments + 1;
// Width of each segment X
const segmentWidth = width / widthSegments;
// Height of each segment Z
const segmentHeight = height / heightSegments;
// Generate vertices, normals and uvs
for (let iz = 0; iz < gridZ; iz++)
{
const z = iz * segmentHeight - heightHalf;
for (let ix = 0; ix < gridX; ix++)
{
const x = ix * segmentWidth - widthHalf;
vertices.push(x, 0, z);
normals.push(0, 1, 0);
uvs.push(ix / widthSegments, 1 - iz / heightSegments);
}
}
// Indices
for (let iz = 0; iz < heightSegments; iz++)
{
for (let ix = 0; ix < widthSegments; ix++)
{
const a = ix + gridX * iz;
const b = ix + gridX * (iz + 1);
const c = ix + 1 + gridX * (iz + 1);
const d = ix + 1 + gridX * iz;
// Faces
indices.push(a, b, d, b, c, d);
}
}
}
public static buildSkirt(width: number = 1.0, height: number = 1.0, widthSegments: number = 1.0, heightSegments: number = 1.0, skirtDepth: number, indices: number[], vertices: number[], normals: number[], uvs: number[]): void
{
// Half width X
const widthHalf = width / 2;
// Half width Z
const heightHalf = height / 2;
// Size of the grid in X
const gridX = widthSegments + 1;
// Size of the grid in Z
const gridZ = heightSegments + 1;
// Width of each segment X
const segmentWidth = width / widthSegments;
// Height of each segment Z
const segmentHeight = height / heightSegments;
let start = vertices.length / 3;
// Down X
for (let ix = 0; ix < gridX; ix++)
{
const x = ix * segmentWidth - widthHalf;
const z = -heightHalf;
vertices.push(x, -skirtDepth, z);
normals.push(0, 1, 0);
uvs.push(ix / widthSegments, 1);
}
// Indices
for (let ix = 0; ix < widthSegments; ix++)
{
const a = ix;
const d = ix + 1;
const b = ix + start;
const c = ix + start + 1;
indices.push(d, b, a, d, c, b);
}
start = vertices.length / 3;
// Up X
for (let ix = 0; ix < gridX; ix++)
{
const x = ix * segmentWidth - widthHalf;
const z = heightSegments * segmentHeight - heightHalf;
vertices.push(x, -skirtDepth, z);
normals.push(0, 1, 0);
uvs.push(ix / widthSegments, 0);
}
// Index of the beginning of the last X row
let offset = gridX * gridZ - widthSegments - 1;
for (let ix = 0; ix < widthSegments; ix++)
{
const a = offset + ix;
const d = offset + ix + 1;
const b = ix + start;
const c = ix + start + 1;
indices.push(a, b, d, b, c, d);
}
start = vertices.length / 3;
// Down Z
for (let iz = 0; iz < gridZ; iz++)
{
const z = iz * segmentHeight - heightHalf;
const x = - widthHalf;
vertices.push(x, -skirtDepth, z);
normals.push(0, 1, 0);
uvs.push(0, 1 - iz / heightSegments);
}
for (let iz = 0; iz < heightSegments; iz++)
{
const a = iz * gridZ;
const d = (iz + 1) * gridZ;
const b = iz + start;
const c = iz + start + 1;
indices.push(a, b, d, b, c, d);
}
start = vertices.length / 3;
// Up Z
for (let iz = 0; iz < gridZ; iz++)
{
const z = iz * segmentHeight - heightHalf;
const x = widthSegments * segmentWidth - widthHalf;
vertices.push(x, -skirtDepth, z);
normals.push(0, 1, 0);
uvs.push(1.0, 1 - iz / heightSegments);
}
for (let iz = 0; iz < heightSegments; iz++)
{
const a = iz * gridZ + heightSegments;
const d = (iz + 1) * gridZ + heightSegments;
const b = iz + start;
const c = iz + start + 1;
indices.push(d, b, a, d, c, b);
}
}
}