UNITY变化的三维模型的颜色只有某些部分 [英] UNITY-Changing ONLY certain part of 3D model's color

查看:983
本文介绍了UNITY变化的三维模型的颜色只有某些部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很新的与unity3D,我想问一个问题
我有3D人体模型(默认的统一模型),它具有层次骨结构。

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

我真的很无能这一点,因为我刚开始学习团结约3个月前,所以我真的需要你的帮助,这是我渲染的财产,如果它的帮助

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

推荐答案

我只是想补充一点,有可能会实现你想要使用什么更简单的方法的投影机的。这是统一用于绘制实时的网面的各种效果,如弹孔的常用工具。利用同样的原理可以突出网格的一些区域。你可以把投影机,手电筒,这一切的光照射到改变其质感。有标准下的资产/影响一些示例投影机。你可能想从那里开始。

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.

要创建projectror,创建一个空的游戏对象=>添加组件=>投影机。

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

修改

您可能会想尝试另一种想法是使用的顶点颜色的。的网格的每个顶点包含,除了坐标,颜色参数,这是经由着色器访问。所以,你可以更改特定组顶点的颜色,以突出它们。这里有2点你必须要注意:

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)大多数着色器选择忽略顶点颜色,与精灵着色器的例外。您将需要一个自定义着色器,如这个

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

2)你需要以某种方式知道哪个顶点正是你想要突出。你可以做的是要遍历 Mesh.boneWeights 。如果你想选择一个胳膊,例如,你需要的是有重量> 1臂骨指数的所有顶点。你怎么找到的臂骨指数?该指数对应于 SkinnedMeshRenderer.bones 索引。还是只挑选一些顶点,你肯定知道是属于手臂和看到它的骨骼权重来查找索引。

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.

下面的示例脚本根据选定骨骼指数改变顶点颜色。它连接到您的SkinnedMeshRenderer(通常是第二个层次结构对象):

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>();
        // 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);
        }
    }
}



查看示例项目:< A HREF =https://www.dropbox.com/s/yfoqo44bubcr48s/HighlightBone.zip?dl=0相对=nofollow> https://www.dropbox.com/s/yfoqo44bubcr48s/HighlightBone.zip? DL = 0

演示:的 https://dl.dropboxusercontent.com/u/16950335/bones/index.html - 点击身体部位看他们突出

Demo: https://dl.dropboxusercontent.com/u/16950335/bones/index.html - click on body parts to see them highlight.

这篇关于UNITY变化的三维模型的颜色只有某些部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆