Elin Decompiled Documentation EA 23.102 Nightly
Loading...
Searching...
No Matches
ProceduralMesh.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using UnityEngine;
3
4public class ProceduralMesh : ScriptableObject
5{
6 public Vector2 tiling = Vector2.one;
7
8 public float UVPadding = 0.02f;
9
10 public Vector3 pos = new Vector3(0f, 0f, -1f);
11
12 public Vector3 offset;
13
14 public Vector3 size = Vector3.one;
15
16 public bool top;
17
18 public bool calculateNormal = true;
19
20 private Mesh mesh;
21
22 private List<int> triangles = new List<int>();
23
24 private List<Vector3> vertices = new List<Vector3>();
25
26 private List<Vector2> uv = new List<Vector2>();
27
28 private int triOffset;
29
30 public Mesh GetMesh()
31 {
32 if (!mesh)
33 {
34 Create();
35 }
36 return mesh;
37 }
38
39 public void Create()
40 {
41 mesh = new Mesh();
42 triOffset = 0;
43 triangles.Clear();
44 vertices.Clear();
45 uv.Clear();
46 if (top)
47 {
48 vertices.AddRange(new Vector3[4]
49 {
50 new Vector3(0f, size.y, size.z) + pos,
51 new Vector3(size.x, size.y, size.z) + pos,
52 new Vector3(size.x, size.y, 0f) + pos,
53 new Vector3(0f, size.y, 0f) + pos
54 });
55 uv.AddRange(GetUVs(0));
57 }
58 else
59 {
60 vertices.AddRange(new Vector3[4]
61 {
62 new Vector3(0f, size.y, 0f) + pos,
63 new Vector3(size.x, size.y, 0f) + pos,
64 new Vector3(size.x, offset.y, 0f) + pos,
65 new Vector3(0f, offset.y, 0f) + pos
66 });
67 uv.AddRange(GetUVs(0));
69 }
70 mesh.SetVertices(vertices);
71 mesh.SetUVs(0, uv);
72 mesh.SetTriangles(triangles, 0);
74 {
75 mesh.RecalculateNormals();
76 }
77 }
78
79 public void AddTriangles()
80 {
81 triangles.AddRange(new int[6]
82 {
84 1 + triOffset,
85 2 + triOffset,
86 2 + triOffset,
87 3 + triOffset,
89 });
90 triOffset += 4;
91 }
92
93 private Vector2[] GetUVs(int id)
94 {
95 Vector2[] array = new Vector2[4];
96 Vector2 vector = new Vector2(1f / tiling.x, 1f / tiling.y);
97 int num = id % (int)tiling.x;
98 int num2 = id / (int)tiling.x;
99 float num3 = UVPadding / tiling.x;
100 array[0] = new Vector2((float)num / tiling.x + num3, 1f - (float)num2 / tiling.y - num3);
101 array[1] = new Vector2((float)num / tiling.x + vector.x - num3, 1f - (float)num2 / tiling.y - num3);
102 array[2] = new Vector2((float)num / tiling.x + vector.x - num3, 1f - ((float)num2 / tiling.y + vector.y) + num3);
103 array[3] = new Vector2((float)num / tiling.x + num3, 1f - ((float)num2 / tiling.y + vector.y) + num3);
104 return array;
105 }
106}
List< Vector2 > uv
List< Vector3 > vertices
List< int > triangles
Vector2[] GetUVs(int id)