From 283840b2f3e9418c658d077ed515e305bfecd8f1 Mon Sep 17 00:00:00 2001 From: Antoine Lelievre Date: Tue, 27 Jul 2021 16:02:05 +0200 Subject: [PATCH 01/11] Light anchor fixes --- .../Editor/Lighting/LightAnchorEditor.cs | 18 +++++++++++++----- .../Runtime/Lights/LightAnchor.cs | 10 +++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs b/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs index 8c40551adc9..6c61690293a 100644 --- a/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs +++ b/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs @@ -30,6 +30,9 @@ public class LightAnchorEditor : Editor VisualElement m_GameViewRootElement; VisualElement m_ClickCatcher; + SerializedProperty m_DistanceProperty; + SerializedProperty m_FrameSpaceProperty; + LightAnchor manipulator { get { return target as LightAnchor; } @@ -125,13 +128,15 @@ public override void OnInspectorGUI() } EditorGUILayout.Space(); + // TODO: warning box when distance is at 0 (can't set rotation (or can we?)) EditorGUI.BeginChangeCheck(); - m_Distance = EditorGUILayout.FloatField(LightAnchorStyles.distanceProperty, manipulator.distance); + EditorGUILayout.PropertyField(m_DistanceProperty, LightAnchorStyles.distanceProperty); distanceChanged = EditorGUI.EndChangeCheck(); EditorGUI.BeginChangeCheck(); - var dropRect = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight); - m_FrameSpace = (LightAnchor.UpDirection)EditorGUI.EnumPopup(dropRect, LightAnchorStyles.upDirectionProperty, manipulator.frameSpace); + // var dropRect = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight); + // m_FrameSpace = (LightAnchor.UpDirection)EditorGUI.EnumPopup(dropRect, LightAnchorStyles.upDirectionProperty, manipulator.frameSpace); + EditorGUILayout.PropertyField(m_FrameSpaceProperty, LightAnchorStyles.upDirectionProperty); upChanged = EditorGUI.EndChangeCheck(); if (m_FoldoutPreset = EditorGUILayout.Foldout(m_FoldoutPreset, "Common")) @@ -266,7 +271,7 @@ public override void OnInspectorGUI() { Undo.RecordObjects(new UnityEngine.Object[] { target, manipulator.transform }, "Light Anchor Change"); - manipulator.frameSpace = m_FrameSpace; + manipulator.frameSpace = (LightAnchor.UpDirection)m_FrameSpaceProperty.enumValueIndex; manipulator.SynchronizeOnTransform(camera); UpdateCache(); } @@ -281,7 +286,7 @@ public override void OnInspectorGUI() if (rollChanged) manipulator.roll = m_Roll; if (distanceChanged) - manipulator.distance = m_Distance; + manipulator.distance = m_DistanceProperty.floatValue; manipulator.UpdateTransform(camera, anchor); IsCacheInvalid(manipulator); @@ -321,6 +326,9 @@ void OnEnable() EnableClickCatcher(m_EnableClickCatcher); } } + + m_DistanceProperty = serializedObject.FindProperty("m_Distance"); + m_FrameSpaceProperty = serializedObject.FindProperty("m_FrameSpace"); } void EditorToolsOnactiveToolChanged() diff --git a/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs b/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs index df00a3ab726..74804a189b0 100644 --- a/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs +++ b/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs @@ -19,7 +19,7 @@ public class LightAnchor : MonoBehaviour const float k_ArcRadius = 5; const float k_AxisLength = 10; - [SerializeField] + [SerializeField, Min(0)] float m_Distance = 0f; [SerializeField] UpDirection m_FrameSpace = UpDirection.World; @@ -69,8 +69,8 @@ public float roll /// public float distance { - get { return m_Distance; } - set { m_Distance = Mathf.Max(value, .01f); } + get { return Mathf.Max(m_Distance, 0.0001f); } + set { m_Distance = value; } } /// @@ -102,7 +102,7 @@ public UpDirection frameSpace /// public Vector3 anchorPosition { - get { return transform.position + transform.forward * m_Distance; } + get { return transform.position + transform.forward * distance; } } struct Axes @@ -229,7 +229,7 @@ void UpdateTransform(Vector3 up, Vector3 right, Vector3 forward, Vector3 anchor) { Quaternion worldYawRot = Quaternion.AngleAxis(m_Yaw, up); Quaternion worldPitchRot = Quaternion.AngleAxis(m_Pitch, right); - Vector3 worldPosition = anchor + (worldYawRot * worldPitchRot) * forward * m_Distance; + Vector3 worldPosition = anchor + (worldYawRot * worldPitchRot) * forward * distance; transform.position = worldPosition; Vector3 lookAt = (anchor - worldPosition).normalized; From fa11739e062d8ddeacabbc5ff7b31a1a0a78cb14 Mon Sep 17 00:00:00 2001 From: Antoine Lelievre Date: Tue, 27 Jul 2021 16:02:56 +0200 Subject: [PATCH 02/11] Updated changelog --- com.unity.render-pipelines.high-definition/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/com.unity.render-pipelines.high-definition/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md index d949e56b330..32ccdd46ec7 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -328,6 +328,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Fixed white flashes when history is reset due to changes on type of upsampler. - Fixed misc TAA issue: Slightly improved TAA flickering, Reduced ringing of TAA sharpening, tweak TAA High quality central color filtering. - Fixed TAA upsampling algorithm, now work properly +- Fixed light anchor min distance value + properties not working with prefabs (case 1345509). ### Changed - Changed Window/Render Pipeline/HD Render Pipeline Wizard to Window/Rendering/HDRP Wizard From d880d3aa21b841c63848ac00d0d987b14950950f Mon Sep 17 00:00:00 2001 From: Antoine Lelievre Date: Tue, 27 Jul 2021 16:09:40 +0200 Subject: [PATCH 03/11] Cleanup --- .../Editor/Lighting/LightAnchorEditor.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs b/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs index 6c61690293a..bc3384aa3b5 100644 --- a/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs +++ b/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs @@ -128,14 +128,11 @@ public override void OnInspectorGUI() } EditorGUILayout.Space(); - // TODO: warning box when distance is at 0 (can't set rotation (or can we?)) EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(m_DistanceProperty, LightAnchorStyles.distanceProperty); distanceChanged = EditorGUI.EndChangeCheck(); EditorGUI.BeginChangeCheck(); - // var dropRect = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight); - // m_FrameSpace = (LightAnchor.UpDirection)EditorGUI.EnumPopup(dropRect, LightAnchorStyles.upDirectionProperty, manipulator.frameSpace); EditorGUILayout.PropertyField(m_FrameSpaceProperty, LightAnchorStyles.upDirectionProperty); upChanged = EditorGUI.EndChangeCheck(); From b292733292cea0260d120055ebc5a452dc4f4094 Mon Sep 17 00:00:00 2001 From: Antoine Lelievre Date: Tue, 27 Jul 2021 16:30:13 +0200 Subject: [PATCH 04/11] Remove require component Light on the light anchor --- com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs b/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs index 74804a189b0..261952fa027 100644 --- a/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs +++ b/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs @@ -10,7 +10,6 @@ namespace UnityEngine /// Represents camera-space light controls around a virtual pivot point. /// [AddComponentMenu("Rendering/Light Anchor")] - [RequireComponent(typeof(Light))] [ExecuteInEditMode] [DisallowMultipleComponent] [CoreRPHelpURLAttribute("View-Lighting-Tool")] From 4133e71485d317f9b4b219d3b1c8953dea821764 Mon Sep 17 00:00:00 2001 From: Antoine Lelievre Date: Wed, 28 Jul 2021 11:45:48 +0200 Subject: [PATCH 05/11] Update com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs Co-authored-by: Remi Slysz <40034005+RSlysz@users.noreply.github.com> --- .../Editor/Lighting/LightAnchorEditor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs b/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs index bc3384aa3b5..b4947d268a7 100644 --- a/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs +++ b/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs @@ -268,7 +268,7 @@ public override void OnInspectorGUI() { Undo.RecordObjects(new UnityEngine.Object[] { target, manipulator.transform }, "Light Anchor Change"); - manipulator.frameSpace = (LightAnchor.UpDirection)m_FrameSpaceProperty.enumValueIndex; + manipulator.frameSpace = (LightAnchor.UpDirection)m_FrameSpaceProperty.intValue; manipulator.SynchronizeOnTransform(camera); UpdateCache(); } From d262217a309c507fd41d3399ac8109b0dc8400ae Mon Sep 17 00:00:00 2001 From: Antoine Lelievre Date: Wed, 28 Jul 2021 12:42:04 +0200 Subject: [PATCH 06/11] Fix anchor direction algorithm to work with distance of 0 and add anchor position override field --- .../Editor/Lighting/LightAnchorEditor.cs | 18 ++++++---- .../Runtime/Lights/LightAnchor.cs | 35 ++++++++++++++++--- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs b/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs index bc3384aa3b5..f96fe71c7d6 100644 --- a/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs +++ b/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs @@ -32,6 +32,7 @@ public class LightAnchorEditor : Editor SerializedProperty m_DistanceProperty; SerializedProperty m_FrameSpaceProperty; + SerializedProperty m_AnchorPositionOverrideProperty; LightAnchor manipulator { @@ -57,14 +58,11 @@ public override void OnInspectorGUI() UpdateCache(); } - // anchor is cached for it cannot be changed from the inspector, - // we have a dedicated editor tool to move the anchor - var anchor = manipulator.anchorPosition; - bool yawChanged = false; bool pitchChanged = false; bool rollChanged = false; bool distanceChanged = false; + bool positionOverrideChanged = false; bool upChanged = false; using (var change = new EditorGUI.ChangeCheckScope()) @@ -136,6 +134,10 @@ public override void OnInspectorGUI() EditorGUILayout.PropertyField(m_FrameSpaceProperty, LightAnchorStyles.upDirectionProperty); upChanged = EditorGUI.EndChangeCheck(); + EditorGUI.BeginChangeCheck(); + EditorGUILayout.PropertyField(m_AnchorPositionOverrideProperty, LightAnchorStyles.anchorPositionOverrideProperty); + positionOverrideChanged = EditorGUI.EndChangeCheck(); + if (m_FoldoutPreset = EditorGUILayout.Foldout(m_FoldoutPreset, "Common")) { Color cachedColor = GUI.backgroundColor; @@ -272,7 +274,7 @@ public override void OnInspectorGUI() manipulator.SynchronizeOnTransform(camera); UpdateCache(); } - if (yawChanged || pitchChanged || rollChanged || distanceChanged) + if (yawChanged || pitchChanged || rollChanged || distanceChanged || positionOverrideChanged) { Undo.RecordObjects(new UnityEngine.Object[] { target, manipulator.transform }, "Light Anchor Change"); @@ -284,8 +286,10 @@ public override void OnInspectorGUI() manipulator.roll = m_Roll; if (distanceChanged) manipulator.distance = m_DistanceProperty.floatValue; + if (positionOverrideChanged) + manipulator.anchorPositionOverride = m_AnchorPositionOverrideProperty.objectReferenceValue as Transform; - manipulator.UpdateTransform(camera, anchor); + manipulator.UpdateTransform(camera, manipulator.anchorPosition); IsCacheInvalid(manipulator); } } @@ -326,6 +330,7 @@ void OnEnable() m_DistanceProperty = serializedObject.FindProperty("m_Distance"); m_FrameSpaceProperty = serializedObject.FindProperty("m_FrameSpace"); + m_AnchorPositionOverrideProperty = serializedObject.FindProperty("m_AnchorPositionOverride"); } void EditorToolsOnactiveToolChanged() @@ -566,6 +571,7 @@ static class LightAnchorStyles static public GUIContent presetTextureRimRight = EditorGUIUtility.TrTextContent("", "Rim Right", UnityEditor.Rendering.CoreEditorUtils.LoadIcon(LightAnchorStyles.k_IconFolder, "PresetRim_Right", ".png", false)); static public GUIContent distanceProperty = EditorGUIUtility.TrTextContent("Distance", "Controls how far 'back', the light is placed from its anchor"); static public GUIContent upDirectionProperty = EditorGUIUtility.TrTextContent("Up direction", "Specifies the space in which the up direction of the anchor is defined. Local is relative to the camera."); + static public GUIContent anchorPositionOverrideProperty = EditorGUIUtility.TrTextContent("Anchor Position Override", "Specifies the anchor position manually instead of relying on the angles, distance and transform position to compute the anchor position."); static public GUIContent[] angleSubContent = new[] { EditorGUIUtility.TrTextContent("Orbit"), diff --git a/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs b/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs index 261952fa027..c55f8a3707a 100644 --- a/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs +++ b/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs @@ -22,6 +22,8 @@ public class LightAnchor : MonoBehaviour float m_Distance = 0f; [SerializeField] UpDirection m_FrameSpace = UpDirection.World; + [SerializeField] + Transform m_AnchorPositionOverride; float m_Yaw; float m_Pitch; @@ -68,8 +70,8 @@ public float roll /// public float distance { - get { return Mathf.Max(m_Distance, 0.0001f); } - set { m_Distance = value; } + get => m_Distance; + set => m_Distance = Mathf.Max(0, value); } /// @@ -101,7 +103,13 @@ public UpDirection frameSpace /// public Vector3 anchorPosition { - get { return transform.position + transform.forward * distance; } + get + { + if (anchorPositionOverride != null) + return anchorPositionOverride.position; + else + return transform.position + transform.forward * distance; + } } struct Axes @@ -111,6 +119,16 @@ struct Axes public Vector3 forward; } + /// + /// Overrides the pivot of used to compute the light position. This is useful to track an existing object in the scene. + /// The transform of the light will be automatically updated by the Update() method of the LightAnchor. + /// + public Transform anchorPositionOverride + { + get => m_AnchorPositionOverride; + set => m_AnchorPositionOverride = value; + } + /// /// Normalizes the input angle to be in the range of -180 and 180. /// @@ -181,6 +199,15 @@ Axes GetWorldSpaceAxes(Camera camera) }; } + void Update() + { + if (anchorPositionOverride == null || Camera.main == null) + return; + + if (anchorPositionOverride.hasChanged) + UpdateTransform(Camera.main, anchorPosition); + } + void OnDrawGizmosSelected() { var camera = Camera.main; @@ -231,7 +258,7 @@ void UpdateTransform(Vector3 up, Vector3 right, Vector3 forward, Vector3 anchor) Vector3 worldPosition = anchor + (worldYawRot * worldPitchRot) * forward * distance; transform.position = worldPosition; - Vector3 lookAt = (anchor - worldPosition).normalized; + Vector3 lookAt = -((worldYawRot * worldPitchRot) * forward).normalized; Vector3 angles = Quaternion.LookRotation(lookAt, up).eulerAngles; angles.z = m_Roll; transform.eulerAngles = angles; From 1fd279ce5a759438538c5e653eb87a08d3fa7501 Mon Sep 17 00:00:00 2001 From: Antoine Lelievre Date: Wed, 28 Jul 2021 14:34:59 +0200 Subject: [PATCH 07/11] Fix anchor position when target is null --- .../Editor/Lighting/LightAnchorEditor.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs b/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs index 8e6d84fb75d..ca07324e6ee 100644 --- a/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs +++ b/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs @@ -58,6 +58,10 @@ public override void OnInspectorGUI() UpdateCache(); } + // anchor is cached for it cannot be changed from the inspector, + // we have a dedicated editor tool to move the anchor + var anchor = manipulator.anchorPosition; + bool yawChanged = false; bool pitchChanged = false; bool rollChanged = false; @@ -289,7 +293,10 @@ public override void OnInspectorGUI() if (positionOverrideChanged) manipulator.anchorPositionOverride = m_AnchorPositionOverrideProperty.objectReferenceValue as Transform; - manipulator.UpdateTransform(camera, manipulator.anchorPosition); + if (manipulator.anchorPositionOverride != null) + anchor = manipulator.anchorPosition; + + manipulator.UpdateTransform(camera, anchor); IsCacheInvalid(manipulator); } } From dff2ab37732000fe33050e15b99d1abe988d3d38 Mon Sep 17 00:00:00 2001 From: Antoine Lelievre Date: Wed, 4 Aug 2021 14:47:33 +0200 Subject: [PATCH 08/11] Check light anchor position override when assigned --- .../Editor/Lighting/LightAnchorEditor.cs | 15 ++++++++++++++- .../Editor/Lighting/LightAnchorEditorTool.cs | 9 ++++++--- .../Editor/Lighting/LightAnchorHandles.cs | 13 +++++++++++-- .../Runtime/Lights/LightAnchor.cs | 11 +++++++++++ 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs b/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs index ca07324e6ee..10370103641 100644 --- a/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs +++ b/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs @@ -291,7 +291,20 @@ public override void OnInspectorGUI() if (distanceChanged) manipulator.distance = m_DistanceProperty.floatValue; if (positionOverrideChanged) - manipulator.anchorPositionOverride = m_AnchorPositionOverrideProperty.objectReferenceValue as Transform; + { + var newTransform = m_AnchorPositionOverrideProperty.objectReferenceValue as Transform; + + if (newTransform != null) + { + // Check that the assigned transform is not child of the light anchor, otherwise it would cause problems when moving the light position + if (newTransform.IsChildOf(manipulator.transform)) + Debug.LogError($"Can't assign '{newTransform.name}' because it's a child of the Light Anchor component"); + else + manipulator.anchorPositionOverride = newTransform; + } + else + manipulator.anchorPositionOverride = newTransform; + } if (manipulator.anchorPositionOverride != null) anchor = manipulator.anchorPosition; diff --git a/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditorTool.cs b/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditorTool.cs index 70118c61526..9b8d5caf817 100644 --- a/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditorTool.cs +++ b/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditorTool.cs @@ -30,7 +30,10 @@ public override GUIContent toolbarIcon /// Always return true public override bool IsAvailable() { - return true; + var lightAnchor = target as LightAnchor; + + // Hide the transform if a position override object is assigned + return (lightAnchor?.anchorPositionOverride == null); } /// @@ -39,7 +42,7 @@ public override bool IsAvailable() /// The window that is displaying the custom editor tool. public override void OnToolGUI(EditorWindow window) { - if (target == null) + if (target is LightAnchor l && l?.anchorPositionOverride == null) return; DoTargetGUI(target); @@ -112,7 +115,7 @@ LightAnchorHandles GetHandles(UnityObject obj) LightAnchorHandles handles; if (!m_LightAnchorHandles.TryGetValue(obj, out handles)) { - handles = new LightAnchorHandles(); + handles = new LightAnchorHandles(obj as LightAnchor); m_LightAnchorHandles.Add(obj, handles); } diff --git a/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorHandles.cs b/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorHandles.cs index 99bc7dd85e6..f01bc2d0394 100644 --- a/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorHandles.cs +++ b/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorHandles.cs @@ -16,11 +16,15 @@ public class LightAnchorHandles /// public Vector3 anchorPosition { get; set; } + LightAnchor target; + /// /// Initializes and returns an instance of LightAnchorHandles /// - public LightAnchorHandles() + /// Target object + public LightAnchorHandles(LightAnchor target) { + this.target = target; } /// @@ -31,7 +35,12 @@ public void OnGUI() Handles.color = Color.yellow; Handles.DrawDottedLine(lightPosition, anchorPosition, 2f); - anchorPosition = Handles.PositionHandle(anchorPosition, Quaternion.identity); + // Orient the handle rotation depending on the editor pivot rotation mode + var handleRotation = Quaternion.identity; + if (Tools.pivotRotation == PivotRotation.Local && target != null) + handleRotation = target.transform.rotation; + + anchorPosition = Handles.PositionHandle(anchorPosition, handleRotation); } } } diff --git a/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs b/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs index c55f8a3707a..0ce167e4b0d 100644 --- a/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs +++ b/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs @@ -153,6 +153,10 @@ public void SynchronizeOnTransform(Camera camera) Vector3 worldAnchorToLight = transform.position - anchorPosition; + // In case the distance is 0 or the anchor override is at the same position than the light anchor + if (worldAnchorToLight.magnitude == 0) + worldAnchorToLight = -transform.forward; + Vector3 projectOnGround = Vector3.ProjectOnPlane(worldAnchorToLight, axes.up); projectOnGround.Normalize(); @@ -187,6 +191,8 @@ Axes GetWorldSpaceAxes(Camera camera) viewToWorld = viewToWorld * Matrix4x4.Rotate(worldTilt); } + // TODO: these vectors are only correct when the light anchor is at the center of the screen. + // We can use the anchor pos to improve the results on the edge of the screen (problem caused by perspective) Vector3 up = (viewToWorld * Vector3.up).normalized; Vector3 right = (viewToWorld * Vector3.right).normalized; Vector3 forward = (viewToWorld * Vector3.forward).normalized; @@ -204,6 +210,9 @@ void Update() if (anchorPositionOverride == null || Camera.main == null) return; + // TODO: add checkbox that remove this update + // Realtime Position update + // Realtime Rotation update if (anchorPositionOverride.hasChanged) UpdateTransform(Camera.main, anchorPosition); } @@ -217,6 +226,8 @@ void OnDrawGizmosSelected() return; } + // TODO: fix light rotated when camera rotates + Axes axes = GetWorldSpaceAxes(camera); Vector3 anchor = anchorPosition; Vector3 d = transform.position - anchor; From 3b97dac9a4280eac6ca7c50a21079e5da7b80967 Mon Sep 17 00:00:00 2001 From: Antoine Lelievre Date: Wed, 4 Aug 2021 15:19:30 +0200 Subject: [PATCH 09/11] Fix light anchor transform override when assigned + refresh when camera moves --- .../Editor/Lighting/LightAnchorEditor.cs | 7 +++++++ .../Runtime/Lights/LightAnchor.cs | 13 ++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs b/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs index 10370103641..9e6660fbde7 100644 --- a/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs +++ b/com.unity.render-pipelines.core/Editor/Lighting/LightAnchorEditor.cs @@ -300,7 +300,14 @@ public override void OnInspectorGUI() if (newTransform.IsChildOf(manipulator.transform)) Debug.LogError($"Can't assign '{newTransform.name}' because it's a child of the Light Anchor component"); else + { + float newDistance = Vector3.Distance(manipulator.transform.position, newTransform.position); manipulator.anchorPositionOverride = newTransform; + // Orient the object to face the new override position + manipulator.SynchronizeOnTransform(camera); + // And adjust it's distance to avoid modifying it's position. + manipulator.distance = newDistance; + } } else manipulator.anchorPositionOverride = newTransform; diff --git a/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs b/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs index 0ce167e4b0d..ca886896e87 100644 --- a/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs +++ b/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs @@ -183,6 +183,17 @@ public void UpdateTransform(Camera camera, Vector3 anchor) Axes GetWorldSpaceAxes(Camera camera) { + // Fallback when the light anchor object is child of the camera (bad setup) + if (transform.IsChildOf(camera.transform)) + { + return new Axes + { + up = Vector3.up, + right = Vector3.right, + forward = Vector3.forward, + }; + } + Matrix4x4 viewToWorld = camera.cameraToWorldMatrix; if (m_FrameSpace == UpDirection.World) { @@ -213,7 +224,7 @@ void Update() // TODO: add checkbox that remove this update // Realtime Position update // Realtime Rotation update - if (anchorPositionOverride.hasChanged) + if (anchorPositionOverride.hasChanged || Camera.main.transform.hasChanged) UpdateTransform(Camera.main, anchorPosition); } From 72d74b1b886313732ddcb2bd0d866dca6c1ffeff Mon Sep 17 00:00:00 2001 From: Antoine Lelievre Date: Wed, 4 Aug 2021 18:13:26 +0200 Subject: [PATCH 10/11] "Fix" the rotation of the light with the camera + duplicate object fix --- .../Runtime/Lights/LightAnchor.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs b/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs index ca886896e87..7d35b8d5960 100644 --- a/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs +++ b/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs @@ -25,8 +25,11 @@ public class LightAnchor : MonoBehaviour [SerializeField] Transform m_AnchorPositionOverride; + [SerializeField] float m_Yaw; + [SerializeField] float m_Pitch; + [SerializeField] float m_Roll; /// @@ -195,6 +198,16 @@ Axes GetWorldSpaceAxes(Camera camera) } Matrix4x4 viewToWorld = camera.cameraToWorldMatrix; + + // Correct view to world for perspective + if (!camera.orthographic && camera.transform.position != anchorPosition) + { + var d = (anchorPosition - camera.transform.position).normalized; + var f = Quaternion.LookRotation(d); + viewToWorld = Matrix4x4.Scale(new Vector3(1, 1, -1)) * Matrix4x4.TRS(camera.transform.position, f, Vector3.one).inverse; + viewToWorld = viewToWorld.inverse; + } + if (m_FrameSpace == UpDirection.World) { Vector3 viewUp = (Vector3)(Camera.main.worldToCameraMatrix * Vector3.up); @@ -202,8 +215,6 @@ Axes GetWorldSpaceAxes(Camera camera) viewToWorld = viewToWorld * Matrix4x4.Rotate(worldTilt); } - // TODO: these vectors are only correct when the light anchor is at the center of the screen. - // We can use the anchor pos to improve the results on the edge of the screen (problem caused by perspective) Vector3 up = (viewToWorld * Vector3.up).normalized; Vector3 right = (viewToWorld * Vector3.right).normalized; Vector3 forward = (viewToWorld * Vector3.forward).normalized; @@ -221,9 +232,6 @@ void Update() if (anchorPositionOverride == null || Camera.main == null) return; - // TODO: add checkbox that remove this update - // Realtime Position update - // Realtime Rotation update if (anchorPositionOverride.hasChanged || Camera.main.transform.hasChanged) UpdateTransform(Camera.main, anchorPosition); } From 42d3258d4821b19160c8469130a09a1518c48e53 Mon Sep 17 00:00:00 2001 From: SoufianeKHIAT Date: Tue, 24 Aug 2021 17:24:04 +0200 Subject: [PATCH 11/11] Fix formating --- com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs b/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs index 7d35b8d5960..e8a88932005 100644 --- a/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs +++ b/com.unity.render-pipelines.core/Runtime/Lights/LightAnchor.cs @@ -204,7 +204,7 @@ Axes GetWorldSpaceAxes(Camera camera) { var d = (anchorPosition - camera.transform.position).normalized; var f = Quaternion.LookRotation(d); - viewToWorld = Matrix4x4.Scale(new Vector3(1, 1, -1)) * Matrix4x4.TRS(camera.transform.position, f, Vector3.one).inverse; + viewToWorld = Matrix4x4.Scale(new Vector3(1, 1, -1)) * Matrix4x4.TRS(camera.transform.position, f, Vector3.one).inverse; viewToWorld = viewToWorld.inverse; }