|
可自定义 半径、厚度、边数
- using System;
- using UnityEngine;
- #if UNITY_EDITOR
- using UnityEditor;
- using LitJson;
- #endif
- using System.Collections;
- using System.Collections.Generic;
- using System.Runtime;
- using UnityEngine.Experimental.U2D;
- [RequireComponent(typeof(MeshFilter))]
- [RequireComponent(typeof(MeshRenderer))]
- public class DeformReel : MonoBehaviour
- {
- //老虎机转轮半径
- public float radius = 1;
- //老虎机轮子宽度
- public float width = 1;
- //老虎机轮子几个边
- public int side = 8;
- //轮子是否镂空 系数
- public float thickness = 0;
- // Start is called before the first frame update
- void Start()
- {
- Main();
- }
- // Update is called once per frame
- void Update()
- {
- }
- void OnValidate()
- {
- Main();
- }
- void Main()
- {
- MeshFilter mf = this.gameObject.GetComponent<MeshFilter>();
- Mesh mesh = mf.mesh;
- mesh.Clear();
- List<Vector3> listVertexOneDmt = new List<Vector3>();
- //圆柱体
- if (thickness == 0)
- {
- int singleIdx = 0;
- for (int i = 0, lenI = (side + 1) * 2; i < lenI; i++)
- {
- //y z轴面的中心点
- if (i == 0)
- {
- listVertexOneDmt.Add(new Vector3(width / 2f, 0, 0));
- }
- //y z轴面的从上顺时针顶点
- else if (i < lenI / 2)
- {
- listVertexOneDmt.Add(new Vector3(width / 2f,
- Mathf.Cos(360f / side * (i - 1) * Mathf.PI / 180f) * radius,
- Mathf.Sin(360f / side * (i - 1) * Mathf.PI / 180f) * radius));
- }
- //反面的点
- else
- {
- listVertexOneDmt.Add(new Vector3(-listVertexOneDmt[i - lenI / 2].x,
- listVertexOneDmt[i - lenI / 2].y,
- listVertexOneDmt[i - lenI / 2].z));
- }
- }
- //side*4个三角形 side*4*3个点
- int[] triangles = new int[side * 3 * 4];
- for (int i = 0, lenI = triangles.Length; i < lenI; i++)
- {
- triangles[i] = 0;
- }
- int[] sides = new int[side + 1];
- //0 1 2 3 4 5 6
- for (int i = 0, lenI = sides.Length; i < lenI; i++)
- {
- sides[i] = i;
- }
- //y z轴的三角区域 6*3=18 右侧边的6个三角形参数
- //[(0,1,2),(0,2,3),(0,3,4),(0,4,5),(0,5,6),(0,6,1)]
- for (int i = 0, lenI = side; i < lenI; i++)
- {
- for (int j = 0, lenJ = 3; j < lenJ; j++)
- {
- if (j == 0)
- {
- triangles[i * 3 + j] = 0;
- }
- else if (j == 1)
- {
- triangles[i * 3 + j] = i + 1;
- }
- else
- {
- triangles[i * 3 + j] = i + 2 > side ? 1 : i + 2;
- }
- }
- }
- //7 8 9 10 11 12 13
- for (int i = 0, lenI = sides.Length; i < lenI; i++)
- {
- sides[i] = i + side + 1;
- }
- //y z轴的三角区域 6*3=18 左侧边的6个三角形参数
- //(7,9,8) ,(7,10,9),(7,11,10),(7,12,11),(7,13,12),(7,8,13)
- for (int i = side, lenI = side * 2; i < lenI; i++)
- {
- for (int j = 0, lenJ = 3; j < lenJ; j++)
- {
- if (j==0)
- {
- triangles[i * 3 + j] = side+1;
- }
- else if (j==1)
- {
- triangles[i * 3 + j] = i+3>side*2+1?side+2:i+3;
- }
- else
- {
- triangles[i * 3 + j] = i+2 ;
- }
- }
- }
- //1,2,3,4,5,6
- sides = new int[side];
- for (int i = 1, lenI = side; i <= lenI; i++)
- {
- sides[i - 1] = i;
- }
- //(2,1,8) (3,2,9) (4,3,10) (5,4,11) (6,5,12) (1,6,13)
- for (int i = side * 2, lenI = side * 3; i < lenI; i++)
- {
- for (int j = 0, lenJ = 3; j < lenJ; j++)
- {
- if (j == 0)
- {
- triangles[i * 3 + j] = (i - side * 2) + 2>side?1: (i - side * 2) + 2;
- }
- else if(j==1)
- {
- triangles[i * 3 + j] = (i - side * 2) + 1;
- }
- else
- {
- triangles[i * 3 + j] = (i - side * 2) +side+2;
- }
- }
- }
- //8,9,10,11,12,13
- for (int i = 1, lenI = side; i <= lenI; i++)
- {
- sides[i - 1] = i + side + 1;
- }
- //(8,9,2) (9,10,3) (10,11,4) (11,12,5) (12,13,6) (13,8,1)
- for (int i = side * 3, lenI = side * 4; i < lenI; i++)
- {
- for (int j = 0, lenJ = 3; j < lenJ; j++)
- {
- if (j == 2)
- {
- triangles[i * 3 + j] = (i - side * 3 + 2) > (side) ? 1 : (i - side * 3 + 2);
- }
- else if (j == 0)
- {
- triangles[i * 3 + j] = i - side * 3 + side + 2;
- }
- else if (j == 1)
- {
- triangles[i * 3 + j] = (i - side * 3 + side + 3) > ((side) * 2 + 1)
- ? (side + 2)
- : (i - side * 3 + side + 3);
- }
- }
- }
- Debug.Log(" mesh.triangles:" + JsonMapper.ToJson(triangles));
- mesh.vertices = listVertexOneDmt.ToArray();
- mesh.triangles = triangles;
- }
- // mesh.uv = uvs;
- mesh.RecalculateBounds();
- mesh.RecalculateNormals();
- }
- }
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|