示例#1
0
        private void MoveMeshesForCornerPiece(TerrainPiece currentTerrain, TerrainPiece nextTerrain, Vector3 origin)
        {
            //Because of the rotations these can overlap.  So get the difference, then add our spacing for the corners
            //TODO: Revisit this, I don't think it is working quite right
            float maxXPrevious = currentTerrain.FrontMesh.RotatedPlaneVerticies.Select(p => p.x).Max();
            float minXCurrent  = nextTerrain.FrontMesh.RotatedPlaneVerticies.Select(p => p.x).Min();
            float diff         = Math.Abs(maxXPrevious - minXCurrent);
            float spacing      = diff + settings.CornerMeshWidth;

            //Move every mesh piece in this terrain to give us space to have our corner mesh
            for (int j = 0; j < nextTerrain.MeshPieces.Count; j++)
            {
                MeshPiece mp         = nextTerrain.MeshPieces[j];
                Vector3   moveAmount = new Vector3(origin.x + spacing, origin.y, origin.z);

                if (mp.PlaneType == MeshPiece.Plane.Detail)
                {
                    //Move the detail mesh slightly in front of the front plane mesh
                    moveAmount = moveAmount + settings.DetailPlaneOffset;
                    mp.MoveMesh(moveAmount, mp.PlaneType);
                }
                else
                {
                    mp.MoveMesh(moveAmount, mp.PlaneType);
                }
            }
        }
示例#2
0
        public void UpdateAllFrontTopVerticies()
        {
            if (AllFrontTopVerticies == null)
            {
                AllFrontTopVerticies = new List <Vector3>();
            }
            else
            {
                AllFrontTopVerticies.Clear();
            }

            List <TerrainPiece> pieces = Pool.TerrainPieces.OrderBy(terp => terp.FrontMesh.BottomLeftCorner.x).ToList();

            for (int i = 0; i < pieces.Count(); i++)
            {
                TerrainPiece tp = pieces[i];

                MeshPiece mp = tp.FrontMesh;
                for (int k = 0; k < mp.RotatedPlaneVerticies.Count(); k++)
                {
                    //Evens are bottom verts, odds are top
                    if (k % 2 != 0)
                    {
                        AllFrontTopVerticies.Add(mp.RotatedPlaneVerticies[k]);
                    }
                }
            }
        }
示例#3
0
        public List <Vector3> GetCornerVerts(TerrainPiece previousTerrain, TerrainPiece currentTerrain, MeshPiece.Plane planeType, bool topVerts)
        {
            MeshPiece lastMesh    = null;
            MeshPiece currentMesh = null;

            List <Vector3> verts = new List <Vector3>();

            if (planeType == MeshPiece.Plane.Front)
            {
                lastMesh    = previousTerrain.FrontMesh;
                currentMesh = currentTerrain.FrontMesh;
            }
            else if (planeType == MeshPiece.Plane.Detail)
            {
                lastMesh    = previousTerrain.DetailMesh;
                currentMesh = currentTerrain.DetailMesh;
            }

            if (topVerts)
            {
                verts.Add(lastMesh.TopRightCorner);
                verts.Add(currentMesh.TopLeftCorner);
            }
            else
            {
                verts.Add(lastMesh.BottomRightCorner);
                verts.Add(currentMesh.BottomLeftCorner);
            }

            return(verts);
        }
示例#4
0
        public void CreateCorner(VertexGenerator vg, TerrainPiece previousTerrain, TerrainPiece currentTerrain, Transform newParent)
        {
            //Our plane is made up of the last top and bottom verts of the previous mesh, and the first top and bottom verts of the current mesh
            List <Vector3> topVerticies    = GetCornerVerts(previousTerrain, currentTerrain, MeshPiece.Plane.Front, true);
            List <Vector3> bottomVerticies = GetCornerVerts(previousTerrain, currentTerrain, MeshPiece.Plane.Front, false);

            //Create our front mesh piece
            MeshPiece meshPiece = new MeshPiece(vg, MeshPiece.Plane.Front, settings);

            meshPiece.CreateCorner(topVerticies, bottomVerticies);

            //The first mesh could be null if we are below the minimum verticies we need to create a plane
            if (meshPiece.MeshObject != null)
            {
                TransformHelpers th = new TransformHelpers();

                //Now we've created the front of our mesh
                InstantiateTerrainObject(0f, newParent);
                MeshPieces.Add(meshPiece);

                //Add detail mesh
                if (settings.DrawDetailMeshRenderer)
                {
                    MeshPiece meshPieceDetail = new MeshPiece(vg, MeshPiece.Plane.Detail, settings);
                    topVerticies    = GetCornerVerts(previousTerrain, currentTerrain, MeshPiece.Plane.Detail, true);
                    bottomVerticies = GetCornerVerts(previousTerrain, currentTerrain, MeshPiece.Plane.Detail, false);
                    meshPieceDetail.CreateCorner(topVerticies, bottomVerticies);
                    MeshPieces.Add(meshPieceDetail);
                }

                if (settings.DrawTopMeshCollider || settings.DrawTopMeshRenderer)
                {
                    //Create the verticies for the top of our mesh, and add that too
                    MeshPiece meshPieceTop = new MeshPiece(vg, MeshPiece.Plane.Top, settings);

                    //Use the top verts as our bottom z row
                    bottomVerticies = th.CopyList(topVerticies);
                    Vector3 firstBottomVertex = topVerticies[0];

                    //Then shift the top verts into the z plane
                    topVerticies = th.MoveStartVertex(topVerticies, firstBottomVertex, new Vector3(firstBottomVertex.x, firstBottomVertex.y, firstBottomVertex.z + settings.TopPlaneHeight), false);
                    meshPieceTop.CreateCorner(topVerticies, bottomVerticies);
                    if (settings.TopPhysicsMaterial2D != null)
                    {
                        meshPieceTop.edgeCollider.sharedMaterial = settings.TopPhysicsMaterial2D; // assign Physics Material if any
                    }
                    if (settings.TerrainLayer != 0)
                    {
                        meshPieceTop.MeshObject.layer = settings.TerrainLayer; // assign Layer if any
                    }
                    MeshPieces.Add(meshPieceTop);
                }

                //Just to tidy up the heirarchy
                ParentMeshesToTerrainObject();
            }
        }
示例#5
0
        //Get the farthest x point at the end of our last mesh
        public float GetFarthestX()
        {
            float        x    = 0;
            TerrainPiece last = GetLastTerrainPiece();

            if (last == null)
            {
                return(x);
            }
            return(last.NextTerrainOrigin.x);
        }
示例#6
0
        //Get the farthest x point at the end of our last mesh
        public float GetFarthestX()
        {
            float        x    = 0;
            TerrainPiece last = GetLastTerrainPiece();

            if (last == null)
            {
                return(x);
            }
            //Debug.Log(settings.TerrainManagerName + ".TerrainManager.GetFarthestX:return " + last.NextTerrainOrigin.x);
            return(last.NextTerrainOrigin.x);
        }
示例#7
0
        public TerrainPiece GenerateTerrainPiece(TerrainPiece currentTerrain, Vector3 origin)
        {
            //Don't keep generation if we have no rules left
            if (VertexGen.CurrentTerrainRule == null)
            {
                return(null);
            }

            //Create our next terrain piece (consists of multiple meshes)
            TerrainPiece nextTerrain = new TerrainPiece(settings);

            nextTerrain.Create(VertexGen, origin);

            //We can legitimately get a null terrain object if we don't have enough verts to create another.  This then moves us to the next rule.
            //Retry once to see if we get more terrain, if not, we are at the end of the rules and cannot generate any more terrain
            if (nextTerrain.TerrainObject == null && VertexGen.CurrentTerrainRule != null)
            {
                nextTerrain.Create(VertexGen, origin);
            }


            //Make sure we had enough verticies to create a terrain piece
            if (nextTerrain.TerrainObject != null)
            {
                //Make sure this is not the first piece
                if (currentTerrain != null)
                {
                    //Is our angle between pieces different?  Then we need a corner piece
                    if (currentTerrain.TerrainAngle != nextTerrain.TerrainAngle)
                    {
                        //Move our next terrain mesh pieces out some so we can fit in the corner terrain piece
                        MoveMeshesForCornerPiece(currentTerrain, nextTerrain, origin);

                        //Create corner terrain piece between the previous piece and this one
                        TerrainPiece tpCorner = new TerrainPiece(settings);
                        tpCorner.CreateCorner(VertexGen, currentTerrain, nextTerrain);
                        Pool.Add(tpCorner);
                    }
                }


                //And now create our standard piece
                Pool.Add(nextTerrain);
                return(nextTerrain);
            }
            else
            {
                return(null);
            }
        }
示例#8
0
        /// <summary>
        /// Generate terrain pieces up the the given x point in space
        /// </summary>
        /// <param name="endX"></param>
        public void Generate(float endX)
        {
            //At the end of our rules?  Stop here.
            if (VertexGen.CurrentTerrainRule == null)
            {
                return;
            }

            //The piece we are on right now (before generating a new piece)
            TerrainPiece currentTerrain = GetLastTerrainPiece();

            if (currentTerrain == null)
            {
                currentTerrain = GenerateTerrainPiece(null, settings.OriginalStartPoint);
            }

            while (currentTerrain.NextTerrainOrigin.x < endX)
            {
                //Generate our next terrain
                currentTerrain = GenerateTerrainPiece(currentTerrain, currentTerrain.NextTerrainOrigin);

                //Can't generate any more terrain?  Break out
                if (currentTerrain == null)
                {
                    break;
                }
            }

            //Create a terrain manager if we don't have one, and organize our objects
            if (!TerrainObject)
            {
                InstantiateTerrainObject(settings.TerrainManagerName);
            }

            ParentTerrainPiecesToTerrainObject();

            //Update our list of the top verticies
            UpdateAllFrontTopVerticies();
        }
示例#9
0
        /// <summary>
        /// Remove terrain pieces once the are out of view of the left side of the camera
        /// </summary>
        public void Cleanup(float beginX)
        {
            List <TerrainPiece> piecesToRemove = new List <TerrainPiece>();

            for (int i = 0; i < Pool.TerrainPieces.Count; i++)
            {
                TerrainPiece piece = Pool.TerrainPieces[i];
                if (piece.NextTerrainOrigin.x < beginX)
                {
                    piecesToRemove.Add(piece);
                }
            }
            for (int i = 0; i < piecesToRemove.Count(); i++)
            {
                Pool.Remove(piecesToRemove[i]);
            }

            if (Pool.TerrainPieces.Count == 0)
            {
                RemoveTerrainObject(settings.TerrainManagerName);
            }
        }
示例#10
0
 public void Add(TerrainPiece tp)
 {
     TerrainPieces.Add(tp);
 }
示例#11
0
 public void Add(TerrainPiece tp)
 {
     TerrainPieces.Add(tp);
 }
示例#12
0
 public void Remove(TerrainPiece tp)
 {
     TerrainPieces.Remove(tp);
     GameObject.DestroyImmediate(tp.TerrainObject);
 }
示例#13
0
        public void CreateCorner(VertexGenerator vg, TerrainPiece previousTerrain, TerrainPiece currentTerrain)
        {
            //Our plane is made up of the last top and bottom verts of the previous mesh, and the first top and bottom verts of the current mesh
            List<Vector3> topVerticies = GetCornerVerts(previousTerrain, currentTerrain, MeshPiece.Plane.Front, true);
            List<Vector3> bottomVerticies = GetCornerVerts(previousTerrain, currentTerrain, MeshPiece.Plane.Front, false);

            //Create our front mesh piece
            MeshPiece meshPiece = new MeshPiece(vg, MeshPiece.Plane.Front, settings);
            meshPiece.CreateCorner(topVerticies, bottomVerticies);

            //The first mesh could be null if we are below the minimum verticies we need to create a plane
            if (meshPiece.MeshObject != null)
            {
                TransformHelpers th = new TransformHelpers();

                //Now we've created the front of our mesh
                InstantiateTerrainObject();
                MeshPieces.Add(meshPiece);

                //Add detail mesh

                if (settings.DrawDetailMeshRenderer)
                {
                    MeshPiece meshPieceDetail = new MeshPiece(vg, MeshPiece.Plane.Detail, settings);
                    topVerticies = GetCornerVerts(previousTerrain, currentTerrain, MeshPiece.Plane.Detail, true);
                    bottomVerticies = GetCornerVerts(previousTerrain, currentTerrain, MeshPiece.Plane.Detail, false);
                    meshPieceDetail.CreateCorner(topVerticies, bottomVerticies);
                    MeshPieces.Add(meshPieceDetail);
                }

                if (settings.DrawTopMeshCollider || settings.DrawTopMeshRenderer)
                {
                    //Create the verticies for the top of our mesh, and add that too
                    MeshPiece meshPieceTop = new MeshPiece(vg, MeshPiece.Plane.Top, settings);

                    //Use the top verts as our bottom z row
                    bottomVerticies = th.CopyList(topVerticies);
                    Vector3 firstBottomVertex = topVerticies[0];

                    //Then shift the top verts into the z plane
                    topVerticies = th.MoveStartVertex(topVerticies, firstBottomVertex, new Vector3(firstBottomVertex.x, firstBottomVertex.y, firstBottomVertex.z + settings.TopPlaneHeight), false);
                    meshPieceTop.CreateCorner(topVerticies, bottomVerticies);
                    MeshPieces.Add(meshPieceTop);
                }

                //Just to tidy up the heirarchy
                ParentMeshesToTerrainObject();

            }
        }
示例#14
0
 public void Remove(TerrainPiece tp)
 {
     TerrainPieces.Remove(tp);
     GameObject.DestroyImmediate(tp.TerrainObject);
 }
示例#15
0
        private void MoveMeshesForCornerPiece(TerrainPiece currentTerrain, TerrainPiece nextTerrain, Vector3 origin)
        {
            //Because of the rotations these can overlap.  So get the difference, then add our spacing for the corners
            //TODO: Revisit this, I don't think it is working quite right
            float maxXPrevious = currentTerrain.FrontMesh.RotatedPlaneVerticies.Select(p => p.x).Max();
            float minXCurrent = nextTerrain.FrontMesh.RotatedPlaneVerticies.Select(p => p.x).Min();
            float diff = Math.Abs(maxXPrevious - minXCurrent);
            float spacing = diff + settings.CornerMeshWidth;

            //Move every mesh piece in this terrain to give us space to have our corner mesh
            for (int j = 0; j < nextTerrain.MeshPieces.Count; j++)
            {
                MeshPiece mp = nextTerrain.MeshPieces[j];
                Vector3 moveAmount = new Vector3(origin.x + spacing, origin.y, origin.z);

                if (mp.PlaneType == MeshPiece.Plane.Detail)
                {
                    //Move the detail mesh slightly in front of the front plane mesh
                   moveAmount = moveAmount + settings.DetailPlaneOffset;
                   mp.MoveMesh(moveAmount, mp.PlaneType);
                }
                else
                {
                    mp.MoveMesh(moveAmount, mp.PlaneType);
                }

            }
        }
示例#16
0
        public TerrainPiece GenerateTerrainPiece(TerrainPiece currentTerrain, Vector3 origin)
        {
            //Don't keep generation if we have no rules left
            if (VertexGen.CurrentTerrainRule == null){return null;}

            //Create our next terrain piece (consists of multiple meshes)
            TerrainPiece nextTerrain = new TerrainPiece(settings);
            nextTerrain.Create(VertexGen, origin);

            //We can legitimately get a null terrain object if we don't have enough verts to create another.  This then moves us to the next rule.
            //Retry once to see if we get more terrain, if not, we are at the end of the rules and cannot generate any more terrain
            if (nextTerrain.TerrainObject == null && VertexGen.CurrentTerrainRule != null)
            {
                nextTerrain.Create(VertexGen, origin);
            }

            //Make sure we had enough verticies to create a terrain piece
            if (nextTerrain.TerrainObject != null)
            {
                //Make sure this is not the first piece
                if (currentTerrain != null)
                {
                    //Is our angle between pieces different?  Then we need a corner piece
                    if (currentTerrain.TerrainAngle != nextTerrain.TerrainAngle)
                    {
                        //Move our next terrain mesh pieces out some so we can fit in the corner terrain piece
                        MoveMeshesForCornerPiece(currentTerrain, nextTerrain, origin);

                        //Create corner terrain piece between the previous piece and this one
                        TerrainPiece tpCorner = new TerrainPiece(settings);
                        tpCorner.CreateCorner(VertexGen, currentTerrain, nextTerrain);
                        Pool.Add(tpCorner);
                    }
                }

                //And now create our standard piece
                Pool.Add(nextTerrain);
                return nextTerrain;
            }
            else
            {
                return null;
            }
        }
示例#17
0
        public List<Vector3> GetCornerVerts(TerrainPiece previousTerrain, TerrainPiece currentTerrain, MeshPiece.Plane planeType, bool topVerts)
        {
            MeshPiece lastMesh = null;
            MeshPiece currentMesh = null;

            List<Vector3> verts = new List<Vector3>();

            if (planeType == MeshPiece.Plane.Front)
            {
                lastMesh = previousTerrain.FrontMesh;
                currentMesh = currentTerrain.FrontMesh;
            }
            else if (planeType == MeshPiece.Plane.Detail)
            {
                lastMesh = previousTerrain.DetailMesh;
                currentMesh = currentTerrain.DetailMesh;
            }

            if (topVerts)
            {
                verts.Add(lastMesh.TopRightCorner);
                verts.Add(currentMesh.TopLeftCorner);
            }
            else
            {
                verts.Add(lastMesh.BottomRightCorner);
                verts.Add(currentMesh.BottomLeftCorner);
            }

            return verts;
        }