Dynamic load model

Aug 24, 2011 at 6:07 AM

I am very grateful for your help! Once again to ask!


v1.0.2 can dynamically load multiple models,as the following code !

I like to do this in v1.0.4.    I want to know if  v1.0.4 can be achieved and how to do? What code needs to be adjusted!

 

       

List<Model> listmodel; 

List<string> addmodels = new List<string>();     

StatesManager statesManager;



private void Button7_Click(object sender, RoutedEventArgs e)        {            addmodels.Add("2");                    }



	string[] modelstr = { "1", "3"};
        int modelindex = 0;
        int addmodelindex = 0;
        List<string> addmodels = new List<string>();
        private void drawingSurface_Draw(object sender, DrawEventArgs e)
        {            
            Dispatcher.BeginInvoke(() => {
                if (listmodel.Count != 0 && modelindex < modelstr.Length )
                {
                    InitModel(e.GraphicsDevice, modelstr[modelindex++]);
                }
                if (addmodels.Count > 0 && addmodelindex<addmodels.Count)
                {
                    InitModel(e.GraphicsDevice, addmodels[addmodelindex++]);
                }
            });      
      
            if (listmodel.Count == 0)
            {
                Dispatcher.BeginInvoke(() => { InitModel(e.GraphicsDevice, "dimian"); });
                statesManager = new StatesManager(e.GraphicsDevice);
            }
            if (listmodel.Count == 0)
                return;

            DateTime start = DateTime.Now;

            // States
            statesManager.DepthBufferEnable = true;
            statesManager.CullMode = CullMode.None;
            statesManager.ApplyDepthStencilState();
            statesManager.ApplyRasterizerState();
            e.GraphicsDevice.BlendState = BlendState.AlphaBlend;

            // Draw
            e.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, new Color(0, 0, 0, 0), 1.0f, 0);

            for (int i = 0; i < listmodel.Count; i++)
            {
                foreach (ModelMesh mesh in listmodel[i].Meshes)
                {
                    
                    foreach (BasicEffect basicEffect in mesh.Effects)
                    {
                        basicEffect.SceneAmbientColor = new Color(0.3f, 0.3f, 0.3f, 0);
                        basicEffect.World = Matrix.Identity;
                        basicEffect.View = camera.View;
                        basicEffect.Projection = camera.Projection;
                        basicEffect.LightPosition = camera.Position + new Vector3(1, 5, 3);
                        basicEffect.CameraPosition = camera.Position;
                    }
                }

                listmodel[i].Draw();
            }
            lock (moves)
            {
                foreach (MoveDirection move in moves)
                {
                    camera.ComputeMove(move);
                }
            }

            camera.ApplyInertia();

            e.InvalidateSurface();
        }

        private void InitModel(GraphicsDevice device,string modelname)
        {
            Stream objStream = Application.GetResourceStream(new Uri("/Plot3D;component/M/"+modelname+".obj", UriKind.Relative)).Stream;
            ObjImporter importer = new ObjImporter();

            importer.OnImportCompleted += m =>
            {
                waitBar.Visibility = Visibility.Collapsed;
                model = m;
                listmodel.Add(m);
                drawingSurface.Invalidate();
            };

           
            importer.OnImportProgressChanged += p =>
            {
                waitProgressBar.Value = p;
            };

            waitBar.Visibility = Visibility.Visible;

            importer.ImportAsync(objStream, GetResourceStream, device, ImportationOptions.Optimize);
        }

        Stream GetResourceStream(string name)
        {
            try
            {
                return Application.GetResourceStream(new Uri(string.Format("/Plot3D;component/M/{0}", name), UriKind.Relative)).Stream;
            }
            catch
            {
                Dispatcher.BeginInvoke(() => MessageBox.Show("Unable to find " + name, "Error", MessageBoxButton.OK));
                return null;
            }
        }

 

 

Aug 24, 2011 at 6:20 AM

I'm Very sorry

After submitted a request to you for help, I suddenly understand what went wrong!

 
Thanks again!

 

Babylontoolkit  is powerful!

Jul 30, 2012 at 5:12 AM
Edited Jul 30, 2012 at 5:13 AM

can i get your full solution? i don't know how to import any 3d type model!!! can u help me?