U3D技术采集Unity基础

可弯曲绳子动态生成

2017-03-16  本文已影响16人  游戏开发小Y

RealRobPro2.cs

using UnityEngine;
using System.Collections;
using System.Text;
using System.IO;
using System;
using System.Runtime.InteropServices;
using System.Collections.Specialized;
using System.Linq;
using System.Collections.Generic;

public class RealRobPro2 : MonoBehaviour
{
    public int vCt = 6;
    protected Mesh ms;
    public int ptCt;
    protected Vector3[] newVertices;
    protected Vector2[] newUV;
    protected int[] newTriangles;
    public float thicness = 0.02f;
    public int startCenterCt;
    public int endCenterCt;
    public Material mat;
    public bool isUpdateMesh = true;
    public float tilingScale = 50.0f;
    public bool isDeNormal = false;

    public void createMesh()
    {
        ptCt = 0;
        ms = new Mesh();
        foreach (Transform t in transform)
        {
            if (t.gameObject.name.StartsWith("Joint"))
            {
                ++ptCt;
            }
        }
        if (ptCt == 0)
        {
            return;
        }
        newVertices = new Vector3[ptCt * vCt + 2];
        newTriangles = new int[(ptCt - 1) * (vCt - 1) * 6 + (vCt - 1) * 6];
        newUV = new Vector2[ptCt * vCt + 2];

        startCenterCt = ptCt * vCt;
        endCenterCt = ptCt * vCt + 1;

        int k = 0;
        for (int i = 0; i < ptCt - 1; ++i)
        {
            for (int j = 0; j < vCt - 1; ++j)
            {
                int z = j + 1;
                //if (z == vCt)
                //{
                //    z = 0;
                //}
                if (!isDeNormal)
                {
                    newTriangles[k + 0] = i * vCt + j;

                    newTriangles[k + 1] = (i + 1) * vCt + j;

                    newTriangles[k + 2] = i * vCt + z;

                    newTriangles[k + 3] = i * vCt + z;

                    newTriangles[k + 4] = (i + 1) * vCt + j;

                    newTriangles[k + 5] = (i + 1) * vCt + z;
                }
                else
                {
                    newTriangles[k + 2] = i * vCt + j;

                    newTriangles[k + 1] = (i + 1) * vCt + j;

                    newTriangles[k + 0] = i * vCt + z;

                    newTriangles[k + 5] = i * vCt + z;

                    newTriangles[k + 4] = (i + 1) * vCt + j;

                    newTriangles[k + 3] = (i + 1) * vCt + z;
                }
                k += 6;
            }
        }
        for (int i = 0; i < vCt - 1; ++i)
        {
            newTriangles[k + 0] = i;
            newTriangles[k + 1] = startCenterCt;
            int z = i + 1;
            //if (z == vCt)
            //{
            //    z = 0;
            //}
            newTriangles[k + 2] = z;
            k += 3;
        }
        for (int i = 0; i < vCt - 1; ++i)
        {
            newTriangles[k + 0] = ptCt * vCt - 1 - i;
            newTriangles[k + 1] = endCenterCt;
            int z = ptCt * vCt - 1 - i - 1;
            //if (i + 1 == vCt)
            //{
            //    z = ptCt * vCt - 1;
            //}
            newTriangles[k + 2] = z;
            k += 3;
        }
        for (int i = 0; i < ptCt; ++i)
        {
            for (int j = 0; j < vCt; ++j)
            {
                newUV[i * vCt + j] = new Vector2(1.0f / (vCt + 1) * j, 1.0f / ptCt * i);
            }
        }
        newUV[startCenterCt] = Vector2.zero;
        newUV[endCenterCt] = Vector2.zero;

        MeshFilter mfs = GetComponent<MeshFilter>() as MeshFilter;
        if (mfs == null)
        {
            mfs = gameObject.AddComponent<MeshFilter>();
        }
        mfs.mesh = ms;
        ms.vertices = newVertices;
        ms.uv = newUV;
        ms.triangles = newTriangles;
        MeshRenderer mr = GetComponent<MeshRenderer>() as MeshRenderer;
        if (mr == null)
        {
            mr = gameObject.AddComponent<MeshRenderer>();
            if (mat != null)
            {
                mr.material = mat;
            }
            else
            {
                mr.material = Resources.Load("Common/Material/ropeMat") as Material;
            }
            GetComponent<Renderer>().material.SetTextureScale("_MainTex", new Vector2(1.0f, getLen() * tilingScale));
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (isUpdateMesh)
        {
            UpdateMesh();
        }
    }

    public bool isMeshCollider = false;
    void UpdateMesh()
    {
        MeshFilter mf = GetComponent<MeshFilter>();
        if (mf == null)
        {
            createMesh();
        }
        Mesh mesh = GetComponent<MeshFilter>().mesh;
        Vector3[] vertices = mesh.vertices;

        GameObject posObj = new GameObject("tempForRob");

        for (int i = 0; i < ptCt; ++i)
        {
            Transform curJoint = transform.GetChild(i);
            posObj.transform.parent = curJoint;
            if (i == 0)
            {
                vertices[startCenterCt] = curJoint.position - transform.position;
            }
            if (i == ptCt - 1)
            {
                vertices[endCenterCt] = curJoint.position - transform.position;
                for (int j = 0; j < vCt; ++j)
                {
                    //posObj.transform.localPosition = new Vector3(0.0f,
                    //                                             thicness * Mathf.Cos(Mathf.PI * 2.0f * j / vCt),
                    //                                             thicness * Mathf.Sin(Mathf.PI * j * 2.0f / vCt));
                    posObj.transform.localPosition = new Vector3(thicness * Mathf.Cos(Mathf.PI * 2.0f * j / (vCt - 1)),
                                                                    thicness * Mathf.Sin(Mathf.PI * j * 2.0f / (vCt - 1)),
                                                                    0.0f);
                    vertices[i * vCt + j] = posObj.transform.position - transform.position;
                }
            }
            else
            {
                for (int j = 0; j < vCt; ++j)
                {
                    posObj.transform.localPosition = new Vector3(thicness * Mathf.Cos(Mathf.PI * 2.0f * j / (vCt - 1)),
                                                                    thicness * Mathf.Sin(Mathf.PI * j * 2.0f / (vCt - 1)),
                                                                    0.0f);
                    vertices[i * vCt + j] = posObj.transform.position - transform.position;
                }
            }
        }
        for (int i = 0; i < ptCt; ++i)
        {
            for (int j = 0; j < vCt + 1; ++j)
            {
                newUV[i * vCt + j] = new Vector2(1.0f / (vCt + 1) * j, 1.0f / ptCt * i);
            }
        }
        GameObject.Destroy(posObj);
        mesh.vertices = vertices;
        mesh.RecalculateNormals();
        if (isMeshCollider)
        {
            MeshCollider mc = ProcBasic.AddIndependentComponent<MeshCollider>(gameObject, true);
            //mc.mesh = mesh;
            isMeshCollider = false;
        }
        mesh.RecalculateBounds();
        if (GetComponent<Renderer>() != null && GetComponent<Renderer>().material != null)
        {
            GetComponent<Renderer>().material.SetTextureScale("_MainTex", new Vector2(1.0f, getLen() * tilingScale));
        }
       
    }

    float getLen()
    {
        float len = 0.0f;
        for (int i = 0; i < ptCt - 1; ++i)
        {
            len += Vector3.Distance(transform.GetChild(i).position, transform.GetChild(i + 1).position);
        }
        return len;
    }
}

上一篇下一篇

猜你喜欢

热点阅读