Good evening) I make a 2d outline shader. Found answer to the post:
there, script
came up to me, but the outline resizes with the removal of the camera (I’m trying to make the size static (the size of the contour changes with the removal of the camera, but I do NOT need it )
Will you help me please?(
The shader was designed such that _OutlineWidth was defined in screen pixels width. The _ScreenParams in the above shader holds the current render resolution. You can remove that / _ScreenParams.xy, but then the _OutlineWidth will be in “screen space” width. I’m not entirely sure what you mean by “removal of the camera” though. Are you using this on something you’re manually rendering to a render texture?
this shader adjusts the outline size depending on the distant camera, but I don’t need that. I need a outline size of 3 (for example), that is, when the camera is farther away, it will be almost invisible (or it will be visible but quite a bit) will you help me please?
if remove _ScreenParams.xy from the code, then the outline will appear only if the camera is very, very close. Gif:
3 what. Three sprite texels? The shader has no idea how big a texel is in the vertex shader (where this code is running), so that’s not actually possible to do programatically. You’d have to fudge the size to be whatever you needed. And it’ll break if the sprite’s scale changes because, again, the vertex shader has no idea how big a texel is.
Because the _OutlineWidth is now in screen widths. If you originally had the value at “3” or something, you’ll want to set it to something like 0.002 after you remove that value to get something similar. But it’s technically still being done in screen space.
For what you’re looking to do you probably want something like this:
// add this line
v.vertex.xy += offset * _OutlineWidth;
o.pos = UnityObjectToClipPos(v.vertex);
// comment this line out or delete it
// o.pos.xy += offset * 2 * o.pos.w * _OutlineWidth / _ScreenParams.xy;
If you’re working on your sprites on a pixel sized canvas, _OutlineWidth will be fairly easy to understand as it’ll just be canvas pixels. But again, if you scale the sprite to anything not 1:1 you’ll have to figure out what the “correct” _OutlineWidth value is.
Once again I have to contact you … I’m trying to improve this shader, as I found out that I could use the Flip function in SpriteRenderer, I had to write “Cull Off”. Then I decided to get rid of the weird, narrow outline. It appears even when “_OutlineWidth” = 0, no matter how the script is written So:
And the last thing. I needed to make the outline adjust to the screen size (as it was at the very beginning), but with some kind of border (that is, the outline size adjusts to the distance of the camera if it is not too far (some number)) … That is, with a very strong distance from the camera, this would not happen:
(the picture is not that far away but this is just an example. I will be glad for any help (especially with the last question, it worries me very much) For earlier thanks)
Its “fine”, but not necessarily great. A better option would probably be to swap the sprite’s material to one that doesn’t use the outline shader instead.
Otherwise I’d probably fix it in the vertex shader. Setting the alpha to zero does mean you don’t see it, but you’re still paying the full cost of it rendering at those pixels.
The last lines in the post above refer to the shader in its original state (where the outline size adjusts to fit the screen). I will try to ask again How about putting a limit on the maximum size of the outline? (that is, if the camera is too far, for example, then the increase in the outline width stops) This worries me, and the last thing I want to improve. And yes, thank you so much for the answer.
This specific technique can’t handle sharp corners, unless the edges align perfectly with the world space axis. It’s an unsolvable limitation of a multi-pass setup like this. If you want outlines on arbitrarily rotated objects, you will need to do the outline using a different technique.