From 81bae08836b5c07a390179532fb97ea937055ab2 Mon Sep 17 00:00:00 2001 From: Amanda Ghassaei <amandaghassaei@gmail.com> Date: Mon, 22 Jun 2015 13:08:35 -0700 Subject: [PATCH] adding import menu back in --- js/cells/DMACell.js | 57 +++++-- js/cells/supercells/DMASuperCell.js | 4 +- js/main.js | 5 +- js/menus/ImportMenuView.js | 4 +- js/menus/templates/Ribbon.html | 2 +- js/models/AppState.js | 2 +- js/models/PList.js | 2 +- js/three/FillGeometry.js | 250 ++++++++++++++-------------- 8 files changed, 183 insertions(+), 143 deletions(-) diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js index e33f40b5..d4d02cce 100644 --- a/js/cells/DMACell.js +++ b/js/cells/DMACell.js @@ -12,7 +12,6 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', ' if (json.index) this.index = new THREE.Vector3(json.index.x, json.index.y, json.index.z); if (superCell) this.superCell = superCell; - this.materialName = json.materialName || appState.get("materialType"); //object 3d is parent to all 3d elements owned by cell: cell mesh and wireframe, parts, beams, nodes, etc @@ -121,17 +120,14 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', ' return this.getOrientation().multiply(this.superCell.getAbsoluteOrientation());//order matters! }; - DMACell.prototype.getEuler = function(){ - return this.object3D.rotation.clone(); - }; - - DMACell.prototype.applyRotation = function(vector){// + DMACell.prototype.applyRotation = function(vector){//todo local rotation? vector.applyQuaternion(this.getAbsoluteOrientation()); return vector; }; DMACell.prototype.applyAbsoluteRotation = function(vector){ vector.applyQuaternion(this.getAbsoluteOrientation()); + return vector; }; @@ -143,7 +139,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', ' //highlighting DMACell.prototype.calcHighlighterParams = function(face, point){//this works for rectalinear, override in subclasses - var direction = face.normal.clone().applyQuaternion(this.getAbsoluteOrientation()); + var direction = this.applyAbsoluteRotation(face.normal.clone());//todo local orientation? var position = this.getAbsolutePosition(); var self = this; _.each(_.keys(position), function(key){ @@ -153,12 +149,11 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', ' }; DMACell.prototype.setDeleteMode = function(state){ - var material; if (!state && !this.materialName) return;//cell may be deleted by now if (state) material = materials.deleteMaterial.threeMaterial; else material = this.getMaterial(true); - if (!material) return;//cell may be deleted by now + if (!material) return;//no material object found if (this.object3D.children[0].material == material) return; if (this.cells){ @@ -264,8 +259,13 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', ' } else setVisiblity(); break; case "beam": -// if (!this.beams) this.beams = this._initBeams(); - setVisiblity(); + if (!this.beams) this.beams = this._initBeams(function(){ + if (!this.nodes) this.nodes = self._initNodes(function(){ + setVisiblity(); + }); + else setVisiblity(); + }); + else setVisiblity(); break; case "node": // if (!this.nodes) this.nodes = this._initNodes(); @@ -301,7 +301,40 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', ' //subcomponents DMACell.prototype._initParts = function(callback){ - callback();//override in subclasses + if (callback) callback();//override in subclasses + return []; + }; + + DMACell.prototype._initNodes = function(callback){ +// var PI2 = Math.PI * 2; +// var material = new THREE.SpriteCanvasMaterial({ +// color: 0xffffff, +// program: function ( context ) { +// context.beginPath(); +// context.arc( 0, 0, 2, 0, PI2, true ); +// context.fill(); +// } +// }); +// +// var particle = new THREE.Sprite(material); +// particle.name = "beam"; +// this.addChildren(particle); + if (callback) callback(); + return true; + }; + + DMACell.prototype._initBeams = function(callback){ + if (this.cells) { + _.each(this.cells) + } + var wireframe = this._buildWireframe(this.object3D.children[0], this._getGeometry()); + wireframe.name = "beam"; + if (wireframe.material.wireframeLinewidth) wireframe.material.wireframeLinewidth = 5; + else wireframe.material.linewidth = 5; + wireframe.material.color = this.getMaterial(true).color; + this.addChildren(wireframe); + if (callback) callback(); + return true; }; diff --git a/js/cells/supercells/DMASuperCell.js b/js/cells/supercells/DMASuperCell.js index ce374dfe..024c21e9 100644 --- a/js/cells/supercells/DMASuperCell.js +++ b/js/cells/supercells/DMASuperCell.js @@ -76,7 +76,9 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'], DMASuperCell.prototype.setMode = function(mode, callback){ var self = this; DMACell.prototype.setMode.call(this, mode, function(){ - var numChildren = self.object3D.children.length-2; + var numChildren = _.filter(self.object3D.children, function(child){ + return child.name == "object3D"; + }).length;//todo this is weird self._loopCells(function(cell){ if (cell) cell.setMode(mode, function(){ if (--numChildren <= 0) { diff --git a/js/main.js b/js/main.js index 1b058a93..72d3e5f5 100644 --- a/js/main.js +++ b/js/main.js @@ -19,6 +19,7 @@ require.config({ stlLoader: '../dependencies/loaders/STLLoader', threeModel: 'three/ThreeModel', threeView: 'three/ThreeView', + fillGeometry: 'three/FillGeometry', //models globals: 'models/globals', @@ -94,7 +95,7 @@ require.config({ menuWrapper: 'menus/MenuWrapperView', menuParent: 'menus/MenuParentView', latticeMenu: 'menus/LatticeMenuView', - importMenu: 'menus/LatticeMenuView', + importMenu: 'menus/ImportMenuView', partMenu: 'menus/PartMenuView', scriptMenu: 'menus/ScriptMenuView', physicsMenu: 'menus/PhysicsMenuView', @@ -114,7 +115,7 @@ require.config({ modalViewTemplate: 'menus/templates/ModalView.html', menuWrapperTemplate: 'menus/templates/MenuWrapperView.html', latticeMenuTemplate: 'menus/templates/LatticeMenuView.html', - importMenuTemplate: 'menus/templates/LatticeMenuView.html', + importMenuTemplate: 'menus/templates/ImportMenuView.html', partMenuTemplate: 'menus/templates/PartMenuView.html', scriptMenuTemplate: 'menus/templates/ScriptMenuView.html', physicsMenuTemplate: 'menus/templates/PhysicsMenuView.html', diff --git a/js/menus/ImportMenuView.js b/js/menus/ImportMenuView.js index 19fa26fb..9bbb3b2a 100644 --- a/js/menus/ImportMenuView.js +++ b/js/menus/ImportMenuView.js @@ -2,8 +2,8 @@ * Created by aghassaei on 1/26/15. */ -define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice', 'importMenuTemplate'], - function($, _, MenuParentView, plist, lattice, template){ +define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice', 'text!importMenuTemplate', 'fillGeometry', 'stlLoader'], + function($, _, MenuParentView, plist, lattice, template, FillGeometry, THREE){ return MenuParentView.extend({ diff --git a/js/menus/templates/Ribbon.html b/js/menus/templates/Ribbon.html index 179021b3..509d0625 100644 --- a/js/menus/templates/Ribbon.html +++ b/js/menus/templates/Ribbon.html @@ -5,7 +5,7 @@ <% if (allPartTypes[cellType][connectionType]){ %> <a data-type="part" class="btn btn-primary btn-ribbon ribbonCellMode<% if (cellMode == "part"){ %> ribbon-selected<% } %>" href="#"><img data-type="part" src="assets/imgs/part-sm.png"></a> <% } %> - <!--<a data-type="beam" class="btn btn-primary btn-ribbon ribbonCellMode<% if (cellMode == "beam"){ %> ribbon-selected<% } %>" href="#">Beam</a>--> + <a data-type="beam" class="btn btn-primary btn-ribbon ribbonCellMode<% if (cellMode == "beam"){ %> ribbon-selected<% } %>" href="#">Beam</a> <a class="btn btn-primary btn-ribbon ribbonDeleteMode<% if (deleteMode){ %> ribbon-selected"<% } %>"><span class="fui-cross"></span></a> </div> </div> diff --git a/js/models/AppState.js b/js/models/AppState.js index bdfdcdeb..b282da4c 100644 --- a/js/models/AppState.js +++ b/js/models/AppState.js @@ -83,7 +83,7 @@ define(['underscore', 'backbone', 'threeModel', 'three', 'plist', 'globals'], fu var currentTab = this.get("currentTab"); if (currentTab != "animate") this.set("stockSimulationPlaying", false); if (currentTab != "cam") this.set("manualSelectOrigin", false); - if (currentTab == "import" && globals.lattice.get("connectionType") == "edgeRot") globals.lattice.set("partType", "voxLowPoly"); + if (currentTab == "import" && this.lattice.get("connectionType") == "edgeRot") this.lattice.set("partType", "voxLowPoly"); this.get("lastNavTab")[this.get("currentNav")] = currentTab;//store tab this._updateCellMode(currentTab); }, diff --git a/js/models/PList.js b/js/models/PList.js index 28aa1585..58b11fcf 100644 --- a/js/models/PList.js +++ b/js/models/PList.js @@ -8,7 +8,7 @@ define(['three'], function(THREE){ allMenuTabs: { navDesign:{ lattice:"Lattice", -// import:"Import", + import:"Import", //sketch:"Sketch", material:"Material", part:"Part", diff --git a/js/three/FillGeometry.js b/js/three/FillGeometry.js index 89a8cd0e..5bb69554 100644 --- a/js/three/FillGeometry.js +++ b/js/three/FillGeometry.js @@ -2,133 +2,137 @@ * Created by aghassaei on 1/16/15. */ - -FillGeometry = Backbone.Model.extend({ - - defaults: { - filename: "No File Loaded", - mesh: null, - boundingBox: null//show bounding box for mesh - }, - - initialize: function(){ - - //bind events - }, - - buildNewMesh:function(geometry){ - this.removeMesh(); - - //center geometry in x and y - geometry.computeBoundingBox(); - geometry.applyMatrix(new THREE.Matrix4().makeTranslation(0,0,-geometry.boundingBox.min.z));//set on top of baseplane - geometry.computeBoundingBox(); - - var mesh = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial( - {color:0xf25536, - shading: THREE.FlatShading, - transparent:true, - opacity:0.4, - side:THREE.DoubleSide})); - this.makeBoundingBox(mesh); - this.set({mesh: mesh}); - globals.three.sceneAdd(mesh); - globals.three.render(); - }, - - makeBoundingBox: function(mesh){ - var box = new THREE.BoxHelper(mesh); - box.material.color.setRGB(0,0,0); - box.material.opacity = 0.4; - box.material.transparent = true; - this.set("boundingBox", box); -// globals.three.sceneAdd(box); - }, - - fillGeo: function(){ - var boundingBox = this.get("boundingBox"); - boundingBox.geometry.computeBoundingBox(); - var bounds = boundingBox.geometry.boundingBox; - - var scale = globals.lattice.get("scale"); - - var minIndex = globals.lattice.getIndexForPosition(bounds.min); - var maxIndex = globals.lattice.getIndexForPosition(bounds.max); - globals.lattice.checkForMatrixExpansion(null, maxIndex, minIndex);//expand cells matrix before - - var raycaster = new THREE.Raycaster(); - var direction = new THREE.Vector3(0,0,1); - var mesh = this.get("mesh"); - raycaster.near = 0; - raycaster.far = bounds.max-bounds.min+2;//add some padding just in case - for (var x=minIndex.x;x<=maxIndex.x;x++){ - for (var y=minIndex.y;y<=maxIndex.y;y++){ - var origin = globals.lattice.getPositionForIndex({x:x, y:y, z:minIndex.z}); - origin.z = bounds.min.z-1;//more padding - raycaster.set(origin, direction); - var intersections = raycaster.intersectObject(mesh); - if (intersections.length == 0) continue; - var inside = false; - var nextIntersectionIndex = 0; - var nextIntersection = intersections[nextIntersectionIndex].distance; - for (var z=minIndex.z;z<=maxIndex.z;z++){ - var index = {x:x,y:y,z:z}; - var position = globals.lattice.getPositionForIndex(index); - if (!inside){ - if (position.z<nextIntersection) continue; - else { - inside = true; - globals.lattice.getUItarget().addCellAtIndex(index, true, true); - } - } else { - if (position.z<nextIntersection) { - globals.lattice.getUItarget().addCellAtIndex(index, true, true); - continue; +define(['three', 'underscore', 'backbone', 'lattice', 'threeModel'], + function(THREE, _, Backbone, lattice, three){ + + return Backbone.Model.extend({ + + defaults: { + filename: "No File Loaded", + mesh: null, + boundingBox: null,//show bounding box for mesh + scale: 1 + }, + + initialize: function(){ + + //bind events + }, + + buildNewMesh:function(geometry){ + this.removeMesh(); + + //center geometry in x and y + geometry.computeBoundingBox(); + geometry.applyMatrix(new THREE.Matrix4().makeTranslation(0,0,-geometry.boundingBox.min.z));//set on top of baseplane + geometry.computeBoundingBox(); + + var mesh = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial( + {color:0xf25536, + shading: THREE.FlatShading, + transparent:true, + opacity:0.4, + side:THREE.DoubleSide})); + this.makeBoundingBox(mesh); + this.set({mesh: mesh}); + three.sceneAdd(mesh); + three.render(); + }, + + makeBoundingBox: function(mesh){ + var box = new THREE.BoxHelper(mesh); + box.material.color.setRGB(0,0,0); + box.material.opacity = 0.4; + box.material.transparent = true; + this.set("boundingBox", box); + // three.sceneAdd(box); + }, + + fillGeo: function(){ + var boundingBox = this.get("boundingBox"); + boundingBox.geometry.computeBoundingBox(); + var bounds = boundingBox.geometry.boundingBox; + + var scale = this.get("scale"); + + var minIndex = lattice.getIndexForPosition(bounds.min); + var maxIndex = lattice.getIndexForPosition(bounds.max); + lattice.checkForMatrixExpansion(null, maxIndex, minIndex);//expand cells matrix before + + var raycaster = new THREE.Raycaster(); + var direction = new THREE.Vector3(0,0,1); + var mesh = this.get("mesh"); + raycaster.near = 0; + raycaster.far = bounds.max-bounds.min+2;//add some padding just in case + for (var x=minIndex.x;x<=maxIndex.x;x++){ + for (var y=minIndex.y;y<=maxIndex.y;y++){ + var origin = lattice.getPositionForIndex({x:x, y:y, z:minIndex.z}); + origin.z = bounds.min.z-1;//more padding + raycaster.set(origin, direction); + var intersections = raycaster.intersectObject(mesh); + if (intersections.length == 0) continue; + var inside = false; + var nextIntersectionIndex = 0; + var nextIntersection = intersections[nextIntersectionIndex].distance; + for (var z=minIndex.z;z<=maxIndex.z;z++){ + var index = {x:x,y:y,z:z}; + var position = lattice.getPositionForIndex(index); + if (!inside){ + if (position.z<nextIntersection) continue; + else { + inside = true; + lattice.getUItarget().addCellAtIndex(index, true, true); + } + } else { + if (position.z<nextIntersection) { + lattice.getUItarget().addCellAtIndex(index, true, true); + continue; + } + else inside = false; } - else inside = false; + var next = this._getNextIntersection(position, intersections, nextIntersectionIndex, inside); + if (!next) break; + inside = next.inside; + nextIntersection = next.nextIntersection; + nextIntersectionIndex = next.nextIntersetcionIndex; } - var next = this._getNextIntersection(position, intersections, nextIntersectionIndex, inside); - if (!next) break; - inside = next.inside; - nextIntersection = next.nextIntersection; - nextIntersectionIndex = next.nextIntersetcionIndex; } } + three.render(); + }, + + _getNextIntersection: function(position, intersections, nextIntersectionIndex, inside){ + nextIntersectionIndex += 1; + if (nextIntersectionIndex < intersections.length) { + var nextIntersection = intersections[nextIntersectionIndex].distance; + if (nextIntersection < position.z) return this._getNextIntersection(position, intersections, nextIntersectionIndex, !inside); + return {nextIntersection:nextIntersection, nextIntersetcionIndex: nextIntersectionIndex, inside:inside}; + } + else return null; + }, + + subtractGeo: function(){ + lattice.subtractMesh(this.get("mesh")); + }, + + removeMesh: function(){ + if (!this.get("mesh")) return; + three.sceneRemove(this.get("mesh")); + three.sceneRemove(this.get("boundingBox")); + this.set("mesh", null); + this.set("boundingBox", null); + this.set("filename", this.defaults.filename); + three.render(); + }, + + scale: function(scale){ + // var currentScale = this.get("scale"); + // for (var i=0;i<currentScale.length;i++){ + // if (!scale[i]) scale[i] = currentScale[i]; + // } + // this.get("mesh").scale.set(scale[0], scale[1], scale[2]); + // this.set("scale", scale); } - globals.three.render(); - }, - - _getNextIntersection: function(position, intersections, nextIntersectionIndex, inside){ - nextIntersectionIndex += 1; - if (nextIntersectionIndex < intersections.length) { - var nextIntersection = intersections[nextIntersectionIndex].distance; - if (nextIntersection < position.z) return this._getNextIntersection(position, intersections, nextIntersectionIndex, !inside); - return {nextIntersection:nextIntersection, nextIntersetcionIndex: nextIntersectionIndex, inside:inside}; - } - else return null; - }, - - subtractGeo: function(){ - globals.lattice.subtractMesh(this.get("mesh")); - }, - - removeMesh: function(){ - if (!this.get("mesh")) return; - globals.three.sceneRemove(this.get("mesh")); - globals.three.sceneRemove(this.get("boundingBox")); - this.set("mesh", null); - this.set("boundingBox", null); - this.set("filename", this.defaults.filename); - globals.three.render(); - }, - - scale: function(scale){ -// var currentScale = this.get("scale"); -// for (var i=0;i<currentScale.length;i++){ -// if (!scale[i]) scale[i] = currentScale[i]; -// } -// this.get("mesh").scale.set(scale[0], scale[1], scale[2]); -// this.set("scale", scale); - } + }); }); -- GitLab