KITASENJU DESIGN BLOG

memo, html, javascript, unity

DrawMeshIndirectedを何度か呼ぶ

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class Duplicator : MonoBehaviour {

    [SerializeField] protected Mesh _mesh;
    [SerializeField] protected Material _mat;
    protected List<Matrix4x4[]> _matricesList;
    protected List<Vector4[]> _colorsList;
    protected List<PieceData[]> _dataList;
    protected MaterialPropertyBlock _propertyBlock;
   
    public const int TOTAL = 4000;
    public const int UNIT = 1000;//1 draw callあたり
    [SerializeField] private int _iteration=0;



    void Start(){

        _propertyBlock = new MaterialPropertyBlock();
        
        _iteration = 4;
        _matricesList = new List<Matrix4x4[]>();
        _colorsList = new List<Vector4[]>();
        _dataList = new List<PieceData[]>();

        int index = 0;

        for(int i=0;i<_iteration;i++){

            var matrices =new Matrix4x4[UNIT];
            var colors =new Vector4[UNIT];
            var data =new PieceData[UNIT];
        
            for(int j=0;j<UNIT;j++){

                matrices[j] = Matrix4x4.identity;
                data[j] = new PieceData();

                data[j].pos.x = 12f * ( Random.value-0.5f );
                data[j].pos.y = 12f * ( Random.value-0.5f );
                data[j].pos.z = 12f * ( Random.value-0.5f );

                data[j].rot = Quaternion.Euler(
                    Random.value * 360f,
                    Random.value * 360f,
                    Random.value * 360f
                );
                //_uvs[_count] = SpriteUV.GetUv(Mathf.FloorToInt(Random.value*6),4,4);
                colors[j] = new Vector4(
                    Random.value,
                    Random.value,
                    Random.value,
                    1f
                );

                index++;

            }

            _matricesList.Add(matrices);
            _colorsList.Add(colors);
            _dataList.Add(data);

        }

    }


    void Update(){

        for(int j=0;j<_iteration;j++){

            var matrices    = _matricesList[j];
            var colors      = _colorsList[j];
            var data        = _dataList[j];

            for (int i = 0; i < matrices.Length; i++)
            {
                //ひとつひとつコピー
                matrices[i].SetTRS( 
                    data[i].pos,
                    data[i].rot,
                    data[i].scale
                );
                matrices[i] = transform.localToWorldMatrix * matrices[i];

            }

            _DrawMesh(colors,matrices);

        }

    }

    private void _DrawMesh(Vector4[] colors, Matrix4x4[] matrices){
        
            _propertyBlock.SetVectorArray("_Color", colors);
            Graphics.DrawMeshInstanced(
                    _mesh, 
                    0, 
                    _mat, 
                    matrices, 
                    UNIT, 
                    _propertyBlock, 
                    ShadowCastingMode.Off, 
                    false, 
                    gameObject.layer
            );

    }

}
"FOOTER"