IPhysicsMesh CreatePhysicsMesh(MyModel model)
        {
            IPhysicsMesh physicsMesh = new MyPhysicsMesh();

            physicsMesh.SetAABB(model.BoundingBox.Min, model.BoundingBox.Max);


            for (int v = 0; v < model.GetVerticesCount(); v++)
            {
                Vector3 vertex = model.GetVertex(v);
                Vector3 normal = model.GetVertexNormal(v);
                Vector3 tangent = model.GetVertexTangent(v);
                if (model.TexCoords == null)
                    model.LoadTexCoordData();
                Vector2 texCoord = model.TexCoords[v].ToVector2();

                physicsMesh.AddVertex(vertex, normal, tangent, texCoord);
            }

            for (int i = 0; i < model.Indices16.Length; i++)
            {
                physicsMesh.AddIndex(model.Indices16[i]);
            }

            for (int i = 0; i < model.GetMeshList().Count; i++)
            {
                var mesh = model.GetMeshList()[i];
                physicsMesh.AddSectionData(mesh.IndexStart, mesh.TriCount, mesh.Material.Name);
            }

            return physicsMesh;
        }
        public override void RecreateControls(bool constructor)
        {
            base.RecreateControls(constructor);

            m_currentPosition = -m_size.Value / 2.0f + new Vector2(0.02f, 0.10f);

            m_currentPosition.Y += 0.01f;

            
            m_scale = 0.7f;

            AddCaption("Render Model FX", Color.Yellow.ToVector4());
            AddShareFocusHint();

            //if (MySession.ControlledObject == null)
                //return;

            AddButton(new StringBuilder("Reload textures"), delegate { VRageRender.MyRenderProxy.ReloadTextures(); });


            //Line line = new Line(MySector.MainCamera.Position, MySector.MainCamera.Position + MySector.MainCamera.ForwardVector * 10);
            //var res = MyEntities.GetIntersectionWithLine(ref line, null, null);
            //if (!res.HasValue)
            //    return;

            ////MyModel model = MySession.ControlledObject.ModelLod0;
            //m_model = res.Value.Entity.ModelLod0;

            m_modelsCombo = AddCombo();
            var modelList = MyModels.LoadedModels.Values.ToList();

            if (modelList.Count == 0)
                return;

            for (int i = 0; i < modelList.Count; i++)
            {
                var model = modelList[i];
                m_modelsCombo.AddItem((int)i, new StringBuilder(System.IO.Path.GetFileNameWithoutExtension(model.AssetName)));
            }

            m_modelsCombo.SelectItemByIndex(m_currentModelSelectedItem);
            m_modelsCombo.ItemSelected += new MyGuiControlCombobox.ItemSelectedDelegate(modelsCombo_OnSelect);

            m_model = modelList[m_currentModelSelectedItem];

            if (m_model == null)
                return;

            m_meshesCombo = AddCombo();
            for (int i = 0; i < m_model.GetMeshList().Count; i++)
            {
                var mesh = m_model.GetMeshList()[i];
                m_meshesCombo.AddItem((int)i, new StringBuilder(mesh.Material.Name));
            }
            m_meshesCombo.SelectItemByIndex(m_currentSelectedMeshItem);
            m_meshesCombo.ItemSelected += new MyGuiControlCombobox.ItemSelectedDelegate(meshesCombo_OnSelect);

            if (MySector.MainCamera != null)
            {
                m_voxelsCombo = AddCombo();
                m_voxelsCombo.AddItem(-1, new StringBuilder("None"));
                int i = 0;
                foreach (var voxelMaterial in MyDefinitionManager.Static.GetVoxelMaterialDefinitions())
                {
                    m_voxelsCombo.AddItem(i++, new StringBuilder(voxelMaterial.Id.SubtypeName));
                }
                m_voxelsCombo.SelectItemByIndex(m_currentSelectedVoxelItem + 1);
                m_voxelsCombo.ItemSelected += new MyGuiControlCombobox.ItemSelectedDelegate(voxelsCombo_OnSelect);
            }

            var selectedMesh = m_model.GetMeshList()[m_currentSelectedMeshItem];
            var selectedMaterial = selectedMesh.Material;
            m_diffuseColor = AddColor(new StringBuilder("Diffuse"), selectedMaterial, MemberHelper.GetMember(() => selectedMaterial.DiffuseColor));

            m_specularIntensity = AddSlider("Specular intensity", selectedMaterial.SpecularIntensity, 0, 32, null);

            m_specularPower = AddSlider("Specular power", selectedMaterial.SpecularPower, 0, 128, null);
        }