首页 > 其他分享 >Color Linear Interpolation线性插值

Color Linear Interpolation线性插值

时间:2022-11-01 14:13:13浏览次数:61  
标签:Linear lerp Color FirstCol 线性插值 finalCol SecondCol float4 Blend

写在前面:

本文章为个人学习笔记,方便以后自己复习,也希望能帮助到他人。

由于本人水平有限难免出现错误,还请评论区指出,多多指教。

部分图元和素材来源于网络,如有侵权请联系本人删除。

参考资料与链接会在文章末尾贴出。

=======================================================================

1.Interpolate Color

考虑如何将两种颜色混合起来:

     _FirstCol ("FirstCol",Color) = (1,1,1,1)
        _SecondCol ("SecondCol",Color) = (1,1,1,1)
        _Blend ("Blend Value",Range(0,1)) = 0
    
        float4 frag (v2f i) : SV_Target
        {              
                float4 finalCol = _FirstCol + _SecondCol * _Blend;
                return finalCol;
        }

颜色已经可以变化,但是其实只有一种颜色受到了影响,我们继续修改下公式:

        float4 frag (v2f i) : SV_Target
        {              
             float4 finalCol = _FirstCol * (1-_Blend) + _SecondCol * _Blend;
             return finalCol;
        }    

 

现在就可以从颜色1变换到颜色2,这个过程就叫做Linear Interpolation线性插值。在hlsl(high level shading language)中是有内置公式的:

 

 

跟我们自己写的一样效果但是更简单:

 

       float4 frag (v2f i) : SV_Target
            {              
                //float4 finalCol = _FirstCol * (1-_Blend) + _SecondCol * _Blend;
                float4 finalCol = lerp(_FirstCol,_SecondCol,_Blend);
                return finalCol;
            }

2.Texture Linear Interpolation

lerp将A B两个值根据weight权重混合起来,实际上,我们采样贴图,最终得出的也是一个值,我们不妨试试将两个纹理采样结果lerp起来:

     _Tex0 ("Tex0",2D) = "white"{}
        _Tex1 ("Tex1",2D) = "white"{}
         
        _Blend ("Blend Value",Range(0,1)) = 0

        float4 frag (v2f i) : SV_Target
        {              
                //float4 finalCol = _FirstCol * (1-_Blend) + _SecondCol * _Blend;
                float4 tex0 = tex2D(_Tex0,i.uv);
                float4 tex1 = tex2D(_Tex1,i.uv);
                float4 finalCol = lerp(tex0,tex1,_Blend);
                return finalCol;
         }

 

 

可以做出一张纹理向另一张纹理过渡的效果。

进一步想,lerp公式里面的weight是一个值,贴图采样也是一个值,那么我们可以试试用贴图采样结果做权重:

     _Tex0 ("Tex0",2D) = "white"{}
        _Tex1 ("Tex1",2D) = "white"{}
         
        _Blend ("Blend Value",Range(0,1)) = 0
        _WeightTex("WeightTex",2D) = "white"{}

          float4 frag (v2f i) : SV_Target
         {              
                //float4 finalCol = _FirstCol * (1-_Blend) + _SecondCol * _Blend;
                float4 tex0 = tex2D(_Tex0,i.uv);
                float4 tex1 = tex2D(_Tex1,i.uv);

                float4 weight = tex2D(_WeightTex,i.uv);
                float4 finalCol = lerp(tex0,tex1,weight);
                return finalCol;
          }

 

 

 

 

我们用一张黑白图做权重,白色部分为1,黑色为0,代入lerp公式就得出了上图效果。

细想,如果我们权重贴图做得精细点,那么就是否可以控制哪些部分我要的是哪张纹理的效果呢?

标签:Linear,lerp,Color,FirstCol,线性插值,finalCol,SecondCol,float4,Blend
From: https://www.cnblogs.com/pisconoob/p/16847477.html

相关文章

  • Gamma and Linear Space(色彩空间)
    写在前面:本文章为个人学习笔记,方便以后自己复习,也希望能帮助到他人。由于本人水平有限难免出现错误,还请评论区指出,多多指教。部分图元和素材来源于网络,如有侵权请联系本......
  • 机器学习 之 liblinear的帮助文档翻译
    文章目录​​〇、推荐​​​​一、liblinear版本​​​​二、翻译整合​​​​介绍​​​​安装​​​​快速开始​​​​Scipy快速入门​​​​设计说明​​​​数据结构​......
  • Linear Algebra and Linear Programming Notes
    LinearAlgebraandLinearProgrammingDualityTheoryPrimalanddualproblemsofLPs如何理解?\[\bar{x}=\left(\begin{array}{l}x\\y\end{array}\right),\quad......
  • CF149D Coloring Brackets
    题意:给出一串合法括号,按以下规则给括号染色:1.每个符号可以染红色、蓝色,或者不染色;2.相邻两个符号不能染同一种颜色,可以都不染色;3.一对括号有且仅有一个符号染色。求......
  • Fluent让colormap始终显示在云图的顶部
    打开View->Options->Colormap,选择Top。   ......
  • 【XSY3270】Domino Colorings(轮廓线dp,状压)
    若已经知道了每个格子的颜色,我们可以用轮廓线DP(类似插头DP)判断棋盘是否能被多米诺骨牌填满,设\(dp[S]\)表示是否存在某种填法使得轮廓线每个位置是否被填的状态为\(S\)......
  • LeetCode_Array_75. Sort Colors 颜色分类 (C++)
    目录​​1,题目描述​​​​2,思路​​​​3,代码【C++】​​1,题目描述Givenanarraywithnobjectscoloredred,whiteorblue,sortthemin-placesothatobjectsoft......
  • PAT_甲级_1027 Colors in Mars (20分) (C++)【签到题】【进制转换】
    目录​​1,题目描述​​​​题目大意​​​​2,思路​​​​3,代码​​1,题目描述SampleInput:154371 SampleOutput:#123456题目大意将0-168的十进制数转化为13进制;  2,思......
  • 线性回归 Linear Regression
    线性回归的预设线性只能通过每个样本各维的线性组合获得预测结果,这使得函数很简单,但拟合能力较弱。同方差性每个样本的方差不变。方差不同会使得拟合函数对某些数......
  • 图像处理:ColorMap将灰度图像[0,1]区间上的像素值映射到RGB的[0,255]
    1.起因在做人群计数时,常常使用密度图来估计人数。密度图中每个像素的值都是介于0和1之间的浮点数,原本是灰度图。但是使用python的matplotlib.pyplot并指定cmap参数可以画......