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<Camera>();
// 手动设置相机,让它提供场景的深度信息
// 这样我们就可以在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.
/*
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (null != Mat)
{
// Copies source texture into destination render texture with a shader.
// 使用material把这个source渲染到那个destination, Blit(source, destination, material)
// 使用这个material的意思是使用这个material的shader
// 这个material没必要赋到物体上
// Graphics.Blit(source, destination, Mat);
RenderTexture depthTexture = new RenderTexture(source.width, source.height, 32);
Graphics.Blit(source, depthTexture, Mat);
saveDepthMap(depthTexture);
}
}
*/
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<Camera>().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 如下
版权声明:本文为qq_41286360原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。