0

3d cube with colored cells

TheInevitable360 2 years ago updated by james vens 7 months ago 0

I am working on a code to build a 3D cube that has mesh/cell in each direction and each cell has its own color. now i sue this method which produces exactly what i want:

private void BuildCustomCube22(int x, int y, int z)
{
    double cubeSize = 1.0; // Size of each cube cell

    Random random = new Random();

    var modelGroup = new Model3DGroup();

    for (int i = 0; i < x; i++)
    {
        for (int j = 0; j < y; j++)
        {
            for (int k = 0; k < z; k++)
            {
                var position = new Point3D(
                    i * cubeSize + cubeSize / 2,
                    j * cubeSize + cubeSize / 2,
                   k * cubeSize + cubeSize / 2);

               // Generate a random color for each cube
               Color color = Color.FromRgb((byte)random.Next(256), (byte)random.Next(256),
               (byte)random.Next(256));

               var cube = new BoxVisual3D
               {
                   Center = position,
                   Width = cubeSize,
                   Height = cubeSize,
                   Length = cubeSize,
                  Material = MaterialHelper.CreateMaterial(color),

                };

               // Add the cube to the model group
               modelGroup.Children.Add(cube.Model);
           }
       }
   }

    // Add the model group to the viewport
    viewport.Children.Add(new ModelVisual3D { Content = modelGroup });
}

and the result is:

Image 195

The issue is when i increase mesh like 100*100*100, i get very poor performance like not being able to easily zoom in/out or move object around. I wonder how can i achieve the result i want event with high number of cells with no lag.

P.S: somebody told me: You are making way too many models. You can try to use Helix toolkit sharpdx version with instancing rendering capability

but i don't know if it can solve my issue and i don't know how to implement it.