Beispiel #1
0
        public MainViewModel()
        {
            EffectsManager = new DefaultEffectsManager();
            // titles
            Title    = "Simple Demo";
            SubTitle = "WPF & SharpDX";

            // camera setup
            Camera = new PerspectiveCamera {
                Position         = new Point3D(3, 3, 5),
                LookDirection    = new Vector3D(-3, -3, -5),
                UpDirection      = new Vector3D(0, 1, 0),
                FarPlaneDistance = 5000000
            };

            // setup lighting
            AmbientLightColor         = Colors.DimGray;
            DirectionalLightColor     = Colors.White;
            DirectionalLightDirection = new Vector3D(-2, -5, -2);

            // floor plane grid
            Grid          = LineBuilder.GenerateGrid(new Vector3(0, 1, 0), -5, 5, -5, 5);
            GridColor     = Colors.Black;
            GridTransform = new Media3D.TranslateTransform3D(0, -3, 0);

            // scene model3d
            var b1 = new MeshBuilder();

            b1.AddSphere(new Vector3(0, 0, 0), 0.5);
            b1.AddBox(new Vector3(0, 0, 0), 1, 0.5, 2, BoxFaces.All);

            var meshGeometry = b1.ToMeshGeometry3D();

            meshGeometry.Colors = new Color4Collection(meshGeometry.TextureCoordinates.Select(x => x.ToColor4()));
            Model = meshGeometry;

            // lines model3d
            var e1 = new LineBuilder();

            e1.AddBox(new Vector3(0, 0, 0), 1, 0.5, 2);
            Lines = e1.ToLineGeometry3D();

            var textBuilder = new MeshBuilder();

            textBuilder.ExtrudeText("HelixToolkit.SharpDX", "Arial", System.Windows.FontStyles.Normal, System.Windows.FontWeights.Bold,
                                    14, new Vector3(1, 0, 0), new Vector3(0, 0, 0), new Vector3(0, 0, 1));
            TextModel = textBuilder.ToMesh();

            // model trafos
            Model1Transform = new Media3D.TranslateTransform3D(0, 0, 0);
            Model2Transform = new Media3D.TranslateTransform3D(-2, 0, 0);
            Model3Transform = new Media3D.TranslateTransform3D(+2, 0, 0);
            Model4Transform = new Media3D.TranslateTransform3D(-8, 0, -5);

            // model materials
            RedMaterial   = PhongMaterials.Red;
            GreenMaterial = PhongMaterials.Green;
            BlueMaterial  = PhongMaterials.Blue;
            //var diffColor = this.RedMaterial.DiffuseColor;
            //diffColor.Alpha = 0.5f;
            //this.RedMaterial.DiffuseColor = diffColor;

            Points = new PointGeometry3D();
            var ptPos = new Vector3Collection();
            var ptIdx = new IntCollection();

            for (int x = 0; x < 10; x++)
            {
                for (int y = 0; y < 10; y++)
                {
                    for (int z = 0; z < 10; z++)
                    {
                        ptIdx.Add(ptPos.Count);
                        ptPos.Add(new Vector3(x, y, z));
                    }
                }
            }

            Points.Positions = ptPos;
            Points.Indices   = ptIdx;

            Text = new BillboardText3D();
            int numRows    = 11;
            int numColumns = 11;

            string[] texts = new string[]
            {
                "HelixToolkit",
                "abcde",
                "random",
                "SharpDX",
                "DirectX"
            };
            float angle = 0;

            for (var i = 0; i < numRows; i++)
            {
                for (var j = 0; j < numColumns; j++)
                {
                    angle += (float)Math.PI / 10;
                    Text.TextInfo.Add(new TextInfo(texts[(i + j) % texts.Length], new Vector3((i - numRows / 2), 0.0f, (j - numColumns / 2)))
                    {
                        Foreground = new Color4((float)i / numRows, 1 - (float)i / numRows, (float)(numColumns - j) / numColumns, 1f),
                        Background = new Color4(1 - (float)i / numRows, (float)(numColumns - j) / numColumns, (float)i / numRows, 0.8f),
                        Scale      = Math.Max(0.01f, (float)i / numRows * 0.02f),
                        Angle      = angle
                    });
                }
            }

            Billboard1Model = new BillboardSingleText3D()
            {
                TextInfo = new TextInfo("Model 1", new Vector3(0, 1, 0))
                {
                    Angle = 0
                },
                FontColor       = Colors.Blue.ToColor4(),
                FontSize        = 12,
                BackgroundColor = Colors.Plum.ToColor4(),
                FontStyle       = System.Windows.FontStyles.Italic,
                Padding         = new System.Windows.Thickness(2),
            };

            var background = Colors.Blue;

            background.A    = (byte)120;
            Billboard2Model = new BillboardSingleText3D()
            {
                TextInfo = new TextInfo("Model 2", new Vector3(2, 1, 0))
                {
                    Angle = -(float)Math.PI / 3
                },
                FontSize        = 12,
                FontColor       = Colors.Green.ToColor4(),
                BackgroundColor = background.ToColor4(),
                FontWeight      = System.Windows.FontWeights.Bold,
                Padding         = new System.Windows.Thickness(2),
            };
            background      = Colors.Purple;
            background.A    = (byte)50;
            Billboard3Model = new BillboardSingleText3D(2, 0.8f)
            {
                TextInfo = new TextInfo("Model 3", new Vector3(-2, 1, 0))
                {
                    Angle = -(float)Math.PI / 6
                },
                FontSize        = 12,
                FontColor       = Colors.Red.ToColor4(),
                BackgroundColor = background.ToColor4(),
                FontFamily      = "Times New Roman",
                FontStyle       = System.Windows.FontStyles.Italic,
                Padding         = new System.Windows.Thickness(2),
            };


            //BillboardImageModel = new BillboardSingleImage3D(CreateBitmapSample()) { MaskColor = Color.Black };
            BillboardImageModel = new BillboardSingleImage3D(CreatePNGSample(), 1, 1)
            {
                Angle = -(float)Math.PI / 5
            };
            BillboardImageModel.Center = new Vector3(2, 2, 0);

            UpXCommand        = new RelayCommand(x => { UpDirection = new Vector3D(1, 0, 0); });
            UpYCommand        = new RelayCommand(x => { UpDirection = new Vector3D(0, 1, 0); });
            UpZCommand        = new RelayCommand(x => { UpDirection = new Vector3D(0, 0, 1); });
            BackgroundTexture =
                BitmapExtensions.CreateLinearGradientBitmapStream(EffectsManager, 128, 128, Direct2DImageFormat.Bmp,
                                                                  new Vector2(0, 0), new Vector2(0, 128), new SharpDX.Direct2D1.GradientStop[]
            {
                new SharpDX.Direct2D1.GradientStop()
                {
                    Color = Colors.White.ToColor4(), Position = 0f
                },
                new SharpDX.Direct2D1.GradientStop()
                {
                    Color = Colors.DarkGray.ToColor4(), Position = 1f
                }
            });
        }
        public MainViewModel()
        {
            // titles
            Title    = "Simple Demo";
            SubTitle = "WPF & SharpDX";

            // camera setup
            Camera = new PerspectiveCamera {
                Position         = new Point3D(3, 3, 5),
                LookDirection    = new Vector3D(-3, -3, -5),
                UpDirection      = new Vector3D(0, 1, 0),
                FarPlaneDistance = 5000000
            };

            // default render technique
            RenderTechniquesManager = new DefaultRenderTechniquesManager();
            RenderTechnique         = RenderTechniquesManager.RenderTechniques[DefaultRenderTechniqueNames.Blinn];
            EffectsManager          = new DefaultEffectsManager(RenderTechniquesManager);

            // setup lighting
            AmbientLightColor         = new Color4(0.1f, 0.1f, 0.1f, 1.0f);
            DirectionalLightColor     = Color.White;
            DirectionalLightDirection = new Vector3(-2, -5, -2);

            // floor plane grid
            Grid          = LineBuilder.GenerateGrid();
            GridColor     = SharpDX.Color.Black;
            GridTransform = new Media3D.TranslateTransform3D(-5, -1, -5);

            // scene model3d
            var b1 = new MeshBuilder();

            b1.AddSphere(new Vector3(0, 0, 0), 0.5);
            b1.AddBox(new Vector3(0, 0, 0), 1, 0.5, 2, BoxFaces.All);

            var meshGeometry = b1.ToMeshGeometry3D();

            meshGeometry.Colors = new Color4Collection(meshGeometry.TextureCoordinates.Select(x => x.ToColor4()));
            Model = meshGeometry;

            // lines model3d
            var e1 = new LineBuilder();

            e1.AddBox(new Vector3(0, 0, 0), 1, 0.5, 2);
            Lines = e1.ToLineGeometry3D();

            // model trafos
            Model1Transform = new Media3D.TranslateTransform3D(0, 0, 0);
            Model2Transform = new Media3D.TranslateTransform3D(-2, 0, 0);
            Model3Transform = new Media3D.TranslateTransform3D(+2, 0, 0);

            // model materials
            RedMaterial   = PhongMaterials.Red;
            GreenMaterial = PhongMaterials.Green;
            BlueMaterial  = PhongMaterials.Blue;
            //var diffColor = this.RedMaterial.DiffuseColor;
            //diffColor.Alpha = 0.5f;
            //this.RedMaterial.DiffuseColor = diffColor;

            Points = new PointGeometry3D();
            var ptPos = new Vector3Collection();
            var ptIdx = new IntCollection();

            for (int x = 0; x < 10; x++)
            {
                for (int y = 0; y < 10; y++)
                {
                    for (int z = 0; z < 10; z++)
                    {
                        ptIdx.Add(ptPos.Count);
                        ptPos.Add(new Vector3(x, y, z));
                    }
                }
            }

            Points.Positions = ptPos;
            Points.Indices   = ptIdx;

            Text = new BillboardText3D();

            for (var i = 0; i < 50; i++)
            {
                for (var j = 0; j < 50; j++)
                {
                    Text.TextInfo.Add(new TextInfo("Hello World", new Vector3(i, j, 0)));
                }
            }

            Billboard1Model = new BillboardSingleText3D()
            {
                TextInfo        = new TextInfo("Model 1", new Vector3(0, 1, 0)),
                FontColor       = Color.Blue,
                FontSize        = 12,
                BackgroundColor = Color.Plum,
                FontStyle       = System.Windows.FontStyles.Italic,
                Padding         = new System.Windows.Thickness(2)
            };

            var background = Color.Blue;

            background.A    = (byte)120;
            Billboard2Model = new BillboardSingleText3D()
            {
                TextInfo        = new TextInfo("Model 1", new Vector3(2, 1, 0)),
                FontSize        = 12,
                FontColor       = Color.Green,
                BackgroundColor = background,
                FontWeight      = System.Windows.FontWeights.Bold,
                Padding         = new System.Windows.Thickness(2)
            };
            background      = Color.Purple;
            background.A    = (byte)50;
            Billboard3Model = new BillboardSingleText3D(2, 0.8f)
            {
                TextInfo        = new TextInfo("Model 1", new Vector3(-2, 1, 0)),
                FontSize        = 12,
                FontColor       = Color.Red,
                BackgroundColor = background,
                FontFamily      = new System.Windows.Media.FontFamily("Times New Roman"),
                FontStyle       = System.Windows.FontStyles.Italic,
                Padding         = new System.Windows.Thickness(2)
            };


            //BillboardImageModel = new BillboardSingleImage3D(CreateBitmapSample()) { MaskColor = Color.Black };
            BillboardImageModel        = new BillboardSingleImage3D(CreatePNGSample(), 1, 1);
            BillboardImageModel.Center = new Vector3(2, 2, 0);
        }