Shapes with Render Queue set have material recreated every run

Avatar
  • updated
  • Declined

What I'm seeing


I am setting the Render Queue on a Polygon that lives in the scene. Each time I run the game, the scene is marked as dirty. When I save, the text diff shows that a new material got created and the renderer now references that object:

For example, this gets deleted:

--- !u!21 &1761578016
Material:
serializedVersion: 8
m_ObjectHideFlags: 0 <...>

This gets created:

--- !u!21 &81265532
Material:
serializedVersion: 8
m_ObjectHideFlags: 0 <...>

And the MeshRenderer goes from

m_Materials:
- {fileID: 1761578016}

to:

m_Materials:
- {fileID: 81265532}

The two material are identical other than that ID.

Each run causes this change.

Other Versions

I also tried this in 2022.3.51f1 (with Built-in) and in 6000.0.35f1 (with URP)

Those act a little differently. I get 

Destroying assets is not permitted to avoid data loss.
If you really want to remove an asset use DestroyImmediate (theObject, true);
UnityEngine.Object:Destroy (UnityEngine.Object)
Shapes.ShapesExtensions:TryDestroyInOnDestroy (UnityEngine.Object,UnityEngine.Object) (at Assets/Shapes/Scripts/Runtime/Utils/ShapesExtensions.cs:84)
Shapes.ShapeRenderer:TryDestroyInstancedMaterials (bool) (at Assets/Shapes/Scripts/Runtime/Components/ShapeRenderer.cs:372)
Shapes.ShapeRenderer:OnDestroy () (at Assets/Shapes/Scripts/Runtime/Components/ShapeRenderer.cs:339)

Why am I doing this?

I saw the post where you were asking why someone would even want this feature, so I figure I'll explain the reasoning here. 

It's mostly because I am awful at graphics and this is what worked for me. ;) This is beyond the scope of the question, but maybe I can steal some advice.

I am making a card game that displays at a slight angle. I have a situation where I want to see the following things, back-to-front:

1. Opaque "table" background

2. Polygon with transparency to encapsulate a certain area of cards

3. Translucent auras (I am just using the Standard shader and a texture at the moment) for hover/selection/etc...

4. The alpha-tested card with a super basic shader (some simple bells and whistles like saturation and brightness). No actual transparency

The issue is that I want those auras in #3 to sit just a little below the card and just a little above the polygon area. Depending on position and angle, #2 and #3 will occasionally be rendered in the wrong order. I suspect this is because it's sorting by distance from center of object to camera.

My answer was to set the Polygon Render Queue to 2600 so it renders after the opaque and alpha-test, but before all the other transparent stuff. Obviously if I add more "this before this" transparency rules, it will get annoying. I also looked at Sorting Layers, but it seemed like that was going to make me have to put everything in layers and I wasn't sure if that was the correct approach.

Thanks for your time and for the awesome library. I am happy to provide examples/repros.

 

Reporting a bug? please specify Unity version:
2022.3.5f1
Reporting a bug? please specify Shapes version:
4.5
Reporting a bug? please specify Render Pipeline:
Built-in render pipeline
Avatar
Freya Holmér creator
  • Declined

So, you're likely having two separate issues!

one is that materials are serialized to the scene, which is, a complicated issue. The other, about "Destroying assets is not permitted to avoid data loss", is a bug introduced in the latest update, and there's a workaround in this thread

as for sorting, generally it's best to stick to sorting by depth, and placing things along the depth axis of your camera in order to control sorting. Having custom settings on separate objects will break batching, which can cause performance issues long term. I'm not 100% sure if you had a question/request beyond those two existing threads, but I presume it was mostly about those two things!

Avatar
T. J.

Thanks for the super prompt replay! Looks like both issues are well know. Sorry the serialization one is such a pain :'(

Also, you've led me to a very simple solution! I might be misunderstanding what you were saying about playing objects along the camera depth axis. That's what got me into this mess (because my camera is tilted but the board is an arrangement of flat object).


What I was thinking I really wanted was to be able to control the sort calculation. Your message got me to look around and apparently that's just a free feature (Transparency Sort Mode/Axis). I just set that to Custom and (0,0,1). Everyone is much better behaved now. Perf is really never going to be an issue in this game, but it makes management a lot easier.

So thanks for the nudge!