Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 160 additions & 0 deletions Packages/jp.keijiro.metamesh/Editor/Cone.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Rendering;
using Unity.Mathematics;

namespace Metamesh {

[System.Serializable]
public sealed class Cone
{
public float Radius = 1;
public float Height = 1;
public uint Columns = 24;
public uint Rows = 12;
public Axis Axis = Axis.Y;
public bool InvertAxis;
public bool Cap = true;

public void Generate(Mesh mesh)
{
// Parameter sanitization
var res = math.int2((int)Columns, (int)Rows);
res = math.max(res, math.int2(3, 1));

// Axis selection
var va = float3.zero;
var vx = float3.zero;

var ai = (int)Axis;

va[(ai + 0) % 3] = 1;
vx[(ai + 1) % 3] = 1;

// Vertex array
var vtx = new List<float3>();
var nrm = new List<float3>();
var uv0 = new List<float2>();

// (Body vertices)
for (var iy = 0; iy < res.y + 1; iy++)
{
for (var ix = 0; ix < res.x + 1; ix++)
{
var u = (float)ix / res.x;
var v = (float)iy / res.y;

var rot = quaternion.AxisAngle(va, u * math.PI * -2);
var n = math.mul(rot, vx);
float radius = InvertAxis ? v * Radius : (1 - v) * Radius;
var p = n * radius + va * (v - 0.5f) * Height;

vtx.Add(p);
nrm.Add(n);
uv0.Add(math.float2(u, v));
}
}

// (End cap vertices)
if (Cap)
{
if (InvertAxis)
{
vtx.Add(va * Height / +2);
nrm.Add(+va);
uv0.Add(math.float2(0.5f, 0.5f));
}
else
{
vtx.Add(va * Height / -2);
nrm.Add(-va);
uv0.Add(math.float2(0.5f, 0.5f));
}

for (var ix = 0; ix < res.x; ix++)
{
var u = (float)ix / res.x * math.PI * 2;

var rot = quaternion.AxisAngle(va, -u);
var p = math.mul(rot, vx) * Radius;

if (InvertAxis)
{
vtx.Add(p + va * Height / +2);
nrm.Add(+va);
uv0.Add(math.float2(math.cos(+u), math.sin(+u)) / 2 + 0.5f);
}
else
{
vtx.Add(p + va * Height / -2);
nrm.Add(-va);
uv0.Add(math.float2(math.cos(-u), math.sin(-u)) / 2 + 0.5f);
}
}
}

// Index array
var idx = new List<int>();
var i = 0;

// (Body indices)
for (var iy = 0; iy < res.y; iy++, i++)
{
for (var ix = 0; ix < res.x; ix++, i++)
{
idx.Add(i);
idx.Add(i + res.x + 1);
idx.Add(i + 1);

idx.Add(i + 1);
idx.Add(i + res.x + 1);
idx.Add(i + res.x + 2);
}
}

// (End cap indices)
if (Cap)
{
i += res.x + 1;

for (var ix = 0; ix < res.x - 1; ix++)
{
if (InvertAxis)
{
idx.Add(i + ix + 2);
idx.Add(i + ix + 1);
idx.Add(i);
}
else
{
idx.Add(i);
idx.Add(i + ix + 1);
idx.Add(i + ix + 2);
}
}

if (InvertAxis)
{
idx.Add(i + 1);
idx.Add(i + res.x);
idx.Add(i);
}
else
{
idx.Add(i);
idx.Add(i + res.x);
idx.Add(i + 1);
}
}

// Mesh object construction
if (vtx.Count > 65535) mesh.indexFormat = IndexFormat.UInt32;
mesh.SetVertices(vtx.Select(v => (Vector3)v).ToList());
mesh.SetNormals(nrm.Select(v => (Vector3)v).ToList());
mesh.SetUVs(0, uv0.Select(v => (Vector2)v).ToList());
mesh.SetIndices(idx, MeshTopology.Triangles, 0);
}
}

}
3 changes: 3 additions & 0 deletions Packages/jp.keijiro.metamesh/Editor/Cone.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Packages/jp.keijiro.metamesh/Editor/Enums.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Metamesh {

public enum Shape { Plane, Box, Sphere, Icosphere, Cylinder, RoundedBox, Ring, Disc }
public enum Shape { Plane, Box, Sphere, Icosphere, Cylinder, RoundedBox, Ring, Disc, Cone }
public enum Axis { X, Y, Z }

} // namespace Metamesh
2 changes: 2 additions & 0 deletions Packages/jp.keijiro.metamesh/Editor/MetameshImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public sealed class MetameshImporter : ScriptedImporter
[SerializeField] Ring _ring = new Ring();
[SerializeField] Disc _disc = new Disc();
[SerializeField] bool _generateLightmapUVs = false;
[SerializeField] Cone _cone = new Cone();

public override void OnImportAsset(AssetImportContext context)
{
Expand Down Expand Up @@ -61,6 +62,7 @@ Mesh ImportAsMesh(string path)
case Shape.RoundedBox : _roundedBox.Generate(mesh); break;
case Shape.Ring : _ring .Generate(mesh); break;
case Shape.Disc : _disc .Generate(mesh); break;
case Shape.Cone : _cone .Generate(mesh); break;
}

mesh.RecalculateBounds();
Expand Down
3 changes: 3 additions & 0 deletions Packages/jp.keijiro.metamesh/Editor/MetameshImporterEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ sealed class MetameshImporterEditor : ScriptedImporterEditor
SerializedProperty _ring;
SerializedProperty _disc;
SerializedProperty _generateLightmapUVs;
SerializedProperty _cone;

public override void OnEnable()
{
Expand All @@ -34,6 +35,7 @@ public override void OnEnable()
_roundedBox = serializedObject.FindProperty("_roundedBox");
_ring = serializedObject.FindProperty("_ring");
_disc = serializedObject.FindProperty("_disc");
_cone = serializedObject.FindProperty("_cone");
_generateLightmapUVs = serializedObject.FindProperty("_generateLightmapUVs");
}

Expand All @@ -53,6 +55,7 @@ public override void OnInspectorGUI()
case Shape.RoundedBox: EditorGUILayout.PropertyField(_roundedBox); break;
case Shape.Ring : EditorGUILayout.PropertyField(_ring); break;
case Shape.Disc : EditorGUILayout.PropertyField(_disc); break;
case Shape.Cone : EditorGUILayout.PropertyField(_cone); break;
}

EditorGUILayout.PropertyField(_generateLightmapUVs);
Expand Down
2 changes: 2 additions & 0 deletions Packages/jp.keijiro.metamesh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ At the moment, it only supports very basic primitive shapes.
- Sphere (UV sphere)
- Icosphere
- Cylinder
- Cone
- Rounded box
- Disc
- Ring
- Cone

How To Install
--------------
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ At the moment, it only supports very basic primitive shapes.
- Rounded box
- Disc
- Ring
- Cone

Related Project
---------------
Expand Down