Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
79f01d7
Improved 2023 support.
Cameron-Micka May 7, 2024
659cb08
Version bump.
Cameron-Micka May 7, 2024
76df246
Merge branch 'main' of https://github.com/microsoft/MixedReality-Grap…
Cameron-Micka May 22, 2024
5ec9853
Merge branch 'main' of https://github.com/microsoft/MixedReality-Grap…
Cameron-Micka Jul 27, 2024
376d2aa
UNITY_6000_0_OR_NEWER support.
Cameron-Micka Oct 2, 2024
301277c
Convert tabs to spaces.
Cameron-Micka Oct 3, 2024
531a303
Bump package version.
Cameron-Micka Oct 3, 2024
2f979b2
Merge branch 'main' of https://github.com/Cameron-Micka/MixedReality-…
Cameron-Micka Nov 19, 2024
9e4b4a0
Merge branch 'main' of https://github.com/microsoft/MixedReality-Grap…
Cameron-Micka Nov 19, 2024
e7443e5
Merge branch 'main' of https://github.com/microsoft/MixedReality-Grap…
Cameron-Micka Dec 2, 2024
bc4489a
Project update and remove unneeded deps.
Cameron-Micka Dec 2, 2024
62afd9d
Extra comments.
Cameron-Micka Dec 2, 2024
a06e99b
Package update.
Cameron-Micka Dec 2, 2024
25d58bf
Merge branch 'main' of https://github.com/microsoft/MixedReality-Grap…
Cameron-Micka Dec 2, 2024
68273e6
Merge branch 'main' of https://github.com/microsoft/MixedReality-Grap…
Cameron-Micka Mar 27, 2025
14dace1
Merge branch 'main' of https://github.com/microsoft/MixedReality-Grap…
Cameron-Micka Apr 3, 2025
7f213c3
Adding facing option.
Cameron-Micka Apr 3, 2025
69e3387
Add logic to flip the cookie UV.y.
Cameron-Micka Apr 3, 2025
3eff470
Adding top edge rotation bias.
Cameron-Micka Apr 3, 2025
a048bf3
Fix optmization check.
Cameron-Micka Apr 3, 2025
d8902c7
Better cookie sampling.
Cameron-Micka Apr 4, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions com.microsoft.mrtk.graphicstools.unity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this package will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

## [0.8.4] - 2025-04-03

### Changed

- Added more properties to the experimental AreaLight component.

## [0.8.3] - 2025-03-26

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ private void OnSceneGUI()
// Draw the area light's normal only if it will not overlap with the current tool.
if (!((Tools.current == Tool.Move || Tools.current == Tool.Scale) && Tools.pivotRotation == PivotRotation.Local))
{
Handles.DrawLine(light.transform.position, light.transform.position + light.transform.forward);
var normal = light.transform.forward * ((light.Facing == AreaLight.ForwardFacing.PositiveZ) ? 1.0f : -1.0f);
Handles.DrawLine(light.transform.position, light.transform.position + normal);
}

Handles.color = new Color(255.0f / 255.0f, 165.0f / 255.0f, 0.0f / 255.0f); // Orange.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,23 @@ public partial class AreaLight : BaseLight, IComparable<AreaLight>
private static readonly float[,] offsets = new float[4, 2] { { 1, 1 }, { 1, -1 }, { -1, -1 }, { -1, 1 } };
private const int lutResolution = 64;
private const int lutMatrixDim = 3;
private static readonly Matrix4x4 rotation180Up = Matrix4x4.Rotate(Quaternion.AngleAxis(180.0f, Vector3.up));

private static Texture2D transformInvTextureSpecular;
private static Texture2D transformInvTextureDiffuse;
private static Texture2D ampDiffAmpSpecFresnel;

private static int lastAreaLightUpdate = -1;
private static List<AreaLight> activeAreaLights = new(maxAreaLights);
private static List<AreaLight> activeAreaLightsSorted = new(maxAreaLights);
private static Vector4[] areaLightData = new Vector4[areaLightDataSize * areaLightCount];
private static Matrix4x4[] areaLightVerts = new Matrix4x4[areaLightCount];
private static Texture[] areaLightCookies = new Texture[areaLightCount];
private static int _AreaLightDataID;
private static int _AreaLightVertsID;
private static int[] _AreaLightCookiesIDs = new int[areaLightCount];
private static int lastAreaLightUpdate = -1;
private static int areaLightDataID;
private static int areaLightVertsID;
private static int[] areaLightCookiesIDs = new int[areaLightCount];
private static int facingID;
private static int uvStartsAtTopID;
private static CullingGroup cullingGroup;
private static BoundingSphere[] boundingSpheres = new BoundingSphere[maxAreaLights];

Expand Down Expand Up @@ -89,6 +92,25 @@ public Vector2 Size
set => size = value;
}

public enum ForwardFacing
{
PositiveZ,
NegativeZ,
}

[Tooltip("The forward direction of the light.")]
[SerializeField]
private ForwardFacing facing = ForwardFacing.PositiveZ;

/// <summary>
/// The forward direction of the light.
/// </summary>
public ForwardFacing Facing
{
get => facing;
set => facing = value;
}

[Tooltip("Optional texture to use instead of a solid color.")]
[SerializeField]
private Texture cookie;
Expand All @@ -102,6 +124,19 @@ public Texture Cookie
set => cookie = value;
}

[Tooltip("Should the texture UV coordinate convention for this cookie have Y starting at the top of the image.")]
[SerializeField]
private bool cookieUVStartsAtTop = true;

/// <summary>
/// Should the texture UV coordinate convention for this cookie have Y starting at the top of the image.
/// </summary>
public bool CookieUVStartsAtTop
{
get => cookieUVStartsAtTop;
set => cookieUVStartsAtTop = value;
}

[Tooltip("Should the area light have a visualization?")]
[SerializeField]
private bool drawLightSource = true;
Expand Down Expand Up @@ -215,14 +250,17 @@ public Camera CullingGroupCamera
/// <inheritdoc/>
protected override void Initialize()
{
_AreaLightDataID = Shader.PropertyToID("_AreaLightData");
_AreaLightVertsID = Shader.PropertyToID("_AreaLightVerts");
areaLightDataID = Shader.PropertyToID("_AreaLightData");
areaLightVertsID = Shader.PropertyToID("_AreaLightVerts");

for (int i = 0; i < _AreaLightCookiesIDs.Length; ++i)
for (int i = 0; i < areaLightCookiesIDs.Length; ++i)
{
_AreaLightCookiesIDs[i] = Shader.PropertyToID($"_AreaLightCookie{i}");
areaLightCookiesIDs[i] = Shader.PropertyToID($"_AreaLightCookie{i}");
}

facingID = Shader.PropertyToID("_facing");
uvStartsAtTopID = Shader.PropertyToID("_uvStartsAtTop");

CreateLUTs();
UpdateLightSourceVisual();

Expand Down Expand Up @@ -362,18 +400,23 @@ protected override void UpdateLights(bool forceUpdate = false)

if (light)
{
areaLightData[dataIndex] = light.Color;
var color = light.Color;
areaLightData[dataIndex] = new Vector4(color.r,
color.g,
color.b,
light.cookieUVStartsAtTop ? 1.0f : 0.0f);

var lightVerts = new Matrix4x4();
var localToWorld = light.transform.localToWorldMatrix;

// A little bit of bias to prevent the light from lighting itself.
const float z = 0.01f;
if (light.facing == ForwardFacing.NegativeZ)
{
localToWorld *= rotation180Up;
}

Matrix4x4 lightVerts = new Matrix4x4();
for (int v = 0; v < 4; ++v)
{
Vector3 vertex = new Vector3(light.size.x * offsets[v, 0],
light.size.y * offsets[v, 1],
z) * 0.5f;
lightVerts.SetRow(v, light.transform.TransformPoint(vertex));
lightVerts.SetRow(v, TransformVertex(v, light.size, localToWorld));
}

areaLightVerts[i] = lightVerts;
Expand All @@ -397,13 +440,13 @@ protected override void UpdateLights(bool forceUpdate = false)
}
}

Shader.SetGlobalVectorArray(_AreaLightDataID, areaLightData);
Shader.SetGlobalMatrixArray(_AreaLightVertsID, areaLightVerts);
Shader.SetGlobalVectorArray(areaLightDataID, areaLightData);
Shader.SetGlobalMatrixArray(areaLightVertsID, areaLightVerts);

// There is no SetGlobalTextureArray so pass in 1 by 1.
for (int i = 0; i < areaLightCookies.Length; ++i)
{
Shader.SetGlobalTexture(_AreaLightCookiesIDs[i], areaLightCookies[i]);
Shader.SetGlobalTexture(areaLightCookiesIDs[i], areaLightCookies[i]);
}

lastAreaLightUpdate = Time.frameCount;
Expand Down Expand Up @@ -467,6 +510,18 @@ private void OnDrawGizmos()
}
#endif

private static Vector3 TransformVertex(int index, Vector2 size, Matrix4x4 localToWorld)
{
// A little bit of bias to prevent the light from lighting itself.
const float z = 0.01f;

var vertex = new Vector3(size.x * offsets[index, 0],
size.y * offsets[index, 1],
z) * 0.5f;

return localToWorld.MultiplyPoint(vertex);
}

private static void CreateLUTs()
{
if (transformInvTextureDiffuse == null)
Expand Down Expand Up @@ -506,6 +561,8 @@ private void UpdateLightSourceVisual()

lightSourceVisual.sharedMaterial.color = Color;
lightSourceVisual.sharedMaterial.mainTexture = drawLightSourceCookie ? drawLightSourceCookie : cookie;
lightSourceVisual.sharedMaterial.SetFloat(facingID, (float)facing);
lightSourceVisual.sharedMaterial.SetFloat(uvStartsAtTopID, cookieUVStartsAtTop ? 0.0f : 1.0f);
lightSourceVisual.transform.localScale = new Vector3(size.x, size.y, 1.0f);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Material CookieFilterMaterial

[Tooltip("How many blur passes to perform during Dual blurring.")]
[SerializeField]
[Range(2, 7)]
[Range(0, 7)]
private int blurPasses = 3;

/// <summary>
Expand All @@ -68,7 +68,7 @@ public Material CookieFilterMaterial
public int BlurPasses
{
get => blurPasses;
set => blurPasses = Mathf.Clamp(value, 2, 7);
set => blurPasses = Mathf.Clamp(value, 0, 7);
}

/// <summary>
Expand Down
Loading