Skip to content

Commit e4bd67f

Browse files
Experimental Feature: Area Lights (#236)
1 parent 612d918 commit e4bd67f

56 files changed

Lines changed: 28435 additions & 7 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

com.microsoft.mrtk.graphicstools.unity/Editor/Experimental/AreaLight.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using UnityEditor;
5+
using UnityEngine;
6+
7+
namespace Microsoft.MixedReality.GraphicsTools.Editor
8+
{
9+
/// <summary>
10+
/// Improves object selection and adds a shortcut to create a configured game object and component from the game object context menu.
11+
/// </summary>
12+
[CustomEditor(typeof(AreaLight))]
13+
public class AreaLightInspector : UnityEditor.Editor
14+
{
15+
private void OnSceneGUI()
16+
{
17+
AreaLight light = target as AreaLight;
18+
19+
if (light == null)
20+
{
21+
return;
22+
}
23+
24+
if (light.enabled)
25+
{
26+
Handles.color = light.Color;
27+
}
28+
else
29+
{
30+
Handles.color = Color.gray;
31+
}
32+
33+
EditorGUI.BeginChangeCheck();
34+
Vector2 size = DrawRectHandles(light.transform.rotation, light.transform.position, light.Size);
35+
if (EditorGUI.EndChangeCheck())
36+
{
37+
Undo.RecordObject(light, "Adjust Area Light Size");
38+
light.Size = size;
39+
}
40+
41+
// Draw the area light's normal only if it will not overlap with the current tool.
42+
if (!((Tools.current == Tool.Move || Tools.current == Tool.Scale) && Tools.pivotRotation == PivotRotation.Local))
43+
{
44+
Handles.DrawLine(light.transform.position, light.transform.position + light.transform.forward);
45+
}
46+
47+
Handles.color = new Color(255.0f / 255.0f, 165.0f / 255.0f, 0.0f / 255.0f); // Orange.
48+
49+
// Different visual representation for runtime and edit mode because runtime using CullingGroup to cull lights.
50+
if (Application.isPlaying)
51+
{
52+
var bounds = light.SphereBoundsWorldSpace;
53+
Handles.RadiusHandle(Quaternion.identity, bounds.position, bounds.radius);
54+
}
55+
else
56+
{
57+
var bounds = light.BoundsWorldSpace;
58+
Handles.DrawWireCube(bounds.center, bounds.size);
59+
}
60+
61+
if (light.CullingActive)
62+
{
63+
Handles.Label(light.transform.position, $"Visible: {light.IsVisible}\nDist: {light.Distance.ToString("n1")}");
64+
}
65+
}
66+
67+
private bool HasFrameBounds() { return true; }
68+
69+
private Bounds OnGetFrameBounds()
70+
{
71+
var light = target as AreaLight;
72+
Debug.Assert(light != null);
73+
return light.BoundsWorldSpace;
74+
}
75+
76+
[MenuItem("GameObject/Light/Graphics Tools/Area Light")]
77+
private static void CreateAreaLight(MenuCommand menuCommand)
78+
{
79+
GameObject gameObject = InspectorUtilities.CreateGameObjectFromMenu<AreaLight>(menuCommand);
80+
81+
if (gameObject != null)
82+
{
83+
gameObject.transform.position = new Vector3(0.0f, 1.0f, 0.0f);
84+
gameObject.transform.rotation = Quaternion.Euler(0.0f, 180.0f, 0.0f);
85+
}
86+
}
87+
88+
private static float SizeSlider(Vector3 p, Vector3 d, float r)
89+
{
90+
Vector3 position = p + d * r;
91+
float size = HandleUtility.GetHandleSize(position);
92+
bool temp = GUI.changed;
93+
GUI.changed = false;
94+
position = Handles.Slider(position, d, size * 0.03f, Handles.DotHandleCap, 0.0f);
95+
96+
if (GUI.changed)
97+
{
98+
r = Vector3.Dot(position - p, d);
99+
}
100+
101+
GUI.changed |= temp;
102+
return r;
103+
}
104+
private static Color ToActiveColorSpace(Color color)
105+
{
106+
return (QualitySettings.activeColorSpace == ColorSpace.Linear) ? color.linear : color;
107+
}
108+
109+
private static Vector2 DrawRectHandles(Quaternion rotation, Vector3 position, Vector2 size)
110+
{
111+
Vector3 up = rotation * Vector3.up;
112+
Vector3 right = rotation * Vector3.right;
113+
114+
float halfWidth = 0.5f * size.x;
115+
float halfHeight = 0.5f * size.y;
116+
117+
Vector3 topRight = position + up * halfHeight + right * halfWidth;
118+
Vector3 bottomRight = position - up * halfHeight + right * halfWidth;
119+
Vector3 bottomLeft = position - up * halfHeight - right * halfWidth;
120+
Vector3 topLeft = position + up * halfHeight - right * halfWidth;
121+
122+
// Draw the rectangle.
123+
Handles.DrawLine(topRight, bottomRight);
124+
Handles.DrawLine(bottomRight, bottomLeft);
125+
Handles.DrawLine(bottomLeft, topLeft);
126+
Handles.DrawLine(topLeft, topRight);
127+
128+
// Give handles twice the alpha of the lines.
129+
Color originalColor = Handles.color;
130+
Color color = Handles.color;
131+
color.a = Mathf.Clamp01(Handles.color.a * 2);
132+
Handles.color = ToActiveColorSpace(color);
133+
134+
// Draw the handles.
135+
halfHeight = SizeSlider(position, up, halfHeight);
136+
halfHeight = SizeSlider(position, -up, halfHeight);
137+
halfWidth = SizeSlider(position, right, halfWidth);
138+
halfWidth = SizeSlider(position, -right, halfWidth);
139+
140+
size.x = Mathf.Max(0.0f, 2.0f * halfWidth);
141+
size.y = Mathf.Max(0.0f, 2.0f * halfHeight);
142+
143+
Handles.color = originalColor;
144+
145+
return size;
146+
}
147+
}
148+
}

com.microsoft.mrtk.graphicstools.unity/Editor/Experimental/AreaLight/AreaLightInspector.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.

com.microsoft.mrtk.graphicstools.unity/Runtime/Experimental/Acrylic/Scripts/AcrylicLayer.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ public void ApplyBlur(ref RenderTexture source, ref RenderTexture destination)
379379
Debug.LogWarning("Null blur source texture.");
380380
return;
381381
}
382+
382383
if (useDualBlur)
383384
{
384385
dualBlur.ApplyBlur("AcrylicLayer" + index + "_Blur", source, settings.blurPasses);
@@ -409,6 +410,17 @@ public void ApplyBlur(ref RenderTexture source, ref RenderTexture destination)
409410
}
410411
}
411412

413+
public void ApplyDualBlur(ref RenderTexture source, int iterations)
414+
{
415+
if (source == null)
416+
{
417+
Debug.LogWarning("Null blur source texture.");
418+
return;
419+
}
420+
421+
dualBlur.ApplyBlur("AcrylicLayer" + index + "_Blur", source, iterations);
422+
}
423+
412424
public void SetBlendSource(RenderTexture input, bool both)
413425
{
414426
InitRenderTexture(ref blendSource[blendSourceIndex], input.width, input.height, 0, "BlendSource");

com.microsoft.mrtk.graphicstools.unity/Runtime/Experimental/AreaLight.meta

Lines changed: 8 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)