#Mask then blur - Shader Help

1 messages · Page 1 of 1 (latest)

wicked halo
#

here is a thread for anyone who's able to help, I don't want to keep cluttering the channel so moving everything here

harsh burrow
#

post your light and cutoff image

wicked halo
# harsh burrow post your light and cutoff image

the cutoff texture is a tilemap, which is the black area you see, and the light is just a white circle that fades out the tilemap is taken as a _MainTex and the light is a 2D Light Texture node

#

I have a theory as to what might be causing that fracture

#

I think I may need to separate the UVs used for the initial light and the referenced "cutoff image" tilemap

#

that is what I currently have using ```csharp
void GaussianBlur_float(UnityTexture2D TextureMainTexture, UnityTexture2D TextureLight, float2 LightUV, float2 MainTexUV, float Blur, UnitySamplerState Sampler, out float4 Out_RGBA)
{
float4 col = float4(0.0, 0.0, 0.0, 0.0);
float kernelSum = 0.0;

int upper = ((Blur - 1) / 2);
int lower = -upper;

// cut out light from Texture

for (int x = lower; x <= upper; ++x)
{
    for (int y = lower; y <= upper; ++y)
    {
        kernelSum ++;

        float2 offset = float2(_MainTex_TexelSize.x * x, _MainTex_TexelSize.y * y);

        float4 tmp = TextureLight.Sample(Sampler, LightUV + offset);
        col += tmp.a * TextureMainTexture.Sample(Sampler, MainTexUV + offset).a * tmp;
    }
}

col /= kernelSum;
Out_RGBA = float4(col.r, col.g, col.b, col.a);
//Out_Alpha = col.a;

}

#

I don't believe this is properly cutting out my light texture

harsh burrow
#

so each square is a main texture and it's tilled to create a mask for the cutoff?

wicked halo
#

is what appears to be happening

wicked halo
#

however it's all stored on a tilemap

#

and the tilemap holds the shader material

#

I will send screenshots of the three step process I am hoping to achieve and I assume that'll help you visualize what I am going for better

#

^ Step 1, both the light texture and tilemap shown in full

#

^ Step 2, the tilemap cuts itself out from the light in theory producing an effect similar to as shown, however since the light isn't actually on the tilemap at all yet, it is invisible in practice.

#

Step 3, the blur effect is added which would then show on the tilemap which goes into the lerp function with the maintexture to mask and reveal it's texture when the blur is overlapped

harsh burrow
#

I undertand. First, does the code to cutoff work well?

wicked halo
#

I believe the blur portion works based on the image below responding to the blur effect being added

harsh burrow
#

does the output without blur look as intended?

wicked halo
#

with my "working" cutoff effect before it shouldn't show any light at all as it doesn't overlap my texture because it is cutting it off

harsh burrow
#

this part

wicked halo
#

The light wasn't affecting the tilemap which means it is probably cutting it off

harsh burrow
#

can't you apply to alpha to look albedo to see if the cutoff is well applied?

wicked halo
#

is it "working"

#

and a shader will not display anything that isn't on the applied shader area right

#

so it would never show it if it were working correctly, however while working on it

#

there were a lot of ways to tell when it wasn't working

#

// masking out the tilemap early before cutting off etc

wicked halo
harsh burrow
wicked halo
#

it should look something like this

#

or that same effect

wicked halo
# wicked halo

except instead of white it shows the cutout texture's actual texture with a tint of the light color

#

this would be after cutout and before blur, but you wouldn't be able to see this

harsh burrow
#

each square is a plane. can be empty(light blue) or full (black). The texture is sent to the shader in _MainTex. Right?

#

Main tex is these two?

wicked halo
harsh burrow
#

so it a back ground

wicked halo
#

yeah

harsh burrow
#

black or nothing

wicked halo
#

that is errelevant

#

main tex is

#

maintex looks like this

#

not the middle part

harsh burrow
#

gray = 0 alpha?

wicked halo
#

this is maintex

#

yes

#

and all of the maintex area would be 1 alpha

harsh burrow
#

So Light tex - gray zone of Main tex and blur the result.

wicked halo
#

main tex is on the left and on the right is the light which is a part of the "2D Light Texture" Node

#

which grabs all visible lights on the screen and converts them to a texture

#

so then you would cut out any of the light texture with an alpha that overlaps with "MainTex"

#

then blur that and use any of the blur that afterwards overlaps with maintex to mask over maintex and reveal maintex's texture

#

so this is what it would look like after being cut out

#

then blurring that would make it bigger so it would overlap again

#

and it would hopefully give a result like this

harsh burrow
#

(ignore the green and orange), so this would be the result before the blur?

wicked halo
#

so it would only leave the middle area light

harsh burrow
wicked halo
#

yes exactly that

harsh burrow
#

ok I see

wicked halo
#

then that would blur and any overlap with the main texture would mask out and reveal it's texture

harsh burrow
#

ok

harsh burrow
wicked halo
# harsh burrow this?

Yes! Very close to that, except the middle part shouldn't be shown but I assume that is just because "volumetric lighting" is enabled

#

also is your scene in 3d?

harsh burrow
#

I tried on my 3d project, should be the same

wicked halo
harsh burrow
wicked halo
#

you are a legend.

harsh burrow
#

let me clean a bit

wicked halo
harsh burrow
#

void GaussianBlur_float(UnityTexture2D TextureMainTexture, UnityTexture2D TextureLight, float2 LightUV, float2 MainTexUV, float Blur, UnitySamplerState Sampler, out float4 Out_RGBA)
{
float4 col = float4(0.0, 0.0, 0.0, 0.0);
float kernelSum = 0.0;

int upper = ((Blur - 1) / 2);
int lower = -upper;

// cut out light from Texture

for (int x = lower; x <= upper; ++x)
{
    for (int y = lower; y <= upper; ++y)
    {
        kernelSum ++;

        float2 offset = float2(_MainTex_TexelSize.x * x, _MainTex_TexelSize.x * y);

        float4 tmp = TextureLight.Sample(Sampler, LightUV + offset);
        col += tmp.a * (1 - TextureMainTexture.Sample(Sampler, MainTexUV + offset).a) * tmp;
    }
}

col /= kernelSum;
Out_RGBA = float4(col.r, col.g, col.b, col.a);
//Out_Alpha = col.a;

}

#

I think it the same one as we did before

wicked halo
#

oh okay perfect

#

it looks the same

#

I have just been struggling to get it to work I am quite new to shaders in general so I don't understand UVs all too well.

harsh burrow
#

Oh yeah

#

(1- sampler main tex)

#

was the modification

wicked halo
#

oh I see that yeah

harsh burrow
wicked halo
#

or can they share one

harsh burrow
#

Can split, I guess?

wicked halo
#

Hmm, it still doesn't seem to work

#

this is a section in my previous script where I would lerp the values of my light and my maintex to mask it with a color of my choice so maybe that needs to be implemented? But I'm really unsure

wicked halo
# harsh burrow

also in your shader graph all your visuals are very clear, is that just because you are using a custom mask texture or is there a setting I can enable?

harsh burrow
#

Try one thing before

#

sample the light and put the rgb out put on albedo

wicked halo
harsh burrow
#

yes

#

just to see what it look like before everyting

wicked halo
#

this is without a screen position uv

#

which I think is necessary to get the tilemap to act as a whole

#

this is if I add a "screen position" UV

harsh burrow
#

try to use this image for the light

#

just to see if the blur work

wicked halo
#

Alright

#

do you want me to do it the same way just straight into the albedo

#

or with the whole shader

harsh burrow
#

in the light tex blur fonction

wicked halo
#

okay

#

do you know how I use that directly in the shader graph?

#

when I drag it in it locks it to a sample texture 2d node

#

nevermind

#

I got it

harsh burrow
#

drop the image in the shader graph

wicked halo
#

so test this?

harsh burrow
#

yes

wicked halo
#

this gives the same result as before

harsh burrow
#

is the donut one texture?

wicked halo
harsh burrow
#

ok

wicked halo
#

just your shader?

harsh burrow
#

what is this?

wicked halo
#

my donut with your shader

#

somehow

harsh burrow
#

show me the preview of the main text sampler node

wicked halo
harsh burrow
#

of your shader

wicked halo
#

yours looks the same when I remove the default

harsh burrow
#

your donut isn't one big texture but look of small game object with the same mat?

wicked halo
#

if you get a 2d project

#

it's a tilemap but you can paint "tiles" inside

harsh burrow
#

yeah

wicked halo
#

so they all have the same mat

#

if you make a 2d project

#

I can give the same tiles so you can replicate the scene 1-1

harsh burrow
#

one last thing to try

#

a different approach

wicked halo
#

okay!

harsh burrow
#

get the distance between light and pixel, apply inverse square law with the distance.

wicked halo
#

I spent like 20 hours doing that and it's super laggy

harsh burrow
#

I see

wicked halo
#

I think this approach is good

#

something small is breaking it I think

#

it's really finnicky

harsh burrow
#

yes, the problem is

#

you main tex doesm't have any transparentcy

wicked halo
#

oh

#

isn't by default the alpha 1?

harsh burrow
#

your main tex is only a small part but the light text is the whole area

wicked halo
#

and if not can we not set it

wicked halo
#

so they are lined up based on size on screen

#

okay wait

#

I might have got something working

#

I need to lerp it with the black color to test it fully

#

screen position on just the light UV and nothing on the main uv

#

the problem is that everything is white and I can only see the edge of the tilemap because of it's texture

harsh burrow
#

yes

#

can you get a coin of the light with the uvs?

wicked halo
#

this used to show the light idk why it's not atm

#

I had to make a new scene to fix it last time

#

that could be the reason

#

because this isn't displaying light atm for some reason

harsh burrow
#

you can use the point light png to debug instead

wicked halo
#

it's not a good representation of how the light works because it won't be parsed the same way the texture 2d node would be

harsh burrow
#

if you can get the small portion of the light, then blur it and add on the main tex. You would have the same effect

#

maybe?

wicked halo
#

then the idea is to blur it

harsh burrow
#

you don't need the cutout because every tile are in the dark section.

wicked halo
#

I do need the cutout

harsh burrow
#

hmm, I will create a 2d project and comeback

wicked halo
#

if I have no cutout then it will spill overtop everything no matter what

#

I can barely think I've been working at this for like 25 hours haha

harsh burrow
#

i feel you

wicked halo
#

if you make a 2d urp project on unity 2022.3.25f1 or just any recent version

#

and setup urp then make a tilemap I can show you how to replicate my scene fully

harsh burrow
#

loading... I will go eat something meanwhile

wicked halo
#

this is my closest attempt

#

but I don't think it's really cutting anything out

#

however the blur is working

#

I can increase the blur, but if I make it high it slides the light to the right

#

but that's not too important right now

#

I can probably fix that later

harsh burrow
#

well, if you are really stuck at this. You can send me the asset for me to look when I have free time.

#

Godspeed

wicked halo
#

the texture is kinda irrelevant as it is just revealed based on the light

#

but yeah this is a really rough one haha

#

I do really appreciate all your help thusfar, I think I am just also really tired right now so I am not very helpful haha.

#

You're a legend 🫂

harsh burrow
#

Thank you. Sleep well and good luck.

wicked halo
wicked halo
#

Mask then blur - Shader Help

analog oyster
#

If it appears black at first its because the Kawase is acting up and you just need to run the scene.

#

I made it for 2022.3.21f1

#

Scene view doesn't work either lol

#

but if you add things to the light layer it will blur them and multiply them w/ the shadows in the scene

#

control the blur here

#

gradually increasing blur

#

omg realized i rushed this out and made a mistake -- its too late in the night to fix rn but the additive shader needs to change from

#

Blend OneMinusDstColor One to Blend SrcAlpha One

#

on line 20

#

in Sprite-Additive.shader

#

It'll give your sprites form again lol

#

Ah! One more thing to consider is that this blur approach might not handle zooming in and out so you'll need to think a bit about that. One way to handle it would be to have the blurring camera be a constant size.

wicked halo
#

I'm so sorry for the lack of response until now, but holy I really appreciate it.

wicked halo
analog oyster
#

Good luck!! These posts feel like little puzzles to me. This one jumped out bc I haven't thought too much about 2D. Glad the ideas are helpful!

wicked halo