Skip to content

Instantly share code, notes, and snippets.

@hman278
Created December 29, 2023 14:06
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 hman278/6f99601838937933653c80b38153cfff to your computer and use it in GitHub Desktop.
Save hman278/6f99601838937933653c80b38153cfff to your computer and use it in GitHub Desktop.
I made this on accident, maybe someone finds the useful (Use RenderMeshIndirect for speed)
using System.Collections;
using UnityEngine;
public class NoInstancing : MonoBehaviour
{
private struct MeshData
{
public Vector3 offset;
public Quaternion rotation;
public Color color;
};
[SerializeField] private Material _material;
[SerializeField] private Mesh _mesh;
private short _gridSize = 64;
private RenderParams _renderParams;
private MeshData[,] _meshDatas;
private Color[,] _oldColors;
private Color[,] _newColors;
private void Start()
{
_renderParams = new RenderParams(_material);
_meshDatas = new MeshData[_gridSize, _gridSize];
_oldColors = new Color[_gridSize, _gridSize];
_newColors = new Color[_gridSize, _gridSize];
//StartCoroutine(LerpToNewEmissionColors());
}
float alpha = 0f;
private void Update()
{
alpha = alpha < 1f ? alpha + Time.deltaTime * 500f : 0f;
for (int i = 0; i < _gridSize; ++i)
{
for (int j = 0; j < _gridSize; ++j)
{
if (alpha == 0f)
{
_oldColors[i, j] = _newColors[i, j];
_newColors[i, j] = GetRandomEmissionColor(1f);
}
// Update mesh colors
_meshDatas[i, j].color = Color.Lerp(_oldColors[i, j], _newColors[i, j], alpha);
_renderParams.matProps = new MaterialPropertyBlock();
_renderParams.matProps.SetColor("_EmissionColor", _meshDatas[i, j].color);
//
Graphics.RenderMesh(_renderParams, _mesh, 0, Matrix4x4.Translate(new Vector3(i, j, 0)));
}
}
}
// It is important for one color to be 0
private Color GetRandomEmissionColor(float colorIntensity)
{
float r = Random.Range(0, 2) * colorIntensity;
float g = Random.Range(0, 2) * colorIntensity;
float b = 0f;
if (r == 0 || g == 0)
{
b = Random.Range(0, 2) * colorIntensity;
if (r == 0 && g == 0)
{
b = colorIntensity;
}
}
return new Color(r, g, b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment