Beispiel #1
0
    public TerrainModifier(Terrain terrain, List <GeneratedElement> elements)
    {
        this.terrain = terrain;

        float[] cpsLeft  = new float[elements.Count];
        float[] cpsRight = new float[elements.Count];

        for (int i = 0; i < elements.Count; i++)
        {
            Vector3 toRight = (Quaternion.Euler(0f, (elements[i].Direction * 180f) / Mathf.PI, 0f)) * (new Vector3(elements[i].WidthEnd, 0f, 0f));

            Vector3 samplePosLeft  = elements[i].Position - toRight;
            Vector3 samplePosRight = elements[i].Position + toRight;

            cpsLeft[i]  = terrain.SampleHeight(samplePosLeft);
            cpsRight[i] = terrain.SampleHeight(samplePosRight);
        }

        heightsLeft  = new ClosedSpline <float>(cpsLeft);
        heightsRight = new ClosedSpline <float>(cpsRight);

        int   cpsAmount = 2000;
        float scale     = 10f;

        float[,] cps = new float[cpsAmount + 1, cpsAmount + 1];

        for (int x = cpsAmount / -2; x <= cpsAmount / 2; x++)
        {
            for (int y = cpsAmount / -2; y <= cpsAmount / 2; y++)
            {
                cps[x + cpsAmount / 2, y + cpsAmount / 2] = terrain.SampleHeight(new Vector3(x * scale, 0f, y * scale));
            }
        }

        tempGeneratedCPs = cps;

        tensorProductPlane = new TensorProductPlane(new Vector3((cpsAmount / -2) * scale, 0f, (cpsAmount / -2) * scale), scale, cps);
    }
Beispiel #2
0
    public TerrainModifier(Terrain terrain)
    {
        sectors = new List <TensorProductPlane>();

        this.terrain = terrain;

        int   cpsAmount = 2000;
        float scale     = 10f;

        float[,] cps = new float[cpsAmount + 1, cpsAmount + 1];

        for (int x = cpsAmount / -2; x <= cpsAmount / 2; x++)
        {
            for (int y = cpsAmount / -2; y <= cpsAmount / 2; y++)
            {
                cps[x + cpsAmount / 2, y + cpsAmount / 2] = terrain.SampleHeight(new Vector3(x * scale, 0f, y * scale));
            }
        }

        tempGeneratedCPs = cps;

        tensorProductPlane = new TensorProductPlane(new Vector3((cpsAmount / -2) * scale, 0f, (cpsAmount / -2) * scale), scale, cps);
    }
Beispiel #3
0
    public void FillSectorsByElements(List <Vector3> verticesAbs, int sectorIndex, float[,] predefinedHeights)
    {
        int   cpsAmount = 2000;
        float scale     = 10f;

        float[,] cps           = predefinedHeights;
        float[,] trackDistance = new float[cpsAmount + 1, cpsAmount + 1];
        for (int x = cpsAmount / -2; x <= cpsAmount / 2; x++)
        {
            for (int y = cpsAmount / -2; y <= cpsAmount / 2; y++)
            {
                trackDistance[x + cpsAmount / 2, y + cpsAmount / 2] = -1f;
            }
        }

        /*float[,] cps = new float[cpsAmount + 1, cpsAmount + 1];
         *
         * for (int x = cpsAmount / -2; x <= cpsAmount / 2; x++)
         * {
         *  for (int y = cpsAmount / -2; y <= cpsAmount / 2; y++)
         *  {
         *      cps[x + cpsAmount / 2, y + cpsAmount / 2] = terrain.SampleHeight(new Vector3(x * scale, 0f, y * scale));
         *  }
         * }*/

        Vector3[] vertices = new Vector3[verticesAbs.Count];
        for (int i = 0; i < verticesAbs.Count; i++)
        {
            vertices[i] = verticesAbs[i] - (new Vector3((cpsAmount / -2) * scale, 0f, (cpsAmount / -2) * scale));

            int castX = (int)(vertices[i].x / 10f);
            int castZ = (int)(vertices[i].z / 10f);

            trackDistance[castX - 1, castZ - 1] = (float)(((int)i / (int)2));
            trackDistance[castX - 1, castZ]     = (float)(((int)i / (int)2));
            trackDistance[castX - 1, castZ + 1] = (float)(((int)i / (int)2));
            trackDistance[castX - 1, castZ + 2] = (float)(((int)i / (int)2));
            trackDistance[castX, castZ + 2]     = (float)(((int)i / (int)2));
            trackDistance[castX + 1, castZ + 2] = (float)(((int)i / (int)2));
            trackDistance[castX + 2, castZ + 2] = (float)(((int)i / (int)2));
            trackDistance[castX + 2, castZ + 1] = (float)(((int)i / (int)2));
            trackDistance[castX + 2, castZ]     = (float)(((int)i / (int)2));
            trackDistance[castX + 2, castZ - 1] = (float)(((int)i / (int)2));
            trackDistance[castX + 1, castZ - 1] = (float)(((int)i / (int)2));
            trackDistance[castX, castZ - 1]     = (float)(((int)i / (int)2));
            trackDistance[castX, castZ]         = (float)(((int)i / (int)2));
            trackDistance[castX, castZ + 1]     = (float)(((int)i / (int)2));
            trackDistance[castX + 1, castZ]     = (float)(((int)i / (int)2));
            trackDistance[castX + 1, castZ + 1] = (float)(((int)i / (int)2));


            //vertices[i] = vertices[i] / scale;
        }


        float minX = float.MaxValue;
        float maxX = float.MinValue;
        float minZ = float.MaxValue;
        float maxZ = float.MinValue;

        for (int i = 0; i < vertices.Length; i++)
        {
            if (vertices[i].x < minX)
            {
                minX = vertices[i].x;
            }
            if (vertices[i].x > maxX)
            {
                maxX = vertices[i].x;
            }
            if (vertices[i].z < minZ)
            {
                minZ = vertices[i].z;
            }
            if (vertices[i].z > maxZ)
            {
                maxZ = vertices[i].z;
            }
        }

        minX = minX / 10f;
        maxX = maxX / 10f;
        minZ = minZ / 10f;
        maxZ = maxZ / 10f;

        int intMaxX = ((int)maxX) + 1;
        int intMinX = ((int)minX) - 1;
        int intMaxZ = ((int)maxZ) + 1;
        int intMinZ = ((int)minZ) - 1;

        for (int x = intMinX; x <= intMaxX; x++)
        {
            for (int z = intMinZ; z <= intMaxZ; z++)
            {
                recRaiseCps(cps, x, z, scale, trackDistance);
            }
        }


        if (sectorIndex >= sectors.Count)
        {
            sectors.Add(new TensorProductPlane(new Vector3((cpsAmount / -2) * scale, 0f, (cpsAmount / -2) * scale), scale, cps));
        }
        else
        {
            sectors[sectorIndex] = new TensorProductPlane(new Vector3((cpsAmount / -2) * scale, 0f, (cpsAmount / -2) * scale), scale, cps);
        }
    }