美文网首页unityUnity
unity3d 画实线圆,虚线圆,五边形,正方形,三角形,圆盘

unity3d 画实线圆,虚线圆,五边形,正方形,三角形,圆盘

作者: 思玉1987 | 来源:发表于2018-05-25 11:05 被阅读19次

首先创建一个Quad 然后把脚本绑定在Quad里面, 你懂的,分别修改脚本里的属性RingType分别绘制不用的图形
有图有真相


z1.png z2.png z3.png z4.png z5.png z6.png

上代码

using UnityEngine;
using System.Collections;

public class AnnulusQuad : MonoBehaviour
{
    public enum RingType
    {
        FullLine,//实线
        DottedLine,//虚线
        Pentagon,//五边形
        Square,//正方形
        Triangle,//三角形
        Disk,//圆盘
    }
    //圈的步长
    public int depth = 4;
    //圈的半径,最大是1
    public float radius = 1;
    //内圈半径
    private float radius1;
    public float width = 2.5f;

    public RingType ringType = RingType.DottedLine;
    private MeshFilter _meshFilter;

    Mesh mesh;
    void Awake()
    {
        _meshFilter = GetComponent<MeshFilter>();
        MeshRenderer meshRenderer = gameObject.GetComponent<MeshRenderer>();
        Rebuild();
    }
    private void Rebuild()
    {
        if (mesh == null)
            mesh = new Mesh();
        switch (ringType)
        {
            case RingType.DottedLine:
                depth = 4;
                radius = -3;
                width = 0.2f;
                RebuildRingDataDotted();
                break;
            case RingType.Disk:
                depth = 4;
                radius = 1;
                width = 2.5f;
                RebuildRingDataFull();
                break;
            case RingType.FullLine:
                depth = 4;
                radius = -3;
                width = 0.2f;
                RebuildRingDataFull();
                break;
            case RingType.Pentagon:
                depth = 72;
                radius = -3;
                width = 0.2f;
                RebuildRingDataFull();
                break;
            case RingType.Square:
                depth = 90;
                radius = -3;
                width = 0.2f;
                RebuildRingDataFull();
                break;
            case RingType.Triangle:
                depth = 120;
                radius = -3;
                width = 0.2f;
                RebuildRingDataFull();
                break;
        }
        _meshFilter.sharedMesh = mesh;

    }
    private float Deg2Rad;

    private Vector3[] vertices;
    private int[] indexes;
    private Vector2[] uvs;
    private void RebuildRingDataDotted()
    {
        mesh.Clear();
        Deg2Rad = Mathf.Deg2Rad;
        radius1 = radius - width;
        int count = 360 / depth;
        int totalTringleCount = count;
        int totalVertexCount = totalTringleCount * 3;
        int totalIndexCount = totalVertexCount;
        if (vertices == null || vertices.Length < totalVertexCount)
            vertices = new Vector3[totalVertexCount];
        if (indexes == null || indexes.Length < totalIndexCount)
            indexes = new int[totalIndexCount];
        if (uvs == null || uvs.Length < totalVertexCount)
            uvs = new Vector2[totalVertexCount];
        int vertexIndex = 0;
        for (int i = 0; i < count; i += 2)
        {
            Vector3 v0 = GetPos(radius, i * depth);
            Vector3 v1 = GetPos(radius1, i * depth);
            Vector3 v2 = GetPos(radius, (i + 1) * depth);
            Vector3 v3 = GetPos(radius1, (i + 1) * depth);
            vertices[vertexIndex++] = v0;
            vertices[vertexIndex++] = v1;
            vertices[vertexIndex++] = v2;
            vertices[vertexIndex++] = v2;
            vertices[vertexIndex++] = v1;
            vertices[vertexIndex++] = v3;
        }
        for (int i = 0; i < totalIndexCount; i++)
        {
            indexes[i] = i;
            uvs[i] = Vector2.zero;
        }
        if (vertices.Length > totalVertexCount)
        {
            for (int i = vertexIndex, il = vertices.Length; i < il; i++)
            {
                vertices[i] = Vector3.zero;
                indexes[i] = 0;
            }
        }
        mesh.vertices = vertices;
        mesh.SetIndices(indexes, MeshTopology.Triangles, 0);
        mesh.uv = uvs;
        mesh.MarkDynamic();
    }

    private void RebuildRingDataFull()
    {
        mesh.Clear();
        Deg2Rad = Mathf.Deg2Rad;
        radius1 = radius - width;
        int count = 360 / depth;
        int totalTringleCount = count * 2;
        int totalVertexCount = totalTringleCount * 3;
        int totalIndexCount = totalVertexCount;
        if (vertices == null || vertices.Length != totalVertexCount)
            vertices = new Vector3[totalVertexCount];
        if (indexes == null || indexes.Length != totalIndexCount)
            indexes = new int[totalIndexCount];
        if (uvs == null || uvs.Length < totalVertexCount)
            uvs = new Vector2[totalVertexCount];
        int vertexIndex = 0;
        for (int i = 0; i < count; i++)
        {
            Vector3 v0 = GetPos(radius, i * depth);
            Vector3 v1 = GetPos(radius1, i * depth);
            Vector3 v2 = GetPos(radius, (i + 1) * depth);
            Vector3 v3 = GetPos(radius1, (i + 1) * depth);
            vertices[vertexIndex++] = v0;
            vertices[vertexIndex++] = v1;
            vertices[vertexIndex++] = v2;
            vertices[vertexIndex++] = v2;
            vertices[vertexIndex++] = v1;
            vertices[vertexIndex++] = v3;
        }
        for (int i = 0; i < totalIndexCount; i++)
        {
            indexes[i] = i;
            uvs[i] = Vector2.zero;
        }
        if (vertices.Length > totalVertexCount)
        {
            for (int i = vertexIndex, il = vertices.Length; i < il; i++)
            {
                vertices[i] = Vector3.zero;
                indexes[i] = 0;
            }
        }
        mesh.vertices = vertices;
        mesh.SetIndices(indexes, MeshTopology.Triangles, 0);
        mesh.uv = uvs;
        mesh.MarkDynamic();
    }

    private Vector3 GetPos(float radius, float angle)
    {
        float x = radius * Mathf.Sin(angle * Deg2Rad);
        float y = radius * Mathf.Cos(angle * Deg2Rad);
        return new Vector3(x, y, 0);
    }
    void Update()
    {
        //Rebuild();
    }

}

相关文章

  • unity3d 画实线圆,虚线圆,五边形,正方形,三角形,圆盘

    首先创建一个Quad 然后把脚本绑定在Quad里面, 你懂的,分别修改脚本里的属性RingType分别绘制不用的图...

  • iOS 画线-圆

    画实线、虚线圆 .h .m 用法 效果图

  • Python初试-turtle

    1、正方形 2、五边形 3、边框颜色 4、填充颜色 5、五角星 6、正方形圈 7、同心圆 8、填充颜色同心圆

  • 太极浅悟

    近日,复读太极图,似有初浅感悟: 大圆小圆无穷圆,虚线实线虚实线。 虚虚实实难琢磨,只缘太极无极限。 ...

  • 项目管理知识UML整理(待深度学习)@2019-02-14

    UML 接口:空心圆+直线(唐老鸭类实现了‘讲人话’);依赖:虚线+箭头(动物和空气的关系);关联:实线+箭头(企...

  • "意外收获"

    把正方形分割成四个等腰直角三角形,把其中的两个小三角形可以拼成一个小正方形,边长是圆的半径。这是在教学外圆...

  • iOS画虚线 画实线

    // 画虚线- (void)drawRect:(CGRect)rect { CAShapeLayer *dott...

  • 视频特效学习01-OpenGL初探Demo

    5.三角形渲染Demo(实践) 6.正方形图形移动Demo(实践) 7.绘制图形:三角形、正方形、圆、正弦函数(实...

  • iOS 虚线圆

    CAShapeLayer * line = [CAShapeLayer layer];CGMutablePathR...

  • 数学易错题

    一,填空 1.扇形是由圆的两条————和圆上的一段————围成的图形。 2.在长方形,正方形等边三角形和圆中,对称...

网友评论

    本文标题:unity3d 画实线圆,虚线圆,五边形,正方形,三角形,圆盘

    本文链接:https://www.haomeiwen.com/subject/gpuyjftx.html