Search Unity

Question Texture dilema/issue ( or so I think)

Discussion in 'Shader Graph' started by R_Chris_D, Jul 23, 2021.

  1. R_Chris_D

    R_Chris_D

    Joined:
    Sep 28, 2016
    Posts:
    3
    I've been working on a custom function node graph for some time, and I've been documenting myself regarding shaders for a while now (still a noob tho), and I still can't understand this phenomenon about float4 output. Basically I am using shadergraph almost as a 2D editing software. I have some spheres in the scene that I want to use as "alpha" for another shader and I'm using a camera with a RenderTexture output, to produce a Texture2DArray to feed inside my custom function graph. Each slice takes a snapshot of each sphere by itself, and they all have a complete dark material. What I am trying to do is merge each Texture2D from the array, one by one and produce an "alpha" map with 3 intensities ( white, grey, black), Where ( black => is the most transparent area). I am trying to iterate through all my slices in the Texture2dArray, but I've notice something that I do not fully understand. The float4 outputs that the custom function spits, even tho it looks completely 0/1, black/white, and even sampled as pure white/black, it still has some 'hidden' data that reveals itself when increasing the intensity. I don't know much about the depth of images and what they really store in there but I assumed that if I snapshot a completely black object there's no way to retain any other information.


    This is the code for generating the 2 outputs:

    Code (CSharp):
    1. void AlphaGenerate_float (Texture2D source, Texture2DArray mask, int textureCount, SamplerState state, float2 UV, float2 res, float Alpha, float Beta, float Delta, out float4 output, out float4 output2)
    2. {
    3.  
    4.     float4 grey = mask.Sample(state, float3(UV, 0));
    5.     float4 black = 1.0f;
    6.  
    7.     for (int i = 1; i < textureCount; i++)
    8.     {
    9.         black *= grey + float4(min(1.0f,mask.Sample(state, float3(UV, i)).rgb), 1.0f);;
    10.         grey *= float4(min(1.0f, mask.Sample(state, float3(UV, i)).rgb), 1.0f);;
    11.     }
    12.  
    13.     output = float4(black.rgb, 1.0f);
    14.     output2 = float4(grey.rgb, 1.0f) - float4(-0.5f, -0.5f, -0.5f, -0.5f);
    Thanks in advance,
    Chris

    ############################

    Just when I was about to go to bed, after not sleeping for 2 days, I figured it out during a hot shower.. it's the only graph I didn't even think of trying "Clamp", does exactly what I needed.. I even tried making my own 'clamp' with Min, Max, but for some reason I completely forgot about it..
     

    Attached Files:

    Last edited: Jul 23, 2021
  2. R_Chris_D

    R_Chris_D

    Joined:
    Sep 28, 2016
    Posts:
    3
    This is the final code in case someone is interested. Not so many examples on the internet with the custom function node. Code with sphere merging and processing + applying the blur based on resulting map. Wish there was a way to blur the resulting masking map, but not easy to manipulate float4.

    Code (CSharp):
    1.  
    2. void BlurFilters_float (Texture2D source, Texture2DArray mask, int textureCount, SamplerState state, float2 UV, float2 res, float Alpha, float Beta, float Delta, out float4 output, out float4 output2)
    3.    {
    4.  
    5.        float4 grey = mask.Sample(state, float3(UV, 0));
    6.        float4 black = 1.0f;
    7.  
    8.        for (int i = 1; i < textureCount; i++)
    9.        {
    10.            black *= grey + float4(min(1.0f,mask.Sample(state, float3(UV, i)).rgb), 0.0f);;
    11.            grey *= float4(min(1.0f, mask.Sample(state, float3(UV, i)).rgb), 0.0f);;
    12.        }
    13.  
    14.   float4 subt = clamp(float4(black.rgb, 0.0f), 0.0f, 1.0f) * clamp(float4(grey.rgb, 0.0f) - float4(-0.5f, -0.5f, -0.5f, -0.5f), 0.0f, 1.0f);
    15.    float4 tex = 0;
    16.    for (int y = 0; y < Alpha; y++)
    17.    {
    18.        for (int x = 0; x < Alpha; x++)
    19.        {
    20.  
    21.            float2 displace = float2(x, y) - (Alpha - 1) / 2.0;
    22.            float grayscale = (subt.r + subt.g + subt.b) / 3 + displace* Beta / Alpha;
    23.  
    24.            tex += source.Sample(state, UV + displace * Beta * grayscale / Alpha);
    25.        }
    26.    }
    27.    output = tex / (Alpha * Alpha);
    28.    output2 = subt;
    29. }