Skip to content

Instantly share code, notes, and snippets.

@kaiware007
Last active November 30, 2019 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaiware007/57b23f084c8138152c12f9468ee2b5dd to your computer and use it in GitHub Desktop.
Save kaiware007/57b23f084c8138152c12f9468ee2b5dd to your computer and use it in GitHub Desktop.
立方体を等分割したメッシュを作成するEditor拡張。GPUパーティクル芸用なのでUV座標は入ってないし通常のシェーダではまともに表示されない可能性大。Editorフォルダに入れてメニューからCustom/Cube Mesh Makerを選択して使う。
using UnityEngine;
using UnityEditor;
public class CubeMeshMakerWizard : ScriptableWizard
{
public string filename = "mesh";
public int xnum = 10;
public int ynum = 10;
public int znum = 10;
[MenuItem("Custom/Cube Mesh Maker")]
static void Init()
{
DisplayWizard<CubeMeshMakerWizard>("Cube Mesh Maker");
}
/// <summary>
/// Createボタンが押された
/// </summary>
private void OnWizardCreate()
{
MeshMaker meshMaker = new MeshMaker();
Mesh mesh = meshMaker.GenerageMesh(xnum, ynum, znum);
AssetDatabase.CreateAsset(mesh, "Assets/" + filename + ".asset");
AssetDatabase.SaveAssets();
}
}
using System.Collections.Generic;
using UnityEngine;
public class MeshMaker {
protected virtual bool IsInShape(Vector3 pos)
{
return true;
}
public Mesh GenerageMesh(int xnum, int ynum, int znum)
{
Mesh mesh = new Mesh();
int vertexCount = xnum * ynum * znum;
float xdiv = 1f / xnum;
float ydiv = 1f / ynum;
float zdiv = 1f / znum;
List<Vector3> vertices = new List<Vector3>();
List<int> indices = new List<int>();
int index = 0;
for (int i = 0; i < vertexCount; i++)
{
Vector3 pos = new Vector3((i % xnum) * xdiv, (i / xnum % ynum) * ydiv, (i / (xnum * ynum) % znum) * zdiv);
if (IsInShape(pos))
{
vertices.Add(pos);
indices.Add(index++);
}
}
mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
mesh.vertices = vertices.ToArray();
mesh.SetIndices(indices.ToArray(), MeshTopology.Points, 0);
mesh.RecalculateBounds();
return mesh;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment