diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js index 7b34b746a059f5a9b068f8f0224c312a76cf1aa4..df65540700e8a75feecae155e9b87e3db3381e70 100644 --- a/js/cells/DMACell.js +++ b/js/cells/DMACell.js @@ -232,18 +232,12 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', ' console.warn("no material type set for cell"); return null; } - if (!materials.list[this.materialName]) { - console.warn("no material object found of type "); - console.warn(this.materialName); + var material = materials.getMaterialForId(this.materialName, returnTHREEObject, this.isTransparent); + if (!material) { + console.warn("no material object found of type " + id); return null; } - if (!returnTHREEObject) return materials.list[this.materialName]; - if (!materials.list[this.materialName].threeMaterial){ - console.warn("no three material object found for type "+ this.materialName); - return null; - } - if (this.isTransparent) return materials.list[this.materialName].transparentMaterial; - return materials.list[this.materialName].threeMaterial; + return material; }; DMACell.prototype.setMaterial = function(material){ @@ -489,7 +483,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', ' var data = { materialName: this.materialName }; -// if (materials.list[this.materialName].sparseCells) return data;//material definition in material composites +// if (materials.getMaterialForID(this.materialName).sparseCells) return data;//material definition in material composites // if (this.cells) data.cells = this.cells; return data; }; diff --git a/js/cells/supercells/CompositeCell.js b/js/cells/supercells/CompositeCell.js index 0547bbee1e933e05d50eea93412b42d78d450686..9720a4d8c3472606db5c045fbee35664ce713f68 100644 --- a/js/cells/supercells/CompositeCell.js +++ b/js/cells/supercells/CompositeCell.js @@ -12,7 +12,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'superCell', CompositeCell.prototype = Object.create(DMASuperCell.prototype); CompositeCell.prototype._getGeometry = function(){ - var dimensions = materials.list[this.materialName].dimensions; + var dimensions = materials.getMaterialForId(this.materialName).dimensions; var geo = new THREE.BoxGeometry(dimensions.x*lattice.xScale(), dimensions.y*lattice.yScale(), dimensions.z*lattice.zScale()); geo.applyMatrix(new THREE.Matrix4().makeTranslation((dimensions.x/2-0.5)*lattice.xScale(), (dimensions.y/2-0.5)*lattice.yScale(), (dimensions.z/2-0.5)*lattice.zScale())); return geo; diff --git a/js/materials/DMAMaterials.js b/js/materials/DMAMaterials.js index 9d0d99ad90e667735b1ed42d86eeae97772107f6..bf1810224bf8e15bdd995739333d06f51af12e64 100644 --- a/js/materials/DMAMaterials.js +++ b/js/materials/DMAMaterials.js @@ -57,8 +57,6 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu lattice.reinitAllCellsOfTypes(allChangedMaterialsList); } - - return false; } function deleteMaterial(id){ @@ -76,8 +74,41 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu return deleted; } - function getMaterialForId(id){ - return materialsList[id]; + function getMaterialForId(id, returnTHREEObject, transparent){ + var material = materialsList[id]; + if (!returnTHREEObject) return material; + if (!material.threeMaterial){ + console.warn("no three material object found for type "+ id); + return null; + } + if (transparent) return material.transparentMaterial; + return material.threeMaterial; + } + + var materialNameIndex = 1; + + function getMaterialCopy(id){ + if (materialsList[id]){ + return JSON.parse(JSON.stringify(toJSON(id))); + } + return { + name: "Material " + materialNameIndex++, + color: '#000000', + altColor: '#000000', + noDelete: false, + properties: {} + }; + } + + function toJSON(id){ + var material = materialsList[id]; + return { + name: material.name, + color: material.color, + altColor: material.altColor, + noDelete: material.noDelete, + properties: material.properties + } } function getDeleteMaterial(){ @@ -257,12 +288,14 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu setMaterial: setMaterial, deleteMaterial: deleteMaterial, getMaterialForId: getMaterialForId, + getMaterialCopy: getMaterialCopy, getCompositeKeys: getCompositeKeys, getVaildAvailableCompositeKeys: getVaildAvailableCompositeKeys, getChildCellTypes:getChildCellTypes, setToDefaultMaterial: setToDefaultMaterial, getDeleteMaterial: getDeleteMaterial, getNextCompositeID: getNextCompositeID, - getNextMaterialID: getNextMaterialID + getNextMaterialID: getNextMaterialID, + toJSON: toJSON }; }); \ No newline at end of file diff --git a/js/menus/CompositeMenuView.js b/js/menus/CompositeMenuView.js index 8ae3da366360fcf520b23b24ec5603db7dffea3e..3daed9ec797f477db706e88dde43268c529c139d 100644 --- a/js/menus/CompositeMenuView.js +++ b/js/menus/CompositeMenuView.js @@ -78,8 +78,9 @@ define(['jquery', 'underscore', 'menuParent', 'compositeEditorLattice', 'plist', _saveCompositeToFile: function(e){ e.preventDefault(); + var self = this; require(['fileSaver'], function(fileSaver){ - fileSaver.saveMaterial(this.composite.get("id"), this.composite.toJSONForSave(bounds)); + fileSaver.saveMaterial(self.composite.get("id"), self.composite.toJSONForSave(bounds));//todo save children too }); }, diff --git a/js/menus/MaterialEditorMenuView.js b/js/menus/MaterialEditorMenuView.js index d7e0a224a4e54687246670af507b0f4284c41bc3..3cdaf15ac53ce73757f29b993543b2854130151e 100644 --- a/js/menus/MaterialEditorMenuView.js +++ b/js/menus/MaterialEditorMenuView.js @@ -6,8 +6,6 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'materials', 'text!materialEditorMenuTemplate', 'fileSaver'], function($, _, MenuParentView, plist, materials, template, fileSaver){ - var materialNameIndex = 1; - return MenuParentView.extend({ events: { @@ -25,16 +23,7 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'materials', 'text!materi if (id == "new") id = materials.getNextMaterialID(); this.materialID = id; - var material = _.clone(materials.getMaterialForId(options.myObject));//todo do this in dmamaterials - if (!material) material = { - name: "Material " + materialNameIndex++, - color: '#000000', - altColor: '#000000', - noDelete: false, - properties: {} - }; - this.material = material; - + this.material = materials.getMaterialCopy(id); }, getPropertyOwner: function($target){ diff --git a/js/models/FileSaver.js b/js/models/FileSaver.js index 5015d44367b6f256c1de6fb76bc5fa6718c5a3e5..61d79d3235044ef7f569518f6dc9d3ea8e64f9df 100644 --- a/js/models/FileSaver.js +++ b/js/models/FileSaver.js @@ -84,7 +84,7 @@ define(['underscore', 'fileSaverLib', 'lattice', 'materials', 'ribbon', 'menuWra } function _getMaterialDataToSave(id){ - return _.omit(materials.list[id], "threeMaterial", "transparentMaterial"); + return materials.toJSON(id); } function loadFile(data){//parsed json todo make this better - load composite diff --git a/todo b/todo index 4f82b6a5ecec694fc6d86c6055861c865e8faea1..891900bda8a66fd80b20601b07f7cf304dd92d74 100644 --- a/todo +++ b/todo @@ -1,10 +1,11 @@ import stl fill - offset, scale, rotation -hierarchical +composite - origin - rotation - change bounding box - - placement ui 3D + - placement ui 3D could be better + - need material class material transformations -> change material of a cell, change in bulk