diff --git a/PCK-Studio/Rendering/CubeMesh.cs b/PCK-Studio/Rendering/CubeMesh.cs index 24e6222a..7e0a8de3 100644 --- a/PCK-Studio/Rendering/CubeMesh.cs +++ b/PCK-Studio/Rendering/CubeMesh.cs @@ -51,8 +51,6 @@ namespace PckStudio.Rendering 22, 23, 20 ]; - public override Matrix4 Transform => Matrix4.Identity; - internal static VertexBufferLayout VertexBufferLayout { get; } = new VertexBufferLayout().Add(ShaderDataType.Float3).Add(ShaderDataType.Float2); public CubeMesh(Cube cube) : this(nameof(CubeMesh), cube) @@ -79,9 +77,14 @@ namespace PckStudio.Rendering public Cube GetCube() => _cube; + public override Matrix4 GetTransform() + { + return Matrix4.Identity; + } + public override BoundingBox GetBounds(Matrix4 transform) { - return _cube.GetBoundingBox(Transform * transform); + return _cube.GetBoundingBox(GetTransform() * transform); } internal override IEnumerable GetVertices() @@ -90,7 +93,7 @@ namespace PckStudio.Rendering Vector2 uv = _cube.Uv; - BoundingBox boundingBox = GetBounds(Transform); + BoundingBox boundingBox = GetBounds(GetTransform()); Vector3 from = boundingBox.Start; Vector3 to = boundingBox.End; diff --git a/PCK-Studio/Rendering/CubeMeshCollection.cs b/PCK-Studio/Rendering/CubeMeshCollection.cs index 166a6d4e..1e8a87d3 100644 --- a/PCK-Studio/Rendering/CubeMeshCollection.cs +++ b/PCK-Studio/Rendering/CubeMeshCollection.cs @@ -58,7 +58,16 @@ namespace PckStudio.Rendering set => _offset = value; } - public override Matrix4 Transform => (Matrix4.CreateRotationX(MathHelper.DegreesToRadians(Rotation.X)) * Matrix4.CreateRotationY(MathHelper.DegreesToRadians(Rotation.Y)) * Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(Rotation.Z))).Pivoted(Translation + _offset, Pivot); + public override Matrix4 GetTransform() + { + Matrix4 rotations = ( + Matrix4.CreateRotationX(MathHelper.DegreesToRadians(Rotation.X)) * + Matrix4.CreateRotationY(MathHelper.DegreesToRadians(Rotation.Y)) * + Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(Rotation.Z)) + ); + Matrix4 translation = Matrix4.CreateTranslation(Translation + Offset); + return translation * (rotations).Pivoted(Pivot - Offset); + } public int Count => cubes.Count; @@ -92,7 +101,7 @@ namespace PckStudio.Rendering internal override IEnumerable GetVertices() => cubes.Where(c => c.Visible).SelectMany(c => - c.GetVertices().Select(vertex => new TextureVertex(Vector3.TransformPosition(vertex.Position, c.Transform), vertex.TexPosition)) + c.GetVertices().Select(vertex => new TextureVertex(Vector3.TransformPosition(vertex.Position, c.GetTransform()), vertex.TexPosition)) ); internal override IEnumerable GetIndices() @@ -156,7 +165,7 @@ namespace PckStudio.Rendering if (!cubes.IndexInRange(index)) throw new IndexOutOfRangeException(); - return cubes[index].GetBounds(Transform).Center; + return cubes[index].GetBounds(GetTransform()).Center; } internal BoundingBox GetCubeBoundingBox(int index) @@ -164,14 +173,14 @@ namespace PckStudio.Rendering if (!cubes.IndexInRange(index)) throw new IndexOutOfRangeException(); - return cubes[index].GetBounds(Transform); + return cubes[index].GetBounds(GetTransform()); } public override BoundingBox GetBounds(Matrix4 transform) { IEnumerable boundingBoxes = cubes .Where(c => c.Visible) - .Select(c => c.GetBounds(Transform * transform)); + .Select(c => c.GetBounds(GetTransform() * transform)) return BoundingBox.GetEnclosingBoundingBox(boundingBoxes); } @@ -181,7 +190,7 @@ namespace PckStudio.Rendering throw new IndexOutOfRangeException(); Vector3 faceCenter = cubes[index] is CubeMesh c ? c.GetCube().GetFaceCenter(face) : Vector3.Zero; - return Vector3.TransformPosition(faceCenter, Transform); + return Vector3.TransformPosition(faceCenter, GetTransform()); } internal void SetVisible(int index, bool visible) diff --git a/PCK-Studio/Rendering/GenericMesh.cs b/PCK-Studio/Rendering/GenericMesh.cs index b691281d..7d5e4cea 100644 --- a/PCK-Studio/Rendering/GenericMesh.cs +++ b/PCK-Studio/Rendering/GenericMesh.cs @@ -29,7 +29,6 @@ namespace PckStudio.Rendering internal VertexBufferLayout VertexLayout { get; } internal bool Visible { get; } - public abstract Matrix4 Transform { get; } protected GenericMesh(string name, bool visible, PrimitiveType type, VertexBufferLayout vertexLayout) { @@ -41,6 +40,7 @@ namespace PckStudio.Rendering public abstract GenericMesh SetVisible(bool visible); public abstract BoundingBox GetBounds(Matrix4 transform); + public abstract Matrix4 GetTransform(); internal abstract IEnumerable GetVertices(); internal abstract IEnumerable GetIndices(); diff --git a/PCK-Studio/Rendering/ModelRenderer.cs b/PCK-Studio/Rendering/ModelRenderer.cs index 8f697da7..ab09399d 100644 --- a/PCK-Studio/Rendering/ModelRenderer.cs +++ b/PCK-Studio/Rendering/ModelRenderer.cs @@ -217,7 +217,7 @@ namespace PckStudio.Rendering foreach (CubeMeshCollection item in _rootCollection) { - DrawMesh(item, shader, item.Transform * Matrix4.CreateScale(1f, -1f, -1f)); + DrawMesh(item, shader, item.GetTransform() * Matrix4.CreateScale(1f, -1f, -1f)); } _modelRenderTexture.Unbind(); } diff --git a/PCK-Studio/Rendering/SkinRenderer.cs b/PCK-Studio/Rendering/SkinRenderer.cs index 0c6234e8..c7cb5c59 100644 --- a/PCK-Studio/Rendering/SkinRenderer.cs +++ b/PCK-Studio/Rendering/SkinRenderer.cs @@ -1055,7 +1055,7 @@ namespace PckStudio.Rendering float inflate = autoInflateOverlayParts && box.IsOverlayPart() ? box.Type == "HEADWEAR" ? OverlayScale * 2 : OverlayScale : 0f; Cube cube = box.ToCube(inflate); CubeMeshCollection cubeMesh = meshStorage[box.Type]; - yield return cube.GetBoundingBox(cubeMesh.Transform); + yield return cube.GetBoundingBox(cubeMesh.GetTransform()); } yield break; } @@ -1072,7 +1072,7 @@ namespace PckStudio.Rendering private void RenderPart(ShaderProgram shader, GenericMesh mesh, Matrix4 partMatrix, Matrix4 globalMatrix) where T : struct { - Matrix4 transform = partMatrix * mesh.Transform * globalMatrix; + Matrix4 transform = partMatrix * mesh.GetTransform() * globalMatrix; DrawMesh(mesh, shader, transform); }