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
|
var builder = new function(){
var base = this
base.tube = new Tube ()
var els = []
base.init = function(){
base.bind()
}
base.bind = function(){
base.tube.on("clip", rebuild)
}
function rebuild(){
if (window.scene) {
clear()
build()
}
}
function build (){
clipper.rooms = sort_rooms_by_id(clipper.rooms)
clipper.regions.forEach(function(r){
walls(r).forEach(function(el){
els.push(el)
scene.add(el)
})
})
clipper.rooms = sort_rooms_by_height(clipper.rooms)
clipper.rooms.forEach(function(r){
floors(r).forEach(function(el){
els.push(el)
scene.add(el)
})
})
}
function clear (){
els.forEach(function(el){
scene.remove(el)
})
els = []
}
function walls (r){
var list = [], el = null
var width = r.x.length()
var depth = r.y.length()
var height = clipper.rooms[r.id].height
if (r.sides & FRONT) {
el = wall('.front')
el.width = width
el.height = height
el.rotationY = PI
el.x = r.x.a + width/2
el.y = height/2
el.z = r.y.a
list.push(el)
}
if (r.sides & BACK) {
var el = wall('.back')
el.width = width
el.height = height
el.rotationY = 0
el.x = r.x.b - width/2
el.y = height/2
el.z = r.y.b
list.push(el)
}
if (r.sides & LEFT) {
el = wall('.left')
el.rotationY = HALF_PI
el.height = height
el.width = depth
el.x = r.x.a
el.y = height/2
el.z = r.y.a + depth/2
list.push(el)
}
if (r.sides & RIGHT) {
el = wall('.right')
el.rotationY = -HALF_PI
el.height = height
el.width = depth
el.x = r.x.b
el.y = height/2
el.z = r.y.b - depth/2
list.push(el)
}
return list
}
function floors(rm){
var list = [], el = null
var already_constructed = rm.intersects.filter(function(rr){ return rr.constructed })
sort_rooms_by_height(already_constructed)
if (already_constructed.length > 0) {
// render the regions that don't intersect with anything we've already rendered
// if the height is different, calculate the overlapping sides and render half-walls
rm.regions.forEach(function(r){
var intersected = false
for (var i = 0; i < already_constructed.length; i++) {
if (already_constructed[i].rect.contains(r)) {
intersected = true
r.sides = 0xf
}
else if (already_constructed[i].rect.intersects(r)) {
intersected = true
if (rm.height < already_constructed[i].height) {
list = list.concat( ceiling_walls( rm, already_constructed[i], r ) )
}
}
}
if (! intersected) {
list.push( ground(rm, r) )
list.push( ceiling(rm, r) )
}
})
}
else {
// render floor and ceiling for the entire rectangle
list.push( ground(rm, rm.rect) )
list.push( ceiling(rm, rm.rect) )
}
rm.constructed = true
return list
}
function ceiling_walls( lo, hi, r ){
var list = []
var width = r.x.length()
var depth = r.y.length()
var height = hi.height - lo.height
if (! (r.sides & LEFT) && r.x.a == hi.rect.x.a) {
el = wall('.left')
el.rotationY = HALF_PI
el.height = height
el.width = depth
el.x = r.x.a
el.y = lo.height + height/2
el.z = r.y.a + depth/2
list.push(el)
r.sides |= LEFT
console.log(hi.height, lo.height)
}
if (! (r.sides & RIGHT) && r.x.b == hi.rect.x.b) {
el = wall('.right')
el.rotationY = -HALF_PI
el.height = height
el.width = depth
el.x = r.x.b
el.y = lo.height + height/2
el.z = r.y.b - depth/2
list.push(el)
r.sides |= RIGHT
}
if (! (r.sides & FRONT) && r.y.a == hi.rect.y.a) {
el = wall('.front')
el.width = width
el.height = height
el.rotationY = PI
el.x = r.x.a + width/2
el.y = lo.height + height/2
el.z = r.y.a
list.push(el)
r.sides |= FRONT
}
if (! (r.sides & BACK) && r.y.b == hi.rect.y.b) {
el = wall('.back')
el.width = width
el.height = height
el.rotationY = 0
el.x = r.x.b - width/2
el.y = lo.height + height/2
el.z = r.y.b
list.push(el)
r.sides |= BACK
}
return list
}
function ground(rm, r){
var width = r.x.length()
var depth = r.y.length()
var el = wall('.bottom')
el.height = depth
el.width = width
el.x = r.x.a + width/2
el.y = 0
el.z = r.y.a + depth/2
el.rotationX = PI/2
el.el.style.backgroundColor = "#f00"
return el
}
function ceiling(rm, r){
var width = r.x.length()
var depth = r.y.length()
var height = rm.height
var el = wall('.top')
el.height = depth
el.width = width
el.x = r.x.a + width/2
el.y = height
el.z = r.y.a + depth/2
el.rotationX = -PI/2
el.el.style.backgroundColor = "#00f"
return el
}
function wall(klass){
var el = new MX.Object3D(".face" + (klass || ""))
el.width = el.height = el.scaleX = el.scaleY = el.scaleZ = 1
el.z = el.y = el.x = 0
el.type = "Face"
el.el.style.opacity = 1.0
return el
}
}
|