Skip to content

Instantly share code, notes, and snippets.

@sfider
Created April 28, 2022 16:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfider/c18829b759cc19bdd75404d90e8e2731 to your computer and use it in GitHub Desktop.
Save sfider/c18829b759cc19bdd75404d90e8e2731 to your computer and use it in GitHub Desktop.
/*
* Copyright 2022 Marcin Swiderski
*
* The MIT Licence (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using UnityEditor;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Rendering;
[ExecuteAlways]
public class PixelPeekScript : MonoBehaviour
{
#if UNITY_EDITOR
private RenderTexture _renderTexture;
private Texture2D _peekTexture;
private Color _peekPixel;
private void OnEnable()
{
RenderPipelineManager.beginCameraRendering += OnBeginCameraRendering;
RenderPipelineManager.endCameraRendering += OnEndCameraRendering;
_peekTexture = new Texture2D(1, 1);
_peekPixel = Color.black;
}
private void OnDisable()
{
RenderPipelineManager.beginCameraRendering -= OnBeginCameraRendering;
RenderPipelineManager.endCameraRendering -= OnEndCameraRendering;
ReleaseRenderTexture();
_peekTexture = null;
}
private void OnGUI()
{
float rf = _peekPixel.r;
float gf = _peekPixel.g;
float bf = _peekPixel.b;
int ri = (int)(rf * 255.0f);
int gi = (int)(gf * 255.0f);
int bi = (int)(bf * 255.0f);
float flt = rf + gf / 255.0f;
string rgbH = $"RGB(hex): {ri << 16 | gi << 8 | bi << 0:x6}";
string rgbI = $"RGB(int): {ri}, {gi}, {bi}";
string rgbF = $"RGB(float): {rf}, {gf}, {bf}";
string rgFenc = $"Float(enc): {flt}";
GUIStyle style = new GUIStyle(EditorStyles.textArea);
style.fontSize = 24;
GUILayout.Label($"{rgbH}\n{rgbI}\n{rgbF}\n{rgFenc}", style);
}
private void OnBeginCameraRendering(ScriptableRenderContext context, Camera cam)
{
if (cam == GetComponent<Camera>()) {
EnsureRenderTexture(cam.pixelWidth, cam.pixelHeight);
cam.targetTexture = _renderTexture;
}
}
private void OnEndCameraRendering(ScriptableRenderContext context, Camera cam)
{
if (_renderTexture != null && cam.targetTexture == _renderTexture) {
cam.targetTexture = null;
Graphics.Blit(_renderTexture, (RenderTexture)null);
Vector2 cursor = Mouse.current.position.ReadValue();
int screenXMax = cam.pixelWidth - 1;
int screenYMax = cam.pixelHeight - 1;
int pixelX = Mathf.Clamp((int)cursor.x, 0, screenXMax);
int pixelY = screenYMax - Mathf.Clamp((int)cursor.y, 0, screenYMax);
RenderTexture currentRT = RenderTexture.active;
RenderTexture.active = _renderTexture;
_peekTexture.ReadPixels(new Rect(pixelX, pixelY, 1, 1), 0, 0);
_peekPixel = _peekTexture.GetPixel(0, 0);
RenderTexture.active = currentRT;
}
}
private void EnsureRenderTexture(int width, int height)
{
if (_renderTexture == null || _renderTexture.width != width || _renderTexture.height != height) {
ReleaseRenderTexture();
_renderTexture = RenderTexture.GetTemporary(width, height, 24);
}
}
private void ReleaseRenderTexture()
{
if (_renderTexture != null) {
RenderTexture.ReleaseTemporary(_renderTexture);
_renderTexture = null;
}
}
#endif // UNITY_EDITOR
}
@sfider
Copy link
Author

sfider commented Apr 28, 2022

Just attach it to your main camera and enable/disable it in the hierarchy when needed. Tested with URP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment