コード例 #1
0
        internal NodeObject MakeNode(string label, Vector3 position, Color color)
        {
            GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);

            sphere.transform.position   = position;
            sphere.transform.localScale = Vector3.one * (float)System.Math.Max(0.1, nodeScale) * this.modelScale;
            MeshRenderer renderer = sphere.GetComponent <MeshRenderer>();

            renderer.material.color = color;
            Rigidbody body = sphere.AddComponent <Rigidbody>();

            body.mass        = 0.05F;
            body.angularDrag = 0.05f;
            body.useGravity  = false; // turn off gravity
            body.drag        = 0.8f;
            SphereCollider collider = sphere.GetComponent <SphereCollider>();

            collider.material.bounciness = 1.0F;
            sphere.AddComponent <NVRCollisionSoundObject>();
            NVRInteractableItem interactable = sphere.AddComponent <NVRInteractableItem>();

            interactable.CanAttach = true;
            interactable.DisableKinematicOnAttach = true;
            interactable.EnableKinematicOnDetach  = false;
            interactable.EnableGravityOnDetach    = false;
            NodeObject node = sphere.AddComponent <NodeObject>();

            node.label = label;
            return(node);
        }
コード例 #2
0
 public Model(string name, float scale, List <LabelAction> labelActions, NodeObject[] nodeObjects, EdgeObject[] edgeObjects)
 {
     this.name   = name;
     this.scale  = scale;
     this.labels = new Label[labelActions.Count];
     this.nodes  = new Node[nodeObjects.Length];
     this.edges  = new Edge[edgeObjects.Length];
     for (int i = 0; i < labels.Length; i++)
     {
         this.labels[i] = new Label(i, labelActions[i]);
         this.knownLabels[labelActions[i].name] = this.labels[i];
     }
     for (int i = 0; i < nodes.Length; i++)
     {
         NodeObject obj     = nodeObjects[i];
         Label      label   = (obj.label != null && knownLabels.ContainsKey(obj.label)) ? knownLabels[obj.label] : null;
         int        labelId = (label == null) ? FindLabelIdByColor(labels, nodeObjects[i]) : label.labelId;
         this.nodes[i] = new Node(i, labelId, nodeObjects[i]);
         this.knownNodes[nodeObjects[i]] = this.nodes[i];
     }
     for (int i = 0; i < edges.Length; i++)
     {
         Node startNode = knownNodes[edgeObjects[i].startNode];
         Node endNode   = knownNodes[edgeObjects[i].endNode];
         this.edges[i] = new Edge(startNode.nodeId, endNode.nodeId);
     }
 }
コード例 #3
0
        internal EdgeObject MakeConnection(NodeObject first, NodeObject second)
        {
            Debug.Log("Making connection from " + first + " to " + second);
            GameObject connection = GameObject.CreatePrimitive(PrimitiveType.Cube);

            DestroyColliders(connection);
            MeshRenderer renderer = connection.GetComponent <MeshRenderer>();

            renderer.material.color         = Color.gray;
            connection.transform.localScale = Vector3.one * edgeScale;  // make very small at first
            EdgeObject edge = connection.AddComponent <EdgeObject>();

            edge.floatingSpheres = this;
            edge.startNode       = first;
            edge.endNode         = second;
            edge.scale           = edgeScale;
            edge.UpdateConnection();
            first.AddEdge(edge);
            second.AddEdge(edge);
            if (edges != null)
            {
                connection.transform.parent = edges.transform;
            }
            return(edge);
        }
コード例 #4
0
 public void TriggerPressed(InteractorFacade hand)
 {
     if (this.createMode)
     {
         NodeObject grabbedNode      = GrabbedNode(hand);
         NodeObject otherGrabbedNode = OtherGrabbedNode(hand);
         if (grabbedNode != null && otherGrabbedNode != null)
         {
             MakeConnection(grabbedNode, otherGrabbedNode);
         }
         else
         {
             NodeObject node = TriggerCreate(head.transform, WhichHand(hand).transform);
             if (grabbedNode != null)
             {
                 MakeConnection(grabbedNode, node);
             }
             if (otherGrabbedNode != null)
             {
                 MakeConnection(otherGrabbedNode, node);
             }
         }
     }
     else if (hovering != null)
     {
         hovering.Detach();
         Destroy(hovering.gameObject, 0.1f);
         hovering = null;
     }
 }
コード例 #5
0
        private void PullNode(NodeObject sphere, float factor, NodeObject parent)
        {
            Rigidbody rigid = sphere.GetComponent <Rigidbody>();

            if (rigid == null)
            {
                Debug.LogError("Sphere has no rigidbody, cannot pull it: " + sphere);
            }
            else
            {
                Vector3 move = (rightController.transform.position - sphere.transform.position) * factor;
                Debug.Log("Pulling node by " + move);
                rigid.velocity = rigid.velocity + move;
                if (factor > 0.1)
                {
                    foreach (NodeObject other in sphere.OtherNodes())
                    {
                        if (other != parent)
                        {
                            PullNode(other, factor / 2f, sphere);
                        }
                    }
                }
            }
        }
コード例 #6
0
 private void NormalizeNode(NodeObject node)
 {
     if (node != null && previousColor != null)
     {
         MeshRenderer renderer = node.GetComponentInChildren <MeshRenderer>();
         renderer.material.color = previousColor;
     }
 }
コード例 #7
0
 public virtual void PointerExited(TransformData destination)
 {
     if (hovering != null)
     {
         Debug.Log("Exiting hovering of node: " + hovering);
         NormalizeNode(hovering);
         hovering = null;
     }
 }
コード例 #8
0
        internal EdgeObject FindEdge(NodeObject other)
        {
            EdgeObject edge = FindEdge(other, this, incoming);

            if (edge == null)
            {
                edge = FindEdge(this, other, outgoing);
            }
            return(edge);
        }
コード例 #9
0
 private void HighlightNode(NodeObject node)
 {
     if (node != null)
     {
         MeshRenderer renderer = node.GetComponentInChildren <MeshRenderer>();
         previousColor = renderer.material.color;
         Color color = renderer.material.color;
         color.a = 0.5f;
         color.b = 1f;
         color.g = 1f;
         renderer.material.color = color;
     }
 }
コード例 #10
0
 private EdgeObject FindEdge(NodeObject first, NodeObject second, HashSet <EdgeObject> edges)
 {
     HashSet <EdgeObject> .Enumerator edge = edges.GetEnumerator();
     while (edge.MoveNext())
     {
         EdgeObject current = edge.Current;
         if (current.endNode == second && current.startNode == first)
         {
             return(current);
         }
     }
     return(null);
 }
コード例 #11
0
 private NodeObject GrabbedNode(InteractorFacade hand)
 {
     foreach (GameObject obj in hand.GrabbedObjects)
     {
         Debug.Log("Trigger pressed while object is grabbed: " + obj);
         NodeObject grabbedNode = obj.GetComponent <NodeObject>();
         if (grabbedNode != null)
         {
             return(grabbedNode);
         }
     }
     return(null);
 }
コード例 #12
0
 public virtual void PointerEntered(TransformData destination)
 {
     if (hovering != null)
     {
         PointerExited(destination);
     }
     Debug.Log("Searching for hovering sphere at: " + destination.Position);
     hovering = FindNodeAt(destination);
     if (hovering != null)
     {
         Debug.Log("Entering hovering of node: " + hovering);
         HighlightNode(hovering);
     }
 }
コード例 #13
0
        public NodeObject MakeOne(float x, float y, float z)
        {
            NodeObject node = MakeNode("unknown", new Vector3(x, y, z), GetColor());

            if (selectLabels != null)
            {
                LabelAction selectedLabel = selectLabels.GetSelectedLabelAction();
                if (selectedLabel != null)
                {
                    node.label = selectedLabel.name;
                }
            }
            //DebugSphere(node, "Created");
            return(node);
        }
コード例 #14
0
        public virtual void Selected(TransformData destination)
        {
            DateTime now = System.DateTime.Now;

            if (now - lastSelectionTime > TimeSpan.FromSeconds(0.2))
            {
                lastSelectionTime = now;
                Debug.Log("Searching for selected sphere at: " + destination.Position);
                pulling = null;
                NodeObject node = FindNodeAt(destination);
                if (node != null)
                {
                    PullNode(node, 0.5f, null);
                }
            }
        }
コード例 #15
0
            private int FindLabelIdByColor(Label[] labels, NodeObject nodeObject)
            {
                MeshRenderer renderer = nodeObject.GetComponent <MeshRenderer>();
                Color        color    = renderer.material.color;

                Debug.Log("Searching for label by color: " + color);
                foreach (Label label in labels)
                {
                    if (label.color.Equals(color))
                    {
                        Debug.Log("Found label: " + label);
                        return(label.labelId);
                    }
                }
                Debug.Log("Found no label matching color: " + color);
                return(-1);
            }
コード例 #16
0
        internal void ImportModel(string name)
        {
            Debug.Log("Importing model: " + name);
            ClearModel();
            string filename = ModelFileName(directory, name);

            Debug.Log("Importing model from: " + filename);
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Model));

            System.IO.FileStream file = System.IO.File.Open(filename, System.IO.FileMode.Open);
            Model model = (Model)ser.ReadObject(file);

            file.Close();
            floatingSpheres.modelScale = model.scale;
            Dictionary <Node, NodeObject> nodes = new Dictionary <Node, NodeObject>();

            foreach (Label label in model.labels)
            {
                floatingSpheres.selectLabels.AddLabel(label.labelId, label.name, label.color);
            }
            foreach (Node node in model.nodes)
            {
                string labelName = null;
                Color  color     = UnityEngine.Random.ColorHSV();
                if (node.labelId >= 0 && node.labelId < model.labels.Length)
                {
                    Label label = model.labels[node.labelId];
                    labelName = label.name;
                    color     = label.color;
                }
                NodeObject nodeObject = floatingSpheres.MakeNode(labelName, node.position, color);
                nodes[node] = nodeObject;
            }
            foreach (Edge edge in model.edges)
            {
                Node startNode = model.nodes[edge.startNode];
                Node endNode   = model.nodes[edge.endNode];
                floatingSpheres.MakeConnection(nodes[startNode], nodes[endNode]);
            }
        }
コード例 #17
0
        private void UpdateNodePositions(NodeObject startNode, NodeObject endNode)
        {
            Vector3 startPoint = startNode.gameObject.transform.position;
            Vector3 endPoint   = endNode.gameObject.transform.position;
            Vector3 offset     = endPoint - startPoint;
            float   length     = offset.magnitude;

            if (length > maxLength)
            {
                //Debug.LogError("Node gap too large: " + length);
            }
            else
            {
                float shiftBy = (length - targetLength * floatingSpheres.modelScale) * 0.05f;
                if (System.Math.Abs(shiftBy) > 0.001d)
                {
                    Vector3 shift = new Vector3(offset.x * shiftBy, offset.y * shiftBy, offset.z * shiftBy);
                    startNode.gameObject.transform.position = startPoint + shift;
                    endNode.gameObject.transform.position   = endPoint - shift;
                }
            }
        }
コード例 #18
0
        internal NodeObject MakeNode(string label, Vector3 position, Color color)
        {
            Debug.Log("Making a sphere from the template: " + template);
            GameObject sphere = Instantiate(template.gameObject);

            sphere.transform.parent     = this.spheres.transform;
            sphere.name                 = "Node " + spheres.transform.childCount;
            sphere.transform.position   = position;
            sphere.transform.localScale = Vector3.one * (float)System.Math.Max(0.1, nodeScale) * this.modelScale;
            MeshRenderer renderer = sphere.GetComponentInChildren <MeshRenderer>();

            renderer.material.color = color;
            Rigidbody body = sphere.GetComponentInChildren <Rigidbody>();

            body.mass        = 0.05F;
            body.angularDrag = 0.05f;
            body.useGravity  = false; // turn off gravity
            body.drag        = 0.8f;
            SphereCollider collider = sphere.GetComponentInChildren <SphereCollider>();

            collider.material.bounciness = 1.0F;
            sphere.AddComponent <NVRCollisionSoundObject>();

            /*
             * NVRInteractableItem interactable = sphere.AddComponent<NVRInteractableItem>();
             * interactable.CanAttach = true;
             * interactable.DisableKinematicOnAttach = true;
             * interactable.EnableKinematicOnDetach = false;
             * interactable.EnableGravityOnDetach = false;
             */
            NodeObject node = sphere.AddComponent <NodeObject>();

            node.label = label;
            sphere.SetActive(true);
            return(node);
        }
コード例 #19
0
 internal void DeleteObjectAt(NVRHand hand)
 {
     Debug.Log("Deleting object interacting with " + hand);
     if (hand.IsInteracting)
     {
         //DebugSphere(hand.CurrentlyInteracting, "Interacting with");
         NodeObject sphere = hand.CurrentlyInteracting.GetComponent <NodeObject>();
         if (sphere == null)
         {
             Debug.Log("Hand is interacting with something that is not a NodeObject: " + hand.CurrentlyInteracting);
         }
         else
         {
             Debug.Log("Deleting sphere " + sphere);
             hand.EndInteraction(hand.CurrentlyInteracting);
             sphere.Detach();
             Destroy(sphere.gameObject, 0.1f);
         }
     }
     else if (hand.IsHovering)
     {
         Debug.Log("No interacting object, but hand is hovering");
     }
 }
コード例 #20
0
 public Node(int nodeId, int labelId, NodeObject node)
 {
     this.nodeId   = nodeId;
     this.labelId  = labelId;
     this.position = node.transform.position;
 }
コード例 #21
0
 public void FixedUpdate()
 {
     if (hand != null)
     {
         if (hand.Inputs[NVRButtons.ApplicationMenu].PressDown)
         {
             appMenuWasPressed = true;
         }
         if (hand.Inputs[NVRButtons.ApplicationMenu].PressUp)
         {
             if (appMenuWasPressed)
             {
                 MenuPressed();
             }
             appMenuWasPressed = false;
         }
         if (hand.UseButtonDown)
         {
             //if(!floatingSpheres.canvasInput.OnCanvas)
             {
                 triggerWasPressed = true;
             }
         }
         if (hand.UseButtonUp)
         {
             if (triggerWasPressed)// && !floatingSpheres.canvasInput.OnCanvas)
             {
                 TriggerPressed();
             }
             triggerWasPressed = false;
         }
         if (hand.IsInteracting)
         {
             this.twoHandInteraction = false;
             if (otherHand != null)
             {
                 if (otherHand.IsInteracting)
                 {
                     this.twoHandInteraction = true;
                     if (otherHand.CurrentlyInteracting == hand.CurrentlyInteracting)
                     {
                         // NewtonVR already filters this out, so we would need more changes to get to this point
                         Debug.Log("Two hands interacting with same object: " + hand.CurrentlyInteracting);
                     }
                     else
                     {
                         //FloatingSpheres.DebugSphere(hand.CurrentlyInteracting, "Interacting with");
                         NodeObject first  = hand.CurrentlyInteracting.GetComponent <NodeObject>();
                         NodeObject second = otherHand.CurrentlyInteracting.GetComponent <NodeObject>();
                         if (first == null)
                         {
                             Debug.Log("Hand is interacting with something that is not a NodeObject: " + hand.CurrentlyInteracting);
                         }
                         else if (second == null)
                         {
                             Debug.Log("Other hand is interacting with something that is not a NodeObject: " + hand.CurrentlyInteracting);
                         }
                         else
                         {
                             EdgeObject edge = first.FindEdge(second);
                             if (edge == null)
                             {
                                 Debug.Log("No edge exists - making one");
                                 edge = floatingSpheres.MakeConnection(first, second);
                             }
                         }
                     }
                 }
             }
         }
     }
     else
     {
         Debug.LogError("No hand defined for " + this);
     }
     //Debug.Log("FixedUpdate: " + this);
 }