Skip to content
Permalink
Browse files

Merge remote-tracking branch 'origin/master-3.0'

  • Loading branch information
xen2 committed Feb 6, 2019
2 parents 2a12c66 + bdec6ba commit 762e95559739055993afcb0fe67bc7c4b98acdee
@@ -1,13 +1,60 @@
// Copyright (c) Xenko contributors (https://xenko.com) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using Xenko.Core.Mathematics;
using Xenko.Engine;

namespace Xenko.Rendering.Sprites
{
public class RenderSprite : RenderObject
{
public SpriteComponent SpriteComponent;
private Matrix lastWorldMatrix;
private Vector2 lastHalfSpriteSize;
private SpriteType lastSpriteType;

public SpriteComponent SpriteComponent;
public TransformComponent TransformComponent;

internal void CalculateBoundingBox()
{
var transform = TransformComponent;
var currentSprite = SpriteComponent.CurrentSprite;

RenderGroup = SpriteComponent.RenderGroup;

// update the sprite bounding box
var halfSpriteSize = currentSprite?.Size / 2 ?? Vector2.Zero;
var worldMatrix = transform.WorldMatrix;

// Only calculate if we've changed...
if (lastWorldMatrix != worldMatrix || lastHalfSpriteSize != halfSpriteSize || lastSpriteType != SpriteComponent.SpriteType)
{
Vector3 halfBoxSize;
var boxWorldPosition = worldMatrix.TranslationVector;

if (SpriteComponent.SpriteType == SpriteType.Billboard)
{
// Make a gross estimation here as we don't have access to the camera view matrix
// TODO: move this code or grant camera view matrix access to this processor
var maxScale = Math.Max(worldMatrix.Row1.Length(), Math.Max(worldMatrix.Row2.Length(), worldMatrix.Row3.Length()));
halfBoxSize = maxScale * halfSpriteSize.Length() * Vector3.One;
}
else
{
halfBoxSize = new Vector3(
Math.Abs(worldMatrix.M11 * halfSpriteSize.X) + Math.Abs(worldMatrix.M21 * halfSpriteSize.Y),
Math.Abs(worldMatrix.M12 * halfSpriteSize.X) + Math.Abs(worldMatrix.M22 * halfSpriteSize.Y),
Math.Abs(worldMatrix.M13 * halfSpriteSize.X) + Math.Abs(worldMatrix.M23 * halfSpriteSize.Y));
}

// Update bounding box
BoundingBox = new BoundingBoxExt(boxWorldPosition - halfBoxSize, boxWorldPosition + halfBoxSize);

// Save current state for next check
lastWorldMatrix = worldMatrix;
lastHalfSpriteSize = halfSpriteSize;
lastSpriteType = SpriteComponent.SpriteType;
}
}
}
}
@@ -33,33 +33,7 @@ public override void Draw(RenderContext gameTime)

if (renderSprite.Enabled)
{
var transform = renderSprite.TransformComponent;

// TODO GRAPHICS REFACTOR: Proper bounding box. Reuse calculations in sprite batch.
// For now we only set a center for sorting, but no extent (which disable culling)
renderSprite.BoundingBox = new BoundingBoxExt { Center = transform.WorldMatrix.TranslationVector };
renderSprite.RenderGroup = renderSprite.SpriteComponent.RenderGroup;

// update the sprite bounding box
Vector3 halfBoxSize;
var halfSpriteSize = currentSprite?.Size / 2 ?? Vector2.Zero;
var worldMatrix = renderSprite.TransformComponent.WorldMatrix;
var boxOffset = worldMatrix.TranslationVector;
if (renderSprite.SpriteComponent.SpriteType == SpriteType.Billboard)
{
// Make a gross estimation here as we don't have access to the camera view matrix
// TODO: move this code or grant camera view matrix access to this processor
var maxScale = Math.Max(worldMatrix.Row1.Length(), Math.Max(worldMatrix.Row2.Length(), worldMatrix.Row3.Length()));
halfBoxSize = maxScale * halfSpriteSize.Length() * Vector3.One;
}
else
{
halfBoxSize = new Vector3(
Math.Abs(worldMatrix.M11 * halfSpriteSize.X + worldMatrix.M21 * halfSpriteSize.Y),
Math.Abs(worldMatrix.M12 * halfSpriteSize.X + worldMatrix.M22 * halfSpriteSize.Y),
Math.Abs(worldMatrix.M13 * halfSpriteSize.X + worldMatrix.M23 * halfSpriteSize.Y));
}
renderSprite.BoundingBox = new BoundingBoxExt(boxOffset - halfBoxSize, boxOffset + halfBoxSize);
renderSprite.CalculateBoundingBox();
}

// TODO Should we allow adding RenderSprite without a CurrentSprite instead? (if yes, need some improvement in RenderSystem)

0 comments on commit 762e955

Please sign in to comment.
You can’t perform that action at this time.