KopMap/Assets/MindPowerSdk/TerrainLayerUtility.cs
2025-04-03 02:30:16 +08:00

64 lines
2.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using System;
using MindPowerSdk;
using UnityEngine;
public static class AtlasBuilder
{
/// <summary>
/// 将 textures 中的贴图每 countPerAtlas 张打包成一个图集,默认图集尺寸为 2048。
/// </summary>
/// <param name="textures">所有原始贴图,数量应为 countPerAtlas 的倍数</param>
/// <param name="countPerAtlas">每个图集中包含的贴图数量(例如 16</param>
/// <param name="atlasSize">图集尺寸</param>
/// <returns>打包好的图集数组</returns>
public static Texture2D[] BuildAtlases(Texture2D[] textures, int countPerAtlas, int atlasSize = 2048)
{
int atlasCount = textures.Length / countPerAtlas;
Texture2D[] atlases = new Texture2D[atlasCount];
for (int i = 0; i < atlasCount; i++)
{
Texture2D atlas = new Texture2D(atlasSize, atlasSize, TextureFormat.RGBA32, false);
Texture2D[] group = new Texture2D[countPerAtlas];
for (int j = 0; j < countPerAtlas; j++)
{
group[j] = textures[i * countPerAtlas + j];
}
// 自动打包第二个参数表示边距像素atlasSize 限制最大尺寸
atlas.PackTextures(group, 2, atlasSize);
atlases[i] = atlas;
}
return atlases;
}
}
public static class TerrainLayerBuilder
{
/// <summary>
/// 从 4 个图集创建 TerrainLayer每个图集对应一个层
/// </summary>
/// <param name="atlases">必须包含 4 个图集</param>
/// <returns>TerrainLayer 数组</returns>
public static TerrainLayer[] BuildTerrainLayers(Texture2D[] atlases)
{
if (atlases.Length != 4)
{
Debug.LogError("期望传入 4 个图集贴图");
return null;
}
TerrainLayer[] layers = new TerrainLayer[4];
for (int i = 0; i < 4; i++)
{
TerrainLayer tl = new TerrainLayer();
tl.diffuseTexture = atlases[i];
// 假设图集内为 4×4 布局,每个子贴图尺寸为 atlas 尺寸的 1/4
tl.tileSize = new Vector2(tl.diffuseTexture.width / 4f, tl.diffuseTexture.height / 4f);
layers[i] = tl;
}
return layers;
}
}