Search code examples
c#wpfxaml3dhelix-3d-toolkit

WPF models appear black with helix viewer 3d c#


I am new to WPF and models, and I wanted to make an interface that could open various obj files and display the models.

At the momment all of them appear black in the viewport as below:

black model

I'm not sure why, but I think it is a lightning issue, because all the models have different positions, and I don't know how to change the position. I don't think it has to do with the obj it self, as in the viewer from windows, it appears colored:

colored model

This is my xaml part:

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="2*"/>
        </Grid.ColumnDefinitions>

    <ListBox x:Name="Groupfiles" SelectionChanged="Groupfiles_Selected" Grid.Column="0"></ListBox>
    <StackPanel Grid.Column="1">
        <Button x:Name="ClickThebutton" Click="ClickThebutton_Click" >Click Me</Button>
    </StackPanel>
    <helix:HelixViewport3D x:Name="viewPort3d" ZoomExtentsWhenLoaded="true" Grid.Column="2" >
    </helix:HelixViewport3D>
</Grid>

This is the c# part:

ModelVisual3D device3D = new ModelVisual3D();
string file_to_open = null;
file_to_open = Groupfiles.SelectedItem.ToString();
string obj_file = Path.ChangeExtension(file_to_open, ".obj");
if (!File.Exists(obj_file))
{
     /// converts original file to obj file
}

device3D.Content = Display3d(obj_file);

viewPort3d.Children.Clear();
viewPort3d.Children.Add(device3D);
viewPort3d.ZoomExtents();

The Display Method is as such (from the helix3d tutorial I saw):

public static Model3D Display3d(string model)
{
    Model3D device = null;
    try
    {
        //Adding a gesture here
        viewPort3d.RotateGesture =new MouseGesture(MouseAction.LeftClick));
            
        //Import 3D model file
        ModelImporter import = new ModelImporter();

        //Load the 3D model file
        device= import.Load(model);
    }
    catch (Exception e)
    {
        // Handle exception in case can not find the 3D model file
        MessageBox.Show("Exception Error : " + e.StackTrace);
    }
    return device;
}

I have tried to find an answer to this issue but to no avail. Could you please help me.


Solution

  • I solved my issue by adding these two lines after zoomextents();

    ui.viewPort3d.Children.Remove(ui.defaultLights);
    ui.viewPort3d.Children.Add(ui.defaultLights);
    

    Thank you for everyone who took their time to help.