Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d 统一模拟深度相机_Unity3d_Shader_Hlsl - Fatal编程技术网

Unity3d 统一模拟深度相机

Unity3d 统一模拟深度相机,unity3d,shader,hlsl,Unity3d,Shader,Hlsl,我试图在unity中模拟深度摄影机。我使用一个着色器来获得深度纹理,我能够让相机显示这个深度图像 现在的问题是比例超出了最近和最远的对象,从“近剪裁”窗格延伸到远剪裁平面。我希望这是灰度的,以便最远的对象的颜色值为0,最近的对象的颜色值为1,而不管这些对象位于剪裁平面之间的何处 这是我得到的屏幕截图: 我正在使用着色器来实现这一点。这是着色器的代码: Shader "Depth/DepthPostProcessShader" { Properties { _Ma

我试图在unity中模拟深度摄影机。我使用一个着色器来获得深度纹理,我能够让相机显示这个深度图像

现在的问题是比例超出了最近和最远的对象,从“近剪裁”窗格延伸到远剪裁平面。我希望这是灰度的,以便最远的对象的颜色值为0,最近的对象的颜色值为1,而不管这些对象位于剪裁平面之间的何处

这是我得到的屏幕截图:

我正在使用着色器来实现这一点。这是着色器的代码:

Shader "Depth/DepthPostProcessShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex;
            sampler2D _CameraDepthTexture;

            fixed4 frag (v2f i) : SV_Target
            {
                //get depth from depth texture
                fixed4 depthCol = tex2D(_CameraDepthTexture, i.uv);

                return depthCol;
            }
            ENDCG
        }
    }
}
我正在将脚本附加到主摄影机。代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DepthPostProcessing : MonoBehaviour
{
    //material that's applied when doing postprocessing
    [SerializeField]
    private Material postprocessMaterial;

    private void Start(){
        //get the camera and tell it to render a depth texture
        Camera cam = GetComponent<Camera>();
        cam.depthTextureMode = DepthTextureMode.Depth;
    }

    //method which is automatically called by unity after the camera is done rendering
    private void OnRenderImage(RenderTexture source, RenderTexture destination){
        //draws the pixels from the source texture to the destination texture
        Graphics.Blit(source, destination, postprocessMaterial);
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类深度后处理:单行为
{
//进行后处理时应用的材质
[序列化字段]
私人材料后处理材料;
私有void Start(){
//获取相机并告诉它渲染深度纹理
摄像头摄像头=GetComponent();
cam.depthTextureMode=depthTextureMode.Depth;
}
//方法,该方法在摄影机完成渲染后由unity自动调用
私有void OnRenderImage(RenderTexture源、RenderTexture目标){
//将像素从源纹理绘制到目标纹理
Blit(源、目标、后处理材料);
}
}

我的想法是,我测量最近和最远物体的距离,以获得更好的比例。我似乎无法找到如何从
sampler2D\u CameraDepthTexture

中获取这些属性。现在的问题是相机的位置值为0。
什么?抱歉,希望我在编辑时对其进行了一点清理。@Draco18s我将问题编辑得更清楚。更改相机的位置可能更容易在渲染深度过程之前剪切平面。