Esempio n. 1
0
    /******************************************************************************************************************************************************************/

    public void SetHapticGeometry()
    {
        //Get array of all object with tag "Touchable"
        GameObject[] myObjects = GameObject.FindGameObjectsWithTag("Touchable") as GameObject[];
        Debug.Log("1");

        for (int ObjId = 0; ObjId < myObjects.Length; ObjId++)
        {
            Debug.Log("2");
            /***************************************************************/
            //Set the Transformation Matric of the Object
            /***************************************************************/
            //Get the Transformation matrix from object
            Matrix4x4 m = new Matrix4x4();

            //Build a transform Matrix from the translation/rotation and Scale parameters fo the object - Local Matrix
            //m.SetTRS(myObjects[ObjId].transform.position,myObjects[ObjId].transform.rotation,myObjects[ObjId].transform.localScale);

            //Build a transform Matrix from the translation/rotation and Scale parameters fo the object - Glabal Matrix
            m = myObjects[ObjId].transform.localToWorldMatrix;
            Debug.Log("3");
            //Convert Matrix4x4 to double16
            double[] matrix = ConverterClass.ConvertMatrix4x4ToDouble16(m);
            //Convert Double16 To IntPtr
            IntPtr dstDoublePtr = ConverterClass.ConvertDouble16ToIntPtr(matrix);
            Debug.Log("3a");
            //Convert String to Byte[] (char* in C++) and Byte[] to IntPtr
            IntPtr dstCharPtr = ConverterClass.ConvertStringToByteToIntPtr(myObjects[ObjId].name);
            Debug.Log("3b");
            //Send the transformation Matrix of the object
            PluginImport.SetObjectTransform(ObjId, dstCharPtr, dstDoublePtr);
            Debug.Log(myObjects[ObjId].gameObject.name);
            Debug.Log("3c");
            /***************************************************************/

            /***************************************************************/
            //Set the Mesh of the Object
            /***************************************************************/
            //Get Mesh of Object
            Mesh mesh = myObjects[ObjId].GetComponent <MeshFilter>().mesh;
            Debug.Log("3d");
            Vector3[] vertices = mesh.vertices;
            Debug.Log("3e");
            int[] triangles = mesh.triangles;
            Debug.Log("4");
            //Reorganize the Array
            float[] verticesToSend = ConverterClass.ConvertVector3ArrayToFloatArray(vertices);
            //Allocate Memory according to needed space for float* (3*4)
            IntPtr dstVerticesArrayPtr = Marshal.AllocCoTaskMem(vertices.Length * 3 * Marshal.SizeOf(typeof(float)));
            //Copy to dstPtr
            Marshal.Copy(verticesToSend, 0, dstVerticesArrayPtr, vertices.Length * 3);

            //Convert Int[] to IntPtr
            IntPtr dstTrianglesArrayPtr = ConverterClass.ConvertIntArrayToIntPtr(triangles);

            //Send the Raw Mesh of the object - transformation are not applied on the Mesh vertices
            PluginImport.SetObjectMesh(ObjId, dstVerticesArrayPtr, dstTrianglesArrayPtr, vertices.Length, triangles.Length);
            /***************************************************************/
            Debug.Log("5");
            /***************************************************************/
            //Get the haptic parameter configuration
            /***************************************************************/
            ReadHapticProperties(ObjId, myObjects[ObjId]);
            /***************************************************************/

            Debug.Log(PluginImport.GetHapticObjectFaceCount(ObjId));
        }
    }