protected override void OnUpdate(GameTime gameTime) { base.OnUpdate(gameTime); var item = Item; if (item == null || item.Renderer == null) { return; } var bounds = Screen.RenderBounds; if (bounds != _previousBounds) { //var c = bounds.Center; //Camera.RenderPosition = new Vector3(c.X, c.Y, 0.0f); //Camera.UpdateProjectionMatrix(); //Camera.UpdateAspectRatio((float) bounds.Width / (float) bounds.Height); Camera.UpdateAspectRatio(bounds.Width / (float)bounds.Height); _previousBounds = bounds; } // Camera.MoveTo(EntityPosition, Vector3.Zero); // Camera.UpdateProjectionMatrix(); var args = new UpdateArgs() { GameTime = gameTime, Camera = Camera, GraphicsDevice = Alex.Instance.GraphicsDevice }; Camera.Update(args); Camera.UpdateProjectionMatrix(); item.Renderer.Update(args, MCMatrix.CreateTranslation(new Vector3(0, 0, 0))); }
/// <summary> /// Creates a new <see cref="T:Microsoft.Xna.Framework.Vector3" /> that contains a transformation of 3d-vector by the specified <see cref="T:Microsoft.Xna.Framework.Matrix" />. /// </summary> /// <param name="position">Source <see cref="T:Microsoft.Xna.Framework.Vector3" />.</param> /// <param name="matrix">The transformation <see cref="T:Microsoft.Xna.Framework.Matrix" />.</param> /// <returns>Transformed <see cref="T:Microsoft.Xna.Framework.Vector3" />.</returns> public static Vector3 Transform(this Vector3 position, MCMatrix matrix) { return(Vector3.Transform(position, matrix)); Transform(ref position, ref matrix, out position); return(position); }
private void UpdateThirdPerson(bool frontSideView) { var position = Position; var direction = new Vector3(Rotation.X, Rotation.Y, frontSideView ? Rotation.Z : -Rotation.Z); direction.Normalize(); var offset = Rotation * (direction * 3.5f); offset.Y = 2.5f; //offset *= direction; //offset.Normalize(); Direction = direction; Vector3 lookAtOffset = new Vector3( position.X + offset.X, position.Y + offset.Y, position.Z + offset.Z); Target = position; ViewMatrix = MCMatrix.CreateLookAt(lookAtOffset, position, Vector3.Up); Frustum = new BoundingFrustum(ViewMatrix * ProjectionMatrix); }
public override void Update(IUpdateArgs args) { if (CanRender) { var knownPos = KnownPosition.ToVector3(); // var bb = base.BoundingBox; // var knownPos = bb.GetCenter(); if (DoRotation) { //var offset = new Vector3((float) Width, (float) Height, (float) Width) / 2f; var offset = new Vector3(0.5f, 0f, 0.5f); ItemRenderer.Update(args, MCMatrix.CreateScale(0.25f) * MCMatrix.CreateTranslation(-offset) * MCMatrix.CreateRotationY(MathHelper.ToRadians(_rotation)) * MCMatrix.CreateTranslation(offset) * MCMatrix.CreateTranslation(knownPos)); } else { ItemRenderer.Update(args, MCMatrix.CreateScale(0.25f) * MCMatrix.CreateRotationY(MathHelper.ToRadians(KnownPosition.Yaw)) * MCMatrix.CreateTranslation(knownPos)); } } if (DoRotation) { _rotation += 45f * (float)args.GameTime.ElapsedGameTime.TotalSeconds; } }
protected override void UpdateViewMatrix() { var normalPosition = new Vector3(Position.X, 256, Position.Z); var targetPosition = new Vector3(normalPosition.X, 0, normalPosition.Z); ViewMatrix = MCMatrix.CreateLookAt(normalPosition, targetPosition, Vector3.Forward); }
private void UpdateViewMatrixFirstPerson() { var position = Position; var direction = new Vector3(Rotation.X, Rotation.Y, Rotation.Z); direction.Normalize(); //var target = position Target = position + direction; Direction = direction; ViewMatrix = MCMatrix.CreateLookAt(position, Target, Vector3.Up); Frustum = new BoundingFrustum(ViewMatrix * ProjectionMatrix); }
/// <summary> /// Creates a new <see cref="T:Microsoft.Xna.Framework.Vector3" /> that contains a transformation of 3d-vector by the specified <see cref="T:Microsoft.Xna.Framework.Matrix" />. /// </summary> /// <param name="position">Source <see cref="T:Microsoft.Xna.Framework.Vector3" />.</param> /// <param name="matrix">The transformation <see cref="T:Microsoft.Xna.Framework.Matrix" />.</param> /// <param name="result">Transformed <see cref="T:Microsoft.Xna.Framework.Vector3" /> as an output parameter.</param> public static void Transform(ref Vector3 position, ref MCMatrix matrix, out Vector3 result) { result.X = position.X * matrix.M11 + position.Y * matrix.M21 + position.Z * matrix.M31 + matrix.M41; result.Y = position.X * matrix.M12 + position.Y * matrix.M22 + position.Z * matrix.M32 + matrix.M42; result.Z = position.X * matrix.M13 + position.Y * matrix.M23 + position.Z * matrix.M33 + matrix.M43; }
private ModelBone ProcessBone( EntityModel source, EntityModelBone bone, ref List <VertexPositionColorTexture> vertices, Vector2 textureSize, Dictionary <string, ModelBone> modelBones) { ModelBone modelBone; int startIndex = vertices.Count; int elementCount = 0; if (bone.Cubes != null) { foreach (var cube in bone.Cubes) { if (cube == null) { Log.Warn("Cube was null!"); continue; } var inflation = (float)(cube.Inflate ?? bone.Inflate); var mirror = cube.Mirror ?? bone.Mirror; var origin = cube.InflatedOrigin(inflation); MCMatrix matrix = MCMatrix.CreateTranslation(origin); if (cube.Rotation.HasValue) { var rotation = cube.Rotation.Value; Vector3 pivot = origin + (cube.InflatedSize(inflation) / 2f); if (cube.Pivot.HasValue) { pivot = cube.InflatedPivot(inflation); // cube.Pivot.Value; } matrix = MCMatrix.CreateTranslation(origin) * MCMatrix.CreateTranslation((-pivot)) * MCMatrix.CreateRotationDegrees(rotation) * MCMatrix.CreateTranslation(pivot); } Cube built = new Cube(cube, textureSize, mirror, inflation); ModifyCubeIndexes(ref vertices, built.Front, origin, matrix); ModifyCubeIndexes(ref vertices, built.Back, origin, matrix); ModifyCubeIndexes(ref vertices, built.Top, origin, matrix); ModifyCubeIndexes(ref vertices, built.Bottom, origin, matrix); ModifyCubeIndexes(ref vertices, built.Left, origin, matrix); ModifyCubeIndexes(ref vertices, built.Right, origin, matrix); } } elementCount = vertices.Count - startIndex; modelBone = new ModelBone(bone, startIndex, elementCount); if (bone.Rotation.HasValue) { var r = bone.Rotation.Value; modelBone.BindingRotation = new Vector3(r.X, r.Y, r.Z); } if (bone.BindPoseRotation.HasValue) { var r = bone.BindPoseRotation.Value; modelBone.BindingRotation += new Vector3(r.X, r.Y, r.Z); } foreach (var childBone in source.Bones.Where( x => x.Parent != null && string.Equals(x.Parent, bone.Name, StringComparison.OrdinalIgnoreCase))) { if (childBone.Parent != null && childBone.Parent.Equals(childBone.Name)) { continue; } if (string.IsNullOrWhiteSpace(childBone.Name)) { childBone.Name = Guid.NewGuid().ToString(); } var child = ProcessBone(source, childBone, ref vertices, textureSize, modelBones); //child.Parent = modelBone; modelBone.AddChild(child); if (!modelBones.TryAdd(childBone.Name, child)) { Log.Warn($"Failed to add bone! {childBone.Name}"); break; } } return(modelBone); }
public void Update(IUpdateArgs args, MCMatrix characterMatrix) { if (_disposed) { return; } //if (!Monitor.TryEnter(_disposeLock, 0)) // return; try { //var device = args.GraphicsDevice; if (CurrentAnim == null && Animations.TryDequeue(out var animation)) { animation.Setup(); animation.Start(); CurrentAnim = animation; } if (CurrentAnim != null) { CurrentAnim.Update(args.GameTime); if (CurrentAnim.IsFinished()) { CurrentAnim.Reset(); CurrentAnim = null; } } if (Definition.Pivot.HasValue) { var pivot = (Definition.Pivot ?? Vector3.Zero); WorldMatrix = MCMatrix.CreateTranslation(-pivot) * MCMatrix.CreateRotationDegrees(BindingRotation + Rotation) * MCMatrix.CreateTranslation(pivot) * characterMatrix; } else { WorldMatrix = MCMatrix.CreateRotationDegrees(BindingRotation + Rotation) * characterMatrix; } var children = Children.ToArray(); if (children.Length > 0) { foreach (var child in children) { child.Update(args, WorldMatrix); } } } finally { // Monitor.Exit(_disposeLock); } }
public override void UpdateProjectionMatrix() { ProjectionMatrix = MCMatrix.CreateOrthographic(256, 256, 0.05f, 256f); }