How can I use a Sprite as a 3D object

Hello,

“Sprite Renderer” uses “Order in Layer”, but I would like to achieve an effect so that the sprites behave like 3d objects, and those parts of the sprites that are closer in space are drawn on top

For example, a blue and a red sprite do not pass through each other, instead, there can be only one on top and at the same time entirely.

8413215--1112124--upload_2022-9-4_3-25-42.png

And you can also see that the green sprite is further, but is drawn on top

8413215--1112127--upload_2022-9-4_3-25-56.png

The effect I would like to achieve

8413215--1112130--upload_2022-9-4_3-26-14.png

I understand that this is how Sprite Renderer is designed, for the reason that it was created for 2d, and it does not take into account space as I would like

Grateful for any ideas.

Perhaps this can be done somehow through the shader? Or perhaps there is another component? (not Sprite Renderer)
I read the forum and saw a similar topic, they mentioned that something similar is easy to solve with Universl RP, but without details, so I did not understand what it was about.

Can you not use a standard 3D MeshRenderer and draw a simple quad? Why do you need to use the 2D SpriteRenderer if you want to draw 3D planes?

1 Like

Let me tell you more about what I want to achieve

I plan to use a PNG file with transparency (with an isometric image) that is cut and transformed outside of Unity, resulting in 2 PNG files on the left and right side. (On the left and right sprite)

This way I plan to build a scene that will look like a drawing, but for simplicity it will actually be 3D

The character will also be PNG with transparency

All 3 sprites end up with a different orientation (different rotation along the Y axis)

A small draft to demonstrate my words more clearly


If these sprites are arranged as I showed in the first message, they will visually gather into an isometric image (if you set the camera settings correctly). But at the same time, I will be able to work with the scene in 3D (there are several reasons for this)

As Melv suggested you can do this with a simple quad easily enough. You can also do this with a Sprite renderer, you just need a sprite shader that writes to the depth buffer.

2 Likes

Thanks for your reply!
I haven’t worked with shaders yet, would Shader Graph work for that?
Could you explain this idea in more detail, where could I start?

yes

setup is relatively trivial for just starting---- youtube vids can go through constructing concepts
also reading up inside the online Unity manual /tutorials

texture → color → master node is basic stuff
you are interested in setting surface type DepthWrite/DepthTesting choices that are part of the Graph Inspector

1 Like

sorry for bothering

My Sprite Lit shader doesn’t have the “Allow Material Override” option, I guess I did something wrong…

I have installed Universal RP and also Shader Graph, Unity 2021 LTS version
What did I miss?

not exactly - the sprite pulldowns inside Unity are for quick ease of use expecting you do not want/need extended 3D Graphics; they’ve been stripped out for quick/optimized use

to do what you want either means

  • switching to Mesh renderer + simple quads
  • make a sprite friendly shader that has 3D features you need

just switch ‘Material’ type to extend 3D concepts you need

either unlit…or lit (but only if you are sure you need dynamic lighting)

1 Like

…and because this question is going to come up anyway…

Sprite Renderer component can not talk to a material “directly” unless you make the texture reference name as the component expects

_MainTex

8417568--1113351--upload_2022-9-5_14-2-27.png

1 Like

Thank you very much for your advice, it helped a lot!

At the moment sprites A and B use the new shader and behave like 3D objects.

The documentation for Depth Write\Test says “This property is only exposed if Allow Material Override is enabled for this Shader Graph.”
Interesting fact that it works even when turned off

Now there is a problem with transparency
As far as I understand, each transparent pixel equates to either fully transparent or fully opaque, and “Alpha Clip Treshhold” defines this border.

Please tell me how can I make the new shader have transparency like C and D?
(this is a standard Sprite Renderer object with a base shader)

1 Like

sorry for two posts in a row, forgot to mention that i also tried changing “Surface Type” to “Transparent” but it definitely doesn’t render correctly
8419644--1113858--upload_2022-9-6_23-28-22.png

alpha clipping is this stenciling [example]
two sprites ‘tight’ construction
8420916--1114080--upload_2022-9-6_19-37-21.png

8420916--1114083--sprite_softness.gif

‘soft’ object depth/sorting takes more effort than what was assumed by your diagrams
this is not easy like you wish

I noticed that this problem depends on the value of “Order in Layer”
In the left option, Blue has a higher value
In the right, Red has a higher value

8421774--1114329--upload_2022-9-7_18-4-7.png

If the green Order in Layer is set above all

8421774--1114332--upload_2022-9-7_18-4-38.png

Theoretically, it is generally possible to solve this problem, or their “Sprite Renderer” nature will not allow to do this, and is it worth trying through Quad?

Sorry for taking up so much of your time already

i’m not sure;
you’ve hit the limit of what I technically understand for realtime rendering.

i know that I can not solve the issue in 3D, regardless of 2D sprite construction
sorting VFX is one thing but I work without depth and have to make choices what layer tthings sit on

sorting, depth + softness are afaik not easy; requiring a far more experienced
shader engineer or principal environment artist ( i would assume)

I’m not entirely sure what is going on in those images but it sounds like you want to have semi-transparency. This is usually referred to as Alpha Transparency as it uses the alpha color channel to control the opacity of each pixel. Alpha-transparent objects need to be rendered in a different way from purely opaque objects or objects that only use clipping (they must be rendered in order of furthest from camera to closest) so in this case you will once again need to return to a shader that DOES NOT write to the depth buffer.

Now here’s the bad news: Unity sorts objects to be rendered by their center points which can result in your original problem where one object can appear to pop in front of another and then later behind it. What you probably want is something called Order Independent Transparency which is really tricky, not super performant, and not available out of the box in Unity. However, there is at least one workaround - you could try using the sprite renderer’s ‘Order In Layer’ property to adjust things based on various factors. This would in fact not be too different from how traditional 2D engines sorted and rendered objects in isometric views.

I’m not sure what kinds of performance issues you might run into if you have loads of objects with varying sorting orders but probably unless you plan on having thousands of on-screen sprites it won’t really matter much. Your best bet would be to throw a quick test scene together with loads of randomly placed sprites and change their sorting orders in realtime to see what the impact is and if it’s acceptable for your project.

1 Like

Thanks for your reply!

This method generally works. Even before creating this theme, I tried sorting through a script, but sometimes I notice a drop in FPS in a scene where there are a lot of decorative objects.

Some objects in the scene have a rectangular base (instead of a square one).
Square objects sort nicely when the pivot is set to “base visual center” (by the word Base, I mean the plane of which the imaginary geometric figure touches the floor), no script is needed for that, but the main problem is that I don’t want to cut rectangular base object sprites into square base object sprites. This makes it too difficult to add new content and populate the world. Physics.OverlapSphere is often called in the script, this is probably a performance problem.

In the future, I will still consider the option with Quad, maybe everything is simpler in it, or probably I will try to optimize the sorting script.

Thank you for your attention!