Files

85 lines
4.1 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;
namespace BlockWorld
{
/// <summary>运行时画出来的 128×128 图集(8×8 格,每格 16px),不需要任何美术资源</summary>
public static class AtlasTexture
{
public const int Tile = 16, Cols = 8, Size = Tile * Cols;
public static float Du => 1f / Cols;
public static float U0(int tile) => (tile % Cols) / (float)Cols;
public static float V0(int tile) => (tile / Cols) / (float)Cols;
static Texture2D tex;
public static Texture2D Get()
{
if (tex != null) return tex;
tex = new Texture2D(Size, Size, TextureFormat.RGBA32, false)
{
filterMode = FilterMode.Point,
wrapMode = TextureWrapMode.Clamp,
name = "block-atlas"
};
var px = new Color32[Size * Size];
for (int i = 0; i < px.Length; i++) px[i] = new Color32(0, 0, 0, 0);
Fill(px, Blocks.TGrassTop, C(96, 160, 74), C(70, 128, 54), 0.35f, 3);
Fill(px, Blocks.TGrassSide, C(134, 96, 67), C(112, 78, 52), 0.35f, 5);
OverlayTopBand(px, Blocks.TGrassSide, C(95, 158, 70));
Fill(px, Blocks.TDirt, C(134, 96, 67), C(112, 78, 52), 0.35f, 5);
Fill(px, Blocks.TStone, C(128, 128, 128), C(104, 104, 104), 0.4f, 7);
Fill(px, Blocks.TSand, C(220, 208, 160), C(200, 186, 138), 0.3f, 13);
Fill(px, Blocks.TWater, C(58, 118, 200), C(78, 140, 220), 0.2f, 17);
Fill(px, Blocks.TLogSide, C(110, 84, 52), C(70, 52, 30), 0.5f, 19);
Fill(px, Blocks.TLogTop, C(150, 118, 76), C(96, 72, 42), 0.45f, 23);
Fill(px, Blocks.TLeaves, C(46, 110, 52), C(32, 84, 40), 0.45f, 31);
Fill(px, Blocks.TPlanks, C(176, 138, 86), C(140, 108, 64), 0.3f, 37);
Fill(px, Blocks.TGlass, C(190, 216, 236), C(150, 180, 205), 0.15f, 41);
Fill(px, Blocks.TBrick, C(150, 74, 60), C(120, 56, 44), 0.35f, 43);
Fill(px, Blocks.TBedrock, C(60, 60, 64), C(30, 30, 34), 0.6f, 53);
Fill(px, Blocks.TSnow, C(242, 246, 252), C(224, 232, 244), 0.3f, 71);
Fill(px, Blocks.TCobble, C(120, 120, 120), C(80, 80, 80), 0.5f, 73);
Fill(px, Blocks.TGravel, C(128, 120, 112), C(92, 86, 80), 0.5f, 89);
Fill(px, Blocks.TPig, C(238, 168, 168), C(220, 148, 148), 0.3f, 101);
Fill(px, Blocks.TSheep, C(238, 238, 232), C(214, 214, 206), 0.35f, 103);
Fill(px, Blocks.TLeg, C(96, 76, 68), C(78, 60, 52), 0.3f, 107);
Fill(px, Blocks.TEye, C(30, 28, 30), C(30, 28, 30), 0f, 109);
Fill(px, Blocks.TPick, C(196, 200, 208), C(150, 154, 162), 0.3f, 113);
Fill(px, Blocks.TAxe, C(196, 200, 208), C(150, 154, 162), 0.3f, 127);
Fill(px, Blocks.TShovel, C(196, 200, 208), C(150, 154, 162), 0.3f, 131);
Fill(px, Blocks.TSword, C(226, 232, 240), C(170, 178, 190), 0.3f, 137);
tex.SetPixels32(px);
tex.Apply(false, false);
return tex;
}
static Color32 C(int r, int g, int b) => new Color32((byte)r, (byte)g, (byte)b, 255);
static void Fill(Color32[] px, int tile, Color32 baseCol, Color32 speckCol, float density, int seed)
{
int ox = (tile % Cols) * Tile, oy = (tile / Cols) * Tile;
for (int y = 0; y < Tile; y++)
for (int x = 0; x < Tile; x++)
{
int h = seed * 7919 + y * Tile + x;
h = (h ^ (h >> 13)) * 1274126177;
float v = ((h ^ (h >> 16)) & 0x7fffffff) / (float)0x7fffffff;
px[(oy + y) * Size + ox + x] = v < density ? speckCol : baseCol;
}
}
static void OverlayTopBand(Color32[] px, int tile, Color32 col)
{
int ox = (tile % Cols) * Tile, oy = (tile / Cols) * Tile;
for (int y = 0; y < 4; y++)
for (int x = 0; x < Tile; x++)
{
if (x % 3 == 0 && y > 2) continue;
px[(oy + Tile - 1 - y) * Size + ox + x] = col; // 贴图 v 轴向上
}
}
}
}