none
Problem in Importing multiple 3d models by babylon tool kit

    General discussion

  • Hi,

    We are using Babylon Toolkit importer to import 3D model in our application. We are successful in importing a single 3D model, but while trying to import more than one 3d models only one model is visible .It is observed that the model which is first imported gets visible, rest models don't get shown on the surface. For each of the model we have used separate drawing surface, and put them in separate row in a grid. We want all the 3D models to be visible.

    In the attached project we have used 2 buttons to import 3d models. If buton1 is clicked first then its model is only shown, even if we click button 2 later on. Vice versa also happens if we click button 2.

    MainPage.XAML 
    <Grid x:Name="LayoutRoot" Background="White" > <Grid.RowDefinitions> <RowDefinition x:Name="row1" Height="25"></RowDefinition> <RowDefinition x:Name="row2"></RowDefinition> <RowDefinition x:Name="row3"></RowDefinition> </Grid.RowDefinitions> <Button Content="Button1" Grid.Row="0" Height="23" HorizontalAlignment="Left" Margin="0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> <Button Content="Button2" Grid.Row="0" Height="23" HorizontalAlignment="Left" Margin="80,0,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" /> </Grid>

    Mainpage.xaml.cs
    public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { Importer3D importer = new Importer3D(); DrawingSurface drs1 = importer.Get3DObject(); drs1.SetValue(Grid.RowProperty, 1); drs1.SetValue(Grid.ColumnProperty, 0); LayoutRoot.Children.Add(drs1); } private void button2_Click(object sender, RoutedEventArgs e) { Importer3D importer = new Importer3D(); DrawingSurface drs2 = importer.Get3DObject(); drs2.SetValue(Grid.RowProperty, 2); drs2.SetValue(Grid.ColumnProperty, 0); LayoutRoot.Children.Add(drs2); } }

    Importer3D.cs
    public partial class Importer3D
        {
            Model model;
            bool mouseLeftDown;
            System.Windows.Point startPosition;
            readonly OrbitCamera camera = new OrbitCamera { Alpha = -0.4f };
            StatesManager statesManager;
            ModelMesh selectedMesh;
         
            ObjImporter importer;
            DrawingSurface drawingSurface;
           
          
            public enum MouseMode
            {
                ArcRotate,
                ObjectPicking
            }
            public DrawingSurface Get3DObject()
            {
    
                return drawingSurface;
            }
      
    
            public Importer3D()
            {
               
              
                importer = new ObjImporter(SynchronizationContext.Current);
                drawingSurface = new DrawingSurface();
                drawingSurface.Draw += new EventHandler<DrawEventArgs>(DrawingSurface_Draw);
                drawingSurface.SizeChanged+=new SizeChangedEventHandler(drawingSurface_SizeChanged);
                drawingSurface.MouseMove+=new System.Windows.Input.MouseEventHandler(drawingSurface_MouseMove);
                drawingSurface.MouseLeftButtonDown+=new System.Windows.Input.MouseButtonEventHandler(drawingSurface_MouseLeftButtonDown);
                drawingSurface.MouseRightButtonDown+=new System.Windows.Input.MouseButtonEventHandler(drawingSurface_MouseRightButtonDown);
                drawingSurface.MouseWheel+=new System.Windows.Input.MouseWheelEventHandler(drawingSurface_MouseWheel);
                ComputeAspectRatio();
            }
    
            private void DrawingSurface_Draw(object sender, DrawEventArgs e)
            {
                GraphicsDevice device = GraphicsDeviceManager.Current.GraphicsDevice;
                if (model == null)
                {
                    Init(device);
    
                    statesManager = new StatesManager(device);
                }
    
                if (model == null)
                    return;
    
                DateTime start = DateTime.Now;
    
                // States
                statesManager.DepthBufferEnable = true;
                statesManager.CullMode = CullMode.None;
                statesManager.ApplyDepthStencilState();
                statesManager.ApplyRasterizerState();
                device.BlendState = BlendState.AlphaBlend;
    
                // Draw
                device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, new Color(0, 0, 0, 0), 1.0f, 0);
    
                foreach (ModelMesh mesh in model.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;
    
                        basicEffect.EmissiveColor = mesh == selectedMesh ? new Color(0.5f, 0.5f, 0.5f, 0) : Color.Black;
                       
                    }
                }
                //try
    
    
    
                // Particles
                try
                {
                    model.Draw();
                }
                catch (Exception)
                {
    
    
                }
                var ellapsed = (float)e.TotalTime.TotalSeconds;
                int verticesCount = model.Meshes.Sum(v => v.VerticesCount);
    
    
                e.InvalidateSurface();
            }
    
            #region Object loading
            
            private void Init(GraphicsDevice device)
            {
                Stream objStream = Application.GetResourceStream(new Uri("SilverlightApplication2;component/Model/brazile.obj", UriKind.Relative)).Stream;
               
                importer.OnImportCompleted += m =>
                {
                  
                    model = m.BuildModel(device, new Babylon.Toolbox.ModelBuilding.BasicEffectMaterialConverter());
    
                      drawingSurface.Invalidate();
                };
    
                      
    
                importer.ImportAsync(objStream, GetResourceStream, ImportationOptions.None | ImportationOptions.GeneratePickingInformations);
               
              
            }
    
           
    
    
            Stream GetResourceStream(string name)
            {
                try
                {
                 
                    return Application.GetResourceStream(new Uri(string.Format("SilverlightApplication2;component/Model/{0}", name), UriKind.Relative)).Stream;
                 
               
                
                }
                catch
                {
                   
                    return null;
                }
            }
            #endregion
    
            #region Aspect ratio
            private void drawingSurface_SizeChanged(object sender, SizeChangedEventArgs e)
            {
                ComputeAspectRatio();
            }
    
            void ComputeAspectRatio()
            {
                camera.AspectRatio = (float)(drawingSurface.ActualWidth / drawingSurface.ActualHeight);
            }
    
            private void UserControl_Loaded(object sender, RoutedEventArgs e)
            {
                ComputeAspectRatio();
            }
            #endregion
    
            #region Mouse
            private void drawingSurface_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
            {
                System.Windows.Point currentPosition = e.GetPosition(drawingSurface);
                if (!mouseLeftDown)
                    return;
    
                camera.InertialAlpha += (float)(currentPosition.X - startPosition.X) * camera.AngularSpeed;
                camera.InertialBeta -= (float)(currentPosition.Y - startPosition.Y) * camera.AngularSpeed;
                startPosition = currentPosition;
                
    
                /* System.Windows.Point currentPosition = e.GetPosition(drawingSurface);
    
                 switch (currentMode)
                 {
                     case MouseMode.ArcRotate:
                         if (!mouseLeftDown)
                             return;
    
                         camera.InertialAlpha += (float)(currentPosition.X - startPosition.X) * camera.AngularSpeed;
                         camera.InertialBeta -= (float)(currentPosition.Y - startPosition.Y) * camera.AngularSpeed;
                         startPosition = currentPosition;
                         break;
                     case MouseMode.ObjectPicking:
                         if (model == null)
                             return;
    
                         Ray ray = Utilities.CreateRay((float)currentPosition.X, (float)currentPosition.Y, (float)drawingSurface.ActualWidth, (float)drawingSurface.ActualHeight, Matrix.Identity, camera.View, camera.Projection);
    
                         float selectedDistance = float.MaxValue;
                         foreach (ModelMesh mesh in model.Meshes)
                         {
                             if (!ray.Intersects(mesh.BoundingSphere).HasValue)
                                 continue;
    
                             float distance;
                             if (mesh.Intersects(ray, out distance))
                             {
                                 if (distance < selectedDistance)
                                 {
                                     selectedDistance = distance;
                                     selectedMesh = mesh;
                                 }
                             }
                         }
                         if (selectedDistance == float.MaxValue)
                             selectedMesh = null;
                         else
                         {
                             Debug.WriteLine(ray.Position + ray.Direction * selectedDistance);
                         }
    
                         break;
                 }*/
            }
    
            private void drawingSurface_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                //switch (currentMode)
                //{
                //    case MouseMode.ArcRotate:
                mouseLeftDown = true;
                startPosition = e.GetPosition(drawingSurface);
                //        break;
                //}
            }
    
            private void drawingSurface_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                mouseLeftDown = false;
            }
    
            private void drawingSurface_MouseRightButtonDown(object sender, System.Windows.Input.MouseEventArgs e)
            {
                 mouseLeftDown = false;
            }
    
            private void drawingSurface_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
            {
                 camera.Radius -= e.Delta * camera.Radius / 1000.0f;
                 camera.FarPlane = camera.Radius + 50;
            }
            #endregion
    
          
            #region Particules
            static Texture2D LoadTexture(string resource, GraphicsDevice gd)
            {
                var stream = Application.GetResourceStream(new Uri(string.Format("Footballs-NE;component/Models/{0}", resource), UriKind.Relative)).Stream;
                var bmp = new BitmapImage();
                bmp.SetSource(stream);
    
    
                Texture2D res = new Texture2D(gd, bmp.PixelWidth, bmp.PixelHeight, false, SurfaceFormat.Color);
                bmp.CopyTo(res);
                return res;
            }
    
    
            #endregion
    
    
            public System.Threading.SynchronizationContext Synchonization { get; set; }
        }
    }

    DLL ref are :

    Babylon.Importers.dll(1.0.4.0)
    Babylon.Toolbox.dll(1.0.4.0)
    Microsoft.Xna.Framework.Math.dll(5.0.5.0)
    Monday, April 30, 2012 10:59 AM