Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by farhanblu · Jan 04, 2020 at 01:03 AM · shadertextfontuv coordinates

Custom font rendering in UI.Text via shader

Hi all, I am trying to achieve something for a couple of days now and find myself stuck. The problem briefly is :

A Character, lets say 'd' should show slightly displaced vertically upwards (by x) if the character before it was an 'a'. But if the character before it was 'e', it should be vertically displaced downwards (by x*2). I have a big set of similar conditions to meet for other characters. I can not use TextMesh, the text has to be UI.Text inside a 'Screen Space Camera'.

I initially got it working by modifying unused characters' glyphs so that they had the same letter 'd' but with different vertical offsets. But this approach is not viable for say 100 other such pair of characters.

So I took Unity's Built-In Font shader from here and tried to modify it for my requirement, but I have not had any success.

The following function is a monobehaviour method, called in Update, that I am using to set the parameters values of the shader

 void SetDPos()
     {
         font.RequestCharactersInTexture(_str);
         
         CharacterInfo dInfo = new CharacterInfo();
         
         for (int i = 0; i < _str.Length; i++)
         {
             CharacterInfo ch;
             font.GetCharacterInfo(_str[i], out ch);
             if (_str[i] == 'd')
                 dInfo = ch;
         }
 
         float [] dArray = new float[]
         {
             dInfo.uvTopLeft.x,
             dInfo.uvTopLeft.y,
             dInfo.uvBottomRight.x,
             dInfo.uvBottomRight.y
         };
         
         Material fontMaterial = _text.material;
         fontMaterial.SetTexture("_MainTex", font.material.mainTexture);
         fontMaterial.SetInt("_TexCoorCount", 1);
         fontMaterial.SetFloatArray("_TexCoorArray", dArray); //two uv's of character d
         _text.material = fontMaterial;
     }


Then I changed the vertex function to take the uv values of character 'd', passed inside "_TexCoorArray" so that if the texcoord parameter was inside the uv coordinates, the vertex should be displaced by 10, as follows :

 v2f vert (appdata_t v)
             {
                 v2f o;
                 UNITY_SETUP_INSTANCE_ID(v);
                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
                 
                 o.color = v.color * _Color;
                 
                 
                for( int j = 0; j < _TexCoorCount; j+=4)
                 {
                     if(v.texcoord.x >= _TexCoorArray[j + 0] && v.texcoord.y >= _TexCoorArray[j + 1]   
                         && v.texcoord.x <= _TexCoorArray[j + 2]  && v.texcoord.y <= _TexCoorArray[j + 3] ) 
                     {
                         v.vertex = float4(v.vertex.x + 10, v.vertex.y, v.vertex.z, v.vertex.w); 
                     } 
                      
                 }
                 
                 o.texcoord = (v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw);
                 
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 return o; 
             }

But my condition never seems to be met. I believe the 'texcoord' is the uv for the font texture, and the parameter I am passing is also based on the same texture. Please guide me to a solution here, I am not familiar with writing shaders.

@Bunny83

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Xepherys · Jun 24, 2020 at 02:46 PM

I'm actually looking to see if I can implement SIL Graphite in Unity as either a wrapper or a replacement for the font renderer. This would be one possible use case, though it would require rebuilding your existing TTF/OTF font with Graphite SDL markup for your needs.

But, this sort of situation is a great example of the usefulness. Hopefully I can get something that'll work using the Graphite libraries.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

191 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing Font material variables in script 1 Answer

Font Scaling Issues 2 Answers

Make my text bigger? 0 Answers

3DText Outline Shader 2 Answers

Custom Font not loaded in Android 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges