【发布时间】:2015-02-11 22:59:36
【问题描述】:
我从 Unity 中的网络摄像头纹理获取纹理,但它对我的项目来说太高了。 我不需要它 1280x720 ,但我会减小尺寸。 如何在 Unity 中更改它的参数? 谢谢你。谢谢你的建议。
【问题讨论】:
标签: c# unity3d textures webcam
我从 Unity 中的网络摄像头纹理获取纹理,但它对我的项目来说太高了。 我不需要它 1280x720 ,但我会减小尺寸。 如何在 Unity 中更改它的参数? 谢谢你。谢谢你的建议。
【问题讨论】:
标签: c# unity3d textures webcam
webCamTexture.requestedWidth 和 webCamTexture.requestedHeight 返回您最初尝试初始化 WebCamTexture 的宽度/高度。他们不反映真实的宽度/高度。 webCamTexture.width/webCamTexture.height 也始终没有。问题在于,在某些情况下,由于设备相机的硬件和分辨率不同,webCamTexture 的大小是在一段时间后确定的,这通常与设备的屏幕不同。
您可以做的是获取像素 (webCamTexture.GetPixels()) 1 次,然后在一帧后您可以使用 webCamTexture.width/.height 返回捕获的纹理大小。
来自插件 CameraCaptureKit (https://www.assetstore.unity3d.com/en/#!/content/56673) 的此类代码示例,它具有执行您描述的功能。
// ANDREAS added this: In some cases we apparently don't get correct width and height until we have tried to read pixels
// from the buffer.
void TryDummySnapshot( ) {
if(!gotAspect) {
if( webCamTexture.width>16 ) {
if( Application.platform == RuntimePlatform.IPhonePlayer ) {
if(verbose)Debug.Log("Already got width height of WebCamTexture.");
} else {
if(verbose)Debug.Log("Already got width height of WebCamTexture. - taking a snapshot no matter what.");
var tmpImg = new Texture2D( webCamTexture.width, webCamTexture.height, TextureFormat.RGB24, false );
Color32[] c = webCamTexture.GetPixels32();
tmpImg.SetPixels32(c);
tmpImg.Apply();
Texture2D.Destroy(tmpImg);
}
gotAspect = true;
} else {
if(verbose)Debug.Log ("Taking dummy snapshot");
var tmpImg = new Texture2D( webCamTexture.width, webCamTexture.height, TextureFormat.RGB24, false );
Color32[] c = webCamTexture.GetPixels32();
tmpImg.SetPixels32(c);
tmpImg.Apply();
Texture2D.Destroy(tmpImg);
}
}
}
回到你的问题的重点,当我遵循 iOS 代码时,你应该能够通过将“requestedWidth/requstedHeight”设置为更小的值来设置更小的 WebCamTexture 纹理大小,然后 Unity 将(至少在 iOS 上) 尝试选择最接近您要求的纹理大小的相机分辨率。
这发生在 CameraCapture.mm 中
- (NSString*)pickPresetFromWidth:(int)w height:(int)h
{
static NSString* preset[] =
{
AVCaptureSessionPreset352x288,
AVCaptureSessionPreset640x480,
AVCaptureSessionPreset1280x720,
AVCaptureSessionPreset1920x1080,
};
static int presetW[] = { 352, 640, 1280, 1920 };
//[AVCamViewController setFlashMode:AVCaptureFlashModeAuto forDevice:[[self videoDeviceInput] device]];
#define countof(arr) sizeof(arr)/sizeof(arr[0])
static_assert(countof(presetW) == countof(preset), "preset and preset width arrrays have different elem count");
int ret = -1, curW = -10000;
for(int i = 0, n = countof(presetW) ; i < n ; ++i)
{
if( ::abs(w - presetW[i]) < ::abs(w - curW) && [self.captureSession canSetSessionPreset:preset[i]] )
{
ret = i;
curW = presetW[i];
}
}
NSAssert(ret != -1, @"Cannot pick capture preset");
return ret != -1 ? preset[ret] : AVCaptureSessionPresetHigh;
#undef countof
}
【讨论】:
您可以使用请求的高度和请求的宽度来调整您想要从相机获得的纹理大小。但是,您需要在相机未运行时执行此操作,您将获得设备本身支持的最接近的尺寸。
【讨论】: