diff --git a/com.unity.render-pipelines.core/CHANGELOG.md b/com.unity.render-pipelines.core/CHANGELOG.md index c4c59f93a8e..a4ddc266923 100644 --- a/com.unity.render-pipelines.core/CHANGELOG.md +++ b/com.unity.render-pipelines.core/CHANGELOG.md @@ -84,6 +84,7 @@ The version number for this package has increased due to a version update of a r - Fixed Lens Flare 'radialScreenAttenuationCurve invisible' - Fixed Lens Flare rotation for Curve Distribution - Fixed potentially conflicting runtime Rendering Debugger UI command by adding an option to disable runtime UI altogether (1345783). +- Fixed Lens Flare position for celestial at very far camera distances. It now locks correctly into the celestial position regardless of camera distance (1363291) - Fixed issues caused by automatically added EventSystem component, required to support Rendering Debugger Runtime UI input. (1361901) ### Changed diff --git a/com.unity.render-pipelines.core/Runtime/PostProcessing/LensFlareCommonSRP.cs b/com.unity.render-pipelines.core/Runtime/PostProcessing/LensFlareCommonSRP.cs index da489d36d09..8b810c628c0 100644 --- a/com.unity.render-pipelines.core/Runtime/PostProcessing/LensFlareCommonSRP.cs +++ b/com.unity.render-pipelines.core/Runtime/PostProcessing/LensFlareCommonSRP.cs @@ -272,7 +272,19 @@ static Vector2 GetLensFlareRayOffset(Vector2 screenPos, float position, float gl globalSin0 * rayOff.x + globalCos0 * rayOff.y); } - static Vector3 WorldToViewport(bool isCameraRelative, Matrix4x4 viewProjMatrix, Vector3 cameraPosWS, Vector3 positionWS) + static Vector3 WorldToViewport(Camera camera, bool isLocalLight, bool isCameraRelative, Matrix4x4 viewProjMatrix, Vector3 positionWS) + { + if (isLocalLight) + { + return WorldToViewportLocal(isCameraRelative, viewProjMatrix, camera.transform.position, positionWS); + } + else + { + return WorldToViewportDistance(camera, positionWS); + } + } + + static Vector3 WorldToViewportLocal(bool isCameraRelative, Matrix4x4 viewProjMatrix, Vector3 cameraPosWS, Vector3 positionWS) { Vector3 localPositionWS = positionWS; if (isCameraRelative) @@ -286,7 +298,18 @@ static Vector3 WorldToViewport(bool isCameraRelative, Matrix4x4 viewProjMatrix, viewportPos.y = viewportPos.y * 0.5f + 0.5f; viewportPos.y = 1.0f - viewportPos.y; viewportPos.z = viewportPos4.w; + return viewportPos; + } + static Vector3 WorldToViewportDistance(Camera cam, Vector3 positionWS) + { + Vector4 camPos = cam.worldToCameraMatrix * positionWS; + Vector4 viewportPos4 = cam.projectionMatrix * camPos; + Vector3 viewportPos = new Vector3(viewportPos4.x, viewportPos4.y, 0f); + viewportPos /= viewportPos4.w; + viewportPos.x = viewportPos.x * 0.5f + 0.5f; + viewportPos.y = viewportPos.y * 0.5f + 0.5f; + viewportPos.z = viewportPos4.w; return viewportPos; } @@ -386,7 +409,7 @@ static public void DoLensFlareDataDrivenCommon(Material lensFlareShader, LensFla positionWS = comp.transform.position; } - viewportPos = WorldToViewport(isCameraRelative, viewProjMatrix, cam.transform.position, positionWS); + viewportPos = WorldToViewport(cam, !isDirLight, isCameraRelative, viewProjMatrix, positionWS); if (usePanini && cam == Camera.main) { @@ -421,11 +444,11 @@ static public void DoLensFlareDataDrivenCommon(Material lensFlareShader, LensFla globalColorModulation *= distanceAttenuation; Vector3 dir = (cam.transform.position - comp.transform.position).normalized; - Vector3 screenPosZ = WorldToViewport(isCameraRelative, viewProjMatrix, cam.transform.position, positionWS + dir * comp.occlusionOffset); + Vector3 screenPosZ = WorldToViewport(cam, !isDirLight, isCameraRelative, viewProjMatrix, positionWS + dir * comp.occlusionOffset); float adjustedOcclusionRadius = isDirLight ? comp.celestialProjectedOcclusionRadius(cam) : comp.occlusionRadius; Vector2 occlusionRadiusEdgeScreenPos0 = (Vector2)viewportPos; - Vector2 occlusionRadiusEdgeScreenPos1 = (Vector2)WorldToViewport(isCameraRelative, viewProjMatrix, cam.transform.position, positionWS + cam.transform.up * adjustedOcclusionRadius); + Vector2 occlusionRadiusEdgeScreenPos1 = (Vector2)WorldToViewport(cam, !isDirLight, isCameraRelative, viewProjMatrix, positionWS + cam.transform.up * adjustedOcclusionRadius); float occlusionRadius = (occlusionRadiusEdgeScreenPos1 - occlusionRadiusEdgeScreenPos0).magnitude; cmd.SetGlobalVector(_FlareData1, new Vector4(occlusionRadius, comp.sampleCount, screenPosZ.z, actualHeight / actualWidth));