forked from William-McGonagle/unity-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRectangularPrism.cs
More file actions
115 lines (92 loc) · 3.34 KB
/
RectangularPrism.cs
File metadata and controls
115 lines (92 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RectangularPrism
{
/// <summary>
/// Gets the volume of a Rectangular Prism with a variable size.
/// </summary>
/// <param name="size">The size of the Rectangular Prism.</param>
/// <returns>Volume of Rectangular Prism as a float.</returns>
public static float Volume(float size)
{
return Volume(size, size, size);
}
/// <summary>
/// Gets the volume of a Rectangular Prism given the Width, Height, and Depth.
/// </summary>
/// <param name="width">Width of the Rectangular Prism. (X Axis)</param>
/// <param name="height">Height of the Rectangular Prism. (Y Axis)</param>
/// <param name="depth">Depth of the Rectangular Prism. (Z Axis)</param>
/// <returns>Volume of Rectangular Prism as a float.</returns>
public static float Volume(float width, float height, float depth)
{
return width * height * depth;
}
/// <summary>
/// Generates a Rectangular Prism with a size of 1. (1x1x1)
/// </summary>
/// <returns>The mesh of a Rectangular Prism.</returns>
public static Mesh Generate()
{
return Generate(1, 1, 1);
}
/// <summary>
/// Generate a Rectangular Prism with a variable size. (NxNxN)
/// </summary>
/// <param name="size">The size of the Rectangular Prism.</param>
/// <returns>The mesh of a Rectangular Prism.</returns>
public static Mesh Generate(float size)
{
return Generate(size, size, size);
}
/// <summary>
/// Generate the Mesh for a Rectangular Prism given a width, height, and depth. (WxHxD)
/// </summary>
/// <param name="width">Width of the Rectangular Prism. (X Axis)</param>
/// <param name="height">Height of the Rectangular Prism. (Y Axis)</param>
/// <param name="depth">Depth of the Rectangular Prism. (Z Axis)</param>
/// <returns>The mesh of a Rectangular Rrism.</returns>
public static Mesh Generate(float width, float height, float depth)
{
// Create the Vertices Array
Vector3[] vertices = {
new Vector3(-width / 2, -height / 2, -depth / 2),
new Vector3( width / 2, -height / 2, -depth / 2),
new Vector3( width / 2, height / 2, -depth / 2),
new Vector3(-width / 2, height / 2, -depth / 2),
new Vector3(-width / 2, height / 2, depth / 2),
new Vector3( width / 2, height / 2, depth / 2),
new Vector3( width / 2, -height / 2, depth / 2),
new Vector3(-width / 2, -height / 2, depth / 2),
};
// Create the Triangles Array
int[] triangles = {
0, 2, 1,
0, 3, 2,
2, 3, 4,
2, 4, 5,
1, 2, 5,
1, 5, 6,
0, 7, 4,
0, 4, 3,
5, 4, 7,
5, 7, 6,
0, 6, 7,
0, 1, 6,
};
// Create Mesh Object
Mesh output = new Mesh();
// Set Mesh Object Data
output.vertices = vertices;
output.triangles = triangles;
// Optimize Mesh
output.Optimize();
// Clean Up Mesh
output.RecalculateTangents();
output.RecalculateNormals();
output.RecalculateBounds();
// Output the Cleaned Mesh
return output;
}
}