Exemplo n.º 1
0
 public PrefabManager(TerrainSettings s)
 {
     th       = new TransformHelpers();
     Pool     = new PrefabPool();
     settings = s;
     InstantiatePrefabManagerObject();
 }
Exemplo n.º 2
0
 public PrefabManager(Settings s)
 {
     th = new TransformHelpers();
     Pool = new PrefabPool();
     settings = s;
     InstantiatePrefabManagerObject();
 }
Exemplo n.º 3
0
        public List <Vector3> GenerateCalculatedVertices(List <Vector3> keyVertices)
        {
            List <Vector3>   allVerticies = new List <Vector3>();
            TransformHelpers th           = new TransformHelpers();

            //Start by inserting after the first key point
            int insertIndex = 1;

            for (int i = 0; i < keyVertices.Count - 1; i++)
            {
                //Add the key vert
                allVerticies.Add(keyVertices[i]);

                Vector3 currentVertex = keyVertices[i];
                Vector3 nextVertex    = keyVertices[i + 1];

                float x0 = currentVertex.x;
                float x1 = nextVertex.x;

                float y0 = currentVertex.y;
                float y1 = nextVertex.y;


                //How many segments between our two key points
                int totalSegments = Mathf.CeilToInt(Mathf.Ceil(x1 - x0) / CalculatedVertexSpacing);

                //The width of each of these segments
                float segmentWidth = (x1 - x0) / totalSegments;


                for (int j = 1; j < totalSegments; j++)
                {
                    float newX = x0 + j * segmentWidth;

                    //Calculate our new y value by cosine interpolation
                    float mu   = (float)j / (float)totalSegments;
                    float newY = th.CosineInterpolate(y0, y1, mu);

                    Vector3 newVert = new Vector3(newX, newY, settings.OriginalStartPoint.z);
                    allVerticies.Insert(insertIndex, newVert);

                    //Move to the next calculated point
                    insertIndex += 1;
                }

                //Jump over the key point and move on to the next calculated point
                insertIndex += 1;

                if (i == keyVertices.Count - 2)
                {
                    allVerticies.Add(nextVertex);
                }
            }



            return(allVerticies);
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 5
0
 public PrefabManager(Settings s, Transform parentTransform)
 {
     th       = new TransformHelpers();
     Pool     = new PrefabPool();
     settings = s;
     for (int i = 0; i < settings.PrefabRules.Count(); i++)
     {
         settings.PrefabRules[i].StartLocation      = Vector3.zero;
         settings.PrefabRules[i].CurrentLocation    = Vector3.zero;
         settings.PrefabRules[i].LastPrefabLocation = Vector3.zero;
     }
     this.parentTransform = parentTransform;
     InstantiatePrefabManagerObject(ManagerName);
 }
Exemplo n.º 6
0
        public void MoveMesh(Vector3 move, Plane planeType)
        {
            TransformHelpers th = new TransformHelpers();

            //Update all the verticies
            KeyTopVerticies       = th.MoveStartVertex(KeyTopVerticies, move, false, planeType);
            KeyBottomVerticies    = th.MoveStartVertex(KeyBottomVerticies, move, false, planeType);
            AllTopVerticies       = th.MoveStartVertex(AllTopVerticies, move, false, planeType);
            AllBottomVerticies    = th.MoveStartVertex(AllBottomVerticies, move, false, planeType);
            PlaneVerticies        = th.MoveStartVertex(PlaneVerticies, move, false, planeType);
            RotatedPlaneVerticies = th.MoveStartVertex(RotatedPlaneVerticies, move, false, planeType);

            //Now clear and update the mesh
            CreateMesh();
        }
Exemplo n.º 7
0
        private List <Vector2> GetUVMapping(List <Vector3> planeVerticies)
        {
            TransformHelpers th = new TransformHelpers();

            float textureHeight = 1;
            float textureWidth  = 1;


            float xTiling = 1f;
            float yTiling = 1f;

            //The uv tiling also has to factor in the height and width of the textures we are using
            if (settings.MainMaterial != null && settings.MainMaterial.mainTexture != null)
            {
                textureHeight = settings.MainMaterial.mainTexture.height;
                textureWidth  = settings.MainMaterial.mainTexture.width;
            }

            if (settings.TopMaterial != null && settings.TopMaterial.mainTexture != null)
            {
                textureHeight = settings.TopMaterial.mainTexture.height;
                textureWidth  = settings.TopMaterial.mainTexture.width;
            }

            if (settings.DetailMaterial != null && settings.DetailMaterial.mainTexture != null)
            {
                textureHeight = settings.DetailMaterial.mainTexture.height;
                textureWidth  = settings.DetailMaterial.mainTexture.width;
            }

            //Set our tiling depending on the plane
            if (PlaneType == Plane.Front)
            {
                xTiling = settings.MainMaterialXTiling;
                yTiling = settings.MainMaterialYTiling;
            }

            if (PlaneType == Plane.Top)
            {
                xTiling = settings.TopMaterialXTiling;
                yTiling = settings.TopMaterialYTiling;
            }

            if (PlaneType == Plane.Detail)
            {
                xTiling = settings.DetailMaterialXTiling;
                yTiling = settings.DetailMaterialYTiling;
            }


            //Tile the texture as needed
            textureWidth  = textureWidth / xTiling;
            textureHeight = textureHeight / yTiling;

            //Track how far along we are on the top texture mapping
            float currentTopTextureX = 0;


            List <Vector2> uvs = new List <Vector2>();

            for (int i = 0; i < planeVerticies.Count; i++)
            {
                Vector3 vertex         = planeVerticies[i];
                Vector3 previousVertex = Vector3.zero;
                if (i > 0)
                {
                    previousVertex = planeVerticies[i - 1];
                }


                //Our standard uv mapping is just our point in space divided by the width and the height of our texture (assuming an x/y plane)
                float xMapping = vertex.x / textureWidth;
                float yMapping = vertex.y / textureHeight;


                if (PlaneType == Plane.Top)
                {
                    //We have to factor in the rise in y (from the lowest point in the list to our current point), as well as the x movement across
                    //in our uv mapping.  This is because this is not a flat plane

                    //The first time through, set our current x position based off the vertex
                    if (currentTopTextureX == 0)
                    {
                        currentTopTextureX = vertex.x;
                    }
                    xMapping = (currentTopTextureX) / textureWidth;

                    //After that, increment it by the length of the line between the two points (which includes the movement in the x and y plane) in the uv mapping
                    if (previousVertex != Vector3.zero)
                    {
                        float length = GetLengthOfLine(vertex, previousVertex);
                        currentTopTextureX += length;
                        xMapping            = (currentTopTextureX) / textureWidth;
                    }

                    //For the y mapping, since we are in the z plane divide the texture height by z instead of y.
                    yMapping = vertex.z / textureHeight;
                }


                //If we want the uv mapping to follow the curve of the plane instead, set it here
                if (PlaneType == Plane.Front && settings.MainPlaneFollowTerrainCurve)
                {
                    if (i % 2 == 0)
                    {
                        yMapping = -settings.MainPlaneHeight / textureHeight;
                    }
                    else
                    {
                        yMapping = 1;
                    }
                }

                if (PlaneType == Plane.Detail && settings.DetailPlaneFollowTerrainCurve)
                {
                    if (i % 2 == 0)
                    {
                        yMapping = -settings.DetailPlaneHeight / textureHeight;
                    }
                    else
                    {
                        yMapping = 1;
                    }
                }

                //Finally set the actual uv mapping
                Vector2 uv = new Vector2(xMapping, yMapping);


                //Now set the rotation of the uv mapping
                if (settings.MainMaterialRotation != 0 && PlaneType == Plane.Front)
                {
                    uv = th.RotateVertex(uv, settings.MainMaterialRotation);
                }

                if (settings.DetailMaterialRotation != 0 && PlaneType == Plane.Detail)
                {
                    uv = th.RotateVertex(uv, settings.DetailMaterialRotation);
                }


                if (settings.TopMaterialRotation != 0 && PlaneType == Plane.Top)
                {
                    uv = th.RotateVertex(uv, settings.TopMaterialRotation);
                }

                uvs.Add(uv);
            }

            return(uvs);
        }
Exemplo n.º 8
0
        private void SetVerticies(Vector3 origin, float angle, List <Vector3> keyVerticies)
        {
            if (keyVerticies != null)
            {
                KeyTopVerticies = keyVerticies;
            }

            //Don't try and set the vertices if we're below the min we need for a plane
            if (BelowMinVerts())
            {
                return;
            }
            if (vg.CurrentTerrainRule == null)
            {
                return;
            }


            TransformHelpers th = new TransformHelpers();

            AllTopVerticies = vg.GenerateCalculatedVertices(KeyTopVerticies);

            //For the front plane, set a fixed lower boundary on the verts (bottom of mesh will be a straight line)
            if (PlaneType == Plane.Front)
            {
                Vector3 firstBottomVertex = AllTopVerticies[0];
                Vector3 shift             = new Vector3(firstBottomVertex.x, firstBottomVertex.y - settings.MainPlaneHeight, firstBottomVertex.z);

                AllBottomVerticies = th.MoveStartVertex(AllTopVerticies, AllTopVerticies[0], shift, true);
                KeyBottomVerticies = th.MoveStartVertex(KeyTopVerticies, AllTopVerticies[0], shift, true);

                //Test perp verts
                //Vector3 shift = new Vector3(firstBottomVertex.x, firstBottomVertex.y, firstBottomVertex.z);
                //AllBottomVerticies = th.GetPerpendicularOffset(AllTopVerticies, settings.MainPlaneHeight);
                //KeyBottomVerticies = th.GetPerpendicularOffset(KeyTopVerticies, settings.MainPlaneHeight);
            }

            if (PlaneType == Plane.Detail)
            {
                Vector3 firstBottomVertex = th.CopyVertex(AllTopVerticies[0]);
                Vector3 shift             = new Vector3(firstBottomVertex.x, firstBottomVertex.y - settings.DetailPlaneHeight, firstBottomVertex.z);

                AllBottomVerticies = th.MoveStartVertex(AllTopVerticies, firstBottomVertex, shift, true);
                KeyBottomVerticies = th.MoveStartVertex(KeyTopVerticies, firstBottomVertex, shift, true);
            }

            //For the top of the mesh, shift the verticies in the z direction
            if (PlaneType == Plane.Top)
            {
                //The bottom verts are a copy of the top
                AllBottomVerticies = th.CopyList(AllTopVerticies);
                KeyBottomVerticies = th.CopyList(KeyTopVerticies);

                Vector3 firstBottomVertex = AllTopVerticies[0];

                //Then shift the top verts into the z plane
                AllTopVerticies = th.MoveStartVertex(AllTopVerticies, AllTopVerticies[0], new Vector3(firstBottomVertex.x, firstBottomVertex.y, firstBottomVertex.z + settings.TopPlaneHeight), false);
            }


            //Now stich top and bottom verticies together into a plane
            PlaneVerticies = vg.GetPlaneVerticies(AllBottomVerticies, AllTopVerticies);


            //For the top plane we have to move our point of origin based on the plane height
            if (PlaneType == Plane.Top)
            {
                origin = new Vector3(origin.x, origin.y, origin.z + settings.TopPlaneHeight);
            }


            //Now move the whole plane to the point of origin (usually where the last mesh ended)
            //Move relative to the vertex at index 1 (the top of the plane) instead of zero (the bottom of the plane) since we want to match the top of the meshes
            PlaneVerticies = th.MoveStartVertex(PlaneVerticies, PlaneVerticies[1], origin, false);

            //Store the rotated verticies so we know where the actual end point of the mesh is (and where to start the next one)
            //Create the mesh from the rotate verticies, but generate it from the non-rotated ones
            if (angle != 0)
            {
                RotatedPlaneVerticies = th.RotateVertices(PlaneVerticies, angle);
                RotatedPlaneVerticies = th.MoveStartVertex(RotatedPlaneVerticies, RotatedPlaneVerticies[1], origin, false);
            }
            else
            {
                RotatedPlaneVerticies = PlaneVerticies;
            }
        }
Exemplo n.º 9
0
        private List<Vector2> GetUVMapping(List<Vector3> planeVerticies)
        {

            TransformHelpers th = new TransformHelpers();
			
			float textureHeight = 1;
			float textureWidth = 1;


            float xTiling = 1f;
            float yTiling = 1f;

            //The uv tiling also has to factor in the height and width of the textures we are using
			if (settings.MainMaterial!= null && settings.MainMaterial.mainTexture != null){	        
	             textureHeight = settings.MainMaterial.mainTexture.height;
	             textureWidth = settings.MainMaterial.mainTexture.width;
			}
    
            if (settings.TopMaterial !=null && settings.TopMaterial.mainTexture != null)
            {
                textureHeight = settings.TopMaterial.mainTexture.height;
                textureWidth = settings.TopMaterial.mainTexture.width;
            }

            if (settings.DetailMaterial != null && settings.DetailMaterial.mainTexture != null)
            {
                textureHeight = settings.DetailMaterial.mainTexture.height;
                textureWidth = settings.DetailMaterial.mainTexture.width;
            }

            //Set our tiling depending on the plane
            if (PlaneType == Plane.Front)
            {
                xTiling = settings.MainMaterialXTiling;
                yTiling = settings.MainMaterialYTiling;
            }

            if (PlaneType == Plane.Top)
            {         
                xTiling = settings.TopMaterialXTiling;
                yTiling = settings.TopMaterialYTiling;
            }

            if (PlaneType == Plane.Detail)
            {
                xTiling = settings.DetailMaterialXTiling;
                yTiling = settings.DetailMaterialYTiling;
            }


            //Tile the texture as needed
            textureWidth = textureWidth / xTiling;
            textureHeight = textureHeight / yTiling;

            //Track how far along we are on the top texture mapping
            float currentTopTextureX = 0;


            List<Vector2> uvs = new List<Vector2>();
            for (int i = 0; i < planeVerticies.Count; i++)
            {
                Vector3 vertex = planeVerticies[i];
                Vector3 previousVertex = Vector3.zero;
                if (i > 0)
                {
                    previousVertex = planeVerticies[i - 1];
                }
               

                //Our standard uv mapping is just our point in space divided by the width and the height of our texture (assuming an x/y plane)
                float xMapping = vertex.x / textureWidth;
                float yMapping = vertex.y / textureHeight;

             
                if (PlaneType == Plane.Top)
                {
                    //We have to factor in the rise in y (from the lowest point in the list to our current point), as well as the x movement across
                    //in our uv mapping.  This is because this is not a flat plane

                    //The first time through, set our current x position based off the vertex
                    if (currentTopTextureX == 0) { currentTopTextureX = vertex.x; }              
                    xMapping = (currentTopTextureX) / textureWidth;

                    //After that, increment it by the length of the line between the two points (which includes the movement in the x and y plane) in the uv mapping
                    if (previousVertex != Vector3.zero)
                    {
                        float length = GetLengthOfLine(vertex, previousVertex);
                        currentTopTextureX += length;
                        xMapping = (currentTopTextureX) / textureWidth;
                    }

                    //For the y mapping, since we are in the z plane divide the texture height by z instead of y.
                    yMapping = vertex.z / textureHeight;
                }


                //If we want the uv mapping to follow the curve of the plane instead, set it here
                if (PlaneType == Plane.Front && settings.MainPlaneFollowTerrainCurve)
                {
                    if (i % 2 == 0)
                    {
                        yMapping = -settings.MainPlaneHeight / textureHeight;
                    }
                    else
                    {
                        yMapping = 1;
                    }
                }

                if (PlaneType == Plane.Detail && settings.DetailPlaneFollowTerrainCurve)
                {
                    if (i % 2 == 0)
                    {
                        yMapping = -settings.DetailPlaneHeight / textureHeight;
                    }
                    else
                    {
                        yMapping = 1;
                    }
                }

                //Finally set the actual uv mapping
                Vector2 uv = new Vector2(xMapping, yMapping);


                //Now set the rotation of the uv mapping
                if (settings.MainMaterialRotation != 0 && PlaneType == Plane.Front)
                {
                    uv = th.RotateVertex(uv, settings.MainMaterialRotation);
                }

                if (settings.DetailMaterialRotation != 0 && PlaneType == Plane.Detail)
                {
                    uv = th.RotateVertex(uv, settings.DetailMaterialRotation);
                }


                if (settings.TopMaterialRotation != 0 && PlaneType == Plane.Top)
                {
                    uv = th.RotateVertex(uv, settings.TopMaterialRotation);
                }

                uvs.Add(uv);
            }

            return uvs;

        }
Exemplo n.º 10
0
        private void SetVerticies(Vector3 origin, float angle, List<Vector3> keyVerticies)
        {
			if (keyVerticies !=null){KeyTopVerticies = keyVerticies;}
			
			//Don't try and set the vertices if we're below the min we need for a plane
			if (BelowMinVerts()){return;}
            if (vg.CurrentTerrainRule == null) { return; }

          
            TransformHelpers th = new TransformHelpers();

            AllTopVerticies = vg.GenerateCalculatedVertices(KeyTopVerticies);

            //For the front plane, set a fixed lower boundary on the verts (bottom of mesh will be a straight line)
            if (PlaneType == Plane.Front)
            {               
                Vector3 firstBottomVertex = AllTopVerticies[0];
                Vector3 shift = new Vector3(firstBottomVertex.x, firstBottomVertex.y - settings.MainPlaneHeight, firstBottomVertex.z);

                AllBottomVerticies = th.MoveStartVertex(AllTopVerticies, AllTopVerticies[0], shift, true);
                KeyBottomVerticies = th.MoveStartVertex(KeyTopVerticies, AllTopVerticies[0], shift, true);

                //Test perp verts
                //Vector3 shift = new Vector3(firstBottomVertex.x, firstBottomVertex.y, firstBottomVertex.z);
                //AllBottomVerticies = th.GetPerpendicularOffset(AllTopVerticies, settings.MainPlaneHeight);
                //KeyBottomVerticies = th.GetPerpendicularOffset(KeyTopVerticies, settings.MainPlaneHeight);
            }

            if (PlaneType == Plane.Detail)
            {
                Vector3 firstBottomVertex = th.CopyVertex(AllTopVerticies[0]);
                Vector3 shift = new Vector3(firstBottomVertex.x, firstBottomVertex.y - settings.DetailPlaneHeight, firstBottomVertex.z);

                AllBottomVerticies = th.MoveStartVertex(AllTopVerticies,firstBottomVertex, shift, true);
                KeyBottomVerticies = th.MoveStartVertex(KeyTopVerticies, firstBottomVertex, shift, true);
            }

            //For the top of the mesh, shift the verticies in the z direction
            if (PlaneType == Plane.Top)
            {
               
                //The bottom verts are a copy of the top
                AllBottomVerticies = th.CopyList(AllTopVerticies);
                KeyBottomVerticies = th.CopyList(KeyTopVerticies);
				
				Vector3 firstBottomVertex = AllTopVerticies[0];

                //Then shift the top verts into the z plane
                AllTopVerticies = th.MoveStartVertex(AllTopVerticies, AllTopVerticies[0], new Vector3(firstBottomVertex.x, firstBottomVertex.y, firstBottomVertex.z + settings.TopPlaneHeight), false);
            }


            //Now stich top and bottom verticies together into a plane
            PlaneVerticies = vg.GetPlaneVerticies(AllBottomVerticies, AllTopVerticies);


            //For the top plane we have to move our point of origin based on the plane height
            if (PlaneType == Plane.Top)
            {
                origin = new Vector3(origin.x, origin.y, origin.z + settings.TopPlaneHeight);
            }

			
			//Now move the whole plane to the point of origin (usually where the last mesh ended)
            //Move relative to the vertex at index 1 (the top of the plane) instead of zero (the bottom of the plane) since we want to match the top of the meshes
            PlaneVerticies = th.MoveStartVertex(PlaneVerticies, PlaneVerticies[1], origin, false);

            //Store the rotated verticies so we know where the actual end point of the mesh is (and where to start the next one)
            //Create the mesh from the rotate verticies, but generate it from the non-rotated ones
            if (angle!=0)
            {
                RotatedPlaneVerticies = th.RotateVertices(PlaneVerticies, angle);	
			    RotatedPlaneVerticies = th.MoveStartVertex(RotatedPlaneVerticies, RotatedPlaneVerticies[1], origin, false);
            }
            else
            {
                RotatedPlaneVerticies = PlaneVerticies;
            }

           
        }
Exemplo n.º 11
0
        public void MoveMesh(Vector3 move, Plane planeType)
        {
            TransformHelpers th = new TransformHelpers();

            //Update all the verticies
            KeyTopVerticies = th.MoveStartVertex(KeyTopVerticies, move, false, planeType);
            KeyBottomVerticies = th.MoveStartVertex(KeyBottomVerticies, move, false, planeType);
            AllTopVerticies = th.MoveStartVertex(AllTopVerticies, move, false, planeType);
            AllBottomVerticies = th.MoveStartVertex(AllBottomVerticies, move, false, planeType);
            PlaneVerticies = th.MoveStartVertex(PlaneVerticies, move, false, planeType);
            RotatedPlaneVerticies = th.MoveStartVertex(RotatedPlaneVerticies, move, false, planeType);

            //Now clear and update the mesh
            CreateMesh();
        }
Exemplo n.º 12
0
        public List<Vector3> GenerateCalculatedVertices(List<Vector3> keyVertices)
        {
            List<Vector3> allVerticies = new List<Vector3>();
            TransformHelpers th = new TransformHelpers();

            //Start by inserting after the first key point
            int insertIndex = 1;

            for (int i = 0; i < keyVertices.Count - 1; i++)
            {

                //Add the key vert
                allVerticies.Add(keyVertices[i]);

                Vector3 currentVertex = keyVertices[i];
                Vector3 nextVertex = keyVertices[i + 1];

                float x0 = currentVertex.x;
                float x1 = nextVertex.x;

                float y0 = currentVertex.y;
                float y1 = nextVertex.y;

                //How many segments between our two key points
                int totalSegments = Mathf.CeilToInt(Mathf.Ceil(x1 - x0) / CalculatedVertexSpacing);

                //The width of each of these segments
                float segmentWidth = (x1 - x0) / totalSegments;

                for (int j = 1; j < totalSegments; j++)
                {
                    float newX = x0 + j * segmentWidth;

                    //Calculate our new y value by cosine interpolation
                    float mu = (float)j / (float)totalSegments;
                    float newY = th.CosineInterpolate(y0, y1, mu);

                    Vector3 newVert = new Vector3(newX, newY, settings.OriginalStartPoint.z);
                    allVerticies.Insert(insertIndex, newVert);

                    //Move to the next calculated point
                    insertIndex += 1;
                }

                //Jump over the key point and move on to the next calculated point
                insertIndex += 1;

                if (i == keyVertices.Count - 2)
                {
                    allVerticies.Add(nextVertex);
                }
            }

            return allVerticies;
        }
Exemplo n.º 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();

            }
        }