Skip to content

Commit 276d8a1

Browse files
committed
Fixed read pixel being flipped when using OpenGL
• Added a PointToTexelCoordinate function that takes account for OpenGL-like uv flipping.
1 parent 34976a2 commit 276d8a1

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

Assets/Nessie/ASE/Editor/ColorDebug.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,29 @@ private static void ShowColorTooltip(Vector2 mousePos, ParentNode node)
9696
return;
9797
}
9898

99-
Vector2 normalizedPreviewPos = PointToRectNormalized(mousePos, previewRect);
100-
Vector2 texturePos = normalizedPreviewPos * new Vector2(node.PreviewTexture.width, node.PreviewTexture.height);
101-
10299
// Push a single pixel from a RT into a Tex2D.
103100
RenderTexture previousRT = RenderTexture.active;
104101
RenderTexture.active = node.PreviewTexture;
105-
PreviewPixel.ReadPixels(new Rect(texturePos.x, texturePos.y, 1, 1), 0, 0, false);
102+
Vector2 texelPos = PointToTexelCoordinate(mousePos, previewRect, node.PreviewTexture);
103+
PreviewPixel.ReadPixels(new Rect(texelPos.x, texelPos.y, 1, 1), 0, 0, false);
106104
RenderTexture.active = previousRT;
107105

108106
// Each channel is represented with a full 32-bit float.
109107
Color color = UnpackColor(PreviewPixel.GetRawTextureData());
110108
DrawColorTooltip(node, mousePos, color);
111109
}
112110

111+
private static Vector2 PointToTexelCoordinate(Vector2 point, Rect textureRect, Texture texture)
112+
{
113+
Vector2 normalizedRectPos = PointToRectNormalized(point, textureRect);
114+
if (!SystemInfo.graphicsUVStartsAtTop)
115+
{
116+
normalizedRectPos.y = 1f - normalizedRectPos.y;
117+
}
118+
119+
return normalizedRectPos * new Vector2(texture.width, texture.height);
120+
}
121+
113122
private static Vector2 PointToRectNormalized(Vector2 point, Rect rect)
114123
{
115124
return (point - new Vector2(rect.x, rect.y)) / new Vector2(rect.width, rect.height);

0 commit comments

Comments
 (0)