如何将两个纹理采样到同一对象上?

问题描述

我必须重现一种效果,该效果包括将两种纹理(平铺+硬币)结合起来以实现以下目的:

Goal

到目前为止我取得的最好成绩:

Result

Visual Studio Solution to reproduce the problem

上面的链接将带您进入项目,这里是我在像素着色器中尝试执行的操作:

float4 PS(PS_INPUT input) : SV_Target
{
    float4 color1;
    float4 color2;
    float4 blendColor;


    // Get the pixel color from the first texture.
    color1 = texTile.Sample(samLinear,input.Tex) * vMeshColor;

    // Get the pixel color from the second texture.
    color2 = texCoin.Sample(samLinear,input.Tex) * vMeshColor;

    // Blend the two pixels together and multiply by the gamma value.
    blendColor = color1 * color2;

    // Saturate the final color.
    blendColor = saturate(blendColor);

    return blendColor;
}

但这似乎不是正确的方法
我应该采取哪种方法才能取得预期的结果?

解决方法

好吧,当示例图像似乎已与alpha蒙版混合时,首先要混合它们,而不是使用alpha蒙版进行混合。

一个例子可能像下面这样;只要硬币有一个Alpha通道。
(否则,您必须计算Alpha或在图像编辑软件中添加Alpha。

    float3 blend(float4 CoinTex,float3 GridTex)
    {
         // Inverse of alpha,to get the area around the coin
         // Alpha spans from [0,1] so the expression below would suffice
         float1 inverseAlpha = (1 - CoinTex.a);

         float3 blendedTex = 0;
         
         // If-else that is evaluated to decide how they'll be overlayed
         if (inverseAlpha > 0.0 ){
             blendedTex = GridTex.rgb;
         } else {blendedTex = CoinTex.rgb;}

         return blendedTex;
    }