Skip to content

Commit

Permalink
Coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
keijiro committed Oct 7, 2021
1 parent 4608729 commit 587ec14
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 252 deletions.
143 changes: 72 additions & 71 deletions Packages/jp.keijiro.metamesh/Editor/Box.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,79 @@
using System.Collections.Generic;
using System.Linq;

namespace Metamesh
namespace Metamesh {

[System.Serializable]
public class Box
{
[System.Serializable]
public class Box
public float Width = 1;
public float Height = 1;
public float Depth = 1;

public void Generate(Mesh mesh)
{
public float Width = 1;
public float Height = 1;
public float Depth = 1;

public void Generate(Mesh mesh)
{
var x = Width / 2;
var y = Height / 2;
var z = Depth / 2;

var v0 = new Vector3(-x, -y, -z);
var v1 = new Vector3( x, -y, -z);
var v2 = new Vector3(-x, -y, z);
var v3 = new Vector3( x, -y, z);

var v4 = new Vector3(-x, y, -z);
var v5 = new Vector3( x, y, -z);
var v6 = new Vector3(-x, y, z);
var v7 = new Vector3( x, y, z);

var t0 = new Vector2(0, 0);
var t1 = new Vector2(1, 0);
var t2 = new Vector2(0, 1);
var t3 = new Vector2(1, 1);

var vtx = new List<Vector3>();
var uv0 = new List<Vector2>();

// Bottom
vtx.Add(v0); vtx.Add(v1); vtx.Add(v2);
vtx.Add(v1); vtx.Add(v3); vtx.Add(v2);
uv0.Add(t1); uv0.Add(t0); uv0.Add(t3);
uv0.Add(t0); uv0.Add(t2); uv0.Add(t3);

// Top
vtx.Add(v4); vtx.Add(v6); vtx.Add(v5);
vtx.Add(v5); vtx.Add(v6); vtx.Add(v7);
uv0.Add(t0); uv0.Add(t2); uv0.Add(t1);
uv0.Add(t1); uv0.Add(t2); uv0.Add(t3);

// Side faces
vtx.Add(v0); vtx.Add(v4); vtx.Add(v1);
vtx.Add(v1); vtx.Add(v4); vtx.Add(v5);
uv0.Add(t0); uv0.Add(t2); uv0.Add(t1);
uv0.Add(t1); uv0.Add(t2); uv0.Add(t3);

vtx.Add(v1); vtx.Add(v5); vtx.Add(v3);
vtx.Add(v3); vtx.Add(v5); vtx.Add(v7);
uv0.Add(t0); uv0.Add(t2); uv0.Add(t1);
uv0.Add(t1); uv0.Add(t2); uv0.Add(t3);

vtx.Add(v2); vtx.Add(v6); vtx.Add(v0);
vtx.Add(v0); vtx.Add(v6); vtx.Add(v4);
uv0.Add(t0); uv0.Add(t2); uv0.Add(t1);
uv0.Add(t1); uv0.Add(t2); uv0.Add(t3);

vtx.Add(v3); vtx.Add(v7); vtx.Add(v2);
vtx.Add(v2); vtx.Add(v7); vtx.Add(v6);
uv0.Add(t0); uv0.Add(t2); uv0.Add(t1);
uv0.Add(t1); uv0.Add(t2); uv0.Add(t3);

var idx = Enumerable.Range(0, vtx.Count).ToList();

mesh.SetVertices(vtx);
mesh.SetUVs(0, uv0);
mesh.SetIndices(idx, MeshTopology.Triangles, 0);
mesh.RecalculateNormals();
}
var x = Width / 2;
var y = Height / 2;
var z = Depth / 2;

var v0 = new Vector3(-x, -y, -z);
var v1 = new Vector3( x, -y, -z);
var v2 = new Vector3(-x, -y, z);
var v3 = new Vector3( x, -y, z);

var v4 = new Vector3(-x, y, -z);
var v5 = new Vector3( x, y, -z);
var v6 = new Vector3(-x, y, z);
var v7 = new Vector3( x, y, z);

var t0 = new Vector2(0, 0);
var t1 = new Vector2(1, 0);
var t2 = new Vector2(0, 1);
var t3 = new Vector2(1, 1);

var vtx = new List<Vector3>();
var uv0 = new List<Vector2>();

// Bottom
vtx.Add(v0); vtx.Add(v1); vtx.Add(v2);
vtx.Add(v1); vtx.Add(v3); vtx.Add(v2);
uv0.Add(t1); uv0.Add(t0); uv0.Add(t3);
uv0.Add(t0); uv0.Add(t2); uv0.Add(t3);

// Top
vtx.Add(v4); vtx.Add(v6); vtx.Add(v5);
vtx.Add(v5); vtx.Add(v6); vtx.Add(v7);
uv0.Add(t0); uv0.Add(t2); uv0.Add(t1);
uv0.Add(t1); uv0.Add(t2); uv0.Add(t3);

// Side faces
vtx.Add(v0); vtx.Add(v4); vtx.Add(v1);
vtx.Add(v1); vtx.Add(v4); vtx.Add(v5);
uv0.Add(t0); uv0.Add(t2); uv0.Add(t1);
uv0.Add(t1); uv0.Add(t2); uv0.Add(t3);

vtx.Add(v1); vtx.Add(v5); vtx.Add(v3);
vtx.Add(v3); vtx.Add(v5); vtx.Add(v7);
uv0.Add(t0); uv0.Add(t2); uv0.Add(t1);
uv0.Add(t1); uv0.Add(t2); uv0.Add(t3);

vtx.Add(v2); vtx.Add(v6); vtx.Add(v0);
vtx.Add(v0); vtx.Add(v6); vtx.Add(v4);
uv0.Add(t0); uv0.Add(t2); uv0.Add(t1);
uv0.Add(t1); uv0.Add(t2); uv0.Add(t3);

vtx.Add(v3); vtx.Add(v7); vtx.Add(v2);
vtx.Add(v2); vtx.Add(v7); vtx.Add(v6);
uv0.Add(t0); uv0.Add(t2); uv0.Add(t1);
uv0.Add(t1); uv0.Add(t2); uv0.Add(t3);

var idx = Enumerable.Range(0, vtx.Count).ToList();

mesh.SetVertices(vtx);
mesh.SetUVs(0, uv0);
mesh.SetIndices(idx, MeshTopology.Triangles, 0);
mesh.RecalculateNormals();
}
}

} // namespace Metamesh
2 changes: 1 addition & 1 deletion Packages/jp.keijiro.metamesh/Editor/Cylinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,4 @@ public void Generate(Mesh mesh)
}
}

}
} // namespace Metamesh
11 changes: 6 additions & 5 deletions Packages/jp.keijiro.metamesh/Editor/Enums.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Metamesh
{
public enum Shape { Plane, Box, Sphere, Icosphere, Cylinder, RoundedBox }
public enum Axis { X, Y, Z }
}
namespace Metamesh {

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

} // namespace Metamesh
2 changes: 1 addition & 1 deletion Packages/jp.keijiro.metamesh/Editor/Icosphere.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ public void Generate(Mesh mesh)
}
}

}
} // namespace Metamesh
2 changes: 1 addition & 1 deletion Packages/jp.keijiro.metamesh/Editor/IcosphereBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,4 @@ public int GetMidpoint(int i1, int i2)
#endregion
}

}
} // namespace Metamesh
93 changes: 47 additions & 46 deletions Packages/jp.keijiro.metamesh/Editor/MetameshImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,65 @@
using UnityEditor.Experimental.AssetImporters;
#endif

namespace Metamesh
namespace Metamesh {

[ScriptedImporter(1, "metamesh")]
public sealed class MetameshImporter : ScriptedImporter
{
[ScriptedImporter(1, "metamesh")]
public sealed class MetameshImporter : ScriptedImporter
{
#region ScriptedImporter implementation
#region ScriptedImporter implementation

[SerializeField] Shape _shape = Shape.Box;
[SerializeField] Plane _plane = null;
[SerializeField] Box _box = new Box();
[SerializeField] Sphere _sphere = new Sphere();
[SerializeField] Icosphere _icosphere = new Icosphere();
[SerializeField] Cylinder _cylinder = new Cylinder();
[SerializeField] RoundedBox _roundedBox = new RoundedBox();
[SerializeField] Shape _shape = Shape.Box;
[SerializeField] Plane _plane = null;
[SerializeField] Box _box = new Box();
[SerializeField] Sphere _sphere = new Sphere();
[SerializeField] Icosphere _icosphere = new Icosphere();
[SerializeField] Cylinder _cylinder = new Cylinder();
[SerializeField] RoundedBox _roundedBox = new RoundedBox();

public override void OnImportAsset(AssetImportContext context)
{
var gameObject = new GameObject();
var mesh = ImportAsMesh(context.assetPath);
public override void OnImportAsset(AssetImportContext context)
{
var gameObject = new GameObject();
var mesh = ImportAsMesh(context.assetPath);

var meshFilter = gameObject.AddComponent<MeshFilter>();
meshFilter.sharedMesh = mesh;
var meshFilter = gameObject.AddComponent<MeshFilter>();
meshFilter.sharedMesh = mesh;

var meshRenderer = gameObject.AddComponent<MeshRenderer>();
meshRenderer.sharedMaterial =
AssetDatabase.GetBuiltinExtraResource<Material>("Default-Diffuse.mat");
var meshRenderer = gameObject.AddComponent<MeshRenderer>();
meshRenderer.sharedMaterial =
AssetDatabase.GetBuiltinExtraResource<Material>("Default-Diffuse.mat");

context.AddObjectToAsset("prefab", gameObject);
if (mesh != null) context.AddObjectToAsset("mesh", mesh);
context.AddObjectToAsset("prefab", gameObject);
if (mesh != null) context.AddObjectToAsset("mesh", mesh);

context.SetMainObject(gameObject);
}
context.SetMainObject(gameObject);
}

#endregion
#endregion

#region Reader implementation
#region Reader implementation

Mesh ImportAsMesh(string path)
Mesh ImportAsMesh(string path)
{
var mesh = new Mesh();
mesh.name = "Mesh";

switch (_shape)
{
var mesh = new Mesh();
mesh.name = "Mesh";

switch (_shape)
{
case Shape.Plane : _plane .Generate(mesh); break;
case Shape.Box : _box .Generate(mesh); break;
case Shape.Sphere : _sphere .Generate(mesh); break;
case Shape.Icosphere : _icosphere .Generate(mesh); break;
case Shape.Cylinder : _cylinder .Generate(mesh); break;
case Shape.RoundedBox : _roundedBox.Generate(mesh); break;
}

mesh.RecalculateBounds();
mesh.UploadMeshData(true);

return mesh;
case Shape.Plane : _plane .Generate(mesh); break;
case Shape.Box : _box .Generate(mesh); break;
case Shape.Sphere : _sphere .Generate(mesh); break;
case Shape.Icosphere : _icosphere .Generate(mesh); break;
case Shape.Cylinder : _cylinder .Generate(mesh); break;
case Shape.RoundedBox : _roundedBox.Generate(mesh); break;
}

#endregion
mesh.RecalculateBounds();
mesh.UploadMeshData(true);

return mesh;
}

#endregion
}

} // namespace Metamesh
87 changes: 44 additions & 43 deletions Packages/jp.keijiro.metamesh/Editor/MetameshImporterEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,55 @@
using UnityEditor.Experimental.AssetImporters;
#endif

namespace Metamesh
namespace Metamesh {

[CustomEditor(typeof(MetameshImporter))]
sealed class MetameshImporterEditor : ScriptedImporterEditor
{
[CustomEditor(typeof(MetameshImporter))]
sealed class MetameshImporterEditor : ScriptedImporterEditor
SerializedProperty _shape;
SerializedProperty _plane;
SerializedProperty _box;
SerializedProperty _sphere;
SerializedProperty _icosphere;
SerializedProperty _cylinder;
SerializedProperty _roundedBox;

public override void OnEnable()
{
SerializedProperty _shape;
SerializedProperty _plane;
SerializedProperty _box;
SerializedProperty _sphere;
SerializedProperty _icosphere;
SerializedProperty _cylinder;
SerializedProperty _roundedBox;

public override void OnEnable()
{
base.OnEnable();
_shape = serializedObject.FindProperty("_shape");
_plane = serializedObject.FindProperty("_plane");
_box = serializedObject.FindProperty("_box");
_sphere = serializedObject.FindProperty("_sphere");
_icosphere = serializedObject.FindProperty("_icosphere");
_cylinder = serializedObject.FindProperty("_cylinder");
_roundedBox = serializedObject.FindProperty("_roundedBox");
}
base.OnEnable();
_shape = serializedObject.FindProperty("_shape");
_plane = serializedObject.FindProperty("_plane");
_box = serializedObject.FindProperty("_box");
_sphere = serializedObject.FindProperty("_sphere");
_icosphere = serializedObject.FindProperty("_icosphere");
_cylinder = serializedObject.FindProperty("_cylinder");
_roundedBox = serializedObject.FindProperty("_roundedBox");
}

public override void OnInspectorGUI()
public override void OnInspectorGUI()
{
serializedObject.Update();

EditorGUILayout.PropertyField(_shape);

switch ((Shape)_shape.enumValueIndex)
{
serializedObject.Update();

EditorGUILayout.PropertyField(_shape);

switch ((Shape)_shape.enumValueIndex)
{
case Shape.Plane : EditorGUILayout.PropertyField(_plane); break;
case Shape.Box : EditorGUILayout.PropertyField(_box); break;
case Shape.Sphere : EditorGUILayout.PropertyField(_sphere); break;
case Shape.Icosphere : EditorGUILayout.PropertyField(_icosphere); break;
case Shape.Cylinder : EditorGUILayout.PropertyField(_cylinder); break;
case Shape.RoundedBox: EditorGUILayout.PropertyField(_roundedBox); break;
}

serializedObject.ApplyModifiedProperties();
ApplyRevertGUI();
case Shape.Plane : EditorGUILayout.PropertyField(_plane); break;
case Shape.Box : EditorGUILayout.PropertyField(_box); break;
case Shape.Sphere : EditorGUILayout.PropertyField(_sphere); break;
case Shape.Icosphere : EditorGUILayout.PropertyField(_icosphere); break;
case Shape.Cylinder : EditorGUILayout.PropertyField(_cylinder); break;
case Shape.RoundedBox: EditorGUILayout.PropertyField(_roundedBox); break;
}

[MenuItem("Assets/Create/Metamesh")]
public static void CreateNewAsset()
=> ProjectWindowUtil.CreateAssetWithContent
("New Metamesh.metamesh", "");
serializedObject.ApplyModifiedProperties();
ApplyRevertGUI();
}

[MenuItem("Assets/Create/Metamesh")]
public static void CreateNewAsset()
=> ProjectWindowUtil.CreateAssetWithContent
("New Metamesh.metamesh", "");
}

} // namespace Metamesh
Loading

0 comments on commit 587ec14

Please sign in to comment.