Skip to content

Commit

Permalink
[Graphics] Fix light component API (#2215)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eideren authored May 29, 2024
1 parent 3f0c3ce commit 817da70
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 59 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,38 @@ namespace Stride.Rendering.Lights
/// </summary>
[DataContract("LightDirectional")]
[Display("Directional")]
public class LightDirectional : DirectLightBase
public class LightDirectional : ColorLightBase, IDirectLight
{
/// <summary>
/// Gets or sets the shadow.
/// </summary>
/// <value>The shadow.</value>
/// <userdoc>The settings of the light shadow</userdoc>
[DataMember(200)]
public LightDirectionalShadowMap Shadow { get; set; }

public LightDirectional()
{
Shadow = new LightDirectionalShadowMap
Shadow = new()
{
Size = LightShadowMapSize.Large,
};
}

public override bool HasBoundingBox
public bool HasBoundingBox
{
get
{
return false;
}
}

public override BoundingBox ComputeBounds(Vector3 position, Vector3 direction)
public BoundingBox ComputeBounds(Vector3 position, Vector3 direction)
{
return BoundingBox.Empty;
}

public override float ComputeScreenCoverage(RenderView renderView, Vector3 position, Vector3 direction)
public float ComputeScreenCoverage(RenderView renderView, Vector3 position, Vector3 direction)
{
// As the directional light is covering the whole screen, we take the max of current width, height
return Math.Max(renderView.ViewSize.X, renderView.ViewSize.Y);
Expand All @@ -47,5 +55,7 @@ public override bool Update(RenderLight light)
{
return true;
}

LightShadowMap IDirectLight.Shadow => Shadow;
}
}
20 changes: 15 additions & 5 deletions sources/engine/Stride.Rendering/Rendering/Lights/LightPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ namespace Stride.Rendering.Lights
/// </summary>
[DataContract("LightPoint")]
[Display("Point")]
public class LightPoint : DirectLightBase
public class LightPoint : ColorLightBase, IDirectLight
{
/// <summary>
/// Initializes a new instance of the <see cref="LightPoint"/> class.
/// </summary>
public LightPoint()
{
Radius = 1.0f;
Shadow = new LightPointShadowMap()
Shadow = new()
{
Size = LightShadowMapSize.Small,
Type = LightPointShadowMapType.CubeMap,
Expand All @@ -42,7 +42,15 @@ public LightPoint()
[DataMemberIgnore]
internal float InvSquareRadius;

public override bool HasBoundingBox
/// <summary>
/// Gets or sets the shadow.
/// </summary>
/// <value>The shadow.</value>
/// <userdoc>The settings of the light shadow</userdoc>
[DataMember(200)]
public LightPointShadowMap Shadow { get; set; }

public bool HasBoundingBox
{
get
{
Expand All @@ -57,12 +65,12 @@ public override bool Update(RenderLight light)
return true;
}

public override BoundingBox ComputeBounds(Vector3 positionWS, Vector3 directionWS)
public BoundingBox ComputeBounds(Vector3 positionWS, Vector3 directionWS)
{
return new BoundingBox(positionWS - Radius, positionWS + Radius);
}

public override float ComputeScreenCoverage(RenderView renderView, Vector3 position, Vector3 direction)
public float ComputeScreenCoverage(RenderView renderView, Vector3 position, Vector3 direction)
{
// http://stackoverflow.com/questions/21648630/radius-of-projected-sphere-in-screen-space
var targetPosition = new Vector4(position, 1.0f);
Expand All @@ -82,5 +90,7 @@ public override float ComputeScreenCoverage(RenderView renderView, Vector3 posit
// Size on screen
return (float)pr * Math.Max(renderView.ViewSize.X, renderView.ViewSize.Y);
}

LightShadowMap IDirectLight.Shadow => Shadow;
}
}
18 changes: 14 additions & 4 deletions sources/engine/Stride.Rendering/Rendering/Lights/LightSpot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Stride.Rendering.Lights
/// </summary>
[DataContract("LightSpot")]
[Display("Spot")]
public class LightSpot : DirectLightBase
public class LightSpot : ColorLightBase, IDirectLight
{
// These values have to match the ones defined in "TextureProjectionReceiverBase.sdsl".
public enum FlipModeEnum
Expand Down Expand Up @@ -158,15 +158,23 @@ public override bool Update(RenderLight light)
return true;
}

public override bool HasBoundingBox
/// <summary>
/// Gets or sets the shadow.
/// </summary>
/// <value>The shadow.</value>
/// <userdoc>The settings of the light shadow</userdoc>
[DataMember(200)]
public LightStandardShadowMap Shadow { get; set; }

public bool HasBoundingBox
{
get
{
return true;
}
}

public override BoundingBox ComputeBounds(Vector3 position, Vector3 direction)
public BoundingBox ComputeBounds(Vector3 position, Vector3 direction)
{
// Calculates the bouding box of the spot target
var spotTarget = position + direction * Range;
Expand All @@ -178,7 +186,7 @@ public override BoundingBox ComputeBounds(Vector3 position, Vector3 direction)
return box;
}

public override float ComputeScreenCoverage(RenderView renderView, Vector3 position, Vector3 direction)
public float ComputeScreenCoverage(RenderView renderView, Vector3 position, Vector3 direction)
{
// TODO: We could improve this by calculating a screen-aligned triangle and a sphere at the end of the cone.
// With the screen-aligned triangle we would cover the entire spotlight, not just its end.
Expand All @@ -204,5 +212,7 @@ public override float ComputeScreenCoverage(RenderView renderView, Vector3 posit
// Size on screen
return (float)pr * Math.Max(renderView.ViewSize.X, renderView.ViewSize.Y);
}

LightShadowMap IDirectLight.Shadow => Shadow;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public override bool CanRenderLight(IDirectLight light)
public override LightShadowMapTexture CreateShadowMapTexture(RenderView renderView, RenderLight renderLight, IDirectLight light, int shadowMapSize)
{
var shadowMap = base.CreateShadowMapTexture(renderView, renderLight, light, shadowMapSize);
shadowMap.CascadeCount = ((LightDirectionalShadowMap)light.Shadow).GetCascadeCount();
shadowMap.CascadeCount = ((LightDirectional)light).Shadow.GetCascadeCount();
// Views with orthographic cameras cannot use cascades, we force it to 1 shadow map here.
if (renderView.Projection.M44 == 1.0f)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ public override ILightShadowMapShaderGroupData CreateShaderGroupData(LightShadow

public override bool CanRenderLight(IDirectLight light)
{
var lightPoint = light as LightPoint;
if (lightPoint != null)
return ((LightPointShadowMap)lightPoint.Shadow).Type == LightPointShadowMapType.CubeMap;
if (light is LightPoint lightPoint)
return lightPoint.Shadow.Type == LightPointShadowMapType.CubeMap;
return false;
}

Expand Down Expand Up @@ -85,7 +84,7 @@ private static void GetViewParameters(LightShadowMapTexture shadowMapTexture, in
/// </returns>
private static Vector2 GetShadowMapDepthParameters(LightShadowMapTexture shadowMapTexture)
{
var lightPoint = shadowMapTexture.Light as LightPoint;
var lightPoint = (LightPoint)shadowMapTexture.Light;
Vector2 clippingPlanes = GetLightClippingPlanes(lightPoint);

return CameraKeys.ZProjectionACalculate(clippingPlanes.X, clippingPlanes.Y);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,8 @@ public override ILightShadowMapShaderGroupData CreateShaderGroupData(LightShadow

public override bool CanRenderLight(IDirectLight light)
{
var pl = light as LightPoint;
if (pl != null)
{
var type = ((LightPointShadowMap)pl.Shadow).Type;
return type == LightPointShadowMapType.DualParaboloid;
}
if (light is LightPoint lightPoint)
return lightPoint.Shadow.Type == LightPointShadowMapType.DualParaboloid;
return false;
}

Expand Down Expand Up @@ -121,7 +117,7 @@ public override void Collect(RenderContext context, RenderView sourceView, Light
/// </summary>
private void CalculateViewDirection(LightShadowMapTexture shadowMapTexture)
{
var pointShadowMapTexture = shadowMapTexture as ShadowMapTexture;
var pointShadowMapTexture = (ShadowMapTexture)shadowMapTexture;
Matrix.Orthonormalize(ref shadowMapTexture.RenderLight.WorldMatrix, out pointShadowMapTexture.ForwardMatrix);
pointShadowMapTexture.ForwardMatrix.Invert();
}
Expand All @@ -134,7 +130,7 @@ private void CalculateViewDirection(LightShadowMapTexture shadowMapTexture)
/// </returns>
private Vector2 GetShadowMapDepthParameters(LightShadowMapTexture shadowMapTexture)
{
var lightPoint = shadowMapTexture.Light as LightPoint;
var lightPoint = (LightPoint)shadowMapTexture.Light;
Vector2 clippingPlanes = GetLightClippingPlanes(lightPoint);
return new Vector2(clippingPlanes.X, 1.0f / (clippingPlanes.Y - clippingPlanes.X));
}
Expand All @@ -146,12 +142,12 @@ private Vector2 GetLightClippingPlanes(LightPoint pointLight)

private float GetShadowMapFarPlane(LightShadowMapTexture shadowMapTexture)
{
return GetLightClippingPlanes(shadowMapTexture.Light as LightPoint).Y;
return GetLightClippingPlanes((LightPoint)shadowMapTexture.Light).Y;
}

private void GetViewParameters(LightShadowMapTexture shadowMapTexture, int index, out Matrix view, bool forCasting)
{
var pointShadowMapTexture = shadowMapTexture as ShadowMapTexture;
var pointShadowMapTexture = (ShadowMapTexture)shadowMapTexture;
Matrix flippingMatrix = Matrix.Identity;

// Flip Y for rendering shadow maps
Expand Down

0 comments on commit 817da70

Please sign in to comment.