Объяснение в комментариях
Shader "Unlit/MapText"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_FontTex("FontTexture", 2D) = "white" {}
_FontTextColumns("FontTexture Columns", Int) = 3
_FontTextRows("FontTexture Rows", Int) = 3
_StringCharacterCount("Length of String", Int) = 3
_StringOffset("String offset", Vector) = (0.5,0.5,0,0)
_StringScale("String scale", Vector) = (0.25,0.25,0,0)
_CharWidth("Character width", Float) = 1.0
_Color("Color", Color) = (1,1,1,1)
}
SubShader
{
Tags { "RenderType" = "Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float2 uv1 : TEXCOORD1;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _FontTex;
float4 _FontTex_ST;
float4 _Color;
// font texture information
int _FontTextColumns;
int _FontTextRows;
// string length
int _StringCharacterCount;
// float array because there's no SetIntArray in c#
float _String_Chars[512];
// string placement & scaling
float4 _StringOffset;
float4 _StringScale;
// Character width - combine with StringScale to change character spacing
float _CharWidth;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
// discard pixel if _StringCharacterCount < 1
clip(_StringCharacterCount - 1);
fixed4 col;
// Determine what character in the string this pixel is in
// And what UV of that character we are in
float charIndex = 0;
float2 inCharUV = float2(0,0);
// Avoid i.uv.x = 1 and indexing charIndex[_StringCharacterCount]
i.uv.x = clamp(i.uv.x, 0.0, 0.99999);
// Scale and offset uv
i.uv = clamp((i.uv - _StringOffset.xy) / _StringScale.xy + 0.5, 0, 1);
// Find where in the char to sample
inCharUV = float2(
modf(i.uv.x * _StringCharacterCount, charIndex),
i.uv.y);
// Scale inCharUV.x based on charWidth factor
inCharUV.x = (inCharUV.x-0.5f)/charWidth + 0.5f;
// Clamp char uv
// alternatively you could clip if outside (0,0)-(1,1) rect
inCharUV = clamp(inCharUV, 0, 1);
// Get char uv in font texture space
float fontIndex = _String_Chars[charIndex];
float fontRow = floor(fontIndex / _FontTextColumns);
float fontColumn = floor(fontIndex % _FontTextColumns);
float2 fontUV = float2(
(fontColumn + inCharUV.x) / _FontTextColumns,
1.0 - (fontRow + 1.0 - inCharUV.y) / _FontTextRows);
// Sample the font texture at that uv
col = tex2D(_FontTex, fontUV);
// Modify by color:
col = col * _Color;
return col;
}
ENDCG
}
}
}
А затем в C # вам нужно установить массив строк и указать длину строки, которую вы хотите нарисовать.Нет SetIntArray
, но SetFloatArray
работает:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test : MonoBehaviour
{
Material mapTextMaterial;
void Awake()
{
Renderer renderer = GetComponent<Renderer>();
mapTextMaterial = renderer.material;
float[] stringArray = new float[] { 2f, 0f, 6f, 4f, 3f }; // "CAGED"
mapTextMaterial.SetFloatArray("_String_Chars", stringArray);
mapTextMaterial.SetInt("_StringCharacterCount", stringArray.Length);
}
}
Обязательно инициализируйте _String_Chars
и _StringCharacterCount
чем-то в Awake
, иначе это может привести к неожиданным результатам.Подумайте о включении «пустого» символа в текстуру шрифта, чтобы можно было инициализировать односимвольную строку, которая является просто пустым символом.
Этот шейдер создает такой эффект (обратите внимание на различные свойства шейдера справа):
![cyan CAGED on quad](https://i.stack.imgur.com/hwv1i.png)
Просто убедитесь, что в тексте шрифта "Generate Mip Maps" отключено (не отмечено)или вы получите неожиданные артефакты:
![lines due to mip maps](https://i.stack.imgur.com/FojkR.png)