Exemplo n.º 1
0
 public void Run(int numRays)
 {
     sw.Restart();
     RCUCApi.rcu_raycast_manager_run(rcuRaycastManager, rayDataArray, intersectionDataArray, (uint)numRays);
     sw.Stop();
     UnityEngine.Debug.Log("RCU: Throwing the rays took " + sw.Elapsed.ToString());
 }
Exemplo n.º 2
0
 public void ReleaseRaycastEnvironment()
 {
     meshFilterArray       = null;
     rayDataArray          = null;
     intersectionDataArray = null;
     RCUCApi.rcu_destroy_raycast_manager(rcuRaycastManager);
     RCUCApi.rcu_destroy_scene(rcuScene);
     RCUCApi.rcu_destroy_allocator(rcuAllocator);
 }
Exemplo n.º 3
0
    public void SetupRaycastEnvironment(MeshRenderer[] meshRendererArray, int maxNumRays)
    {
        // Initialize the stopwatch
        sw.Restart();

        // Allocate the ray arrays
        ResetRayCount(maxNumRays);

        // Create all the native pointers
        rcuAllocator      = RCUCApi.rcu_create_allocator();
        rcuScene          = RCUCApi.rcu_create_scene(rcuAllocator);
        rcuRaycastManager = RCUCApi.rcu_create_raycast_manager(rcuAllocator);

        // Array that holds the matrix
        float[] transformMatrix = new float[16];

        int maxVertCount   = 0;
        int numGameObjects = meshRendererArray.Length;
        int numMeshFilters = 0;

        for (int geoIdx = 0; geoIdx < numGameObjects; ++geoIdx)
        {
            // Grab the next mesh renderer
            MeshRenderer meshRenderer = meshRendererArray[geoIdx];

            // Grab the next game object
            GameObject gameObject = meshRenderer.gameObject;

            // Grab the mesh filter
            MeshFilter meshFilter = gameObject.GetComponent <MeshFilter>();
            if (meshFilter != null && meshFilter.sharedMesh)
            {
                // Fetch the mesh to push
                Mesh currentMesh = meshFilter.sharedMesh;

                // Contrbute to the max
                maxVertCount = Math.Max(currentMesh.vertexCount, maxVertCount);

                // Increase the mesh filter count
                numMeshFilters++;
            }
        }

        // Create the mesh filter array
        meshFilterArray = new MeshFilter[numMeshFilters];

        // Allocate the array for the marshaling
        float[] vertArray       = new float[3 * maxVertCount];
        float[] normalDataArray = new float[3 * maxVertCount];
        float[] texDataCoord    = new float[2 * maxVertCount];

        int meshFilterIterator = 0;

        // Let's now push all the geometry to the plugin
        for (int geoIdx = 0; geoIdx < numGameObjects; ++geoIdx)
        {
            // Grab the next mesh renderer
            MeshRenderer meshRenderer = meshRendererArray[geoIdx];

            // Grab the next game object
            GameObject gameObject = meshRenderer.gameObject;

            // Grab the mesh filter
            MeshFilter meshFilter = gameObject.GetComponent <MeshFilter>();
            if (meshFilter != null && meshFilter.sharedMesh)
            {
                // Set it in the array
                meshFilterArray[meshFilterIterator] = meshFilter;

                // Fetch the mesh to push
                Mesh currentMesh = meshFilter.sharedMesh;

                uint subMeshCount = (uint)currentMesh.subMeshCount;
                for (uint subMeshIdx = 0; subMeshIdx < subMeshCount; ++subMeshIdx)
                {
                    Matrix4x4 transform = gameObject.transform.localToWorldMatrix.transpose;
                    for (int i = 0; i < 16; ++i)
                    {
                        transformMatrix[i] = transform[i];
                    }

                    // Flatten the position array
                    Vector3[] positionArray = currentMesh.vertices;
                    uint      numVerts      = (uint)positionArray.Length;
                    for (int vIdx = 0; vIdx < numVerts; ++vIdx)
                    {
                        vertArray[3 * vIdx]     = positionArray[vIdx].x;
                        vertArray[3 * vIdx + 1] = positionArray[vIdx].y;
                        vertArray[3 * vIdx + 2] = positionArray[vIdx].z;
                    }

                    Vector3[] normalArray = currentMesh.normals;
                    uint      numNormals  = (uint)normalArray.Length;
                    for (int nIdx = 0; nIdx < numNormals; ++nIdx)
                    {
                        normalDataArray[3 * nIdx]     = normalArray[nIdx].x;
                        normalDataArray[3 * nIdx + 1] = normalArray[nIdx].y;
                        normalDataArray[3 * nIdx + 2] = normalArray[nIdx].z;
                    }

                    Vector2[] uvArray      = currentMesh.uv;
                    int       numTexCoords = uvArray.Length;
                    if (numTexCoords > 0)
                    {
                        for (int tIdx = 0; tIdx < numTexCoords; ++tIdx)
                        {
                            texDataCoord[2 * tIdx]     = uvArray[tIdx].x;
                            texDataCoord[2 * tIdx + 1] = uvArray[tIdx].y;
                        }
                    }

                    // Flatten the index array
                    int[] subMeshIndices = currentMesh.GetIndices((int)subMeshIdx);
                    uint  numTriangles   = (uint)(subMeshIndices.Length / 3);

                    // Push the geometry to the scene
                    RCUCApi.rcu_scene_append_geometry(rcuScene, (uint)meshFilterIterator, subMeshIdx, vertArray, normalDataArray, texDataCoord, numVerts, subMeshIndices, numTriangles, transformMatrix);
                }

                meshFilterIterator++;
            }
        }
        sw.Stop();

        UnityEngine.Debug.Log("RCU: Pushing data to the plugin took " + sw.Elapsed.ToString());

        // Init the raycast manager
        sw.Restart();
        RCUCApi.rcu_raycast_manager_setup(rcuRaycastManager, rcuScene);
        sw.Stop();
        UnityEngine.Debug.Log("RCU: Initializing the raycasting structures took " + sw.Elapsed.ToString());
    }