Skip to content

Instantly share code, notes, and snippets.

@st4rdog
Created July 18, 2018 22:24
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save st4rdog/fefe61884292ca457bbe9395c5458749 to your computer and use it in GitHub Desktop.
Save st4rdog/fefe61884292ca457bbe9395c5458749 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class LightmapPixelPicker : MonoBehaviour {
public Color surfaceColor;
public float brightness1; // http://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color
public float brightness2; // http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx
public LayerMask layerMask;
void Update()
{
Raycast();
// BRIGHTNESS APPROX
brightness1 = (surfaceColor.r + surfaceColor.r + surfaceColor.b + surfaceColor.g + surfaceColor.g + surfaceColor.g) / 6;
// BRIGHTNESS
brightness2 = Mathf.Sqrt((surfaceColor.r * surfaceColor.r * 0.2126f + surfaceColor.g * surfaceColor.g * 0.7152f + surfaceColor.b * surfaceColor.b * 0.0722f));
}
void OnGUI()
{
GUILayout.BeginArea(new Rect(10f, 10f, Screen.width, Screen.height));
GUILayout.Label("R = " + string.Format("{0:0.00}", surfaceColor.r));
GUILayout.Label("G = " + string.Format("{0:0.00}", surfaceColor.g));
GUILayout.Label("B = " + string.Format("{0:0.00}", surfaceColor.b));
GUILayout.Label("Brightness Approx = " + string.Format("{0:0.00}", brightness1));
GUILayout.Label("Brightness = " + string.Format("{0:0.00}", brightness2));
GUILayout.EndArea();
}
void Raycast()
{
// RAY TO PLAYER'S FEET
Ray ray = new Ray(transform.position, -Vector3.up);
Debug.DrawRay(ray.origin, ray.direction * 5f, Color.magenta);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, 5f, layerMask))
{
// GET RENDERER OF OBJECT HIT
Renderer hitRenderer = hitInfo.collider.GetComponent<Renderer>();
// GET LIGHTMAP APPLIED TO OBJECT
LightmapData lightmapData = LightmapSettings.lightmaps[hitRenderer.lightmapIndex];
// STORE LIGHTMAP TEXTURE
Texture2D lightmapTex = lightmapData.lightmapColor;
// GET LIGHTMAP COORDINATE WHERE RAYCAST HITS
Vector2 pixelUV = hitInfo.lightmapCoord;
// GET COLOR AT THE LIGHTMAP COORDINATE
Color surfaceColor = lightmapTex.GetPixelBilinear(pixelUV.x, pixelUV.y);
// APPLY
this.surfaceColor = surfaceColor;
}
}
}
@clara2g
Copy link

clara2g commented Dec 7, 2020

Hi! What you're doing here is amazing and I have been trying to get the lightning of a texture for a while with different approaches without success. But for some reason this code doesn't work for me. My hitRenderer.lightmapIndex is -1 and LightmapSettings.lightmaps is an array of length 1 that starts at 0 and cannot be accesed at -1. Do you know what could be the problem? Thank you :)

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