Recognize colors on model


I’m trying to create a method to recognize colors on a 3D model to place particles on specific points. For example, placing glittering particles on the yellow parts of an amour.

I managed to recognize colors on a texture with “.GetPixel32()” and comparing hue, brightness and saturation, but I want to know where that part of the texture is placed on the model (at hands, on both eyes, in his cannibal mutant guinea pig, …).


After some digging, I finally found the solution:

    public void RecognizeColors(GameObject colorModel) {
        		Texture2D tex = colorModel.GetComponent<Renderer>().material.mainTexture as Texture2D;
        		Mesh colorModelMesh = colorModel.GetComponent<MeshFilter>().mesh;
        		// Loop through vertices 
        		for (int i = 0; i < colorModelMesh.vertices.Length; i++) {
        			Vector3 vertice = colorModelMesh.vertices*;*

_ Vector2 pixelInTexture = colorModelMesh.uv*; // or .uv2, if you need the secondary texture*_
_ pixelInTexture.x *= tex.width;
pixelInTexture.y *= tex.height;_

* Color32 colorInTexture = tex.GetPixel((int)pixelInTexture.x, (int)pixelInTexture.y);*

// Now you can compare color and do whatever you wanted

if (CompareColors(colorInTexture, desiredColor)) {
ParticleSystem ps = Instantiate(particleToPlace) as ParticleSystem;
ps.transform.SetParent(colorModel.transform, true);
ps.transform.localPosition = vertice;