/// <summary> /// Setup a capture request for a channel. Once completed, the functor will be called with the channel data, in the format requested. /// </summary> /// <param name="request"> AsyncRequest to enqueue readbacks to. When all are completed, the request is marked completed. </param> /// <param name="channel"> The channel to capture data from (color, depth etc.) </param> /// <param name="camera"> The Camera to capture data from. </param> /// <param name="format"> The graphics format you want the data to be in. </param> /// <param name="functor"> The completion functor to call with the data. </param> /// <param name="forceFlipY"> Flags allowing you to force flipY for arbitrary channels. </param> /// <param name="readWrite"> Specify the desired color space conversion. If Default, then will be set to sRGB for SRP Color channel. </param> public static void SetupCaptureRequest ( AsyncRequest <CaptureState> request, Channel channel, Camera camera, GraphicsFormat format, Func <AsyncRequest <CaptureState>, AsyncRequest.Result> functor, ForceFlip forceFlipY, RenderTextureReadWrite readWrite = RenderTextureReadWrite.Default ) { request.data.SetFunctor(channel, functor); Debug.Assert(request.data.camera == camera, "Capture: Camera must match the camera in request.data.camera"); Debug.Assert(GraphicsUtilities.SupportsRenderTextureFormat(format), $"Capture: GraphicsFormat {format} not supported for {channel} channel"); var material = SelectShaderVariantForChannel(channel, format); if (scriptableRenderPipeline) { request.data.SetTrigger(channel, (cb, rtid) => SetupCaptureRequestCommandBufferForChannel(request, channel, camera, cb, rtid, material, format, forceFlipY, readWrite, HandleReadbackCompletion)); } else { SetupCaptureRequestCommandBufferForChannel(request, channel, camera, null, default, material, format, forceFlipY, readWrite, HandleReadbackCompletion);
/// <summary> /// Capture Screenshot asynchronously for a given source camera /// </summary> /// <param name="sourceCamera">Source camera for which the screen capture is to be performed</param> /// <param name="renderTextureFormat">Render Texture format for the screen capture</param> /// <param name="path">Path where the image is to be saved</param> /// <param name="format">Image format in which the file is to be saved. Default is set to RAW</param> public void ScreenCaptureAsync <T>(Camera sourceCamera, GraphicsFormat renderTextureFormat, string path, CaptureImageEncoder.ImageFormat format = CaptureImageEncoder.ImageFormat.Raw) where T : struct { Debug.Assert((sourceCamera != null), "Source Camera cannot be null"); Debug.Assert(GraphicsUtilities.SupportsRenderTextureFormat(renderTextureFormat)); Func <AsyncRequest <CaptureCamera.CaptureState>, AsyncRequest <CaptureCamera.CaptureState> .Result> functor = (AsyncRequest <CaptureCamera.CaptureState> r) => { r.data.colorBuffer = CaptureImageEncoder.EncodeArray(r.data.colorBuffer as Array, sourceCamera.pixelWidth, sourceCamera.pixelHeight, GraphicsFormat.R8G8B8A8_UNorm, format); var result = Unity.Simulation.FileProducer.Write(GetPath(), r.data.colorBuffer as Array); return(result ? AsyncRequest <CaptureCamera.CaptureState> .Result.Completed : AsyncRequest <CaptureCamera.CaptureState> .Result.Error); }; CaptureCamera.Capture(sourceCamera, functor, forceFlip: ForceFlip.None); }
/// <summary> /// Main Capture entrypoint. /// </summary> /// <param name="camera"> The Camera to capture data from. </param> /// <param name="colorFunctor"> Completion functor for the color channel. </param> /// <param name="colorFormat"> The pixel format to capture in. </param> /// <param name="depthFunctor"> Completion functor for the depth channel. </param> /// <param name="depthFormat"> The pixel format to capture in. </param> /// <param name="motionVectorsFunctor"> Completion functor for the motion vectors channel. </param> /// <param name="motionFormat"> The pixel format to capture in. </param> /// <param name="flipY"> Whether or not to flip the image vertically. </param> /// <returns>AsyncRequest<CaptureState></returns> public static AsyncRequest <CaptureState> Capture ( Camera camera, Func <AsyncRequest <CaptureState>, AsyncRequest <CaptureState> .Result> colorFunctor = null, GraphicsFormat colorFormat = GraphicsFormat.R8G8B8A8_UNorm, Func <AsyncRequest <CaptureState>, AsyncRequest <CaptureState> .Result> depthFunctor = null, GraphicsFormat depthFormat = GraphicsFormat.R16_UNorm, Func <AsyncRequest <CaptureState>, AsyncRequest <CaptureState> .Result> motionVectorsFunctor = null, GraphicsFormat motionFormat = GraphicsFormat.R16_UNorm, bool flipY = false ) { #if UNITY_EDITOR Debug.Assert(camera != null, "Capture camera cannot be null."); Debug.Assert(colorFunctor != null || depthFunctor != null || motionVectorsFunctor != null, "Capture one functor must be valid."); if (colorFunctor != null) { Debug.Assert(GraphicsUtilities.SupportsRenderTextureFormat(colorFormat), "GraphicsFormat not supported"); } if (depthFunctor != null) { Debug.Assert((camera.depthTextureMode & (DepthTextureMode.Depth | DepthTextureMode.DepthNormals)) != 0, "Depth not specified for camera"); Debug.Assert(GraphicsUtilities.SupportsRenderTextureFormat(depthFormat), "GraphicsFormat not supported"); } if (motionVectorsFunctor != null) { Debug.Assert((camera.depthTextureMode & DepthTextureMode.MotionVectors) != 0, "Motion vectors not enabled in depthTextureMode"); Debug.Assert(SystemInfo.supportsMotionVectors, "Motion vectors are not supported"); Debug.Assert(GraphicsUtilities.SupportsRenderTextureFormat(motionFormat), "GraphicsFormat not supported"); } #endif // UNITY_EDITOR var req = Manager.Instance.CreateRequest <AsyncRequest <CaptureState> >(); SetupCaptureRequest(req, Channel.Color, camera, CameraEvent.AfterEverything, BuiltinRenderTextureType.CameraTarget, colorFormat, colorFunctor, flipY); SetupCaptureRequest(req, Channel.Depth, camera, CameraEvent.AfterDepthTexture, BuiltinRenderTextureType.Depth, depthFormat, depthFunctor, flipY); SetupCaptureRequest(req, Channel.Motion, camera, CameraEvent.BeforeImageEffects, BuiltinRenderTextureType.MotionVectors, motionFormat, motionVectorsFunctor, flipY); #if UNITY_2019_3_OR_NEWER SRPSupport?.QueueCameraRequest(camera, req); #endif return(req); }