zoukankan      html  css  js  c++  java
  • Unity使用LoadImage 读取byte[]图片时,会出现白边问题

    直接上代码

        /// <summary>
        /// 修复图片导入时白边问题
        /// </summary>
        /// <param name="texture"></param>
        public static void FixTransparency(this Texture2D texture)
        {
            Color32[] pixels = texture.GetPixels32();
            int w = texture.width;
            int h = texture.height;
         
            for     (int y = 0; y < h; y++) {
                for (int x = 0; x < w; x++) {
                    int idx    = y * w + x;
                    Color32 pixel = pixels[idx];
                    if (pixel.a == 0) {
                        bool done = false;
                        if (!done && x > 0)   done = TryAdjacent(ref pixel, pixels[idx - 1]);        // Left   pixel
                        if (!done && x < w-1) done = TryAdjacent(ref pixel, pixels[idx + 1]);        // Right  pixel
                        if (!done && y > 0)   done = TryAdjacent(ref pixel, pixels[idx - w]);        // Top    pixel
                        if (!done && y < h-1) done = TryAdjacent(ref pixel, pixels[idx + w]);        // Bottom pixel
                        pixels[idx] = pixel;
                    }
                }
            }
         
            texture.SetPixels32(pixels);
            texture.Apply();
        }
        
        private static bool TryAdjacent(ref Color32 pixel, Color32 adjacent)
        {
            if (adjacent.a == 0) return false;
         
            pixel.r = adjacent.r;
            pixel.g = adjacent.g;
            pixel.b = adjacent.b;
            return true;
        }

    在Unity Answers中发现了一个大牛写的png文件会有白边的原因分析,Messy Alpha Problem,大概是因为PhotoShop中,会把100% Transparent的地方默认颜色值设为白色,这样会导致在Runtime时,做图片interpolation运算时候,由于这样100%Transparent点的干扰使用会出现白边的情况,大牛提出的解决方法是,在PS中,利用插件为这些100%Transparent点,填充上相邻像素点的颜色,而由于我们的图片资源太多了,让美术一个一个改图片也不现实,所以参考了一段Google上的代码,通过代码来完成了PS中插线的工作流程.PNG transparency has white border/halo,在这个问题中有这段代码.至此问题基本解决,但是貌似性能消耗太多

    参考链接:https://www.cnblogs.com/luckisnow/p/4642467.html

  • 相关阅读:
    小程序游戏如何接入支付呢?
    手机回复小程序客服消息
    小程序客服发送卡片消息
    小程序多客服对应售前售后,或者不同的客服人员
    php 7 event 安装
    workerman相关
    树莓派:你是我的眼
    Python应用03 使用PyQT制作视频播放器
    从写博到出书:过程全记录
    协议森林17 我和你的悄悄话 (SSL/TLS协议)
  • 原文地址:https://www.cnblogs.com/sanyejun/p/14856576.html
Copyright © 2011-2022 走看看