Exemple #1
0
    public void ChangeSelectionPlaneVisualizationMethod()
    {
        /* Change the visualization method of the navigation, i.e. arrows and color circle */
        if (selection_visualization_ == SelectionVisualizationMethod.ARROWS)
        {
            selection_visualization_ = SelectionVisualizationMethod.COLOR_CIRCLE;
        }
        else
        {
            selection_visualization_ = SelectionVisualizationMethod.ARROWS;
        }

        if (selection_plane_previous_ != null)
        {
            SelectionPlaneSpheres   planes = selection_plane_previous_.GetComponent <SelectionPlaneSpheres>();
            SelectionPlaneCylinders planec = selection_plane_previous_.GetComponent <SelectionPlaneCylinders>();
            if (planes != null)
            {
                planes.ChangeVisualization();
            }
            if (planec != null)
            {
                planec.ChangeVisualization();
            }
        }
    }
Exemple #2
0
    /* Spawn a selection sphere for 2D navigation for the currently selected atom */
    private void SpawnSelectionPlaneSpheres()
    {
        if (selection_plane_previous_ != null)
        {
            Destroy(selection_plane_previous_);
        }

        selection_plane_previous_ = Instantiate(prefab_selection_plane_spheres_, selected_atom_.transform.position, Quaternion.identity);
        selection_plane_previous_.transform.parent = transform;
        SelectionPlaneSpheres plane = selection_plane_previous_.GetComponent <SelectionPlaneSpheres>();

        plane.visualization  = selection_visualization_;
        plane.center_sphere_ = selected_atom_;

        /* Get and set the spheres within the selection radius */
        ClearHighlighted();
        Collider[] hitColliders = Physics.OverlapSphere(selected_atom_.transform.position, SELECTION_MODE_SPHERE_RADIUS);
        foreach (Collider c in hitColliders)
        {
            ISphere s = c.gameObject.GetComponent <ISphere>();
            if (s == null || s == selected_atom_)
            {
                continue;
            }

            plane.AddSphere(s);
        }
    }
Exemple #3
0
    /* Gradually move the model towards or away from the user, given a selected object, atom or bond */
    private void MoveTowardsSelectedObject(float speed)
    {
        if (selection_plane_previous_ == null)
        {
            return;
        }
        /* Calculate desired position of the selected object, just in front of the camera */
        SelectionPlaneSpheres   plane1 = selection_plane_previous_.GetComponent <SelectionPlaneSpheres>();
        SelectionPlaneCylinders plane2 = selection_plane_previous_.GetComponent <SelectionPlaneCylinders>();

        /* Calculate the desired distance that we want the selected object to be in front of the user */
        float   distance = 0;
        Vector3 target;

        if (plane1 != null)
        {
            target = plane1.center_sphere_.transform.position;
            if (GetVisualizationMethod() == VisualizationMethod.BALL_AND_STICK)
            {
                distance = 6 * AtomicRadii.ball_and_stick_radius;
            }
            else
            {
                distance = 4 * AtomicRadii.GetCovalentRadius(selected_atom_.atom_.element_);
            }
        }
        else if (plane2 != null)
        {
            target   = plane2.center_cylinder_.transform.position + plane2.center_cylinder_.transform.up * plane2.center_cylinder_.height_ / 2;
            distance = plane2.center_cylinder_.height_;
        }
        else
        {
            return;
        }
        Vector3 movement_direction = Vector3.Normalize(target - Camera.main.transform.position);

        Vector3 desired_position = target - movement_direction * distance;

        /* Calculate the movement speed, scale it based on the distance to the object */
        speed = speed * Vector3.Distance(desired_position, Camera.main.transform.position);

        /* Change position */
        transform.position = transform.position - speed * Time.deltaTime * movement_direction;
    }
Exemple #4
0
 public void DecreaseSelectionSphereRadius()
 {
     Atoms.SELECTION_MODE_SPHERE_RADIUS -= 0.02f;
     if (selection_plane_previous_ != null)
     {
         SelectionPlaneSpheres   planes = selection_plane_previous_.GetComponent <SelectionPlaneSpheres>();
         SelectionPlaneCylinders planec = selection_plane_previous_.GetComponent <SelectionPlaneCylinders>();
         if (planes != null)
         {
             planes.ChangeRadius();
         }
         if (planec != null)
         {
             planec.ChangeRadius();
         }
     }
     transform.GetChild(1).GetComponent <ModePanel>().SetRadius(SELECTION_MODE_SPHERE_RADIUS);
 }
Exemple #5
0
    /* FPS counter */
    //void OnGUI() {
    //    GUI.Label(new Rect(0, 0, 100, 100), (1.0f / Time.smoothDeltaTime).ToString());
    //}

    void Update()
    {
        /* If the bring forward the panel button is hit, the ray cast only in UI layer */
        if (Input.GetKey(KeyCode.Space))
        {
            bool ui_hit = RayCastUIlayer();
            if (ui_hit)
            {
                return;
            }
        }

        RaycastHit hit;

        /*
         * Perform ray casting towards the camera direction, move the ray origin slightly forward to avoid intersections with spheres that
         * we are currently inside
         */
        bool ray_cast_hit = Physics.Raycast(Camera.main.transform.position + Camera.main.transform.forward * AtomicRadii.ball_and_stick_radius, Camera.main.transform.forward, out hit, 100.0f);

        /* Process model movement input */
        if (Input.GetKey(KeyCode.Keypad1))
        {
            MoveTowardsSelectedObject(speed_object_towards_move);
        }

        if (Input.GetKey(KeyCode.Keypad7))
        {
            MoveTowardsSelectedObject(-speed_object_towards_move);
        }

        if (Input.GetKey(KeyCode.Keypad4))
        {
            transform.position = transform.position - Camera.main.transform.right * speed_object_vertical_move;
        }

        if (Input.GetKey(KeyCode.Keypad6))
        {
            transform.position = transform.position + Camera.main.transform.right * speed_object_vertical_move;
        }

        if (Input.GetKey(KeyCode.Keypad8))
        {
            transform.position = transform.position + Camera.main.transform.up * speed_object_vertical_move;
        }

        if (Input.GetKey(KeyCode.Keypad5))
        {
            transform.position = transform.position - Camera.main.transform.up * speed_object_vertical_move;
        }

        /* Check ray cast against UI */
        if (ray_cast_hit)
        {
            Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward * hit.distance, Color.white);

            ButtonEvent button = hit.transform.GetComponent <ButtonEvent>();
            if (button != null)
            {
                ProcessRayCastUIHit(hit);
            }
        }

        /* Process state machine */
        if (state == STATE.EXPLORING_ATOMS)
        {
            /* If there is a selected atom, and the discard button is pressed, then destory selection */
            if (selected_atom_ != null)
            {
                if (Input.GetKeyDown(KeyCode.Escape) == true)
                {
                    ClearHighlighted();
                    selected_atom_ = null;
                    Destroy(selection_plane_previous_);
                    return;
                }

                return;
            }

            /* Else process ray cast hit */
            if (ray_cast_hit)
            {
                ProcessRayCastHit(hit);
            }
            else
            {
                /* If ray cast failed, clear highlighting */
                ClearHighlighted();
                if (exploring_method_ != ExploringMethod.CHAINS)
                {
                    ClearColored();
                }

                //Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward * 1000, Color.white);
            }
        }
        else if (state == STATE.ATOM_DISTANCES)
        {
            /* If discard button, then reset state */
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                ResetState(true);
                return;
            }

            /* if we have selected an atom, process distances */
            if (selected_atom_ != null)
            {
                SelectionPlaneSpheres plane = selection_plane_previous_.GetComponent <SelectionPlaneSpheres>();

                /*
                 * Get the current selected atom, and if it's different than the previous, and the marked button is pressed
                 * mark atom, and calculate distance
                 */
                int     current_index    = atom_selected_id_ % 4;
                ISphere previously_added = ((current_index == 0) ? atoms_selected_[3] : atoms_selected_[current_index - 1]);
                if (Input.GetKeyDown(KeyCode.E) && previously_added != plane.center_sphere_)
                {
                    atoms_selected_[current_index] = plane.center_sphere_;

                    atom_selected_id_++;

                    /* If marked atom object is null, spawn it */
                    if (marked_atom_object_ == null)
                    {
                        marked_atom_object_ = Instantiate(prefab_marked_atom_, plane.center_sphere_.transform.position, Quaternion.identity);
                        marked_atom_object_.transform.parent = this.transform;
                    }
                    /* Calculate radius of the atom based on the viusalization method, and move marked atom object */
                    float selected_atom_radius;
                    if (GetVisualizationMethod() == VisualizationMethod.BALL_AND_STICK)
                    {
                        selected_atom_radius = AtomicRadii.ball_and_stick_radius;
                    }
                    else
                    {
                        selected_atom_radius = AtomicRadii.GetCovalentRadius(plane.center_sphere_.atom_.element_);
                    }
                    marked_atom_object_.transform.position = plane.center_sphere_.transform.position + 1.4f * Vector3.up * selected_atom_radius;
                    marked_sphere_ = plane.center_sphere_.gameObject;

                    /* If the previously marked atom is not null, then calculate distance */
                    if (previously_added != null)
                    {
                        Vector3 middle = (previously_added.transform.position + plane.center_sphere_.transform.position) / 2;

                        /* Destroy the previous atom distance object from the world */
                        if (atom_distance_previous_ != null)
                        {
                            Destroy(atom_distance_previous_);
                        }
                        /* and spawn the new in the middle of the distance */
                        atom_distance_previous_ = Instantiate(prefab_atom_distance_, middle, Quaternion.identity);
                        atom_distance_previous_.transform.parent = transform;
                        AtomDistance temp = atom_distance_previous_.GetComponent <AtomDistance>();
                        temp.atom1_ = previously_added;
                        temp.atom2_ = plane.center_sphere_;
                    }
                }
                return;
            }

            /* If there is not selected atom, process ray casting as usual */
            if (ray_cast_hit)
            {
                ProcessRayCastHit(hit);
            }
            else
            {
                ClearHighlighted();
                if (exploring_method_ != ExploringMethod.CHAINS)
                {
                    ClearColored();
                }
            }
        }
        else if (state == STATE.BOND_ANGLES)
        {
            /* If discard button is hit, reset state */
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                ResetState(true);
                return;
            }

            /* If selection is spawned, process selected bonds */
            if (selected_bond_ != null)
            {
                SelectionPlaneCylinders plane = selection_plane_previous_.GetComponent <SelectionPlaneCylinders>();

                int       current_index    = bonds_selected_id_ % 2;
                ICylinder previously_added = ((current_index == 0) ? bonds_selected_[1] : bonds_selected_[current_index - 1]);
                /* if the precious is different than the currently selected, select bond, and the arc */
                if (previously_added != plane.center_cylinder_)
                {
                    bonds_selected_[current_index] = plane.center_cylinder_;

                    bonds_selected_id_++;

                    if (previously_added != null)
                    {
                        if (arc_previous_ != null)
                        {
                            Destroy(arc_previous_);
                        }
                        SpawnArc(bonds_selected_[0], bonds_selected_[1]);
                    }
                }
                return;
            }

            /* Else, process ray casting for bonds */
            if (ray_cast_hit)
            {
                ClearHighlighted();

                ICylinder icylinder = hit.transform.GetComponent <ICylinder>();

                if (icylinder != null)
                {
                    icylinder.SetHighlighted(HighlightColors.HIGHLIGHT_COLOR.WHITE);
                    previously_highlighted_bond_ = icylinder;

                    /* If bond seleted, spawn selection object */
                    if (Input.GetMouseButtonDown(0) == true)
                    {
                        selected_bond_ = icylinder;
                        SpawnSelectionPlaneCylinders();
                    }
                }
            }
            else
            {
                ClearHighlighted();
            }
        }
        else if (state == STATE.TORSION_ANGLE)
        {
            /* if the torsion planed has been spawned, highlight the atoms that participate in it */
            if (torsion_plane_spawned_)
            {
                atoms_selected_[0].SetHighlighted(HighlightColors.HIGHLIGHT_COLOR.GREEN);
                atoms_selected_[1].SetHighlighted(HighlightColors.HIGHLIGHT_COLOR.GREEN);
                atoms_selected_[2].SetHighlighted(HighlightColors.HIGHLIGHT_COLOR.GREEN);
                atoms_selected_[3].SetHighlighted(HighlightColors.HIGHLIGHT_COLOR.GREEN);
            }

            /* If discard is pushed, and the torsion planed is spawned, go back to atom selection
             * else reset and destroy selection
             */
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                if (torsion_plane_spawned_)
                {
                    ISphere last_added_sphere = ((atom_selected_id_ == 0) ? atoms_selected_[3] : atoms_selected_[atom_selected_id_ - 1]);
                    ResetState(false);
                    selected_atom_ = last_added_sphere;
                    SpawnSelectionPlaneSpheres();
                    info_ui_.SetAtom(last_added_sphere);
                }
                else
                {
                    ResetState(true);
                }

                return;
            }

            /* If selection is spawned, then process the currently selected atom */
            if (selected_atom_ != null)
            {
                SelectionPlaneSpheres plane = selection_plane_previous_.GetComponent <SelectionPlaneSpheres>();

                ISphere previously_added = ((atom_selected_id_ == 0) ? atoms_selected_[3] : atoms_selected_[atom_selected_id_ - 1]);
                if (Input.GetKeyDown(KeyCode.E) && atom_selected_id_ < 4 && previously_added != plane.center_sphere_)
                {
                    atoms_selected_[atom_selected_id_ % 4] = plane.center_sphere_;
                    info_ui_.SetTorsionAtom(atom_selected_id_, plane.center_sphere_.atom_.name_);

                    atom_selected_id_++;

                    /* If reached 4 selected atoms, spawn the torsion plane object */
                    if (atom_selected_id_ == 4)
                    {
                        SpawnTorsionAngle();
                        selected_atom_ = null;
                        Destroy(selection_plane_previous_);
                        torsion_plane_spawned_ = true;
                    }
                }
                return;
            }

            /* Else ray cast as usual */
            if (ray_cast_hit && !torsion_plane_spawned_)
            {
                ProcessRayCastHit(hit);
            }
            else
            {
                ClearHighlighted();
                if (exploring_method_ != ExploringMethod.CHAINS)
                {
                    ClearColored();
                }
            }
        } /* End of torsion angle state */
    }