Skip to content

Instantly share code, notes, and snippets.

@samloeschen
Last active July 29, 2020 12:01
Show Gist options
  • Select an option

  • Save samloeschen/0a081537991f7754041871461d917280 to your computer and use it in GitHub Desktop.

Select an option

Save samloeschen/0a081537991f7754041871461d917280 to your computer and use it in GitHub Desktop.
AnimationCurveToLUT
using UnityEngine;
using Unity.Mathematics;
public static class AnimationCurveExtensions {
public static Texture2D ToLUT(this AnimationCurve curve, int width, bool apply = true) {
// create texture
Texture2D tex = new Texture2D(width, 1, TextureFormat.R8, 0, true);
float x = 0f;
float t = 0f;
float texelSize = 1f / (width - 1);
for (int i = 0; i < width; i++) {
t = math.saturate(curve.Evaluate(x));
tex.SetPixel(i, 0, new Color(t, t, t, t));
x += texelSize;
}
// upload to gpu
if (apply) {
tex.Apply();
}
return tex;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment