So: I have animation of character- one sprite consisting of 6 sliced sprites. Sprite mode is set to multiple. I am playing animation where I am looping through sliced sprites.
Now: I have shader where I would like to apply different effect based on current sliced sprit.
In other words, could I somehow detect in shader which sliced sprite is currently showed?
Or
Which variable is changing in shader when animation is looping through sliced sprites?
Nothing is changing in the shader. It has no idea which sprite is being displayed. It’s just displaying the sprite mesh it’s given, which has its UVs set to show a specific sprite. If you’re using auto-atlas sprites, there’s no real way to know which sprite is being shown in the shader since the UV range each sprite is in isn’t something the shader knows, or even cares about.
If you want to apply effects based on a particular sprite with no c# code, you’ll have to manually create your atlases so you can know and hardcode the UV range to look for in the shader.
However I highly recommend not doing that and just adding a c# script that swaps the material based on the sprite.
Exactly! This is noticed. Let’s say I have 5 sliced sprites inside of one. So when I am displaying the 1st one UV.x is from 0 to o.2. When displaying the 2nd one UV.x is from 0.2 to 0.4. etc.
My problem is that I want to apply only for sliced sprite something like:
uv.y = uv.y + abs(sin(3.14*(uv.x)))
However, this is applied through whole texture (so from UV.x -0 ->1, not only for sliced texture).
In order to get desired effect I have to modify equation to:
And I don’t know how to get this CountOfSlicedSprites inside of shader. At least if there would be offset of the first sliced sprite then I could calculate it.
The shader doesn’t know what slice is being rendered, just like it doesn’t know what sprite is being rendered. Sliced sprites are done by the sprite renderer building a mesh with multiple parts. Just like determining what sprite you are rendering, you’d have to not use an auto atlased sprite and hard code the UV ranges.
Alternatively you could implement the sliced sprite in a custom shader that applies to a quad (though you’d still need to use a non-auto atlased sprite). Unity 2018 and prior had some c# classes for modifying the sprite / UI mesh that would let you modify the mesh used to add extra data to help out, but they appear to have been removed in 2019.
I am just beginner now and trying to get my head around this.
Is there any way how could I remap UVs of MainText? So the current slice would have UV.x 0 → 1? But to do it in shader. I would prefer not to use sprite atlas. Just in case here is my code, so we understand each other.