示例#1
0
    public static void DoSplat(TerrainInfo info)
    {
        if (!ValidParameterCheck(info.TerrainParameterList))
        {
            Debug.LogError("Invalid textures for splat mapping, make sure you have textures set in the reorderable list!");
            return;
        }
        float[,,] splatmapData = new float[info.TerrainWidth, info.TerrainHeight, info.TerrainParameterList.Count];
        SplatPrototype[] splat_lists = new SplatPrototype[info.TerrainParameterList.Count];
        for (int i = 0; i < splat_lists.Length; i++)
        {
            splat_lists[i]         = new SplatPrototype();
            splat_lists[i].texture = info.TerrainParameterList[i].TerrainTexture;
        }
        info._Terrain.terrainData.splatPrototypes = splat_lists;
        for (int y = 0; y < info._Terrain.terrainData.alphamapHeight; y++)
        {
            for (int x = 0; x < info._Terrain.terrainData.alphamapWidth; x++)
            {
                // float height = terrainHeightMap[x, y];

                // Setup an array to record the mix of texture weights at this point
                float[] splatWeights = new float[info._Terrain.terrainData.alphamapLayers];
                int     idx          = BiomeGeneration.GetCorrectBiomeIndex(info, x, y);
                // if the current parameterBoundry is smaller or equal to the height means we found our boundry
                splatWeights[idx] = 0.5f;
                // blending, if we arnt at ocean/snow level
                if (idx >= 1 && idx < info.TerrainParameterList.Count - 1)
                {
                    splatWeights[idx + 1] = 0.25f;
                    splatWeights[idx - 1] = 0.25f;
                }
                // we are at ocean level, blend the sand
                else if (idx == 0)
                {
                    splatWeights[idx + 1] = 0.5f;
                }
                // we are at snow level, blend the snow
                else if (idx == info.TerrainParameterList.Count - 1)
                {
                    splatWeights[idx - 1] = 0.5f;
                }
                // Sum of all textures weights must add to 1, so calculate normalization factor from sum of weights
                // float z = splatWeights.Sum();

                // Loop through each terrain texture
                for (int i = 0; i < info._Terrain.terrainData.alphamapLayers; i++)
                {
                    // Normalize so that sum of all texture weights = 1
                    // splatWeights[i] /= z;
                    // Assign this point to the splatmap array
                    splatmapData[x, y, i] = splatWeights[i];
                }
            }
        }
        // Finally assign the new splatmap to the terrainData:
        info._Terrain.terrainData.SetAlphamaps(0, 0, splatmapData);
    }
示例#2
0
 public static Color[] GenerateHeightmapTexture(TerrainGeneration terrainGeneration, DebugPlaneType planeContent)
 {
     Color[] terrainTexture = new Color[terrainGeneration.TerrainInfo.TerrainWidth * terrainGeneration.TerrainInfo.TerrainHeight];
     for (int y = 0; y < terrainGeneration.TerrainInfo.TerrainHeight; y++)
     {
         for (int x = 0; x < terrainGeneration.TerrainInfo.TerrainWidth; x++)
         {
             if (planeContent == DebugPlaneType.kAll)
             {
                 if (terrainGeneration.TerrainInfo.TerrainTextureType == TextureType.kColored)
                 {
                     terrainTexture[y * terrainGeneration.TerrainInfo.TerrainWidth + x] = terrainGeneration.TerrainInfo.TerrainParameterList[BiomeGeneration.GetCorrectBiomeIndex(terrainGeneration.TerrainInfo, x, y)].TerrainColor;
                 }
                 else
                 {
                     var valsTogether = terrainGeneration.TerrainInfo.HeightMap[x, y] + terrainGeneration.TerrainInfo.TemperatureMap[x, y] + terrainGeneration.TerrainInfo.MoistureMap[x, y];
                     valsTogether = valsTogether / 3;
                     terrainTexture[y * terrainGeneration.TerrainInfo.TerrainWidth + x] = Color.Lerp(Color.white, Color.black, valsTogether);
                 }
             }
             else if (terrainGeneration.TerrainInfo.TerrainTextureType == TextureType.kGrayscale)
             {
                 if (planeContent == DebugPlaneType.kHeightMap)
                 {
                     terrainTexture[y * terrainGeneration.TerrainInfo.TerrainWidth + x] = Color.Lerp(Color.white, Color.black, terrainGeneration.TerrainInfo.HeightMap[x, y]);
                 }
                 else if (planeContent == DebugPlaneType.kMoistureMap)
                 {
                     terrainTexture[y * terrainGeneration.TerrainInfo.TerrainWidth + x] = Color.Lerp(Color.white, Color.black, terrainGeneration.TerrainInfo.MoistureMap[x, y]);
                 }
                 else if (planeContent == DebugPlaneType.kTemperatureMap)
                 {
                     terrainTexture[y * terrainGeneration.TerrainInfo.TerrainWidth + x] = Color.Lerp(Color.white, Color.black, terrainGeneration.TerrainInfo.TemperatureMap[x, y]);
                 }
             }
         }
     }
     return(terrainTexture);
 }
示例#3
0
    // everything looks moved one idx down, baisicly water is one idx up
    public static void DoSplat(TerrainInfo info, SeasonType seasonType = SeasonType.kUndefined, float seasonAmmount = 1.0f)
    {
        if (!ValidParameterCheck(info.TerrainParameterList))
        {
            Debug.LogError("Invalid textures for splat mapping, make sure you have textures set in the reorderable list!");
            return;
        }

        float[,,] splatmapData = new float[info.TerrainWidth, info.TerrainHeight, info.TerrainParameterList.Count + 1];
        TerrainLayer[] terrain_layers = new TerrainLayer[info.TerrainParameterList.Count + 1];
        for (int i = 0; i < terrain_layers.Length - 1; i++)
        {
            terrain_layers[i] = new TerrainLayer();
            switch (seasonType)
            {
            case SeasonType.kSpring:
                terrain_layers[i].diffuseTexture = info.TerrainParameterList[i].TerrainTextureSpring;
                break;

            case SeasonType.kSummer:
                terrain_layers[i].diffuseTexture = info.TerrainParameterList[i].TerrainTextureSummer;
                break;

            case SeasonType.kAutumn:
                terrain_layers[i].diffuseTexture = info.TerrainParameterList[i].TerrainTextureAutumn;
                break;

            case SeasonType.kWinter:
                terrain_layers[i].diffuseTexture = info.TerrainParameterList[i].TerrainTextureWinter;
                break;

            case SeasonType.kUndefined:
                terrain_layers[i].diffuseTexture = info.TerrainParameterList[i].TerrainTextureSpring;
                break;
            }
        }
        terrain_layers[terrain_layers.Length - 1] = new TerrainLayer();
        terrain_layers[terrain_layers.Length - 1].diffuseTexture = info.RoadGenerator.RoadTexture;
        info._Terrain.terrainData.terrainLayers = terrain_layers;
        var tmpRoad = new List <(int, int)>();

        for (int y = 0; y < info._Terrain.terrainData.alphamapHeight; y++)
        {
            for (int x = 0; x < info._Terrain.terrainData.alphamapWidth; x++)
            {
                float[] splatWeights = new float[info._Terrain.terrainData.alphamapLayers];
                if (info.RoadGenerator.IsRoadOnCoordinates(y, x))
                {
                    tmpRoad.Add((x, y));
                    //DoRoadSplat(ref splatmapData, x, y);
                    continue;
                }
                else
                {
                    // Setup an array to record the mix of texture weights at this point
                    int idx = BiomeGeneration.GetCorrectBiomeIndex(info, x, y);
                    splatWeights[idx] = .75f;
                    // blending, if we arnt at ocean/snow level
                    if (idx >= 1 && idx < info.TerrainParameterList.Count - 1)
                    {
                        splatWeights[idx + 1] = 0.125f;
                        splatWeights[idx - 1] = 0.125F;
                    }
                    // WE ARE AT MNT, BLEND SMTH ELSE
                    else if (idx == 0)
                    {
                        splatWeights[idx + 1] = 0.25f;
                    }
                    // we are at snow level, blend the snow
                    else if (idx == info.TerrainParameterList.Count - 1)
                    {
                        splatWeights[idx - 1] = 0.25f;
                    }
                    // Sum of all textures weights must add to 1, so calculate normalization factor from sum of weights
                }
                float z = splatWeights.Sum();

                // Loop through each terrain texture
                for (int i = 0; i < info._Terrain.terrainData.alphamapLayers; i++)
                {
                    // Normalize so that sum of all texture weights = 1
                    splatWeights[i] /= z;
                    // Assign this point to the splatmap array
                    splatmapData[x, y, i] = splatWeights[i];
                }
            }
        }
        for (int i = 0; i < tmpRoad.Count; i++)
        {
            var seg = tmpRoad[i];
            DoRoadSplat(ref splatmapData, seg.Item1, seg.Item2);
        }
        // Finally assign the new splatmap to the terrainData:
        info._Terrain.terrainData.SetAlphamaps(0, 0, splatmapData);
    }