【问题标题】:How do I stop a camera from rendering to a RenderTexture?如何阻止相机渲染到 RenderTexture?
【发布时间】:2020-09-16 22:10:04
【问题描述】:

我的场景中有一个相机,它的目标纹理设置为渲染纹理。我希望该相机将场景渲染到渲染纹理中,然后停止渲染,基本上将帧冻结到渲染纹理中。我怎样才能做到这一点?禁用相机或将其目标纹理设置为 null 似乎只会使渲染纹理看起来不可见。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    如果你想拥有快照功能,你可以这样做

    public class SnapshotController : MonoBehaviour
    {
        [Tooltip("If you want to capture a specific camera drag it here, otherwise the MainCamera will be used")]
        [SerializeField] private Camera _camera;
     
        [Tooltip("If you have a specific RenderTexture for the snapshot drag it here, otherwise one will be generated on runtime")]   
        [SerializeField] private RenderTexture _renderTexture;
        
        private void Awake()
        {
            if(!_camera) _camera = Camera.main;
        
            if(!_renderTexture)
            {
                _renderTexture = new RenderTexture(Screen.width, Screen.height , 24 , RenderTextureFormat.ARGB32);
                _renderTexture.useMipMap = false;
                _renderTexture.antiAliasing =1;
            }
        }
        
        public void Snapshot(Action<Texture2D> onSnapshotDone)
        {
            StartCoroutine(SnapshotRoutine(onSnapshotDone));
        }
        
        private IEnumerator SnapshotRoutine (Action<Texture2D> onSnapshotDone)
        {
            // this also captures gui, remove if you don't wanna capture gui
            yield return new WaitForEndOfFrame(); 
                   
            // If RenderTexture.active is set any rendering goes into this RenderTexture
            // instead of the GameView
            RenderTexture.active = _renderTexture;
            _camera.targetTexture = _renderTexture;
                   
            // renders into the renderTexture
            _camera.Render();
         
            // Create a new Texture2D        
            var result = new Texture2D(Screen.width,Screen.height,TextureFormat.ARGB32,false);
            // copies the pixels into the Texture2D          
            result.ReadPixels(new Rect(0,0,Screen.width,Screen.height),0,0,false);
            result.Apply();
           
            // reset the RenderTexture.active so nothing else is rendered into our RenderTexture      
            RenderTexture.active = null;
            _camera.targetTexture = null;
        
            // Invoke the callback with the resulting snapshot Texture
            onSnapshotDone?.Invoke(result);
        }
    }
    

    然后你会像这样使用它

    // Pass in a callback telling the routine what to do when the snapshot is ready
    xy.GetComponent<SnapshotController>(). Snapshot(HandleNewSnapshotTexture);
    
    ...
    
    private void HandleNewSnapshotTexture (Texture2D texture)
    {
        var material = GetComponent<Renderer>().material;
    
        // IMPORTANT! Textures are not automatically GC collected. 
        // So in order to not allocate more and more memory consider actively destroying
        // a texture as soon as you don't need it anymore
        if(material.mainTexture) Destroy (material.mainTexture);     
    
        material.mainTexture = texture;
    }
    

    【讨论】:

      【解决方案2】:

      您可以在开始冻结的帧中拍摄实际渲染纹理的快照,获取此图像数据并将渲染纹理替换为应用此冻结图像的实际纹理。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-01
        • 2018-02-11
        • 2015-08-13
        • 2019-05-23
        • 2014-10-03
        • 2014-07-24
        • 2016-10-26
        • 1970-01-01
        相关资源
        最近更新 更多