UNITY-change ONLY a specific part of the color of the 3D model

I am really new to 3D unity, and I would like to ask a question. I have a three-dimensional human model (the default unity model), which has a hierarchical bone structure.

What I want to achieve here, when I press a specific trigger, I want to color one of her limbs with a different color (only one of his limbs). This is an illustration of what I want to achieve.

enter image description here

I really don't know about this because I just started learning Unity about 3 months ago, so I really need your help, this is a feature of my visualization tool, if it helps

enter image description here

+4
source share
2

, , , . , , . , . , . / . .

, = > = > .

, , - . , , , . , , . 2 :

1) , . , .

2) - , . , Mesh.boneWeights. , , > 1 . ? SkinnedMeshRenderer.bones. , , , .

script, . SkinnedMeshRenderer ( ):

using UnityEngine;
using System.Collections;

public class BoneHiglighter : MonoBehaviour {

    public Color32 highlightColor = Color.red;
    public Color32 regularColor = Color.white;

    public SkinnedMeshRenderer smr;

    // Just for sake of demonstration
    public Transform bone;
    private Transform prevBone;


    // Find bone index given bone transform
    int GetBoneIndex(Transform bone) {
        Debug.Assert(smr != null);
        var bones = smr.bones;

        for (int i = 0; i < bones.Length; ++i) {
            if (bones[i] == bone) return i;
        }

        return -1;
    }

    // Change vertex colors highlighting given bone
    void Highlight(Transform bone) {
        Debug.Assert(smr != null);
        var idx = GetBoneIndex(bone);
        var mesh = smr.sharedMesh;
        var weights = mesh.boneWeights;
        var colors = new Color32[weights.Length];

        for (int i = 0; i < colors.Length; ++i) {
            float sum = 0;
            if (weights[i].boneIndex0 == idx && weights[i].weight0 > 0)
                sum += weights[i].weight0;
            if (weights[i].boneIndex1 == idx && weights[i].weight1 > 0)
                sum += weights[i].weight1;
            if (weights[i].boneIndex2 == idx && weights[i].weight2 > 0)
                sum += weights[i].weight2;
            if (weights[i].boneIndex3 == idx && weights[i].weight3 > 0)
                sum += weights[i].weight3;

            colors[i] = Color32.Lerp(regularColor, highlightColor, sum);
        }

        mesh.colors32 = colors;

    }

    void Start() {
        // If not explicitly specified SkinnedMeshRenderer try to find one
        if (smr == null) smr = GetComponent<SkinnedMeshRenderer>();
        // SkinnedMeshRenderer has only shared mesh. We should not modify it.
        // So we make a copy on startup, and work with it.
        smr.sharedMesh = (Mesh)Instantiate(smr.sharedMesh);

        Highlight(bone);
    }

    void Update() {
        if (prevBone != bone) {
            // User selected different bone
            prevBone = bone;
            Highlight(bone);
        }
    }
}

. : https://www.dropbox.com/s/yfoqo44bubcr48s/HighlightBone.zip?dl=0

: https://dl.dropboxusercontent.com/u/16950335/bones/index.html - , .

+4

, . - , , :

  • 1 (3d ).
  • 1 (Skinned mesh renderer 1 )
  • RGBA, . : 2D- 3D- UV-. UV- - , 3D- 2D- . 3D Vertex, 2D - UV. , UV 2D- ( , gimp ..), . Google UV mapping . : UV- Unity ( script), 3D-.
  • , , . , , . , , script. Texture2D.GetPixels() SetPixels().

, , , , :

  • 3D-.
  • , ,
  • Unity
  • , ( , ).
  • RGBA .
  • .
  • , , .
0

Source: https://habr.com/ru/post/1621684/


All Articles