81 lines
2.4 KiB
C#
81 lines
2.4 KiB
C#
using LE_LevelEditor.Core;
|
|
using LE_LevelEditor.Events;
|
|
using UnityEngine;
|
|
|
|
namespace LE_LevelEditor.Commands
|
|
{
|
|
public class LE_CmdChangeTerrainAlphamaps : LE_CmdBase
|
|
{
|
|
private LE_TerrainManager m_terrainMgr;
|
|
|
|
private LE_TerrainManager.AlphamapData m_alphamapsDelta;
|
|
|
|
public LE_CmdChangeTerrainAlphamaps(LE_TerrainManager p_terrainMgr, LE_TerrainManager.AlphamapData p_alphamapsDelta)
|
|
{
|
|
m_terrainMgr = p_terrainMgr;
|
|
m_alphamapsDelta = p_alphamapsDelta;
|
|
m_isExecuted = true;
|
|
}
|
|
|
|
public override long GetStoredBytes()
|
|
{
|
|
if (m_alphamapsDelta != null)
|
|
{
|
|
return 20 + 4 * m_alphamapsDelta.m_alphamaps.Length;
|
|
}
|
|
return 0L;
|
|
}
|
|
|
|
public override bool Execute()
|
|
{
|
|
return base.Execute() && Apply(1f);
|
|
}
|
|
|
|
public override bool Rollback()
|
|
{
|
|
return base.Rollback() && Apply(-1f);
|
|
}
|
|
|
|
private bool Apply(float p_direction)
|
|
{
|
|
if (m_terrainMgr == null || m_alphamapsDelta == null)
|
|
{
|
|
Debug.LogError("LE_CmdChangeTerrainAlphamaps: Apply: could not execute, m_terrainMgr or m_alphamapsDelta are null!");
|
|
return false;
|
|
}
|
|
int xBase = m_alphamapsDelta.m_xBase;
|
|
int yBase = m_alphamapsDelta.m_yBase;
|
|
int length = m_alphamapsDelta.m_alphamaps.GetLength(1);
|
|
int length2 = m_alphamapsDelta.m_alphamaps.GetLength(0);
|
|
int length3 = m_alphamapsDelta.m_alphamaps.GetLength(2);
|
|
if (length > m_terrainMgr.TerrainData.alphamapWidth || length2 > m_terrainMgr.TerrainData.alphamapHeight)
|
|
{
|
|
Debug.LogError("LE_CmdChangeTerrainAlphamaps: Apply: could not execute, terrain alpha map resolution was reduced in the meantime!");
|
|
return false;
|
|
}
|
|
if (length3 > m_terrainMgr.TerrainData.alphamapLayers)
|
|
{
|
|
Debug.LogError("LE_CmdChangeTerrainAlphamaps: Apply: could not execute, terrain alpha map layers count was reduced in the meantime!");
|
|
return false;
|
|
}
|
|
float[,,] alphamaps = m_terrainMgr.TerrainData.GetAlphamaps(xBase, yBase, length, length2);
|
|
for (int i = 0; i < length; i++)
|
|
{
|
|
for (int j = 0; j < length2; j++)
|
|
{
|
|
for (int k = 0; k < length3; k++)
|
|
{
|
|
alphamaps[j, i, k] += p_direction * m_alphamapsDelta.m_alphamaps[j, i, k];
|
|
}
|
|
}
|
|
}
|
|
m_terrainMgr.TerrainData.SetAlphamaps(m_alphamapsDelta.m_xBase, m_alphamapsDelta.m_yBase, alphamaps);
|
|
if (LE_EventInterface.OnChangeLevelData != null)
|
|
{
|
|
LE_EventInterface.OnChangeLevelData(m_terrainMgr, new LE_LevelDataChangedEvent(LE_ELevelDataChangeType.TERRAIN_ALPHAMAPS));
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|