From d506fe0a3253537a59033e0a2bdf28f0030faa3b Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Wed, 28 Jan 2015 18:09:00 -0500
Subject: [PATCH] part scaling

---
 js/fea/dmaCell.js | 31 ++++++++++++++++++-------------
 js/fea/dmaPart.js | 30 ++++++++++++++++++++++--------
 2 files changed, 40 insertions(+), 21 deletions(-)

diff --git a/js/fea/dmaCell.js b/js/fea/dmaCell.js
index bc8a5fec..bb41d71b 100644
--- a/js/fea/dmaCell.js
+++ b/js/fea/dmaCell.js
@@ -34,20 +34,8 @@
         globalCellScale = scale;
         cellGeometry1 = unitCellGeo1.clone();
         cellGeometry1.applyMatrix(new THREE.Matrix4().makeScale(scale, scale, scale));
-        setFlags(cellGeometry1);
         cellGeometry2 = unitCellGeo2.clone();
         cellGeometry2.applyMatrix(new THREE.Matrix4().makeScale(scale, scale, scale));
-        setFlags(cellGeometry2);
-    }
-
-    function setFlags(geometry){
-        geometry.verticesNeedUpdate = true;
-        geometry.elementsNeedUpdate = true;
-        geometry.morphTargetsNeedUpdate = true;
-        geometry.uvsNeedUpdate = true;
-        geometry.normalsNeedUpdate = true;
-        geometry.colorsNeedUpdate = true;
-        geometry.tangentsNeedUpdate = true;
     }
 
     function DMACell(mode, indices, scale) {
@@ -125,7 +113,17 @@
     };
 
     DMACell.prototype.changeScale = function(scale){
-        if (globalCellScale != scale) setGlobalCellScale(scale);
+
+        //update geometry
+        if (globalCellScale != scale) {
+            setGlobalCellScale(scale);
+        }
+        if (this.indices.z%2==0){
+            this._updateVertices(cellGeometry1.vertices);
+        } else {
+            this._updateVertices(cellGeometry2.vertices);
+        }
+
         var position = this._calcPosition(scale, this.indices);
         this._setMeshPosition(this.cellMesh, position);
         _.each(this.parts, function(part){
@@ -133,6 +131,13 @@
          });
     };
 
+    DMACell.prototype._updateVertices = function(vertices){
+        _.each(this.cellMesh.children, function(mesh){
+            mesh.geometry.vertices = vertices;
+            mesh.geometry.verticesNeedUpdate = true;
+        });
+    };
+
     DMACell.prototype.remove = function(){
         if (this.cellMesh) window.three.sceneRemove(this.cellMesh);
     };
diff --git a/js/fea/dmaPart.js b/js/fea/dmaPart.js
index 28d0ef61..0901841e 100644
--- a/js/fea/dmaPart.js
+++ b/js/fea/dmaPart.js
@@ -12,16 +12,25 @@
     loader.addEventListener('load', onMeshLoad);
     loader.load("data/trianglePart.stl");
 
+    var unitPartGeo;
     var partGeometry;
+    var globalPartScale = 30;
     function onMeshLoad(e){
         console.log("part loaded");
-        partGeometry = e.content;
-        partGeometry.computeBoundingBox();
-        var unitScale = 1/partGeometry.boundingBox.max.y;
-        partGeometry.applyMatrix(new THREE.Matrix4().makeScale(unitScale, unitScale, unitScale));
-        partGeometry.applyMatrix(new THREE.Matrix4().makeTranslation(0.2,-0.5, 0));
-        partGeometry.applyMatrix(new THREE.Matrix4().makeRotationZ(-Math.PI/6));
-        partGeometry.applyMatrix(new THREE.Matrix4().makeScale(30,30,30));
+        unitPartGeo = e.content;
+        unitPartGeo.dynamic = true;
+        unitPartGeo.computeBoundingBox();
+        var unitScale = 1/unitPartGeo.boundingBox.max.y;
+        unitPartGeo.applyMatrix(new THREE.Matrix4().makeScale(unitScale, unitScale, unitScale));
+        unitPartGeo.applyMatrix(new THREE.Matrix4().makeTranslation(0.2,-0.5, 0));
+        unitPartGeo.applyMatrix(new THREE.Matrix4().makeRotationZ(-Math.PI/6));
+        setGlobalPartScale(globalPartScale)
+    }
+
+    function setGlobalPartScale(scale){
+        globalPartScale = scale;
+        partGeometry = unitPartGeo.clone();
+        partGeometry.applyMatrix(new THREE.Matrix4().makeScale(scale,scale,scale));
     }
 
     function DMAPart(type, position, oddZFlag) {
@@ -65,8 +74,13 @@
     };
 
     DMAPart.prototype.changeScale = function(scale, position){
+        if (globalPartScale != scale) setGlobalPartScale(scale);
         this.position = position;
-        if (this.mesh) this._setMeshPosition(this.mesh, scale, position);
+        if (this.mesh) {
+            this._setMeshPosition(this.mesh, scale, position);
+            this.mesh.geometry.vertices = partGeometry.vertices;
+            this.mesh.geometry.verticesNeedUpdate = true;
+        }
     };
 
     DMAPart.prototype.show = function(){
-- 
GitLab