I want to render high precision float values to a texture, but the values I read are clamped to [0, 1]
. Is there anyway to get unclamped values?
Here is the fragment portion of the shader code:
float4 frag (v2f i) : SV_TARGET
{
return float4(0.098, 0.698, 200, 100000); // Obviously a contrived example.
}
Here is the code creating the texture to render to:
RenderTextureDescriptor rtd = new RenderTextureDescriptor(1, 1, RenderTextureFormat.ARGBFloat);
RenderTexture rt = RenderTexture.GetTemporary(rtd);
RenderTexture.active = rt;
mainCamera.SetReplacementShader(selectionShader, "");
mainCamera.Render(); // OnPostRender will be called
mainCamera.ResetReplacementShader();
Read code (in OnPostRender
):
Texture2D hitTexture = new Texture2D(1, 1, TextureFormat.RGBAFloat, false);
Rect rect = new Rect(0, 0, 1, 1);
hitTexture.ReadPixels(rect, 0, 0, false);
Color pixelSample = hitTexture.GetPixel(0, 0);
Debug.Log("Sample: " + pixelSample);
The output is “RGBA(0.098, 0.698, 1.000, 1.000)”, rather than “RGBA(0.098, 0.698, 200, 100000)” as hoped/expected.
Here is the complete shader code:
Shader "Unlit/NewUnlitShader"
{
SubShader
{
Tags{ "RenderType" = "Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 vertex : SV_POSITION;
};
struct FragOut
{
float value : SV_TARGET;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
float4 frag(v2f i) : SV_TARGET
{
return float4(0.098, 0.698, 200, 100000);
}
ENDCG
}
}
}