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) } base.wheel = new wheel({ el: document.querySelector("#map"), update: function(e, val, delta){ console.log(e.clientX, e.clientY, delta) } }) function rebuild(){ if (window.scene) { clear() build() } } function build (){ clipper.rooms = sort_rooms_by_height(clipper.rooms) clipper.rooms.forEach(function(r){ floors(r).forEach(function(el){ els.push(el) scene.add(el) }) }) clipper.rooms = sort_rooms_by_id(clipper.rooms) clipper.regions.forEach(function(r){ walls(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 }) 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.intersects(r)) { // probably create walls here? intersected = true } } 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 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 } }