• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Single Cube Generator a guest Feb 18th, 2020 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. using System.Collections.Generic;
2. using UnityEngine;
3.
4. public class SingleCubeGenerator : MonoBehaviour
5. {
6.     [SerializeField] MeshFilter _meshFilter = null;
7.
8.     // we use the values 24 and 36 because there are 6 * 4 vertices (24) and 6 * 6 triangle indices (36)
9.     private Vector3[] _vertices = new Vector3;
10.     private Vector3[] _normals = new Vector3;
11.     private Vector2[] _uvs = new Vector2;
12.     private int[] _triangles = new int;
13.
14.     void Start()
15.     {
16.         PopulateBuffers();
17.
18.         if (_meshFilter.sharedMesh == null)
19.             _meshFilter.sharedMesh = new Mesh();
20.         var mesh = _meshFilter.sharedMesh;
21.         // we clear in case this is an existing mesh.
22.         mesh.Clear();
23.         // we use the Set* methods because they are less heavy than the normal properties!
24.         mesh.SetVertices(_vertices);
25.         mesh.SetNormals(_normals);
26.         mesh.SetUVs(0, _uvs);
27.         mesh.SetTriangles(_triangles, 0);
28.     }
29.
30.     void PopulateBuffers()
31.     {
32.         // Set some counters to use in the CreateQuad method
33.         var vertexCount = 0;
34.         var triangleCount = 0;
35.         // Top face
37.             new Vector3(-.5f, .5f, -.5f),
38.             new Vector3(-.5f, .5f, .5f),
39.             new Vector3(.5f, .5f, .5f),
40.             new Vector3(.5f, .5f, -.5f),
41.             Vector3.up,
42.             ref vertexCount,
43.             ref triangleCount);
44.         // Bottom face
46.             new Vector3(.5f, -.5f, -.5f),
47.             new Vector3(.5f, -.5f, .5f),
48.             new Vector3(-.5f, -.5f, .5f),
49.             new Vector3(-.5f, -.5f, -.5f),
50.             Vector3.down,
51.             ref vertexCount,
52.             ref triangleCount);
53.         // Front face
55.             new Vector3(.5f, -.5f, .5f),
56.             new Vector3(.5f, .5f, .5f),
57.             new Vector3(-.5f, .5f, .5f),
58.             new Vector3(-.5f, -.5f, .5f),
59.             Vector3.forward,
60.             ref vertexCount,
61.             ref triangleCount);
62.
63.         // todo: implement remaining faces
64.     }
65.
66.     void CreateQuad(Vector3 a, Vector3 b, Vector3 c, Vector3 d, Vector3 normal, ref int vertexCount, ref int triangleCount)
67.     {
68.         _vertices[vertexCount + 0] = a;
69.         _vertices[vertexCount + 1] = b;
70.         _vertices[vertexCount + 2] = c;
71.         _vertices[vertexCount + 3] = d;
72.
73.         _normals[vertexCount + 0] = normal;
74.         _normals[vertexCount + 1] = normal;
75.         _normals[vertexCount + 2] = normal;
76.         _normals[vertexCount + 3] = normal;
77.
78.         _uvs[vertexCount + 0] = new Vector2(0, 0);
79.         _uvs[vertexCount + 1] = new Vector2(0, 1);
80.         _uvs[vertexCount + 2] = new Vector2(1, 1);
81.         _uvs[vertexCount + 3] = new Vector2(1, 0);
82.
83.         _triangles[triangleCount + 0] = vertexCount + 0;
84.         _triangles[triangleCount + 1] = vertexCount + 1;
85.         _triangles[triangleCount + 2] = vertexCount + 2;
86.         _triangles[triangleCount + 3] = vertexCount + 2;
87.         _triangles[triangleCount + 4] = vertexCount + 3;
88.         _triangles[triangleCount + 5] = vertexCount + 0;
89.
90.         vertexCount += 4;
91.         triangleCount += 6;
92.     }
93. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top