Skip to content

Instantly share code, notes, and snippets.

@Kellojo
Created December 24, 2019 11:55
Show Gist options
  • Save Kellojo/2bd1f4f8f2d119d1c50f73096db46194 to your computer and use it in GitHub Desktop.
Save Kellojo/2bd1f4f8f2d119d1c50f73096db46194 to your computer and use it in GitHub Desktop.
A simple helper class, which allows you to create radial gradients. This can easily be applied to a texture or to a noise function.
using UnityEngine;
public class Gradient {
/// <summary>
/// Evaluated a radial gradient at the given x,y position
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="sizeX"></param>
/// <param name="sizeY"></param>
/// <param name="minRadius"></param>
/// <param name="strength"></param>
/// <returns></returns>
public static float RadialGradient(int x, int y, int sizeX, int sizeY, float minRadius, float strength) {
float gradientSize = (sizeX + sizeY) / 2;
float distFromCenter = Vector2.Distance(
new Vector2(sizeX * 0.5f, sizeY * 0.5f),
new Vector2(x, y)
) - minRadius;
if (distFromCenter < 0) {
distFromCenter = 0;
}
return 1 - (distFromCenter / gradientSize) * strength;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment