private void Update() { while (m_Requests.Count > 0) { var req = m_Requests.Peek(); var task = m_Tasks.Peek(); if (req.hasError) { NRDebugger.Log("GPU readback error detected"); m_Requests.Dequeue(); CommitResult(null, task); m_Tasks.Dequeue(); } else if (req.done) { var buffer = req.GetData <Color32>(); if (tempTexture != null && tempTexture.width != Width && tempTexture.height != Height) { GameObject.Destroy(tempTexture); tempTexture = null; } if (tempTexture == null) { tempTexture = new Texture2D(Width, Height, TextureFormat.RGB24, false); } tempTexture.SetPixels32(buffer.ToArray()); tempTexture.Apply(); if (task.OnReceive != null) { Texture2D resulttex; if (tempTexture.width != task.Width || tempTexture.height != task.Height) { NRDebugger.LogFormat("[BlendCamera] need to scale the texture which origin width:{0} and out put width:{1}", Width, task.Width); resulttex = ImageEncoder.ScaleTexture(tempTexture, task.Width, task.Height); CommitResult(resulttex, task); //Destroy the scale temp texture. GameObject.Destroy(resulttex); } else { CommitResult(tempTexture, task); } } m_Requests.Dequeue(); m_Tasks.Dequeue(); } else { break; } } }
public byte[] Encode(int width, int height, PhotoCaptureFileOutputFormat format) { if (m_CurrentFrame == null) { NRDebugger.LogWarning("Current frame is empty!"); return(null); } byte[] data = null; RenderTexture pre = RenderTexture.active; RenderTexture targetRT = m_CurrentFrame; RenderTexture.active = targetRT; Texture2D texture2D = new Texture2D(targetRT.width, targetRT.height, TextureFormat.ARGB32, false); texture2D.ReadPixels(new Rect(0, 0, targetRT.width, targetRT.height), 0, 0); texture2D.Apply(); RenderTexture.active = pre; Texture2D outPutTex = texture2D; Texture2D scaleTexture = null; // Scale the texture while the output width or height not equal to the targetRT. if (width != targetRT.width || height != targetRT.height) { scaleTexture = ImageEncoder.ScaleTexture(texture2D, width, height); outPutTex = scaleTexture; } switch (format) { case PhotoCaptureFileOutputFormat.JPG: data = outPutTex.EncodeToJPG(); break; case PhotoCaptureFileOutputFormat.PNG: data = outPutTex.EncodeToPNG(); break; default: break; } // Clear the temp texture. GameObject.Destroy(texture2D); if (scaleTexture != null) { GameObject.Destroy(scaleTexture); } return(data); }
private byte[] Do(int width, int height, BlendMode blendmode, PhotoCaptureFileOutputFormat format) { if (!m_IsInit) { return(null); } byte[] data = null; RenderTexture pre = RenderTexture.active; RenderTexture targetRT; switch (blendmode) { case BlendMode.RGBOnly: targetRT = m_CameraInput.RGBTexture; break; case BlendMode.VirtualOnly: targetRT = m_CameraInput.VirtualTexture; break; case BlendMode.Blend: targetRT = m_CameraInput.BlendTexture; break; case BlendMode.WidescreenBlend: Debug.LogError("Do not support WidescreenBlend mode."); return(null); default: return(null); } RenderTexture.active = targetRT; Texture2D texture2D = new Texture2D(targetRT.width, targetRT.height, TextureFormat.ARGB32, false); texture2D.ReadPixels(new Rect(0, 0, targetRT.width, targetRT.height), 0, 0); texture2D.Apply(); RenderTexture.active = pre; Texture2D outPutTex = texture2D; Texture2D scaleTexture = null; // Scale the texture while the output width or height not equal to the targetRT. if (width != targetRT.width || height != targetRT.height) { scaleTexture = ImageEncoder.ScaleTexture(texture2D, width, height); outPutTex = scaleTexture; } switch (format) { case PhotoCaptureFileOutputFormat.JPG: data = outPutTex.EncodeToJPG(); break; case PhotoCaptureFileOutputFormat.PNG: data = outPutTex.EncodeToPNG(); break; default: break; } // Clear the temp texture. Destroy(texture2D); if (scaleTexture != null) { Destroy(scaleTexture); } return(data); }
private void Update() { while (m_Requests.Count > 0) { var req = m_Requests.Peek(); var task = m_Tasks.Peek(); if (req.hasError) { Debug.Log("GPU readback error detected"); m_Requests.Dequeue(); CommitResult(null, task); m_Tasks.Dequeue(); } else if (req.done) { var buffer = req.GetData <Color32>(); if (m_EncodeTempTex != null && m_EncodeTempTex.width != m_CameraParameters.cameraResolutionWidth && m_EncodeTempTex.height != m_CameraParameters.cameraResolutionHeight) { GameObject.Destroy(m_EncodeTempTex); m_EncodeTempTex = null; } if (m_EncodeTempTex == null) { m_EncodeTempTex = new Texture2D( m_CameraParameters.cameraResolutionWidth, m_CameraParameters.cameraResolutionHeight, TextureFormat.RGB24, false ); } m_EncodeTempTex.SetPixels32(buffer.ToArray()); m_EncodeTempTex.Apply(); if (task.OnReceive != null) { if (m_EncodeTempTex.width != task.Width || m_EncodeTempTex.height != task.Height) { Texture2D scaledtexture; NRDebugger.LogFormat("[BlendCamera] need to scale the texture which origin width:{0} and out put width:{1}", m_EncodeTempTex.width, task.Width); scaledtexture = ImageEncoder.ScaleTexture(m_EncodeTempTex, task.Width, task.Height); CommitResult(scaledtexture, task); //Destroy the scale temp texture. GameObject.Destroy(scaledtexture); } else { CommitResult(m_EncodeTempTex, task); } } m_Requests.Dequeue(); m_Tasks.Dequeue(); } else { break; } } }