#Mask then blur - Shader Help
1 messages · Page 1 of 1 (latest)
here is a thread for anyone who's able to help, I don't want to keep cluttering the channel so moving everything here
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
so each square is a main texture and it's tilled to create a mask for the cutoff?
is what appears to be happening
yeah basically
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
I undertand. First, does the code to cutoff work well?
there's not really a good way to know but based on the full light circle revealing the tilemap texture below it, I assume not.
I believe the blur portion works based on the image below responding to the blur effect being added
does the output without blur look as intended?
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
this part
there's not really a way to know for sure, but in theory there's no reason it wasn't working.
The light wasn't affecting the tilemap which means it is probably cutting it off
can't you apply to alpha to look albedo to see if the cutoff is well applied?
well in theory right this image
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
I am 80% confident this worked as intended
Is the light in the good spot on this image?
no, not really, I'll draw up an example of what it should look like
it should look something like this
or that same effect
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
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?
nono the grey background shown here
so it a back ground
yeah
black or nothing
gray = 0 alpha?
So Light tex - gray zone of Main tex and blur the result.
so it would be here let me get them side by side
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
(ignore the green and orange), so this would be the result before the blur?
the opposite
so it would only leave the middle area light
yes exactly that
ok I see
then that would blur and any overlap with the main texture would mask out and reveal it's texture
ok
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?
I tried on my 3d project, should be the same
oh I see
let me clean a bit
okay sounds perfect! Is that still using a custom script?
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
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.
oh I see that yeah
do the UVs still need to be split on the left side?
or can they share one
Can split, I guess?
okay! I'll try this out and let you know in a sec
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
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?
like the light texture?
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
Alright
do you want me to do it the same way just straight into the albedo
or with the whole shader
in the light tex blur fonction
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
drop the image in the shader graph
yes
is the donut one texture?
it's the _MainTex
what is this?
show me the preview of the main text sampler node
of your shader
your donut isn't one big texture but look of small game object with the same mat?
if you want you can test using the same
if you get a 2d project
it's a tilemap but you can paint "tiles" inside
yeah
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
okay!
get the distance between light and pixel, apply inverse square law with the distance.
I can't do this
I spent like 20 hours doing that and it's super laggy
I see
I think this approach is good
something small is breaking it I think
it's really finnicky
your main tex is only a small part but the light text is the whole area
and if not can we not set it
what if we use a screen position on both the tex and light
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
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
you can use the point light png to debug instead
I can't really move it
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
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?
well that's what the cutout part does
then the idea is to blur it
you don't need the cutout because every tile are in the dark section.
I do need the cutout
hmm, I will create a 2d project and comeback
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
i feel you
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
loading... I will go eat something meanwhile
okay, thank you!
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
well, if you are really stuck at this. You can send me the asset for me to look when I have free time.
Godspeed
it's basically just a tilemap with a square tile painted over it to make that shape with the shader applied
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 🫂
Thank you. Sleep well and good luck.
thanks you too! I'll let you know if I get it working!
Mask then blur - Shader Help
Hi there, idk if I'm allowed to share zips on here. This is a really brittle project. I think the Kawase Blur isn't happy with my rendertexture approach (and I can't say I am either lol) but you can see for yourself how you might implement something like this w/ scriptable render passes here:
https://drive.google.com/file/d/1aKCA5INhGoVFhjd1jvceOTgP0EUw9q8i/view?usp=drive_link
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.
Oh my gosh, thank you so much! I'll have a look at that today, I didn't even think to check this thread until now. You are the best.
I'm so sorry for the lack of response until now, but holy I really appreciate it.
I just went over the system and I am so very thankful for these wonderful ideas, I am so extremely appreciative of you taking the time to do all this. I'll definitely try to employ some of these ideas into my lighting system as they seem very applicable to creating the ideal system I was going for. Truly an amazing person and I really appreciate your work here much love <3 🫂
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!
Haha I know exactly what you mean, I used to do the same thing with python.