Skip to content

Commit 3f13910

Browse files
committed
Add new texture extension methods
1 parent b9460ba commit 3f13910

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using UnityEngine;
2+
3+
namespace Zigurous.Graphics
4+
{
5+
/// <summary>
6+
/// Extension methods for textures.
7+
/// </summary>
8+
public static class TextureExtensions
9+
{
10+
/// <summary>
11+
/// Maps the UV coordinates in the range [0..1] to pixel coordinates
12+
/// in the range [0..width-1] and [0..height-1].
13+
/// </summary>
14+
/// <param name="texture">The texture to get the pixel coordinates from.</param>
15+
/// <param name="u">The UV coordinate in the x-axis.</param>
16+
/// <param name="v">The UV coordinate in the y-axis.</param>
17+
/// <param name="px">The output pixel coordinate in the x-axis.</param>
18+
/// <param name="py">The output pixel coordinate in the y-axis.</param>
19+
public static void GetPixelCoordinates(this Texture2D texture, float u, float v, out int px, out int py)
20+
{
21+
px = (int)Mathf.Lerp(0, texture.width - 1, u);
22+
py = (int)Mathf.Lerp(0, texture.height - 1, v);
23+
}
24+
25+
/// <summary>
26+
/// Maps the pixel coordinates in the range [0..width-1] and
27+
/// [0..height-1] to UV coordinates in the range [0..1].
28+
/// </summary>
29+
/// <param name="texture">The texture to get the UV coordinates from.</param>
30+
/// <param name="px">The pixel coordinate in the x-axis.</param>
31+
/// <param name="py">The pixel coordinate in the y-axis.</param>
32+
/// <param name="u">The output UV coordinate in the x-axis.</param>
33+
/// <param name="v">The output UV coordinate in the y-axis.</param>
34+
public static void GetUVCoordinates(this Texture2D texture, int px, int py, out float u, out float v)
35+
{
36+
u = Mathf.InverseLerp(0, texture.width - 1, px);
37+
v = Mathf.InverseLerp(0, texture.height - 1, py);
38+
}
39+
40+
/// <summarys>
41+
/// Gets the pixel color at the specified UV coordinates.
42+
/// </summary>
43+
/// <param name="texture">The texture to sample from.</param>
44+
/// <param name="u">The UV coordinate in the x-axis.</param>
45+
/// <param name="v">The UV coordinate in the y-axis.</param>
46+
/// <returns>The pixel color.</returns>
47+
public static Color Sample(this Texture2D texture, float u, float v)
48+
{
49+
if (texture == null || texture.width == 0 || texture.height == 0) {
50+
return Color.clear;
51+
}
52+
53+
texture.GetPixelCoordinates(u, v, out int px, out int py);
54+
return texture.GetPixel(px, py);
55+
}
56+
57+
/// <summary>
58+
/// Gets the pixel color at the UV coordinates calculated from a point
59+
/// inside a rectangle.
60+
/// </summary>
61+
/// <param name="texture">The texture to sample from.</param>
62+
/// <param name="rect">The rectangle to sample from.</param>
63+
/// <param name="point">The point inside the rectangle.</param>
64+
/// <returns>The pixel color.</returns>
65+
public static Color Sample(this Texture2D texture, Rect rect, Vector2 point)
66+
{
67+
if (texture == null || texture.width == 0 || texture.height == 0) {
68+
return Color.clear;
69+
}
70+
71+
float u = Mathf.InverseLerp(rect.min.x, rect.max.x, point.x);
72+
float v = Mathf.InverseLerp(rect.min.y, rect.max.y, point.y);
73+
74+
return Sample(texture, u, v);
75+
}
76+
77+
/// <summary>
78+
/// Gets the pixel color at the UV coordinates calculated from a
79+
/// position inside a bounds. The position uses the x and z axis.
80+
/// </summary>
81+
/// <param name="texture">The texture to sample from.</param>
82+
/// <param name="bounds">The bounds to sample from.</param>
83+
/// <param name="position">The position inside the bounds.</param>
84+
/// <returns>The pixel color.</returns>
85+
public static Color Sample(this Texture2D texture, Bounds bounds, Vector3 position)
86+
{
87+
if (texture == null || texture.width == 0 || texture.height == 0) {
88+
return Color.clear;
89+
}
90+
91+
float u = Mathf.InverseLerp(bounds.min.x, bounds.max.x, position.x);
92+
float v = Mathf.InverseLerp(bounds.min.z, bounds.max.z, position.z);
93+
94+
return Sample(texture, u, v);
95+
}
96+
97+
/// <summary>
98+
/// Sets every pixel in the texture to the specified color.
99+
/// </summary>
100+
/// <param name="texture">The texture to set the color of.</param>
101+
/// <param name="color">The color to set the texture to.</param>
102+
public static void SetColor(this Texture2D texture, Color32 color)
103+
{
104+
var colors = texture.GetRawTextureData<Color32>();
105+
int length = colors.Length;
106+
107+
for (int i = 0; i < length; i++) {
108+
colors[i] = color;
109+
}
110+
111+
texture.Apply();
112+
}
113+
114+
}
115+
116+
}

Runtime/Extensions/TextureExtensions.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)