public Project ( System.Vector3 source, System.Matrix projection, System.Matrix view, System.Matrix world ) : System.Vector3 | ||
source | System.Vector3 | The |
projection | System.Matrix | The projection |
view | System.Matrix | The view |
world | System.Matrix | The world |
return | System.Vector3 |
public Butterfly(Vector3 Position, float rotation, Viewport ViewPort, Matrix View, Matrix Projection, ButterflyColors color) { world = Matrix.CreateFromAxisAngle(Vector3.Right, -1 * (float)(Math.PI / 2f)) * Matrix.CreateTranslation(Position); Vector3 ScreenProjection = ViewPort.Project(Position, Projection, View, Matrix.CreateScale(0.1f) * Matrix.CreateFromAxisAngle(Vector3.Right, -1 * (float)(Math.PI / 2f)) * Matrix.CreateTranslation(Position)); hitbox = new Hitbox(new Rectangle((int)(ScreenProjection.X - padX), (int)(ScreenProjection.Y - padY), 2 * padX, 2 * padY)); viewPort = ViewPort; projection = Projection; view = View; hitbox.Entered += new Hitbox.EnteredEventHandler(hitbox_Entered); hitbox.Exited += new Hitbox.ExitedEventHandler(hitbox_Exited); timeSelected = new Stopwatch(); myColor = color; hidden = false; position = Position; startPosition = Position * 20; msOffset = rand.Next(0, 500); wanderDirection = new Vector2(); tether = Vector2.Transform(new Vector2(Position.X, Position.Y), Matrix.CreateScale(0.1f)); }
/// <summary> /// Draws the background screen. /// </summary> public override void Draw(GameTime gameTime) { SpriteBatch spriteBatch = ScreenManager.SpriteBatch; Viewport viewport = ScreenManager.Viewport; Rectangle fullscreen = new Rectangle(0, 0, viewport.Width, viewport.Height); Vector3 horizV; Vector3 horiz; float horizHeight; if (selectionMode == 1) { for (int i = 0; i < 3; i++) { if (!cupTracks[i].HasLoaded) continue; ScreenManager.GraphicsDevice.SetRenderTarget(cupTrackRT[i]); ScreenManager.GraphicsDevice.Clear(cupTracks[i].SkyColor); int nextpos = (trackPos[i] + 1); if (nextpos >= cupTracks[i].TrackSegments.Count) nextpos = nextpos - cupTracks[i].TrackSegments.Count; Camera.projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (193f/108f), 0.001f, 100f); Camera.Position = cupTracks[i].TrackSegments[trackPos[i]].Position + new Vector3(0, 0.5f, 0); Camera.LookAt(cupTracks[i].TrackSegments[nextpos].Position + new Vector3(0, 0.5f, 0), 0f); drawEffect.View = Camera.viewMatrix; drawEffect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 16f/9f, 0.001f, 100f); drawAlphaEffect.View = Camera.viewMatrix; drawAlphaEffect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 16f / 9f, 0.001f, 100f); cupTrackPM[i].Update(gameTime, new Vector2(((1280 * 4) / MathHelper.TwoPi) * Camera.Yaw, 0f)); Viewport testVP = new Viewport(0, 0, 193, 108); horizV = new Vector3(0, 0f, -100); horiz = testVP.Project(horizV, Camera.projectionMatrix, Camera.ViewMatrixUpDownOnly(), Camera.worldMatrix); horizHeight = horiz.Y - (25f * cupTrackPM[i].Scale); ScreenManager.SpriteBatch.Begin(); ScreenManager.SpriteBatch.Draw(texList["blank"], new Rectangle(cupTrackRT[i].Width / 2, (int)(horizHeight), cupTrackRT[i].Width * 2, (cupTrackRT[i].Height - (int)(horizHeight)) + 400), null, cupTracks[i].GroundColor, 0f, new Vector2(0.5f, 0), SpriteEffects.None, 1); ScreenManager.SpriteBatch.End(); ScreenManager.SpriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, Matrix.CreateScale(new Vector3(cupTrackPM[i].Scale, cupTrackPM[i].Scale, 1f)) * Matrix.CreateTranslation(new Vector3(cupTrackRT[i].Width / 2, horizHeight, 0f))); cupTrackPM[i].Draw(ScreenManager.SpriteBatch); ScreenManager.SpriteBatch.End(); ScreenManager.GraphicsDevice.DepthStencilState = DepthStencilState.Default; ScreenManager.GraphicsDevice.RasterizerState = RasterizerState.CullNone; ScreenManager.GraphicsDevice.BlendState = BlendState.Opaque; ScreenManager.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp; cupTracks[i].DrawBatches(ScreenManager.GraphicsDevice, drawEffect, drawAlphaEffect); #if !WINDOWS_PHONE ScreenManager.GraphicsDevice.SetRenderTarget(null); #else ScreenManager.GraphicsDevice.SetRenderTarget(Psuedo3DRacer.renderTarget); #endif } } Camera.projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, ScreenManager.Viewport.AspectRatio, 0.001f, 100f); Camera.Position = new Vector3(3f, 0.2f, 16f); Camera.LookAt(new Vector3(0f, 0.5f, 25f) + lookAtOffset, 0f); drawEffect.View = Camera.viewMatrix; drawEffect.Projection = Camera.projectionMatrix; drawAlphaEffect.View = Camera.viewMatrix; drawAlphaEffect.Projection = Camera.projectionMatrix; ScreenManager.GraphicsDevice.Clear(Track.SkyColor); horizV = new Vector3(0, 0f, -200); horiz = ScreenManager.Viewport.Project(horizV, Camera.projectionMatrix, Camera.ViewMatrixUpDownOnly(), Camera.worldMatrix); horizHeight = horiz.Y - (25 * Parallax.Scale); ScreenManager.SpriteBatch.Begin(); ScreenManager.SpriteBatch.Draw(texList["blank"], new Rectangle(ScreenManager.Viewport.Width / 2, (int)horizHeight, ScreenManager.Viewport.Width * 2, (ScreenManager.Viewport.Height - (int)horizHeight) + 400), null, Track.GroundColor, 0f, new Vector2(0.5f, 0), SpriteEffects.None, 1); ScreenManager.SpriteBatch.End(); ScreenManager.SpriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, Matrix.CreateScale(new Vector3(Parallax.Scale, Parallax.Scale,1f)) * Matrix.CreateTranslation(new Vector3(ScreenManager.Viewport.Width / 2, horizHeight, 0f))); Parallax.Draw(ScreenManager.SpriteBatch); ScreenManager.SpriteBatch.End(); ScreenManager.GraphicsDevice.DepthStencilState = DepthStencilState.Default; ScreenManager.GraphicsDevice.RasterizerState = RasterizerState.CullNone; ScreenManager.GraphicsDevice.BlendState = BlendState.Opaque; ScreenManager.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp; if(Track.HasLoaded) Track.DrawBatches(ScreenManager.GraphicsDevice, drawEffect, drawAlphaEffect); ScreenManager.FadeBackBufferToBlack(0.5f); spriteBatch.Begin(); spriteBatch.Draw(texList["banner-top"], bannerTop1Pos, null, Color.White, 0f, new Vector2(texList["banner-top"].Width / 2, 0f), 1f, SpriteEffects.None, 1); spriteBatch.Draw(texList["banner-bottom"], bannerBottom1Pos, null, Color.White, 0f, new Vector2(texList["banner-bottom"].Width / 2, 0f), 1f, SpriteEffects.None, 1); spriteBatch.Draw(texList["banner-top"], bannerTop2Pos, null, Color.White, 0f, new Vector2(texList["banner-top"].Width / 2, 0f), 1f, SpriteEffects.None, 1); spriteBatch.Draw(texList["banner-bottom"], bannerBottom2Pos, null, Color.White, 0f, new Vector2(texList["banner-bottom"].Width / 2, 0f), 1f, SpriteEffects.None, 1); spriteBatch.Draw(texList["paintcar"], bannerBottom1Pos - new Vector2(0, ((bannerBottom1Pos.Y - (bannerTop1Pos.Y + texList["banner-top"].Height)) / 2) - 1f), null, Color.White * paintCarAlpha, 0f, new Vector2(texList["paintcar"].Width, texList["paintcar"].Height)/2, 1f, SpriteEffects.None, 1); spriteBatch.Draw(texList["selectcup"], bannerBottom2Pos - new Vector2(0, ((bannerBottom2Pos.Y - (bannerTop2Pos.Y + texList["banner-top"].Height)) / 2) - 1f), null, Color.White * selectCupAlpha, 0f, new Vector2(texList["selectcup"].Width, texList["selectcup"].Height) / 2, 1f, SpriteEffects.None, 1); spriteBatch.Draw(texList["car-spot"], SpotPos, null, Color.White, 0f, new Vector2(texList["car-spot"].Width , texList["car-spot"].Height)/2, 1f, SpriteEffects.None, 1); spriteBatch.Draw(texCarDirections[carFrame], CarPos, null, carColor, 0f, new Vector2(texCarDirections[carFrame].Width, texCarDirections[carFrame].Height) / 2, carScale, SpriteEffects.None, 1); spriteBatch.Draw(texList["colors"], PaintPos, null, Color.White, 0f, new Vector2(texList["colors"].Width, texList["colors"].Height) / 2, 1f, SpriteEffects.None, 1); spriteBatch.Draw(texList["triangles"], new Vector2(redRect.Left+4, redRect.Top + 24) + new Vector2((400f/230f) * (float)(carColor.R-25),0f), null, selectedColor==0?Color.White:Color.Black, 0f, new Vector2(texList["triangles"].Width, texList["triangles"].Height) / 2, 1f, SpriteEffects.None, 1); spriteBatch.Draw(texList["triangles"], new Vector2(greenRect.Left + 4, greenRect.Top + 24) + new Vector2((400f / 230f) * (float)(carColor.G - 25), 0f), null, selectedColor == 1 ? Color.White : Color.Black, 0f, new Vector2(texList["triangles"].Width, texList["triangles"].Height) / 2, 1f, SpriteEffects.None, 1); spriteBatch.Draw(texList["triangles"], new Vector2(blueRect.Left + 4, blueRect.Top + 24) + new Vector2((400f / 230f) * (float)(carColor.B - 25), 0f), null, selectedColor == 2 ? Color.White : Color.Black, 0f, new Vector2(texList["triangles"].Width, texList["triangles"].Height) / 2, 1f, SpriteEffects.None, 1); Color trophyColor = Color.Black * 0.5f; switch (trophies[selectedCup]) { case 3: trophyColor = Color.SaddleBrown; break; case 2: trophyColor = Color.Silver; break; case 1: trophyColor = Color.Gold; break; } spriteBatch.Draw(texList["cup"], trophyPos, null, trophyColor, 0f, new Vector2(texList["cup"].Width, texList["cup"].Height) / 2, 1f, SpriteEffects.None, 1); spriteBatch.Draw(texList["arrow"], new Vector2(leftRect.Center.X, leftRect.Center.Y), null, Color.LightGray, 0f, new Vector2(texList["arrow"].Width, texList["arrow"].Height) / 2, 1f, SpriteEffects.FlipHorizontally, 1); spriteBatch.Draw(texList["arrow"], new Vector2(rightRect.Center.X, rightRect.Center.Y), null, Color.LightGray, 0f, new Vector2(texList["arrow"].Width, texList["arrow"].Height) / 2, 1f, SpriteEffects.None, 1); spriteBatch.Draw(texList["cuparrow"], new Vector2(leftCupRect.Center.X, leftCupRect.Center.Y), null, Color.LightGray * (selectedCup>0?1f:0.2f), 0f, new Vector2(texList["cuparrow"].Width, texList["cuparrow"].Height) / 2, 1f, SpriteEffects.None, 1); spriteBatch.Draw(texList["cuparrow"], new Vector2(rightCupRect.Center.X, rightCupRect.Center.Y), null, Color.LightGray * (selectedCup < (Cups.Count-1) ? 1f : 0.2f), 0f, new Vector2(texList["cuparrow"].Width, texList["cuparrow"].Height) / 2, 1f, SpriteEffects.FlipHorizontally, 1); spriteBatch.End(); spriteBatch.Begin(SpriteSortMode.FrontToBack, null); foreach(Cup c in Cups) { spriteBatch.Draw(texList["cup-holder"], c.Position, null, Color.LightGray * c.Scale, 0f, new Vector2(texList["cup-holder"].Width, texList["cup-holder"].Height) / 2, c.Scale, SpriteEffects.None, c.Scale); spriteBatch.Draw(texList["cuptitle" + (Cups.IndexOf(c) + 1).ToString()], c.Position + new Vector2(0, -175 * c.Scale), null, Color.LightGray * c.Scale, 0f, new Vector2(texList["cuptitle" + (Cups.IndexOf(c) + 1).ToString()].Width, texList["cuptitle" + (Cups.IndexOf(c) + 1).ToString()].Height) / 2, c.Scale, SpriteEffects.None, c.Scale); if (selectionMode == 1 && Cups.IndexOf(c) == selectedCup) { if(cupTracks[0].HasLoaded) spriteBatch.Draw(cupTrackRT[0], c.Position + new Vector2(0, -31 * c.Scale), null, Color.White * c.Scale, 0f, new Vector2(cupTrackRT[0].Width, cupTrackRT[0].Height) / 2, c.Scale, SpriteEffects.None, c.Scale); else spriteBatch.Draw(texList["blank-track"], c.Position + new Vector2(0, -31 * c.Scale), null, Color.White * c.Scale, 0f, new Vector2(193, 108) / 2, c.Scale, SpriteEffects.None, c.Scale); if (cupTracks[1].HasLoaded) spriteBatch.Draw(cupTrackRT[1], c.Position + new Vector2(0, 79 * c.Scale), null, Color.White * c.Scale, 0f, new Vector2(cupTrackRT[1].Width, cupTrackRT[1].Height) / 2, c.Scale, SpriteEffects.None, c.Scale); else spriteBatch.Draw(texList["blank-track"], c.Position + new Vector2(0, 79 * c.Scale), null, Color.White * c.Scale, 0f, new Vector2(193, 108) / 2, c.Scale, SpriteEffects.None, c.Scale); if (cupTracks[2].HasLoaded) spriteBatch.Draw(cupTrackRT[2], c.Position + new Vector2(0, 189 * c.Scale), null, Color.White * c.Scale, 0f, new Vector2(cupTrackRT[2].Width, cupTrackRT[2].Height) / 2, c.Scale, SpriteEffects.None, c.Scale); else spriteBatch.Draw(texList["blank-track"], c.Position + new Vector2(0, 189 * c.Scale), null, Color.White * c.Scale, 0f, new Vector2(193, 108) / 2, c.Scale, SpriteEffects.None, c.Scale); } else { spriteBatch.Draw(texList["blank-track"], c.Position + new Vector2(0, -31 * c.Scale), null, Color.White * c.Scale, 0f, new Vector2(193, 108) / 2, c.Scale, SpriteEffects.None, c.Scale); spriteBatch.Draw(texList["blank-track"], c.Position + new Vector2(0, 79 * c.Scale), null, Color.White * c.Scale, 0f, new Vector2(193, 108) / 2, c.Scale, SpriteEffects.None, c.Scale); spriteBatch.Draw(texList["blank-track"], c.Position + new Vector2(0, 189 * c.Scale), null, Color.White * c.Scale, 0f, new Vector2(193, 108) / 2, c.Scale, SpriteEffects.None, c.Scale); } } spriteBatch.End(); ScreenManager.FadeBackBufferToBlack(1f - TransitionAlpha); }
private void drawSpawnBar(Vector3 spawnPoint, float zoom, Viewport viewport) { Vector3 projection = viewport.Project(spawnPoint, RenderingDevice.Camera.Projection, RenderingDevice.Camera.View, RenderingDevice.Camera.World); float baseZoom = 100; float scale = baseZoom / zoom; // baseZoom / zoom gets us a scale that increases when zoom is low; that is; zoomed in. float offset = 15; Vector2 screenCoords = new Vector2(projection.X, projection.Y); screenCoords.X += offset * RenderingDevice.TextureScaleFactor.X; screenCoords.Y -= offset * RenderingDevice.TextureScaleFactor.Y; outerSpawnBar.X = innerSpawnBar.X = (int)(screenCoords.X + texOffset * RenderingDevice.TextureScaleFactor.X * scale); outerSpawnBar.Y = innerSpawnBar.Y = (int)(screenCoords.Y + texOffset * RenderingDevice.TextureScaleFactor.Y * scale); RenderingDevice.SpriteBatch.Draw(Resources.EmptyTex, outerSpawnBar, null, outerBarColor, 0, Vector2.Zero, SpriteEffects.None, 0); if(remaining > 0) RenderingDevice.SpriteBatch.Draw(Resources.EmptyTex, innerSpawnBar, null, innerBarColor, 0, Vector2.Zero, SpriteEffects.None, 0); RenderingDevice.SpriteBatch.Draw(barTex, screenCoords, null, Color.White, 0, Vector2.Zero, RenderingDevice.TextureScaleFactor * scale, SpriteEffects.None, 0); }
public static void Run() { var left = -1; var right = 1; var bottom = -1; var top = 1; var near = 5.0f; var far = near + 30.0f; var xdiv = (right - left) / (far - near); var ydiv = (top - bottom) / (far - near); var world = Matrix.Identity; var view = Matrix.Identity; var proj = Matrix.CreatePerspectiveOffCenter(left, right, bottom, top, near, far); var width = 2; var height = 2; var port = new Viewport(left, bottom, width, height); // From MSDN et. al. // // projection space refers to the space after applying projection transformation from // view space. After the projection transformation, visible content has x and y coordinates // ranging from -1 to 1, and z coordinates ranging from 0 to 1. // // Positive Z comes out of the screen, so negative near and far is into the screen. // // The viewport then mirrors Y so it goes down in screen space. Doing(v => port.Project(v, proj, view, world)).With(-1, -1, -near).ShouldBe(-1, 1, 0) .With( 1, -1, -near).ShouldBe(1, 1, 0) .With(-1, 1, -near).ShouldBe(-1, -1, 0) .With(1, 1, -near).ShouldBe(1, -1, 0) .With( 0, 0, -near).ShouldBe(0, 0, 0) .With(0, 0, -far).ShouldBe(0, 0, 1); // Test project is inverse of unproject Doing(v => port.Unproject(v, proj, view, world)).With(-1, -1, 1).ShouldBe(-1 / near * far, 1 / near * far, -far); Doing(v => port.Project(v, proj, view, world)).With(-1 / near * far, 1 / near * far, -far).ShouldBe(-1, -1, 1); Doing(v => port.Project(v, proj, view, world)).With(-1 / near * far, -1 / near * far, -far).ShouldBe(-1, 1, 1) .With(1 / near * far, 1 / near * far, -far).ShouldBe(1, -1, 1) .With(1 / near * far, -1 / near * far, -far).ShouldBe(1, 1, 1); // Similar verification but now with values that are typical of usage. top = 10; left = -5; bottom = 0; right = 5; near = 1.0f; far = near + 100.0f; world = Matrix.Identity; view = Matrix.Identity; proj = Matrix.CreatePerspectiveOffCenter(left, right, bottom, top, near, far); width = 640; height = 480; port = new Viewport(0, 0, width, height); Doing(v => port.Project(v, proj, view, world)) // Corners of the screen at near plane. .With(left, top, -near).ShouldBe(0, 0, 0) .With(right, top, -near).ShouldBe(width, 0, 0) .With(left, bottom, -near).ShouldBe(0, height, 0) .With(right, bottom, -near).ShouldBe(width, height, 0) // Center of the screen. .With(0, 0, -near).ShouldBe(width / 2, height, 0) .With(0, 0, -far).ShouldBe(width / 2, height, 1); Doing(v => port.Unproject(v, proj, view, world)).With(0, 0, 0).ShouldBe(left, top, -near) .With(width, 0, 0).ShouldBe(right, top, -near) .With(0, height, 0).ShouldBe(left, bottom, -near) .With(width, height, 0).ShouldBe(right, bottom, -near); }
public static void ProjectRayToScreenSpace(Ray ray, Viewport viewport, Matrix viewTransform, Matrix projectionTransform, out Vector2 rayPosition, out Vector2 rayDirection) { Vector3 screenPosition1 = viewport.Project(ray.Position, projectionTransform, viewTransform, Matrix.Identity); Vector3 screenPosition2 = viewport.Project(ray.Position + ray.Direction, projectionTransform, viewTransform, Matrix.Identity); Vector3 direction = screenPosition2 - screenPosition1; rayPosition = new Vector2(screenPosition1.X, screenPosition1.Y); rayDirection = new Vector2(direction.X, direction.Y); }
public void ProjectTest() { Viewport viewport = new Viewport(0, 0, 640, 480); PerspectiveProjection projection = new PerspectiveProjection(); projection.SetFieldOfView(MathHelper.ToRadians(60), viewport.AspectRatio, 10, 1000); Matrix44F view = Matrix44F.CreateLookAt(new Vector3F(0, 0, 0), new Vector3F(0, 0, -1), Vector3F.Up); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(320, 240, 0), viewport.Project(new Vector3F(0, 0, -10), projection, view))); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(0, 0, 0), viewport.Project(new Vector3F(projection.Left, projection.Top, -10), projection, view))); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(640, 0, 0), viewport.Project(new Vector3F(projection.Right, projection.Top, -10), projection, view))); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(0, 480, 0), viewport.Project(new Vector3F(projection.Left, projection.Bottom, -10), projection, view))); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(640, 480, 0), viewport.Project(new Vector3F(projection.Right, projection.Bottom, -10), projection, view))); Vector3[] farCorners = new Vector3[4]; GraphicsHelper.GetFrustumFarCorners(projection, farCorners); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(320, 240, 1), viewport.Project(new Vector3F(0, 0, -1000), projection, view))); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(0, 0, 1), viewport.Project((Vector3F)farCorners[0], projection, view), 1e-4f)); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(640, 0, 1), viewport.Project((Vector3F)farCorners[1], projection, view), 1e-4f)); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(0, 480, 1), viewport.Project((Vector3F)farCorners[2], projection, view), 1e-4f)); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(640, 480, 1), viewport.Project((Vector3F)farCorners[3], projection, view), 1e-4f)); }
public override void Render(SpriteBatch batch, Camera camera, Viewport viewport) { if(camera == null) { return; } Vector3 screenCoord = viewport.Project(WorldPosition, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity); if(viewport.Bounds.Contains((int) screenCoord.X, (int) screenCoord.Y) && screenCoord.Z > 0 && Image != null) { batch.Draw(Image.Image, new Vector2(screenCoord.X, screenCoord.Y) + Offset, Image.SourceRect, Tint, Rotation, new Vector2(Image.SourceRect.Width / 2.0f, Image.SourceRect.Height / 2.0f), Scale, Effects, 0.0f); } }
public override void Render(SpriteBatch batch, Camera camera, Viewport viewport) { Vector2 extents = Font.MeasureString(Text); Vector3 unprojected = viewport.Project(Position, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity); unprojected.X = Math.Min(Math.Max(extents.X * 0.5f, unprojected.X), viewport.Width - extents.X * 0.5f) ; unprojected.Y = Math.Min(Math.Max(extents.Y * 0.5f, unprojected.Y), viewport.Height - extents.Y * 0.5f); batch.DrawString(Font, Text, new Vector2(unprojected.X + 1, unprojected.Y) - extents / 2.0f, StrokeColor); batch.DrawString(Font, Text, new Vector2(unprojected.X - 1, unprojected.Y) - extents / 2.0f, StrokeColor); batch.DrawString(Font, Text, new Vector2(unprojected.X, unprojected.Y + 1) - extents / 2.0f, StrokeColor); batch.DrawString(Font, Text, new Vector2(unprojected.X, unprojected.Y - 1) - extents / 2.0f, StrokeColor); batch.DrawString(Font, Text, new Vector2(unprojected.X, unprojected.Y) - extents / 2.0f, TextColor); base.Render(batch, camera, viewport); }
public override void Render(SpriteBatch batch, Camera camera, Viewport viewport) { Vector2 extents = Font.MeasureString(Text); Vector3 unprojected = viewport.Project(Position, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity); Rectangle rect = new Rectangle((int)(unprojected.X - extents.X / 2.0f - StrokeWeight), (int)(unprojected.Y - extents.Y / 2.0f - StrokeWeight), (int)(extents.X + StrokeWeight + 5), (int)(extents.Y + StrokeWeight + 5)); Drawer2D.FillRect(batch, rect, FillColor); Drawer2D.DrawRect(batch, rect, RectStrokeColor, StrokeWeight); batch.DrawString(Font, Text, new Vector2(unprojected.X + 1, unprojected.Y) - extents / 2.0f, StrokeColor); batch.DrawString(Font, Text, new Vector2(unprojected.X - 1, unprojected.Y) - extents / 2.0f, StrokeColor); batch.DrawString(Font, Text, new Vector2(unprojected.X, unprojected.Y + 1) - extents / 2.0f, StrokeColor); batch.DrawString(Font, Text, new Vector2(unprojected.X, unprojected.Y - 1) - extents / 2.0f, StrokeColor); batch.DrawString(Font, Text, new Vector2(unprojected.X, unprojected.Y) - extents / 2.0f, TextColor); base.Render(batch, camera, viewport); }
public void RenderSunMoon(DwarfTime time, GraphicsDevice device, Camera camera, Viewport viewPort, float scale) { Matrix rot = Matrix.CreateRotationZ(-(CosTime + 0.5f * (float) Math.PI)); SunPosition = new Vector3(1000, 100, 0); Vector3 moonPosition = new Vector3(-1000, 100, 0); SunPosition = Vector3.Transform(SunPosition, rot); moonPosition = Vector3.Transform(moonPosition, rot); SunPosition += camera.Position; moonPosition += camera.Position; Vector3 cameraFrameSun = Vector3.Transform(SunPosition, camera.ViewMatrix * camera.ProjectionMatrix); Vector3 cameraFramMoon = Vector3.Transform(moonPosition, camera.ViewMatrix * camera.ProjectionMatrix); Vector3 unProjectSun = viewPort.Project(SunPosition, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity); Vector3 unProjectMoon = viewPort.Project(moonPosition, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity); DwarfGame.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.PointClamp, DepthStencilState.None, RasterizerState.CullNone, null, Matrix.Identity); if(unProjectSun.Z > 0 && cameraFrameSun.Z > 0) { DwarfGame.SpriteBatch.Draw(SunTexture, new Vector2(unProjectSun.X - SunTexture.Width / 2 * scale, unProjectSun.Y - SunTexture.Height / 2 * scale), null, Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0.0f); } if(unProjectMoon.Z > 0 && cameraFramMoon.Z > 0) { DwarfGame.SpriteBatch.Draw(MoonTexture, new Vector2(unProjectMoon.X - SunTexture.Width / 2 * scale, unProjectMoon.Y - SunTexture.Height / 2 * scale), null, Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0.0f); } DwarfGame.SpriteBatch.End(); Vector3 sunDir = (camera.Position - SunPosition); sunDir.Normalize(); SunlightDir = sunDir; }
public Vector3 GetScreenCoordinates(Vector3 point) { // todo: retrieve viewport Viewport v = new Viewport(0, 0, 1024, 768); return v.Project(point, ProjectionMatrix, viewMatrix, Matrix.Identity); }
public override void Render(SpriteBatch batch, Camera camera, Viewport viewport) { if(batch == null || camera == null) { return; } Vector2 extents = Datastructures.SafeMeasure(Font, Text); Vector3 unprojected = viewport.Project(Position, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity); if (unprojected.Z < 0) { return; } Rectangle rect = new Rectangle((int) (unprojected.X - extents.X / 2.0f - StrokeWeight), (int) (unprojected.Y - extents.Y / 2.0f - StrokeWeight), (int) (extents.X + StrokeWeight + 5), (int) (extents.Y + StrokeWeight + 5)); Drawer2D.FillRect(batch, rect, FillColor); Drawer2D.DrawRect(batch, rect, RectStrokeColor, StrokeWeight); Drawer2D.SafeDraw(batch, Text, Font, StrokeColor, new Vector2(unprojected.X + 1, unprojected.Y) - extents / 2.0f, Vector2.Zero); Drawer2D.SafeDraw(batch, Text, Font, StrokeColor, new Vector2(unprojected.X - 1, unprojected.Y) - extents / 2.0f, Vector2.Zero); Drawer2D.SafeDraw(batch, Text, Font, StrokeColor, new Vector2(unprojected.X, unprojected.Y + 1) - extents / 2.0f, Vector2.Zero); Drawer2D.SafeDraw(batch, Text, Font, StrokeColor, new Vector2(unprojected.X, unprojected.Y - 1) - extents / 2.0f, Vector2.Zero); Drawer2D.SafeDraw(batch, Text, Font, TextColor, new Vector2(unprojected.X, unprojected.Y) - extents / 2.0f, Vector2.Zero); }
public override void PreRender(DwarfTime time, SpriteBatch sprites) { if(!HomeSet) { HomePosition = PlayState.Camera.Target; HomeSet = true; } if(IsVisible && !IsMinimized && GUI.RootComponent.IsVisible) { Viewport originalViewport = PlayState.GraphicsDevice.Viewport; Camera.Update(time, PlayState.ChunkManager); Camera.Target = PlayState.Camera.Target + Vector3.Up * 50; Camera.Phi = -(float) Math.PI * 0.5f; Camera.Theta = PlayState.Camera.Theta; Camera.UpdateProjectionMatrix(); PlayState.GraphicsDevice.SetRenderTarget(RenderTarget); PlayState.GraphicsDevice.Clear(ClearOptions.Target, Color.Black, 0, 0); PlayState.DefaultShader.Parameters["xView"].SetValue(Camera.ViewMatrix); PlayState.DefaultShader.Parameters["xEnableFog"].SetValue(0); PlayState.DefaultShader.Parameters["xProjection"].SetValue(Camera.ProjectionMatrix); PlayState.DefaultShader.CurrentTechnique = PlayState.DefaultShader.Techniques["Textured_colorscale"]; PlayState.GraphicsDevice.BlendState = BlendState.AlphaBlend; PlayState.ChunkManager.RenderAll(Camera, time, PlayState.GraphicsDevice, PlayState.DefaultShader, Matrix.Identity, ColorMap); PlayState.WaterRenderer.DrawWaterFlat(PlayState.GraphicsDevice, Camera.ViewMatrix, Camera.ProjectionMatrix, PlayState.DefaultShader, PlayState.ChunkManager); PlayState.GraphicsDevice.Textures[0] = null; PlayState.GraphicsDevice.Indices = null; PlayState.GraphicsDevice.SetVertexBuffer(null); PlayState.DefaultShader.Parameters["xEnableFog"].SetValue(1); DwarfGame.SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, null, RasterizerState.CullNone); Viewport viewPort = new Viewport(RenderTarget.Bounds); foreach(MinimapIcon icon in PlayState.ComponentManager.RootComponent.GetChildrenOfTypeRecursive<MinimapIcon>()) { if (!icon.Parent.IsVisible) { continue; } Vector3 screenPos = viewPort.Project(icon.GlobalTransform.Translation, Camera.ProjectionMatrix, Camera.ViewMatrix, Matrix.Identity); if(RenderTarget.Bounds.Contains((int) screenPos.X, (int) screenPos.Y)) { sprites.Draw(icon.Icon.Image, new Vector2(screenPos.X, screenPos.Y), icon.Icon.SourceRect, Color.White, 0.0f, new Vector2(icon.Icon.SourceRect.Width / 2.0f, icon.Icon.SourceRect.Height / 2.0f), icon.IconScale, SpriteEffects.None, 0); } } sprites.End(); PlayState.GraphicsDevice.BlendState = BlendState.AlphaBlend; PlayState.GraphicsDevice.DepthStencilState = DepthStencilState.None; PlayState.GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; PlayState.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp; PlayState.GraphicsDevice.SetRenderTarget(null); } base.PreRender(time, sprites); }
//Updates the life bar texture public void updateLifeBarsMonsters(int i,int percentage, Vector3 position, Camera cam, Viewport viewport) { Vector3 posi = viewport.Project(position, cam.projection, cam.view, Matrix.Identity); Vector2 pos = new Vector2(posi.X, posi.Y) + new Vector2(-40 ,-40); //pos = new Vector2(0, 0); if (percentage == 100) monstersLife[i].Update(ref life100, pos); if (percentage == 75) monstersLife[i].Update(ref life75, pos); if (percentage == 50) monstersLife[i].Update(ref life50, pos); if (percentage == 25) monstersLife[i].Update(ref life25, pos); }