Last active
July 29, 2020 12:01
-
-
Save samloeschen/0a081537991f7754041871461d917280 to your computer and use it in GitHub Desktop.
AnimationCurveToLUT
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 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