public void LoadMaterials()
        {
            lock (this)
            {
                MyRender.GetRenderProfiler().StartProfilingBlock("MyModel::LoadData");


                MyRender.Log.WriteLine("MyModel.LoadData -> START", LoggingOptions.LOADING_MODELS);
                MyRender.Log.IncreaseIndent(LoggingOptions.LOADING_MODELS);

                MyRender.Log.WriteLine("m_assetName: " + m_assetName, LoggingOptions.LOADING_MODELS);

                //  Read data from model TAG parameter. There are stored vertex positions, triangle indices, vectors, ... everything we need.
                MyRender.GetRenderProfiler().StartProfilingBlock("Model - load data - import data");

                string assetForImport = AssetName;
                var fsPath = Path.IsPathRooted(AssetName) ? AssetName : Path.Combine(MyFileSystem.ContentPath, AssetName);
                if (!MyFileSystem.FileExists(fsPath))
                {
                    MyRender.Log.WriteLine("ERROR: Asset " + AssetName + "not exists!");
                    assetForImport = @"Models\Debug\Error.mwm";
                }

                MyRender.Log.WriteLine(String.Format("Importing asset {0}, path: {1}", assetForImport, AssetName), LoggingOptions.LOADING_MODELS);
                try
                {
                    m_importer.ImportData(assetForImport);
                }
                catch
                {
                    MyRender.Log.WriteLine(String.Format("Importing asset failed {0}", m_assetName));
                    throw;
                }
                MyRender.GetRenderProfiler().EndProfilingBlock();

                MyRender.GetRenderProfiler().StartProfilingBlock("Model - load data - load tag data");
                Dictionary<string, object> tagData = m_importer.GetTagData();
                if (tagData.Count == 0)
                {
                    throw new Exception(String.Format("Uncompleted tagData for asset: {0}, path: {1}", m_assetName, AssetName));
                }
                MyRender.GetRenderProfiler().EndProfilingBlock();

                MyRender.GetRenderProfiler().StartProfilingBlock("Model - load data - mesh");
                m_meshContainer.Clear();

                if (tagData.ContainsKey(MyImporterConstants.TAG_MESH_PARTS))
                {
                    List<int> indices = new List<int>(GetVerticesCount()); // Default capacity estimation

                    List<MyMeshPartInfo> meshParts = tagData[MyImporterConstants.TAG_MESH_PARTS] as List<MyMeshPartInfo>;
                    foreach (MyMeshPartInfo meshPart in meshParts)
                    {
                        if (meshPart.m_MaterialDesc != null)
                            MyRenderModels.Materials[meshPart.m_MaterialDesc.MaterialName] = meshPart.m_MaterialDesc;

                        MyRenderMesh mesh = new MyRenderMesh(meshPart, m_assetName);

                        mesh.IndexStart = indices.Count;
                        mesh.TriCount = meshPart.m_indices.Count / 3;

                        System.Diagnostics.Debug.Assert(mesh.TriCount > 0);

                        m_meshContainer.Add(mesh);
                    }
                }


                PreloadTextures(LoadingMode.Immediate);

                m_importer.Clear();

                MyRender.Log.DecreaseIndent(LoggingOptions.LOADING_MODELS);
                MyRender.Log.WriteLine("MyModel.LoadData -> END", LoggingOptions.LOADING_MODELS);

                MyRender.GetRenderProfiler().EndProfilingBlock();
            }
        }
        public override void GetRenderElementsForShadowmap(MyLodTypeEnum lodTypeEnum, List <MyRender.MyRenderElement> renderElements, List <MyRender.MyRenderElement> transparentRenderElements)
        {
            MyRenderModel model;

            int lodIndex = m_lods.Count > (int)lodTypeEnum ? (int)lodTypeEnum : 0;

            if (ShadowBoxLod && lodTypeEnum == MyLodTypeEnum.LOD1)
            {
                model = MyDebugDraw.ModelBoxLowRes;
            }
            else
            {
                model = m_lods[lodIndex].ShadowModel == null ? m_lods[lodIndex].Model : m_lods[lodIndex].ShadowModel;
            }

            if (model == null || model.LoadState != LoadState.Loaded)
            {
                return;
            }

            var drawMatrix = GetWorldMatrixForDraw();

            if (m_drawTechnique == MyMeshDrawTechnique.GLASS)
            {
                int meshCount = model.GetMeshList().Count;
                for (int i = 0; i < meshCount; i++)
                {
                    MyRenderMesh mesh = model.GetMeshList()[i];

                    MyRenderMeshMaterial material = mesh.Material;

                    if (!material.Enabled)
                    {
                        continue;
                    }

                    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     = EntityDithering;
                        renderElement.ColorMaskHSV  = EntityColorMaskHSV;


                        if (material.DrawTechnique == MyMeshDrawTechnique.GLASS)
                        {
                            renderElement.Dithering = mesh.GlassDithering;
                        }
                        else
                        {
                            renderElement.Dithering = 0;
                        }


                        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;
                            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);
                        }
                    }
                }
            }
            else
            {
                if (!MyRender.IsRenderOverloaded)
                {
                    int  meshCount      = model.GetMeshList().Count;
                    bool separateMeshes = false;
                    for (int i = 0; i < meshCount; i++)
                    {
                        MyRenderMesh mesh = model.GetMeshList()[i];
                        if (mesh.BonesUsed != null)
                        {
                            separateMeshes = true;
                            break;
                        }
                    }

                    if (!separateMeshes)
                    {
                        MyRender.MyRenderElement renderElement;
                        MyRender.AllocateRenderElement(out renderElement);

                        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.Dithering         = 0;
                        renderElement.BonesUsed         = null;

                        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;
                        }

                        renderElement.IndexStart = 0;
                        if (renderElement.IndexBuffer != null)
                        {
                            renderElement.TriCount = model.GetTrianglesCount();
                        }

                        //renderElement.DebugName = entity.Name;
                        renderElement.WorldMatrix        = WorldMatrix;
                        renderElement.WorldMatrixForDraw = drawMatrix;

                        renderElements.Add(renderElement);
                    }
                    else
                    {
                        for (int i = 0; i < meshCount; i++)
                        {
                            MyRenderMesh mesh = model.GetMeshList()[i];

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

                            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.Dithering         = 0;

                            renderElement.BonesUsed = mesh.BonesUsed;

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

                            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;
                                renderElement.DrawTechnique     = model.BoneIndices.Length > 0 ? MyMeshDrawTechnique.MESH_INSTANCED_SKINNED : MyMeshDrawTechnique.MESH_INSTANCED;
                            }

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

                            renderElements.Add(renderElement);
                        }
                    }
                }
            }
        }
        //  Sort of lazy-load, where constructor just saves information about what this model should be, but real load is done here - and only one time.
        //  This loads only vertex data, doesn't touch GPU
        //  Can be called from main and background thread
        public void LoadData()
        {
            if (m_loadedData) return;
            if (m_loadedContent) return;

            lock (this)
            {
                if (m_loadedData) return;
                if (m_loadedContent) return;

                MyRender.GetRenderProfiler().StartProfilingBlock("MyModel::LoadData");


                MyRender.Log.WriteLine("MyModel.LoadData -> START", LoggingOptions.LOADING_MODELS);
                MyRender.Log.IncreaseIndent(LoggingOptions.LOADING_MODELS);

                MyRender.Log.WriteLine("m_assetName: " + m_assetName, LoggingOptions.LOADING_MODELS);

                //  Read data from model TAG parameter. There are stored vertex positions, triangle indices, vectors, ... everything we need.
                MyRender.GetRenderProfiler().StartProfilingBlock("Model - load data - import data");

                string assetForImport = AssetName;
                var fsPath = Path.IsPathRooted(AssetName) ? AssetName : Path.Combine(MyFileSystem.ContentPath, AssetName);
                if (!MyFileSystem.FileExists(fsPath))
                {
                    MyRender.Log.WriteLine("ERROR: Asset " + AssetName + "not exists!");
                    assetForImport = @"Models\Debug\Error.mwm";
                }

                MyRender.Log.WriteLine(String.Format("Importing asset {0}, path: {1}", assetForImport, AssetName), LoggingOptions.LOADING_MODELS);
                try
                {
                    m_importer.ImportData(assetForImport);
                }
                catch
                {
                    MyRender.Log.WriteLine(String.Format("Importing asset failed {0}", m_assetName));
                    throw;
                }
                MyRender.GetRenderProfiler().EndProfilingBlock();

                MyRender.GetRenderProfiler().StartProfilingBlock("Model - load data - load tag data");
                Dictionary<string, object> tagData = m_importer.GetTagData();
                if (tagData.Count == 0)
                {
                    throw new Exception(String.Format("Uncompleted tagData for asset: {0}, path: {1}", m_assetName, AssetName));
                }
                MyRender.GetRenderProfiler().EndProfilingBlock();

                MyRender.GetRenderProfiler().StartProfilingBlock("Model - load data - vertex, normals, texture coords");

                object patternScale;
                if (tagData.TryGetValue(MyImporterConstants.TAG_PATTERN_SCALE, out patternScale))
                {
                    PatternScale = (float)patternScale;
                }

                HalfVector4[] vertices = (HalfVector4[])tagData[MyImporterConstants.TAG_VERTICES];

                //Dont assert, it can be animation
                //System.Diagnostics.Debug.Assert(vertices.Length > 0);

                Byte4[] normals = (Byte4[])tagData[MyImporterConstants.TAG_NORMALS];
                m_vertices = new MyCompressedVertexNormal[vertices.Length];
                if (normals.Length > 0)
                {
                    for (int v = 0; v < vertices.Length; v++)
                    {
                        m_vertices[v] = new MyCompressedVertexNormal()
                        {
                            Position = vertices[v],// VF_Packer.PackPosition(ref vertices[v]),
                            Normal = normals[v]//VF_Packer.PackNormalB4(ref normals[v])
                        };
                    }
                }
                else
                {
                    for (int v = 0; v < vertices.Length; v++)
                    {
                        m_vertices[v] = new MyCompressedVertexNormal()
                        {
                            Position = vertices[v],// VF_Packer.PackPosition(ref vertices[v]),
                        };
                    }
                }

                m_verticesCount = vertices.Length;

                HalfVector2[] forLoadingTexCoords0 = (HalfVector2[])tagData[MyImporterConstants.TAG_TEXCOORDS0];
                m_forLoadingTexCoords0 = new HalfVector2[forLoadingTexCoords0.Length];
                for (int t = 0; t < forLoadingTexCoords0.Length; t++)
                {
                    m_forLoadingTexCoords0[t] = forLoadingTexCoords0[t];// new HalfVector2(forLoadingTexCoords0[t]);
                    m_forLoadingTexCoords0[t] = new HalfVector2(m_forLoadingTexCoords0[t].ToVector2() / PatternScale);
                }

                MyRender.GetRenderProfiler().EndProfilingBlock();

                MyRender.GetRenderProfiler().StartProfilingBlock("Model - load data - mesh");
                m_meshContainer.Clear();

                if (tagData.ContainsKey(MyImporterConstants.TAG_MESH_PARTS))
                {
                    List<int> indices = new List<int>(GetVerticesCount()); // Default capacity estimation
                    int maxIndex = 0;

                    List<MyMeshPartInfo> meshParts = tagData[MyImporterConstants.TAG_MESH_PARTS] as List<MyMeshPartInfo>;
                    foreach (MyMeshPartInfo meshPart in meshParts)
                    {
                        if (meshPart.m_MaterialDesc != null)
                            MyRenderModels.Materials[meshPart.m_MaterialDesc.MaterialName] = meshPart.m_MaterialDesc;

                        MyRenderMesh mesh = new MyRenderMesh(meshPart, m_assetName);

                        mesh.IndexStart = indices.Count;
                        mesh.TriCount = meshPart.m_indices.Count / 3;

                        System.Diagnostics.Debug.Assert(mesh.TriCount > 0);

                        foreach (var i in meshPart.m_indices)
                        {
                            indices.Add(i);
                            if (i > maxIndex)
                            {
                                maxIndex = i;
                            }
                        }

                        m_meshContainer.Add(mesh);

                        if (meshPart.m_MaterialDesc != null && meshPart.Technique == MyMeshDrawTechnique.GLASS)
                        {
                            float minimumGlassShadow = 0.0f;

                            if (string.IsNullOrEmpty(meshPart.m_MaterialDesc.GlassCW))
                                continue;

                            if (string.IsNullOrEmpty(meshPart.m_MaterialDesc.GlassCCW))
                                continue;

                            var materialCW = MyTransparentMaterials.GetMaterial(meshPart.m_MaterialDesc.GlassCW);
                            var materialCCW = MyTransparentMaterials.GetMaterial(meshPart.m_MaterialDesc.GlassCCW);

                            mesh.GlassDithering = System.Math.Max(materialCW.Color.W, minimumGlassShadow);

                            MyRenderMesh glassMesh = new MyRenderMesh(meshPart, m_assetName);
                            glassMesh.GlassDithering = System.Math.Max(materialCCW.Color.W, minimumGlassShadow);


                            glassMesh.IndexStart = indices.Count;
                            glassMesh.TriCount = meshPart.m_indices.Count / 3;

                            System.Diagnostics.Debug.Assert(glassMesh.TriCount > 0);

                            for (int i = 0; i < meshPart.m_indices.Count; i += 3)
                            {
                                indices.Add(meshPart.m_indices[i + 0]);
                                indices.Add(meshPart.m_indices[i + 2]);
                                indices.Add(meshPart.m_indices[i + 1]);
                            }

                            m_meshContainer.Add(glassMesh);
                        }
                    }

                    if (maxIndex <= ushort.MaxValue)
                    {
                        // create 16 bit indices
                        m_Indices_16bit = new ushort[indices.Count];
                        for (int i = 0; i < indices.Count; i++)
                        {
                            m_Indices_16bit[i] = (ushort)indices[i];
                        }
                    }
                    else
                    {
                        // use 32bit indices
                        m_Indices = indices.ToArray();
                    }

                    m_trianglesCount = indices.Count / 3;
                }


                MyRender.GetRenderProfiler().EndProfilingBlock();

                MyRender.GetRenderProfiler().StartProfilingBlock("Model - load data - other data");
                if (MyRenderConstants.RenderQualityProfile.UseNormals && m_forLoadingTexCoords0.Length > 0)
                {
                    var verticesNum = vertices.Length;
                    
                    Byte4[] forLoadingTangents = (Byte4[])tagData[MyImporterConstants.TAG_TANGENTS];
                    Byte4[] forLoadingBitangents = (Byte4[])tagData[MyImporterConstants.TAG_BINORMALS];
                    m_forLoadingTangents = new Byte4[forLoadingTangents.Length];

                    for (int v = 0; v < forLoadingTangents.Length; v++)
                    {
                        var N = VF_Packer.UnpackNormal(m_vertices[v].Normal.PackedValue);
                        var T = VF_Packer.UnpackNormal(forLoadingTangents[v].PackedValue);
                        var B = VF_Packer.UnpackNormal(forLoadingBitangents[v].PackedValue);

                        var tangentSign = new Vector4(T.X, T.Y, T.Z, 0);
                        tangentSign.W = T.Cross(N).Dot(B) < 0 ? -1 : 1;

                        m_forLoadingTangents[v] = VF_Packer.PackTangentSignB4(ref tangentSign);
                    }
                }

                m_specularShininess = (float)tagData[MyImporterConstants.TAG_SPECULAR_SHININESS];
                m_specularPower = (float)tagData[MyImporterConstants.TAG_SPECULAR_POWER];
                m_rescaleFactor = (float)tagData[MyImporterConstants.TAG_RESCALE_FACTOR];

                BoneIndices = (Vector4I[])tagData[MyImporterConstants.TAG_BLENDINDICES];
                BoneWeights = (Vector4[])tagData[MyImporterConstants.TAG_BLENDWEIGHTS];

                Animations = (ModelAnimations)tagData[MyImporterConstants.TAG_ANIMATIONS];
                Bones = (MyModelBone[])tagData[MyImporterConstants.TAG_BONES];
                
                if(BoneIndices.Length > 0 && Bones.Length > MyRenderConstants.MAX_SHADER_BONES)
                {
                    List<MyMeshPartInfo> meshParts = tagData[MyImporterConstants.TAG_MESH_PARTS] as List<MyMeshPartInfo>;

                    Dictionary<int, int> vertexChanged = new Dictionary<int,int>();
                    for(int p=0; p<meshParts.Count; p++)
                    {
                        var meshPart = meshParts[p];

                        Dictionary<int, int> bonesUsed = new Dictionary<int, int>();

                        int trianglesNum = meshPart.m_indices.Count / 3;
                        for (int i = 0; i < trianglesNum; i++)
                        {
                            for (int j = 0; j < 3; j++)
                            {
                                int index = meshPart.m_indices[i * 3 + j];
                                if(BoneWeights[index].X > 0)
                                    bonesUsed[BoneIndices[index].X] = 1;
                                if (BoneWeights[index].Y > 0)
                                    bonesUsed[BoneIndices[index].Y] = 1;
                                if (BoneWeights[index].Z > 0)
                                    bonesUsed[BoneIndices[index].Z] = 1;
                                if (BoneWeights[index].W > 0)
                                    bonesUsed[BoneIndices[index].W] = 1;
                            }
                        }

                        var partBones = new List<int>(bonesUsed.Keys);
                        partBones.Sort();
                        if (partBones.Count > 0 && partBones[partBones.Count - 1] >= MyRenderConstants.MAX_SHADER_BONES)
                        {
                            for(int i=0; i<partBones.Count; i++)
                            {
                                bonesUsed[partBones[i]] = i;
                            }

                            Dictionary<int, int> vertexTouched = new Dictionary<int, int>();

                            for (int i = 0; i < trianglesNum; i++)
                            {
                                for (int j = 0; j < 3; j++)
                                {
                                    int index = meshPart.m_indices[i * 3 + j];
                                    if(!vertexTouched.ContainsKey(index))
                                    { 
                                        if (BoneWeights[index].X > 0)
                                            BoneIndices[index].X = bonesUsed[BoneIndices[index].X];
                                        if (BoneWeights[index].Y > 0)
                                            BoneIndices[index].Y = bonesUsed[BoneIndices[index].Y];
                                        if (BoneWeights[index].Z > 0)
                                            BoneIndices[index].Z = bonesUsed[BoneIndices[index].Z];
                                        if (BoneWeights[index].W > 0)
                                            BoneIndices[index].W = bonesUsed[BoneIndices[index].W];

                                        vertexTouched[index] = 1;

                                        int changes = 0;
                                        vertexChanged.TryGetValue(index, out changes);
                                        vertexChanged[index] = changes + 1;
                                    }
                                }
                            }

                            m_meshContainer[p].BonesUsed = partBones.ToArray();
                        }
                    }

                    if (vertexChanged.Values.Count > 0)
                        Debug.Assert(vertexChanged.Values.Max() < 2, "Vertex shared between model parts, will likely result in wrong skinning");
                }

                BoundingBox = (BoundingBox)tagData[MyImporterConstants.TAG_BOUNDING_BOX];
                BoundingSphere = (BoundingSphere)tagData[MyImporterConstants.TAG_BOUNDING_SPHERE];
                BoundingBoxSize = BoundingBox.Max - BoundingBox.Min;
                BoundingBoxSizeHalf = BoundingBoxSize / 2.0f;
                Dictionary<string, MyModelDummy> Dummies = tagData[MyImporterConstants.TAG_DUMMIES] as Dictionary<string, MyModelDummy>;
                MyRender.GetRenderProfiler().EndProfilingBlock();

                if (tagData.ContainsKey(MyImporterConstants.TAG_LODS))
                {
                    var tagLODs = tagData[MyImporterConstants.TAG_LODS];

                    LODs.Clear();
                    LODs.AddArray((MyLODDescriptor[])tagLODs);

                    foreach (var lodDesc in LODs)
                    {
                        if (!string.IsNullOrEmpty(lodDesc.RenderQuality))
                        {
                            lodDesc.RenderQualityList = new List<int>();
                            string[] qualityStrings = lodDesc.RenderQuality.ToUpper().Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
                            foreach (string qs in qualityStrings)
                            {
                                string qs2 = qs.Trim();
                                if (qs2 == "LOW")
                                    lodDesc.RenderQualityList.Add((int)MyRenderQualityEnum.LOW);
                                else
                                    if (qs2 == "NORMAL")
                                        lodDesc.RenderQualityList.Add((int)MyRenderQualityEnum.NORMAL);
                                    else
                                        if (qs2 == "HIGH")
                                            lodDesc.RenderQualityList.Add((int)MyRenderQualityEnum.HIGH);
                                        else
                                            if (qs2 == "EXTREME")
                                                lodDesc.RenderQualityList.Add((int)MyRenderQualityEnum.EXTREME);
                            }
                        }
                    }
                }


                MyRender.Log.WriteLine("Vertexes.Length: " + GetVerticesCount(), LoggingOptions.LOADING_MODELS);
                MyRender.Log.WriteLine("Centered: " + (bool)tagData[MyImporterConstants.TAG_CENTERED], LoggingOptions.LOADING_MODELS);
                MyRender.Log.WriteLine("UseChannelTextures: " + (bool)tagData[MyImporterConstants.TAG_USE_CHANNEL_TEXTURES], LoggingOptions.LOADING_MODELS);
                MyRender.Log.WriteLine("Length in meters: " + (float)tagData[MyImporterConstants.TAG_LENGTH_IN_METERS], LoggingOptions.LOADING_MODELS);
                MyRender.Log.WriteLine("Rescale to length in meters?: " + (bool)tagData[MyImporterConstants.TAG_RESCALE_TO_LENGTH_IN_METERS], LoggingOptions.LOADING_MODELS);
                MyRender.Log.WriteLine("SpecularShininess: " + m_specularShininess, LoggingOptions.LOADING_MODELS);
                MyRender.Log.WriteLine("SpecularPower: " + m_specularPower, LoggingOptions.LOADING_MODELS);
                MyRender.Log.WriteLine("RescaleFactor: " + m_rescaleFactor, LoggingOptions.LOADING_MODELS);
                MyRender.Log.WriteLine("BoundingBox: " + BoundingBox, LoggingOptions.LOADING_MODELS);
                MyRender.Log.WriteLine("BoundingSphere: " + BoundingSphere, LoggingOptions.LOADING_MODELS);

                MyPerformanceCounter.PerAppLifetime.MyModelsCount++;
                MyPerformanceCounter.PerAppLifetime.MyModelsMeshesCount += m_meshContainer.Count;
                MyPerformanceCounter.PerAppLifetime.MyModelsVertexesCount += GetVerticesCount();
                MyPerformanceCounter.PerAppLifetime.MyModelsTrianglesCount += m_trianglesCount;

                ModelInfo = new MyModelInfo(GetTrianglesCount(), GetVerticesCount(), BoundingBoxSize);

                m_loadedData = true;

                m_importer.Clear();

                MyRender.Log.DecreaseIndent(LoggingOptions.LOADING_MODELS);
                MyRender.Log.WriteLine("MyModel.LoadData -> END", LoggingOptions.LOADING_MODELS);

                MyRender.GetRenderProfiler().EndProfilingBlock();
            }
        }
        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);
                    }
                }
            }
        }