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 DanaScully · Jul 04, 2017 at 08:59 AM · meshrendertrianglescolorsvertexcolor

triangles colors render on top of each other

I am using a script to color my mesh triangles. The works but I get incorrect rendering. Any idea what is causing the problem?

 void Start () {
         Mesh mesh = gameObject.GetComponent<MeshFilter>().sharedMesh;
         int[] triangles = mesh.triangles;
         Vector3[] vertices = mesh.vertices;
         Vector2[] UVs = mesh.uv;
 
         Color[] colors = new Color[triangles.Length];
 
         Vector3[] modifiedVerts = new Vector3[triangles.Length];
         Vector2[] newUVs = new Vector2[triangles.Length];
         int[] modifiedtris = new int[triangles.Length];
 
         Color c = new Color();
         for(int i = 0; i < triangles.Length; i++)
         {
             modifiedVerts[i] = vertices[triangles[i]];
             modifiedtris[i] = i;
             newUVs[i] = UVs[triangles[i]];
 
             int index = triangles[i];
             if (i % 3 == 0)
             {
                 c = new Color(Random.Range(0.2f, 0.7f),
                         Random.Range(0.2f, 0.7f),
                         Random.Range(0.2f, 0.7f),
                         1.0f);
 
               
             }
             colors[index] = c;
         }
 
         mesh.vertices = modifiedVerts;
         mesh.triangles = modifiedtris;
         mesh.uv = newUVs;
 
         mesh.RecalculateNormals();![alt text][1]
         mesh.RecalculateBounds();
         mesh.RecalculateTangents();
 
         mesh.colors = colors;
 
     }

 


[1]: /storage/temp/97090-pervertex.png

pervertex.png (73.9 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Jul 04, 2017 at 09:49 AM

You most likely use a shader that doesn't write to the depth buffer and / or doesn't perform a depth test.

The triangles in one mesh are rendered in the order they are defined in the triangles array. It looks like one of the top triangles is rendered first (the area that is approximately covered by the red and brown triangle). This however is completely overwritten. Next the red front face is rendered which overwrites the top color in the back. Next the top lower half is rendered (green), that is again overwritten by brown, blue and magenta as they are rendered later.

If depth write and depth testing is used by the shader a triangle which is rendered further away can't overwrite an already rendered triangle that is closer due to the depth test.

It also seems your shader does not use any culling (so front and back side of each triangle are rendered). It would help to know what kind of cube / mesh you want to render? Should that cube be opaque or transparent?

Most transparent shaders do not write to the depth buffer but do perform a depth test. Unity always renders opaque geometry first. Transparent objects are sorted from far to near. However self-overlapping triangles within a single mesh are not reordered. So a transparent mesh should never have triangles that self-overlap. This is the typical example which can't be solved by sorting, only by splitting at least one triangle into several smaller triangles: example

For more information on that problem, see this article.

edit

Here's a simple shader that combines a texture (if provided) with the vertex colors of the mesh:

 //filename: UnlitVertexColor.shader
 Shader "Unlit/UnlitVertexColor"
 {
     Properties
     {
         _MainTex ("Texture", 2D) = "white" {}
     }
     SubShader
     {
         Tags { "RenderType"="Opaque" }
         LOD 100
 
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             
             #include "UnityCG.cginc"
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
                 float4 color : COLOR0;
             };
 
             struct v2f
             {
                 float2 uv : TEXCOORD0;
                 float4 vertex : SV_POSITION;
                 float4 color : COLOR0;
             };
 
             sampler2D _MainTex;
             float4 _MainTex_ST;
             
             v2f vert (appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                 o.color = v.color;
                 return o;
             }
             
             fixed4 frag (v2f i) : SV_Target
             {
                 fixed4 col = tex2D(_MainTex, i.uv);
                 return i.color + col;
             }
             ENDCG
         }
     }
 }

If you don't want to use any texture at all you can simplify even more.

Here's the same thing without any texture, just plain unlit vertex colors:

 Shader "Unlit/UnlitVertexColor"
 {
     SubShader
     {
         Tags { "RenderType"="Opaque" }
         LOD 100
 
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             
             #include "UnityCG.cginc"
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float4 color : COLOR0;
             };
 
             struct v2f
             {
                 float4 vertex : SV_POSITION;
                 float4 color : COLOR0;
             };
 
             v2f vert (appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.color = v.color;
                 return o;
             }
             
             fixed4 frag (v2f i) : SV_Target
             {
                 return i.color;
             }
             ENDCG
         }
     }
 }

Alternatively if you don't want to do any fancy things in the shader you can use an old fix-function shader without any CG code. This should do the same as the last example:

 Shader "Unlit/UnlitVertexColor"
 {
     SubShader
     {
         Tags { "RenderType"="Opaque" }
         Pass
         {
             Cull Back
             ZTest LEqual
             ZWrite On
             Lighting Off
             BindChannels
             {
                 Bind "Vertex", vertex
                 Bind "texcoord", texcoord
                 Bind "Color", color
             }
         }
     }
 }

Comment
Add comment · Show 4 · 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 DanaScully · Jul 04, 2017 at 09:55 AM 0
Share

I don't want to have transparency at all. I have created a new material and assigned it's shader to Particles/Alpha Blended Premultiply. Any other shader is not displaying the face coloring or vertex coloring.

avatar image Bunny83 DanaScully · Jul 04, 2017 at 10:33 AM 1
Share

Particle shaders are all transparent shaders and "alpha blending" is transparency -.-

Almost no shader actually use vertex colors since almost any coloring / shading is usually done with textures. You can simply create your own Unlit vertex color shader. I'll edit my answer.

avatar image DanaScully Bunny83 · Jul 04, 2017 at 11:10 AM 0
Share

@Bunny83 Thanks for the guide. It solved the problem. In general when would recommend writing shaders of our own in Unity? I am familiar with shaders in OpenGL but I get confused when I have to use them in Unity.

Show more comments

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

72 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

Related Questions

Object odd light problem with mesh? 1 Answer

random colors for each triangle face 1 Answer

contains two different ids for the same vertex 0 Answers

How to find the center of quad polygons in a mesh 1 Answer

Problem drawing a mesh with Graphics.DrawMeshNow 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