【发布时间】: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