Inheritance: MonoBehaviour
        /// <summary>
        /// Generates the meshes and attaches them to the given parent GameObject
        /// Call to finalise the view being built
        /// </summary>
        /// <param name="parent">Parent.</param>
        /// <param name="material">Material.</param>
        public View apply(GameObject parent, Material material)
        {
            parent.name = Name;
            
            GameObject viewGameObject = new GameObject();

            viewGameObject.transform.SetParent(parent.transform, false);

            View view = viewGameObject.AddComponent<View>();
            view.gameObject.tag = "View";
            view.gameObject.name = "View";


            var data = new BigMesh.BigMeshData(myMeshTopolgy,
                                               positions.ToArray(),
                                               Indices.ToArray(),
                                               colours.ToArray(),
                                               normals.ToArray(),
                                               uvs.ToArray(),
                                               uv2s.ToArray(),
                                               chunkSizeList.ToArray(),
                                               material,
                                               LineLength.ToArray());

            BigMesh meshObject = BigMesh.createBigMesh(data);
            meshObject.gameObject.name = "BigMesh";
            meshObject.transform.SetParent(viewGameObject.transform, false);

            bigMesh = meshObject;
            view.BigMesh = bigMesh;

            return view;
        }
Beispiel #2
0
        /// <summary>
        /// Creates a big mesh. Only handles meshes composed of triangles, points, and lines.
        /// </summary>
        /// <returns>The big mesh.</returns>
        /// <param name="meshData">Mesh data.</param>
        public static BigMesh createBigMesh(BigMeshData meshData)
        {
            List<Mesh> localMeshList = new List<Mesh>();
            GameObject parentObject = null;
            
            // Optimisation for when there is only one mesh
            //if (meshData.vertices.Length <= VERTEX_LIMIT)
            //{
                parentObject = createMesh(meshData.vertices,
                    meshData.indices,
                    meshData.colours,
                    meshData.normals,
                    meshData.uvs,
                    meshData.meshTopology,
                    meshData.material);
                localMeshList.Add(parentObject.GetComponent<MeshFilter>().sharedMesh);
            //}
            //else // Split it up
//            {
//                parentObject = new GameObject();
//                parentObject.name = "BigMesh";

//                List<Vector3> vertexBuffer = new List<Vector3>();
//                List<int> indexBuffer = new List<int>();
//                List<Color> colourBuffer = new List<Color>();
//                List<Vector3> normalBuffer = new List<Vector3>();
//                List<Vector3> uvBuffer = new List<Vector3>();

//                // Create all the meshes
//                int offset0 = 0;
//                ///
//                ///  >>>>>>>>> POINT TOPOLOGY <<<<<<<<<<<<<<
//                ///
//                if (meshData.meshTopology == MeshTopology.Points)
//                {
//                    for (int i = 0; i < meshData.vertices.Length; i++)
//                    {
//                        if ((vertexBuffer.Count) >= VERTEX_LIMIT)
//                        {
//                            GameObject meshObject = createMesh(vertexBuffer.ToArray(), 
//                                indexBuffer.ToArray(), 
//                                colourBuffer.ToArray(),
//                                normalBuffer.ToArray(),
//                                uvBuffer.ToArray(),
//                                meshData.meshTopology, 
//                                meshData.material);
//                            meshObject.transform.parent = parentObject.transform;

//                            vertexBuffer.Clear();
//                            indexBuffer.Clear();
//                            colourBuffer.Clear();
//                            normalBuffer.Clear();
//                            uvBuffer.Clear();

//                            localMeshList.Add(meshObject.GetComponent<MeshFilter>().sharedMesh);
//                            offset0 += VERTEX_LIMIT;
//                        }

//                        indexBuffer.Add(i - offset0);
//                        vertexBuffer.Add(meshData.vertices[i]);
//                        colourBuffer.Add(meshData.colours[i]);
//                        normalBuffer.Add(meshData.normals[i]);
//                        uvBuffer.Add(meshData.uvs[i]);                    }
//                }
//                ///
//                ///  >>>>>>>>> LINE TOPOLOGY <<<<<<<<<<<<<<
//                ///
//                else if (meshData.meshTopology == MeshTopology.Lines)
//                {
//                    int MaxIndex = meshData.indices.Max(); //should correspond to the size of the dataset - 1
//                    Dictionary<int, int> indexBtoVertexBIndices = new Dictionary<int, int>();

//                    int currentLine = 0;
//                    int currentLineCount = meshData.lineLength[0] * 2;
//                    HashSet<int> globalIndices = new HashSet<int>();

//                    for (int i = 0; i < meshData.indices.Length; i++)
//                    {
//                        //get the index of the vertex to access
//                        int vbIndex = meshData.indices[i];
                        
//                        if (currentLineCount == 0 && currentLine + 1 < meshData.lineLength.Length)
//                        {
//                            int newVertexCounter = 0;

//                            //look up next vertices in the coming line
//                            for (int k = i; k < i + meshData.lineLength[currentLine + 1] * 2; k++)
//                            {
//                                if (k < meshData.indices.Length)
//                                    if (!globalIndices.Contains(meshData.indices[k])) newVertexCounter++;
//                            }

//                            if (vertexBuffer.Count + newVertexCounter > VERTEX_LIMIT)
//                            {
//                                GameObject meshObject = createMesh(vertexBuffer.ToArray(), 
//                                                                   indexBuffer.ToArray(), 
//                                                                   colourBuffer.ToArray(), 
//                                                                   normalBuffer.ToArray(),
//                                                                   uvBuffer.ToArray(),
//                                                                   meshData.meshTopology, meshData.material);
//                                meshObject.transform.parent = parentObject.transform;
//                                localMeshList.Add(meshObject.GetComponent<MeshFilter>().sharedMesh);

//                                //print("created mesh " + localMeshList.Count + " containing # vertices: " + vertexBuffer.Count);

//                                vertexBuffer.Clear();
//                                indexBuffer.Clear();
//                                colourBuffer.Clear();
//                                normalBuffer.Clear();
//                                uvBuffer.Clear();
//                                indexBtoVertexBIndices.Clear();
//                                globalIndices.Clear();

//                            }

//                            currentLine++;
//                            currentLineCount = meshData.lineLength[currentLine] * 2;

//                        }
//                        //else
//                        {
//                            //in any case keep filling up the index buffer
//                            // normalise the index
//                            int localIndexBufferIndex = 0;

//                            if (!indexBtoVertexBIndices.ContainsKey(vbIndex))
//                            {
//                                localIndexBufferIndex = vertexBuffer.Count;

//                                vertexBuffer.Add(meshData.vertices[vbIndex]);
//                                colourBuffer.Add(meshData.colours[vbIndex]);
//                                normalBuffer.Add(meshData.normals[vbIndex]);
//                                uvBuffer.Add(meshData.uvs[vbIndex]);

//                                indexBtoVertexBIndices.Add(vbIndex, localIndexBufferIndex);

//                            }
//                            else
//                            {
//                                localIndexBufferIndex = indexBtoVertexBIndices[vbIndex];
//                            }

//                            indexBuffer.Add(localIndexBufferIndex);

//                            globalIndices.Add(vbIndex);
//                            //decrease line count
//                            currentLineCount--;

//                        }
//                    }

//                }

//                if (vertexBuffer.Count > 0)
//                {
//                    //print("creeating ")
//                    GameObject meshObject = createMesh(vertexBuffer.ToArray(), 
//                                                       indexBuffer.ToArray(), 
//                                                       colourBuffer.ToArray(), 
//                                                       normalBuffer.ToArray(),
//                                                       uvBuffer.ToArray(),
//                                                       meshData.meshTopology, meshData.material);
//                    meshObject.transform.parent = parentObject.transform;
//                    localMeshList.Add(meshObject.GetComponent<MeshFilter>().sharedMesh);

////                    print("created mesh " + localMeshList.Count + " containing # vertices: " + vertexBuffer.Count);

//                }

//                #region broken code for connected topology with 65k+ lines
//                //else if (meshData.meshTopology == MeshTopology.Lines)
//                //{
//                //    int previousChunkOffset = 0;

//                //    for (int chunk=0;chunk<meshData.chunkIndicesSize.Length; chunk++)
//                //    {
//                //        int chunkOffset = previousChunkOffset + meshData.chunkIndicesSize[chunk];// *(BigMesh.VERTEX_LIMIT);
//                //        print("chunk offset " + chunkOffset);

//                //        vertexBuffer.Clear();
//                //        indexBuffer.Clear();
//                //        colourBuffer.Clear();
//                //        normalBuffer.Clear();

//                //        //read 65k vertices
//                //        for (int i=0;i<=BigMesh.VERTEX_LIMIT;i++)
//                //        {
//                //            vertexBuffer.Add(meshData.vertices[i + chunkOffset]);
//                //            colourBuffer.Add(meshData.colours[i + chunkOffset]);
//                //            normalBuffer.Add(meshData.normals[i + chunkOffset]);
//                //        }

//                //        //read the index buffer
//                //        for (int i = 0; i < meshData.chunkIndicesSize[chunk]; i++)
//                //        {
//                //            indexBuffer.Add(meshData.indices[i]);
//                //        }

//                //        print("at 0: " + indexBuffer[0]);
//                //        print("at 1: " + indexBuffer[1]);
//                //        print("at 2: " + indexBuffer[2]);

//                //        print("at last -2: " + indexBuffer[indexBuffer.Count -3]);
//                //        print("at last -1: " + indexBuffer[indexBuffer.Count - 2]);
//                //        print("at last: " + indexBuffer[indexBuffer.Count - 1]);

//                //        GameObject meshObject = createMesh(vertexBuffer.ToArray(), indexBuffer.ToArray(), colourBuffer.ToArray(), normalBuffer.ToArray(), meshData.meshTopology, meshData.material);
//                //        meshObject.transform.parent = parentObject.transform;

//                //        previousChunkOffset = chunkOffset;
//                //    }
//                //}
//                #endregion

//                #region old bugged code for big mesh creation
//                //for (int i = 0; i < meshData.vertices.Length - 1; i += 2)
//                //{
//                //    if ((vertexBuffer.Count) > VERTEX_LIMIT)
//                //    {
//                //        GameObject meshObject = createMesh(vertexBuffer.ToArray(), indexBuffer.ToArray(), colourBuffer.ToArray(), normalBuffer.ToArray(), meshData.meshTopology, meshData.material);
//                //        meshObject.transform.parent = parentObject.transform;

//                //        vertexBuffer.Clear();
//                //        indexBuffer.Clear();
//                //        colourBuffer.Clear();
//                //        normalBuffer.Clear();

//                //        localMeshList.Add(meshObject.GetComponent<MeshFilter>().sharedMesh);
//                //        //Debug.Log("completed line from " + meshData.indices[i] + " to " + meshData.indices[i + 1]);
//                //        //if(i+2 < meshData.vertices.Length)
//                //        //Debug.Log("next indice is " + meshData.indices[i+2]);

//                //        //if (localMeshList.Count > 1) goto debuglines;
//                //    }

//                //    int index1 = meshData.indices[i];// + offset];
//                //    int index2 = meshData.indices[i + 1];
//                //    //indexBuffer.Add(vertexBuffer.Count);
//                //    indexBuffer.Add(index1 % (VERTEX_LIMIT));// + offset]);
//                //    indexBuffer.Add(index2 % (VERTEX_LIMIT));

//                //    //if(i%2 ==0)
//                //    vertexBuffer.Add(meshData.vertices[i]);
//                //    vertexBuffer.Add(meshData.vertices[i + 1]);

//                //    if (meshData.colours != null)
//                //    {
//                //        // if (i % 2 == 0)
//                //        colourBuffer.Add(meshData.colours[index1]);
//                //        colourBuffer.Add(meshData.colours[index2]);

//                //    }

//                //    if (meshData.normals != null)
//                //    {
//                //        // if (i % 2 == 0)
//                //        normalBuffer.Add(meshData.normals[index1]);
//                //        normalBuffer.Add(meshData.normals[index2]);

//                //    }

//                //}
//                #endregion

//                //// Create remaining
//                //if (vertexBuffer.Count >= stride)
//                //{
//                //    GameObject meshObject = createMesh(vertexBuffer.ToArray(), indexBuffer.ToArray(), colourBuffer.ToArray(), normalBuffer.ToArray(), meshData.meshTopology, meshData.material);
//                //    meshObject.transform.parent = parentObject.transform;

//                //    localMeshList.Add(meshObject.GetComponent<MeshFilter>().sharedMesh);
//                //}
//            }


            BigMesh bigMeshComponent = parentObject.AddComponent<BigMesh>();
            bigMeshComponent.SharedMaterial = meshData.material;

            bigMeshComponent.meshList = localMeshList;

            return bigMeshComponent;
        }