查看: 1680|回复: 9
打印 上一主题 下一主题

实时增加文字至贴图

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2011-11-25 15:13:00 |只看该作者 |倒序浏览


           实时增加文字至贴图,Adding Text-To-Texture at Runtime in unity3d Without Using Render Texture
         

           I was working on a library in Unity that was using a lot of 3D text and therefore was requiring a lot of draw calls. This got me thinking, since all the pieces of text were on the same orientation plane would there be a way instead to draw all the text to a one texture and render that instead. Another benefit of doing this would be you could then apply text at runtime to a non-flat mesh via a texture. This would allow you to do cool things like personalizing a player’s jersey with the user’s name, localizing complex in-game advertisements, etc. So I decided to look into this. What started as a few experiments turned into not so small project.
         

            
         




           Doing research I found a way of achieving this using Render Texture. What you could do was by using either with GUI text or 3D Text place a camera to capture a single frame and render it to a texture. While not completely straightforward or easy it does work and allows quite a bit of flexibility on what you are creating. This method does come with a drawback, it needs Render Texture support. Only Unity Pro supports Render Texture, plus they are not supported on the iPhone (Unity Feature Breakdown).
         

           Since my goal was to create a library that all Unity developers could use, this was not an acceptable solution.
         

           My next idea was to manually draw the text to a texture using SetPixels. While the performance of writing to a texture using SetPixels is less than ideal, I only needed to do this once.
           

           Note: The reason why this is so slow is after calling setPixel(s) you have to call Applywhich send the whole texture to the GPU. The Apply method is unfortunately very slow, but is “usable” if you are not needing to call it repeatedly.
         

           I thought developing this using this method shouldn’t be too bad as I knew when you import a font into Unity, Unity automatically creates a texture containing the font. What I didn’t anticipate was that the letters were not in a grid format, but places tightly together. With no runtime access to letter position data, this was not going to work.
         

           Next on my list was Custom Font, a feature where you can “easily” create your own font in Unity. With Custom Font you supply your own font image laid out in a grid. Using this method I could determine where the letters were positioned. I now needed a way create a font image in a grid pattern and get all the Per Character Kerning values from a font.
         

           In digging through the internet I came across a few mentions of something called the Unity Font Mapper. It took me a very long time but I finally tracked down a link to the app: http://loewald.com/unity/unityfontmapper.zip. This Mac application not only generates the font image in a grid pattern, but generates a file containing the Per Character Kerning information.
         

           Once I had a Custom Font working I then discovered there is no programming access to any of its properties (I still use Custom Font as part of my final solution in hopes at some point access will be opened up to those values). This means these values now needed to be entered twice if you want to use the same font for both a Text-to-Texture font and as a regular font.
         

           I now had all the pieces and it was just a matter of putting it all together. While the current version of this code is far from perfect and is missing features like colored text it does exactly what I wanted it to do.
         

           To check out the demo click here.
         

           To get the source (unitypackage) click here.
         

           To get only the TextToTexture class (TextToTexture.cs) click here.
         

           Source Requirements: Unity 2.6
         

           Setup Steps
         

           1.Create a font image in a grid pattern and collect kerning values (either manually or using the Unity Font Mapper)
           

           2.Import the font image(texture) into Unity and select ‘Build Alpha From Grayscale’ and set ‘Is Readable’ to true.
           

           3.Add this texture to a material
           

           4.Within Unity create a Custom Font. If you aren’t wanting to use this font for anything but Text-to-Texture you can just setup the basics and ignore the Per Character Kerning values within the Custom Font (these take a long time to enter)
           

           5.Create a script that calls TextToTexture and passes all the required information (example in source: AddTextToTexture.cs)
           

           Note: I used a Decal material in this demo so the text would appear on top of the primary texture. But you can use any material that you wish.
         



           转自
           
            http://blog.almostlog
           
           ical.com/2010/08/20/adding-text-to-texture-at-runtime-in-unity3d-without-using-render-texture/
         
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-2-24 23:30:29 |只看该作者
頂。。。
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-3-28 23:26:55 |只看该作者
心中有爱,爱咋咋地
回复

使用道具 举报

643

主题

1

听众

9937

积分

高级设计师

Rank: 6Rank: 6

纳金币
9935
精华
1

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2012-3-30 17:58:48 |只看该作者
好好啊
回复

使用道具 举报

643

主题

1

听众

9937

积分

高级设计师

Rank: 6Rank: 6

纳金币
9935
精华
1

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-3-30 18:30:06 |只看该作者
在顶~~~~~~~~~~
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

6#
发表于 2012-4-2 23:24:47 |只看该作者
路过……
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

7#
发表于 2012-4-6 23:32:56 |只看该作者
此地無銀。。。
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

8#
发表于 2012-8-6 00:22:24 |只看该作者
好可爱的字,学习了
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

9#
发表于 2012-9-30 23:24:53 |只看该作者
俺是新人,这厢有礼了!
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

10#
发表于 2013-2-16 23:29:45 |只看该作者
很经典,很实用,学习了!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-18 13:18 , Processed in 0.082929 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部