Advertisement
Guest User

ASCII Display mesh in Unity

a guest
Oct 24th, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.99 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
  6. public class DisplayASCII : MonoBehaviour
  7. {
  8.     private int width, height;
  9.     private int cellWidth, cellHeight;
  10.  
  11.     private Mesh mesh;
  12.     private Texture2D texture;
  13.  
  14.     private Color32[] pixelBuffer;
  15.     private bool[,] fontMask;
  16.  
  17.     public void Initialize(int width, int height, string fontPath)
  18.     {
  19.         TextureFormat format = TextureFormat.RGBA32;
  20.         // Load font from path and set cell dimensions
  21.         Texture2D ascii = Resources.Load<Texture2D>(fontPath);
  22.         Material asciiMaterial = Resources.Load<Material>("ASCIIMaterial");
  23.  
  24.         cellWidth = ascii.width / 16;
  25.         cellHeight = ascii.height / 16;
  26.  
  27.         // Create font mask and cell buffer
  28.         pixelBuffer = new Color32[cellWidth * cellHeight];
  29.         fontMask = new bool[256, pixelBuffer.Length];
  30.  
  31.         // Loop through each character and store them in font mask
  32.         for (int i = 0; i < fontMask.GetLength(0); i++)
  33.         {
  34.             int y = i / 16;
  35.             int x = i - y * 16;
  36.  
  37.             var fontcolors = ascii.GetPixels(x * cellWidth, (ascii.height - cellHeight) - (y * cellHeight), cellWidth, cellHeight);
  38.  
  39.             for (int j = 0; j < fontMask.GetLength(1); j++)
  40.             {
  41.                 fontMask[i, j] = (fontcolors[j].a == 1);
  42.             }
  43.         }
  44.  
  45.         // Store dimensions and create viewport texture
  46.         this.width = width;
  47.         this.height = height;
  48.  
  49.         texture = new Texture2D(cellWidth * width, cellHeight * height, format, false);
  50.         texture.filterMode = FilterMode.Point;
  51.         // Create mesh
  52.         mesh = new Mesh();
  53.         GetComponent<MeshFilter>().mesh = mesh;
  54.  
  55.         // Create vertices
  56.         Vector3[] vertices = new Vector3[(width + 1) * (height + 1)];
  57.         Vector2[] uv = new Vector2[vertices.Length];
  58.  
  59.         for (int i = 0, y = 0; y <= height; y++)
  60.         {
  61.             for (int x = 0; x <= width; x++, i++)
  62.             {
  63.                 vertices[i] = new Vector3(x, y);
  64.                 uv[i] = new Vector2((float)x / width, (float)y / height);
  65.             }
  66.         }
  67.         // Apply vertices to mesh
  68.         mesh.vertices = vertices;
  69.         mesh.uv = uv;
  70.  
  71.         // Create triangles
  72.         int[] triangles = new int[width * height * 6];
  73.         for (int ti = 0, vi = 0, y = 0; y < height; y++, vi++)
  74.         {
  75.             for (int x = 0; x < width; x++, ti += 6, vi++)
  76.             {
  77.                 triangles[ti] = vi;
  78.                 triangles[ti + 3] = triangles[ti + 2] = vi + 1;
  79.                 triangles[ti + 4] = triangles[ti + 1] = vi + width + 1;
  80.                 triangles[ti + 5] = vi + width + 2;
  81.             }
  82.         }
  83.  
  84.         // Apply triangles to mesh
  85.         mesh.triangles = triangles;
  86.         mesh.RecalculateNormals();
  87.  
  88.         MeshRenderer mr = GetComponent<MeshRenderer>();
  89.         mr.material = asciiMaterial;
  90.         mr.material.mainTexture = texture;
  91.         mr.allowOcclusionWhenDynamic = false;
  92.         mr.receiveShadows = false;
  93.         mr.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
  94.         mr.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off;
  95.         mr.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
  96.  
  97.         transform.localScale = new Vector3(cellWidth, cellHeight, 1);
  98.         transform.position = new Vector3(-width * cellWidth / 2, -height * cellHeight / 2, 0f);
  99.     }
  100.  
  101.     // Update a single cell position on the texture
  102.     public void UpdateCell(int x, int y, ConsoleCell cell)
  103.     {
  104.         for (int i = 0; i < pixelBuffer.Length; i++)
  105.         {
  106.             pixelBuffer[i] = fontMask[cell.character, i] ? cell.foreground : cell.background;
  107.         }
  108.         texture.SetPixels32(x * cellWidth, y * cellHeight, cellWidth, cellHeight, pixelBuffer);
  109.     }
  110.  
  111.     // Apply changes to texture
  112.     public void ApplyTexture()
  113.     {
  114.         texture.Apply();
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement