Skip to content

Instantly share code, notes, and snippets.

@ogrew
Created May 13, 2022 15:05
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 ogrew/35c56b0c4a2bb15cca7142505f1fa1bf to your computer and use it in GitHub Desktop.
Save ogrew/35c56b0c4a2bb15cca7142505f1fa1bf to your computer and use it in GitHub Desktop.
DrawMeshInstancedを試す(その1)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
public class DrawMeshInstanced_demo : MonoBehaviour
{
[SerializeField] private int count = 1000;
[SerializeField] private float radius = 5;
[SerializeField] private Mesh copyMesh;
[SerializeField] private Material material;
private Matrix4x4[] matrices;
private MaterialPropertyBlock block;
private void Start()
{
SetUp();
}
private void Update()
{
Graphics.DrawMeshInstanced(copyMesh, 0, material, matrices, count, block);
}
private void SetUp()
{
matrices = new Matrix4x4[count];
Vector4[] colors = new Vector4[count];
block = new MaterialPropertyBlock();
for (int i = 0; i < count; i++)
{
Vector3 pos = Random.insideUnitSphere * radius;
Quaternion rot = Quaternion.Euler(
Random.Range(-180, 180),
Random.Range(-180, 180),
Random.Range(-180, 180)
);
Vector3 size = Vector3.one;
matrices[i].SetTRS(pos, rot, size);
matrices[i] = transform.localToWorldMatrix * matrices[i];
colors[i] = Color.Lerp(Color.magenta, Color.cyan, Random.value);
}
block.SetVectorArray("_Colors", colors);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment