diff --git a/js/main.js b/js/main.js index b5183a15f0e3ea1a2a71ae4c67875545572ee44a..2e38b74ee81b960525675684db60f8ce3656b639 100644 --- a/js/main.js +++ b/js/main.js @@ -20,5 +20,5 @@ $(function(){ //threeJS View new ThreeView({model:window.three, lattice:lattice, appState:appState}); - lattice.addCell(new THREE.Vector3(0,0,0)); + lattice.addCellAtIndex({x:0,y:0,z:0}); }); diff --git a/js/models/Lattice.js b/js/models/Lattice.js index 0fac5990c7842db589df54049c64cd641bdf7c64..35398388e2a8c5d736045fcabce17dea7fd70a4e 100644 --- a/js/models/Lattice.js +++ b/js/models/Lattice.js @@ -52,9 +52,7 @@ Lattice = Backbone.Model.extend({ ///////////////////////////////ADD/REMOVE CELLS///////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// - addCell: function(absPosition){ - - var cells = this.get("cells"); + addCellAtPosition: function(absPosition){ //calc indices in cell matrix var scale = this.get("scale"); @@ -66,11 +64,19 @@ Lattice = Backbone.Model.extend({ position.z = Math.round(absPosition.z/octHeight); if (position.z%2 == 1) position.y += 1; + this.addCellAtIndex(position); + }, + + addCellAtIndex: function(indices){ + + var cells = this.get("cells"); + var scale = this.get("scale"); + //check for matrix expansion var lastMax = this.get("cellsMax"); var lastMin = this.get("cellsMin"); - var newMax = this._updateCellsMax(position, lastMax); - var newMin = this._updateCellsMin(position, lastMin); + var newMax = this._updateCellsMax(indices, lastMax); + var newMin = this._updateCellsMin(indices, lastMin); if (newMax) { this._expandCellsArray(cells, this._subtract(newMax, lastMax), false); this.set("cellsMax", newMax); @@ -80,20 +86,13 @@ Lattice = Backbone.Model.extend({ this.set("cellsMin", newMin); } - var index = this._subtract(position, this.get("cellsMin")); - if (!cells[index.x][index.y][index.z]) cells[index.x][index.y][index.z] = new DMACell(this.get("cellMode"), position, scale, this); + var index = this._subtract(indices, this.get("cellsMin")); + if (!cells[index.x][index.y][index.z]) cells[index.x][index.y][index.z] = new DMACell(this.get("cellMode"), indices, scale, this); else console.warn("already a cell there"); this.set("numCells", this.get("numCells")+1); window.three.render(); }, - removeCellFromMesh: function(object){ - - if (!object) return; - this.removeCell(object.parent.myCell); - - }, - removeCell: function(cell){ var index = this._subtract(cell.indices, this.get("cellsMin")); var cells = this.get("cells"); diff --git a/js/threeViews/Highlighter.js b/js/threeViews/Highlighter.js index bcd69d7b9fc9af647adcab9122db4159aa41651c..2780500acb5dfd9642d5cebc94ad9bbcfbb91a37 100644 --- a/js/threeViews/Highlighter.js +++ b/js/threeViews/Highlighter.js @@ -51,7 +51,7 @@ Highlighter = Backbone.View.extend({ if (this.isVisible() && this._isHighlighting(face)) return;//nothing has changed - this.currentHighlightedFace = face; + this.intersectedFace = face; if (face.normal.z<0.99){//only highlight horizontal faces this.hide(); @@ -63,13 +63,9 @@ Highlighter = Backbone.View.extend({ this.show(); }, - _getCurrentIntersectedCell: function(){ - return this.currentIntersectedCell; - }, setNoCellIntersections: function(){ this.intersectedCell = null; - this.currentIntersectedCell = null; this.hide(); }, @@ -78,7 +74,7 @@ Highlighter = Backbone.View.extend({ }, _isHighlighting: function(face){ - return this.currentHighlightedFace == face; + return this.intersectedFace == face; }, _highlightFace: function(object, face){ @@ -99,19 +95,25 @@ Highlighter = Backbone.View.extend({ return newVertices; }, - getNextCellPosition: function(){ + _getNextCellPosition: function(){ return this.mesh.geometry.vertices[0]; }, + _getNextCellVertices: function(){//offset vertices with +1 in z + var vertices = _.clone(this.intersectedCell.indices); + vertices.z += 1; + return vertices; + }, + addRemoveVoxel: function(shouldAdd){ if (shouldAdd){ if (!this.isVisible()) return; - this.model.addCell(this.getNextCellPosition()); + if (this.intersectedFace && !this.intersectedCell) this.model.addCellAtPosition(this._getNextCellPosition()); + else this.model.addCellAtIndex(this._getNextCellVertices()); } else { - var currentIntersectedCell = this._getCurrentIntersectedCell(); - if (currentIntersectedCell === this.model.get("basePlane").get("mesh")) return; - this.model.removeCellFromMesh(currentIntersectedCell); + if (this.intersectedFace && !this.intersectedCell) return;//baseplane + this.model.removeCell(this.intersectedCell); } this.hide(); } diff --git a/js/threeViews/ThreeView.js b/js/threeViews/ThreeView.js index 1657612ba721ac82e62b88412c836e89fed8dd44..ec1e7f0ed34206766530b17c1818bb013c71ce13 100644 --- a/js/threeViews/ThreeView.js +++ b/js/threeViews/ThreeView.js @@ -141,11 +141,11 @@ ThreeView = Backbone.View.extend({ this.highlighter.highlightCell(intersection.object, intersection.face); -// if (this.appState.get("deleteMode") && this.mouseIsDown){ -// this.highlighter.addRemoveVoxel(false); -// } else if (this.mouseIsDown && this.appState.get("shift")){ -// this.highlighter.addRemoveVoxel(true); -// } + if (this.appState.get("deleteMode") && this.mouseIsDown){ + this.highlighter.addRemoveVoxel(false); + } else if (this.mouseIsDown && this.appState.get("shift")){ + this.highlighter.addRemoveVoxel(true); + } // if (this.appState.get("extrudeMode") && this.mouseIsDown){ // if (!this.highlighter.isVisible) return; // this.extrudeVisualizer.makeMeshFromProfile([this.highlighter]);