unity3d 画弧面mesh,抛物线mesh
废话不多,直接上图,有图有真相
unity3d 画弧面mesh,抛物线mesh
直接上代码

using UnityEngine;
using System.Collections;

public class CreateArcSurface : MonoBehaviour
{
    [Header("【开始点位置】")]
    public Transform begin;
    [Header("【结束点位置】")]
    public Transform end;
    [Header("【弧形圆滑度】")]
    public int planesSeg = 64;
    private float planeLength = 0;
    [Header("【弧形宽度】")]
    public float planeWidth = 1;
    [Header("【弧度】")]
    public float planethreshold = 0.3f;
    MeshFilter meshFilter;
    void Start()
    {
       meshFilter = gameObject.GetComponent<MeshFilter>();
       meshFilter.mesh = init(planethreshold);
    }
    void Update()
    {
        float z = Mathf.Lerp(begin.position.z , end.position.z, 0.5f);
        Vector3 v3 = new Vector3(0, 0, z);

        gameObject.transform.position = v3;
        planeLength = Mathf.Abs((begin.position.z - end.position.z));
        meshFilter.mesh = init(planethreshold);
    }
    public Mesh init(float threshold)
    {
        Mesh mesh = new Mesh();
        int segments_count = planesSeg;
        int vertex_count = (segments_count + 1) * 2;
        Vector3[] vertices = new Vector3[vertex_count];
        int vi = 0;
        float widthSetup = planeLength * 1.0f / segments_count;
        float r = planeLength * 1.0f / (Mathf.Sin(threshold / 2) * 2);
        float angleSetup = threshold / planesSeg;
        float coangle = (Mathf.PI - threshold) / 2;
        float h = r - (r * Mathf.Cos(threshold / 2));
        float diff = r - h;
        for (int si = 0; si <= segments_count; si++)
        {
            float x = 0;
            float z = 0;
            if (threshold == 0)
            {
                x = widthSetup * si;
                vertices[vi++] = new Vector3(-planeLength / 2 + x, planeWidth / 2, z);
                vertices[vi++] = new Vector3(-planeLength / 2 + x, -planeWidth / 2, z);
            }
            else
            {
                x = r * Mathf.Cos(coangle + angleSetup * si);
                z = r * Mathf.Sin(coangle + angleSetup * si);
                float position_x = Mathf.Lerp(begin.position.x, end.position.x, (float)si / (float)segments_count);
                Vector3 v1 = new Vector3(planeWidth / 2 + position_x, z - diff, -x);
                Vector3 v2 = new Vector3(-planeWidth / 2 + position_x, z - diff, -x);
                vertices[vi++] = v1;
                vertices[vi++] = v2;
                //GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                //go.name = si.ToString();
                //go.transform.position = new Vector3(Mathf.Lerp(begin.position.x, end.position.x, (float)si / (float)segments_count), -x, z - diff);
                //go.transform.localScale = v1;
                //GameObject go1 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                //go1.name = si.ToString();
                //go1.transform.position = v2;
                //go1.transform.localScale = Vector3.one * 0.5f;
            }
        }
        int indices_count = segments_count * 3 * 2;
        int[] indices = new int[indices_count];
        int vert = 0; 
        int idx = 0;
        for (int si = 0; si < segments_count; si++)
        {
            indices[idx++] = vert + 1;
            indices[idx++] = vert;
            indices[idx++] = vert + 3;
            indices[idx++] = vert;
            indices[idx++] = vert + 2;
            indices[idx++] = vert + 3;
            vert += 2;
        }

        mesh.vertices = vertices;
        mesh.triangles = indices;
        Vector2[] uv = new Vector2[vertices.Length];
        float uvSetup = 1.0f / segments_count;
        int iduv = 0;
        for (int i = 0; i < uv.Length; i = i + 2)
        {
            uv[i] = new Vector2(uvSetup * iduv, 1);
            uv[i + 1] = new Vector2(uvSetup * iduv, 0);
            iduv++;
        }
        mesh.uv = uv;
        //mesh.RecalculateBounds();
        mesh.RecalculateNormals();
        return mesh;
    }
}

相关文章:

  • 2022-01-25
  • 2022-12-23
  • 2022-12-23
  • 2021-11-09
  • 2021-12-14
  • 2021-12-07
  • 2021-07-06
  • 2022-12-23
猜你喜欢
  • 2021-09-18
  • 2021-11-15
  • 2021-08-12
  • 2021-10-11
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案