Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Trouble with Graphics.Blit()

Discussion in 'Scripting' started by Crazycarpet, Oct 8, 2023.

  1. Crazycarpet

    Crazycarpet

    Joined:
    Dec 5, 2015
    Posts:
    46
    I have some code where I thought that I was performing 'Blit' wrong where I render a camera to the RenderTexture m_LightIntensityTexture using a Camera then apply the shader from m_SamplingMaterial in Graphics.Blit(). It turns out the issue was with my shader itself and not with the way I am using blit, here is the code in question:

    Code (CSharp):
    1. void Awake()
    2. {
    3.     // Find the sample camera reference.
    4.     SampleCamera = GetComponent<Camera>();
    5.  
    6.     // Create a new material using the lighting tools intensity sampling shader.
    7.     Shader samplingShader = Shader.Find("LightingTools/IntensitySamplingShader");
    8.     m_SamplingMaterial = new Material(samplingShader);
    9.  
    10.     // Rebuild all relevant settings.
    11.     Rebuild();
    12. }
    13.  
    14. void LateUpdate()
    15. {
    16.     // Sample automatically once per frame.
    17.     Sample();
    18. }
    19.  
    20. /// <summary>Automatically rebuilds any relevant runtime features for this component using the current settings. Does not 'sample' automatically.</summary>
    21. public void Rebuild()
    22. {
    23.     // Compute sample camera's aspect ratio.
    24.     m_CameraAspectRatio = (float)SampleCamera.pixelWidth / SampleCamera.pixelHeight;
    25.  
    26.     // Create the render texture.
    27.     int resolutionY = VALID_RESOLUTIONS[resolutionIndex];
    28.     m_LightIntensityTexture = new RenderTexture(Mathf.RoundToInt(resolutionY * m_CameraAspectRatio), resolutionY, 0, RenderTextureFormat.RFloat);
    29.     m_LightIntensityTexture.enableRandomWrite = true;
    30.     m_LightIntensityTexture.Create();
    31.  
    32.     // Set the sample camera's target texture.
    33.     SampleCamera.targetTexture = m_LightIntensityTexture;
    34.  
    35.     // (Re)build the texture buffer.
    36.     m_TextureBuffer = new Texture2D(m_LightIntensityTexture.width, m_LightIntensityTexture.height, TextureFormat.RFloat, false);
    37. }
    38.  
    39. /// <summary>Samples the lighting-only view of the 'sample camera'.</summary>
    40. public void Sample()
    41. {
    42.     // Render the sample camera.
    43.     SampleCamera.Render();
    44.  
    45.     // Sample lighting intensity to the render texture.
    46.     Graphics.Blit(null, m_LightIntensityTexture, m_SamplingMaterial);
    47.  
    48.     // Read all pixels to the texture buffer.
    49.     RenderTexture.active = m_LightIntensityTexture;
    50.     m_TextureBuffer.ReadPixels(new Rect(0, 0, m_LightIntensityTexture.width, m_LightIntensityTexture.height), 0, 0);
    51.     RenderTexture.active = null;
    52.     m_TextureBuffer.Apply(false);
    53.     m_TextureBufferPixels = m_TextureBuffer.GetPixels();
    54. }
    I now realize the issue was with the shader itself not doing what it was supposed to.
     
    Last edited: Oct 15, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    A lot of stuff just "early returns" when it is disabled.

    If you set the
    SampleCamera.enabled = true;
    does it work?

    The other thing to watch out for is this kinda crazy ninja code:

    Anytime you use Find() you're at the mercy of typos and mis-alignment between assets and code. Find / Get are just not good to use if there's a better way. In this case, make a Material asset and drag it into a slot.

    Not only that but if you insist on using
    Shader.Find()
    and you do not have at least one Material in your game referencing each and every single shader you ever contemplate finding with
    Shader.Find()
    , those shaders (including "standard" ones!) will silently be stripped from the build and you'll get magenta goodness.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Forum's are intended to be public resources for everyone and not a personal assistant tool for you alone.

    Editing out your question that has an answer makes this thread useless for posterity. We have no idea now what @Kurt-Dekker is even answering.
     
  4. Crazycarpet

    Crazycarpet

    Joined:
    Dec 5, 2015
    Posts:
    46
    I appreciate your feedback but the question would have just confused users with the same issue. The issue was with my shader code not with anything else.

    I thought it is best to not leave the confusing question up, sorry that is has clearly ruffled your feathers. I appreciate the users response but the response was wrong and the code was correct so it would not have helped anyone.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    I've reported the post to be restored and locked.
     
  6. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    541
    I'm neverminding.
     
    Crazycarpet likes this.
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    You could have responded with what the problem actually was and how you solved it.
     
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    So you found related threads that were just as confusing and instead it was completely unrelated. That'd be a great response basically saying "oh hey, when you see this, it might not actually be related and you might want to look elsewhere."

    Your post was up for all of an hour. I just got done with my lunch break and fixing a bug. An hour is not a long time.

    Also... what's up with the knock on facial hair? Rude...
     
  9. Crazycarpet

    Crazycarpet

    Joined:
    Dec 5, 2015
    Posts:
    46
    You are correct, I restored the original post (with some corrections) and if the thread ends up remaining unlocked (which I doubt given the mess it now is) I will continue posting the next debugging steps to fix the shader itself (the real issue) and the debugging steps I take.

    Your facial hair is bold and shows good character, thanks for the feedback.

    Thanks to @Kurt-Dekker for attempting to solve my problem, the problem is many similar posts I've found contain nothing other than an incorrect solution like the one provided (which I still appreciate, again you have to way of seeing my shader or the info I pass to it), I thought I would delete the post to not have more confusing posts of the kind. But after reconsideration if the post ends up staying opened and is cleaned up it may provide a lot of use to a user in the same predicament is me who keeps running into posts with the wrong solution.

    I've deleted any non-relevant post to clean up the thread.
     
    lordofduct and Kurt-Dekker like this.
  10. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    541
    ...and they all lived happily ever after...

    The End