Example #1
0
    private void DrawModel(FLATData.FlatRes cortexData)
    {
        // Destroy old model
        if (m_MeshModelParts.Count > 0)
        {
            print("Destroying old mesh..");
            foreach (GameObject mesh in m_MeshModelParts)
            {
                Destroy(mesh);
            }
            m_MeshModelParts.Clear();
        }
        //m_FullLineModel.SetActive(false);
        m_FullLineModel.GetComponent <FullLineModelRenderer>().StartFadeOut();

        // Set meshPartMat to transparent to start
        m_MeshPartPrefab.GetComponent <Renderer>().sharedMaterial.color -= new Color(0, 0, 0, 1);

        print("Query done. Building Mesh...");
        //Reset model position/rotation/scale
        transform.localPosition = m_DefaultModelPos;
        transform.localRotation = m_DefaultModelRotation;
        float newQueryBoxScale = m_QueryBox.transform.localScale.x; // x,y,z all same
        float newScale         = m_defaultNewQueryScale / newQueryBoxScale;

        transform.localScale = newScale * m_DefaultModelScale;
        m_CurQueryScale      = transform.localScale;


        // Put data into vector3 form
        List <Vector3> cortexVertices = new List <Vector3>();

        for (int i = 0; i < cortexData.numcoords; i += 3)
        {
            Vector3 v = new Vector3(cortexData.coords[i], cortexData.coords[i + 1], cortexData.coords[i + 2]);

            cortexVertices.Add(v);
        }

        //print("Min: " + min.ToString());
        //print("Max: " + max.ToString());

        int vertexCount = cortexVertices.Count;

        m_watch = System.Diagnostics.Stopwatch.StartNew();

        if (vertexCount > 0)
        {
            // Calculate number of meshes needed
            int numMeshesRequired = (vertexCount / MAX_VERTICES_PER_MESH) + 1;

            for (int i = 0; i < numMeshesRequired; i++)
            {
                // Create mesh and vertecis, uv and triangles lists
                Mesh           mesh      = new Mesh();
                List <Vector2> UVs       = new List <Vector2>();
                List <int>     triangles = new List <int>();

                // Calculate start and end vert indexes for current mesh
                int startInd = i * MAX_VERTICES_PER_MESH;
                int endInd   = Mathf.Min(startInd + MAX_VERTICES_PER_MESH, vertexCount);

                // Get vertices for current mesh
                Vector3[] vertices = cortexVertices.GetRange(startInd, endInd - startInd).ToArray();

                // Set mesh vertices
                mesh.vertices = vertices;

                // Set UVs and triangles for mesh
                for (int j = 0; j < endInd - startInd; j++)
                {
                    UVs.Add(new Vector2(vertices[j].x, vertices[j].z));

                    if (j < endInd - startInd - 2 && (j % 3 == 0))
                    {
                        triangles.Add(j);
                        triangles.Add(j + 1);
                        triangles.Add(j + 2);
                    }
                }

                mesh.triangles = triangles.ToArray();
                mesh.uv        = UVs.ToArray();
                mesh.RecalculateNormals();

                // Instantiate new game object to hold mesh and set is as child of 'meshparts' object
                GameObject meshPart = Instantiate(m_MeshPartPrefab);
                meshPart.transform.SetParent(m_MeshParts.transform);

                // Add mesh to gameobject
                MeshFilter mf = meshPart.GetComponent <MeshFilter>();
                if (mf)
                {
                    mf.mesh = mesh;
                }

                // Add to mesh parts list
                m_MeshModelParts.Add(meshPart);

                // Reset transform of new game object
                meshPart.transform.localPosition = new Vector3(0, 0, 0);
                meshPart.transform.localScale    = new Vector3(1, 1, 1);
            }
        }

        print("Model build Time: " + m_watch.ElapsedMilliseconds);

        // Center the new model
        m_MeshParts.transform.localPosition = -1 * m_QueryCenter;
        m_QueryBox.transform.localPosition  = Vector3.zero;

        // Start transition of centering and resizing new model
        //ModelTransition();
        StartQueryFadeIn();

        m_QueryShown = true;

        // Set control mode back to explore
        ControlModeManager cmm = GetComponent <ControlModeManager>();

        if (cmm)
        {
            cmm.SetControlMode(ControlModeManager.CONTROL_MODE.QUERY_MODEL);
        }

        // Deactivate 'loading' visuals
        m_queryInProgress = false;
        m_queryBoxFlash.SetFlashActive(false);
        m_ScreenDisplay.ShowQueryLoading(false);
        print("Done");
    }