public override void Init(MyObjectBuilder_EntityBase objectBuilder) { MyObjectBuilder_GhostCharacter characterOb = (MyObjectBuilder_GhostCharacter)objectBuilder; base.Init(objectBuilder); SetupPhysics(true); m_worldMatrixOriginal = this.WorldMatrix; NeedsUpdate |= MyEntityUpdateEnum.EACH_FRAME | MyEntityUpdateEnum.BEFORE_NEXT_FRAME; MyOpenVR.LMUAdd(null, WorldMatrix, ControllerRole.head, 1); }
public override void Draw() { base.Draw(); MyRenderProxy.DebugDrawText2D(new Vector2(50, 50), "LMU (ctrl *):" + MyOpenVR.LmuDebugOnOff, Color.White, 1.0f); MyRenderProxy.DebugDrawText2D(new Vector2(250, 50), "2D (ctrl /):" + MyOpenVR.Debug2DImage, Color.White, 1.0f); MyRenderProxy.DebugDrawText2D(new Vector2(450, 50), "SYNC (ctrl ,):" + MyOpenVR.SyncWait, Color.White, 1.0f); MyRenderProxy.DebugDrawText2D(new Vector2(100, 80), "Missed frames: " + MyOpenVR.MissedFramesCount, Color.Purple, 1.0f); MyRenderProxy.DebugDrawText2D(new Vector2(400, 80), "Wait ms: " + MyOpenVR.WaitTimeMs.ToString("00.00"), Color.MediumPurple, 1.0f); MyRenderProxy.DebugDrawText2D(new Vector2(100, 100), "IPD (hmd wheel) =" + (MyOpenVR.Ipd_2 * 2).ToString(), Color.Green, 1.0f); //MyRenderProxy.DebugDrawText2D(new Vector2(100, 120), "height (ctrl +-) =" + MyOpenVR.FloorOffset.ToString(), Color.Green, 1.0f); Quaternion rot = Quaternion.CreateFromRotationMatrix(MyOpenVR.Controller1Matrix); Vector3 angles = MyMath.QuaternionToEuler(rot); var anglesDeg = Vector3.Multiply(angles, (float)(180f / Math.PI)); MyRenderProxy.DebugDrawText2D(new Vector2(100, 150), "C1 angles:" + anglesDeg.ToString(), Color.Gray, 1.0f); sb.Clear(); for (int i = 0; i < 64; i++) { if (MyOpenVR.Controller1State.WasButtonPressed((Valve.VR.EVRButtonId)i)) { sb.Append('+' + i.ToString() + " "); } if (MyOpenVR.Controller1State.WasButtonReleased((Valve.VR.EVRButtonId)i)) { sb.Append('-' + i.ToString() + " "); } if (MyOpenVR.Controller1State.IsButtonPressed((Valve.VR.EVRButtonId)i)) { sb.Append(MyOpenVR.GetButtonName(i)); sb.Append(" "); } } MyRenderProxy.DebugDrawText2D(new Vector2(100, 170), sb.ToString(), Color.Yellow, 1.0f); //touchpads: if (MyOpenVR.Controller1State.GetTouchpadXY(ref m_c1touch)) { MyRenderProxy.DebugDrawText2D(new Vector2(100, 190), "C1 touchpad:" + m_c1touch.X.ToString("0.00") + ", " + m_c1touch.Y.ToString("0.00"), Color.RosyBrown, 1.0f); } if (MyOpenVR.Controller2State.GetTouchpadXY(ref m_c2touch)) { MyRenderProxy.DebugDrawText2D(new Vector2(100, 210), "C2 touchpad:" + m_c2touch.X.ToString("0.00") + ", " + m_c2touch.Y.ToString("0.00"), Color.RosyBrown, 1.0f); } MyRenderProxy.DebugDrawText2D(new Vector2(100, 230), "C1 analog trigger: " + MyOpenVR.Controller1State.GetAnalogTrigger().ToString("0.00"), Color.Yellow, 1.0f); DrawFrameTiming(); }
public override void UpdateAfterSimulation() { base.UpdateAfterSimulation(); //headset position update WorldMatrix = MyOpenVR.HeadsetMatrixD * m_worldMatrixOriginal; Physics.RigidBody.SetWorldMatrix(WorldMatrix); //VRageRender.MyRenderProxy.DebugDrawSphere(WorldMatrix.Translation, 1, Color.Red, 1, false); //Weapons' positions update: if (m_leftWeapon != null) { var gunBase = m_leftWeapon.GunBase as MyGunBase; if (gunBase != null) { ((MyEntity)m_leftWeapon).WorldMatrix = gunBase.m_holdingDummyMatrix * MyOpenVR.Controller1Matrix * m_worldMatrixOriginal; } else { ((MyEntity)m_leftWeapon).WorldMatrix = MyOpenVR.Controller1Matrix * m_worldMatrixOriginal; } HandleButtons(ref m_leftWeapon, ref m_leftWeaponInfo, false, m_leftWeapons); } if (m_rightWeapon != null) { var gunBase = m_rightWeapon.GunBase as MyGunBase; if (gunBase != null) { ((MyEntity)m_rightWeapon).WorldMatrix = gunBase.m_holdingDummyMatrix * MyOpenVR.Controller2Matrix * m_worldMatrixOriginal; } else { ((MyEntity)m_rightWeapon).WorldMatrix = MyOpenVR.Controller2Matrix * m_worldMatrixOriginal; } HandleButtons(ref m_rightWeapon, ref m_rightWeaponInfo, true, m_rightWeapons); } if (MyDamage > 0) { MyDamage -= 80; if (MyDamage <= 0) { MyOpenVR.UnFade(0.5f); } } }
private void DrawFrameTiming() { int vertPos = 250; if (!m_freezeTiming) { var ret = MyOpenVR.GetFrameTiming(ref m_timing, 0); } foreach (var field in m_timing.GetType().GetFields()) { MyRenderProxy.DebugDrawText2D(new Vector2(50, vertPos += 10), field.Name + ": " + field.GetValue(m_timing), Color.NavajoWhite, 0.5f); if (m_logTiming) { VRage.Utils.MyLog.Default.WriteLine(field.Name + ": " + field.GetValue(m_timing)); } } MyRenderProxy.DebugDrawText2D(new Vector2(50, vertPos += 10), "freeze (ctrl -): " + m_freezeTiming + " to console (ctrl +): " + m_logTiming, Color.White, 0.5f); }
internal static void InitUsingOpenVR() { m_VBdata = MyOpenVR.GetStencilMask(); InitInternal(m_VBdata); }
internal static unsafe void InitSubsystems() { MyManagers.OnDeviceInit(); //MyRwTextures.Init(); MyHwBuffers.Init(); ResetShadows(MyShadowCascades.Settings.NewData.CascadesCount, RenderSettings.ShadowQuality.ShadowCascadeResolution()); MyRender11.Init(); MyCommon.Init(); MyVertexLayouts.Init(); MyShaders.Init(); MyMeshes.Init(); MyMeshTableSrv.Init(); MyLightRendering.Init(); MyLinesRenderer.Init(); MySpritesRenderer.Init(); MyPrimitivesRenderer.Init(); MyOutline.Init(); MyBlur.Init(); MyTransparentRendering.Init(); MyFoliageComponents.Init(); MyBillboardRenderer.Init(); // hardcoded limits MyDebugRenderer.Init(); MyScreenDecals.Init(); MyEnvProbeProcessing.Init(); MyAtmosphereRenderer.Init(); MyCloudRenderer.Init(); MyAAEdgeMarking.Init(); MyScreenPass.Init(); MyCopyToRT.Init(); MyBlendTargets.Init(); MyFXAA.Init(); MyDepthResolve.Init(); MyBloom.Init(); MyLuminanceAverage.Init(); MyToneMapping.Init(); MySSAO.Init(); MyHdrDebugTools.Init(); MySceneMaterials.Init(); MyMaterials1.Init(); MyVoxelMaterials1.Init(); MyMeshMaterials1.Init(); MyHBAO.Init(); try { if (m_settings.UseStereoRendering) { var openVR = new MyOpenVR(); MyStereoStencilMask.InitUsingOpenVR(); } } catch (System.Exception e) { if (!VRage.MyCompilationSymbols.DX11ForceStereo) { throw; } MyStereoStencilMask.InitUsingUndefinedMask(); } }
private static void DrawScene() { AddDebugQueueMessage("Frame render start"); MyGpuProfiler.IC_BeginBlock("UpdateSceneFrame"); UpdateSceneFrame(); MyGpuProfiler.IC_EndBlock(); var testingDepth = MyRender11.MultisamplingEnabled ? MyScreenDependants.m_resolvedDepth : MyGBuffer.Main.DepthStencil; MyGpuProfiler.IC_BeginBlock("Clear"); MyGBuffer.Main.Clear(VRageMath.Color.Black); MyGpuProfiler.IC_EndBlock(); if (MyOpenVR.Static != null) { ProfilerShort.Begin("OpenVR.WaitForNextStart"); MyOpenVR.WaitForNextStart(); ProfilerShort.End(); } ProfilerShort.Begin("DrawGameScene"); DrawGameScene(Backbuffer); ProfilerShort.End(); if (MyOpenVR.Static != null) { ProfilerShort.Begin("OpenVR.DisplayEye"); MyGpuProfiler.IC_BeginBlock("OpenVR.DisplayEye"); MyGBuffer.Main.Clear(VRageMath.Color.Black);//image is in HMD now, lets draw the rest for overlay MyOpenVR.Static.DisplayEye(MyRender11.Backbuffer.Resource.NativePointer); MyGpuProfiler.IC_EndBlock(); ProfilerShort.End(); } ProfilerShort.Begin("Draw scene debug"); MyGpuProfiler.IC_BeginBlock("Draw scene debug"); DrawSceneDebug(); ProfilerShort.End(); ProfilerShort.Begin("ProcessDebugMessages"); ProcessDebugMessages(); MyGpuProfiler.IC_EndBlock(); ProfilerShort.End(); ProfilerShort.Begin("MyDebugRenderer.Draw"); MyGpuProfiler.IC_BeginBlock("MyDebugRenderer.Draw"); MyDebugRenderer.Draw(MyRender11.Backbuffer); MyGpuProfiler.IC_EndBlock(); ProfilerShort.End(); 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) { ProfilerShort.Begin("Screenshot"); if (m_screenshot.Value.SizeMult == Vector2.One) { SaveScreenshotFromResource(Backbuffer.Resource); } else { TakeCustomSizedScreenshot(m_screenshot.Value.SizeMult); } ProfilerShort.End(); } ProfilerShort.Begin("ProcessDebugOutput"); AddDebugQueueMessage("Frame render end"); ProcessDebugOutput(); ProfilerShort.End(); }
private static void SetupCameraMatricesInternal(MyRenderMessageSetCameraViewMatrix message, MyEnvironmentMatrices envMatrices, MyStereoRegion typeofEnv) {//uses m_leftEye to handle HMD images var viewMatrix = message.ViewMatrix; var cameraPosition = message.CameraPosition; if (MyStereoRender.Enable) { if (MyOpenVR.Static != null && message.LastMomentUpdateIndex != 0) { MatrixD origin = MatrixD.Identity; MyOpenVR.LMUMatrixGetOrigin(ref origin, message.LastMomentUpdateIndex); viewMatrix = MatrixD.Invert(origin); } } var viewMatrixAt0 = viewMatrix; viewMatrixAt0.M14 = 0; viewMatrixAt0.M24 = 0; viewMatrixAt0.M34 = 0; viewMatrixAt0.M41 = 0; viewMatrixAt0.M42 = 0; viewMatrixAt0.M43 = 0; viewMatrixAt0.M44 = 1; if (MyStereoRender.Enable) { if (MyOpenVR.Static != null) { if (message.LastMomentUpdateIndex != 0) { var tViewMatrix = Matrix.Transpose(viewMatrix); var viewHMDat0 = MyOpenVR.ViewHMD; viewHMDat0.M14 = 0; viewHMDat0.M24 = 0; viewHMDat0.M34 = 0; viewHMDat0.M41 = 0; viewHMDat0.M42 = 0; viewHMDat0.M43 = 0; viewHMDat0.M44 = 1; //cameraPosition += tViewMatrix.Up * MyOpenVR.ViewHMD.Translation.Y; //cameraPosition += tViewMatrix.Backward * MyOpenVR.ViewHMD.Translation.X; //cameraPosition += tViewMatrix.Right * MyOpenVR.ViewHMD.Translation.Z; viewMatrixAt0 = viewMatrixAt0 * viewHMDat0; viewMatrix = viewMatrix * viewHMDat0; if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.LEFT) { viewMatrixAt0 = GetMatrixEyeTranslation(true, viewMatrixAt0) * viewMatrixAt0; viewMatrix = GetMatrixEyeTranslation(true, viewMatrix) * viewMatrix; } else if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.RIGHT) { viewMatrixAt0 = GetMatrixEyeTranslation(false, viewMatrixAt0) * viewMatrixAt0; viewMatrix = GetMatrixEyeTranslation(false, viewMatrix) * viewMatrix; } } } else { if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.LEFT) { viewMatrixAt0 = GetMatrixEyeTranslation(true, viewMatrixAt0) * viewMatrixAt0; viewMatrix = GetMatrixEyeTranslation(true, viewMatrix) * viewMatrix; } else if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.RIGHT) { viewMatrixAt0 = GetMatrixEyeTranslation(false, viewMatrixAt0) * viewMatrixAt0; viewMatrix = GetMatrixEyeTranslation(false, viewMatrix) * viewMatrix; } } } var originalProjection = message.ProjectionMatrix; //var invOriginalProjection = Matrix.CreatePerspectiveFovRhInverse(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.NearPlane, message.FarPlane); float aspectRatio = MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y; if (typeofEnv != MyStereoRegion.FULLSCREEN) { aspectRatio /= 2; } var renderProjection = Matrix.CreatePerspectiveFieldOfView(message.FOV, aspectRatio, message.FarPlane, message.NearPlane); var invProj = Matrix.CreatePerspectiveFovRhInverse(message.FOV, aspectRatio, message.FarPlane, message.NearPlane); renderProjection = Matrix.CreatePerspectiveFovRhInfiniteComplementary(message.FOV, aspectRatio, message.NearPlane); invProj = Matrix.CreatePerspectiveFovRhInfiniteComplementaryInverse(message.FOV, aspectRatio, message.NearPlane); var invView = Matrix.Transpose(viewMatrixAt0); invView.M41 = (float)cameraPosition.X; invView.M42 = (float)cameraPosition.Y; invView.M43 = (float)cameraPosition.Z; envMatrices.ViewAt0 = viewMatrixAt0; envMatrices.InvViewAt0 = Matrix.Transpose(viewMatrixAt0); envMatrices.ViewProjectionAt0 = viewMatrixAt0 * renderProjection; envMatrices.InvViewProjectionAt0 = invProj * Matrix.Transpose(viewMatrixAt0); cameraPosition.AssertIsValid(); envMatrices.CameraPosition = cameraPosition; envMatrices.View = viewMatrix; envMatrices.ViewD = viewMatrix; envMatrices.OriginalProjectionD = originalProjection; envMatrices.InvView = invView; envMatrices.ViewProjection = viewMatrix * renderProjection; envMatrices.InvViewProjection = invProj * invView; envMatrices.Projection = renderProjection; envMatrices.InvProjection = invProj; envMatrices.ViewProjectionD = envMatrices.ViewD * (MatrixD)renderProjection; envMatrices.NearClipping = message.NearPlane; envMatrices.FarClipping = message.FarPlane; envMatrices.LargeDistanceFarClipping = message.FarPlane * 500.0f; envMatrices.FovY = message.FOV; MyUtils.Init(ref envMatrices.ViewFrustumD); envMatrices.ViewFrustumD.Matrix = envMatrices.ViewProjectionD; MyUtils.Init(ref envMatrices.ViewFrustumClippedD); envMatrices.ViewFrustumClippedD.Matrix = envMatrices.ViewD * envMatrices.OriginalProjectionD; }
private void SwitchWeapon(ref IMyHandheldGunObject <MyDeviceBase> weapon, ref MyVRWeaponInfo weaponInfo, List <MyVRWeaponInfo> weapons, ControllerRole role, Vector2?touchpadPos) { int currentIndex = -1; if (weapon != null) { for (int i = 0; i < weapons.Count; ++i) { var info = weapons[i]; if (info.DefinitionId == weapon.DefinitionId) { currentIndex = i; break; } } } int nextIndex = 0; if (touchpadPos != null) { Vector2 pos = touchpadPos.Value; if (touchpadPos != Vector2.Zero) { pos.Normalize(); float anglePerWeaponSector = 360f / weapons.Count; float dot = Vector2.Dot(Vector2.UnitX, pos); float angle = (float)Math.Acos(Math.Abs(dot)); if (pos.Y >= 0) { if (dot < 0) { angle = 180 - angle; } } else { if (dot < 0) { angle = 180 + angle; } else { angle = 360 - angle; } } nextIndex = (int)Math.Floor(angle / anglePerWeaponSector); Debug.Assert(nextIndex < weapons.Count); } } if (nextIndex == currentIndex) { return; } if (weapon != null) { weapon.OnControlReleased(); MyEntity weaponEntity = (MyEntity)weapon; weaponEntity.Close(); weapon = null; } { weaponInfo = weapons[nextIndex]; weapon = CreateWeapon(weaponInfo.DefinitionId, weaponInfo.Reference); weapon.OnControlAcquired(null); MyEntity weaponEntity = (MyEntity)weapon; } var gunBase = weapon.GunBase as MyGunBase; Matrix holdingMatrix = gunBase != null ? gunBase.m_holdingDummyMatrix : Matrix.Identity; MyOpenVR.LMUAdd(holdingMatrix, m_worldMatrixOriginal, role, weaponInfo.Reference); }
void HandleButtons(ref IMyHandheldGunObject <MyDeviceBase> weapon, ref MyVRWeaponInfo weaponInfo, bool secondController, List <MyVRWeaponInfo> weapons) { if (weapon == null) { return; } MyWeaponSharedActionsComponentBase sharedWeaponActionsComponent = null; ((MyEntity)weapon).Components.TryGet <MyWeaponSharedActionsComponentBase>(out sharedWeaponActionsComponent); if (sharedWeaponActionsComponent != null) { sharedWeaponActionsComponent.Update(); } if (MyOpenVR.GetControllerState(secondController).IsButtonPressed(EVRButtonId.k_EButton_SteamVR_Trigger)) {//holding the trigger var gunBase = weapon.GunBase as MyGunBase; MyGunStatusEnum status; weapon.CanShoot(MyShootActionEnum.PrimaryAction, this.EntityId, out status); if (status != MyGunStatusEnum.Cooldown && status != MyGunStatusEnum.BurstLimit) { weapon.Shoot(MyShootActionEnum.PrimaryAction, gunBase != null ? (Vector3)gunBase.GetMuzzleWorldMatrix().Forward : Vector3.Forward, null); if (sharedWeaponActionsComponent != null) { sharedWeaponActionsComponent.Shoot(MyShootActionEnum.PrimaryAction); } } } if (MyOpenVR.GetControllerState(secondController).WasButtonReleased(EVRButtonId.k_EButton_SteamVR_Trigger)) { weapon.EndShoot(MyShootActionEnum.PrimaryAction); if (sharedWeaponActionsComponent != null) { sharedWeaponActionsComponent.EndShoot(MyShootActionEnum.PrimaryAction); } } if (MyOpenVR.GetControllerState(secondController).WasButtonPressed(EVRButtonId.k_EButton_Grip)) { var gunBase = weapon.GunBase as MyGunBase; weapon.Shoot(MyShootActionEnum.SecondaryAction, gunBase != null ? (Vector3)gunBase.GetMuzzleWorldMatrix().Forward : Vector3.Forward, null); if (sharedWeaponActionsComponent != null) { sharedWeaponActionsComponent.Shoot(MyShootActionEnum.SecondaryAction); } } if (MyOpenVR.GetControllerState(secondController).WasButtonReleased(EVRButtonId.k_EButton_Grip)) { weapon.EndShoot(MyShootActionEnum.SecondaryAction); if (sharedWeaponActionsComponent != null) { sharedWeaponActionsComponent.EndShoot(MyShootActionEnum.SecondaryAction); } } if (MyOpenVR.GetControllerState(secondController).WasButtonPressed(EVRButtonId.k_EButton_ApplicationMenu)) { var gunBase = weapon.GunBase as MyGunBase; weapon.Shoot(MyShootActionEnum.TertiaryAction, gunBase != null ? (Vector3)gunBase.GetMuzzleWorldMatrix().Forward : Vector3.Forward, null); if (sharedWeaponActionsComponent != null) { sharedWeaponActionsComponent.Shoot(MyShootActionEnum.TertiaryAction); } } if (MyOpenVR.GetControllerState(secondController).WasButtonReleased(EVRButtonId.k_EButton_ApplicationMenu)) { weapon.EndShoot(MyShootActionEnum.TertiaryAction); if (sharedWeaponActionsComponent != null) { sharedWeaponActionsComponent.EndShoot(MyShootActionEnum.TertiaryAction); } } Vector2 touchpadPos = Vector2.Zero; bool validTouchpadPos = false; validTouchpadPos = MyOpenVR.GetControllerState(secondController).GetTouchpadXY(ref touchpadPos); if (MyOpenVR.GetControllerState(secondController).WasButtonPressed(EVRButtonId.k_EButton_SteamVR_Touchpad)) { SwitchWeapon(ref weapon, ref weaponInfo, weapons, secondController ? ControllerRole.rightHand : ControllerRole.leftHand, validTouchpadPos ? touchpadPos : (Vector2?)null); } else { if (validTouchpadPos) { if (weapon is ITouchPadListener) { (weapon as ITouchPadListener).TouchPadChanged(touchpadPos); } } } }
public bool DoDamage(float damage, MyStringHash damageSource, bool sync, VRage.Game.ModAPI.MyHitInfo?hitInfo = null, long attackerId = 0) { MyDamage += damage; MyOpenVR.FadeToColor(0.1f, COLOR_WHEN_HIT); return(true); }
private static void DrawScene() { AddDebugQueueMessage("Frame render start"); MyGpuProfiler.IC_BeginBlock("UpdateSceneFrame"); UpdateSceneFrame(); MyGpuProfiler.IC_EndBlock(); MyGpuProfiler.IC_BeginBlock("Clear"); MyGBuffer.Main.Clear(VRageMath.Color.Black); MyGpuProfiler.IC_EndBlock(); if (MyOpenVR.Static != null) { ProfilerShort.Begin("OpenVR.WaitForNextStart"); MyOpenVR.WaitForNextStart(); ProfilerShort.End(); } IBorrowedRtvTexture debugAmbientOcclusion; // TODO: Think of another way to get this texture to the DebugRenderer... ProfilerShort.Begin("DrawGameScene"); DrawGameScene(Backbuffer, out debugAmbientOcclusion); ProfilerShort.End(); if (MyOpenVR.Static != null) { ProfilerShort.Begin("OpenVR.DisplayEye"); MyGpuProfiler.IC_BeginBlock("OpenVR.DisplayEye"); MyGBuffer.Main.Clear(VRageMath.Color.Black);//image is in HMD now, lets draw the rest for overlay MyOpenVR.Static.DisplayEye(MyRender11.Backbuffer.Resource.NativePointer); MyGpuProfiler.IC_EndBlock(); ProfilerShort.End(); } ProfilerShort.Begin("Draw scene debug"); MyGpuProfiler.IC_BeginBlock("Draw scene debug"); DrawSceneDebug(); ProfilerShort.End(); ProfilerShort.Begin("ProcessDebugMessages"); ProcessDebugMessages(); MyGpuProfiler.IC_EndBlock(); ProfilerShort.End(); ProfilerShort.Begin("MyDebugRenderer.Draw"); MyGpuProfiler.IC_BeginBlock("MyDebugRenderer.Draw"); MyDebugRenderer.Draw(MyRender11.Backbuffer, debugAmbientOcclusion); MyGpuProfiler.IC_EndBlock(); ProfilerShort.End(); debugAmbientOcclusion.Release(); ProfilerShort.Begin("MyPrimitivesRenderer.Draw"); MyGpuProfiler.IC_BeginBlock("MyPrimitivesRenderer.Draw"); MyPrimitivesRenderer.Draw(MyRender11.Backbuffer); MyGpuProfiler.IC_EndBlock(); ProfilerShort.End(); ProfilerShort.Begin("MyLinesRenderer.Draw"); MyGpuProfiler.IC_BeginBlock("MyLinesRenderer.Draw"); MyLinesRenderer.Draw(MyRender11.Backbuffer); MyGpuProfiler.IC_EndBlock(); ProfilerShort.End(); if (m_screenshot.HasValue && m_screenshot.Value.IgnoreSprites) { ProfilerShort.Begin("Screenshot"); if (m_screenshot.Value.SizeMult == Vector2.One) { SaveScreenshotFromResource(Backbuffer); } else { TakeCustomSizedScreenshot(m_screenshot.Value.SizeMult); } ProfilerShort.End(); } ProfilerShort.Begin("ProcessDebugOutput"); AddDebugQueueMessage("Frame render end"); ProcessDebugOutput(); ProfilerShort.End(); }
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: { AddDebugQueueMessage("Frame render start"); MyGpuProfiler.IC_BeginBlock("UpdateSceneFrame"); UpdateSceneFrame(); MyGpuProfiler.IC_EndBlock(); var testingDepth = MyRender11.MultisamplingEnabled ? MyScreenDependants.m_resolvedDepth : MyGBuffer.Main.DepthStencil; MyGpuProfiler.IC_BeginBlock("Clear"); MyGBuffer.Main.Clear(VRageMath.Color.Black); MyGpuProfiler.IC_EndBlock(); if (MyOpenVR.Static != null) { ProfilerShort.Begin("OpenVR.WaitForNextStart"); MyOpenVR.WaitForNextStart(); ProfilerShort.End(); } ProfilerShort.Begin("DrawGameScene"); DrawGameScene(Backbuffer); ProfilerShort.End(); if (MyOpenVR.Static != null) { ProfilerShort.Begin("OpenVR.DisplayEye"); MyGpuProfiler.IC_BeginBlock("OpenVR.DisplayEye"); MyGBuffer.Main.Clear(VRageMath.Color.Black); //image is in HMD now, lets draw the rest for overlay MyOpenVR.Static.DisplayEye(MyRender11.Backbuffer.m_resource.NativePointer); MyGpuProfiler.IC_EndBlock(); ProfilerShort.End(); } ProfilerShort.Begin("TransferPerformanceStats"); TransferPerformanceStats(); ProfilerShort.End(); ProfilerShort.Begin("Draw scene debug"); MyGpuProfiler.IC_BeginBlock("Draw scene debug"); DrawSceneDebug(); ProfilerShort.End(); ProfilerShort.Begin("ProcessDebugMessages"); ProcessDebugMessages(); MyGpuProfiler.IC_EndBlock(); ProfilerShort.End(); ProfilerShort.Begin("MyDebugRenderer.Draw"); MyGpuProfiler.IC_BeginBlock("MyDebugRenderer.Draw"); MyDebugRenderer.Draw(MyRender11.Backbuffer); MyGpuProfiler.IC_EndBlock(); ProfilerShort.End(); 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) { ProfilerShort.Begin("Screenshot"); if (m_screenshot.Value.SizeMult == Vector2.One) { SaveScreenshotFromResource(Backbuffer.m_resource); } else { TakeCustomSizedScreenshot(m_screenshot.Value.SizeMult); } ProfilerShort.End(); } 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) { ProfilerShort.Begin("MyRenderStatsDraw.Draw"); MyGpuProfiler.IC_BeginBlock("MyLinesRenderer.Draw"); MyRenderStatsDraw.Draw(MyRenderStats.m_stats, 0.6f, VRageMath.Color.Yellow); MyGpuProfiler.IC_EndBlock(); ProfilerShort.End(); } ProfilerShort.Begin("ProcessDebugOutput"); AddDebugQueueMessage("Frame render end"); ProcessDebugOutput(); ProfilerShort.End(); break; } } }
private static void SetupCameraMatricesInternal(MyRenderMessageSetCameraViewMatrix message, MyEnvironmentMatrices envMatrices, MyStereoRegion typeofEnv) { var originalProjection = message.ProjectionMatrix; var viewMatrix = message.ViewMatrix; var cameraPosition = message.CameraPosition; if (MyStereoRender.Enable) { if (MyOpenVR.Static != null && message.LastMomentUpdateIndex != 0) { MatrixD origin = MatrixD.Identity; MyOpenVR.LMUMatrixGetOrigin(ref origin, message.LastMomentUpdateIndex); viewMatrix = MatrixD.Invert(origin); } } var viewMatrixAt0 = viewMatrix; viewMatrixAt0.M14 = 0; viewMatrixAt0.M24 = 0; viewMatrixAt0.M34 = 0; viewMatrixAt0.M41 = 0; viewMatrixAt0.M42 = 0; viewMatrixAt0.M43 = 0; viewMatrixAt0.M44 = 1; if (MyStereoRender.Enable) { if (MyOpenVR.Static != null) { if (message.LastMomentUpdateIndex != 0) { var tViewMatrix = Matrix.Transpose(viewMatrix); var viewHMDat0 = MyOpenVR.ViewHMD; viewHMDat0.M14 = 0; viewHMDat0.M24 = 0; viewHMDat0.M34 = 0; viewHMDat0.M41 = 0; viewHMDat0.M42 = 0; viewHMDat0.M43 = 0; viewHMDat0.M44 = 1; //cameraPosition += tViewMatrix.Up * MyOpenVR.ViewHMD.Translation.Y; //cameraPosition += tViewMatrix.Backward * MyOpenVR.ViewHMD.Translation.X; //cameraPosition += tViewMatrix.Right * MyOpenVR.ViewHMD.Translation.Z; viewMatrixAt0 = viewMatrixAt0 * viewHMDat0; viewMatrix = viewMatrix * viewHMDat0; if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.LEFT) { viewMatrixAt0 = GetMatrixEyeTranslation(true, viewMatrixAt0) * viewMatrixAt0; viewMatrix = GetMatrixEyeTranslation(true, viewMatrix) * viewMatrix; } else if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.RIGHT) { viewMatrixAt0 = GetMatrixEyeTranslation(false, viewMatrixAt0) * viewMatrixAt0; viewMatrix = GetMatrixEyeTranslation(false, viewMatrix) * viewMatrix; } } } else { if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.LEFT) { viewMatrixAt0 = GetMatrixEyeTranslation(true, viewMatrixAt0) * viewMatrixAt0; viewMatrix = GetMatrixEyeTranslation(true, viewMatrix) * viewMatrix; } else if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.RIGHT) { viewMatrixAt0 = GetMatrixEyeTranslation(false, viewMatrixAt0) * viewMatrixAt0; viewMatrix = GetMatrixEyeTranslation(false, viewMatrix) * viewMatrix; } } } float aspectRatio = MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y; if (typeofEnv != MyStereoRegion.FULLSCREEN) { aspectRatio /= 2; } Matrix projMatrix = Matrix.CreatePerspectiveFovRhInfiniteComplementary(message.FOV, aspectRatio, message.NearPlane); cameraPosition.AssertIsValid(); envMatrices.ViewAt0 = viewMatrixAt0; envMatrices.InvViewAt0 = Matrix.Invert(viewMatrixAt0); envMatrices.ViewProjectionAt0 = viewMatrixAt0 * projMatrix; envMatrices.InvViewProjectionAt0 = Matrix.Invert(viewMatrixAt0 * projMatrix); envMatrices.CameraPosition = cameraPosition; envMatrices.View = viewMatrix; envMatrices.ViewD = viewMatrix; envMatrices.OriginalProjectionD = originalProjection; envMatrices.InvView = Matrix.Invert(viewMatrix); envMatrices.ViewProjection = viewMatrix * projMatrix; envMatrices.InvViewProjection = Matrix.Invert(viewMatrix * projMatrix); envMatrices.Projection = projMatrix; envMatrices.InvProjection = Matrix.Invert(projMatrix); envMatrices.ViewProjectionD = envMatrices.ViewD * (MatrixD)projMatrix; envMatrices.NearClipping = message.NearPlane; envMatrices.FarClipping = message.FarPlane; envMatrices.LargeDistanceFarClipping = message.FarPlane * 500.0f; int width = MyRender11.ViewportResolution.X; int height = MyRender11.ViewportResolution.Y; float fovH = message.FOV; envMatrices.FovH = fovH; envMatrices.FovV = (float)(2 * Math.Atan(Math.Tan(fovH / 2.0) * (height / (double)width))); MyUtils.Init(ref envMatrices.ViewFrustumD); envMatrices.ViewFrustumD.Matrix = envMatrices.ViewProjectionD; MyUtils.Init(ref envMatrices.ViewFrustumClippedD); envMatrices.ViewFrustumClippedD.Matrix = envMatrices.ViewD * envMatrices.OriginalProjectionD; }