using UnityEditor.Media; using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; public class RT2script : MonoBehaviour { MediaEncoder encoder; public RenderTexture colorImage, depthImage; public Texture2D outputImage; public int targetFrameRate = 60; int frameCount = 0; // Start is called before the first frame update void Start() { // Define video attributes var videoAttr = new VideoTrackAttributes { frameRate = new MediaRational(targetFrameRate), width = 512, height = 256, includeAlpha = false }; // Define file path for movie var encodedFilePath = Path.Combine(Path.GetTempPath(), "my_movie_depth.mp4"); Debug.Log(encodedFilePath); if (File.Exists(encodedFilePath)) { File.Delete(encodedFilePath); } colorImage = new RenderTexture(256,256,16,RenderTextureFormat.ARGB32); depthImage = new RenderTexture(256,256,16,RenderTextureFormat.ARGB32); outputImage = new Texture2D((int)videoAttr.width, (int)videoAttr.height, TextureFormat.RGBA32, false); Camera c = GetComponent(); c.targetTexture = colorImage; Camera depthCamera = new GameObject("DepthCamera").AddComponent(); depthCamera.CopyFrom(c); // depthCamera.transform.position = c.transform.position; // depthCamera.transform.rotation = c.transform.rotation; // depthCamera.fieldOfView = c.fieldOfView; // depthCamera.nearClipPlane = c.nearClipPlane; // depthCamera.farClipPlane = c.farClipPlane; // depthCamera.orthographic = c.orthographic; depthCamera.targetTexture = depthImage; depthCamera.clearFlags = CameraClearFlags.SolidColor; depthCamera.backgroundColor = Color.black; depthCamera.SetReplacementShader(Shader.Find("Custom/DepthOnly"), ""); encoder = new MediaEncoder(encodedFilePath, videoAttr); } // Update is called once per frame void Update() { float expectedFrames = Time.timeSinceLevelLoad * targetFrameRate; if (frameCount < expectedFrames) { // Graphics.CopyTexture(colorImage, 0, 0, 0, 0, 256, 256, outputImage, 0, 0, 0, 0); // Graphics.CopyTexture(depthImage, 0, 0, 0, 0, 256, 256, outputImage, 0, 0, 256, 0); RenderTexture.active = colorImage; outputImage.ReadPixels(new Rect(0, 0, 256, 256), 0, 0); outputImage.Apply(); RenderTexture.active = depthImage; outputImage.ReadPixels(new Rect(0, 0, 256, 256), 256, 0); outputImage.Apply(); encoder.AddFrame(outputImage); frameCount += 1; } } void OnDestroy() { encoder.Dispose(); } }