首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何停止将摄像头渲染到RenderTexture?

如何停止将摄像头渲染到RenderTexture?
EN

Stack Overflow用户
提问于 2020-09-17 06:10:05
回答 2查看 848关注 0票数 1

我的场景中有一个摄影机,它的目标纹理设置为渲染纹理。我希望摄像机将场景渲染到渲染纹理中,但随后停止渲染,基本上将帧冻结到渲染纹理中。我如何才能做到这一点?禁用摄影机或将其目标纹理设置为空似乎只会使渲染纹理看起来不可见。

EN

回答 2

Stack Overflow用户

发布于 2020-09-17 15:04:50

如果你想要一个快照功能,你可以这样做

代码语言:javascript
复制
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);
    }
}

然后你可以像这样使用它。

代码语言:javascript
复制
// 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
EN

Stack Overflow用户

发布于 2020-09-17 07:11:49

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63928572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档