[4507] | 1 | using System; |
---|
| 2 | using System.Collections.Generic; |
---|
| 3 | using System.Linq; |
---|
| 4 | using System.Text; |
---|
| 5 | |
---|
| 6 | using Microsoft.Xna.Framework; |
---|
| 7 | using Microsoft.Xna.Framework.Graphics; |
---|
| 8 | |
---|
| 9 | namespace Krypton |
---|
| 10 | { |
---|
| 11 | public static class LightTextureBuilder |
---|
| 12 | { |
---|
| 13 | /// <summary> |
---|
| 14 | /// Create a Point Light based on a size. |
---|
| 15 | /// </summary> |
---|
| 16 | /// <param name="device">Your game's GraphicsDevice</param> |
---|
| 17 | /// <param name="size">Maximum Size</param> |
---|
| 18 | /// <returns>Light Texture</returns> |
---|
| 19 | public static Texture2D CreatePointLight(GraphicsDevice device, int size) |
---|
| 20 | { |
---|
| 21 | return CreateConicLight(device, size, MathHelper.TwoPi, 0); |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | /// <summary> |
---|
| 25 | /// Create a Conic Light based on the size, and field of view. |
---|
| 26 | /// </summary> |
---|
| 27 | /// <param name="device">Your game's GraphicsDevice</param> |
---|
| 28 | /// <param name="size">Maximum Size</param> |
---|
| 29 | /// <param name="FOV">Maximum Field of View</param> |
---|
| 30 | /// <returns>Light Texture</returns> |
---|
| 31 | public static Texture2D CreateConicLight(GraphicsDevice device, int size, float FOV) |
---|
| 32 | { |
---|
| 33 | return CreateConicLight(device, size, FOV, 0); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | /// <summary> |
---|
| 37 | /// Create a Conic Light based on the size, field of view, and near plane distance. |
---|
| 38 | /// </summary> |
---|
| 39 | /// <param name="device">Your game's GraphicsDevice</param> |
---|
| 40 | /// <param name="size">Maximum size</param> |
---|
| 41 | /// <param name="FOV">Maximum Field of View</param> |
---|
| 42 | /// <param name="nearPlaneDistance">Prevents texture from being drawn at this plane distance, originating from the center of light</param> |
---|
| 43 | /// <returns>Light Texture</returns> |
---|
| 44 | public static Texture2D CreateConicLight(GraphicsDevice device, int size, float FOV, float nearPlaneDistance) |
---|
| 45 | { |
---|
| 46 | /*if (!IsPowerOfTwo(size)) |
---|
| 47 | throw new Exception("The size must be a power of 2");*/ |
---|
| 48 | float[,] Data = new float[size, size]; |
---|
| 49 | |
---|
| 50 | float center = size / 2; |
---|
| 51 | |
---|
| 52 | FOV = FOV / 2; |
---|
| 53 | |
---|
| 54 | for (int x = 0; x < size; x++) |
---|
| 55 | for (int y = 0; y < size; y++) |
---|
| 56 | { |
---|
| 57 | float Distance = Vector2.Distance(new Vector2(x, y), new Vector2(center)); |
---|
| 58 | |
---|
| 59 | Vector2 Difference = new Vector2(x, y) - new Vector2(center); |
---|
| 60 | float Angle = (float)Math.Atan2(Difference.Y, Difference.X); |
---|
| 61 | |
---|
| 62 | if (Distance <= center && Distance >= nearPlaneDistance && Math.Abs(Angle) <= FOV) |
---|
| 63 | Data[x, y] = (center - Distance) / center; |
---|
| 64 | else |
---|
| 65 | Data[x, y] = 0; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | Texture2D tex = new Texture2D(device, size, size); |
---|
| 69 | |
---|
| 70 | Color[] Data1D = new Color[size * size]; |
---|
| 71 | for (int x = 0; x < size; x++) |
---|
| 72 | for (int y = 0; y < size; y++) |
---|
| 73 | Data1D[x + y * size] = new Color(new Vector3(Data[x, y])); |
---|
| 74 | |
---|
| 75 | tex.SetData<Color>(Data1D); |
---|
| 76 | |
---|
| 77 | return tex; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | /// <summary> |
---|
| 81 | /// Math helper to determine if integer is a power of two |
---|
| 82 | /// </summary> |
---|
| 83 | /// <param name="x">Integer value</param> |
---|
| 84 | private static bool IsPowerOfTwo(int x) |
---|
| 85 | { |
---|
| 86 | return (x & (x - 1)) == 0; |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | } |
---|