public static void ProcessNormalizedSpriteMessage(MyRenderMessageDrawSpriteNormalized sprite) { var rotation = sprite.Rotation; if (sprite.RotationSpeed != 0) { rotation += sprite.RotationSpeed * (float)(MyRender.CurrentDrawTime - MyRender.CurrentUpdateTime).Seconds; } Vector2 rightVector = rotation != 0f ? new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)) : sprite.RightVector; MyTexture2D tex = MyTextureManager.GetTexture <MyTexture2D>(sprite.Texture); if (sprite.OriginNormalized.HasValue) { DrawSpriteBatch(tex, sprite.NormalizedCoord, sprite.Scale, sprite.Color, sprite.DrawAlign, rightVector, sprite.OriginNormalized.Value); } else { DrawSpriteBatch(tex, sprite.NormalizedCoord, sprite.NormalizedSize, sprite.Color, sprite.DrawAlign, rightVector); } }
private static void ProcessDrawMessage(MyRenderMessageBase drawMessage) { switch (drawMessage.MessageType) { case MyRenderMessageEnum.SpriteScissorPush: { var msg = drawMessage as MyRenderMessageSpriteScissorPush; MySpritesRenderer.ScissorStackPush(msg.ScreenRectangle); break; } case MyRenderMessageEnum.SpriteScissorPop: { MySpritesRenderer.ScissorStackPop(); break; } case MyRenderMessageEnum.DrawSprite: { MyRenderMessageDrawSprite sprite = (MyRenderMessageDrawSprite)drawMessage; MyFileTextureManager texManager = MyManagers.FileTextures; MySpritesRenderer.AddSingleSprite(texManager.GetTexture(sprite.Texture, MyFileTextureEnum.GUI, waitTillLoaded: sprite.WaitTillLoaded), sprite.Color, sprite.Origin, sprite.RightVector, sprite.SourceRectangle, sprite.DestinationRectangle); break; } case MyRenderMessageEnum.DrawSpriteNormalized: { MyRenderMessageDrawSpriteNormalized sprite = (MyRenderMessageDrawSpriteNormalized)drawMessage; var rotation = sprite.Rotation; if (sprite.RotationSpeed != 0) { rotation += sprite.RotationSpeed * (float)(MyRender11.CurrentDrawTime - MyRender11.CurrentUpdateTime).Seconds; } Vector2 rightVector = rotation != 0f ? new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)) : sprite.RightVector; int safeGuiSizeY = MyRender11.ResolutionI.Y; int safeGuiSizeX = (int)(safeGuiSizeY * 1.3333f); // This will mantain same aspect ratio for GUI elements var safeGuiRectangle = new VRageMath.Rectangle(MyRender11.ResolutionI.X / 2 - safeGuiSizeX / 2, 0, safeGuiSizeX, safeGuiSizeY); var safeScreenScale = (float)safeGuiSizeY / MyRenderGuiConstants.REFERENCE_SCREEN_HEIGHT; float fixedScale = sprite.Scale * safeScreenScale; var tex = MyManagers.FileTextures.GetTexture(sprite.Texture, MyFileTextureEnum.GUI, true); var normalizedCoord = sprite.NormalizedCoord; var screenCoord = new Vector2(safeGuiRectangle.Left + safeGuiRectangle.Width * normalizedCoord.X, safeGuiRectangle.Top + safeGuiRectangle.Height * normalizedCoord.Y); Vector2 sizeInPixels = tex.Size; var sizeInPixelsScaled = sizeInPixels * fixedScale; Vector2 alignedScreenCoord = screenCoord; var drawAlign = sprite.DrawAlign; if (drawAlign == MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP) { // Nothing to do as position is already at this point } else if (drawAlign == MyGuiDrawAlignEnum.HORISONTAL_CENTER_AND_VERTICAL_CENTER) { // Move position to the texture center alignedScreenCoord -= sizeInPixelsScaled / 2.0f; } else if (drawAlign == MyGuiDrawAlignEnum.HORISONTAL_CENTER_AND_VERTICAL_TOP) { alignedScreenCoord.X -= sizeInPixelsScaled.X / 2.0f; } else if (drawAlign == MyGuiDrawAlignEnum.HORISONTAL_CENTER_AND_VERTICAL_BOTTOM) { alignedScreenCoord.X -= sizeInPixelsScaled.X / 2.0f; alignedScreenCoord.Y -= sizeInPixelsScaled.Y; } else if (drawAlign == MyGuiDrawAlignEnum.HORISONTAL_RIGHT_AND_VERTICAL_BOTTOM) { alignedScreenCoord -= sizeInPixelsScaled; } else if (drawAlign == MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_CENTER) { alignedScreenCoord.Y -= sizeInPixelsScaled.Y / 2.0f; } else if (drawAlign == MyGuiDrawAlignEnum.HORISONTAL_RIGHT_AND_VERTICAL_CENTER) { alignedScreenCoord.X -= sizeInPixelsScaled.X; alignedScreenCoord.Y -= sizeInPixelsScaled.Y / 2.0f; } else if (drawAlign == MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_BOTTOM) { alignedScreenCoord.Y -= sizeInPixelsScaled.Y; // *0.75f; } else if (drawAlign == MyGuiDrawAlignEnum.HORISONTAL_RIGHT_AND_VERTICAL_TOP) { alignedScreenCoord.X -= sizeInPixelsScaled.X; } screenCoord = alignedScreenCoord; var rect = new RectangleF(screenCoord.X, screenCoord.Y, fixedScale * sizeInPixels.X, fixedScale * sizeInPixels.Y); Vector2 origin; if (sprite.OriginNormalized.HasValue) { origin = sprite.OriginNormalized.Value * sizeInPixels; } else { origin = sizeInPixels / 2; } sprite.OriginNormalized = sprite.OriginNormalized ?? new Vector2(0.5f); MyFileTextureManager texManager = MyManagers.FileTextures; MySpritesRenderer.AddSingleSprite(texManager.GetTexture(sprite.Texture, MyFileTextureEnum.GUI, waitTillLoaded: sprite.WaitTillLoaded), sprite.Color, origin, rightVector, null, rect); break; } case MyRenderMessageEnum.DrawSpriteAtlas: { MyRenderMessageDrawSpriteAtlas sprite = (MyRenderMessageDrawSpriteAtlas)drawMessage; MyFileTextureManager texManager = MyManagers.FileTextures; var tex = texManager.GetTexture(sprite.Texture, MyFileTextureEnum.GUI, true); var textureSize = tex.Size; Rectangle?sourceRect = new Rectangle( (int)(textureSize.X * sprite.TextureOffset.X), (int)(textureSize.Y * sprite.TextureOffset.Y), (int)(textureSize.X * sprite.TextureSize.X), (int)(textureSize.Y * sprite.TextureSize.Y)); VRageMath.RectangleF destRect = new VRageMath.RectangleF( (sprite.Position.X) * sprite.Scale.X, (sprite.Position.Y) * sprite.Scale.Y, sprite.HalfSize.X * sprite.Scale.X * 2, sprite.HalfSize.Y * sprite.Scale.Y * 2); Vector2 origin = new Vector2(textureSize.X * sprite.TextureSize.X * 0.5f, textureSize.Y * sprite.TextureSize.Y * 0.5f); MySpritesRenderer.AddSingleSprite(texManager.GetTexture(sprite.Texture, MyFileTextureEnum.GUI, true), sprite.Color, origin, sprite.RightVector, sourceRect, destRect); break; } case MyRenderMessageEnum.DrawString: { var message = drawMessage as MyRenderMessageDrawString; var font = MyRender11.GetFont(message.FontIndex); font.DrawString( message.ScreenCoord, message.ColorMask, message.Text, message.ScreenScale, message.ScreenMaxWidth); break; } default: throw new Exception(); } }
public static void ProcessNormalizedSpriteMessage(MyRenderMessageDrawSpriteNormalized sprite) { var rotation = sprite.Rotation; if (sprite.RotationSpeed != 0) { rotation += sprite.RotationSpeed * (float)(MyRender.CurrentDrawTime - MyRender.CurrentUpdateTime).Seconds; } Vector2 rightVector = rotation != 0f ? new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)) : sprite.RightVector; MyTexture2D tex = MyTextureManager.GetTexture<MyTexture2D>(sprite.Texture); if (sprite.OriginNormalized.HasValue) { DrawSpriteBatch(tex, sprite.NormalizedCoord, sprite.Scale, sprite.Color, sprite.DrawAlign, rightVector, sprite.OriginNormalized.Value); } else { DrawSpriteBatch(tex, sprite.NormalizedCoord, sprite.NormalizedSize, sprite.Color, sprite.DrawAlign, rightVector); } }
internal static void DrawMessageQueue() { if (m_spriteBatch == null) { return; } DrawSpriteStencilEnum currentStencil = DrawSpriteStencilEnum.None; BeginSpriteBatch(); /* * if (m_drawMessages.Count == 0) * { * GraphicsDevice.Clear(ClearFlags.All, new SharpDX.ColorBGRA(1.0f, 1, 0, 1), 1, 0); * DrawText(new Vector2(0, 300), new StringBuilder("No draw input"), Color.White, 1); * } */ while (m_drawMessages.Count > 0) { IMyRenderMessage drawMessage = m_drawMessages.Dequeue(); MyRenderMessageEnum messageType = drawMessage.MessageType; switch (messageType) { case MyRenderMessageEnum.SpriteScissorPush: { var msg = drawMessage as MyRenderMessageSpriteScissorPush; m_spriteBatch.ScissorStack.Push(msg.ScreenRectangle); break; } case MyRenderMessageEnum.SpriteScissorPop: { m_spriteBatch.ScissorStack.Pop(); break; } case MyRenderMessageEnum.DrawSprite: { MyRenderMessageDrawSprite sprite = (MyRenderMessageDrawSprite)drawMessage; ProcessSpriteMessage(sprite); break; } case MyRenderMessageEnum.DrawSpriteNormalized: { MyRenderMessageDrawSpriteNormalized sprite = (MyRenderMessageDrawSpriteNormalized)drawMessage; ProcessNormalizedSpriteMessage(sprite); break; } case MyRenderMessageEnum.DrawSpriteAtlas: { MyRenderMessageDrawSpriteAtlas sprite = (MyRenderMessageDrawSpriteAtlas)drawMessage; ProcessAtlasSprite(sprite); break; } case MyRenderMessageEnum.DrawString: { var message = drawMessage as MyRenderMessageDrawString; var font = MyRender.GetFont(message.FontIndex); font.DrawString( message.ScreenCoord * m_sizeMultiplierForStrings, message.ColorMask, message.Text, message.ScreenScale * m_sizeMultiplierForStrings.X, message.ScreenMaxWidth * m_sizeMultiplierForStrings.X); break; } case MyRenderMessageEnum.DrawScene: { EndSpriteBatch(); MyRenderCamera.UpdateCamera(); MyRender.SetDeviceViewport(MyRenderCamera.Viewport); Draw3D(); DrawDebugMessages(); BeginSpriteBatch(); break; } case MyRenderMessageEnum.DrawVideo: { var rMessage = (MyRenderMessageDrawVideo)drawMessage; MyRender.DrawVideo(rMessage.ID, rMessage.Rectangle, rMessage.Color, rMessage.FitMode); break; } case MyRenderMessageEnum.UpdateEnvironmentMap: { MyRender.GetRenderProfiler().StartProfilingBlock("MyEnvironmentMap.Update"); MyEnvironmentMap.Update(); MyRender.GetRenderProfiler().EndProfilingBlock(); break; } case MyRenderMessageEnum.DrawSecondaryCamera: { var rMessage = (MyRenderMessageDrawSecondaryCamera)drawMessage; MySecondaryCameraRenderer.Instance.ViewMatrix = (MatrixD)rMessage.ViewMatrix; MySecondaryCameraRenderer.Instance.Render(); break; } case MyRenderMessageEnum.DrawSecondaryCameraSprite: { MyRenderMessageDrawSecondaryCameraSprite sprite = (MyRenderMessageDrawSecondaryCameraSprite)drawMessage; Vector2 rightVector = sprite.Rotation != 0f ? new Vector2((float)Math.Cos(sprite.Rotation), (float)Math.Sin(sprite.Rotation)) : sprite.RightVector; //rightVector = new Vector2(1, 1); DrawSpriteMain( MySecondaryCameraRenderer.Instance.GetRenderedTexture(), null, ref sprite.DestinationRectangle, sprite.ScaleDestination, sprite.SourceRectangle, sprite.Color, rightVector, ref sprite.Origin, sprite.Effects, sprite.Depth ); break; } case MyRenderMessageEnum.UpdateBillboardsColorize: { var rMessage = (MyRenderMessageUpdateBillboardsColorize)drawMessage; MyTransparentGeometry.EnableColorize = rMessage.Enable; MyTransparentGeometry.ColorizeColor = rMessage.Color; MyTransparentGeometry.ColorizePlaneDistance = rMessage.Distance; MyTransparentGeometry.ColorizePlaneNormal = rMessage.Normal; break; } default: { System.Diagnostics.Debug.Assert(false, "Unknown draw message"); break; } } } GetRenderProfiler().StartProfilingBlock("SpriteBatchRestart"); EndSpriteBatch(); BeginSpriteBatch(); GetRenderProfiler().EndProfilingBlock(); GetRenderProfiler().StartProfilingBlock("DrawDebugMessages"); DrawDebugMessages(); GetRenderProfiler().EndProfilingBlock(); if (MyRenderProxy.DRAW_RENDER_STATS) { MyRenderStatsDraw.Draw(MyRenderStats.m_stats, 0.6f, Color.Yellow); } if (MyRender.Settings.TearingTest) { DrawTearingTest(); } if (MyRender.Settings.MultimonTest) { // Middle screen var from = new Vector2(GraphicsDevice.Viewport.Width / 2.0f, 0); var to = new Vector2(GraphicsDevice.Viewport.Width / 2.0f, GraphicsDevice.Viewport.Height); VRageRender.MyRenderProxy.DebugDrawLine2D(from, to, Color.Orange, Color.Orange); from = new Vector2(GraphicsDevice.Viewport.Width / 3.0f, 0); to = new Vector2(GraphicsDevice.Viewport.Width / 3.0f, GraphicsDevice.Viewport.Height); VRageRender.MyRenderProxy.DebugDrawLine2D(from, to, Color.Yellow, Color.Yellow); from = new Vector2(GraphicsDevice.Viewport.Width / 3.0f * 2.0f, 0); to = new Vector2(GraphicsDevice.Viewport.Width / 3.0f * 2.0f, GraphicsDevice.Viewport.Height); VRageRender.MyRenderProxy.DebugDrawLine2D(from, to, Color.Yellow, Color.Yellow); } GetRenderProfiler().StartProfilingBlock("SpriteBatchEnd"); EndSpriteBatch(); GetRenderProfiler().EndProfilingBlock(); System.Diagnostics.Debug.Assert(m_spriteBatchUsageCount == 0); }
private static void ProcessDrawMessage(MyRenderMessageBase drawMessage) { switch (drawMessage.MessageType) { case MyRenderMessageEnum.SpriteScissorPush: { var msg = drawMessage as MyRenderMessageSpriteScissorPush; MySpritesRenderer.ScissorStackPush(msg.ScreenRectangle); break; } case MyRenderMessageEnum.SpriteScissorPop: { MySpritesRenderer.ScissorStackPop(); break; } case MyRenderMessageEnum.DrawSprite: { MyRenderMessageDrawSprite sprite = (MyRenderMessageDrawSprite)drawMessage; MySpritesRenderer.AddSingleSprite(MyTextures.GetTexture(sprite.Texture, MyTextureEnum.GUI, waitTillLoaded: sprite.WaitTillLoaded), sprite.Color, sprite.Origin, sprite.RightVector, sprite.SourceRectangle, sprite.DestinationRectangle); break; } case MyRenderMessageEnum.DrawSpriteNormalized: { MyRenderMessageDrawSpriteNormalized sprite = (MyRenderMessageDrawSpriteNormalized)drawMessage; var rotation = sprite.Rotation; if (sprite.RotationSpeed != 0) { rotation += sprite.RotationSpeed * (float)(MyRender11.CurrentDrawTime - MyRender11.CurrentUpdateTime).Seconds; } Vector2 rightVector = rotation != 0f ? new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)) : sprite.RightVector; int safeGuiSizeY = MyRender11.ResolutionI.Y; int safeGuiSizeX = (int)(safeGuiSizeY * 1.3333f); // This will mantain same aspect ratio for GUI elements var safeGuiRectangle = new VRageMath.Rectangle(MyRender11.ResolutionI.X / 2 - safeGuiSizeX / 2, 0, safeGuiSizeX, safeGuiSizeY); var safeScreenScale = (float)safeGuiSizeY / MyRenderGuiConstants.REFERENCE_SCREEN_HEIGHT; float fixedScale = sprite.Scale * safeScreenScale; var tex = MyTextures.GetTexture(sprite.Texture, MyTextureEnum.GUI, true); var normalizedCoord = sprite.NormalizedCoord; var screenCoord = new Vector2(safeGuiRectangle.Left + safeGuiRectangle.Width * normalizedCoord.X, safeGuiRectangle.Top + safeGuiRectangle.Height * normalizedCoord.Y); var sizeInPixels = MyTextures.GetSize(tex); var sizeInPixelsScaled = sizeInPixels * fixedScale; Vector2 alignedScreenCoord = screenCoord; var drawAlign = sprite.DrawAlign; if (drawAlign == MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP) { // Nothing to do as position is already at this point } else if (drawAlign == MyGuiDrawAlignEnum.HORISONTAL_CENTER_AND_VERTICAL_CENTER) { // Move position to the texture center alignedScreenCoord -= sizeInPixelsScaled / 2.0f; } else if (drawAlign == MyGuiDrawAlignEnum.HORISONTAL_CENTER_AND_VERTICAL_TOP) { alignedScreenCoord.X -= sizeInPixelsScaled.X / 2.0f; } else if (drawAlign == MyGuiDrawAlignEnum.HORISONTAL_CENTER_AND_VERTICAL_BOTTOM) { alignedScreenCoord.X -= sizeInPixelsScaled.X / 2.0f; alignedScreenCoord.Y -= sizeInPixelsScaled.Y; } else if (drawAlign == MyGuiDrawAlignEnum.HORISONTAL_RIGHT_AND_VERTICAL_BOTTOM) { alignedScreenCoord -= sizeInPixelsScaled; } else if (drawAlign == MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_CENTER) { alignedScreenCoord.Y -= sizeInPixelsScaled.Y / 2.0f; } else if (drawAlign == MyGuiDrawAlignEnum.HORISONTAL_RIGHT_AND_VERTICAL_CENTER) { alignedScreenCoord.X -= sizeInPixelsScaled.X; alignedScreenCoord.Y -= sizeInPixelsScaled.Y / 2.0f; } else if (drawAlign == MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_BOTTOM) { alignedScreenCoord.Y -= sizeInPixelsScaled.Y; // *0.75f; } else if (drawAlign == MyGuiDrawAlignEnum.HORISONTAL_RIGHT_AND_VERTICAL_TOP) { alignedScreenCoord.X -= sizeInPixelsScaled.X; } screenCoord = alignedScreenCoord; var rect = new RectangleF(screenCoord.X, screenCoord.Y, fixedScale * sizeInPixels.X, fixedScale * sizeInPixels.Y); Vector2 origin; if (sprite.OriginNormalized.HasValue) { origin = sprite.OriginNormalized.Value * sizeInPixels; } else { origin = sizeInPixels / 2; } sprite.OriginNormalized = sprite.OriginNormalized ?? new Vector2(0.5f); MySpritesRenderer.AddSingleSprite(MyTextures.GetTexture(sprite.Texture, MyTextureEnum.GUI, waitTillLoaded: sprite.WaitTillLoaded), sprite.Color, origin, rightVector, null, rect); break; } case MyRenderMessageEnum.DrawSpriteAtlas: { MyRenderMessageDrawSpriteAtlas sprite = (MyRenderMessageDrawSpriteAtlas)drawMessage; var tex = MyTextures.GetTexture(sprite.Texture, MyTextureEnum.GUI, true); var textureSize = MyTextures.GetSize(tex); Rectangle?sourceRect = new Rectangle( (int)(textureSize.X * sprite.TextureOffset.X), (int)(textureSize.Y * sprite.TextureOffset.Y), (int)(textureSize.X * sprite.TextureSize.X), (int)(textureSize.Y * sprite.TextureSize.Y)); VRageMath.RectangleF destRect = new VRageMath.RectangleF( (sprite.Position.X) * sprite.Scale.X, (sprite.Position.Y) * sprite.Scale.Y, sprite.HalfSize.X * sprite.Scale.X * 2, sprite.HalfSize.Y * sprite.Scale.Y * 2); Vector2 origin = new Vector2(textureSize.X * sprite.TextureSize.X * 0.5f, textureSize.Y * sprite.TextureSize.Y * 0.5f); MySpritesRenderer.AddSingleSprite(MyTextures.GetTexture(sprite.Texture, MyTextureEnum.GUI, true), sprite.Color, origin, sprite.RightVector, sourceRect, destRect); break; } case MyRenderMessageEnum.DrawString: { var message = drawMessage as MyRenderMessageDrawString; var font = MyRender11.GetFont(message.FontIndex); font.DrawString( message.ScreenCoord, message.ColorMask, message.Text, message.ScreenScale, message.ScreenMaxWidth); break; } case MyRenderMessageEnum.DrawScene: { UpdateSceneFrame(); ProfilerShort.Begin("DrawScene"); DrawGameScene(Backbuffer); ProfilerShort.Begin("TransferPerformanceStats"); TransferPerformanceStats(); ProfilerShort.End(); ProfilerShort.End(); ProfilerShort.Begin("Draw scene debug"); MyGpuProfiler.IC_BeginBlock("Draw scene debug"); DrawSceneDebug(); MyGpuProfiler.IC_EndBlock(); ProfilerShort.End(); ProfilerShort.Begin("ProcessDebugMessages"); ProcessDebugMessages(); ProfilerShort.End(); ProfilerShort.Begin("MyDebugRenderer.Draw"); MyGpuProfiler.IC_BeginBlock("MyDebugRenderer.Draw"); MyDebugRenderer.Draw(MyRender11.Backbuffer); MyGpuProfiler.IC_EndBlock(); ProfilerShort.End(); var testingDepth = MyRender11.MultisamplingEnabled ? MyScreenDependants.m_resolvedDepth : MyGBuffer.Main.DepthStencil; ProfilerShort.Begin("MyPrimitivesRenderer.Draw"); MyGpuProfiler.IC_BeginBlock("MyPrimitivesRenderer.Draw"); MyPrimitivesRenderer.Draw(MyRender11.Backbuffer, testingDepth); MyGpuProfiler.IC_EndBlock(); ProfilerShort.End(); ProfilerShort.Begin("MyLinesRenderer.Draw"); MyGpuProfiler.IC_BeginBlock("MyLinesRenderer.Draw"); MyLinesRenderer.Draw(MyRender11.Backbuffer, testingDepth); MyGpuProfiler.IC_EndBlock(); ProfilerShort.End(); if (m_screenshot.HasValue && m_screenshot.Value.IgnoreSprites) { if (m_screenshot.Value.SizeMult == Vector2.One) { SaveScreenshotFromResource(Backbuffer.m_resource); } else { TakeCustomSizedScreenshot(m_screenshot.Value.SizeMult); } } ProfilerShort.Begin("MySpritesRenderer.Draw"); MyGpuProfiler.IC_BeginBlock("MySpritesRenderer.Draw"); MySpritesRenderer.Draw(MyRender11.Backbuffer.m_RTV, new MyViewport(MyRender11.ViewportResolution.X, MyRender11.ViewportResolution.Y)); MyGpuProfiler.IC_EndBlock(); ProfilerShort.End(); if (MyRenderProxy.DRAW_RENDER_STATS) { MyRender11.GetRenderProfiler().StartProfilingBlock("MyRenderStatsDraw.Draw"); MyRenderStatsDraw.Draw(MyRenderStats.m_stats, 0.6f, VRageMath.Color.Yellow); ProfilerShort.End(); } break; } } }