Beispiel #1
0
        //main thread mesh assigning
        //assigns meshes vertices , triangles and colors to mesh
        //as well as adds the plant placer script which plants the trees and grass and rocks on the mesh
        public void CreateMesh( )
        {
            if (canCreatemesh)
            {
                if (mesh == null)
                {
                    mesh = new Mesh();
                }
                else if (mesh != null)
                {
                    mesh.Clear();
                }

                mesh.vertices  = verts;
                mesh.triangles = tris;
                mesh.normals   = normals;
                mesh.colors    = uv;

                size = 0;
                if (verts != null)
                {
                    size = verts.Length;
                }

                if (size > 0)
                {
                    shouldrender = true;
                }

                else
                {
                    shouldrender = false;
                }

                if (size > 0)
                {
                    //store in colors

                    //mark mesh as dynamic not sure how well this works?
                    mesh.MarkDynamic();
                    mesh.Optimize();
                    if (m_mesh == null)
                    {
                        m_mesh = new GameObject("Voxel Mesh " + m_pos + "Lod = " + lodLevel);
                    }
                    m_mesh.tag                     = parent.tag;
                    m_mesh.layer                   = parent.gameObject.layer;
                    m_mesh.isStatic                = parent.gameObject.isStatic;
                    m_mesh.transform.parent        = parent;
                    m_mesh.transform.localPosition = m_pos;

                    m_mesh.transform.localPosition = new Vector3(m_mesh.transform.localPosition.x, 0, m_mesh.transform.localPosition.z);
                    m_mesh.transform.localScale    = Vector3.one;
                    mytransform = m_mesh.transform;

                    if (col != null)
                    {
                        UnityEngine.Object.DestroyImmediate(col);
                    }
                    if (size > 0)
                    {
                        col            = m_mesh.AddComponent <MeshCollider>();
                        col.sharedMesh = mesh;
                    }

                    if (HasGrass == false)
                    {
                        //add plant placer component to mesh object
                        plantPlacer placer = m_mesh.AddComponent <plantPlacer>();
                        //assign vertices for plant place to use to know where to place the plants and trees
                        placer.Vertices = verts;

                        //assign colors to plant placer supposed to be used to only place grass on grass area but
                        //i couldnt get it to work
                        //placer.control = control;
                        meshfilter            = m_mesh.AddComponent <MeshFilter>();
                        meshrender            = m_mesh.AddComponent <MeshRenderer>();
                        meshrender.material   = generator.m_material;
                        meshfilter.sharedMesh = mesh;
                        UnityEditor.EditorUtility.SetSelectedWireframeHidden(meshrender, true);
                        mytransform.hideFlags = HideFlags.HideAndDontSave;
                        placer.chunk          = this;
                        HasGrass = true;
                    }
                    //set flag so engine knows it can draw mesh now
                    canDraw = true;
                    //nullify all vertices and tris and normals so as to not hold onto unnecassary information
                    tris    = null;
                    verts   = null;
                    normals = null;
                }
            }
        }
Beispiel #2
0
        //main thread mesh assigning
        //assigns meshes vertices , triangles and colors to mesh
        //as well as adds the plant placer script which plants the trees and grass and rocks on the mesh
        public void CreateMesh( )
        {
            if (mesh == null)
            {
                mesh = new Mesh();
            }
            else if (mesh != null)
            {
                mesh.Clear();
            }

            mesh.vertices  = verts;
            mesh.triangles = tris;
            mesh.normals   = normals;


            size = 0;
            if (verts != null)
            {
                size = verts.Length;
            }

            if (size > 0)
            {
                shouldrender = true;
            }

            else
            {
                shouldrender = false;
            }

            if (size > 0)
            {
                //store in colors
                mesh.colors = control;

                //mark mesh as dynamic not sure how well this works?
                mesh.MarkDynamic();

                if (m_mesh == null)
                {
                    m_mesh = new GameObject("Voxel Mesh " + m_pos.x.ToString() + " " + m_pos.y.ToString()
                                            + " " + m_pos.z.ToString());
                }
                m_mesh.tag                     = parent.tag;
                m_mesh.layer                   = parent.gameObject.layer;
                m_mesh.isStatic                = parent.gameObject.isStatic;
                m_mesh.transform.parent        = parent;
                m_mesh.transform.localPosition = m_pos;
                m_mesh.transform.localScale    = Vector3.one;
                mytransform                    = m_mesh.transform;

                if (col != null)
                {
                    UnityEngine.Object.DestroyImmediate(col);
                }
                if (size > 0)
                {
                    col            = m_mesh.AddComponent <MeshCollider>();
                    col.sharedMesh = mesh;
                }

                if (HasGrass == false)
                {
                    //add plant placer component to mesh object
                    plantPlacer placer = m_mesh.AddComponent <plantPlacer>();
                    //assign vertices for plant place to use to know where to place the plants and trees
                    placer.Vertices = verts;
                    //assign colors to plant placer supposed to be used to only place grass on grass area but
                    //i couldnt get it to work
                    placer.control = control;
                    placer.chunk   = this;
                    HasGrass       = true;
                }
                //set flag so engine knows it can draw mesh now
                canDraw = true;
                //nullify all vertices and tris and normals so as to not hold onto unnecassary information
                tris    = null;
                verts   = null;
                normals = null;
            }
        }