Search Unity

Bug Render Texture is black

Discussion in 'General Graphics' started by MatheusMarkies, Aug 3, 2020.

  1. MatheusMarkies

    MatheusMarkies

    Joined:
    Apr 16, 2017
    Posts:
    67
    Good afternoon.
    I'm trying to get a Render Texture by GetTemporaryRT.
    However, a black texture is returning to me.

    The console shows:
    Code (CSharp):
    1. CommandBuffer: temporary render texture _CameraFrameBuffer not found while executing PostProcessing (SetGlobalTexture)
    2. UnityEngine.Rendering.ScriptableRenderContext:Submit()
    3. CameraRenderer:Submit() (at Assets/MagicByte/Runtime/CameraRenderer.cs:116)
    4. CameraRenderer:Render(ScriptableRenderContext, Camera, Boolean, Boolean, ShadowSettings, PostProcessingAsset) (at Assets/MagicByte/Runtime/CameraRenderer.cs:78)
    5. MagicByteRenderPipeline:Render(ScriptableRenderContext, Camera[]) (at Assets/MagicByte/Runtime/MagicByteRenderPipeline.cs:23)
    6. UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
    Codes:

    Code (CSharp):
    1. CameraClearFlags flags = camera.clearFlags;
    2.  
    3.         PostProcessingCamera PPC = camera.GetComponent<PostProcessingCamera>();
    4.         if (PPC)
    5.         {
    6.  
    7.             postStack.Setup(context, camera, ppAsset);
    8.  
    9.             if (flags > CameraClearFlags.Color)
    10.             {
    11.                 flags = CameraClearFlags.Color;
    12.             }
    13.  
    14.             buffer.GetTemporaryRT(frameBufferId, camera.pixelWidth, camera.pixelHeight, 32, FilterMode.Bilinear, RenderTextureFormat.Default);
    15.             buffer.SetRenderTarget(frameBufferId, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
    16.  
    17.             PPC.OnRenderCamera();
    18.             postStack.PostProcessingDrawing(frameBufferId, BuiltinRenderTextureType.CameraTarget, PPC.OnRenderTone(), 0, buffer);
    19.  
    20.         }
    21.  
    22.         DrawGizmos();
    23.         Cleanup();
    24.         Submit();
    25.     }
    Code (CSharp):
    1. void Cleanup()
    2.     {
    3.         lighting.Cleanup();
    4.         buffer.ReleaseTemporaryRT(frameBufferId);
    5.     }
    PostFXStack Script:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3.  
    4. public partial class PostProcessingStack
    5. {
    6.  
    7.     const string bufferName = "PostProcessing";
    8.     int fxSourceId = Shader.PropertyToID("_PostFXSource");
    9.     CommandBuffer buffer = new CommandBuffer
    10.     {
    11.         name = bufferName
    12.     };
    13.  
    14.     ScriptableRenderContext context;
    15.  
    16.     Camera camera;
    17.  
    18.     PostProcessingAsset settings;
    19.     public bool IsActive = true;
    20.  
    21.     public void Setup(
    22.         ScriptableRenderContext context, Camera camera, PostProcessingAsset settings
    23.     )
    24.     {
    25.         this.context = context;
    26.         this.camera = camera;
    27.         this.settings = camera.cameraType <= CameraType.SceneView ? settings : null;
    28.  
    29.     }
    30.  
    31.     public void PostProcessingDrawing(int sourceId, RenderTargetIdentifier to, Material material, int pass, CommandBuffer cmd)
    32.     {
    33.         Drawing(sourceId, to, material, pass, cmd);
    34.         context.ExecuteCommandBuffer(buffer);
    35.         buffer.Clear();
    36.     }
    37.  
    38.     void Drawing(RenderTargetIdentifier from, RenderTargetIdentifier to, Material material, int pass,CommandBuffer cmd)
    39.     {
    40.         cmd.SetGlobalTexture(fxSourceId, from);
    41.         buffer.SetRenderTarget(to, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
    42.         if(settings != null)
    43.         buffer.DrawProcedural(Matrix4x4.identity, material, pass,MeshTopology.Triangles, 3);
    44.     }
    45. }
    sdfg.PNG sdfgdfg.PNG sdfgdfsg.PNG

    Shader (Vertex and Pixel):
    Code (CSharp):
    1. Shader "Hidden/TonemappingShader"
    2. {
    3.     Properties
    4.     {
    5.         //_MainTexCamera ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Opaque" }
    10.  
    11.         ZTest Always Cull Off ZWrite Off
    12.  
    13.         HLSLINCLUDE
    14.         #include "../ShaderLibrary/Common.hlsl"
    15.         ENDHLSL
    16.  
    17.         Pass
    18.         {
    19.             HLSLPROGRAM
    20.             #pragma target 3.5
    21.             #pragma vertex DefaultPassVertex
    22.             #pragma fragment frag
    23.  
    24.             #include "../ShaderLibrary/UnityInput.hlsl"
    25.  
    26.             TEXTURE2D(_PostFXSource);
    27.             SAMPLER(sampler_PostFXSource);
    28.  
    29.             struct appdata
    30.             {
    31.                 float4 vertex : POSITION;
    32.                 float2 uv : TEXCOORD0;
    33.             };
    34.  
    35.             struct Varyings {
    36.                 float4 positionCS : SV_POSITION;
    37.                 float2 fxUV : VAR_FX_UV;
    38.             };
    39.  
    40.             Varyings DefaultPassVertex(uint vertexID : SV_VertexID) {
    41.                 Varyings output;
    42.                 output.positionCS = float4(
    43.                     vertexID <= 1 ? -1.0 : 3.0,
    44.                     vertexID == 1 ? 3.0 : -1.0,
    45.                     0.0, 1.0
    46.                     );
    47.                 output.fxUV = float2(
    48.                     vertexID <= 1 ? 0.0 : 2.0,
    49.                     vertexID == 1 ? 2.0 : 0.0
    50.                     );
    51.                 if (_ProjectionParams.x < 0.0) {
    52.                     output.fxUV.y = 1.0 - output.fxUV.y;
    53.                 }
    54.                 return output;
    55.             }
    56.  
    57. float4 GetSource(float2 fxUV) {
    58.     return SAMPLE_TEXTURE2D(_PostFXSource, sampler_PostFXSource, fxUV);
    59. }
    60. float4 frag (Varyings i) : SV_Target
    61. {
    62.  
    63.                 float4 color = GetSource(i.fxUV);
    64.  
    65.                 float3 hsv = RGBtoHSV(color.rgb);
    66.                 hsv.x = gmod(hsv.x + _HSV.x, 1.0);
    67.                 hsv.yz *= _HSV.yz;
    68.                 color.rgb = saturate(HSVtoRGB(hsv));
    69.  
    70.                         if (_Tone == 1) color.rgb = linearT(color.rgb);
    71.                         if (_Tone == 2) color.rgb = simpleReinhard(color.rgb);
    72.                         if (_Tone == 3) color.rgb = lumaBasedReinhard(color.rgb);
    73.                         if (_Tone == 4) color.rgb = Photographic(color.rgb);
    74.                         if (_Tone == 5) color.rgb = whitePreservingLuma(color.rgb);      
    75.                         if (_Tone == 6) color.rgb = filmic(color.rgb);
    76.                         if (_Tone == 7) color.rgb = ACESFilm(color.rgb);
    77.                         if (_Tone == 8) color.rgb = Gray(color.rgb);
    78.  
    79.                 color.rgb = pow(color.rgb,_Gamma);
    80.  
    81.                 half3 lms = mul(LIN_2_LMS_MAT, color.rgb);
    82.                 lms *= _WhiteBalance;
    83.                 color.rgb = mul(LMS_2_LIN_MAT, lms);
    84.  
    85.                 color.rgb = saturate((color.rgb - 0.5) * _Contrast + 0.5);
    86.  
    87.                 return color;
    88.             }
    89.             ENDHLSL
    90.         }
    91.     }
    92. }
    93.  
     
  2. MatheusMarkies

    MatheusMarkies

    Joined:
    Apr 16, 2017
    Posts:
    67
    I fixed the problem by changing GetTemporaryRT to preRender the camera.