Search Unity

Moving vertices of a mesh by vertexcolor

Discussion in 'Scripting' started by viktorkadza, Jul 19, 2019.

  1. viktorkadza

    viktorkadza

    Joined:
    Sep 4, 2018
    Posts:
    9
    Hello i am new in Unity Scripting
    , i need help in a basic script to move some vertices of a imported mesh depending on their vertex color. For example moving the vertices which have rgb(0,0,0) and rgb(1,0,0) color in Y direction by 1cm. I started with the basic script from the documentation, but this code moves all vertices left. I tryed putting an if statment but it doesn works. Thanks!
    Code (CSharp):
    1.  Mesh mesh;
    2.     Vector3[] vertices;
    3.     Color[] colors;
    4.     void Start()
    5.     {
    6.         mesh = GetComponent<MeshFilter>().mesh;
    7.         vertices = mesh.vertices;
    8.         colors = mesh.colors;
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.         for (var i = 0; i < vertices.Length; i++)
    14.         {
    15.             if (colors[i] = (0,0,0,0))
    16.             {
    17.             vertices[i] += Vector3.right * Time.deltaTime;
    18.             }
    19.          
    20.        
    21.         }
    22.  
    23.         // assign the local vertices array into the vertices array of the Mesh.
    24.         mesh.vertices = vertices;
    25.         mesh.RecalculateBounds();
    26.     }
     

    Attached Files:

    Last edited: Jul 19, 2019
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,104
    It seems to me all vertices colors in your mesh equals (0,0,0,0)
     
  3. viktorkadza

    viktorkadza

    Joined:
    Sep 4, 2018
    Posts:
    9
    No , the are not. The are (0,0,0,0) , (1,0,0,0) ,(2,0,0,0).... and so on.
     
  4. viktorkadza

    viktorkadza

    Joined:
    Sep 4, 2018
    Posts:
    9
    ok ifoun the solution
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TransformAjtolap : MonoBehaviour
    6. {
    7.  
    8.    
    9.     Mesh mesh;
    10.     Vector3[] vertices;
    11.     Color32[] colors;
    12.     void Start()
    13.     {
    14.         mesh = GetComponent<MeshFilter>().mesh;
    15.         vertices = mesh.vertices;
    16.         colors = mesh.colors32;
    17.        
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         for (var i = 0; i < vertices.Length; i++)
    23.         {
    24.             if (colors[i].r == 0)
    25.             {
    26.             vertices[i] += Vector3.left * Time.deltaTime;
    27.                
    28.             }
    29.            
    30.        
    31.         }
    32.  
    33.         // assign the local vertices array into the vertices array of the Mesh.
    34.         mesh.vertices = vertices;
    35.         mesh.RecalculateBounds();
    36.     }
    37. }
     
  5. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,104
    Lol I missed a typo in your code and that was the error. Replace
    Code (CSharp):
    1. if (colors[i] = (0,0,0,0))
    with
    Code (CSharp):
    1. if (colors[i] == (0,0,0,0))
    and it will work too.

    Single = is assignment operator, not comparsion
    Double == is comparsion