Question by
HalfCarrot · Sep 23, 2019 at 04:47 AM ·
shadershadersshader programmingshaderlab
How to map letters on mesh with shader?
I'm trying to create a shader that allows mapping text on a mesh. How do I align each character horizontally? Currently, all of them overlap each other.
Currently looks like this:
Texture used:
Shader "Unlit/MapText"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_FontTex("FontTexture", 2D) = "white" {}
_Color("Color", Color) = (1,1,1,1)
}
SubShader
{
Tags { "RenderType" = "Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform int _CharacterCount;
uniform float4 _Characters[3];
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float2 uv1 : TEXCOORD1;
};
struct v2f
{
float2 uv : TEXCOORD0;
float2 uv1 : TEXCOORD1;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _FontTex;
float4 _FontTex_ST;
float4 _Color;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.uv1 = TRANSFORM_TEX(v.uv1, _FontTex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed4 col;
_Characters[0] = 0;
_Characters[1] = 1;
_Characters[2] = 2;
for (uint k = 0; k < _Characters.Length; k++)
{
float row = (k % 1024);
float column = (k / 1024);
float2 character = (i.uv1 + float2(row, column)) * 0.33;
_Characters[k] = tex2D(_FontTex, character);
}
col = (_Characters[0] + _Characters[1] + _Characters[2]) * _Color;
return col;
}
ENDCG
}
}
}
Comment