コード例 #1
0
    public bool addDevice(string deviceName, VRPNDeviceType type)
    {
        Debug.Log(deviceName);
        VRPNDevice device = new VRPNDevice(deviceName, type);

        if (true || (availableDevices.Contains(deviceName) && !devices.ContainsKey(deviceName)))
        {
            devices.Add(deviceName, device);
            switch (type)
            {
            case VRPNDeviceType.Phantom:
                Debug.Log("Instanciating VRPNPicker");
                GameObject go = (GameObject)GameObject.Instantiate(Resources.Load("VRPN/VRPNPicker", typeof(GameObject)), Vector3.zero, Quaternion.identity);
                Debug.Log("Configuring VRPNPickerController");
                VRPNPickerController s1 = go.GetComponent <VRPNPickerController>();
                if (s1)
                {
                    s1.setManager(this);
                    s1.setDeviceName(deviceName);
                }

                Debug.Log("After pickercontroller");

                VRPNButtonController s2 = go.GetComponent <VRPNButtonController>();
                if (s2)
                {
                    s2.setManager(this);
                    s2.setDeviceName(deviceName);
                }

                VRPNForceFeedback s3 = go.GetComponent <VRPNForceFeedback>();
                if (s3)
                {
                    s3.setManager(this);
                    s3.setDeviceName(deviceName);
                    if (!s3.startDeviceInLib())
                    {
                        Debug.Log(":: VRPNManager :: Can't activate Force Feedback");
                    }
                }

                break;

            case VRPNDeviceType.Mouse3DConnexion:
                VRPNAnalogController s = GameObject.FindGameObjectWithTag("LoadBox").GetComponent <VRPNAnalogController>();
                s.setManager(this);
                break;

            default:
                break;
            }
            return(true);
        }
        return(false);
    }
コード例 #2
0
    // Update is called once per frame
    void Update()
    {
        if (!initialized && !startDevice())
        {
            return;
        }

        if (!listen)
        {
            VRPNButtonController btn_ctrl = gameObject.GetComponent <VRPNButtonController>() as VRPNButtonController;
            if (btn_ctrl != null)
            {
                VRPNButtonController.OnButton += OnVRPNButtonEvent;
                listen = true;
            }
        }

        int num = maxNumberOfReports;

        // Get the first report for now
        VRPNReport rep;

        if (VRPNTrackerNumPosReports(deviceName) > 0)
        {
            // Retrieve tracker position report
            VRPNTrackerPosReports(deviceName, reports, ref num);
            rep = (VRPNReport)Marshal.PtrToStructure(reports[0], typeof(VRPNReport));

            // Store position in a Vector3
//			trackerPos.x = (float)rep.pos[0];
//			trackerPos.y = (float)rep.pos[1];
//			trackerPos.z = (float)rep.pos[2];
//
//			if (trackerPos.x < minX) {
//				minX = trackerPos.x;
//			}
//			if (trackerPos.x > maxX) {
//				maxX = trackerPos.x;
//			}
//			if (trackerPos.y < minY) {
//				minY = trackerPos.y;
//			}
//			if (trackerPos.y > maxY) {
//				maxY = trackerPos.y;
//			}
//			if (trackerPos.z < minZ) {
//				minZ = trackerPos.z;
//			}
//			if (trackerPos.z > maxZ) {
//				maxZ = trackerPos.z;
//			}
//
//			Debug.Log("X min: " + minX + " max: " + maxX);
//			Debug.Log("Y min: " + minY + " max: " + maxY);
//			Debug.Log("Z min: " + minZ + " max: " + maxZ);

            // Store position in a Vector3
            trackerPos.x = (float)rep.pos[0] * xCoef;
            trackerPos.y = (float)rep.pos[1] * yCoef;
            trackerPos.z = -(float)rep.pos[2] * zCoef;

            // Store quaternion components in a Quaternion
            trackerQuat.x = -(float)rep.quat[0];
            trackerQuat.y = -(float)rep.quat[1];
            trackerQuat.z = (float)rep.quat[2];
            trackerQuat.w = (float)rep.quat[3];
        }
        // Retrieve and store tracker velocity
        VRPNTrackerVelReport(deviceName, velReport, IntPtr.Zero, 0);
        rep = (VRPNReport)Marshal.PtrToStructure(velReport, typeof(VRPNReport));
        trackerVelocity.x = (float)rep.pos[0];
        trackerVelocity.y = (float)rep.pos[1];
        trackerVelocity.z = -(float)rep.pos[2];

        // Retrieve the camera rotation, stored as a Quaternion
        Quaternion camRotation = camGO.transform.rotation;
        // Retrieve the camera position and forward 3D vectors
        Vector3 camPosition = camGO.transform.position;
        Vector3 camForward  = camGO.transform.forward;

        // Clutching
        if (moveWorkspace)
        {
            clutchingOffset.x = currentClutchingPos.x - trackerPos.x;
            clutchingOffset.y = currentClutchingPos.y - trackerPos.y;
            clutchingOffset.z = currentClutchingPos.z - trackerPos.z;

//			Debug.Log ("CurrentScaleF: " + currentScaleFactor);
//			Debug.Log ("TrackerZ: " + trackerQuat.z);
            float c = currentScale * (1 - (currentScaleFactor - trackerQuat.z));
            xCoef = c;
            yCoef = c;
            zCoef = c * 2;
            computeWorkspaceCube();
        }

        Vector3 cubePosOffset = new Vector3(-clutchingOffset.x, -clutchingOffset.y, -clutchingOffset.z);

//		cube.transform.position = camPosition + camForward * 20 - (camRotation * cubePosOffset);
        cube.transform.position = Vector3.Lerp(cube.transform.position, camPosition + camForward * 20 - (camRotation * cubePosOffset), Time.deltaTime * 8.0f);
        cube.transform.rotation = camRotation;

        trackerPos += clutchingOffset;

        // Set the picker orientation to a camera and pen orientation composition
        transform.rotation = camRotation * trackerQuat;
        // Keep the picker in front of the camera at a distance
        // Set the picker position to cam position + in front of it + rotate tracker positions by cam rotation

        transform.position = Vector3.Lerp(transform.position, camPosition + camForward * 20 + camRotation * trackerPos, Time.deltaTime * 8.0f);
//		transform.position = camPosition + camForward * 20 + camRotation * trackerPos;
    }
コード例 #3
0
    void Update()
    {
        if (GUIMoleculeController.toggle_MDDRIVER && mddriverScript == null)
        {
            mddriverScript = GameObject.FindObjectOfType <MDDriver>();
        }

        if (!listen)
        {
            VRPNButtonController btn_ctrl = gameObject.GetComponent <VRPNButtonController>() as VRPNButtonController;
            if (btn_ctrl != null)
            {
                VRPNButtonController.OnButton += OnVRPNButtonEvent;
                listen = true;
            }
        }

        ffScript = gameObject.GetComponent <VRPNForceFeedback>() as VRPNForceFeedback;

        if (checkTime)
        {
            double diffSecs = System.DateTime.Now.Subtract(lastPressedTime).TotalSeconds;
            if (diffSecs >= 1.0f)
            {
                applyForce = true;
                checkTime  = false;
            }
        }

//		Vector3 direction = -transform.forward;

        GameObject closestAtom = MoleculeModel.atoms[0] as GameObject;
        float      minDist     = Vector3.Distance(closestAtom.transform.position, transform.position);

        //Find closest atom
        for (int i = 1; i < MoleculeModel.atoms.Count; i++)
        {
            GameObject go   = MoleculeModel.atoms[i] as GameObject;
            float      dist = Vector3.Distance(go.transform.position, transform.position);
            if (dist < minDist)
            {
                minDist     = dist;
                closestAtom = go;
            }
        }

        float[] forces = new float[atomNumberList.Count * 3];

        Vector3 diff                = atomList[0].transform.position - atomList[1].transform.position;
        Vector3 currentForce        = diff.normalized;
        Vector3 clampedCurrentForce = currentForce * 5.0f;

        forces[0] = clampedCurrentForce.x;
        forces[1] = clampedCurrentForce.y;
        forces[2] = clampedCurrentForce.z;

        forces[3] = -clampedCurrentForce.x;
        forces[4] = -clampedCurrentForce.y;
        forces[5] = -clampedCurrentForce.z;

        string       filename = "umol_2K96_data.txt";
        StreamWriter fpsLog   = new StreamWriter(filename, true);

        fpsLog.WriteLine(clampedCurrentForce.x + "\t" + clampedCurrentForce.y + "\t" + clampedCurrentForce.z + "\t" + Vector3.Distance(atomList[0].transform.position, atomList[1].transform.position));
        fpsLog.Close();
        fpsLog.Dispose();

        mddriverScript.applyForces(atomNumberList.ToArray(), forces);

        for (int i = 0; i < arrowList.Count; i++)
        {
            arrowList[i].transform.up         = diff;
            arrowList[i].transform.localScale = new Vector3(1.0f, 10.0f, 1.0f);
        }

        if (applyForce)
        {
            if (atomList.Count > 0)
            {
                Vector3 barycenter = (atomList[0].transform.position + atomList[1].transform.position) / 2;
                diff = transform.position - barycenter;
                float distance = diff.magnitude;
//				Vector3 d2 = GameObject.FindGameObjectWithTag("LoadBox").transform.worldToLocalMatrix * diff;
//
                // Interactive mode
//				for (int i = 0; i < atomList.Count; i++) {
//					if (atomList[i].rigidbody != null) {
//						atomList[i].rigidbody.AddForce(diff, ForceMode.Impulse);
//					}
//				}

                ffScript.setLinearForceForVector(-diff);
                if (GUIMoleculeController.toggle_MDDRIVER && mddriverScript != null)
                {
                    forces = new float[atomNumberList.Count * 3];

                    diff         = atomList[0].transform.position - atomList[1].transform.position;
                    currentForce = diff.normalized;
//					currentForce *= Mathf.Sqrt(distance);
                    currentForce *= 1 / Mathf.Pow((1 + 0.5f * Mathf.Exp(-3 * distance)), 2);
//					currentForce *= 1 / Mathf.Pow(1 + Mathf.Exp(-(distance - 4)), 2);

                    //Vector3 clampedCurrentForce = currentForce * distance / 4.0f;
                    clampedCurrentForce = currentForce * 10.0f;

                    for (int i = 0; i < atomNumberList.Count; i++)
                    {
                        forces[i]     = clampedCurrentForce.x;
                        forces[i + 1] = clampedCurrentForce.y;
                        forces[i + 2] = clampedCurrentForce.z;
                    }

//					forces[0] = clampedCurrentForce.x;
//					forces[1] = clampedCurrentForce.y;
//					forces[2] = clampedCurrentForce.z;
//
//					forces[3] = -clampedCurrentForce.x;
//					forces[4] = -clampedCurrentForce.y;
//					forces[5] = -clampedCurrentForce.z;

//					filename = "umol_2K96_data.txt";
//					fpsLog = new StreamWriter(filename, true);
//					fpsLog.WriteLine(clampedCurrentForce.x + "\t" + clampedCurrentForce.y + "\t" + clampedCurrentForce.z + "\t" + Vector3.Distance(atomList[0].transform.position, atomList[1].transform.position));
//					fpsLog.Close();
//					fpsLog.Dispose();

                    if (forceApplied == false)
                    {
                        for (int i = 0; i < arrowList.Count; i++)
                        {
                            arrowList[i].transform.GetChild(0).gameObject.GetComponent <Renderer>().enabled = true;
                        }
                        forceApplied = true;
                    }
                    float arrowZScale = distance / 10.0f;
                    float arrowScale  = distance / 20.0f;
                    for (int i = 0; i < arrowList.Count; i++)
                    {
                        arrowList[i].transform.up         = diff;
                        arrowList[i].transform.localScale = new Vector3(arrowScale, arrowZScale, arrowScale);
                    }

                    mddriverScript.applyForces(atomNumberList.ToArray(), forces);
                }
            }
        }
        else
        {
            if (forceApplied == true)
            {
                for (int i = 0; i < arrowList.Count; i++)
                {
                    arrowList[i].transform.GetChild(0).gameObject.GetComponent <Renderer>().enabled = false;
                    arrowList[i].transform.localScale = new Vector3(0.4f, 3.5f, 0.4f);
                }

                forces = new float[atomNumberList.Count * 3];
                for (int i = 0; i < atomNumberList.Count; i++)
                {
                    forces[i]     = 0.0f;
                    forces[i + 1] = 0.0f;
                    forces[i + 2] = 0.0f;
                }
                mddriverScript.applyForces(atomNumberList.ToArray(), forces);
                mddriverScript.resetForces();

                forceApplied = false;
            }
//			GameObject obj = CastRayFromPicker (distance);
            GameObject obj = closestAtom;

            // If there is no raycasted object or the object is not an atom, we reset the state variables and display
//			if (obj == null || (obj.GetComponent<BallUpdateHB>() == null)) {
            if (minDist > 8.0f)
            {
                Reset();
                ffScript.resetForce();
            }
            else
            {
                // The user pressed the button, so we perform the selection and set se selection variable to false
                if (selectCurrentAtom)
                {
                    Select();
                    selectCurrentAtom = false;
                }
                // Set currently pointed atom and associated halo vars
                Set(obj);
            }

            if (ffScript != null)
            {
                ffScript.setForceForAtomPosition(closestAtom.transform.position);
            }
//			Debug.Log ("Calling setForceForAtomPosition");
//			ffScript.setForceForAtomPosition(closestAtom.transform.position);
        }
    }