栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Unity3D

unity保存相机当前帧的图像

Unity3D 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

unity保存相机当前帧的图像

1 创建一个相机,把depth.cs和RenderTexture.cs挂在相机上

2 depth.cs代码
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class Depth : MonoBehaviour
{
    // Start is called before the first frame update
    private Camera m_Camera;
    public RenderTexture depthTexture, source;
    public Material Mat;
    private int width = CameraTexture.width;
    private int height = CameraTexture.height;
    private static int index =0;
    void Start()
    {
         
        m_Camera = gameObject.GetComponent();
   
        // 手动设置相机,让它提供场景的深度信息
        // 这样我们就可以在shader中访问_CameraDepthTexture来获取保存的场景的深度信息
        // float depth = UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, uv)); 获取某个像素的深度值
        m_Camera.depthTextureMode = DepthTextureMode.Depth;
        depthTexture = new RenderTexture(width, height, 32,RenderTextureFormat.ARGBFloat);
        
    }

    private bool test = true;
    void OnPostRender()
    {
        source = m_Camera.activeTexture;
        Graphics.Blit(source, depthTexture, Mat);
        
        //save();
        //if (test)
        //{
        //    save();
        //    test = false;
        //}

    }

    public void save()
    {
        //if (this.name == "RenderAllCamera")
        //{
        //    saveDepthMap(source, "scene.jpg");
        //}
        saveDepthMap(source, "imgs/" + this.name + index + "_scene.jpg");
        //saveDepthMap(depthTexture, "imgs/" + this.name + index + ".jpg");
        index++;
        Debug.Log(index);
    }


    // Event function that Unity calls after a Camera has finished rendering, that allows you to modify the Camera's final image.
    

    private void saveDepthMap(RenderTexture DepthRenderTexture, string MapName)
    {
        int Width = DepthRenderTexture.width;
        int Height = DepthRenderTexture.height;
        Texture2D texture2D = new Texture2D(Width, Height);
        var previous = RenderTexture.active;
        RenderTexture.active = DepthRenderTexture;
        texture2D.ReadPixels(new Rect(0, 0, Width, Height), 0, 0);
        RenderTexture.active = previous;
        texture2D.Apply();
        byte[] Data = texture2D.EncodeToPNG();
        FileStream file = File.Open(MapName, FileMode.Create, FileAccess.Write);
        BinaryWriter writer = new BinaryWriter(file);
        writer.Write(Data);
        file.Close();
    }
    
}
3 RenderTexture.cs代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 

public class CameraTexture : MonoBehaviour
{
    // Start is called before the first frame update

    public RenderTexture renderTexture;
    public  static  int width = 32;
    public  static  int height = 26;
   
    void Start()
    {
        renderTexture = new RenderTexture(width, height, 32, RenderTextureFormat.ARGBFloat);
        GetComponent().targetTexture = renderTexture;

    }

    // Update is called once per frame
    void Update()
    {
       
    }
}

4 DepthMaterial 1 创建材质球

2 替换为depth shader

depth.shader
Shader "Custom/Depth" 
{  
	SubShader {  
		
		Pass{  
			CGPROGRAM  
			#pragma vertex vert  
			#pragma fragment frag  
			#include "UnityCG.cginc"  
 
			sampler2D _CameraDepthTexture;  
			struct v2f 
			{  
			   float4 pos : SV_POSITION;  
			   float4 scrPos:TEXCOORD1;  
			};  
 
			v2f vert (appdata_base v)
			{  
			   v2f f;  		  
			   f.pos = UnityObjectToClipPos (v.vertex);  
			   f.scrPos=ComputeScreenPos(f.pos);  
			   return f;  
			}  
			  
			half4 frag (v2f f) : COLOR
			{  
			   float depthValue =Linear01Depth (tex2Dproj(_CameraDepthTexture,UNITY_PROJ_COORd(f.scrPos)).r);
			   return half4(depthValue*10,depthValue*10,depthValue*10,1);  
			}  
			ENDCG  
		}  
	}  
	FallBack "Diffuse"  
} 
5 如下


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/899745.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号