Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
- public class DisplayASCII : MonoBehaviour
- {
- private int width, height;
- private int cellWidth, cellHeight;
- private Mesh mesh;
- private Texture2D texture;
- private Color32[] pixelBuffer;
- private bool[,] fontMask;
- public void Initialize(int width, int height, string fontPath)
- {
- TextureFormat format = TextureFormat.RGBA32;
- // Load font from path and set cell dimensions
- Texture2D ascii = Resources.Load<Texture2D>(fontPath);
- Material asciiMaterial = Resources.Load<Material>("ASCIIMaterial");
- cellWidth = ascii.width / 16;
- cellHeight = ascii.height / 16;
- // Create font mask and cell buffer
- pixelBuffer = new Color32[cellWidth * cellHeight];
- fontMask = new bool[256, pixelBuffer.Length];
- // Loop through each character and store them in font mask
- for (int i = 0; i < fontMask.GetLength(0); i++)
- {
- int y = i / 16;
- int x = i - y * 16;
- var fontcolors = ascii.GetPixels(x * cellWidth, (ascii.height - cellHeight) - (y * cellHeight), cellWidth, cellHeight);
- for (int j = 0; j < fontMask.GetLength(1); j++)
- {
- fontMask[i, j] = (fontcolors[j].a == 1);
- }
- }
- // Store dimensions and create viewport texture
- this.width = width;
- this.height = height;
- texture = new Texture2D(cellWidth * width, cellHeight * height, format, false);
- texture.filterMode = FilterMode.Point;
- // Create mesh
- mesh = new Mesh();
- GetComponent<MeshFilter>().mesh = mesh;
- // Create vertices
- Vector3[] vertices = new Vector3[(width + 1) * (height + 1)];
- Vector2[] uv = new Vector2[vertices.Length];
- for (int i = 0, y = 0; y <= height; y++)
- {
- for (int x = 0; x <= width; x++, i++)
- {
- vertices[i] = new Vector3(x, y);
- uv[i] = new Vector2((float)x / width, (float)y / height);
- }
- }
- // Apply vertices to mesh
- mesh.vertices = vertices;
- mesh.uv = uv;
- // Create triangles
- int[] triangles = new int[width * height * 6];
- for (int ti = 0, vi = 0, y = 0; y < height; y++, vi++)
- {
- for (int x = 0; x < width; x++, ti += 6, vi++)
- {
- triangles[ti] = vi;
- triangles[ti + 3] = triangles[ti + 2] = vi + 1;
- triangles[ti + 4] = triangles[ti + 1] = vi + width + 1;
- triangles[ti + 5] = vi + width + 2;
- }
- }
- // Apply triangles to mesh
- mesh.triangles = triangles;
- mesh.RecalculateNormals();
- MeshRenderer mr = GetComponent<MeshRenderer>();
- mr.material = asciiMaterial;
- mr.material.mainTexture = texture;
- mr.allowOcclusionWhenDynamic = false;
- mr.receiveShadows = false;
- mr.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
- mr.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off;
- mr.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
- transform.localScale = new Vector3(cellWidth, cellHeight, 1);
- transform.position = new Vector3(-width * cellWidth / 2, -height * cellHeight / 2, 0f);
- }
- // Update a single cell position on the texture
- public void UpdateCell(int x, int y, ConsoleCell cell)
- {
- for (int i = 0; i < pixelBuffer.Length; i++)
- {
- pixelBuffer[i] = fontMask[cell.character, i] ? cell.foreground : cell.background;
- }
- texture.SetPixels32(x * cellWidth, y * cellHeight, cellWidth, cellHeight, pixelBuffer);
- }
- // Apply changes to texture
- public void ApplyTexture()
- {
- texture.Apply();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement