【发布时间】:2016-11-04 18:55:12
【问题描述】:
目前,我尝试将 z 深度效果作为图像效果,但结果图像未正确渲染。出事了……
如果我使用标准着色器(unity 5),结果图像被正确渲染(z 深度图像还可以),但不是无光照着色器。
发生了什么?如果您有任何想法,请告诉我原因。
着色器
Shader "Custom/RenderDepth"
{
Properties
{
_DepthLevel ("Depth Level", Range(1, 3)) = 2
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D_float _CameraDepthTexture;
uniform fixed _DepthLevel;
uniform half4 _MainTex_TexelSize;
struct uinput
{
float4 pos : POSITION;
half2 uv : TEXCOORD0;
};
struct uoutput
{
float4 pos : SV_POSITION;
half2 uv : TEXCOORD0;
};
uoutput vert(uinput i)
{
uoutput o;
o.pos = mul(UNITY_MATRIX_MVP, i.pos);
o.uv = MultiplyUV(UNITY_MATRIX_TEXTURE0, i.uv);
return o;
}
fixed4 frag(uoutput o) : COLOR
{
float depth = UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, o.uv));
depth = pow(Linear01Depth(depth), _DepthLevel);
return depth;
}
ENDCG
}
}
}
CS
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[RequireComponent (typeof(Camera))]
public class Test : MonoBehaviour
{
public Camera _cam;
public Material mat;
public float DepthLevel = 1.0F;
void Start ()
{
_cam.depthTextureMode |= DepthTextureMode.Depth;
}
void Update ()
{
}
void OnRenderImage (RenderTexture source, RenderTexture destination)
{
mat.SetFloat("_DepthLevel", DepthLevel);
Graphics.Blit(source, destination, mat);
}
}
【问题讨论】: