« Kerbal Space ProgramのパーツMODを作る 1 | トップページ | Kerbal Space ProgramのパーツMODを作る 3 »

2015年7月14日 (火)

Kerbal Space ProgramのパーツMODを作る 2

参考リンク

こんなC#プログラムをUnityで実行してオブジェクトを生成。
using UnityEngine;
using System.Collections;
public class makemesh : MonoBehaviour {
 private const int DIV = 15;
 private const float diameter = 0.625f;
 private const float flamewidth = 0.05f;
 private Mesh getMeshTopOfSemicircle( float y)
 {
 Mesh meshTopOfSemicircle = new Mesh ();
 Vector3[] vertices = new Vector3[(DIV + 1) * 2];
 Vector2[] UV = new Vector2[(DIV + 1) * 2];
 int[] triangles = new int[DIV*2*3];
 for (int i=0; i<DIV*2; i+=2) {
 vertices[i]   = new Vector3(  diameter/2            *Mathf.Cos ( Mathf.PI*i/DIV/2), y,  diameter/2            *Mathf.Sin (Mathf.PI*i/DIV/2));
 vertices[i+1] = new Vector3( (diameter/2-flamewidth)*Mathf.Cos ( Mathf.PI*i/DIV/2), y, (diameter/2-flamewidth)*Mathf.Sin (Mathf.PI*i/DIV/2));
 
 UV[i]   = new Vector2( ((float)i)/DIV, 0.0f);
 UV[i+1] = new Vector2( ((float)i)/DIV, 1.0f);
 
 triangles[i*3]   = i+2;
 triangles[i*3+1] = i+1;
 triangles[i*3+2] = i;
 
 triangles[i*3+3] = i+1;
 triangles[i*3+4] = i+2;
 triangles[i*3+5] = i+3;
 }
 vertices[DIV*2]   = new Vector3(  diameter/2            *Mathf.Cos ( Mathf.PI), y,  diameter/2            *Mathf.Sin (Mathf.PI));
 vertices[DIV*2+1] = new Vector3( (diameter/2-flamewidth)*Mathf.Cos ( Mathf.PI), y, (diameter/2-flamewidth)*Mathf.Sin (Mathf.PI));
 UV[DIV*2]   = new Vector2( 1.0f, 0.0f);
 UV[DIV*2+1] = new Vector2( 1.0f, 1.0f);
 meshTopOfSemicircle.vertices  = vertices;
 meshTopOfSemicircle.uv        = UV;
 meshTopOfSemicircle.triangles = triangles;
 return meshTopOfSemicircle;
 }
 private Mesh getMeshBottomOfSemicircle( float y)
 {
 Mesh meshBottomOfSemicircle = new Mesh ();
 
 Vector3[] vertices = new Vector3[(DIV + 1) * 2];
 Vector2[] UV = new Vector2[(DIV + 1) * 2];
 int[] triangles = new int[DIV*2*3];
 
 for (int i=0; i<DIV*2; i+=2) {
 vertices[i]   = new Vector3(  diameter/2            *Mathf.Cos ( Mathf.PI*i/DIV/2), y,  diameter/2            *Mathf.Sin (Mathf.PI*i/DIV/2));
 vertices[i+1] = new Vector3( (diameter/2-flamewidth)*Mathf.Cos ( Mathf.PI*i/DIV/2), y, (diameter/2-flamewidth)*Mathf.Sin (Mathf.PI*i/DIV/2));
 
 UV[i]   = new Vector2( ((float)i)/DIV, 0.0f);
 UV[i+1] = new Vector2( ((float)i)/DIV, 1.0f);
 
 triangles[i*3]   = i;
 triangles[i*3+1] = i+1;
 triangles[i*3+2] = i+2;
 
 triangles[i*3+3] = i+3;
 triangles[i*3+4] = i+2;
 triangles[i*3+5] = i+1;
 }
 vertices[DIV*2]   = new Vector3(  diameter/2            *Mathf.Cos ( Mathf.PI), y,  diameter/2            *Mathf.Sin (Mathf.PI));
 vertices[DIV*2+1] = new Vector3( (diameter/2-flamewidth)*Mathf.Cos ( Mathf.PI), y, (diameter/2-flamewidth)*Mathf.Sin (Mathf.PI));
 
 UV[DIV*2]   = new Vector2( 1.0f, 0.0f);
 UV[DIV*2+1] = new Vector2( 1.0f, 1.0f);
 
 meshBottomOfSemicircle.vertices  = vertices;
 meshBottomOfSemicircle.uv        = UV;
 meshBottomOfSemicircle.triangles = triangles;
 
 return meshBottomOfSemicircle;
 }
 private Mesh getMeshOuterOfSemicircle( float y)
 {
 Mesh meshOuterOfSemicircle = new Mesh ();
 
 Vector3[] vertices = new Vector3[(DIV + 1) * 2];
 Vector2[] UV = new Vector2[(DIV + 1) * 2];
 int[] triangles = new int[DIV*2*3];
 
 for (int i=0; i<DIV*2; i+=2) {
 vertices[i]   = new Vector3( diameter/2*Mathf.Cos ( Mathf.PI*i/DIV/2), y           , diameter/2*Mathf.Sin (Mathf.PI*i/DIV/2));
 vertices[i+1] = new Vector3( diameter/2*Mathf.Cos ( Mathf.PI*i/DIV/2), y+flamewidth, diameter/2*Mathf.Sin (Mathf.PI*i/DIV/2));
 UV[i]   = new Vector2( ((float)i)/DIV, 0.0f);
 UV[i+1] = new Vector2( ((float)i)/DIV, 1.0f);
 
 triangles[i*3]   = i;
 triangles[i*3+1] = i+1;
 triangles[i*3+2] = i+2;
 
 triangles[i*3+3] = i+3;
 triangles[i*3+4] = i+2;
 triangles[i*3+5] = i+1;
 }
 vertices[DIV*2]   = new Vector3( diameter/2*Mathf.Cos ( Mathf.PI), y           , diameter/2*Mathf.Sin (Mathf.PI));
 vertices[DIV*2+1] = new Vector3( diameter/2*Mathf.Cos ( Mathf.PI), y+flamewidth, diameter/2*Mathf.Sin (Mathf.PI));
 UV[DIV*2]   = new Vector2( 1.0f, 0.0f);
 UV[DIV*2+1] = new Vector2( 1.0f, 1.0f);
 
 meshOuterOfSemicircle.vertices  = vertices;
 meshOuterOfSemicircle.uv        = UV;
 meshOuterOfSemicircle.triangles = triangles;
 
 return meshOuterOfSemicircle;
 }
 private Mesh getMeshInnerOfSemicircle( float y)
 {
 Mesh meshInnerOfSemicircle = new Mesh ();
 
 Vector3[] vertices = new Vector3[(DIV + 1) * 2];
 Vector2[] UV = new Vector2[(DIV + 1) * 2];
 int[] triangles = new int[DIV*2*3];
 
 for (int i=0; i<DIV*2; i+=2) {
 vertices[i]   = new Vector3( (diameter/2-flamewidth)*Mathf.Cos ( Mathf.PI*i/DIV/2), y           , (diameter/2-flamewidth)*Mathf.Sin (Mathf.PI*i/DIV/2));
 vertices[i+1] = new Vector3( (diameter/2-flamewidth)*Mathf.Cos ( Mathf.PI*i/DIV/2), y+flamewidth, (diameter/2-flamewidth)*Mathf.Sin (Mathf.PI*i/DIV/2));
 
 UV[i]   = new Vector2( ((float)i)/DIV, 0.0f);
 UV[i+1] = new Vector2( ((float)i)/DIV, 1.0f);
 
 triangles[i*3]   = i+2;
 triangles[i*3+1] = i+1;
 triangles[i*3+2] = i;
 
 triangles[i*3+3] = i+1;
 triangles[i*3+4] = i+2;
 triangles[i*3+5] = i+3;
 }
 vertices[DIV*2]   = new Vector3( (diameter/2-flamewidth)*Mathf.Cos ( Mathf.PI), y           , (diameter/2-flamewidth)*Mathf.Sin (Mathf.PI));
 vertices[DIV*2+1] = new Vector3( (diameter/2-flamewidth)*Mathf.Cos ( Mathf.PI), y+flamewidth, (diameter/2-flamewidth)*Mathf.Sin (Mathf.PI));
 
 UV[DIV*2]   = new Vector2( 1.0f, 0.0f);
 UV[DIV*2+1] = new Vector2( 1.0f, 1.0f);
 
 meshInnerOfSemicircle.vertices  = vertices;
 meshInnerOfSemicircle.uv        = UV;
 meshInnerOfSemicircle.triangles = triangles;
 
 return meshInnerOfSemicircle;
 }
 private Mesh getMeshFaceOfSemicircle( float x, float y, float z)
 {
 Mesh meshFaceOfSemicircle = new Mesh ();
 
 Vector3[] vertices = new Vector3[4];
 Vector2[] UV = new Vector2[4];
 int[] triangles = new int[6];
 vertices [0] = new Vector3 (x, y, z);
 vertices [1] = new Vector3 (x + flamewidth, y, z);
 vertices [2] = new Vector3 (x + flamewidth, y + flamewidth, z);
 vertices [3] = new Vector3 (x, y + flamewidth, z);
 UV [0] = new Vector2 (0f, 0f);
 UV [1] = new Vector2 (1f, 0f);
 UV [2] = new Vector2 (1f, 1f);
 UV [3] = new Vector2 (0f, 1f);
 triangles [0] = 3;
 triangles [1] = 1;
 triangles [2] = 0;
 triangles [3] = 3;
 triangles [4] = 2;
 triangles [5] = 1;
 meshFaceOfSemicircle.vertices  = vertices;
 meshFaceOfSemicircle.uv        = UV;
 meshFaceOfSemicircle.triangles = triangles;
 
 return meshFaceOfSemicircle;
 }
 private Mesh getMeshOfPole()
 {
 Mesh meshOfPole = new Mesh ();
 
 Vector3[] vertices = new Vector3[8];
 Vector2[] UV = new Vector2[8];
 int[] triangles = new int[36];
 
 vertices [0] = new Vector3 (flamewidth/2, 0.5f, diameter/2);
 vertices [1] = new Vector3 (flamewidth/2, -0.5f, diameter/2);
 vertices [2] = new Vector3 (-flamewidth/2, -0.5f, diameter/2);
 vertices [3] = new Vector3 (-flamewidth/2, 0.5f, diameter/2);
 vertices [4] = new Vector3 (flamewidth/2, 0.5f, diameter/2-flamewidth);
 vertices [5] = new Vector3 (flamewidth/2, -0.5f, diameter/2-flamewidth);
 vertices [6] = new Vector3 (-flamewidth/2, -0.5f, diameter/2-flamewidth);
 vertices [7] = new Vector3 (-flamewidth/2, 0.5f, diameter/2-flamewidth);
 UV [0] = new Vector2 (0f, 0f);
 UV [1] = new Vector2 (0f, 0f);
 UV [2] = new Vector2 (0f, 0f);
 UV [3] = new Vector2 (0f, 0f);
 UV [4] = new Vector2 (0f, 0f);
 UV [5] = new Vector2 (0f, 0f);
 UV [6] = new Vector2 (0f, 0f);
 UV [7] = new Vector2 (0f, 0f);
 triangles [0] = 3;
 triangles [1] = 2;
 triangles [2] = 1;
 triangles [3] = 1;
 triangles [4] = 0;
 triangles [5] = 3;
 triangles [6] = 4;
 triangles [7] = 5;
 triangles [8] = 6;
 triangles [9] = 6;
 triangles [10] = 7;
 triangles [11] = 4;
 triangles [12] = 3;
 triangles [13] = 0;
 triangles [14] = 4;
 triangles [15] = 4;
 triangles [16] = 7;
 triangles [17] = 3;
 triangles [18] = 1;
 triangles [19] = 2;
 triangles [20] = 6;
 triangles [21] = 6;
 triangles [22] = 5;
 triangles [23] = 1;
 triangles [24] = 0;
 triangles [25] = 1;
 triangles [26] = 5;
 triangles [27] = 5;
 triangles [28] = 4;
 triangles [29] = 0;
 triangles [30] = 2;
 triangles [31] = 3;
 triangles [32] = 7;
 triangles [33] = 7;
 triangles [34] = 6;
 triangles [35] = 2;
 meshOfPole.vertices  = vertices;
 meshOfPole.uv        = UV;
 meshOfPole.triangles = triangles;
 
 return meshOfPole;
 }
 void Start () {
 CombineInstance [] combine = new CombineInstance[13];
 combine [0].mesh = getMeshTopOfSemicircle (-0.5f);
 combine [1].mesh = getMeshBottomOfSemicircle (-0.5f+flamewidth);
 combine [2].mesh = getMeshTopOfSemicircle (0.5f-flamewidth);
 combine [3].mesh = getMeshBottomOfSemicircle (0.5f);
 combine [4].mesh = getMeshOuterOfSemicircle (-0.5f);
 combine [5].mesh = getMeshOuterOfSemicircle (0.5f - flamewidth);
 combine [6].mesh = getMeshInnerOfSemicircle (-0.5f);
 combine [7].mesh = getMeshInnerOfSemicircle (0.5f - flamewidth);
 combine [8].mesh = getMeshFaceOfSemicircle (-diameter/2 , -0.5f, 0);
 combine [9].mesh = getMeshFaceOfSemicircle (-diameter/2 , 0.5f-flamewidth, 0);
 combine [10].mesh = getMeshFaceOfSemicircle (diameter/2-flamewidth, -0.5f, 0);
 combine [11].mesh = getMeshFaceOfSemicircle (diameter/2-flamewidth, 0.5f-flamewidth, 0);
 combine [12].mesh = getMeshOfPole ();
 Mesh mesh = new Mesh ();
 mesh.CombineMeshes (combine, true, false);
 mesh.RecalculateNormals();
 mesh.RecalculateBounds();
 
 GetComponent<MeshFilter>().sharedMesh = mesh;
 GetComponent<MeshFilter>().sharedMesh.name = "myMesh";
 UnityEditor.AssetDatabase.CreateAsset (mesh, "Assets/mesh.asset");
 
 }
 
 void Update () {
 
 }
}
Kerbal Space Programでパーツを読み込めた。とりあえず今日は満足。

Screenshot9

« Kerbal Space ProgramのパーツMODを作る 1 | トップページ | Kerbal Space ProgramのパーツMODを作る 3 »

コメント

コメントを書く

コメントは記事投稿者が公開するまで表示されません。

(ウェブ上には掲載しません)

トラックバック

この記事のトラックバックURL:
http://app.f.cocolog-nifty.com/t/trackback/1499066/60730984

この記事へのトラックバック一覧です: Kerbal Space ProgramのパーツMODを作る 2:

« Kerbal Space ProgramのパーツMODを作る 1 | トップページ | Kerbal Space ProgramのパーツMODを作る 3 »