LoginSignup
5
1

More than 1 year has passed since last update.

マイクラの土ブロック

Posted at
NewBehaviourScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour{

    public Material material;
    List<int> triangles     = new List<int>();
    List<Vector3> vertices  = new List<Vector3>();
    List<Vector2> uv        = new List<Vector2>();

    void Start(){
        Append(new Voxel(0,0,0,0));
        Init(new GameObject());
    }
    void Append(params Voxel[] voxels){
        foreach(Voxel v in voxels){
            foreach(int t in v.triangles) triangles.Add(vertices.Count+t);
            vertices.AddRange(v.vertices);
            uv.AddRange(v.uv);
        }
    }
    void Init(GameObject g){
        var f = g.AddComponent<MeshFilter>();
        var r = g.AddComponent<MeshRenderer>();
        var m = new Mesh(){
            vertices    = vertices.ToArray(),
            uv          = uv.ToArray(),
            triangles   = triangles.ToArray(),
        };
        f.mesh      = m;
        r.material  = material;
        m.RecalculateNormals();
    }
}

public class Voxel{

    static List<int> data   = new List<int>();
    static int[] order      = {6,7,2,3, 2,3,0,1, 0,1,4,5, 6,2,4,0, 7,6,5,4, 3,7,1,5};
    static int slice        = 8;
    static Voxel(){
        for(int i=0;i<order.Length;i+=4){
            data.AddRange(new []{i,i+1,i+2,i+3,i+2,i+1});
        }
    }

    public List<int> triangles      = data;
    public List<Vector3> vertices   = new List<Vector3>();
    public List<Vector2> uv         = new List<Vector2>();
    public Voxel(int x,int y,int z,int type){
        var cube = new Vector3[8];
        var face = new []{0,1,2,1,1,1};
        for(int i=0;i<8;i++){
            cube[i] = new Vector3((i&1)+x,(i>>1&1)+y,(i>>2&1)+z);
        }
        for(int i=0;i<order.Length;i++){
            vertices.Add(cube[order[i]]);
            uv.Add(new Vector2(type+(i&1),slice-(i>>1&1)-face[i>>2])/slice);
        }
    }
}
5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1