Exemple #1
0
    /// <summary>
    /// 利用笔刷设置贴图
    /// </summary>
    public static async void SetTextureWithBrush(Vector3 point, int index, float radius, int brushIndex)
    {
        Vector3 down    = new Vector3(point.x - radius, 0, point.z - radius);
        Terrain terrain = GetTerrain(point);

        if (terrain != null)
        {
            Vector2Int mapIndex  = GetAlphaMapIndex(terrain, down);
            int        mapRadius = (int)(radius / terrain.terrainData.size.x * terrain.terrainData.alphamapResolution);
            float[,,] map  = terrain.terrainData.GetAlphamaps(mapIndex.x, mapIndex.y, 2 * mapRadius, 2 * mapRadius);
            float[,] brush = await Math2d.ZoomBilinearInterpAsync(brushDic[brushIndex], 2 *mapRadius, 2 *mapRadius);

            // 这么计算所附加的贴图实际上是有问题的,但是我懒得继续算了
            for (int i = 0; i < map.GetLength(0); i++)
            {
                for (int j = 0; j < map.GetLength(1); j++)
                {
                    float temp = (1 - brush[i, j]) / (terrain.terrainData.alphamapLayers - 1);
                    for (int k = 0; k < terrain.terrainData.alphamapLayers; k++)
                    {
                        map[i, j, k] = temp;
                    }
                    map[i, j, index] = 1 - temp;
                }
            }
            terrain.terrainData.SetAlphamaps(mapIndex.x, mapIndex.y, map);
        }
    }
Exemple #2
0
    /// <summary>
    /// 通过自定义笔刷编辑地形
    /// </summary>
    /// <param name="center">中心点</param>
    /// <param name="radius">半径</param>
    /// <param name="opacity">力度</param>
    /// <param name="brushIndex">笔刷索引</param>
    /// <param name="isRise">抬高还是降低</param>
    public static async void ChangeHeightWithBrush(Vector3 center, float radius, float opacity, int brushIndex = 0, bool isRise = true)
    {
        HMArg   arg;
        Terrain terrain = InitHMArg(center, radius, out arg);

        if (terrain == null)
        {
            return;
        }

        // 是否反转透明度
        if (!isRise)
        {
            opacity = -opacity;
        }

        //修改高度图
        //float[,] deltaMap = await Utility.BilinearInterp(brushDic[brushIndex], 2 * mapRadius, 2 * mapRadius);
        float[,] deltaMap = await Math2d.ZoomBilinearInterpAsync(brushDic[brushIndex], 2 *arg.mapRadiusX, 2 *arg.mapRadiusX);

        for (int i = 0; i < 2 * arg.mapRadiusX; i++)
        {
            for (int j = 0; j < 2 * arg.mapRadiusX; j++)
            {
                arg.heightMap[i, j] += deltaMap[i, j] * deltaHeight * opacity;
            }
        }

        // 重新设置高度图
        SetHeightMap(terrain, arg.heightMap, arg.startMapIndex.x, arg.startMapIndex.y);
    }
        /// <summary>
        /// 通过自定义笔刷编辑地形
        /// </summary>
        /// <param name="center">中心点</param>
        /// <param name="radius">半径</param>
        /// <param name="opacity">力度</param>
        /// <param name="brushIndex">笔刷索引</param>
        /// <param name="isRise">抬高还是降低</param>
        private async void InternalChangeHeightWithBrush(Vector3 center, float radius, float opacity, int brushIndex, bool isRise, bool regesterUndo, bool immediate)
        {
            if (!TryGetHeightMapCmd(center, radius, out HeightCmdData arg))
            {
                return;
            }

            if (regesterUndo)
            {
                RegisterUndo(new HeightCmd(new HeightCmdData(arg)));
            }

            // 是否反转透明度
            if (!isRise)
            {
                opacity = -opacity;
            }

            //修改高度图
            //float[,] deltaMap = await Utility.BilinearInterp(brushDic[brushIndex], 2 * mapRadius, 2 * mapRadius);
            float[,] deltaMap = await Math2d.ZoomBilinearInterpAsync(brushDic[brushIndex], 2 *arg.mapRadiusX, 2 *arg.mapRadiusX);

            for (int i = 0; i < 2 * arg.mapRadiusX; i++)
            {
                for (int j = 0; j < 2 * arg.mapRadiusX; j++)
                {
                    arg.heightMap[i, j] += deltaMap[i, j] * deltaHeight * opacity;
                }
            }

            // 重新设置高度图
            SetHeightMap(arg.terrain, arg.heightMap, arg.startMapIndex.x, arg.startMapIndex.y, immediate);
        }