From 82de6a4bd16bcd84c74fdd471f6ca7663eaa2fda Mon Sep 17 00:00:00 2001 From: mszabo Date: Wed, 30 Oct 2024 22:36:31 +0100 Subject: [PATCH] Memoize Y-offset for pawn rendering Matrix4x4.rotation and Quaternion.eulerAngles are, somewhat confusingly, not fields but relatively costly property accessors. When rendering many pawns, repeated calls to them can have a measurable impact, so store the result in a local variable and reuse it. --- Source/CombatExtended/Harmony/Harmony_PawnRenderer.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Source/CombatExtended/Harmony/Harmony_PawnRenderer.cs b/Source/CombatExtended/Harmony/Harmony_PawnRenderer.cs index 3ba9f318b4..d952256b27 100644 --- a/Source/CombatExtended/Harmony/Harmony_PawnRenderer.cs +++ b/Source/CombatExtended/Harmony/Harmony_PawnRenderer.cs @@ -60,11 +60,13 @@ private static void DrawMesh(Mesh mesh, Matrix4x4 matrix, Material mat, int laye muzzleJump = -muzzleJump; casingOffset.x *= -1; } - matrix.SetTRS(position + posVec.RotatedBy(matrix.rotation.eulerAngles.y) + recoilOffset, Quaternion.AngleAxis(matrix.rotation.eulerAngles.y + muzzleJump, Vector3.up), scale); + + float yAngle = matrix.rotation.eulerAngles.y; + matrix.SetTRS(position + posVec.RotatedBy(yAngle) + recoilOffset, Quaternion.AngleAxis(yAngle + muzzleJump, Vector3.up), scale); CompEquippable compEquippable = eq.TryGetComp(); if (compEquippable != null && compEquippable.PrimaryVerb is Verb_ShootCE verbCE) { - verbCE.drawPos = casingDrawPos + (casingOffset + posVec).RotatedBy(matrix.rotation.eulerAngles.y); + verbCE.drawPos = casingDrawPos + (casingOffset + posVec).RotatedBy(yAngle); } if (eq is WeaponPlatform platform) {