samedi 25 avril 2020

C# WPF 3D cubes with different colors

I need your help with this one. Tried everything I thought about.

I want to generate a bunch of cubes in some exact position based on my algorithm. Everything is done but it seems that I can't generate each cube in one color.

private double generatebox(double l, double h, double L, double orgx, double orgy, double orgz, int color)
        {
            Brush brush = new SolidColorBrush(Colors.Blue);
            switch (color)
            {
                case 1: 
                    brush = new SolidColorBrush(Colors.Blue);
                    break;
                case 2:
                    brush = new SolidColorBrush(Colors.Green);
                    break;
                case 3:
                    brush = new SolidColorBrush(Colors.Red);
                    break;
                default:
                    brush = new SolidColorBrush(Colors.Yellow);
                    break;
            }
            Model3DGroup cube = new Model3DGroup();

            //
            Material mat = new DiffuseMaterial(brush);
            //
            Point3D p0 = new Point3D(orgx, orgz, orgy);
            Point3D p1 = new Point3D(orgx + l, orgz, orgy);
            Point3D p2 = new Point3D(orgx + l, orgz + h, orgy);
            Point3D p3 = new Point3D(orgx, orgz + h, orgy);
            Point3D p4 = new Point3D(orgx, orgz + h, orgy + L);
            Point3D p5 = new Point3D(orgx + l, orgz + h, orgy + L);
            Point3D p6 = new Point3D(orgx + l, orgz, orgy + L);
            Point3D p7 = new Point3D(orgx, orgz, orgy + L);
            //
            cube.Children.Add(createTriangleModel(p0, p2, p1, mat));
            cube.Children.Add(createTriangleModel(p0, p3, p2, mat));
            cube.Children.Add(createTriangleModel(p2, p3, p4, mat));
            cube.Children.Add(createTriangleModel(p2, p4, p5, mat));
            cube.Children.Add(createTriangleModel(p1, p2, p5, mat));
            cube.Children.Add(createTriangleModel(p1, p5, p6, mat));
            cube.Children.Add(createTriangleModel(p0, p7, p4, mat));
            cube.Children.Add(createTriangleModel(p0, p4, p3, mat));
            cube.Children.Add(createTriangleModel(p5, p4, p7, mat));
            cube.Children.Add(createTriangleModel(p5, p7, p6, mat));
            cube.Children.Add(createTriangleModel(p0, p6, p7, mat));
            cube.Children.Add(createTriangleModel(p0, p1, p6, mat));
            //
            ModelVisual3D model = new ModelVisual3D();
            model.Content = cube;
            this.viewport3D.Children.Add(model);

            double prod = l * h * L;
            return prod;
        }

This is what I have know. When I call the generatebox function I call it with rnd digit 1...4. The cubes changes the color each time I run the code but all cubes have the same color. I want each cube to have its random color.

Any help will be appreaciate it. Thank you!




Aucun commentaire:

Enregistrer un commentaire