В Unity у меня есть следующий метод, который берет камеру и создает прозрачное изображение (которое уже хорошо работает).RenderTexture rt
создается и создается вне этого метода.
Texture2D reallyTakeShot() {
camera.backgroundColor = Color.white;
Texture2D texWhite = new Texture2D(width, height, TextureFormat.RGB24, mipmap: false);
DoCameraRender(camera, shader);
RenderTexture.active = rt;
texWhite.ReadPixels(new Rect(0f, 0f, width, height), 0, 0);
texWhite.Apply();
camera.backgroundColor = Color.black;
DoCameraRender(camera, shader);
Texture2D texBlack = new Texture2D(width, height, TextureFormat.RGB24, mipmap: false);
texBlack.ReadPixels(new Rect(0f, 0f, width, height), 0, 0);
texBlack.Apply();
Texture2D transparentTexture = new Texture2D(width, height, TextureFormat.ARGB32, mipmap: false);
for (int y = 0; y < transparentTexture.height; ++y)
{
for (int x = 0; x < transparentTexture.width; ++x)
{
float alpha = 1.0f - (texWhite.GetPixel(x, y).r - texBlack.GetPixel(x, y).r);
Color color;
if ((int)(alpha * 100) == 0)
{
color = Color.clear;
}
else
{
color = texBlack.GetPixel(x, y) / alpha;
color.a = alpha;
}
transparentTexture.SetPixel(x, y, color);
}
}
return transparentTexture;
Я пытаюсь сделать серию снимков, которые на самом деле не должны быть на 100% гладкими, но я не получаю многоудачи.Из 30 захватов, большинство из них выглядят одинаково.
Самым последним, что я сделал, был цикл, который вызывает
, который имеет следующий метод (который выводит 0 мс, чтобы сделать захват так,Я предполагаю, что это быстро)
private IEnumerator TakeShotNoWrite()
{
shooting = CaptureStates.Capturing;
Stopwatch sw = new Stopwatch();
bool useJpeg = ModPrefs.GetBool("HomeShot", "use_jpeg");
int outputWidth = ModPrefs.GetInt("HomeShot", "output_width");
Texture2D tex = TakeShotToTexture2(!useJpeg);
caps.Add(tex);
shooting = CaptureStates.NormalCapture;
Console.WriteLine("capture took " + sw.Elapsed.TotalMilliseconds + "ms");
yield return new WaitForFixedUpdate();
// tried these too
// yield return 0;
// yield return WaitForNextFrame();
}
Я запускаю камеру на FixedUpdate()
(это единственный раз, когда я могу заставить объекты выглядеть правильно LateUpdate()
не работает)
private void FixedUpdate()
{
if (shooting == CaptureStates.Capturing)
{
return;
}
if (shooting != CaptureStates.NotCapturing)
{
// Extra StopWatch logic so I can increase the minimum time
// before next screen shot I set it to 30ms.
StartCoroutine(TakeShotNoWrite()); // the render to texture code is above.
// when stopwatch reaches 15 seconds start writing the files
}
}