Skip to content

Commit

Permalink
Add an Icosahedron primitive
Browse files Browse the repository at this point in the history
  • Loading branch information
andybak committed Jun 27, 2024
1 parent 77e515d commit bc02e61
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 127 deletions.
121 changes: 94 additions & 27 deletions Assets/Prefabs/CameraRig.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -9358,7 +9358,6 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 224991721833942628}
- component: {fileID: 33931078607798932}
- component: {fileID: 23400739857238160}
- component: {fileID: 222863757601499486}
- component: {fileID: 114889146673854390}
Expand Down Expand Up @@ -9388,14 +9387,6 @@ RectTransform:
m_AnchoredPosition: {x: -0.0000049592927, y: 0.51994324}
m_SizeDelta: {x: 148.4, y: 32.67}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!33 &33931078607798932
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1125421002062496}
m_Mesh: {fileID: 0}
--- !u!23 &23400739857238160
MeshRenderer:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -27050,6 +27041,7 @@ Transform:
- {fileID: 4142224838695494}
- {fileID: 4499153807668788}
- {fileID: 4488566617152198}
- {fileID: 2674856114974116345}
m_Father: {fileID: 4458942072854162}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
Expand Down Expand Up @@ -35422,7 +35414,6 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 224353535023853912}
- component: {fileID: 33894183074636898}
- component: {fileID: 23779459973462100}
- component: {fileID: 222675228110726036}
- component: {fileID: 114756877966986152}
Expand Down Expand Up @@ -35452,14 +35443,6 @@ RectTransform:
m_AnchoredPosition: {x: -0.00038269162, y: 0.002600002}
m_SizeDelta: {x: 148.4, y: 32.67}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!33 &33894183074636898
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1490861234011590}
m_Mesh: {fileID: 0}
--- !u!23 &23779459973462100
MeshRenderer:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -46856,7 +46839,6 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 224792478581430570}
- component: {fileID: 33180587196729598}
- component: {fileID: 23689086375512880}
- component: {fileID: 222656296281821840}
- component: {fileID: 114302973119263724}
Expand Down Expand Up @@ -46886,14 +46868,6 @@ RectTransform:
m_AnchoredPosition: {x: 0.000000015832484, y: 0.51995945}
m_SizeDelta: {x: 148.4, y: 32.67}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!33 &33180587196729598
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1631856206869138}
m_Mesh: {fileID: 0}
--- !u!23 &23689086375512880
MeshRenderer:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -72375,3 +72349,96 @@ MonoBehaviour:
_SortingLayer: 0
_SortingLayerID: 0
_SortingOrder: 0
--- !u!1 &5181794319663275375
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 2674856114974116345}
- component: {fileID: 7081471672610052149}
- component: {fileID: 6836010759666660914}
- component: {fileID: 7151145942401920378}
m_Layer: 10
m_Name: ID_Icosahedron
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
--- !u!4 &2674856114974116345
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5181794319663275375}
m_LocalRotation: {x: 0.7071068, y: 0.00000006050855, z: 0.000000008354488, w: 0.70710677}
m_LocalPosition: {x: 0.00030002737, y: 0.0039000195, z: 0.031499982}
m_LocalScale: {x: 0.05, y: 0.05, z: 0.05}
m_Children: []
m_Father: {fileID: 4776689306038126}
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!33 &7081471672610052149
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5181794319663275375}
m_Mesh: {fileID: 4300000, guid: 8f63658e7c8574ed587fa33d6ddded97, type: 2}
--- !u!23 &6836010759666660914
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5181794319663275375}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 2100000, guid: d06ae5e60c379dc4484da7d362750c97, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
--- !u!114 &7151145942401920378
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5181794319663275375}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ea4c1758a03182643917b8d44c97d323, type: 3}
m_Name:
m_EditorClassIdentifier:
rend: {fileID: 6836010759666660914}
145 changes: 45 additions & 100 deletions Assets/Scripts/model/core/Primitives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public class Primitives
{
// Note: the Shape enum is used to index things, so it should always start at 0 and count up
// without skipping numbers. Do not define items to have arbitrary values. You will have a bad time.
public enum Shape { CONE, SPHERE, CUBE, CYLINDER, TORUS,
ICOSAHEDRON
};
public enum Shape { CONE, SPHERE, CUBE, CYLINDER, TORUS, ICOSAHEDRON };
private const int LINES_OF_LATITUDE = 8;
private const int LINES_OF_LONGITUDE = 12;
public static readonly int NUM_SHAPES = Enum.GetValues(typeof(Shape)).Length;
Expand All @@ -46,49 +44,51 @@ public enum Shape { CONE, SPHERE, CUBE, CYLINDER, TORUS,
private static readonly float GOLDEN_RATIO_SCALED = 0.1618033988749895f;

private static readonly Vector3[] ICOSAHEDRON_POINTS = {
new Vector3(-0.1f, GOLDEN_RATIO_SCALED, 0),
new Vector3(0.1f, GOLDEN_RATIO_SCALED, 0),
new Vector3(-0.1f, -GOLDEN_RATIO_SCALED, 0),
new Vector3(0.1f, -GOLDEN_RATIO_SCALED, 0),

new Vector3(0, -0.1f, GOLDEN_RATIO_SCALED),
new Vector3(0, 0.1f, GOLDEN_RATIO_SCALED),
new Vector3(0, -0.1f, -GOLDEN_RATIO_SCALED),
new Vector3(0, 0.1f, -GOLDEN_RATIO_SCALED),

new Vector3(GOLDEN_RATIO_SCALED, 0, -0.1f),
new Vector3(GOLDEN_RATIO_SCALED, 0, 0.1f),
new Vector3(-GOLDEN_RATIO_SCALED, 0, -0.1f),
new Vector3(-GOLDEN_RATIO_SCALED, 0, 0.1f)};
new Vector3(-0.1f, GOLDEN_RATIO_SCALED, 0),
new Vector3(0.1f, GOLDEN_RATIO_SCALED, 0),
new Vector3(-0.1f, -GOLDEN_RATIO_SCALED, 0),
new Vector3(0.1f, -GOLDEN_RATIO_SCALED, 0),

new Vector3(0, -0.1f, GOLDEN_RATIO_SCALED),
new Vector3(0, 0.1f, GOLDEN_RATIO_SCALED),
new Vector3(0, -0.1f, -GOLDEN_RATIO_SCALED),
new Vector3(0, 0.1f, -GOLDEN_RATIO_SCALED),

new Vector3(GOLDEN_RATIO_SCALED, 0, -0.1f),
new Vector3(GOLDEN_RATIO_SCALED, 0, 0.1f),
new Vector3(-GOLDEN_RATIO_SCALED, 0, -0.1f),
new Vector3(-GOLDEN_RATIO_SCALED, 0, 0.1f)
};

private static readonly List<int>[] ICOSAHEDRON_FACES = {
// Faces around point 0.
new List<int> { 0, 11, 5 },
new List<int> { 0, 5, 1 },
new List<int> { 0, 1, 7 },
new List<int> { 0, 7, 10 },
new List<int> { 0, 10, 11 },

// Faces adjacent to point 0.
new List<int> { 1, 5, 9 },
new List<int> { 5, 11, 4 },
new List<int> { 11, 10, 2 },
new List<int> { 10, 7, 6 },
new List<int> { 7, 1, 8 },

// Faces around point 3.
new List<int> { 3, 9, 4 },
new List<int> { 3, 4, 2 },
new List<int> { 3, 2, 6 },
new List<int> { 3, 6, 8 },
new List<int> { 3, 8, 9 },

// Faces adjacent to point 3.
new List<int> { 4, 9, 5 },
new List<int> { 2, 4, 11 },
new List<int> { 6, 2, 10 },
new List<int> { 8, 6, 7 },
new List<int> { 9, 8, 1 }};
// Faces around point 0.
new List<int> { 0, 11, 5 },
new List<int> { 0, 5, 1 },
new List<int> { 0, 1, 7 },
new List<int> { 0, 7, 10 },
new List<int> { 0, 10, 11 },

// Faces adjacent to point 0.
new List<int> { 1, 5, 9 },
new List<int> { 5, 11, 4 },
new List<int> { 11, 10, 2 },
new List<int> { 10, 7, 6 },
new List<int> { 7, 1, 8 },

// Faces around point 3.
new List<int> { 3, 9, 4 },
new List<int> { 3, 4, 2 },
new List<int> { 3, 2, 6 },
new List<int> { 3, 6, 8 },
new List<int> { 3, 8, 9 },

// Faces adjacent to point 3.
new List<int> { 4, 9, 5 },
new List<int> { 2, 4, 11 },
new List<int> { 6, 2, 10 },
new List<int> { 8, 6, 7 },
new List<int> { 9, 8, 1 }
};

public static MMesh BuildPrimitive(Shape shape, Vector3 scale, Vector3 offset, int id, int material)
{
Expand All @@ -105,7 +105,7 @@ public static MMesh BuildPrimitive(Shape shape, Vector3 scale, Vector3 offset, i
case Shape.TORUS:
return Torus(id, offset, scale, material);
case Shape.ICOSAHEDRON:
return Icosahedron(id, offset, scale, material);
return AxisAlignedIcosphere(id, offset, scale, material, 0);
default:
return AxisAlignedBox(id, offset, scale, material);
}
Expand Down Expand Up @@ -667,60 +667,5 @@ public static MMesh Torus(int id, Vector3 center, Vector3 scale, int materialId)

return new MMesh(id, center, Quaternion.identity, vertices, faces);
}

/// <summary>
/// Create an icosahedron
/// </summary>
/// <param name="id">Id for the mesh.</param>
/// <param name="center">Center of the icosahedron.</param>
/// <param name="scale">Scale of icosahedron.</param>
/// <param name="materialId">Material id for the mesh.</param>
/// <returns>An MMesh that renders an icosahedron.</returns>
public static MMesh Icosahedron(
int id, Vector3 center, Vector3 scale, int materialId) {
FaceProperties faceProperties = new FaceProperties(materialId);
Dictionary<int, Vertex> vertices = new Dictionary<int, Vertex>
{
// Vertices for an icosahedron of unit radius
{0, new Vertex(0, new Vector3(-0.1f, GOLDEN_RATIO_SCALED, 0))},
{1, new Vertex(1, new Vector3(0.1f, GOLDEN_RATIO_SCALED, 0))},
{2, new Vertex(2, new Vector3(-0.1f, -GOLDEN_RATIO_SCALED, 0))},
{3, new Vertex(3, new Vector3(0.1f, -GOLDEN_RATIO_SCALED, 0))},
{4, new Vertex(4, new Vector3(0, -0.1f, GOLDEN_RATIO_SCALED))},
{5, new Vertex(5, new Vector3(0, 0.1f, GOLDEN_RATIO_SCALED))},
{6, new Vertex(6, new Vector3(0, -0.1f, -GOLDEN_RATIO_SCALED))},
{7, new Vertex(7, new Vector3(0, 0.1f, -GOLDEN_RATIO_SCALED))},
{8, new Vertex(8, new Vector3(GOLDEN_RATIO_SCALED, 0, -0.1f))},
{9, new Vertex(9, new Vector3(GOLDEN_RATIO_SCALED, 0, 0.1f))},
{10, new Vertex(10, new Vector3(-GOLDEN_RATIO_SCALED, 0, -0.1f))},
{11, new Vertex(11, new Vector3(-GOLDEN_RATIO_SCALED, 0, 0.1f))}
};

var faces = new List<Face>
{
new Face(0, new List<int>{0, 1, 4}.AsReadOnly(), vertices, faceProperties),
new Face(1, new List<int>{0, 4, 5}.AsReadOnly(), vertices, faceProperties),
new Face(2, new List<int>{0, 5, 10}.AsReadOnly(), vertices, faceProperties),
new Face(3, new List<int>{0, 10, 11}.AsReadOnly(), vertices, faceProperties),
new Face(4, new List<int>{0, 11, 1}.AsReadOnly(), vertices, faceProperties),
new Face(5, new List<int>{1, 5, 9}.AsReadOnly(), vertices, faceProperties),
new Face(6, new List<int>{1, 9, 8}.AsReadOnly(), vertices, faceProperties),
new Face(7, new List<int>{1, 8, 4}.AsReadOnly(), vertices, faceProperties),
new Face(8, new List<int>{2, 3, 6}.AsReadOnly(), vertices, faceProperties),
new Face(9, new List<int>{2, 6, 7}.AsReadOnly(), vertices, faceProperties),
new Face(10, new List<int>{2, 7, 11}.AsReadOnly(), vertices, faceProperties),
new Face(11, new List<int>{2, 11, 10}.AsReadOnly(), vertices, faceProperties),
new Face(12, new List<int>{2, 10, 3}.AsReadOnly(), vertices, faceProperties),
new Face(13, new List<int>{3, 9, 8}.AsReadOnly(), vertices, faceProperties),
new Face(14, new List<int>{3, 8, 6}.AsReadOnly(), vertices, faceProperties),
new Face(15, new List<int>{3, 6, 9}.AsReadOnly(), vertices, faceProperties),
new Face(16, new List<int>{4, 8, 7}.AsReadOnly(), vertices, faceProperties),
new Face(17, new List<int>{4, 7, 5}.AsReadOnly(), vertices, faceProperties),
new Face(18, new List<int>{5, 7, 11}.AsReadOnly(), vertices, faceProperties),
new Face(19, new List<int>{5, 11, 9}.AsReadOnly(), vertices, faceProperties)

};
return new MMesh(id, center, Quaternion.identity, vertices, faces.ToDictionary(f => f.id));
}
}
}

0 comments on commit bc02e61

Please sign in to comment.