模型自动分离,自动染色


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using VRTK;

public class SZRVRHelper : MonoBehaviour
{
    #region GetAllTransform

    public List<Transform> child = new List<Transform>();
    static Bounds maxBound;

    /// <summary>
    /// 得到当前服务体全部子物体功能
    /// </summary>
    /// <param name="transPrant">要查询的父物体</param>
    public void FindAllTransform(Transform transPrant)
    {
        if (transPrant.childCount > 0)
        {
            for (int i = 0; i < transPrant.childCount; i++)
            {
                FindAllTransform(transPrant.GetChild(i));
            }
        }
        if (!child.Contains(transPrant))
        {
            child.Add(transPrant);
        }
        else
        {
            Debug.LogError(transPrant.name + "-名称重复!!");
        }
    }
    #endregion
    #region GetCenter

    /// <summary>
    /// 获取加载模型的中心点
    /// </summary>
    /// <param name="tans"></param>
    /// <returns></returns>
    public static Vector3 GetCenter(Transform tans)
    {
        maxBound = new Bounds();
        if (tans.childCount > 0 )
        {
            if (tans.GetChild(0).childCount > 0)
            {
                if (tans.GetChild(0).GetChild(0).GetComponent<Renderer>() != null)
                {
                    maxBound = tans.GetChild(0).GetChild(0).GetComponent<Renderer>().bounds;
                    for (int i = 1; i < tans.childCount; i++)
                    {
                        Transform child = tans.GetChild(i);
                        if (child.childCount > 0)
                        {
                            Renderer render = child.GetChild(0).GetComponent<Renderer>();
                            maxBound.Encapsulate(render.bounds);
                        }
                    }
                }

            }
           
        }
        
        return maxBound.center;
    }
    /// <summary>
    /// 获取自身中心点
    /// </summary>
    /// <param name="transform"></param>
    /// <returns></returns>
    public static Vector3 GetTransSelfCenter(Transform transform)
    {
        Vector3 center = transform.GetComponent<Renderer>().bounds.center;
        return center;
    }
    /// <summary>
    /// 关闭手柄射线
    /// </summary>
    /// <param name="handTrans"></param>
    /// <param name="isClose"></param>
    public virtual void CloseLineRender(Transform handTrans,bool isClose)
    {
        //handTrans.GetComponent<VRTK_StraightPointerRenderer>().enabled = isClose;
        //handTrans.Find("MainControllerTooltips").gameObject.SetActive(isClose);
        //handTrans.GetComponent<VRTK_UIPointer>().enabled = isClose;
    }
    #endregion
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using System.Text;

public class ModlesFunction : SZRVRHelper
{
    static Bounds maxBound;
    public bool iszdfl=false;
    public bool isrs=false;
    public static ModlesFunction InstadateModleFunction;
    //private static Dictionary<string, Color> getCurrLoadModelColor = new Dictionary<string, Color>();
    Vector3 center;
    private void Awake()
    {
        InstadateModleFunction = this;
    }
    // Use this for initialization
    void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
    /// <summary>
    /// 自动分离
    /// </summary>
    public void OpenZdfl(Transform transform = null)
    {
        if (transform == null)
        {
            transform = this.transform;
        }
        if (transform.childCount > 0)
        {
            center = GetCenter(transform);
            iszdfl = !iszdfl;
            if (iszdfl)
            {
                foreach (Transform item in transform)
                {
                    Vector3 childCenter = GetTransSelfCenter(item.GetChild(0));
                    Vector3 direction = childCenter - center;
                    direction = direction.normalized;
                    //位置、旋转清零
                    //item.GetChild(0).position = Vector3.zero;
                    //item.GetChild(0).rotation = Quaternion.identity;

                    if (item.childCount > 0)
                    {
                        item.GetChild(0).DOMove(item.GetChild(0).position + direction * 0.1f, 0.5f).SetEase(Ease.OutBack);
                    }
                }
            }
            else
            {
                foreach (Transform item in transform)
                {
                    item.GetChild(0).DOLocalMove(Vector3.zero, 0.5f);
                    item.GetChild(0).localRotation = Quaternion.identity;
                }

            }
        }
    }
    /// <summary>
    /// 染色
    /// </summary>
    public void OpenRs(Transform transform=null)
    {
        if (transform == null)
        {
            transform = this.transform;
        }
        if (transform.childCount > 0)
        {
            //center = GetCenter(sceneManger);
            isrs = !isrs;
            StringBuilder str = new StringBuilder();

            const string splitChar = ",";
            str.Append(isrs.ToString());
            if (isrs)
            {
                str.Append(splitChar);
                foreach (Transform item in transform)
                {
                    Color newColor = new Color(UnityEngine.Random.Range(0.0f, 1.0f),
                        UnityEngine.Random.Range(0.0f, 1.0f),
                        UnityEngine.Random.Range(0.0f, 1.0f));

                    item.GetChild(0).GetComponent<MeshRenderer>().material.color = newColor;
                    //
                    str.Append(item.name);
                    str.Append(splitChar);
                    str.Append(newColor.r.ToString());
                    str.Append(splitChar);
                    str.Append(newColor.g.ToString());
                    str.Append(splitChar);
                    str.Append(newColor.b.ToString());
                    str.Append(splitChar);
                }
                str.Remove(str.Length - 1, 1);
            }
            else
            {
                foreach (Transform item in transform)
                {
                    item.GetChild(0).GetComponent<MeshRenderer>().material.color =ModlesLode.getCurrLoadModelColor[item.name];
                }
            }
        }
    }

  

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值