Created
December 29, 2023 14:06
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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