Skip to content

Commit

Permalink
New shapes: Ring and Disc
Browse files Browse the repository at this point in the history
  • Loading branch information
keijiro committed Oct 7, 2021
1 parent 587ec14 commit deb01e3
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 1 deletion.
93 changes: 93 additions & 0 deletions Packages/jp.keijiro.metamesh/Editor/Circle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Rendering;
using UnityEngine;
using Unity.Mathematics;

namespace Metamesh {

[System.Serializable]
public sealed class Ring
{
public float Radius = 1;
public float Width = 0.1f;
[Range(0, 1)] public float Angle = 1;
public uint Segments = 32;
public Axis Axis = Axis.Z;
public bool DoubleSided = false;

public void Generate(Mesh mesh)
{
// Parameter sanitization
var ext = math.min(Radius, Width / 2);

// Axis selection
var X = float3.zero;
var Y = float3.zero;
var ai = (int)Axis;
X[(ai + 1) % 3] = 1;
Y[(ai + 2) % 3] = 1;

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

var i_div_o = (Radius - Width / 2) / (Radius + Width / 2);

for (var i = 0; i < Segments; i++)
{
var phi = 2 * math.PI * Angle * ((float)i / (Segments - 1) - 0.5f);
var (x, y) = (math.cos(phi), math.sin(phi));

var v = x * X + y * Y;
vtx.Add(v * (Radius - ext));
vtx.Add(v * (Radius + ext));
uv0.Add(math.float2(-x, y) / 2 * i_div_o + 0.5f);
uv0.Add(math.float2(-x, y) / 2 + 0.5f);
}

if (DoubleSided)
{
vtx = vtx.Concat(vtx).ToList();
uv0 = uv0.Concat(uv0).ToList();
}

// Index array
var idx = new List<int>();
var n = (int)Segments;

for (var i = 0; i < n - 1; i++)
{
idx.Add(i * 2);
idx.Add(i * 2 + 1);
idx.Add(i * 2 + 2);

idx.Add(i * 2 + 1);
idx.Add(i * 2 + 3);
idx.Add(i * 2 + 2);
}

if (DoubleSided)
{
for (var i = 0; i < n - 1; i++)
{
idx.Add((n + i) * 2);
idx.Add((n + i) * 2 + 2);
idx.Add((n + i) * 2 + 1);

idx.Add((n + i) * 2 + 1);
idx.Add((n + i) * 2 + 2);
idx.Add((n + i) * 2 + 3);
}
}

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

} // namespace Metamesh
11 changes: 11 additions & 0 deletions Packages/jp.keijiro.metamesh/Editor/Circle.cs.meta

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

79 changes: 79 additions & 0 deletions Packages/jp.keijiro.metamesh/Editor/Disc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Rendering;
using UnityEngine;
using Unity.Mathematics;

namespace Metamesh {

[System.Serializable]
public sealed class Disc
{
public float Radius = 1;
[Range(0, 1)] public float Angle = 1;
public uint Segments = 32;
public Axis Axis = Axis.Z;
public bool DoubleSided = false;

public void Generate(Mesh mesh)
{
// Axis selection
var X = float3.zero;
var Y = float3.zero;
var ai = (int)Axis;
X[(ai + 1) % 3] = 1;
Y[(ai + 2) % 3] = 1;

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

for (var i = 0; i < Segments; i++)
{
var phi = 2 * math.PI * Angle * ((float)i / (Segments - 1) - 0.5f);
var (x, y) = (math.cos(phi), math.sin(phi));

vtx.Add((x * X + y * Y) * Radius);
uv0.Add(math.float2(-x, y) / 2 + 0.5f);
}

vtx.Add(0);
uv0.Add(0.5f);

if (DoubleSided)
{
vtx = vtx.Concat(vtx).ToList();
uv0 = uv0.Concat(uv0).ToList();
}

// Index array
var idx = new List<int>();
var n = (int)Segments;

for (var i = 0; i < n - 1; i++)
{
idx.Add(n);
idx.Add(i);
idx.Add(i + 1);
}

if (DoubleSided)
{
for (var i = 0; i < n - 1; i++)
{
idx.Add(n + 1 + n);
idx.Add(n + 1 + i + 1);
idx.Add(n + 1 + i);
}
}

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

} // namespace Metamesh
11 changes: 11 additions & 0 deletions Packages/jp.keijiro.metamesh/Editor/Disc.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 }
public enum Shape { Plane, Box, Sphere, Icosphere, Cylinder, RoundedBox, Ring, Disc }
public enum Axis { X, Y, Z }

} // namespace Metamesh
4 changes: 4 additions & 0 deletions Packages/jp.keijiro.metamesh/Editor/MetameshImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public sealed class MetameshImporter : ScriptedImporter
[SerializeField] Icosphere _icosphere = new Icosphere();
[SerializeField] Cylinder _cylinder = new Cylinder();
[SerializeField] RoundedBox _roundedBox = new RoundedBox();
[SerializeField] Ring _ring = new Ring();
[SerializeField] Disc _disc = new Disc();

public override void OnImportAsset(AssetImportContext context)
{
Expand Down Expand Up @@ -56,6 +58,8 @@ Mesh ImportAsMesh(string path)
case Shape.Icosphere : _icosphere .Generate(mesh); break;
case Shape.Cylinder : _cylinder .Generate(mesh); break;
case Shape.RoundedBox : _roundedBox.Generate(mesh); break;
case Shape.Ring : _ring .Generate(mesh); break;
case Shape.Disc : _disc .Generate(mesh); break;
}

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

public override void OnEnable()
{
Expand All @@ -29,6 +31,8 @@ public override void OnEnable()
_icosphere = serializedObject.FindProperty("_icosphere");
_cylinder = serializedObject.FindProperty("_cylinder");
_roundedBox = serializedObject.FindProperty("_roundedBox");
_ring = serializedObject.FindProperty("_ring");
_disc = serializedObject.FindProperty("_disc");
}

public override void OnInspectorGUI()
Expand All @@ -45,6 +49,8 @@ public override void OnInspectorGUI()
case Shape.Icosphere : EditorGUILayout.PropertyField(_icosphere); break;
case Shape.Cylinder : EditorGUILayout.PropertyField(_cylinder); break;
case Shape.RoundedBox: EditorGUILayout.PropertyField(_roundedBox); break;
case Shape.Ring : EditorGUILayout.PropertyField(_ring); break;
case Shape.Disc : EditorGUILayout.PropertyField(_disc); break;
}

serializedObject.ApplyModifiedProperties();
Expand Down

0 comments on commit deb01e3

Please sign in to comment.