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;
    
        }
    }

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

  • 相关阅读:
    array_flip
    qy Undefied index报错
    strip_tag
    query使用
    tp5 sql 大于小于
    OfficeTools.OnlineEditWord
    OCX控件打包成CAB并实现数字签名过程
    使用css技术代替传统的frame技术
    delphi常用函数和方法
    js正则表达使用实例
  • 原文地址:https://www.cnblogs.com/leeplogs/p/6758851.html
Copyright © 2011-2022 走看看