Unity image to character drawing

Unity implements picture-to-character painting

Enlarge a picture continuously, and finally it will be displayed as color blocks, which are actually the pixels of the picture, and the picture is composed of pixels. Then we replace the corresponding color with the corresponding character to realize the character painting

plan

The color value of the color is usually composed of RGB, adjust the gray scale of the color from 0 to 255 first, and then make a correspondence between the pixel point and the character corresponding to each position.

First define the characters you need (sort them in the direction from dense to sparse, and vice versa, and only affect the display of the base map)

private readonly string m_Chars = "@*#$%XB0H?OC7>+v=~^:_-'`. ";

then grayscale

var grayRGB = (int) (color.r * 255 * 0.299 + color.g * 255 * 0.587 + color.b * 255 * 0.114);

Color blocks and character maps

int index = (int) (grayRGB / 256.0f * (m_Chars.Length));
var value = m_Chars[index];

Unity's rgb is in the range of 0-1, here it is converted to 0-255 for the convenience of processing, and then the subscript of the character is divided by 256 in order to avoid exceeding the range

Finally look at the processing effect

Effect

I drew two pictures by myself for a test

insert image description here

                                                    
           *B^^~$*                                  
        .*^^^^^^^^^*                                
       ?^^^::^^^^^^^C~                              
      0^^^^^^^^^^^^^^~_                             
      :^^^^^^^^^^^^:^^$                             
     $^^^^^^^^^^^^^^^^^*                            
    H^^^^^^^^^^^^^^^^^^=                            
    $^^^^^^^^^^^^^^^^^^^$                           
    ~^^^^^^^^^^^^^^^^^::$                           
    ^^^^^^^^^^^^^^^^^^^^0                           
   ^^^^^^^^^^^^^^^^^^^^^v                           
   7^^^^^^^^^^^^^^^^^^^^=                           
   _^^^^^^^^^^^^^^^^^^^^?                           
    ^^^^^^^^^^*C^^^^^^^^#      .#@$@~               
    #^^^^^^^^?@*^^^^^^^^%     @777777*              
    %^^^^^^^^v*$^^^^^^^^    ~777777777*             
     v^^^^^^^^C^^^^^^^^X   ^7@77777777C$            
     %^^^^^^^^^^^^^^^^~B  .777*7777777$C            
      #^^^^^^^^^^^^^^^+*  #7777777777*77*           
       $^^^^^^^^^^^^^^^? #77777#77777H777v          
        %^^^^^^^^^^^^^^^ H77777%7777**777$          
          *H^^^^^^^^^^: #777777X@??@7*7777C         
           :^_^^:*v^^^^ #@77777?77777*7777%         
            O*#^ $^^^^^ 77%$7>#777777$7777>.        
                 %^^^^^.777777777777*`77777#        
                  ^^^^:777777X77777#77*777X.        
                  #^^^:@77777??7*7*7777700>7$       
                   ^^^^*7777C7777*%777777777'$      
                   *^^^*0777%77777@7777777%@_+      
                   -^^::`#7%7777777$770@7____$      
                    *^^*__:_+7XX%%X7_______0X       
                     *^:B_______________%?:^O       
                      #^^:*+__________*>^^^^$       
                       ?^:^^^C*^`~@#^^^:^^^@H^:=*`  
                      HB*^^^^^^^^^^^^^^^^^$$::^^^#  
                     *^^^v*+^^^^^^^^^^^-X=>   .     
                     ^^^^^*^~*^^^^^^:.*#^^^.        
                     _@$*O$^^^__#**@+*^>^^^         
                          ^^^^X      **^^^%         
                          @0*7         *^''         
                                        ~           

insert image description here

                                                    
                            #*      ~_#             
                            __    `@__?             
                            __?      *              
                      H____+.O      =  `O___:       
                     C__*___C*@@   @@   *___%       
    ??                >___>*             *0         
    .?0                 *@H. *+---------@%          
     B?B        *~HHHHHHBH`=---------H----->        
      **      ^  %HHHHHHH$----@@-----@@@----#       
        #   $    'HHHHHHHH----@@-----*@@----_       
          @ _     ??HHHHHX------------------#       
           ^        $H?HHH$----------------$        
                       ?H?* H------------*          
            HHH@    %H$           .`    $           
            $HHH    0HH#                #           
             HH%     0O                             
             #X                  *`@   %            
             '          *>    ?@                    
               ?    ?H                              
                                                   

source code

The complete code is as follows

using System.Text;
using UnityEngine.UI;
using UnityEngine;

public class CharPainter : MonoBehaviour
{
    [SerializeField] private Texture2D Texture;
    [SerializeField] private Text Text_Painter;
    [SerializeField] private int Space = 10;
    private readonly string m_Chars = "@*#$%XB0H?OC7>+v=~^:_-'`. ";
    private StringBuilder m_StringBuilder = new StringBuilder(4096);

    void StartPaint()
    {
        //记录一个string
        m_StringBuilder.Remove(0, m_StringBuilder.Length);
        //从上到下, 从左到右, 依次画
        for (int j = Texture.height - 1; j >= 0; j -= Space)
        {
            for (int i = 0; i < Texture.width; i += Space)
            {
                //读取当前的像素点的颜色
                var color = Texture.GetPixel(i, j);
                //灰度处理
                var grayRGB = (int) (color.r * 255 * 0.299 + color.g * 255 * 0.587 + color.b * 255 * 0.114);
                //取出对应的字符
                int index = (int) (grayRGB / 256.0f * (m_Chars.Length));
                var value = m_Chars[index];
                m_StringBuilder.Append(value);
            }
            //每行画完换行
            m_StringBuilder.Append("\n");
        }
        //输出到文本上显示
        Text_Painter.text = m_StringBuilder.ToString();
    }


    void Start()
    {
        StartPaint();
    }

#if UNITY_EDITOR
    private void OnValidate()
    {
        StartPaint();
    }
#endif
}

Guess you like

Origin blog.csdn.net/onelei1994/article/details/125527851
Recommended