public void Render() { Vector3 position = new Vector3(PositionX, PositionY, PositionZ); Vector3 lookAt = new Vector3(0, 0, 1.0f); float pitch = RotationX.ToRadians(); float yaw = RotationY.ToRadians(); float roll = RotationZ.ToRadians(); Matrix rotationMatrix = Matrix.RotationYawPitchRoll(yaw, pitch, roll); lookAt = Vector3.TransformCoordinate(lookAt, rotationMatrix); lookAt = position + lookAt; Vector3 up = Vector3.TransformCoordinate(Vector3.UnitY, rotationMatrix); ViewMatrix = Matrix.LookAtLH(position, lookAt, up); }
/// <summary> /// <para>Draws the shape at the desired coordinates. Coordinates are aligned to world coordinates.</para> /// <para>If you place this draw in a Tile draw such as SpecialDraw, post-draw, or another Tile draw, it will not draw at 60 fps.</para> /// <para>To draw your 3D model at 60 FPS, place it in the main Mod Draw function of your mod and set (ModifyInterfaceLayerFix = true).</para> /// </summary> /// <param name="spriteBatch">Spritebatch that will be used to draw this shape.</param> /// <param name="pos">The world coordinate to draw this shape.</param> public void Draw(Vector2 pos) { SpriteBatch spriteBatch = Main.spriteBatch; //if (_framesSkipped >= FrameskipCount) //{ // _framesSkipped = 0; // return; //} //else //{ // _framesSkipped++; //} if (!Visible) { return; } _effect = new BasicEffect(spriteBatch.GraphicsDevice); // The effect world matrix will handle all visual tranformations to our model // This is easier than transforming all the vertices of our model one-by-one _effect.World = // Handles Rotation Matrix.CreateFromYawPitchRoll( (float)RotationX.ToRadians(), (float)RotationY.ToRadians(), (float)RotationZ.ToRadians()); if (ModifyInterfaceLayerFix) { // Handles Game Zoom set by the Zoom slider _effect.World *= Matrix.CreateScale( Main.GameViewMatrix.Zoom.X, Main.GameViewMatrix.Zoom.X, Main.GameViewMatrix.Zoom.Y); } var posWorldtoScreenCoords = Main.screenPosition - pos; // This is not respecting coordinates when put in Tile Draw // When this is ran through ModifyLayerInterface, it works appropriately // I have no clue what is going on. This is a major impedement. if (false) { float aspectRatio = Main.graphics.GraphicsDevice.Viewport.Width / (float)Main.graphics.GraphicsDevice.Viewport.Height; float fieldOfView = MathHelper.PiOver4; float nearClipPlane = 1; float farClipPlane = 10000; _effect.View = Matrix.CreateLookAt( new Vector3(0, 100, 0), // Camera Position Vector3.Zero, // Look-at Vector Vector3.UnitZ); // Up Axis is Z var x = (((float)Main.screenWidth / 2f)) + posWorldtoScreenCoords.X; var y = (((float)Main.screenHeight / 2f)) + posWorldtoScreenCoords.Y; var yOffset = -862f; if (ModifyInterfaceLayerFix) { x *= Main.GameZoomTarget; y *= Main.GameZoomTarget; } else { x += Main.GameZoomTarget; y += Main.GameZoomTarget; } _effect.World = _effect.World * Matrix.CreateTranslation(x, yOffset, y); _effect.Projection = Matrix.CreatePerspectiveFieldOfView( fieldOfView, aspectRatio, nearClipPlane, farClipPlane); } else if (Projection == ProjectionType.Orthographic) { // We control the objects rotation by controlling the world // This way, we don't need to adjust every individual vertice _effect.View = Matrix.CreateLookAt( new Vector3(0, 10000, 0), // Camera Position Vector3.Zero, // Look-at Vector Vector3.UnitZ); // Up Axis is Z var x = ((float)Main.screenWidth / 2f + posWorldtoScreenCoords.X); var y = ((float)Main.screenHeight / 2f + posWorldtoScreenCoords.Y); if (ModifyInterfaceLayerFix) { x *= Main.GameZoomTarget; y *= Main.GameZoomTarget; } // Moves the object to the desired position // Multiplying the Matrices "adds" them together // // We translate the Y 3000 pixels towards us // so we can see 3D models up to 3000 pixels large // We don't see the Y axis in Orthographic mode // so it doesn't matter how large we make it! _effect.World = _effect.World * Matrix.CreateTranslation(x, 3000, y); // Produces the "flat" style _effect.Projection = Matrix.CreateOrthographic( Main.graphics.GraphicsDevice.Viewport.Width, Main.graphics.GraphicsDevice.Viewport.Height, -10000, 10000); } spriteBatch.End(); spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone, _effect); { Main.graphics.GraphicsDevice.RasterizerState = rasterizerState; Main.graphics.GraphicsDevice.DepthStencilState = depthStencilState; Main.graphics.GraphicsDevice.BlendState = blendState; foreach (var face in Faces) { _effect.TextureEnabled = true; _effect.Texture = face.Texture ?? DefaultTexture; // Lighting var x = (int)(pos.X) / 16; var y = (int)(pos.Y) / 16; Vector3 color = Lighting.GetColor(x, y).ToVector3(); _effect.LightingEnabled = true; _effect.DirectionalLight0.DiffuseColor = color; _effect.DirectionalLight1.DiffuseColor = color; _effect.DirectionalLight2.DiffuseColor = color; _effect.AmbientLightColor = color; _effect.DiffuseColor = color; _effect.EmissiveColor = color; foreach (var pass in _effect.CurrentTechnique.Passes) { pass.Apply(); spriteBatch.GraphicsDevice.DrawUserPrimitives( PrimitiveType.TriangleList, face.Composition, 0, face.PolygonCount); } } spriteBatch.End(); } Main.graphics.GraphicsDevice.RasterizerState = previousRS; Main.graphics.GraphicsDevice.DepthStencilState = previousDS; Main.graphics.GraphicsDevice.BlendState = previousBS; spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend); }