From bd6d41fd58c6c3b8ef90a54735313b0d18151fd4 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Mon, 1 Jun 2015 10:34:48 -0700
Subject: [PATCH] basplane has object3d

---
 js/three/BasePlane.js   | 40 +++++++++++++---------------------------
 js/three/Highlighter.js |  7 +++++--
 js/three/ThreeModel.js  | 17 ++++++++++++++++-
 3 files changed, 34 insertions(+), 30 deletions(-)

diff --git a/js/three/BasePlane.js b/js/three/BasePlane.js
index d561e29e..96a22a05 100644
--- a/js/three/BasePlane.js
+++ b/js/three/BasePlane.js
@@ -7,7 +7,7 @@ BasePlane = Backbone.Model.extend({
 
     defaults: {
         zIndex: 0,
-        mesh: [],
+        object3D: null,
         dimX: 100,
         dimY: 100,
         material: new THREE.MeshBasicMaterial({color:0x000000, transparent:true, opacity:0.2, wireframe:true})
@@ -20,12 +20,14 @@ BasePlane = Backbone.Model.extend({
         this.listenTo(globals.appState, "change:basePlaneIsVisible", this._setVisibility);
 
         //draw mesh
-        this.set("mesh", this._makeBasePlaneMesh());
-
-        var self = this;
-        _.each(this.get("mesh"), function(mesh){
-            globals.three.sceneAdd(mesh, self._checkIsHighlightable(mesh));
+        var meshes = this._makeBasePlaneMesh();
+        var object3D = new THREE.Object3D();
+        _.each(meshes, function(mesh){
+            object3D.add(mesh);
         });
+        object3D.myParent = this;
+        this.set("object3D", object3D);
+        globals.three.sceneAdd(object3D, "basePlane");
         globals.three.render();
         this._setVisibility();
     },
@@ -37,31 +39,17 @@ BasePlane = Backbone.Model.extend({
     },
 
     _setVisibility: function(){
-        _.each(this.get("mesh"), function(mesh){
-            mesh.visible = globals.appState.get("basePlaneIsVisible");
-        });
+        this.get("object3D").visible = globals.appState.get("basePlaneIsVisible");
         globals.three.render();
     },
 
-    ///////////////////////////////////////////////////////////////////////////////////
-    //////////////////////HIGHLIGHTER FUNCTIONALITY////////////////////////////////////
-    ///////////////////////////////////////////////////////////////////////////////////
-
-    _checkIsHighlightable: function(mesh){
-        if (mesh.type == "Mesh") return "basePlane";//don't try to highlight wireframe parts of baseplane
-        return null;
-    },
-
     ///////////////////////////////////////////////////////////////////////////////////
     ////////////////////////////////////////DEALLOC////////////////////////////////////
     ///////////////////////////////////////////////////////////////////////////////////
 
     _removeMesh: function(){
-        var self = this;
-        _.each(this.get("mesh"), function(mesh){
-            if (mesh.myParent) mesh.myParent = null;
-            globals.three.sceneRemove(mesh, self._checkIsHighlightable(mesh));
-        });
+        this.get("object3D").myParent = null;
+        globals.three.sceneRemove(this.get("object3D"), "basePlane");
         globals.three.render();
     },
 
@@ -69,7 +57,7 @@ BasePlane = Backbone.Model.extend({
         this.stopListening();
         this.set("zIndex", null, {silent:true});
         this._removeMesh();
-        this.set("mesh", null, {silent:true});
+        this.set("object3D", null, {silent:true});
         this.set("material", null, {silent:true});
         this.set("unitGeometry", null, {silent:true});
         this.set("dimX", null, {silent:true});
@@ -113,9 +101,7 @@ OctaBasePlane = BasePlane.extend({
         }
 
         geometry.computeFaceNormals();
-        var mesh = new THREE.Mesh(geometry, this.get("material"));
-        mesh.myParent = this;//reference used for intersection highlighting
-        return [mesh];
+        return [THREE.Mesh(geometry, this.get("material"))];
     },
 
     getType: function(){//todo hack from freeform octa, get rid of this eventually
diff --git a/js/three/Highlighter.js b/js/three/Highlighter.js
index 93a59b8d..2be2eb50 100644
--- a/js/three/Highlighter.js
+++ b/js/three/Highlighter.js
@@ -21,7 +21,7 @@ Highlighter = Backbone.View.extend({
 //                vertexColors:THREE.FaceColors
             }));
 
-        globals.three.sceneAdd(this.mesh, null);
+        globals.three.sceneAdd(this.mesh, "highlighter");
         this.hide();
 
         //bind events
@@ -66,7 +66,10 @@ Highlighter = Backbone.View.extend({
         if (!intersection.object) return;
         var highlighted = intersection.object;
         if (!(highlighted.parent instanceof THREE.Scene)) highlighted = highlighted.parent;//cell mesh parent is object3d
-        if (!highlighted.myParent) console.warn("no parent for highlighted object");
+        if (!highlighted.myParent) {
+            console.warn("no parent for highlighted object");
+            return;
+        }
 
         this.highlightedObject = highlighted.myParent;
 
diff --git a/js/three/ThreeModel.js b/js/three/ThreeModel.js
index a3a13c63..ceb13b38 100644
--- a/js/three/ThreeModel.js
+++ b/js/three/ThreeModel.js
@@ -53,6 +53,7 @@ function ThreeModel(){
     }
 
     function sceneAdd(object, type){
+
         scene.add(object);
 
         if (type == "cell"){
@@ -65,7 +66,12 @@ function ThreeModel(){
         } else if (type == "part"){//todo change this
 //            parts.push(object);
         } else if (type == "basePlane"){
-            basePlane.push(object);
+            basePlane.push(object.children[0]);
+        } else if (type == "highlighter"){
+
+        } else {
+            console.warn("no recognized type " + type + " for");
+//            console.log(object);
         }
     }
 
@@ -73,11 +79,20 @@ function ThreeModel(){
 
         if (type == "cell"){
             cells.splice(cells.indexOf(object.children[0]), 1);
+        } else if (type == "supercell"){
+            _.each(object.children, function(child){//add cells as hover
+                if (child.name != "object3D") return;
+                cells.splice(cells.indexOf(child.children[0]), 1);
+            });
         } else if (type == "part"){
 //            parts.splice(parts.indexOf(object), 1);
         } else if (type == "basePlane"){
             basePlane.splice(0, basePlane.length);//delete array without removing reference
+        } else {
+//            console.warn("no recognized type " + type + " for");
+//            console.log(object);
         }
+
         scene.remove(object);
     }
 
-- 
GitLab