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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

GetPixels not fetching alpha channel

Discussion in 'General Graphics' started by DaveHoskins, Jul 7, 2015.

  1. DaveHoskins

    DaveHoskins

    Joined:
    Sep 9, 2013
    Posts:
    190
    Hi, I have a procedural shader that renders useful information into all four channels of the returned color information
    But in my code I can't read the alpha channel, it's the same with floats AND rgba32
    Here's the salient parts of my code:
    ...
    buildTex = RenderTexture.GetTemporary(obj.width, obj.height, 0, RenderTextureFormat.ARGBFloat);
    buildTex.filterMode = FilterMode.Point;
    buildTex.filterMode = HideFlags.HideAndDontSave;
    buildTex.mipMapBias = -99;
    buildTex.antiAliasing = 1;

    RenderTexture.active = obj.buildTex;
    tempTex = new Texture2D(obj.width, obj.height, TextureFormat.RGBAFloat, false);
    tempTex.hideFlags = HideFlags.HideAndDontSave;
    tempTex.wrapMode = TextureWrapMode.Clamp;

    DoOnePatch(obj); //-- Render the useful thing..

    tempTex.ReadPixels(new Rect(0, 0, obj.width, obj.height), 0, 0);
    tempTex.Apply();
    pixelHeights = obj.tempTex.GetPixels();
    ...

    pixelHeights alpha channel is ALWAYS 0.0f!

    I don't want to use another texture and read pixels, because it take so long. Is this fixable?
    Is it a bug, or is it designed this way on purpose?
     
  2. DaveHoskins

    DaveHoskins

    Joined:
    Sep 9, 2013
    Posts:
    190
    I'll just give up on that ever working then, shall I?
     
  3. raja-bala

    raja-bala

    Joined:
    Jul 11, 2012
    Posts:
    29
    What about the other channels? Are they alright?
    Does DoOnePatch(..) use DrawMeshNow? That's the only way you can enforce the draw to actually happen.
    If that is the case, why don't you try using other formats and see if that makes a difference?

    Giving up is up to you, but the lack of replies shouldn't dent the logical debug steps.
     
  4. DaveHoskins

    DaveHoskins

    Joined:
    Sep 9, 2013
    Posts:
    190
    That's not it at all, I tried swapping the alpha channel with any of the others and the data came through. I even returned 1.0 on every channel and the alpha came back as zero.
    I have to now do the math in the shader itself and just return just the red channel, which of course I have to get pixels on all channels for it to work. I have to compromise to get around unity restrictions and bugs, again.
     
  5. Zicandar

    Zicandar

    Joined:
    Feb 10, 2014
    Posts:
    388
    Perhaps in your rendering your not rendering to the alpha channel?
     
  6. DaveHoskins

    DaveHoskins

    Joined:
    Sep 9, 2013
    Posts:
    190
    Ah, nice hint.
    I found I needed a "ColorMask RGBA" instruction.
    Although I'm not sure what it's actually useful for?
    Anyway, I'm happy now!!!!! :)
    Thanks for replying.
     
  7. Zicandar

    Zicandar

    Joined:
    Feb 10, 2014
    Posts:
    388
    ColorMask RGBA tells the shader to output the channels R, G, B and A.
    For some reason unity must default to a ColorMask RGB to save performance (on some platforms?)
    Color masks NOT including alpha are STILL able to use alpha to blend, however they do not WRITE it.