Skip to content

Commit

Permalink
fix(export): Area light's range value is exported accurately in the E…
Browse files Browse the repository at this point in the history
…ditor (as shown in the inspector). (atteneder#106)

* fix: Light.areaSize is not available in builds, so assume default values.
  • Loading branch information
atteneder authored and GitHub Enterprise committed Mar 1, 2024
1 parent a2a6fd9 commit 98ac9fb
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Compiler errors when safe mode (`GLTFAST_SAFE` scripting define) is enabled.
- Compiler error with High Definition Render Pipeline version 17 (2023.3)
- Removed usage of obsolete APIs in High Definition Render Pipeline version 17 (2023.3)
- (Export) Area light's range value is exported accurately (as shown in the inspector).

## [6.2.0] - 2024-01-29

Expand Down
55 changes: 47 additions & 8 deletions Runtime/Scripts/Export/GltfWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,19 +234,16 @@ public bool AddLight(Light uLight, out int lightId)
name = uLight.name
};

var renderPipeline = RenderPipelineUtils.RenderPipeline;

var lightType = uLight.type;

var renderPipeline = RenderPipelineUtils.RenderPipeline;
#if USING_HDRP
HDAdditionalLightData lightHd = null;

if (renderPipeline == RenderPipeline.HighDefinition) {
lightHd = uLight.gameObject.GetComponent<HDAdditionalLightData>();
#if !UNITY_2023_2_OR_NEWER
// For newer HDRP versions, the generic `uLight.type` works just fine
if (lightHd!=null && lightHd.type == HDLightType.Area) {
lightType = LightType.Rectangle;
}
#endif
lightType = lightHd?.TryGetLightType() ?? lightType;
}
#endif

Expand Down Expand Up @@ -279,7 +276,7 @@ public bool AddLight(Light uLight, out int lightId)
}

light.LightColor = uLight.color.linear;
light.range = uLight.range;
light.range = GetLightRange(uLight, lightType);

switch (renderPipeline)
{
Expand Down Expand Up @@ -356,6 +353,48 @@ float GetIntensity(LightUnit unit) {
return true;
}

/// <summary>
/// Retrieves the light's range.
/// In Unity 2023.1 and older `Light.range` would return what now is <see cref="Light.dilatedRange"/>, which is
/// the range extended by a certain area light size factor. This method removes that addition in that case to
/// get the original value that's shown in the inspector.
/// </summary>
/// <param name="light">Unity Light</param>
/// <param name="lightType">Actual light type (might differ from light.type in HDRP).</param>
/// <returns>The light's range.</returns>
static float GetLightRange(Light light, LightType lightType)
{
#if UNITY_2023_2_OR_NEWER
return light.range;
#else
var range = light.range;
switch (lightType)
{
#if !USING_HDRP // And, of course, it behaves correctly in the particular case HDRP+Rectangle.
case LightType.Rectangle:
#if UNITY_EDITOR
var longestSide = light.areaSize.magnitude;
#else
// At runtime, assume default magnitude(vec2(1,1))
var longestSide = 1.4142135624f;
#endif
range -= longestSide * .5f;
break;
#endif // !USING_HDRP
case LightType.Disc:
#if UNITY_EDITOR
var radius = light.areaSize.x;
#else
// At runtime, assume default
const float radius = 1f;
#endif
range -= radius * .5f;
break;
}
return range;
#endif
}

/// <inheritdoc />
public void AddCameraToNode(int nodeId, int cameraId)
{
Expand Down
39 changes: 39 additions & 0 deletions Runtime/Scripts/Export/HDAdditionalLightDataExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-FileCopyrightText: 2024 Unity Technologies and the glTFast authors
// SPDX-License-Identifier: Apache-2.0

#if USING_HDRP && !UNITY_2023_2_OR_NEWER

using UnityEngine;
using UnityEngine.Rendering.HighDefinition;

namespace GLTFast.Export
{
static class HDAdditionalLightDataExtensions
{
/// <summary>
/// In older HDRP versions Light.type may not reflect the actual light type.
/// To get the actual light type, the HDAdditionalLightData component has to be queried.
/// </summary>
/// <param name="lightHd">HDRP specific light component.</param>
/// <returns>The HDRP specific light type or null otherwise.</returns>
public static LightType? TryGetLightType(this HDAdditionalLightData lightHd)
{
switch (lightHd.type)
{
case HDLightType.Area:
switch (lightHd.areaLightShape)
{
case AreaLightShape.Rectangle:
return LightType.Rectangle;
case AreaLightShape.Tube:
case AreaLightShape.Disc:
default:
return LightType.Disc;
}
}

return null;
}
}
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 98ac9fb

Please sign in to comment.