Skip to content

Commit

Permalink
FIxed transforms applying to mousemaps (OpenDreamProject#1889)
Browse files Browse the repository at this point in the history
  • Loading branch information
amylizzle authored Jul 19, 2024
1 parent cfcdc56 commit d0be78d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion OpenDreamClient/Rendering/DreamPlane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void DrawMouseMap(DrawingHandleWorld handle, DreamViewOverlay overlay, Ve
Color targetColor = new Color(colorR, colorG, colorB); //TODO - this could result in mis-clicks due to hash-collision since we ditch a whole byte.
overlay.MouseMapLookup[targetColor] = sprite;

handle.SetTransform(DreamViewOverlay.CreateRenderTargetFlipMatrix(renderTargetSize, pos));
handle.SetTransform(DreamViewOverlay.CalculateDrawingMatrix(sprite.TransformToApply, pos, texture.Size, renderTargetSize));
handle.DrawTextureRect(texture, new Box2(Vector2.Zero, texture.Size), targetColor);
}
}
Expand Down
38 changes: 21 additions & 17 deletions OpenDreamClient/Rendering/DreamViewOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,23 +405,7 @@ public void DrawIcon(DrawingHandleWorld handle, Vector2i renderTargetSize, Rende

handle.UseShader(GetBlendAndColorShader(iconMetaData, ignoreColor: true));

//extract scale component of transform
var transform = iconMetaData.TransformToApply;
Vector2 scaleFactors = new Vector2(
MathF.Sqrt(MathF.Pow(transform.M11,2) + MathF.Pow(transform.M12,2)),
MathF.Sqrt(MathF.Pow(transform.M21,2) + MathF.Pow(transform.M22,2))
);
transform.M11 /= scaleFactors.X;
transform.M12 /= scaleFactors.X;
transform.M21 /= scaleFactors.Y;
transform.M22 /= scaleFactors.Y;

handle.SetTransform(
Matrix3x2.CreateTranslation(-frame.Size/2) //translate to origin
* transform //rotate and translate
* Matrix3x2.CreateTranslation(frame.Size/2) //translate back to original position
* Matrix3x2.CreateScale(scaleFactors) //scale
* CreateRenderTargetFlipMatrix(renderTargetSize, pixelPosition-((scaleFactors-Vector2.One)*frame.Size/2))); //flip and apply scale-corrected translation
handle.SetTransform(CalculateDrawingMatrix(iconMetaData.TransformToApply, pixelPosition, frame.Size, renderTargetSize));
handle.DrawTextureRect(frame, Box2.FromDimensions(Vector2.Zero, frame.Size));
}

Expand Down Expand Up @@ -778,6 +762,26 @@ public static Matrix3x2 CreateRenderTargetFlipMatrix(Vector2i renderTargetSize,
// We must also handle translations here, since RT applies its own transform in an unexpected order
return FlipMatrix * Matrix3x2.CreateTranslation(renderPosition.X, renderTargetSize.Y - renderPosition.Y);
}

public static Matrix3x2 CalculateDrawingMatrix(Matrix3x2 transform, Vector2 pixelPosition, Vector2i frameSize, Vector2i renderTargetSize) {
//extract scale component of transform
Vector2 scaleFactors = new Vector2(
MathF.Sqrt(MathF.Pow(transform.M11,2) + MathF.Pow(transform.M12,2)),
MathF.Sqrt(MathF.Pow(transform.M21,2) + MathF.Pow(transform.M22,2))
);
transform.M11 /= scaleFactors.X;
transform.M12 /= scaleFactors.X;
transform.M21 /= scaleFactors.Y;
transform.M22 /= scaleFactors.Y;

return
Matrix3x2.CreateTranslation(-frameSize/2) //translate to origin
* transform //rotate and translate
* Matrix3x2.CreateTranslation(frameSize/2) //translate back to original position
* Matrix3x2.CreateScale(scaleFactors) //scale
* CreateRenderTargetFlipMatrix(renderTargetSize, pixelPosition-((scaleFactors-Vector2.One)*frameSize/2)); //flip and apply scale-corrected translation

}
}

internal sealed class RendererMetaData : IComparable<RendererMetaData> {
Expand Down

0 comments on commit d0be78d

Please sign in to comment.