在unity3d中将两个不同的纹理合并为一个

Sor*_*ora 3 c# unity-game-engine unityscript

我试图将两个纹理合并为一个,第一个纹理来自webcamTexture 第二个是来自精灵,使用:gameobject.getComponent<SpriteRenderer>().sprite.texture as Texture2D

我在编写函数时遇到问题这是我到目前为止所做的:

public static Texture2D CombineTextures(GameObject obj, Texture2D background, Texture2D TodrawLogo)
{
    Vector3 v = obj.transform.position;// obj is TodrawLogo gameobject
    int width = TodrawLogo.width;
    int height = TodrawLogo.height;
    for (int x =(int)v.x; x < width; x++){
        background.SetPixel(x,(int)v.y,TodrawLogo.GetPixel(x,(int)v.y));
    }
    background.Apply();
    return background;
}
Run Code Online (Sandbox Code Playgroud)

这就是我想要做的:

WebcamTexture 在此输入图像描述

结果纹理应该是这样的 在此输入图像描述

webcamTexture是一个3dplane和徽标是一个single sprite 但遗憾的是我的功能不起作用有没有人知道如何解决这个我知道我应该找到的确切坐标todraw image和设置像素但我无法弄清楚如何

非常欣赏

编辑:

我试图使用@nexx代码:

public static Texture2D CombineTexture(Texture2D background, Texture2D TodrawLogo)
{

  int width = TodrawLogo.width;
  int height = TodrawLogo.height;

  int backWidth = background.width;
  int backHeight = background.height;
// bottom right corner
int startX = backWidth - width;
int startY = backHeight - height;

// loop through texture
int y = 0;
while (y < backHeight) {
    int x = 0;
    while (x < backWidth) {
        // set normal pixels
        background.SetPixel(x,y,background.GetPixel(x,y));
        // if we are at bottom right apply logo 
        //TODO also check alpha, if there is no alpha apply it!
        if(x >= startX && y < backHeight- startY)
            background.SetPixel(x,y,TodrawLogo.GetPixel(x-startX,y-startY));
        ++x;
    }
    ++y;
}
background.Apply();
return background;
 }
Run Code Online (Sandbox Code Playgroud)

但这是我得到的结果图像: 在此输入图像描述

我被困在这可以有人请告诉我,我做错了什么?

小智 8

这是一个有效的例子.我测试了!

public Texture2D AddWatermark(Texture2D background, Texture2D watermark)
{

    int startX = 0;
    int startY = background.height - watermark.height;

    for (int x = startX; x < background.width; x++)
    {

        for (int y = startY; y < background.height; y++)
        {
            Color bgColor = background.GetPixel(x, y);
            Color wmColor = watermark.GetPixel(x - startX, y - startY);

            Color final_color = Color.Lerp(bgColor, wmColor, wmColor.a / 1.0f);

            background.SetPixel(x, y, final_color);
        }
    }

    background.Apply();
    return background;
}
Run Code Online (Sandbox Code Playgroud)