Search Unity

Question Texture Access API jobyfied approach

Discussion in 'General Graphics' started by RendergonPolygons, Nov 29, 2021.

  1. RendergonPolygons

    RendergonPolygons

    Joined:
    Oct 9, 2019
    Posts:
    98
    Hello all,

    I am trying to re-use this rad jobyfied Set_Pixel_Data_Burst_Parallel example, but I can't get it to work. Can you please give me some guidance what I may be doing wrong?

    I am trying to change the alpha for the pixels of a texture that are black. This is the original image RGBA32 image: https://drive.google.com/file/d/1mpJOnI804121PFuxlhtZ4VFJBAhzJp-v/view?usp=sharing

    And this is the output image: all grays were changed to white and some yellow was added. Black was untouched. https://drive.google.com/file/d/1K48VjWJLNdj_nNrXmdupB0XfEZdG1wXO/view?usp=sharing

    This is what I did:

    - I solve for those pixels with R=0 in RGB to set its alpha=0, but debug tells me the R in RGB for black pixels is RGBA(255, 0, 0, 0) (which photoshop tells me this RGB is red?!). So I solved to change the alpha for those pixels with G=0 in RGB.

    - According to the debug, all other pixels that are not G=0 in RGB are RGBA(255, 255, 255, 255), and in theory those should not have changed. However as you can see the grays also changed to white.

    - Not sure if the debug is right - log says Unsupported string.Format for Burst for debug.log

    This is the code I used trying to keep it as close as I can to the one you posted. I call UpdateColorRangeTransparency():

    Code (CSharp):
    1. UpdateColorRangeTransparency(int TexWidth, ColorRange.x, ColorRange.y);
    2. void UpdateColorRangeTransparency(int TexWidth, float bottomColorRange, float topColorRange)
    3.         {
    4.             Unity.Collections.NativeArray<UnityEngine.Color32> data = m_Texture.GetRawTextureData<UnityEngine.Color32>();
    5.             var job = new UpdateTextureColorsIntoNativeArrayBurstParallel()
    6.             {
    7.                 data = data,
    8.                 _TexWidth = TexWidth,
    9.                 bottomColorRange = bottomColorRange,
    10.                 topColorRange = topColorRange
    11.             };
    12.             job.Schedule(TexWidth, 1).Complete();
    13.         }
    14.         [BurstCompile]
    15.         struct UpdateTextureColorsIntoNativeArrayBurstParallel : IJobParallelFor
    16.         {
    17.             [NativeDisableParallelForRestriction] public NativeArray<UnityEngine.Color32> data;
    18.             public int _TexWidth;
    19.             public float bottomColorRange;
    20.             public float topColorRange;
    21.             public void Execute(int y)
    22.             {
    23.                 int _pixelPos = 0;
    24.                 var idx = y * _TexWidth;
    25.                 for (var x = 0; x < _TexWidth; ++x)
    26.                 {
    27.                     _pixelPos = idx++;
    28.                
    29.                     if (data[_pixelPos].g >= bottomColorRange && data[_pixelPos].g < topColorRange)//bottomColorRange is 0; topColorRange is 25
    30.                     {
    31.                         data[_pixelPos] = new UnityEngine.Color(data[_pixelPos].r, data[_pixelPos].g, data[_pixelPos].b, 0);
    32.                         Debug.Log("INRANGE - Data is "+ data[_pixelPos] + " vs range bottom " + bottomColorRange + " top range "+ topColorRange);
    33.                     }
    34.                     else
    35.                     {
    36.                         data[_pixelPos] = new UnityEngine.Color(data[_pixelPos].r, data[_pixelPos].g, data[_pixelPos].b, 255);
    37.                         Debug.Log("OUTSIDERANGE - Data is " + data[_pixelPos] + " vs range bottom " + bottomColorRange + " top range " + topColorRange);
    38.                     }
    39.                 }
    40.             }
    41.         }
     
  2. RendergonPolygons

    RendergonPolygons

    Joined:
    Oct 9, 2019
    Posts:
    98
    In case someone lands in this thread, making all color32 zero worked: data[_pixelPos] = new UnityEngine.Color32(0, 0, 0, 0);
     
    PutridEx likes this.