test mesh
This commit is contained in:
@@ -45,7 +45,14 @@ public class BlockRegistry : MonoBehaviour
|
||||
// Собираем уникальные текстуры (top/bottom/side каждого блока)
|
||||
// Порядок: для блока i → top=i*3, bottom=i*3+1, side=i*3+2
|
||||
int layerCount = definitions.Count * 3;
|
||||
int res = 128; // разрешение должно совпадать с текстурами блоков
|
||||
|
||||
// Авто-определяем разрешение по первой найденной текстуре (берём из rect спрайта)
|
||||
int res = 16;
|
||||
foreach (var d in definitions)
|
||||
{
|
||||
Sprite s = d.topTexture ?? d.sideTexture ?? d.bottomTexture;
|
||||
if (s != null) { res = (int)s.rect.width; break; }
|
||||
}
|
||||
|
||||
TexArray = new Texture2DArray(res, res, layerCount,
|
||||
TextureFormat.RGBA32, true, false);
|
||||
@@ -100,40 +107,43 @@ public class BlockRegistry : MonoBehaviour
|
||||
// ── Утилиты ───────────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Копирует пиксели из src в Texture2DArray на слой layerIndex.
|
||||
/// Если src null или не того размера — заполняет пурпурным (ошибка-текстура).
|
||||
/// Копирует один спрайт (или его часть из атласа) в слой Texture2DArray.
|
||||
/// Использует Graphics.Blit с UV offset/scale — Read/Write не нужен.
|
||||
/// Если src null — заполняет пурпурным (ошибка-текстура).
|
||||
/// </summary>
|
||||
static void CopyTexture(Texture2D src, Texture2DArray dst, int layerIndex, int res)
|
||||
static void CopyTexture(Sprite src, Texture2DArray dst, int layerIndex, int res)
|
||||
{
|
||||
Color[] pixels;
|
||||
|
||||
if (src == null)
|
||||
{
|
||||
// Пурпурный = "текстура не назначена"
|
||||
pixels = new Color[res * res];
|
||||
var pixels = new Color[res * res];
|
||||
for (int p = 0; p < pixels.Length; p++)
|
||||
pixels[p] = (p / res + p) % 2 == 0 ? Color.magenta : Color.black;
|
||||
}
|
||||
else if (src.width != res || src.height != res)
|
||||
{
|
||||
Debug.LogWarning($"[BlockRegistry] {src.name} is {src.width}×{src.height}, expected {res}×{res}. Scaling.");
|
||||
// Простой fallback — масштабировать через RenderTexture
|
||||
var rt = RenderTexture.GetTemporary(res, res);
|
||||
Graphics.Blit(src, rt);
|
||||
var prev = RenderTexture.active;
|
||||
RenderTexture.active = rt;
|
||||
var resized = new Texture2D(res, res, TextureFormat.RGBA32, false);
|
||||
resized.ReadPixels(new Rect(0, 0, res, res), 0, 0);
|
||||
resized.Apply();
|
||||
RenderTexture.active = prev;
|
||||
RenderTexture.ReleaseTemporary(rt);
|
||||
pixels = resized.GetPixels();
|
||||
}
|
||||
else
|
||||
{
|
||||
pixels = src.GetPixels();
|
||||
dst.SetPixels(pixels, layerIndex);
|
||||
return;
|
||||
}
|
||||
|
||||
dst.SetPixels(pixels, layerIndex);
|
||||
// Вычисляем UV offset/scale этого спрайта внутри атласа.
|
||||
// sprite.rect — пиксельные координаты (origin = bottom-left атласа).
|
||||
float atlasW = src.texture.width;
|
||||
float atlasH = src.texture.height;
|
||||
var scale = new Vector2(src.rect.width / atlasW, src.rect.height / atlasH);
|
||||
var offset = new Vector2(src.rect.x / atlasW, src.rect.y / atlasH);
|
||||
|
||||
var rt = RenderTexture.GetTemporary(res, res, 0, RenderTextureFormat.ARGB32);
|
||||
var prevFilter = src.texture.filterMode;
|
||||
src.texture.filterMode = FilterMode.Point;
|
||||
Graphics.Blit(src.texture, rt, scale, offset);
|
||||
src.texture.filterMode = prevFilter;
|
||||
|
||||
var prev = RenderTexture.active;
|
||||
RenderTexture.active = rt;
|
||||
var tmp = new Texture2D(res, res, TextureFormat.RGBA32, false);
|
||||
tmp.ReadPixels(new Rect(0, 0, res, res), 0, 0);
|
||||
tmp.Apply();
|
||||
RenderTexture.active = prev;
|
||||
RenderTexture.ReleaseTemporary(rt);
|
||||
|
||||
dst.SetPixels(tmp.GetPixels(), layerIndex);
|
||||
Object.Destroy(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user