zoukankan      html  css  js  c++  java
  • u3d摄像机截图

    using System;
    using UnityEngine;
    using System.Collections;
    
    public class TestCamreaCapture1 : MonoBehaviour {
    
        private string imagesPath;
        private int imageIndex = 0;
    
        private RenderTexture rt;
        private Texture2D screenShot;
    
        private System.Diagnostics.Stopwatch stopWatch;
    
        // Use this for initialization
        void Start () {
            imagesPath = Application.dataPath + "/../pic/";
            stopWatch = new System.Diagnostics.Stopwatch ();
    
            rt = new RenderTexture (Screen.width,Screen.height, 24);
            //将rt附给一个摄像机,这样这个摄像机实时渲染的结果就都在这个tex上了.
            GetComponent<Camera>().targetTexture = rt;
            screenShot = new Texture2D (Screen.width,Screen.height, TextureFormat.ARGB32, false);
        }
        
        // Update is called once per frame
        void Update () {
            StartCoroutine (ScreenShoot());
        }
    
        IEnumerator ScreenShoot(){
            yield return new WaitForEndOfFrame ();
            stopWatch.Start ();
            ScreenSave ();
            imageIndex++;
            stopWatch.Stop();
            Debug.Log (string.Format("generate one pic use {0} 'ms",stopWatch.ElapsedMilliseconds));
            stopWatch.Reset ();
        }
    
        private void ScreenSave(){
            //记住当前渲染纹理.
            var remeber = RenderTexture.active;
            //将相机的渲染结果设置为当前渲染.
            RenderTexture.active = rt;
            //读取
            screenShot.ReadPixels (new Rect (0, 0, Screen.width, Screen.height), 0, 0);
            byte[] bytes;
            bytes = screenShot.EncodeToJPG ();
            System.IO.File.WriteAllBytes (imagesPath + imageIndex +".jpg", bytes);
            //将之前的渲染纹理变成激活.
            RenderTexture.active = remeber;
    
        }
    }

    个人最喜欢这种方式,效率已经相当高了

  • 相关阅读:
    计算机网络笔记6-应用层
    计算机网络笔记5-传输层
    计算机网络笔记4-网络层
    计算机组成原理笔记7-输入输出系统
    计算机组成原理笔记6-总线
    计算机组成原理笔记5-中央处理器
    计算机网络笔记3-数据链路层
    计算机组成原理笔记4-指令系统
    计算机组成原理笔记3-存储系统
    信息安全数学基础笔记
  • 原文地址:https://www.cnblogs.com/leeplogs/p/6758851.html
Copyright © 2011-2022 走看看