internal void SetAnimationBones(Matrix[] simulatedBones) { if (m_skeletonHierarchy == null) return; var skeletonCount = m_skeletonHierarchy.Length; for (int i = 0; i < skeletonCount; i++) { m_absoluteTransforms[i] = simulatedBones[i]; } for (int i = 0; i < skeletonCount; i++) { if (m_skeletonHierarchy[i].Parent != -1) { m_absoluteTransforms[i] = m_absoluteTransforms[i] * m_absoluteTransforms[m_skeletonHierarchy[i].Parent]; } } int bonesCount = m_skeletonIndices.Length; for (int i = 0; i < bonesCount; i++) { m_skinTransforms[i] = m_skeletonHierarchy[m_skeletonIndices[i]].SkinTransform * m_absoluteTransforms[m_skeletonIndices[i]]; } }
internal void SetAnimationBones(Matrix[] boneAbsoluteTransforms, IReadOnlyList<MyBoneDecalUpdate> boneDecals) { if (m_skeletonHierarchy == null) return; var skeletonCount = m_skeletonHierarchy.Length; for (int i = 0; i < skeletonCount; i++) m_absoluteTransforms[i] = boneAbsoluteTransforms[i]; int bonesCount = m_skeletonIndices.Length; for (int i = 0; i < bonesCount; i++) { m_skinTransforms[i] = Matrix.Transpose(m_skeletonHierarchy[m_skeletonIndices[i]].SkinTransform * m_absoluteTransforms[m_skeletonIndices[i]]); } m_decalUpdateCache.Clear(); for (int it = 0; it < boneDecals.Count; it++) { MyBoneDecalUpdate pair = boneDecals[it]; MyDecalTopoData data; bool found = MyScreenDecals.GetDecalTopoData(pair.DecalID, out data); if (!found) continue; Matrix skinningTrans = ComputeSkinning(data.BoneIndices, ref data.BoneWeights); Matrix transform = data.MatrixBinding * skinningTrans; m_decalUpdateCache.Add(new MyDecalPositionUpdate() { ID = pair.DecalID, Transform = transform }); } MyScreenDecals.UpdateDecals(m_decalUpdateCache); }
internal static MyPerInstanceData FromWorldMatrix(ref Matrix mat) { return new MyPerInstanceData { Row0 = new Vector4(mat.M11, mat.M21, mat.M31, mat.M41), Row1 = new Vector4(mat.M12, mat.M22, mat.M32, mat.M42), Row2 = new Vector4(mat.M13, mat.M23, mat.M33, mat.M43) }; }
static unsafe void TransferData() { var mapping = MyMapping.MapDiscard(RC.Context, MyCommon.GetObjectCB(sizeof(Matrix) * MaxCustomProjections)); for (int i = 0; i < MyRenderProxy.BillboardsViewProjectionRead.Count; i++) { var view = MyRenderProxy.BillboardsViewProjectionRead[i].View; var projection = MyRenderProxy.BillboardsViewProjectionRead[i].Projection; var scaleX = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Width / (float)MyRender11.ViewportResolution.X; var scaleY = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Height / (float)MyRender11.ViewportResolution.Y; var offsetX = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.OffsetX / (float)MyRender11.ViewportResolution.X; var offsetY = (MyRender11.ViewportResolution.Y - MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.OffsetY - MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Height) / (float)MyRender11.ViewportResolution.Y; var viewportTransformation = new Matrix( scaleX, 0, 0, 0, 0, scaleY, 0, 0, 0, 0, 1, 0, offsetX, offsetY, 0, 1 ); mapping.stream.Write(Matrix.Transpose(view * projection * viewportTransformation)); } for (int i = MyRenderProxy.BillboardsViewProjectionRead.Count; i < MaxCustomProjections; i++) { mapping.stream.Write(Matrix.Identity); } mapping.Unmap(); mapping = MyMapping.MapDiscard(RC.Context, m_VB.Buffer); fixed(void * ptr = m_vertexData) { mapping.stream.Write(new IntPtr(ptr), 0, (sizeof(MyVertexFormatPosition) * MaxBillboards * 4)); } mapping.Unmap(); mapping = MyMapping.MapDiscard(RC.Context, m_SB.Buffer); fixed(void *ptr = m_billboardData) { mapping.stream.Write(new IntPtr(ptr), 0, (sizeof(MyBillboardData) * MaxBillboards)); } mapping.Unmap(); }
public static void SetCustomProjection(Matrix projection) { ProjectionMatrix = projection; }
/// <summary> /// Changes FOV for ForwardCamera (updates projection matrix) /// SetViewMatrix overwrites this changes /// </summary> /// <param name="fov"></param> public static void ChangeFov(float fov) { // Projection matrix according to zoom level ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(fov, AspectRatio, GetSafeNear(), MyRenderCamera.FAR_PLANE_DISTANCE); }
public static void SetParentCullObject(uint renderObject, uint parentCullObject, Matrix? childToParent = null) { var message = MessagePool.Get<MyRenderMessageSetParentCullObject>(MyRenderMessageEnum.SetParentCullObject); message.ID = renderObject; message.CullObjectID = parentCullObject; message.ChildToParent = childToParent; EnqueueMessage(message); }
public static void ChangeClipPlanes(float near, float far, bool applyNow = false) { Debug.Assert(!m_backupMatrix.HasValue, "Reset clip planes before changing clip planes again"); m_backupMatrix = ProjectionMatrix; ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(FieldOfView, AspectRatio, near, far); if (applyNow) { UpdateCamera(); } }
internal void UpdateEntity(uint ID, ref Matrix matrix) { m_perInstance.Data[m_entities[ID].InstanceIndex] = MyPerInstanceData.FromWorldMatrix(ref matrix); m_instancesDataDirty = true; }
public static void DebugDrawLine2D(Vector2 pointFrom, Vector2 pointTo, Color colorFrom, Color colorTo, Matrix? projection = null) { var message = MessagePool.Get<MyRenderMessageDebugDrawLine2D>(MyRenderMessageEnum.DebugDrawLine2D); message.PointFrom = pointFrom; message.PointTo = pointTo; message.ColorFrom = colorFrom; message.ColorTo = colorTo; message.Projection = projection; EnqueueMessage(message); }
public static bool SetCharacterTransforms(uint characterID, Matrix[] boneTransforms, IReadOnlyList<MyBoneDecalUpdate> boneDecalUpdates) { var message = MessagePool.Get<MyRenderMessageSetCharacterTransforms>(MyRenderMessageEnum.SetCharacterTransforms); message.CharacterID = characterID; if (message.BoneAbsoluteTransforms == null || message.BoneAbsoluteTransforms.Length < boneTransforms.Length) message.BoneAbsoluteTransforms = new Matrix[boneTransforms.Length]; Array.Copy(boneTransforms, message.BoneAbsoluteTransforms, boneTransforms.Length); message.BoneDecalUpdates.AddRange(boneDecalUpdates); EnqueueMessage(message); return false; }
static unsafe void TransferData() { var mapping = MyMapping.MapDiscard(RC.DeviceContext, MyCommon.GetObjectCB(sizeof(Matrix) * MaxCustomProjections)); for (int i = 0; i < MyRenderProxy.BillboardsViewProjectionRead.Count; i++) { var view = MyRenderProxy.BillboardsViewProjectionRead[i].View; var projection = MyRenderProxy.BillboardsViewProjectionRead[i].Projection; var scaleX = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Width / (float)MyRender11.ViewportResolution.X; var scaleY = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Height / (float)MyRender11.ViewportResolution.Y; var offsetX = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.OffsetX / (float)MyRender11.ViewportResolution.X; var offsetY = (MyRender11.ViewportResolution.Y - MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.OffsetY - MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Height) / (float)MyRender11.ViewportResolution.Y; var viewportTransformation = new Matrix( scaleX, 0, 0, 0, 0, scaleY, 0, 0, 0, 0, 1, 0, offsetX, offsetY, 0, 1 ); var transpose = Matrix.Transpose(view * projection * viewportTransformation); mapping.WriteAndPosition(ref transpose); } for (int i = MyRenderProxy.BillboardsViewProjectionRead.Count; i < MaxCustomProjections; i++) { mapping.WriteAndPosition(ref Matrix.Identity); } mapping.Unmap(); int billboardCount = m_sorted + m_unsorted; mapping = MyMapping.MapDiscard(RC.DeviceContext, m_VB.Buffer); mapping.WriteAndPosition(m_vertexData, 0, billboardCount * 4); mapping.Unmap(); mapping = MyMapping.MapDiscard(RC.DeviceContext, m_SB.Buffer); mapping.WriteAndPosition(m_billboardData, 0, billboardCount); mapping.Unmap(); }
internal unsafe override sealed void RecordCommands(MyRenderableProxy proxy) { if (proxy.Mesh.Buffers.IB == IndexBufferId.NULL || proxy.Draw.IndexCount == 0 || (proxy.flags & MyRenderableProxyFlags.SkipInMainView) > 0) { return; } Stats.Meshes++; SetProxyConstants(proxy); BindProxyGeometry(proxy); Debug.Assert(proxy.Shaders.VS != null); RC.BindShaders(proxy.Shaders); if ((proxy.flags & MyRenderableProxyFlags.DisableFaceCulling) > 0) { RC.SetRS(MyRender11.m_nocullRasterizerState); } else { RC.SetRS(null); } //#if DEBUG if (MyRender11.Settings.Wireframe) { if ((proxy.flags & MyRenderableProxyFlags.DisableFaceCulling) > 0) { RC.SetRS(MyRender11.m_nocullWireframeRasterizerState); } else { RC.SetRS(MyRender11.m_wireframeRasterizerState); } } //#endif //for (int i = 0; i < proxy.submeshes.Length; i++) //{ Stats.Submeshes++; var submesh = proxy.Draw; //if (submesh.Material != null && submesh.Material.TexturesHash != Locals.matTexturesID) //{ // Locals.matTexturesID = submesh.Material.TexturesHash; // RC.BindRawSRV(submesh.Material.SRVs); //} if (submesh.MaterialId != Locals.matTexturesID) { Locals.matTexturesID = submesh.MaterialId; var material = MyMaterials1.ProxyPool.Data[submesh.MaterialId.Index]; RC.MoveConstants(ref material.MaterialConstants); RC.SetConstants(ref material.MaterialConstants, MyCommon.MATERIAL_SLOT); RC.SetSRVs(ref material.MaterialSRVs); } //if (submesh.Material != null && submesh.Material.ConstantsHash != Locals.matConstantsID && submesh.Material.ConstantsBuffer != null) //{ // Stats.MaterialConstantsChanges++; // Locals.matConstantsID = submesh.Material.ConstantsHash; // RC.SetCB(MyCommon.MATERIAL_SLOT, submesh.Material.ConstantsBuffer); // var mapping = MyMapping.MapDiscard(RC.Context, submesh.Material.ConstantsBuffer); // mapping.stream.WriteRange(submesh.Material.Constants); // mapping.Unmap(); //} if (proxy.skinningMatrices != null) { Stats.ObjectConstantsChanges++; MyObjectData objectData = proxy.ObjectData; objectData.Translate(-MyEnvironment.CameraPosition); MyMapping mapping; mapping = MyMapping.MapDiscard(RC.Context, proxy.objectBuffer); void *ptr = &objectData; mapping.stream.Write(new IntPtr(ptr), 0, sizeof(MyObjectData)); if (proxy.skinningMatrices != null) { if (submesh.BonesMapping == null) { for (int j = 0; j < Math.Min(MyRender11Constants.SHADER_MAX_BONES, proxy.skinningMatrices.Length); j++) { mapping.stream.Write(Matrix.Transpose(proxy.skinningMatrices[j])); } } else { for (int j = 0; j < submesh.BonesMapping.Length; j++) { mapping.stream.Write(Matrix.Transpose(proxy.skinningMatrices[submesh.BonesMapping[j]])); } } } mapping.Unmap(); } if (proxy.instanceCount == 0 && submesh.IndexCount > 0) { RC.Context.DrawIndexed(submesh.IndexCount, submesh.StartIndex, submesh.BaseVertex); RC.Stats.DrawIndexed++; Stats.Instances++; Stats.Triangles += submesh.IndexCount / 3; } else if (submesh.IndexCount > 0) { RC.Context.DrawIndexedInstanced(submesh.IndexCount, proxy.instanceCount, submesh.StartIndex, submesh.BaseVertex, proxy.startInstance); RC.Stats.DrawIndexedInstanced++; Stats.Instances += proxy.instanceCount; Stats.Triangles += proxy.instanceCount * submesh.IndexCount / 3; } //} }
internal static void Draw(IRtvBindable renderTarget, IDepthStencil depth) { RC.SetScreenViewport(); RC.SetPrimitiveTopology(PrimitiveTopology.TriangleList); RC.SetInputLayout(m_inputLayout); RC.SetRasterizerState(MyRasterizerStateManager.NocullRasterizerState); RC.SetDepthStencilState(MyDepthStencilStateManager.DefaultDepthState); RC.VertexShader.Set(m_vs); RC.PixelShader.Set(m_ps); RC.SetRtv(depth, MyDepthStencilAccess.ReadOnly, renderTarget); RC.AllShaderStages.SetConstantBuffer(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants); RC.SetBlendState(MyBlendStateManager.BlendTransparent); SortTransparent(); var transpose = Matrix.Transpose(MyRender11.Environment.Matrices.ViewProjectionAt0); var mapping = MyMapping.MapDiscard(MyCommon.ProjectionConstants); mapping.WriteAndPosition(ref transpose); mapping.Unmap(); CheckBufferSize(m_vertexList.Count); RC.SetVertexBuffer(0, m_VB.Buffer, m_VB.Stride); if (m_vertexList.Count > 0) { mapping = MyMapping.MapDiscard(m_VB.Buffer); mapping.WriteAndPosition(m_vertexList.GetInternalArray(), 0, m_vertexList.Count); mapping.Unmap(); } RC.Draw(m_vertexList.Count, 0); if (m_debugMeshes.Count > 0) { var transposeViewProj = Matrix.Transpose(MyRender11.Environment.Matrices.ViewProjection); mapping = MyMapping.MapDiscard(MyCommon.ProjectionConstants); mapping.WriteAndPosition(ref transposeViewProj); mapping.Unmap(); } foreach (var mesh in m_debugMeshes.Values) { if (mesh.depth) { RC.SetRtv(depth, MyDepthStencilAccess.ReadWrite, renderTarget); } else { RC.SetRtv(renderTarget); } if (mesh.edges) { RC.SetRasterizerState(MyRasterizerStateManager.NocullWireframeRasterizerState); } else { RC.SetRasterizerState(MyRasterizerStateManager.NocullRasterizerState); } RC.SetVertexBuffer(0, mesh.vbuffer.Buffer, mesh.vbuffer.Stride); RC.Draw(mesh.vbuffer.Capacity, 0); } RC.SetBlendState(null); m_vertexList.Clear(); m_postSortVertexList.Clear(); m_triangleSortDistance.Clear(); m_sortedIndices.Clear(); }
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; }
static void DrawInstanceComponent(MyInstanceComponent instanceComponent, List <MyHighlightDesc> highlightDescs) { MyRenderContext RC = MyRender11.RC; // common settings (combination of MyHighlightPass.cs and MyRenderingPass.cs): MyMapping mapping = MyMapping.MapDiscard(MyCommon.ProjectionConstants); Matrix matrix = MyRender11.Environment.Matrices.ViewProjectionAt0; matrix = Matrix.Transpose(matrix); mapping.WriteAndPosition(ref matrix); mapping.Unmap(); RC.VertexShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); RC.VertexShader.SetConstantBuffer(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants); RC.PixelShader.SetSamplers(0, MySamplerStateManager.StandardSamplers); RC.PixelShader.SetSrv(MyCommon.DITHER_8X8_SLOT, MyGeneratedTextureManager.Dithering8x8Tex); //RC.AllShaderStages.SetConstantBuffer(MyCommon.ALPHAMASK_VIEWS_SLOT, MyCommon.AlphamaskViewsConstants); // not used! Maybe impostors? RC.SetDepthStencilState(MyDepthStencilStateManager.WriteHighlightStencil, MyHighlight.HIGHLIGHT_STENCIL_MASK); RC.SetBlendState(null); RC.SetPrimitiveTopology(PrimitiveTopology.TriangleList); RC.SetRasterizerState(MyRasterizerStateManager.NocullRasterizerState); RC.SetScreenViewport(); RC.PixelShader.SetConstantBuffer(4, MyCommon.HighlightConstants); for (int i = 0; i < instanceComponent.GetHighlightLodsCount(); i++) { MyLod lod; MyInstanceLodState stateId; float stateData; instanceComponent.GetHighlightLod(i, out lod, out stateId, out stateData); RC.SetIndexBuffer(lod.IB); RC.SetVertexBuffer(0, lod.VB0); IConstantBuffer objectCB = GetObjectCB(RC, instanceComponent, stateData); RC.VertexShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, objectCB); RC.PixelShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, objectCB); foreach (MyHighlightDesc desc in highlightDescs) { MyHighlightDesc descRef = desc; WriteHighlightConstants(ref descRef); if (string.IsNullOrEmpty(desc.SectionName)) { foreach (var part in lod.Parts) { DrawHighlightedPart(RC, part, stateId); } } else { if (lod.Sections != null && lod.Sections.ContainsKey(desc.SectionName)) { foreach (var part in lod.Sections[desc.SectionName].Parts) { DrawHighlightedPart(RC, part, stateId); } } } } } }
void IMyEntity.SetLocalMatrix(VRageMath.Matrix localMatrix, object source) { PositionComp.SetLocalMatrix(localMatrix, source); }
private static void SetupCustomViewProjection(MyEffectTransparentGeometry effect, ref Viewport originalViewport, MyBillboard lastBillboard, ref bool ignoreDepth, ref Matrix projectionMatrix) { if (lastBillboard.CustomViewProjection != -1 && MyRenderProxy.BillboardsViewProjectionRead.ContainsKey(lastBillboard.CustomViewProjection)) { var billboardViewProjection = MyRenderProxy.BillboardsViewProjectionRead[lastBillboard.CustomViewProjection]; effect.SetViewMatrix(ref billboardViewProjection.ViewAtZero); effect.SetProjectionMatrix(ref billboardViewProjection.Projection); Matrix invProjectionMatrix = Matrix.Invert((MyRenderCamera.m_backupMatrix.HasValue ? MyRenderCamera.m_backupMatrix.Value : MyRenderCamera.ProjectionMatrix)); effect.SetInverseDefaultProjectionMatrix(ref invProjectionMatrix); Viewport viewport = new Viewport((int)billboardViewProjection.Viewport.OffsetX, (int)billboardViewProjection.Viewport.OffsetY, (int)billboardViewProjection.Viewport.Width, (int)billboardViewProjection.Viewport.Height); if (MyRender.GetScreenshot() != null) viewport = new Viewport((int)(billboardViewProjection.Viewport.OffsetX * MyRender.GetScreenshot().SizeMultiplier.X), (int)(billboardViewProjection.Viewport.OffsetY * MyRender.GetScreenshot().SizeMultiplier.Y), (int)(billboardViewProjection.Viewport.Width * MyRender.GetScreenshot().SizeMultiplier.X), (int)(billboardViewProjection.Viewport.Height * MyRender.GetScreenshot().SizeMultiplier.Y)); MyRender.SetDeviceViewport(viewport); ignoreDepth = !billboardViewProjection.DepthRead; projectionMatrix = billboardViewProjection.Projection; } else { var viewMatrix = MyRenderCamera.ViewMatrixAtZero; effect.SetViewMatrix(ref viewMatrix); effect.SetProjectionMatrix(ref MyRenderCamera.ProjectionMatrix); MyRender.SetDeviceViewport(originalViewport); ignoreDepth = false; projectionMatrix = MyRenderCamera.ProjectionMatrix; } }
public static void DrawSecondaryCamera(Matrix viewMatrix) { var message = MessagePool.Get<MyRenderMessageDrawSecondaryCamera>(MyRenderMessageEnum.DrawSecondaryCamera); message.ViewMatrix = viewMatrix; EnqueueMessage(message); }
string IMySlimBlock.CalculateCurrentModel(out VRageMath.Matrix orientation) { return(CalculateCurrentModel(out orientation)); }
internal void AddSphereRing(BoundingSphere sphere, Color color, Matrix onb) { float increment = 1.0f / 32; for (float i=0; i < 1; i += increment) { float a0 = 2* (float)Math.PI * i; float a1 = 2* (float)Math.PI * (i + increment); Add( Vector3.Transform(new Vector3(Math.Cos(a0), 0, Math.Sin(a0)) * sphere.Radius, onb) + sphere.Center, Vector3.Transform(new Vector3(Math.Cos(a1), 0, Math.Sin(a1)) * sphere.Radius, onb) + sphere.Center, color); } }
private static void TransformRay(ref Ray ray, ref Matrix matrix) { ray.Direction = Vector3.Transform(ray.Position + ray.Direction, ref matrix); ray.Position = Vector3.Transform(ray.Position, ref matrix); ray.Direction = ray.Direction - ray.Position; }
internal void SetRelativeTransform(Matrix? m) { m_relativeTransform = m; }
internal static void Draw(MyBindableResource renderTarget, MyBindableResource depth) { RC.SetupScreenViewport(); RC.DeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; RC.SetIL(m_inputLayout); RC.SetRS(MyRender11.m_nocullRasterizerState); RC.SetDS(MyDepthStencilState.DefaultDepthState); RC.SetVS(m_vs); RC.SetPS(m_ps); RC.BindDepthRT(depth, DepthStencilAccess.ReadOnly, renderTarget); RC.SetCB(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants); RC.SetBS(MyRender11.BlendTransparent); SortTransparent(); var transpose = Matrix.Transpose(MyEnvironment.ViewProjectionAt0); var mapping = MyMapping.MapDiscard(MyCommon.ProjectionConstants); mapping.WriteAndPosition(ref transpose); mapping.Unmap(); CheckBufferSize(m_vertexList.Count); RC.SetVB(0, m_VB.Buffer, m_VB.Stride); if (m_vertexList.Count > 0) { mapping = MyMapping.MapDiscard(m_VB.Buffer); mapping.WriteAndPosition(m_vertexList.GetInternalArray(), 0, m_vertexList.Count); mapping.Unmap(); } RC.DeviceContext.Draw(m_vertexList.Count, 0); if (m_debugMeshes.Count > 0) { var transposeViewProj = Matrix.Transpose(MyEnvironment.ViewProjection); mapping = MyMapping.MapDiscard(MyCommon.ProjectionConstants); mapping.WriteAndPosition(ref transposeViewProj); mapping.Unmap(); } foreach (var mesh in m_debugMeshes.Values) { if (mesh.depth) { RC.BindDepthRT(depth, DepthStencilAccess.ReadWrite, MyRender11.Backbuffer); } else { RC.BindDepthRT(null, DepthStencilAccess.ReadOnly, null); } if (mesh.edges) { RC.SetRS(MyRender11.m_nocullWireframeRasterizerState); } else { RC.SetRS(MyRender11.m_nocullRasterizerState); } RC.SetVB(0, mesh.vbuffer.Buffer, mesh.vbuffer.Stride); RC.DeviceContext.Draw(mesh.vbuffer.Capacity, 0); } RC.SetBS(null); m_vertexList.Clear(); m_postSortVertexList.Clear(); m_triangleSortDistance.Clear(); m_sortedIndices.Clear(); }
public static bool SetCharacterTransforms( uint characterID, Matrix[] boneTransforms ) { var message = MessagePool.Get<MyRenderMessageSetCharacterTransforms>(MyRenderMessageEnum.SetCharacterTransforms); message.CharacterID = characterID; if (message.RelativeBoneTransforms == null || message.RelativeBoneTransforms.Length < boneTransforms.Length) { message.RelativeBoneTransforms = (Matrix[])boneTransforms.Clone(); } else { for (int i = 0; i < boneTransforms.Length; i++) { message.RelativeBoneTransforms[i] = boneTransforms[i]; } } EnqueueMessage(message); return false; }
private static Matrix GetMatrixEyeTranslation(bool isLeftEye, Matrix view) { float Ipd_2 = 0.2f; if (MyOpenVR.Static != null) Ipd_2 = MyOpenVR.Ipd_2; var invViewMatrix = Matrix.Transpose(view); var eyePosition = (!isLeftEye ? invViewMatrix.Left : invViewMatrix.Right) * Ipd_2; return Matrix.CreateTranslation(eyePosition); }
public static uint CreateDecal(int parentId, Matrix localOBB, string material = "") { var message = MessagePool.Get<MyRenderMessageCreateScreenDecal>(MyRenderMessageEnum.CreateScreenDecal); message.ID = GetMessageId(); message.ParentID = (uint)parentId; message.LocalOBB = localOBB; message.DecalMaterial = material; EnqueueMessage(message); return message.ID; }
static void TransferDataCustomProjections() { var mapping = MyMapping.MapDiscard(RC.DeviceContext, m_cbCustomProjections); for (int i = 0; i < MyRenderProxy.BillboardsViewProjectionRead.Count; i++) { MyBillboardViewProjection viewprojection = MyRenderProxy.BillboardsViewProjectionRead[i]; var scaleX = viewprojection.Viewport.Width / (float)MyRender11.ViewportResolution.X; var scaleY = viewprojection.Viewport.Height / (float)MyRender11.ViewportResolution.Y; var offsetX = viewprojection.Viewport.OffsetX / (float)MyRender11.ViewportResolution.X; var offsetY = (MyRender11.ViewportResolution.Y - viewprojection.Viewport.OffsetY - viewprojection.Viewport.Height) / (float)MyRender11.ViewportResolution.Y; var viewportTransformation = new Matrix( scaleX, 0, 0, 0, 0, scaleY, 0, 0, 0, 0, 1, 0, offsetX, offsetY, 0, 1 ); var transpose = Matrix.Transpose(viewprojection.View * viewprojection.Projection * viewportTransformation); mapping.WriteAndPosition(ref transpose); } for (int i = MyRenderProxy.BillboardsViewProjectionRead.Count; i < MAX_CUSTOM_PROJECTIONS_SIZE; i++) mapping.WriteAndPosition(ref Matrix.Identity); mapping.Unmap(); }
public static void SetCameraViewMatrix(MatrixD viewMatrix, Matrix projectionMatrix, Matrix nearProjectionMatrix, float safenear, float nearFov, float fov, float nearPlane, float farPlane, float nearObjectsNearPlane, float nearObjectsFarPlane, Vector3D cameraPosition) { var message = MessagePool.Get<MyRenderMessageSetCameraViewMatrix>(MyRenderMessageEnum.SetCameraViewMatrix); cameraPosition.AssertIsValid(); message.ViewMatrix = viewMatrix; message.ProjectionMatrix = projectionMatrix; message.NearProjectionMatrix = nearProjectionMatrix; message.SafeNear = safenear; message.NearFOV = nearFov; message.FOV = fov; message.NearPlane = nearPlane; message.FarPlane = farPlane; message.NearObjectsNearPlane = nearObjectsNearPlane; message.NearObjectsFarPlane = nearObjectsFarPlane; message.CameraPosition = cameraPosition; EnqueueMessage(message); }
internal void UpdateEntity(uint ID, ref Matrix matrix) { }
public static void ResetClipPlanes(bool applyNow = false) { Debug.Assert(m_backupMatrix.HasValue, "Nothing to reset, use change clip planes first"); ProjectionMatrix = m_backupMatrix.Value; m_backupMatrix = null; if (applyNow) { UpdateCamera(); } //ChangeClipPlanes(GetSafeNear(), MyCamera.FAR_PLANE_DISTANCE, applyNow); }
internal virtual int AddInstance(string mesh, Matrix matrix) { return -1; }
public static void UpdateCamera() { ViewMatrix = m_viewMatrix; ViewProjectionMatrix = ViewMatrix * ProjectionMatrix; UpdateVectors(); UpdateBoundingFrustum(); ViewMatrixAtZero = Matrix.CreateLookAt(Vector3.Zero, ForwardVector, UpVector); ViewProjectionMatrixAtZero = ViewMatrixAtZero * ProjectionMatrix; UpdateLodTransitionDistances(); CornerFrustum = CalculateCornerFrustum(); ProjectionMatrixForFarObjects = Matrix.CreatePerspectiveFieldOfView(FieldOfView, AspectRatio, NEAR_PLANE_FOR_BACKGROUND, FAR_PLANE_FOR_BACKGROUND); }
internal override int AddInstance(string mesh, Matrix matrix) { AddMesh(mesh); var instanceNum = instancesCounter; foreach (var page in m_includedMeshes[mesh]) { m_pageTable.Add(new MyInstancingTableEntry { InstanceId = instanceNum, MeshId = page }); } m_perInstance.Add(MyPerInstanceData.FromWorldMatrix(ref matrix)); instancesCounter++; return 0; }
internal static void SetAnimation(EntityId entity, Matrix[] simulatedBones) { Debug.Assert(Skinnings.ContainsKey(entity)); var skinning = Skinnings[entity]; var skeletonCount = skinning.SkeletonHierarchy.Length; for (int i = 0; i < skeletonCount; i++) { skinning.AbsoluteTransforms[i] = simulatedBones[i]; } for (int i = 0; i < skeletonCount; i++) { if (skinning.SkeletonHierarchy[i].Parent != -1) { skinning.AbsoluteTransforms[i] = skinning.AbsoluteTransforms[i] * skinning.AbsoluteTransforms[skinning.SkeletonHierarchy[i].Parent]; } } int bonesCount = skinning.SkeletonIndices.Length; for (int i = 0; i < bonesCount; i++) { skinning.SkinTransforms[i] = skinning.SkeletonHierarchy[skinning.SkeletonIndices[i]].SkinTransform * skinning.AbsoluteTransforms[skinning.SkeletonIndices[i]]; } }
internal override int AddInstance(string mesh, Matrix matrix) { AddMesh(mesh); m_meshes[mesh].m_perInstance.Add(MyPerInstanceData.FromWorldMatrix(ref matrix)); m_meshes[mesh].instancesCounter++; return 0; }
internal void SetMatrix(ref Matrix matrix) { WorldMatrix = matrix; if (m_localAabb.HasValue) { Aabb = m_localAabb.Value.Transform(WorldMatrix); } // figure out final matrix for (int i = 0; i < m_components.Count; i++) m_components[i].OnMatrixChange(); if(m_localAabb.HasValue) { for (int i = 0; i < m_components.Count; i++) m_components[i].OnAabbChange(); } }
private static void SetupEffect(ref MyTransparentMaterial materialProperties, MyTransparentMaterial blendMaterialProperties, bool colorize, float colorizeSoftDist, bool near, bool ignoreDepth, ref Matrix projectionMatrix) { MyEffectTransparentGeometry effect = MyRender.GetEffect(MyEffects.TransparentGeometry) as MyEffectTransparentGeometry; effect.SetBillboardTexture(GetTexture(materialProperties)); effect.SetBillboardBlendTexture(GetTexture(blendMaterialProperties)); effect.SetSoftParticleDistanceScale(materialProperties.SoftParticleDistanceScale); effect.SetAlphaMultiplierAndSaturation(1, materialProperties.AlphaSaturation); if (near) { effect.SetProjectionMatrix(ref MyRenderCamera.ProjectionMatrixForNearObjects); Matrix invProjectionMatrix = Matrix.Invert(MyRenderCamera.ProjectionMatrixForNearObjects); effect.SetInverseDefaultProjectionMatrix(ref invProjectionMatrix); } else { effect.SetProjectionMatrix(ref projectionMatrix); Matrix invProjectionMatrix = Matrix.Invert((MyRenderCamera.m_backupMatrix.HasValue ? MyRenderCamera.m_backupMatrix.Value : MyRenderCamera.ProjectionMatrix)); effect.SetInverseDefaultProjectionMatrix(ref invProjectionMatrix); } if (MyRender.Settings.VisualizeOverdraw) { effect.SetTechnique(MyEffectTransparentGeometry.Technique.VisualizeOverdraw); } else { if (colorize) { effect.SetColorizeSoftDistance(colorizeSoftDist); effect.SetColorizeColor(ColorizeColor); effect.SetColorizePlane(ColorizePlaneNormal, ColorizePlaneDistance); effect.SetTechnique(MyEffectTransparentGeometry.Technique.ColorizeHeight); } else if (materialProperties.IgnoreDepth || ignoreDepth) { effect.SetTechnique(MyEffectTransparentGeometry.Technique.IgnoreDepth); } else if (materialProperties.CanBeAffectedByOtherLights) { effect.SetTechnique(MyEffectTransparentGeometry.Technique.Lit); } else if (materialProperties.Reflectivity > 0) { effect.SetTechnique(MyEffectTransparentGeometry.Technique.Reflection); } else { effect.SetTechnique(MyEffectTransparentGeometry.Technique.Unlit); } } }
internal void Add(MyActor child) { child.AddComponent(MyComponentFactory <MyGroupLeafComponent> .Create()); child.GetGroupLeaf().m_parent = m_owner; 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) { child.GetGroupLeaf().m_mergable = true; MyBigMeshTable.Table.AddMesh(model); m_mergablesCounter++; if (!m_isMerged && m_mergablesCounter >= MERGE_THRESHOLD) { TurnIntoMergeInstancing(); } else if (m_isMerged) { Merge(child); } //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; //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; }