Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by IggyZuk · Sep 19, 2018 at 09:22 PM · shadermaterialshadersmaterials

MaterialPropertyBlock causes Z-fighting sprites

Hello, I'm trying to use a MaterialPropertyBlock on a custom sprite shader, but it seems to be causing Z-fighting any time a value is modified inside the MaterialPropertyBlock.


As you can see in the following gif (gif 1), every time the color of a sprite is modified it jumps to the front, and back, flickering like Z-fighting. To be clear, yes both sprites are on the same Sorting Layer, and Order. However, the default sprite material with the same configuration doesn't flicker and looks like gif 2. alt text gif 1: using MaterialPropertyBlock, changing a value causes flicker


I tried using a basic material with the same shader applied and it doesn't have any flickering problems. So the problem definitely has something to do with the MaterialPropertyBlock. alt text gif 2: using basic Material works perfectly (but inefficient)


So my question is: am I'm using MaterialPropertyBlock incorrectly? Is this is a known artifact? How do you deal with it?


 SpriteRenderer spriteRenderer;
 MaterialPropertyBlock mpb;

 void OnEnable()
 {
     spriteRenderer = GetComponent<SpriteRenderer>();
     mpb = new MaterialPropertyBlock();
     spriteRenderer.GetPropertyBlock(mpb);
 }

 void Update()
 {
     mpb = new MaterialPropertyBlock();
     mpb.SetColor("_OutlineColor", OutlineColor);
     spriteRenderer.SetPropertyBlock(mpb);
 }


Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zarenityx · Sep 19, 2018 at 10:18 PM 0
Share

Posting this as a comment since it doesn't really answer the question exactly, but...

It looks like you are using $$anonymous$$aterialPropertyBlocks right. At least, that's how I use them and it works just fine for me. I mostly work in 3D, but I am fairly certain that the way it works in 2D is that, if no method of ordering is specified, it just takes whichever is first on the list to be rendered. Ordinarily this list doesn't change, but if you are doing something like instancing (which I'm assu$$anonymous$$g this is for), or something that might be batchable rather than always be unbatched, it can. Your flickering looks like render order being moved around very fast, since it occurs over the entire sprite uniformly.

As I said, I don't really have a 'real' fix for this, but would it be possible to simply change the ordering explicitly through script while it's animating to be rendered first (and behind), then bring it back into the normal ordering when it's done? If you are instancing these, consider perhaps instancing them in explicit clusters rather than just doing it automatically. It's most certainly a bit of a workaround, but your problem almost certainly has nothing to do with your use of $$anonymous$$aterialPropertyBlock itself. I'd bet it has something to do with batching/instancing, which $$anonymous$$PB works with, while not using $$anonymous$$PB disables it.

avatar image IggyZuk Zarenityx · Sep 20, 2018 at 10:29 AM 0
Share

Thank you, those are some good ideas to try out.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

165 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dynamic batching breaks with the material Mobile/Alpha Blended. 0 Answers

Is this the correct way? 1 Answer

How to apply a color to the second texture in this shader? 1 Answer

How to make a cube with metallic look and soft edges [pic included] 1 Answer

UGUI button gray shader 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges