unity3d改变模型的颜色,UNITY变化的三维模型的颜色只有某些部分

I'm really new with unity3D and I would like to ask a question

I have a 3D human model ( a default unity model) which has a hierarchical bone structure.

What I want to achieve here is, when I press certain trigger, I want to color one of its limb with different color (just one of its limb). This is the illustration of what I want to achieve

0KFnk.jpg

I'm really clueless about this, because I've just started learning Unity for about 3 months ago, so I really need your help, this is the property of my renderer if it's helping

GUebC.png

解决方案

I just wanted to add that there may be an easier way to achieve what you want using projectors. This is the common tool in unity used to draw various effects on mesh surface in real time, such as bullet holes. Using the same principle you can highlight some area of the mesh. You can think of a projector as flashlight, that everything its light hits changes its texture. There are some example projectors under Standard Assets / Effects. You might want to start there.

To create a projectror, create an empty game object => Add Component => Projector.

EDIT

Another idea you may want to try is to use vertex colors. Each vertex of a mesh contains, in addition to coordinates, a color parameter, which is accessible via shader. So, you can change the color of specific set of vertices in order to highlight them. There are 2 points here you have to pay attention to:

1) Most shaders choose to ignore vertex color, with exception of sprites shader. You will need a custom shader, such as this one.

2) You need to know somehow which vertices exactly you want to highlight. What you can do is to iterate over Mesh.boneWeights. If you want to select an arm, for example, what you need is all vertices that have weight > 1 for arm bone index. How do you find the arm bone index? This index corresponds to the index in SkinnedMeshRenderer.bones. Or just pick some vertex you know for sure belongs to arm and see its bone weights to find the index.

Here a sample script that changes vertex colors based on selected bone index. Attach it to your SkinnedMeshRenderer (usually a second level hierarchy object):

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 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);

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值