155 lines
5.2 KiB
C#
155 lines
5.2 KiB
C#
using Thousandto.Core.Asset;
|
||
using System.IO;
|
||
using UnityEngine;
|
||
|
||
namespace Thousandto.Editor.Test
|
||
{
|
||
/// <summary>
|
||
/// 地形网格的详细信息:起始位置、尺寸、索引等
|
||
/// </summary>
|
||
public class MyTerrainData
|
||
{
|
||
public float StartX { get; set; }
|
||
public float StartZ { get; set; }
|
||
|
||
//Z方向为width
|
||
public float Width { get; set; }
|
||
//X方向为height
|
||
public float Height { get; set; }
|
||
|
||
public float PixelScaleX1000 { get; set; }
|
||
|
||
//control图的uv的offset
|
||
public Vector2 Offset { get; set; }
|
||
public Vector3 TerrainSize { get; set; }
|
||
//是否Z方向边界
|
||
public bool IsRightBound { get; set; }
|
||
//是否X方向边界
|
||
public bool IsBottomBound { get; set; }
|
||
|
||
public string Index { get; set; }
|
||
|
||
public string TerrainName { get; set; }
|
||
|
||
public bool MoreLeft { get { return StartZ > 0; } }
|
||
public bool MoreRight { get { return !IsRightBound; } }
|
||
public bool MoreTop { get { return StartX > 0; } }
|
||
public bool MoreBottom { get { return !IsBottomBound; } }
|
||
|
||
|
||
private Rect _rect;
|
||
private bool _hasSetRect = false;
|
||
public Rect Rect
|
||
{
|
||
get
|
||
{
|
||
if (!_hasSetRect)
|
||
{
|
||
_rect = GetRectSize();
|
||
_hasSetRect = true;
|
||
}
|
||
return _rect;
|
||
}
|
||
}
|
||
|
||
public MyTerrainData()
|
||
{
|
||
|
||
}
|
||
|
||
public MyTerrainData(string name)
|
||
{
|
||
TerrainName = name;
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="eraseBound">消除因扩展了一格造成的影响</param>
|
||
/// <returns></returns>
|
||
public string CombineToFileName(bool eraseBound = false)
|
||
{
|
||
//index_zXxXwidthXheith_scale_isRight_isBottom
|
||
var fileName = string.Format("{2}_{8}x{9}_{0}x{1}x{4}x{5}_{3}_{6}_{7}_{10}x{11}x{12}",
|
||
StartZ, StartX, Index, PixelScaleX1000, Width, Height, IsRightBound, IsBottomBound, Offset.x, Offset.y, TerrainSize.x, TerrainSize.y, TerrainSize.z);
|
||
if(eraseBound)
|
||
{
|
||
fileName = string.Format("{2}_{8}x{9}_{0}x{1}x{4}x{5}_{3}_{6}_{7}_{10}x{11}x{12}",
|
||
MoreLeft ? StartZ + 1: StartZ,
|
||
MoreTop ? StartX + 1 : StartX, Index, PixelScaleX1000,
|
||
MoreRight ? Width - 1 : Width,
|
||
MoreBottom ? Height - 1 : Height, IsRightBound, IsBottomBound,
|
||
Offset.x, Offset.y,
|
||
TerrainSize.x, TerrainSize.y, TerrainSize.z);
|
||
|
||
fileName = TerrainName + "_" + fileName;
|
||
|
||
//这里需要重新计算索引
|
||
var data = CreateTerrainDataFromFileName(fileName, false);
|
||
data.Index = "" + Utils.CalcIndex(data.GetCenterPos(), TerrainConfigDefine.BLOCK_SIZE);
|
||
|
||
return data.CombineToFileName();
|
||
}
|
||
|
||
fileName = TerrainName + "_" + fileName;
|
||
|
||
return fileName;
|
||
}
|
||
|
||
public static MyTerrainData CreateTerrainDataFromFileName(string filePath, bool checkFileName = true)
|
||
{
|
||
UnityEngine.Debug.Log("CreateTerrainDataFromFile: " + filePath);
|
||
MyTerrainData data = new MyTerrainData("");
|
||
var fileName = filePath;
|
||
if(checkFileName)
|
||
fileName = Path.GetFileNameWithoutExtension(filePath);
|
||
var dataArray = fileName.Split('_');
|
||
var offsetArray = dataArray[2].Split('x');
|
||
var boundDataArray = dataArray[3].Split('x');
|
||
|
||
Vector2 offset = new Vector2(float.Parse(offsetArray[0]), float.Parse(offsetArray[1]));
|
||
data.Offset = offset;
|
||
|
||
data.TerrainName = dataArray[0];
|
||
data.Index = dataArray[1];
|
||
|
||
data.StartZ = float.Parse(boundDataArray[0]);
|
||
data.StartX = float.Parse(boundDataArray[1]);
|
||
data.Width = float.Parse(boundDataArray[2]);
|
||
data.Height = float.Parse(boundDataArray[3]);
|
||
|
||
data.PixelScaleX1000 = float.Parse(dataArray[4]);
|
||
|
||
data.IsRightBound = bool.Parse(dataArray[5]);
|
||
data.IsBottomBound = bool.Parse(dataArray[6]);
|
||
|
||
var terrainSizeStrArray = dataArray[7].Split('x');
|
||
Vector3 terrainSize = new Vector3(float.Parse(terrainSizeStrArray[0]), float.Parse(terrainSizeStrArray[1]), float.Parse(terrainSizeStrArray[2]));
|
||
data.TerrainSize = terrainSize;
|
||
|
||
return data;
|
||
}
|
||
|
||
public Rect GetRectSize()
|
||
{
|
||
float pixelScale = PixelScaleX1000 / 1000f;
|
||
|
||
float posX = StartX * pixelScale;
|
||
float posY = StartZ * pixelScale;
|
||
|
||
//x方向是width,所以这里要写反
|
||
float height = (Width - StartZ) * pixelScale;
|
||
float width = (Height - StartX) * pixelScale;
|
||
|
||
return new Rect(posX, posY, width, height);
|
||
}
|
||
|
||
public Vector2 GetCenterPos()
|
||
{
|
||
var rect = GetRectSize();
|
||
var centerPos2D = (rect.min + rect.max) / 2;
|
||
return centerPos2D;
|
||
}
|
||
}
|
||
}
|