基本概念
HDR(High Dynamic Range )高动态范围,在图像显示中颜色是用rgb来表示的他们的值是[0-1],0表示最多亮度,1表示最大值。这样比较暗的物体和更亮的物体就被截断到了0-1,显示的光线范围比较狭窄不能表达更加丰富的光线场景。而人眼的感知范围比较大细微的变化也能感受到,而[0-1]截断缺少了更多的细节。为了让人们看的更加舒服就有了HDR的使用了。用float表示每个像素呈现更多的细节。
由于我们的显示器是使用[0-1]的,最后我们需要一个映射使得HDR->LDR([0-1])这个过程称为Tonemapping
开启HDR&使用
- 每个相机都是独立的开启只需要点击相机的HDR即可
- 使用需要到post-processing effects中,在向前渲染路径中没有使用post-processing effects 即使使用了HDR也不会开启像素会被截断到[0-1]
查看HDR
搭一个简单场景,用一个post-processing 写一个简单shader,如果rgb中有一个值大于1就让改像素像素红色
关键代码
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
fixed4 temp = fixed4(0,0,0,1);
if(col.r > 1 || col.g > 1 || col.b>1)
{
temp = fixed4(1,0,0,1);
}
return temp;
}
效果图
完整代码
Shader "Hidden/ShowHdRForRed"
{
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;
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
fixed4 temp = fixed4(0,0,0,1);
if(col.r > 1 || col.g > 1 || col.b>1)
{
temp = fixed4(1,0,0,1);
}
return temp;
}
ENDCG
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PostTest : MonoBehaviour {
//[ImageEffectOpaque]
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Shader sd = Shader.Find("Hidden/ShowHdRForRed");
Material mt = new Material(sd);
Graphics.Blit(source, destination, mt);
}
}