diff --git a/Assets/Scripts/Devices/Camera.cs b/Assets/Scripts/Devices/Camera.cs index a76e9cb9..7729e79b 100644 --- a/Assets/Scripts/Devices/Camera.cs +++ b/Assets/Scripts/Devices/Camera.cs @@ -28,8 +28,6 @@ public partial class Camera : Device protected UnityEngine.Camera cam = null; - public float adjustCapturingRate = 0.95f; - public bool runningDeviceWork = true; protected string targetRTname; @@ -184,10 +182,12 @@ private IEnumerator CameraWorker() cam.Render(); var readback = AsyncGPUReadback.Request(cam.targetTexture, 0, readbackDstFormat); + cam.enabled = false; - yield return new WaitUntil(() => readback.done); + yield return null; + + readback.WaitForCompletion(); - cam.enabled = false; if (readback.hasError) { @@ -196,13 +196,16 @@ private IEnumerator CameraWorker() } // Debug.Assert(request.done); - camData.SetTextureData(readback.GetData()); - - if (parameters.save_enabled) + if (readback.done) { - var saveName = name + "_" + Time.time; - camData.SaveRawImageData(parameters.save_path, saveName); - // Debug.LogFormat("{0}|{1} captured", parameters.save_path, saveName); + camData.SetTextureData(readback.GetData()); + + if (parameters.save_enabled) + { + var saveName = name + "_" + Time.time; + camData.SaveRawImageData(parameters.save_path, saveName); + // Debug.LogFormat("{0}|{1} captured", parameters.save_path, saveName); + } } yield return waitForSeconds; diff --git a/Assets/Scripts/Devices/Device.cs b/Assets/Scripts/Devices/Device.cs index 008a5898..5415ba68 100644 --- a/Assets/Scripts/Devices/Device.cs +++ b/Assets/Scripts/Devices/Device.cs @@ -29,6 +29,8 @@ public abstract class Device : MonoBehaviour private float transportingTimeSeconds = 0; + public float adjustCapturingRate = 0.85f; + void OnDestroy() { if (memoryStreamOutboundQueue.IsCompleted) diff --git a/Assets/Scripts/Devices/Lidar.cs b/Assets/Scripts/Devices/Lidar.cs index 18887db1..b6c0febc 100644 --- a/Assets/Scripts/Devices/Lidar.cs +++ b/Assets/Scripts/Devices/Lidar.cs @@ -60,8 +60,6 @@ public partial class Lidar : Device private LaserCamData[] laserCamData; - public float adjustWaitingPeriod = 0.80f; - void OnRenderImage(RenderTexture source, RenderTexture destination) { if (depthMaterial) @@ -189,7 +187,8 @@ private void SetupLaserCameraData() private IEnumerator LaserCameraWorker() { var axisRotation = Vector3.zero; - var waitForSeconds = new WaitForSeconds(UpdatePeriod * adjustWaitingPeriod); + var waitForSeconds = new WaitForSeconds(UpdatePeriod * adjustCapturingRate); + var readbacks = new AsyncGPUReadbackRequest[numberOfLaserCamData]; while (true) { @@ -204,20 +203,34 @@ private IEnumerator LaserCameraWorker() laserCam.Render(); - var readback = AsyncGPUReadback.Request(laserCam.targetTexture, 0, TextureFormat.RGBA32); - - yield return new WaitUntil(() => readback.done); + readbacks[dataIndex] = AsyncGPUReadback.Request(laserCam.targetTexture, 0, TextureFormat.RGBA32); laserCam.enabled = false; + } + + yield return null; + + for (var dataIndex = 0; dataIndex < numberOfLaserCamData; dataIndex++) + { + var readback = readbacks[dataIndex]; + readback.WaitForCompletion(); if (readback.hasError) { - Debug.LogError("Failed to read GPU texture"); + Debug.LogError("Failed to read GPU texture, dataIndex: " + dataIndex); continue; } // Debug.Assert(readback.done); - data.SetBufferData(readback.GetData()); + if (readback.done) + { + var data = laserCamData[dataIndex]; + data.SetBufferData(readback.GetData()); + } + else + { + Debug.LogWarning("AsyncGPUReadBackback Request was failed, dataIndex: " + dataIndex); + } } yield return waitForSeconds; @@ -226,7 +239,7 @@ private IEnumerator LaserCameraWorker() protected override IEnumerator MainDeviceWorker() { - var waitForSeconds = new WaitForSeconds(UpdatePeriod * adjustWaitingPeriod); + var waitForSeconds = new WaitForSeconds(UpdatePeriod * adjustCapturingRate); var sw = new Stopwatch(); while (true) {