【问题标题】:Unity - Texture2D is blank after SetPixelsUnity - SetPixels 后 Texture2D 为空白
【发布时间】:2022-08-06 03:39:03
【问题描述】:
 Texture2D crop = new Texture2D(size, size);
 crop.SetPixels(texture.GetPixels(x, y, size, size));
 crop.Apply();

Texture2D texture 不是一个空白的黑色屏幕(它是我试图裁剪成的彩色图像),但在这段代码之后crop 只是一个黑色纹理。执行代码时不会抛出任何错误。

变量值如下:

x = 80;
y = 0;
size = 480;
texture.width = 640;
texture.height = 480;

此代码用于将图像裁剪为正方形。

完整的代码是这样的:

WebCamTexture texture = new WebCamTexture(device.name);
texture.Play();

int x, y, size;

if (texture.width > texture.height)
{
    y = 0;
    x = texture.width / 2 - texture.height / 2;
    size = texture.height;
}
else if (texture.height > texture.width)
{
    x = 0;
    y = texture.height / 2 - texture.width / 2;
    size = texture.width;
}
else
{
    x = 0;
    y = 0;
    size = texture.width;
}

Texture2D crop = new Texture2D(size, size);
crop.SetPixels(texture.GetPixels(x, y, size, size));
crop.Apply();
  • 对不起,如果标题听起来很傲慢
  • 我想我不理解数学,但你为什么用x = texture.width / 2 - texture.height / 2; 而不是x = texture.width - texture.height;

标签: c# unity3d textures webcam texture2d


【解决方案1】:

在我看来,您正在创建一个新纹理,但从未设置数据引用。这需要通过加载它或单独设置每个像素来完成。

【讨论】:

  • Texture2D 裁剪 = 新 Texture2D(大小,大小);这行代码制作了一个全黑的纹理,所以我不明白你的意思
  • 我尝试将代码更改为此,结果没有区别(如果这不是您的意思,请解释): Texture2D crop = new Texture2D(size, size); Color[] 颜色 = texture.GetPixels(x, y, size, size); crop.SetPixels(颜色);作物.应用();
猜你喜欢
  • 1970-01-01
  • 2015-06-24
  • 2018-02-11
  • 2018-10-20
  • 2018-11-19
  • 1970-01-01
  • 2018-03-14
  • 1970-01-01
  • 2014-03-09
相关资源
最近更新 更多