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;
            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;
        }
Example #2
0
        internal void Add(MyActor child)
        {
            child.AddComponent(MyComponentFactory <MyGroupLeafComponent> .Create());

            child.GetGroupLeaf().m_parent = m_owner;

            if (m_children == null)
            {
                m_children = new List <MyActor>();
            }
            m_children.Add(child);

            if (child.m_relativeTransform == null)
            {
                child.m_relativeTransform = child.WorldMatrix * Matrix.Invert(m_owner.WorldMatrix);
            }

            if (!m_owner.m_localAabb.HasValue)
            {
                m_owner.m_localAabb = child.m_localAabb;
            }
            else
            {
                var localAabb = child.m_localAabb.Value;
                m_owner.m_localAabb = m_owner.m_localAabb.Value.Include(ref localAabb);
            }

            PropagateMatrixChange(child);

            if (child.GetRenderable() == null)
            {
                return;
            }

            //var mesh = child.GetRenderable().GetMesh();
            var model    = child.GetRenderable().GetModel();
            var material = MyMeshes.GetMeshPart(model, 0, 0).Info.Material;

            bool fracture = model.Info.RuntimeGenerated || model.Info.Dynamic;

            if (MyMeshMaterials1.IsMergable(material) && MyBigMeshTable.Table.IsMergable(model) && !fracture)
            {
                var materialRk            = MyMeshMaterials1.Table[material.Index].RepresentationKey;
                var mergeGroupForMaterial = m_materialGroups.Get(materialRk);
                if (mergeGroupForMaterial == null)
                {
                    var proxyIndex = m_materialGroups.Count;
                    mergeGroupForMaterial = new MyMaterialMergeGroup(MyBigMeshTable.Table, material, proxyIndex);
                    m_materialGroups[MyMeshMaterials1.Table[material.Index].RepresentationKey] = mergeGroupForMaterial;

                    m_dirtyProxy = true;
                }

                child.GetRenderable().SetStandaloneRendering(false);
                child.GetGroupLeaf().m_mergeGroup = mergeGroupForMaterial;

                MyBigMeshTable.Table.AddMesh(model);
                mergeGroupForMaterial.AddEntity(child, model);
                mergeGroupForMaterial.UpdateEntity(child);
            }
            else
            {
                //Debug.WriteLine(String.Format("Mesh {0}, material {1} is not mergable", mesh.Name, material));
            }

            m_dirtyTree = true;
        }