Skip to content

Instantly share code, notes, and snippets.

@codewings
Last active February 1, 2023 08:17
Show Gist options
  • Save codewings/e0cbca172900d650629482b9bbd6dad7 to your computer and use it in GitHub Desktop.
Save codewings/e0cbca172900d650629482b9bbd6dad7 to your computer and use it in GitHub Desktop.
Simple editor window for checking lightmap in unity
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class LightmapAssist : EditorWindow
{
[MenuItem("Window/Lighting/Lightmap Assistant")]
public static void ShowLightmapAssistWnd()
{
EditorWindow.GetWindow(typeof(LightmapAssist), true, "LightmapAssistant");
}
void OnSelectionChange()
{
Repaint();
}
void OnDestroy()
{
if (mLightmapPreviewMaterial != null)
{
DestroyImmediate(mLightmapPreviewMaterial);
mLightmapPreviewMaterial = null;
}
}
public void OnGUI()
{
var go = Selection.activeGameObject;
if (go == null)
{
return;
}
var renderer = go.GetComponent<MeshRenderer>();
if (renderer == null || renderer.lightmapIndex == -1)
{
EditorGUILayout.LabelField(string.Format("{0} has no lightmap.", go.name));
return;
}
var lightmap = LightmapSettings.lightmaps[renderer.lightmapIndex].lightmapColor;
var path = AssetDatabase.GetAssetPath(lightmap);
if (string.IsNullOrEmpty(path) == false)
{
EditorGUILayout.LabelField(string.Format("GameObject:{0}, Lightmap Path:{1}, LightMapIndex:{2}", go.name, path, renderer.lightmapIndex));
}
else
{
EditorGUILayout.LabelField(string.Format("GameObject:{0}", go.name));
}
DrawLightmapWithWireframe(EditorGUILayout.GetControlRect(false, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true)), go.GetComponent<MeshFilter>().sharedMesh,
lightmap, renderer.lightmapScaleOffset);
}
private void DrawLightmapWithWireframe(Rect rect, Mesh mesh, Texture2D lightmap, Vector4 lightmapScaleOffset)
{
if (mLightmapPreviewMaterial == null)
{
mLightmapPreviewMaterial = new Material(Shader.Find("GUI/LightmapAssist Prievew"));
mLightmapPreviewMaterial.hideFlags = HideFlags.HideAndDontSave;
}
mLightmapPreviewMaterial.SetVector("_Viewport", Vector4.zero);
mLightmapPreviewMaterial.color = Color.white;
mLightmapPreviewMaterial.SetVector("_MainTex_ST", new Vector4(1, 1, 0, 0));
EditorGUI.DrawPreviewTexture(rect, lightmap, mLightmapPreviewMaterial, ScaleMode.StretchToFill);
mLightmapPreviewMaterial.color = new Color32(104, 138, 188, 255);
mLightmapPreviewMaterial.SetTexture("_MainTex", Texture2D.whiteTexture);
mLightmapPreviewMaterial.SetVector("_MainTex_ST", lightmapScaleOffset);
mLightmapPreviewMaterial.SetPass(0);
var triangles = mesh.triangles;
var vertices = mesh.uv2.Length <= 0 ? mesh.uv : mesh.uv2;
GL.wireframe = true;
GL.PushMatrix();
{
GL.MultMatrix(Matrix4x4.TRS(new Vector3(rect.xMin, rect.yMin + rect.height, 0), Quaternion.identity, new Vector3(rect.width, -rect.height, 1) ));
GL.Begin(GL.TRIANGLES);
{
for (int i = 0; i < triangles.Length; i++)
{
GL.Vertex3(vertices[triangles[i]].x, vertices[triangles[i]].y, 0f);
}
}
GL.End();
}
GL.PopMatrix();
GL.wireframe = false;
}
private Material mLightmapPreviewMaterial;
}
#endif
Shader "GUI/LightmapAssist Prievew"
{
Properties {
_MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Text Color", Color) = (1,1,1,1)
}
SubShader {
Tags {
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
}
Lighting Off Cull Off ZTest Always ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
sampler2D _MainTex;
uniform float4 _MainTex_ST;
uniform fixed4 _Color;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(float4(v.vertex.xy * _MainTex_ST.xy + _MainTex_ST.zw, v.vertex.zw));
o.color = v.color * _Color;
o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = i.color * tex2D(_MainTex, i.texcoord);
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment