private void CreateCopyMeshes(MyRenderModel m)
 {
     m_meshes = m.GetMeshList().Select(s => new MyRenderGroup()
     {
         IndexStart = s.IndexStart, TriangleCount = s.TriCount, Material = s.Material
     }).ToList();
 }
Example #2
0
        SharpDX.Direct3D9.VertexDeclaration CreateInstanceDeclaration(MyRenderModel model)
        {
            var modelElements = model.GetVertexDeclaration().Elements;
            List <SharpDX.Direct3D9.VertexElement> elements = new List <SharpDX.Direct3D9.VertexElement>(modelElements.Take(modelElements.Length - 1)); // Remove decl end

            elements.AddList(m_instanceBuffer.VertexElements);
            elements.Add(SharpDX.Direct3D9.VertexElement.VertexDeclarationEnd);
            return(new SharpDX.Direct3D9.VertexDeclaration(MyRender.GraphicsDevice, elements.ToArray()));
        }
 public static void AddRuntimeModel(string name, MyRenderModel model)
 {
     if (m_modelsByAssetName.ContainsKey(name))
     {
         //System.Diagnostics.Debug.Fail(name + " runtime model is already added");
         return;
     }
     m_modelsByAssetName.Add(name, model);
 }
 internal static void LoadModelInDrawInBackground(MyRenderModel model)
 {          /*
             * if (MyFakes.LOAD_MODELS_IMMEDIATELY)
             * {
             * model.LoadInDraw(Managers.LoadingMode.Immediate);
             * }
             * else */
     {
         m_loadingQueue.Enqueue(model);
         m_loadModelEvent.Set();
     }
 }
        public static MyRenderModel GetMaterials(string assetName)
        {
            MyRenderModel model;

            m_modelsByAssetName.TryGetValue(assetName, out model);

            if (model == null)
            {
                model = new MyRenderModel(assetName, MyMeshDrawTechnique.MESH);
            }


            model.LoadMaterials();

            return(model);
        }
        /*
         * //  Lazy-loading and then returning reference to model
         * //  Doesn't load vertex/index shader and doesn't touch GPU. Use it when you need model data - vertex, triangles, octre...
         * public static MyModel GetModelOnlyData(MyModelsEnum modelEnum)
         * {
         * int modelInt = (int)modelEnum;
         * m_models[modelInt].LoadData();
         * return m_models[modelInt];
         * }
         *
         * //  Lazy-loading and then returning reference to model
         * //  Doesn't load vertex/index shader and doesn't touch GPU. Use it when you need model data - vertex, triangles, octre...
         * public static MyModel GetModelOnlyDummies(MyModelsEnum modelEnum)
         * {
         * int modelInt = (int)modelEnum;
         * m_models[modelInt].LoadOnlyDummies();
         * return m_models[modelInt];
         * }
         *
         * public static MyModel GetModelOnlyModelInfo(MyModelsEnum modelEnum)
         * {
         * int modelInt = (int)modelEnum;
         * m_models[modelInt].LoadOnlyModelInfo();
         * return m_models[modelInt];
         * }  */

        public static MyRenderModel GetModel(string assetName)
        {
            MyRenderModel model;

            m_modelsByAssetName.TryGetValue(assetName, out model);

            if (model == null)
            {
                model = new MyRenderModel(assetName, MyMeshDrawTechnique.MESH);
                m_modelsByAssetName.Add(assetName, model);
            }


            model.LoadData();
            model.LoadContent();

            return(model);
        }
Example #7
0
        public override void GetRenderElements(MyLodTypeEnum lodTypeEnum, List <MyRender.MyRenderElement> elements, List <MyRender.MyRenderElement> transparentElements)
        {
            if (MyRender.CurrentRenderSetup.CallerID.HasValue && MyRender.CurrentRenderSetup.CallerID.Value == MyRenderCallerEnum.EnvironmentMap && this.EntityDithering > 0)
            {
                return;
            }

            Debug.Assert(m_isDataSet, "Data is not set, have you forgotten to send SetRenderEntityData message?");

            MyRenderModel currentModel = m_lods[0].Model;
            List <MyRenderMeshMaterial> currentMaterials = m_lods[0].MeshMaterials;

            var volume   = WorldVolume;
            var lodIndex = 0;

            if (lodTypeEnum == MyLodTypeEnum.LOD_NEAR)
            {
            }
            else
            if (lodTypeEnum == MyLodTypeEnum.LOD0)
            {
                if (m_lods.Count > 1)
                {
                    var distance = MyUtils.GetSmallestDistanceToSphereAlwaysPositive(ref MyRenderCamera.Position, ref volume);

                    if (distance > MyRenderCamera.FAR_PLANE_DISTANCE)
                    {
                        return;
                    }

                    for (int i = 1; i < m_lods.Count; i++)
                    {
                        var lod = m_lods[i];
                        if (distance < lod.Distance)
                        {
                            break;
                        }

                        currentModel     = lod.Model;
                        currentMaterials = lod.MeshMaterials;
                        lodIndex         = i;
                    }
                }
            }
            else
            {
                return;     //nothing to render in LOD1
            }
            if (currentModel == null)
            {
                return;
            }

            int instanceCount = m_instanceBuffer != null ? m_instanceCount : 1;

            MyRender.ModelTrianglesCountStats += currentModel.GetTrianglesCount() * instanceCount;

            MyPerformanceCounter.PerCameraDrawWrite.EntitiesRendered++;

            CollectRenderElements(elements, transparentElements, currentModel, currentMaterials, lodIndex);
        }
Example #8
0
        internal void CollectRenderElements(List <VRageRender.MyRender.MyRenderElement> renderElements, List <VRageRender.MyRender.MyRenderElement> transparentRenderElements, MyRenderModel model, List <MyRenderMeshMaterial> materials, int lodIndex)
        {
            if (model.LoadState == LoadState.Unloaded)
            {
                //model.LoadInDraw(LoadingMode.Background);
                model.LoadInDraw(LoadingMode.Immediate);
                return;
            }
            if (model.LoadState == LoadState.Loading)
            {
                return;
            }

            if (m_instanceBuffer != null && m_instanceCount == 0)
            {
                return;
            }

            var drawMatrix = GetWorldMatrixForDraw();

            int meshCount = model.GetMeshList().Count;

            for (int i = 0; i < meshCount; i++)
            {
                MyRenderMesh mesh = model.GetMeshList()[i];

                MyRenderMeshMaterial material = model.HasSharedMaterials ? mesh.Material : materials[i];

                if (!material.Enabled)
                {
                    continue;
                }

                if (material.DrawTechnique == MyMeshDrawTechnique.GLASS && EntityDithering == 0)
                {
                    m_drawTechnique = MyMeshDrawTechnique.GLASS;
                    continue;
                }

                //Preload needs to be here because of reloadcontent
                material.PreloadTexture(LoadingMode.Background);

                VRageRender.MyRender.MyRenderElement renderElement;
                VRageRender.MyRender.AllocateRenderElement(out renderElement);

                if (!MyRender.IsRenderOverloaded)
                {
                    //renderElement.DebugName = entity.Name;
                    renderElement.RenderObject = this;

                    renderElement.VertexBuffer      = model.VertexBuffer;
                    renderElement.IndexBuffer       = model.IndexBuffer;
                    renderElement.VertexCount       = model.GetVerticesCount();
                    renderElement.VertexDeclaration = model.GetVertexDeclaration();
                    renderElement.VertexStride      = model.GetVertexStride();
                    renderElement.InstanceBuffer    = null;
                    renderElement.BonesUsed         = mesh.BonesUsed;

                    renderElement.IndexStart = mesh.IndexStart;
                    renderElement.TriCount   = mesh.TriCount;

                    renderElement.WorldMatrixForDraw = drawMatrix;
                    renderElement.WorldMatrix        = WorldMatrix;

                    renderElement.Material      = material;
                    renderElement.DrawTechnique = m_drawTechnique == MyMeshDrawTechnique.MESH || m_drawTechnique == MyMeshDrawTechnique.GLASS ? material.DrawTechnique : m_drawTechnique;
                    renderElement.Color         = EntityColor * material.DiffuseColor;
                    renderElement.Dithering     = mesh.GlassDithering == 0 ? EntityDithering : mesh.GlassDithering;
                    renderElement.ColorMaskHSV  = EntityColorMaskHSV;

                    if (m_instanceBuffer != null)
                    {
                        renderElement.VertexStride      = m_lods[lodIndex].VertexStride;
                        renderElement.VertexDeclaration = m_lods[lodIndex].VertexDeclaration;
                        renderElement.InstanceBuffer    = m_instanceBuffer.InstanceBuffer;
                        renderElement.InstanceStart     = m_instanceStart;
                        renderElement.InstanceCount     = m_instanceCount;
                        renderElement.InstanceStride    = m_instanceBuffer.Stride;

                        if (m_instanceBuffer.Type == MyRenderInstanceBufferType.Generic)
                        {
                            renderElement.DrawTechnique = renderElement.DrawTechnique == MyMeshDrawTechnique.ALPHA_MASKED ? MyMeshDrawTechnique.MESH_INSTANCED_GENERIC_MASKED : MyMeshDrawTechnique.MESH_INSTANCED_GENERIC;
                        }
                        else
                        {
                            renderElement.DrawTechnique = model.BoneIndices.Length > 0 ? MyMeshDrawTechnique.MESH_INSTANCED_SKINNED : MyMeshDrawTechnique.MESH_INSTANCED;
                        }
                    }

                    Debug.Assert(renderElement.VertexBuffer != null, "Vertex buffer cannot be null!");
                    Debug.Assert(renderElement.IndexBuffer != null, "Index buffer cannot be null!");

                    if (material.DrawTechnique == MyMeshDrawTechnique.HOLO)
                    {
                        if (transparentRenderElements != null)
                        {
                            transparentRenderElements.Add(renderElement);
                        }
                    }
                    else
                    {
                        renderElements.Add(renderElement);
                    }
                }
            }
        }
Example #9
0
        public static void Draw()
        {
            if (!Visible)
            {
                return;
            }

            MyRenderModel model = MyRenderModels.GetModel(Model);//MySession.Static.PlayerShip.CockpitGlassModelEnum);

            RasterizerState.CullNone.Apply();

            MyStateObjects.DepthStencil_StencilReadOnly.Apply();

            BlendState.NonPremultiplied.Apply();

            MyEffectCockpitGlass effect = (MyEffectCockpitGlass)MyRender.GetEffect(MyEffects.CockpitGlass);

            effect.SetGlassDirtLevelAlpha(new Vector4(GlassDirtAlpha, 0, 0, 0));

            var     inMatrix = PlayerHeadForCockpitInteriorWorldMatrix;
            MatrixD drawMatrix;

            MatrixD.Multiply(ref inMatrix, ref MyRenderCamera.InversePositionTranslationMatrix, out drawMatrix);

            effect.SetWorldMatrix((Matrix)drawMatrix);
            effect.SetViewMatrix((Matrix)MyRenderCamera.ViewMatrix);

            Matrix projection = MyRenderCamera.ProjectionMatrixForNearObjects;

            effect.SetWorldViewProjectionMatrix((Matrix)(drawMatrix * MyRenderCamera.ViewMatrixAtZero * projection));

            MyRenderMeshMaterial cockpitMaterial = model.GetMeshList()[0].Material;

            cockpitMaterial.PreloadTexture();
            effect.SetCockpitGlassTexture(cockpitMaterial.DiffuseTexture);

            Texture depthRT = MyRender.GetRenderTarget(MyRenderTargets.Depth);

            effect.SetDepthTexture(depthRT);

            effect.SetHalfPixel(MyUtilsRender9.GetHalfPixel(depthRT.GetLevelDescription(0).Width, depthRT.GetLevelDescription(0).Height));

            Vector4 sunColor = MyRender.Sun.Color;

            effect.SetSunColor(new Vector3(sunColor.X, sunColor.Y, sunColor.Z));

            effect.SetDirectionToSun(-MyRender.Sun.Direction);

            effect.SetAmbientColor(Vector3.Zero);
            effect.SetReflectorPosition((Vector3)((Vector3)MyRenderCamera.Position - 4 * MyRenderCamera.ForwardVector));

            if (MyRender.RenderLightsForDraw.Count > 0)
            {
                effect.SetNearLightColor(MyRender.RenderLightsForDraw[0].Color);
                effect.SetNearLightRange(MyRender.RenderLightsForDraw[0].Range);
            }

            MyRender.GetShadowRenderer().SetupShadowBaseEffect(effect);
            effect.SetShadowBias(0.001f);

            MyLights.UpdateEffect(effect, true);

            effect.Begin();
            model.Render();
            effect.End();

            //MyDebugDraw.DrawSphereWireframe(PlayerHeadForCockpitInteriorWorldMatrix, Vector3.One, 1);
        }