Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Rodiaz89 · Apr 06, 2014 at 09:22 PM · shaderspritesrenderertransparencyz-fighting

What can I do to solve problems of z-fighting with sprites in a 3d world

So I've been looking for a solution to a problem i'm having creating a character with the 2d sprites in a 3d world. I've looked at a couple of similar questions but they havent given me the full solution to the problem. Btw even with an orthographic camera it gives me the problem and I also tried changing the near and far clips of the camera with no success.

This post is the one that helped me the most but its giving me a new problem:

So my character is composed of a couple of quads with the sprite renderer component to create a player that moves. All of the quads are in a different Z and I can't use the sorting layer that unity 2d provides since these characters are going to be moving back and forward in the z plane. Here is an image with the problem:

alt text

Original: The original is when i look at it straight, as you can see it doesn't give me any problems, the problem is when I angle the camera (which i need to for my game)

Original Angled: As you can see once i angle the camera the Z-fighting starts as it just goes and cuts off his hand :(

Custom Shader: I created the custom shader exactly as the post above said and it did fix most of the problems when i angled the camera but i gave me new ones, they are the ones highlighted in the red boxes, they appear to be caused by the alpha from the sprites which seem to cut out the stuff that is close behind it and they start showing up more and more when you angle the camera, for example look at the two images of the head, one shows the neck and once i tilt the camera a bit more it cuts it right off.

I don't really know much about shaders, I've been looking around and I haven't been able to find and answer, what should I do?

Just in case here is the shader that i'm using:

 Shader "Sprites/DefaultZ"
 {
     Properties
     {
         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
         _Color ("Tint", Color) = (1,1,1,1)
         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
     }
 
     SubShader
     {
         Tags
         { 
             "Queue"="Transparent" 
             "IgnoreProjector"="True" 
             "RenderType"="TransparentCutout" 
             "PreviewType"="Plane"
             "CanUseSpriteAtlas"="True"
         }
         
         Cull Off
         Lighting Off
         ZWrite On
         Fog { Mode Off }
         Blend SrcAlpha OneMinusSrcAlpha
         Pass
         {
         
         CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma multi_compile DUMMY PIXELSNAP_ON
             #include "UnityCG.cginc"
             
             struct appdata_t
             {
                 float4 vertex   : POSITION;
                 float4 color    : COLOR;
                 float2 texcoord : TEXCOORD0;
             };
 
             struct v2f
             {
                 float4 vertex   : SV_POSITION;
                 fixed4 color    : COLOR;
                 half2 texcoord  : TEXCOORD0;
             };
             
             fixed4 _Color;
 
             v2f vert(appdata_t IN)
             {
                 v2f OUT;
                 OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
                 OUT.texcoord = IN.texcoord;
                 OUT.color = IN.color * _Color;
                 #ifdef PIXELSNAP_ON
                 OUT.vertex = UnityPixelSnap (OUT.vertex);
                 #endif
 
                 return OUT;
             }
 
             sampler2D _MainTex;
 
             fixed4 frag(v2f IN) : COLOR
             {
                 return tex2D(_MainTex, IN.texcoord) * IN.color;
             }
         ENDCG
         }
     }
 }

image.jpg (33.6 kB)
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 ikelaiah · Apr 06, 2014 at 11:35 PM 0
Share

Have you checked that all the normals are facing outward from your 3D application?

avatar image Rodiaz89 · Apr 06, 2014 at 11:50 PM 0
Share

Yes all of the normals are facing the camera. I just checked just in case.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jashan · Jan 06, 2015 at 05:47 PM

You should probably use Sorting Layer and Order in Layer. That would be much easier than messing with Shaders and it gives you full control over rendering order.

These properties are documented in Sprite Render, and there's also a tutorial available: Tutorials / Topics / 2D / Sorting Layers

Comment
Add comment · Share
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
0

Answer by Rodiaz89 · Jan 06, 2015 at 06:02 PM

Yeah, those usually do the trick, problem is that the game has some 3d to it, so if I assign a sorting layer and another character is behind him if would look weird due to the sorting layer, it would show a piece of the arm of the player who is behind the other one. But thanks for the comment :)

Comment
Add comment · Show 2 · Share
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 jamesbdh · Oct 30, 2015 at 09:49 AM 0
Share

Did you manage to solve this problem in the end? I'm having almost exactly the same issue and not getting anywhere.

avatar image Rodiaz89 jamesbdh · Oct 30, 2015 at 12:41 PM 0
Share

Not really, I mean the camera in 2d mode helped a bit but in the end I had to adjust it to the best possible scenario but still get some errors by changing the scale, camera angle and distance between the planes. For the camera the more inclined it is the more error you will get. $$anonymous$$aybe there is a better solution but I gave up and left it still with some issues.

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

24 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

Related Questions

Rendering an entire object as transparent 1 Answer

Cutout Shader getting Opaque when turning alpha off and on again 1 Answer

Is there a shader in Unity that prevents transparent parts from overlapping? 1 Answer

Render texture above something else 1 Answer

Add Color Property to Unlit Alpha? 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