(二)平面plane

概述

本文用来实现一个平面plane,其与四顶点的四边形mesh相比,网格变多。

Mesh代码

基类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(MeshFilter),typeof(MeshRenderer))]
public class CreateMeshBase : MonoBehaviour
{
    MeshFilter meshFilter;

    protected Mesh mesh;

    protected virtual Vector3[] Vertices { get; }
    protected virtual int[] Triangles { get; }
    protected virtual Vector3[] Normals { get; }
    protected virtual Vector2[] Uvs { get; }
    protected virtual string MeshName { get; }

    protected virtual void Start()
    {
        GetMeshFilter();
    }

    protected virtual void Reset()
    {
        GetMeshFilter();
    }

    protected virtual void OnValidate()
    {
        GetMeshFilter();
    }

    void GetMeshFilter()
    {
        if (meshFilter == null)
        {
            meshFilter = GetComponent<MeshFilter>();
            mesh = new Mesh();            
        }

        mesh.triangles = null;
        mesh.uv = null;
        mesh.vertices = null;

        mesh.name = MeshName;
        mesh.vertices = Vertices;
        mesh.triangles = Triangles;
        mesh.uv = Uvs;

        meshFilter.mesh = mesh;
    }

    private void OnDrawGizmos()
    {
        if (Vertices == null) return;

        Gizmos.color = Color.red;
        Gizmos.DrawSphere(Vector3.zero, 0.5f);

        Gizmos.color = Color.blue;

        for (int i = 0; i < Vertices.Length; i++)
        {
            Gizmos.DrawSphere(Vertices[i], 0.3f);
        }
    }
}

网格类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CreatePlane : CreateMeshBase
{
    public int xSize = 10;
    public int ySize = 6;
    public int widthDelta = 2;
    public int heightDelta = 2;

    protected override string MeshName
    {
        get
        {
            return "Plane";
        }
    }

    protected override Vector3[] Vertices
    {
        get
        {
            Vector3[] vertices = new Vector3[(xSize + 1) * (ySize + 1)];

            for (int i = 0; i < ySize+1; i++)
            {
                for (int j = 0; j < xSize+1; j++)
                {
                    int index = i * (xSize + 1) + j;
                    vertices[index] = new Vector3(j * widthDelta, i * heightDelta, 0);
                }
            }
            return vertices;
        }
    }

    protected override int[] Triangles
    {
        get
        {
            int triangleCount = xSize * ySize * 2;
            int[] triangle = new int[triangleCount * 3];

            for (int j = 0; j < ySize; j++)
            {
                for (int i = 0; i < xSize; i++)
                {
                    int squareCount = i + j * xSize;
                    triangle[6 * squareCount] = j * (xSize + 1) + i;
                    triangle[6 * squareCount + 2] = j * (xSize + 1) + i + 1;
                    triangle[6 * squareCount + 1] = (j + 1) * (xSize + 1) + i;
                    triangle[6 * squareCount + 3] = j * (xSize + 1) + i + 1;
                    triangle[6 * squareCount + 5] = (j + 1) * (xSize + 1) + i + 1;
                    triangle[6 * squareCount + 4] = (j + 1) * (xSize + 1) + i;
                }
            }

            return triangle;
        }
    }

    protected override Vector2[] Uvs
    {
        get
        {
             Vector2[] uv = new Vector2[(xSize + 1) * (ySize + 1)];

            for (int i = 0; i < ySize + 1; i++)
            {
                for (int j = 0; j < xSize + 1; j++)
                {
                    int index = i * (xSize + 1) + j;
                    uv[index] = new Vector2((float)j/xSize, (float)i/ySize);
                }
            }

            return uv;
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值