草庐IT

编写图像抖动修正色带问题

HONT blog 2023-03-28 原文

图像色带(Band)问题一般出现在带有渐变的图像上,在影视上可以换成16bit的色深以解决,

而游戏开发中通常对图像做色彩抖动处理来解决。但抖动这块一直没有找到很好的插件,PS也一直没有

相关教程。本文就自己动手丰衣足食;编写一个小工具来实现图像抖动,配合蓝噪声效果更佳。

 

色带问题演示(上图为色带,下图为增加过噪声的效果):

 

 

c#代码(使用时挂载任意GameObject并在脚本上右键Exec,随后会在Assets根目录生成输出图像):

using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ColorDither : MonoBehaviour
{
    public Texture src;
    public Material mat;
    public Texture blueNoiseTex;
    public int iterate = 1;


    [ContextMenu("Exec")]
    private void Exec()
    {
        RenderTexture rt = new RenderTexture(src.width, src.height, 0);
        rt.Create();
        mat.SetInt("_Iterate", iterate);
        mat.SetTexture("_BlueNoiseTex", blueNoiseTex);
        Graphics.Blit(src, rt, mat);

        RenderTexture.active = rt;
        Rect rect = new Rect(0f, 0f, rt.width, rt.height);
        Texture2D tex2D = new Texture2D(rt.width, rt.height, TextureFormat.RGB24, false);
        tex2D.ReadPixels(rect, 0, 0);
        tex2D.Apply();
        
        RenderTexture.active = null;
        rt.Release();
        
        if (File.Exists("Assets/output.jpg"))
            File.Delete("Assets/output.jpg");

        File.WriteAllBytes("Assets/output.jpg", tex2D.EncodeToJPG());
#if UNITY_EDITOR
        UnityEditor.AssetDatabase.Refresh();
#endif
    }
}

 

Shader(还包含了一个8x8矩阵的程序化抖动,但效果没蓝噪声版本好):

Shader "Hidden/ColorDither"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o = (v2f)0;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex;
            uniform float4 _MainTex_TexelSize;
            uniform int _Iterate;

            uniform sampler2D _BlueNoiseTex;

            half _8x8DitherClip(half value, half2 sceneUVs)
            {
                if (value <= 0) return -0.1;
                half2 ditherUV = half2(fmod(sceneUVs.x, 8), fmod(sceneUVs.y, 8));
                const float dither[64] = {
                    0, 48, 12, 60,  3, 51, 15, 63,
                    32, 16, 44, 28, 35, 19, 47, 31,
                    8, 56,  4, 52, 11, 59,  7, 55,
                    40, 24, 36, 20, 43, 27, 39, 23,
                    2, 50, 14, 62,  1, 49, 13, 61,
                    34, 18, 46, 30, 33, 17, 45, 29,
                    10, 58,  6, 54, 9, 57,  5, 53,
                    42, 26, 38, 22, 41, 25, 37, 21 };
                int index = ditherUV.y * 8 + ditherUV.x;
                return value - dither[index] / 64;
            }

            inline fixed4 ExecuteDither(sampler2D tex, half2 duv, half2 uv, int iterate)
            {
                fixed4 dryCol = tex2D(tex, uv);

                fixed4 minValue = dryCol;
                fixed4 maxValue = dryCol;

                for (int x = -iterate; x <= iterate; x++)
                {
                    for (int y = -iterate; y <= iterate; y++)
                    {
                        fixed4 col = tex2D(tex, uv + duv * half2(x, y));
                        minValue = min(col, minValue);
                        maxValue = max(col, maxValue);
                    }
                }

                fixed4 delta = (maxValue - minValue);
                half2 ditherUv = uv*512;

                //8X8 Dither Version:
                //fixed4 wetCol = lerp(minValue, maxValue, step(0, _8x8DitherClip(delta.x, ditherUv)));

                //Blue Noise Version:
                fixed4 wetCol = lerp(minValue, maxValue, tex2D(_BlueNoiseTex, uv*5));
                return wetCol;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                return ExecuteDither(_MainTex, half2(_MainTex_TexelSize.x, _MainTex_TexelSize.y), i.uv, _Iterate);
            }
            ENDCG
        }
    }
}

 

 

具体Demo见gitee:https://gitee.com/Hont/color-dither

有关编写图像抖动修正色带问题的更多相关文章

  1. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  2. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  3. ruby - 在 Ruby 中编写命令行实用程序 - 2

    我想用ruby​​编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序

  4. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  5. ruby - Fast-stemmer 安装问题 - 2

    由于fast-stemmer的问题,我很难安装我想要的任何ruby​​gem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=

  6. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  7. ruby - 用 Ruby 编写一个简单的网络服务器 - 2

    我想在Ruby中创建一个用于开发目的的极其简单的Web服务器(不,不想使用现成的解决方案)。代码如下:#!/usr/bin/rubyrequire'socket'server=TCPServer.new('127.0.0.1',8080)whileconnection=server.acceptheaders=[]length=0whileline=connection.getsheaders想法是从命令行运行这个脚本,提供另一个脚本,它将在其标准输入上获取请求,并在其标准输出上返回完整的响应。到目前为止一切顺利,但事实证明这真的很脆弱,因为它在第二个请求上中断并出现错误:/usr/b

  8. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  9. ruby-on-rails - 简单的 Ruby on Rails 问题——如何将评论附加到用户和文章? - 2

    我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。

  10. ruby-on-rails - 添加回形针新样式不影响旧上传的图像 - 2

    我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司

随机推荐