• 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 Predominant · Jan 12, 2014 at 02:23 AM · colorverticesvertex shader

Vertex colour based on distance from transform center

Hey, I'm trying to set colours for a mesh, based on the distance each vertex is from the center of the transform bounds.

I have a VertexColors Component that I'm using for it, and the result I am getting. It seems to be calculating it wrong. Highest points should be grey, others should be green. Any help is appreciated.

Incorrect vertex colouring

 using UnityEngine;
 using System.Collections;
 
 public class VertexColours : MonoBehaviour
 {
     public Color GrassColour = new Color(68f, 174f, 117f);
     public Color WaterColour = new Color(25f, 59f, 141f);
     public Color DirtColour = new Color(94f, 81f, 8f);
     public Color RockColour = new Color(165f, 165f, 165f);
     public Color SnowColor = new Color(255f, 255f, 255f);
 
     public void UpdateColors()
     {
         Mesh mesh = this.GetComponent<MeshFilter>().mesh;
         Color[] colors = new Color[mesh.vertices.Length];
         float[] distances = new float[mesh.vertices.Length];
 
         for (int i = 0; i < mesh.vertices.Length; i++)
         {
             distances[i] = Mathf.Abs(Vector3.Distance(this.transform.renderer.bounds.center, mesh.vertices[i]));
         }
 
         float max = Mathf.Max(distances);
         float min = Mathf.Min(distances);
 
         for (int i = 0; i < mesh.vertices.Length; i++)
         {
             colors[i] = this.VertexColor(distances[i], (max + min) / 2f);
         }
 
         mesh.colors = colors;
     }
 
     protected Color VertexColor(float distance, float mid)
     {
         if (distance > mid)
         {
             return this.RockColour;
         }
         return this.GrassColour;
     }
 }
 


screenshot.454.png (45.3 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

2 Replies

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

Answer by ncallaway · Jan 12, 2014 at 03:12 AM

I suspect the problem only occurs if the object is not positioned at Vector3.zero.

The distance comparison on line 20 below compares mesh.vertices[i] which is in local space, to renderer.bounds.center which is in global space.

 distances[i] = Mathf.Abs(Vector3.Distance(this.transform.renderer.bounds.center, mesh.vertices[i]));

If you change that line to the following, it should work.

 distances[i] = Vector3.Distance(this.renderer.bounds.center - transform.position, mesh.vertices[i]);

The major change that I've made is that we subtract transform.position from renderer.bounds.center to convert this from world space to local space. That way the distance comparison is between vectors in the same (local) space.

(There are also some other minor differences between the two lines that don't actually make an impact. Mathf.Abs() is unnecessary, as Vector3.Distance() will always be positive (or 0). You also have access to this.renderer directly in a MonoBehavior, and you don't need to go through this.transform in order to get the renderer).

Comment
Add comment · Show 3 · 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 Predominant · Jan 12, 2014 at 03:51 AM 0
Share

Thanks @ncallaway - For this test, I've positioned the object at Vector3.zero world position. But I've also adjusted the code to compensate for local/world space positioning.. Unfortunately this has no effect. The result is very much the same. New image attached:

Incorrect vertex colours

screenshot.456.png (61.5 kB)
avatar image ncallaway · Jan 12, 2014 at 04:46 AM 0
Share

That's interesting. I'm running that code in a unity project and it seems to be working with a capsule. (I'm using a lerp rather than a binary setting on the capsule, but that should be the only difference).

alt text

Any chance you can share your geometry or project so I can see if I get the same errors?

capsule-with-vertex-colors.png (26.6 kB)
avatar image Predominant · Jan 12, 2014 at 08:02 AM 0
Share

What shader are you using?

avatar image
0

Answer by Predominant · Jan 12, 2014 at 08:35 AM

Solved. Thanks! Your answer was correct.

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

How to convert from texture to vertices' colors 0 Answers

Is it possible to create a colour gradient across an entire mesh if only a few vertices have colour? 1 Answer

Vertex Shader problem with _WorldSpaceCameraPos 1 Answer

Color not appearing on created mesh? (only grey) 2 Answers

Changing the color of individual particles 0 Answers


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