Good afternoon.
I’m trying to get a Render Texture by GetTemporaryRT.
However, a black texture is returning to me.
The console shows:
CommandBuffer: temporary render texture _CameraFrameBuffer not found while executing PostProcessing (SetGlobalTexture)
UnityEngine.Rendering.ScriptableRenderContext:Submit()
CameraRenderer:Submit() (at Assets/MagicByte/Runtime/CameraRenderer.cs:116)
CameraRenderer:Render(ScriptableRenderContext, Camera, Boolean, Boolean, ShadowSettings, PostProcessingAsset) (at Assets/MagicByte/Runtime/CameraRenderer.cs:78)
MagicByteRenderPipeline:Render(ScriptableRenderContext, Camera[]) (at Assets/MagicByte/Runtime/MagicByteRenderPipeline.cs:23)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
Codes:
CameraClearFlags flags = camera.clearFlags;
PostProcessingCamera PPC = camera.GetComponent<PostProcessingCamera>();
if (PPC)
{
postStack.Setup(context, camera, ppAsset);
if (flags > CameraClearFlags.Color)
{
flags = CameraClearFlags.Color;
}
buffer.GetTemporaryRT(frameBufferId, camera.pixelWidth, camera.pixelHeight, 32, FilterMode.Bilinear, RenderTextureFormat.Default);
buffer.SetRenderTarget(frameBufferId, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
PPC.OnRenderCamera();
postStack.PostProcessingDrawing(frameBufferId, BuiltinRenderTextureType.CameraTarget, PPC.OnRenderTone(), 0, buffer);
}
DrawGizmos();
Cleanup();
Submit();
}
void Cleanup()
{
lighting.Cleanup();
buffer.ReleaseTemporaryRT(frameBufferId);
}
PostFXStack Script:
using UnityEngine;
using UnityEngine.Rendering;
public partial class PostProcessingStack
{
const string bufferName = "PostProcessing";
int fxSourceId = Shader.PropertyToID("_PostFXSource");
CommandBuffer buffer = new CommandBuffer
{
name = bufferName
};
ScriptableRenderContext context;
Camera camera;
PostProcessingAsset settings;
public bool IsActive = true;
public void Setup(
ScriptableRenderContext context, Camera camera, PostProcessingAsset settings
)
{
this.context = context;
this.camera = camera;
this.settings = camera.cameraType <= CameraType.SceneView ? settings : null;
}
public void PostProcessingDrawing(int sourceId, RenderTargetIdentifier to, Material material, int pass, CommandBuffer cmd)
{
Drawing(sourceId, to, material, pass, cmd);
context.ExecuteCommandBuffer(buffer);
buffer.Clear();
}
void Drawing(RenderTargetIdentifier from, RenderTargetIdentifier to, Material material, int pass,CommandBuffer cmd)
{
cmd.SetGlobalTexture(fxSourceId, from);
buffer.SetRenderTarget(to, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
if(settings != null)
buffer.DrawProcedural(Matrix4x4.identity, material, pass,MeshTopology.Triangles, 3);
}
}
Shader (Vertex and Pixel):
Shader "Hidden/TonemappingShader"
{
Properties
{
//_MainTexCamera ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
ZTest Always Cull Off ZWrite Off
HLSLINCLUDE
#include "../ShaderLibrary/Common.hlsl"
ENDHLSL
Pass
{
HLSLPROGRAM
#pragma target 3.5
#pragma vertex DefaultPassVertex
#pragma fragment frag
#include "../ShaderLibrary/UnityInput.hlsl"
TEXTURE2D(_PostFXSource);
SAMPLER(sampler_PostFXSource);
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings {
float4 positionCS : SV_POSITION;
float2 fxUV : VAR_FX_UV;
};
Varyings DefaultPassVertex(uint vertexID : SV_VertexID) {
Varyings output;
output.positionCS = float4(
vertexID <= 1 ? -1.0 : 3.0,
vertexID == 1 ? 3.0 : -1.0,
0.0, 1.0
);
output.fxUV = float2(
vertexID <= 1 ? 0.0 : 2.0,
vertexID == 1 ? 2.0 : 0.0
);
if (_ProjectionParams.x < 0.0) {
output.fxUV.y = 1.0 - output.fxUV.y;
}
return output;
}
float4 GetSource(float2 fxUV) {
return SAMPLE_TEXTURE2D(_PostFXSource, sampler_PostFXSource, fxUV);
}
float4 frag (Varyings i) : SV_Target
{
float4 color = GetSource(i.fxUV);
float3 hsv = RGBtoHSV(color.rgb);
hsv.x = gmod(hsv.x + _HSV.x, 1.0);
hsv.yz *= _HSV.yz;
color.rgb = saturate(HSVtoRGB(hsv));
if (_Tone == 1) color.rgb = linearT(color.rgb);
if (_Tone == 2) color.rgb = simpleReinhard(color.rgb);
if (_Tone == 3) color.rgb = lumaBasedReinhard(color.rgb);
if (_Tone == 4) color.rgb = Photographic(color.rgb);
if (_Tone == 5) color.rgb = whitePreservingLuma(color.rgb);
if (_Tone == 6) color.rgb = filmic(color.rgb);
if (_Tone == 7) color.rgb = ACESFilm(color.rgb);
if (_Tone == 8) color.rgb = Gray(color.rgb);
color.rgb = pow(color.rgb,_Gamma);
half3 lms = mul(LIN_2_LMS_MAT, color.rgb);
lms *= _WhiteBalance;
color.rgb = mul(LMS_2_LIN_MAT, lms);
color.rgb = saturate((color.rgb - 0.5) * _Contrast + 0.5);
return color;
}
ENDHLSL
}
}
}