- Added writing distances in buffer in RenderDepth.shader - Added reading distances directly from render pass in DepthRenderPassFeature.cs - Added example depth reading in DepthTester.cs
80 lines
2.5 KiB
C#
80 lines
2.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Threading.Tasks;
|
|
using UnityEditor.Rendering;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
public class DepthTester : MonoBehaviour
|
|
{
|
|
public int FrameRes = 256;
|
|
public float Offset = 0.02f;
|
|
|
|
private RenderTexture texture;
|
|
public Texture2D texture2D;
|
|
private Camera camera;
|
|
private float farPlane;
|
|
private float nearPlane;
|
|
private int id;
|
|
private Task<float[]> task;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
camera = GetComponent<Camera>();
|
|
id = camera.GetInstanceID();
|
|
farPlane = camera.farClipPlane;
|
|
nearPlane = camera.nearClipPlane;
|
|
texture = BlendShaderController.CreateTextures(1, FrameRes, FrameRes, false)[0];
|
|
camera.targetTexture = texture;
|
|
texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBAFloat, false);
|
|
task = DepthRenderPassFeature.DepthRenderPass.RequestDepthDataAsync(id);
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (texture != null)
|
|
{
|
|
Destroy(texture);
|
|
}
|
|
|
|
if (texture2D != null)
|
|
{
|
|
Destroy(texture2D);
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
lock(task)
|
|
{
|
|
if(task != null)
|
|
{
|
|
if(task.IsCompletedSuccessfully)
|
|
{
|
|
float[] depths = task.Result;
|
|
int centerIndex = (FrameRes / 2) * FrameRes + (FrameRes / 2);
|
|
float depth = depths[centerIndex];
|
|
Vector3 depthPos = transform.position + transform.right * Offset;
|
|
Debug.DrawLine(depthPos, depthPos + transform.forward * depth, Color.green);
|
|
Debug.Log($"chto to risuet: dist:{depth}; depthsCount: {depths.Length}");
|
|
task = DepthRenderPassFeature.DepthRenderPass.RequestDepthDataAsync(id);
|
|
}
|
|
else if(task.IsCanceled)
|
|
{
|
|
task = DepthRenderPassFeature.DepthRenderPass.RequestDepthDataAsync(id);
|
|
}
|
|
}
|
|
}
|
|
|
|
Vector3 raycastPos = transform.position - transform.right * Offset;
|
|
float rayDistance = 0;
|
|
if(Physics.Raycast(transform.position, transform.forward, out var hit, 100))
|
|
{
|
|
rayDistance = hit.distance;
|
|
Debug.DrawLine(raycastPos, raycastPos + transform.forward * rayDistance, Color.red);
|
|
}
|
|
}
|
|
}
|