我正在尝试将两个纹理合并为一个
第一个纹理来自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;
}
这就是我想要做的事情:
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;
}
但这是我得到的结果图像:
我被困在这可以有人请告诉我我做错了什么?
答案 0 :(得分:7)
这是一个有效的例子。我测试过了!
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;
}
答案 1 :(得分:0)
我对您的CombineTextures
方法进行了一些更改,
public static Texture2D CombineTexture(GameObject obj, 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;
}
您可以在循环中更改内部的值,以将纹理放置在您想要的位置。
答案 2 :(得分:0)
我不知道您希望如何做,但您可以使用Unity的OnGUI方法在需要的地方绘制徽标。
http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnGUI.html
它就像这样简单:
var aTexture : Texture;
function OnGUI() {
if(!aTexture){
Debug.LogError("Assign a Texture in the inspector.");
return;
}
GUI.DrawTexture(Rect(10,10,60,60), aTexture, ScaleMode.ScaleToFit, true, 10.0f);
}
我希望我能提供帮助。
答案 3 :(得分:0)
试试这个(它是对nexx代码的修改。)它将两个纹理组合成一个新的纹理。新纹理保证可写(否则SetPixel / GetPixel无法工作。)
此外,它假设水印纹理小于背景纹理。
请注意:我没有对此进行测试。
public static Texture2D AddWatermark(Texture2D background, Texture2D watermark)
{
// Create a new writable texture.
Texture2D result = new Texture2D(background.width, background.height);
// Draw watermark at bottom right corner.
int startX = background.width - watermark.width;
int startY = background.height - watermark.height;
for (int x = 0; x < background.width; x++) {
for (int y = 0; y < background.height; y++) {
Color bgColor = background.GetPixel(x, y);
Color wmColor = new Color(0, 0, 0, 0);
// Change this test if no longer drawing at the bottom right corner.
if (x >= startX && y >= startY) {
wmColor = watermark.GetPixel(x, y);
}
// Alpha-blend background and watermark color.
Color bended = bgColor * (1.0f - wmColor.a) + wmColor;
blended.a = 1.0f;
result.SetPixel(x, y, blended);
}
}
result.Apply();
return result;
}
答案 4 :(得分:0)
稍微优化的解决方案。仅读写水印所需的区域。
public static Texture2D AddWatermark(Texture2D background, Texture2D watermark, int startPositionX, int startPositionY)
{
//only read and rewrite the area of the watermark
for (int x = startPositionX; x < background.width; x++)
{
for (int y = startPositionY; y < background.height; y++)
{
if (x - startPositionX < watermark.width && y - startPositionY < watermark.height)
{
var bgColor = background.GetPixel(x, y);
var wmColor = watermark.GetPixel(x - startPositionX, y - startPositionY);
var finalColor = Color.Lerp(bgColor, wmColor, wmColor.a / 1.0f);
background.SetPixel(x, y, finalColor);
}
}
}
background.Apply();
return background;
}
答案 5 :(得分:-1)
此代码与两个尺寸不同的图像完美配合
public static Texture2D AddWatermark(Texture2D background, Texture2D watermark, int startX, int startY)
{
Texture2D newTex = new Texture2D(background.width, background.height, background.format, false);
for (int x = 0; x < background.width; x++)
{
for (int y = 0; y < background.height; y++)
{
if (x >= startX && y >= startY && x < watermark.width && y < watermark.height)
{
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);
newTex.SetPixel(x, y, final_color);
}
else
newTex.SetPixel(x, y, background.GetPixel(x, y));
}
}
newTex.Apply();
return newTex;
}