fix offset mask logic

This commit is contained in:
Jose Luis Montañes Ojados
2026-01-06 16:15:35 +01:00
parent 7e598d765a
commit 42f1c86129
7 changed files with 144 additions and 32 deletions

164
Class1.cs
View File

@@ -12,11 +12,13 @@ namespace StrategicMapPlus
[BepInPlugin("com.mod.strategicmapplus", "Strategic Map Plus", "28.0.0")]
public class StrategicMapMod : BaseUnityPlugin
{
public static bool GlobalShowToggle = false;
private float timer = 0f;
private float interval = 0.5f;
private bool texturesLoaded = false;
public static Dictionary<string, Sprite> SpriteCache = new Dictionary<string, Sprite>();
public static Dictionary<string, Texture2D> RawTextureCache = new Dictionary<string, Texture2D>();
private HashSet<string> reportedMissingKeys = new HashSet<string>();
void Awake()
@@ -26,6 +28,12 @@ namespace StrategicMapPlus
void Update()
{
// Toggle con Ctrl + V
if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && Input.GetKeyDown(KeyCode.V))
{
GlobalShowToggle = !GlobalShowToggle;
}
if (!texturesLoaded && Time.time > 3f) LoadTextures();
timer += Time.deltaTime;
@@ -58,6 +66,8 @@ namespace StrategicMapPlus
if (textureLookup.ContainsKey(texName))
{
Texture2D tex = textureLookup[texName];
RawTextureCache[logicKey] = tex; // Guardamos la original para Debug
Textures.IconOffset offset = null;
if (Textures.IconsOffsets.ContainsKey(logicKey)) offset = Textures.IconsOffsets[logicKey];
@@ -166,7 +176,8 @@ namespace StrategicMapPlus
if (SpriteCache.ContainsKey(generatedKey))
{
visualizer.ShowIcon(SpriteCache[generatedKey]);
Texture2D raw = RawTextureCache.ContainsKey(generatedKey) ? RawTextureCache[generatedKey] : null;
visualizer.ShowIcon(SpriteCache[generatedKey], raw);
}
else
{
@@ -203,39 +214,75 @@ namespace StrategicMapPlus
Texture2D newTexture = new Texture2D(originalTexture.width, originalTexture.height);
newTexture.ReadPixels(new Rect(0, 0, tmp.width, tmp.height), 0, 0);
// 5. MODIFICAR LOS PÍXELES (Aquí ocurre la magia)
Color[] pixels = newTexture.GetPixels();
// 5. MODIFICAR LOS PÍXELES (Lógica de Re-muestreo / Resampling)
// En lugar de mover la máscara, dejamos la máscara fija en el centro y movemos la textura (Source).
// Esto permite hacer CLAMP (repetir bordes) para evitar cortes rectos.
Color[] sourcePixels = newTexture.GetPixels(); // Píxeles originales (Source)
Color[] finalPixels = new Color[sourcePixels.Length]; // Píxeles destino (Destination)
float width = newTexture.width;
float height = newTexture.height;
Vector2 center = new Vector2(width / 2f, height / 2f);
float radius = (Mathf.Min(width, height) / 2f); // Radio máximo
// Configuración por defecto
float maskRadius = (Mathf.Min(width, height) / 2f);
float shiftX = 0f;
float shiftY = 0f;
if (offset != null)
{
center += new Vector2(offset.x, offset.y);
if (offset.size > 0) radius = offset.size / 2f;
}
// Size: Escala de la máscara (1.0 = toca bordes)
if (offset.size > 0) maskRadius *= offset.size;
for (int i = 0; i < pixels.Length; i++)
// Offset: Desplazamiento del CONTENIDO (UV Shift)
// Si Offset X > 0 (positivo), queremos ver lo que hay a la derecha,
// por tanto "desplazamos la textura hacia la izquierda" (sumamos al índice de lectura).
shiftX = offset.x * width;
shiftY = offset.y * height;
} else
{
// --- Opción A: Máscara Redonda (Matemática) ---
int x = i % newTexture.width;
int y = i / newTexture.width;
float dist = Vector2.Distance(new Vector2(x, y), center);
// Default
offset = new Textures.IconOffset(0.07f, 0, 0.9f);
if (dist > radius)
{
pixels[i] = Color.clear; // Hacemos transparente lo que esté fuera del círculo
}
// --- Opción B: Quitar el negro (Si prefieres por color) ---
// if (pixels[i].r < 0.1f && pixels[i].g < 0.1f && pixels[i].b < 0.1f)
// {
// pixels[i] = Color.clear;
// }
if (offset.size > 0) maskRadius *= offset.size;
shiftX = offset.x * width;
shiftY = offset.y * height;
}
newTexture.SetPixels(pixels);
int w = (int)width;
int h = (int)height;
for (int i = 0; i < finalPixels.Length; i++)
{
int x = i % w;
int y = i / w;
// 1. MÁSCARA (Siempre centrada en el output)
float dist = Vector2.Distance(new Vector2(x, y), center);
if (dist > maskRadius)
{
finalPixels[i] = Color.clear;
continue;
}
// 2. MUESTREO (Sampling) con Offset
// Calculamos qué píxel de la textura original corresponde a esta posición (x,y)
int srcX = (int)(x + shiftX);
int srcY = (int)(y + shiftY);
// 3. CLAMP (Repetir bordes)
// Si nos salimos de la textura, repetimos el último píxel válido.
// Esto evita el "corte recto" transparente (siempre que el borde tenga color).
srcX = Mathf.Clamp(srcX, 0, w - 1);
srcY = Mathf.Clamp(srcY, 0, h - 1);
// Leemos del array original
int srcIndex = srcY * w + srcX;
finalPixels[i] = sourcePixels[srcIndex];
}
newTexture.SetPixels(finalPixels);
newTexture.Apply();
// 6. Limpieza
@@ -255,15 +302,71 @@ namespace StrategicMapPlus
private bool isInitialized = false;
private bool hasContent = false;
// --- ZONA DEBUG EN INSPECTOR ---
public bool DebugMode = false;
public float DebugOffsetX = 0f;
public float DebugOffsetY = 0f;
public float DebugSize = 1f;
private Texture2D _rawTexture; // La textura original sin recortar
private Texture2D _generatedDebugTex; // Para limpiar memoria
private Sprite _defaultSprite; // Sprite original para restaurar
private float _lastX, _lastY, _lastSize;
private bool _lastDebugMode;
void Update()
{
if (!isInitialized || displayObj == null) return;
bool shouldShow = hasContent && Input.GetKey(KeyCode.V);
// Lógica de visualización (V o Ctrl+V)
bool shouldShow = hasContent && (StrategicMapMod.GlobalShowToggle || Input.GetKey(KeyCode.V));
if (displayObj.activeSelf != shouldShow)
{
displayObj.SetActive(shouldShow);
}
// Lógica de DEBUG en tiempo real
if (_rawTexture != null)
{
// Si activamos el modo debug, forzamos actualización
if (DebugMode && !_lastDebugMode)
{
_lastDebugMode = true;
UpdateDebugTexture();
}
else if (!DebugMode && _lastDebugMode)
{
_lastDebugMode = false;
// AL SALIR DEL DEBUG: Restaurar el sprite original
if (iconImage != null && _defaultSprite != null) iconImage.sprite = _defaultSprite;
}
if (DebugMode)
{
if (_lastX != DebugOffsetX || _lastY != DebugOffsetY || _lastSize != DebugSize)
{
UpdateDebugTexture();
}
}
}
}
private void UpdateDebugTexture()
{
_lastX = DebugOffsetX;
_lastY = DebugOffsetY;
_lastSize = DebugSize;
if (_generatedDebugTex != null) Destroy(_generatedDebugTex);
var debugOffset = new Textures.IconOffset(DebugOffsetX, DebugOffsetY, DebugSize);
_generatedDebugTex = StrategicMapMod.MakeTextureTransparent(_rawTexture, debugOffset);
if (_generatedDebugTex != null)
{
Sprite debugSprite = Sprite.Create(_generatedDebugTex, new Rect(0, 0, _generatedDebugTex.width, _generatedDebugTex.height), new Vector2(0.5f, 0.5f));
if (iconImage != null) iconImage.sprite = debugSprite;
}
}
public void Initialize()
@@ -325,17 +428,26 @@ namespace StrategicMapPlus
if (displayObj != null && displayObj.activeSelf) displayObj.SetActive(false);
}
public void ShowIcon(Sprite s)
public void ShowIcon(Sprite s, Texture2D raw = null)
{
if (!isInitialized) Initialize();
if (!isInitialized) return;
// Guardamos la referencia raw para el debug
_rawTexture = raw;
_defaultSprite = s; // Guardamos el sprite base
// Aseguramos orden de dibujado (Encima de todo)
displayObj.transform.SetAsLastSibling();
if (s != null)
{
iconImage.sprite = s;
// Solo sobreescribimos visualmente si NO estamos en modo debug
if (!DebugMode)
{
iconImage.sprite = s;
}
iconImage.enabled = true;
if (debugText != null) debugText.text = "";
hasContent = true;