KITASENJU DESIGN BLOG

memo, html, javascript, unity

How to use Graphics.DrawMesh

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



public class Duplicator : MonoBehaviour
{

    [SerializeField] private Mesh _mesh;
    [SerializeField] private Material _mat;

    private MaterialPropertyBlock _block;
    private Matrix4x4 _matrix;

    // Start is called before the first frame update
    void Start()
    {
        _matrix = new Matrix4x4();
        _block = new MaterialPropertyBlock();
    }

    // Update is called once per frame
    void Update()
    {

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

            var amp = (float)i/10f;
            var pos = new Vector3(
                amp*Mathf.Cos( i/10f ),
                amp*Mathf.Sin( i/10f ),
                0
            );

            var rot = Quaternion.Euler(
                0,0,i*25f
            );

            var scl = new Vector3(
                2f,2f,2f
            );
            _matrix.SetTRS(
                pos,
                rot,
                scl
            );
            _block.SetVector("_Color",new Color(
                Random.value,
                Random.value,
                Random.value,
                1f
            ));

            //Graphics.DrawMesh(Mesh mesh, Matrix4x4 matrix, Material material, int layer, Camera camera, int submeshIndex, MaterialPropertyBlock properties);
            Graphics.DrawMesh(
                _mesh,
                _matrix,
                _mat,
                gameObject.layer,
                Camera.main,
                0,
                _block
            );

        }

    }
}

"FOOTER"