forked from fogleman/fauxgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
voxel.go
336 lines (318 loc) · 7.04 KB
/
voxel.go
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package fauxgl
type Voxel struct {
X, Y, Z int
Color Color
}
type voxelAxis int
const (
_ voxelAxis = iota
voxelX
voxelY
voxelZ
)
type voxelNormal struct {
Axis voxelAxis
Sign int
}
var (
voxelPosX = voxelNormal{voxelX, 1}
voxelNegX = voxelNormal{voxelX, -1}
voxelPosY = voxelNormal{voxelY, 1}
voxelNegY = voxelNormal{voxelY, -1}
voxelPosZ = voxelNormal{voxelZ, 1}
voxelNegZ = voxelNormal{voxelZ, -1}
)
type voxelPlane struct {
Normal voxelNormal
Position int
Color Color
}
type voxelFace struct {
I0, J0 int
I1, J1 int
}
func NewVoxelMesh(voxels []Voxel) *Mesh {
type key struct {
X, Y, Z int
}
// create lookup table
lookup := make(map[key]bool)
for _, v := range voxels {
lookup[key{v.X, v.Y, v.Z}] = true
}
// find exposed faces
planeFaces := make(map[voxelPlane][]voxelFace)
for _, v := range voxels {
if !lookup[key{v.X + 1, v.Y, v.Z}] {
plane := voxelPlane{voxelPosX, v.X, v.Color}
face := voxelFace{v.Y, v.Z, v.Y, v.Z}
planeFaces[plane] = append(planeFaces[plane], face)
}
if !lookup[key{v.X - 1, v.Y, v.Z}] {
plane := voxelPlane{voxelNegX, v.X, v.Color}
face := voxelFace{v.Y, v.Z, v.Y, v.Z}
planeFaces[plane] = append(planeFaces[plane], face)
}
if !lookup[key{v.X, v.Y + 1, v.Z}] {
plane := voxelPlane{voxelPosY, v.Y, v.Color}
face := voxelFace{v.X, v.Z, v.X, v.Z}
planeFaces[plane] = append(planeFaces[plane], face)
}
if !lookup[key{v.X, v.Y - 1, v.Z}] {
plane := voxelPlane{voxelNegY, v.Y, v.Color}
face := voxelFace{v.X, v.Z, v.X, v.Z}
planeFaces[plane] = append(planeFaces[plane], face)
}
if !lookup[key{v.X, v.Y, v.Z + 1}] {
plane := voxelPlane{voxelPosZ, v.Z, v.Color}
face := voxelFace{v.X, v.Y, v.X, v.Y}
planeFaces[plane] = append(planeFaces[plane], face)
}
if !lookup[key{v.X, v.Y, v.Z - 1}] {
plane := voxelPlane{voxelNegZ, v.Z, v.Color}
face := voxelFace{v.X, v.Y, v.X, v.Y}
planeFaces[plane] = append(planeFaces[plane], face)
}
}
var triangles []*Triangle
var lines []*Line
// find large rectangles, triangulate and outline
for plane, faces := range planeFaces {
faces = combineVoxelFaces(faces)
lines = append(lines, outlineVoxelFaces(plane, faces)...)
triangles = append(triangles, triangulateVoxelFaces(plane, faces)...)
}
return NewMesh(triangles, lines)
}
func combineVoxelFaces(faces []voxelFace) []voxelFace {
// determine bounding box
i0 := faces[0].I0
j0 := faces[0].J0
i1 := faces[0].I1
j1 := faces[0].J1
for _, f := range faces {
if f.I0 < i0 {
i0 = f.I0
}
if f.J0 < j0 {
j0 = f.J0
}
if f.I1 > i1 {
i1 = f.I1
}
if f.J1 > j1 {
j1 = f.J1
}
}
// create arrays
nj := j1 - j0 + 1
ni := i1 - i0 + 1
a := make([][]int, nj)
w := make([][]int, nj)
h := make([][]int, nj)
for j := range a {
a[j] = make([]int, ni)
w[j] = make([]int, ni)
h[j] = make([]int, ni)
}
// populate array
count := 0
for _, f := range faces {
for j := f.J0; j <= f.J1; j++ {
for i := f.I0; i <= f.I1; i++ {
a[j-j0][i-i0] = 1
count++
}
}
}
// find rectangles
var result []voxelFace
for count > 0 {
var maxArea int
var maxFace voxelFace
for j := 0; j < nj; j++ {
for i := 0; i < ni; i++ {
if a[j][i] == 0 {
continue
}
if j == 0 {
h[j][i] = 1
} else {
h[j][i] = h[j-1][i] + 1
}
if i == 0 {
w[j][i] = 1
} else {
w[j][i] = w[j][i-1] + 1
}
minw := w[j][i]
for dh := 0; dh < h[j][i]; dh++ {
if w[j-dh][i] < minw {
minw = w[j-dh][i]
}
area := (dh + 1) * minw
if area > maxArea {
maxArea = area
maxFace = voxelFace{
i0 + i - minw + 1, j0 + j - dh, i0 + i, j0 + j}
}
}
}
}
result = append(result, maxFace)
for j := maxFace.J0; j <= maxFace.J1; j++ {
for i := maxFace.I0; i <= maxFace.I1; i++ {
a[j-j0][i-i0] = 0
count--
}
}
for j := 0; j < nj; j++ {
for i := 0; i < ni; i++ {
w[j][i] = 0
h[j][i] = 0
}
}
}
return result
}
func triangulateVoxelFaces(plane voxelPlane, faces []voxelFace) []*Triangle {
triangles := make([]*Triangle, len(faces)*2)
k := float64(plane.Position) + float64(plane.Normal.Sign)*0.5
for i, face := range faces {
i0 := float64(face.I0) - 0.5
j0 := float64(face.J0) - 0.5
i1 := float64(face.I1) + 0.5
j1 := float64(face.J1) + 0.5
var p1, p2, p3, p4 Vector
switch plane.Normal.Axis {
case voxelX:
p1 = Vector{k, i0, j0}
p2 = Vector{k, i1, j0}
p3 = Vector{k, i1, j1}
p4 = Vector{k, i0, j1}
case voxelY:
p1 = Vector{i0, k, j1}
p2 = Vector{i1, k, j1}
p3 = Vector{i1, k, j0}
p4 = Vector{i0, k, j0}
case voxelZ:
p1 = Vector{i0, j0, k}
p2 = Vector{i1, j0, k}
p3 = Vector{i1, j1, k}
p4 = Vector{i0, j1, k}
}
if plane.Normal.Sign < 0 {
p1, p2, p3, p4 = p4, p3, p2, p1
}
t1 := NewTriangleForPoints(p1, p2, p3)
t2 := NewTriangleForPoints(p1, p3, p4)
t1.V1.Color = plane.Color
t1.V2.Color = plane.Color
t1.V3.Color = plane.Color
t2.V1.Color = plane.Color
t2.V2.Color = plane.Color
t2.V3.Color = plane.Color
triangles[i*2+0] = t1
triangles[i*2+1] = t2
}
return triangles
}
func outlineVoxelFaces(plane voxelPlane, faces []voxelFace) []*Line {
// determine bounding box
i0 := faces[0].I0
j0 := faces[0].J0
i1 := faces[0].I1
j1 := faces[0].J1
for _, f := range faces {
if f.I0 < i0 {
i0 = f.I0
}
if f.J0 < j0 {
j0 = f.J0
}
if f.I1 > i1 {
i1 = f.I1
}
if f.J1 > j1 {
j1 = f.J1
}
}
// padding
i0--
j0--
i1++
j1++
// create array
nj := j1 - j0 + 1
ni := i1 - i0 + 1
a := make([][]bool, nj)
for j := range a {
a[j] = make([]bool, ni)
}
// populate array
for _, f := range faces {
for j := f.J0; j <= f.J1; j++ {
for i := f.I0; i <= f.I1; i++ {
a[j-j0][i-i0] = true
}
}
}
var lines []*Line
for sign := -1; sign <= 1; sign += 2 {
// find "horizontal" lines
for j := 1; j < nj-1; j++ {
start := -1
for i := 0; i < ni; i++ {
if a[j][i] && !a[j+sign][i] {
if start < 0 {
start = i
}
} else if start >= 0 {
end := i - 1
ai := float64(i0+start) - 0.5
bi := float64(i0+end) + 0.5
jj := float64(j0+j) + 0.5*float64(sign)
line := createVoxelOutline(plane, ai, jj, bi, jj)
lines = append(lines, line)
start = -1
}
}
}
// find "vertical" lines
for i := 1; i < ni-1; i++ {
start := -1
for j := 0; j < nj; j++ {
if a[j][i] && !a[j][i+sign] {
if start < 0 {
start = j
}
} else if start >= 0 {
end := j - 1
aj := float64(j0+start) - 0.5
bj := float64(j0+end) + 0.5
ii := float64(i0+i) + 0.5*float64(sign)
line := createVoxelOutline(plane, ii, aj, ii, bj)
lines = append(lines, line)
start = -1
}
}
}
}
return lines
}
func createVoxelOutline(plane voxelPlane, i0, j0, i1, j1 float64) *Line {
k := float64(plane.Position) + float64(plane.Normal.Sign)*0.5
var p1, p2 Vector
switch plane.Normal.Axis {
case voxelX:
p1 = Vector{k, i0, j0}
p2 = Vector{k, i1, j1}
case voxelY:
p1 = Vector{i0, k, j0}
p2 = Vector{i1, k, j1}
case voxelZ:
p1 = Vector{i0, j0, k}
p2 = Vector{i1, j1, k}
}
return NewLineForPoints(p1, p2)
}