public Matrix4 GetLocalToWorldMatrix() { bool matrixRecalculated = false; if (matrixRefreshNeeded) { matrixCache = CalculateMatrix(); matrixRefreshNeeded = false; matrixRecalculated = true; } if (parentTransform != null) { if (parentLocalToWorldMatrixCache != parentTransform.GetLocalToWorldMatrix()) { parentLocalToWorldMatrixCache = parentTransform.GetLocalToWorldMatrix(); localToWorldMatrixCache = parentLocalToWorldMatrixCache * matrixCache; } if (matrixRecalculated) { localToWorldMatrixCache = parentLocalToWorldMatrixCache * matrixCache; } return(localToWorldMatrixCache); } return(matrixCache); }
public void SetParent(Transform3D newParentTransform, bool worldPositionStays = true) { if (worldPositionStays && (newParentTransform != null || parentTransform != null)) { var newTransformMatrix = GetLocalToWorldMatrix(); if (newParentTransform != null) { // (N = P * M) => (P-1 * N = P-1 * P * M) => (P-1 * N = M) newTransformMatrix = Math.Inverse(newParentTransform.GetLocalToWorldMatrix()) * newTransformMatrix; } var newPosition = Math.DecomposeMatrixGetPosition(newTransformMatrix); var newScale = Math.DecomposeMatrixGetScale(newTransformMatrix); var newOrientation = Math.DecomposeMatrixGetOrientation(newTransformMatrix, newScale); SetLocalPosition(newPosition); SetLocalScale(newScale); SetLocalRotation(newOrientation); } parentTransform = newParentTransform; parentLocalToWorldMatrixCache = Matrix4.identity; parentWorldToLocalMatrixCache = Matrix4.identity; }
public override void Draw(Graphics graphics) { camera.Use(graphics); { Tests.Draw(graphics); testmaterial.SetPass(0); graphics.SetModelToWorldMatrix(testtransform.GetLocalToWorldMatrix()); graphics.DrawMesh(testmesh); shapesMaterial.SetPass(0); graphics.SetModelToWorldMatrix(Matrix4.identity); for (int i = 0; i < rays.Count; ++i) { Shapes.DrawLine(rays[i].position, rays[i].GetPoint(100), new Color(1, 0, 0, 0.5f)); } testmaterial.SetPass(0); // ??? graphics.SetModelToWorldMatrix(testtransformbillboard.GetLocalToWorldMatrix()); graphics.DrawTexture(billboard, new IntRect(new Vector2i(-billboard.GetWidth() / 2, 0), billboard.GetSize())); shapesMaterial.SetPass(0); graphics.SetModelToWorldMatrix(Matrix4.identity); //Shapes.DrawSphere(testtransformbillboard.GetPosition(), 0.1f, Color.green); } hudCamera.Use(graphics); { shapesMaterial.SetPass(0); Shapes.DrawRectangle((camera.WorldToScreenPixel(Vector2f.zero) / hudCamera.GetZoom()) - new Vector2f(5, 5), 10, 10, Color.blue); graphics.DrawTexture(proceduralTexture, new IntRect(Vector2i.zero, Math.Round(proceduralTexture.GetSize() * 0.125f))); } }
public override void Draw() { camera.Use(); { // grid draw shapesMaterial.Use(); Core.graphics.DrawMesh(gridMesh); // model test draw testmaterial.Use(); Core.graphics.SetModelToWorldMatrix(testtransform.GetLocalToWorldMatrix()); Core.graphics.DrawMesh(testmesh); // model test bounding box draw shapesMaterial.Use(); Core.graphics.SetWireframe(true); Models.DrawCube(testmesh.boundingBox, Color.red); Core.graphics.SetWireframe(false); // ray test draw shapesMaterial.Use(); Core.graphics.SetModelToWorldMatrix(Matrix4.identity); for (int i = 0; i < rays.Count; ++i) { Shapes.DrawLine(rays[i].position, rays[i].GetPoint(100), new Color(1, 0, 0, 0.5f)); } // model draw test //shapesMaterial.Use(); //Core.graphics.SetModelToWorldMatrix(Matrix4.identity); //Models.DrawCylinder(new Vector3f(10, 1, 10), 5f, 5f, 8f, 32, Color.green); //Core.graphics.SetWireframe(true); //Models.DrawCylinder(new Vector3f(10, 1, 10), 5f, 5f, 8f, 32, Color.maroon); //Core.graphics.SetWireframe(false); // billboard draw (note: last because is transparent object) spritesMaterial.Set("_MainTex", billboard); spritesMaterial.Use(); Core.graphics.SetModelToWorldMatrix(testtransformbillboard.GetLocalToWorldMatrix()); Sprites.DrawQuad(billboard.GetSize(), new IntRect(new Vector2i(-billboard.GetWidth() / 2, 0), billboard.GetSize())); } hudCamera.Use(); { // square indicator test draw shapesMaterial.Use(); Shapes.DrawRectangle((camera.WorldToScreenPixel(Vector2f.zero) / hudCamera.GetZoom()) - new Vector2f(5, 5), 10, 10, Color.blue); // procedural texture draw spritesMaterial.Set("_MainTex", proceduralTexture); spritesMaterial.Use(); Sprites.DrawQuad(proceduralTexture.GetSize(), new IntRect(Vector2i.zero, Math.Round(proceduralTexture.GetSize() * 0.125f))); } }
private static void Graphics_Test_Meshes_1(Graphics graphics) { // TODO: Optimizar var meshMaterial = Core.resource.CreateMaterial("Mobile/VertexLit"); // Standard meshMaterial.SetPass(0); var transform = new Transform3D(); transform.Rotate(new Vector3f(Core.fixedTime.seconds, 0f, 0f)); graphics.SetModelToWorldMatrix(transform.GetLocalToWorldMatrix()); graphics.DrawMesh(CreateTorus(10f, 3f, 24, 18)); }
public void Rotate(Vector3f eulerAngles, Transform3D relativeTo) { Rotate((relativeTo.GetLocalToWorldMatrix() * new Quat(eulerAngles).ToMatrix4()).ToQuaternion(), true); }