- Modifié
XNA Matrix zoom
Hi guys,
I've managed to implement the XNA runtime into my game surprisinly effortlessly, but with only one bug that I'm hoping someone can help me with.
My code is as follows:
spriteBatch.Begin(SpriteSortMode.Immediate, null, null, null, null, null, Map.Camera.GetTransformation());
skeleton.X = (Location.X - Map.Camera.GetScroll().X);
skeleton.Y = (Location.Y - Map.Camera.GetScroll().Y);
skeleton.UpdateWorldTransform();
skeletonRenderer.Begin();
skeletonRenderer.Draw(skeleton);
skeletonRenderer.End();
spriteBatch.End();
It draws the character in the correct location, but my camera has the ability to zoom in and out, and regardless of the cameras value, the spine character is always drawn at the same scale, so it appears that skeletonRenderer is not taking into consideration my Camera transformation.
Any thoughts on this?
Cheers!
James
The problem is that the skeleton gets passed to a basic shader that has its own matrices. Go to SkeletonRender.cs and go to the Begin() method. You need to inform the shader of your transform matrix. Here is one way of solving the problem.
public void Begin (Matrix transform //add your matrix as a parameter) {
defaultBlendState = premultipliedAlpha ? BlendState.AlphaBlend : BlendState.NonPremultiplied;
device.RasterizerState = rasterizerState;
device.BlendState = defaultBlendState;
effect.Projection = Matrix.CreateOrthographicOffCenter(0, device.Viewport.Width, device.Viewport.Height, 0, 1, 0);
effect.World = transform; //set the transform matrix.
}
Your draw code would look like this.
spriteBatch.Begin(SpriteSortMode.Immediate, null, null, null, null, null, Map.Camera.GetTransformation());
skeleton.X = (Location.X - Map.Camera.GetScroll().X);
skeleton.Y = (Location.Y - Map.Camera.GetScroll().Y);
spriteBatch.End();
skeleton.UpdateWorldTransform();
skeletonRenderer.Begin(Map.Camera.GetTransformation());
skeletonRenderer.Draw(skeleton);
skeletonRenderer.End();
I only tested it briefly and it seemed to work ok, hope it works for you too
Awesome! I haven't had to use shader effects much, mostly pixel shaders.
thanks for your help!