Esempio n. 1
0
        public MainViewModel()
        {
            // titles
            this.Title = "Simple Demo (Workitem 10043)";
            this.SubTitle = "Please switch to Viewport 2 and then back to Viewport 1";

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

            if (this.RenderTechniquesManager != null)
            {
                // default render technique
                this.RenderTechnique = RenderTechniquesManager.RenderTechniques.Get(DefaultRenderTechniqueNames.Blinn);
            }

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

            // floor plane grid
            this.Grid = LineBuilder.GenerateGrid();
            this.GridColor = SharpDX.Color.Black;
            this.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()));
            this.Model = meshGeometry;

            // lines model3d
            var e1 = new LineBuilder();
            e1.AddBox(new Vector3(0, 0, 0), 1, 0.5, 2);
            this.Lines = e1.ToLineGeometry3D();

            // model trafos
            this.Model1Transform = new Media3D.TranslateTransform3D(0, 0, 0);
            this.Model2Transform = new Media3D.TranslateTransform3D(-2, 0, 0);
            this.Model3Transform = new Media3D.TranslateTransform3D(+2, 0, 0);
            
            // model materials
            this.RedMaterial = PhongMaterials.Red;
            this.GreenMaterial = PhongMaterials.Green;
            this.BlueMaterial = PhongMaterials.Blue;
            //var diffColor = this.RedMaterial.DiffuseColor;
            //diffColor.Alpha = 0.5f;
            //this.RedMaterial.DiffuseColor = diffColor;            
        }
Esempio n. 2
0
        public MainViewModel()
        {
            RenderTechniquesManager = new DefaultRenderTechniquesManager();
            RenderTechnique = RenderTechniquesManager.RenderTechniques[DefaultRenderTechniqueNames.Blinn];
            EffectsManager = new DefaultEffectsManager(RenderTechniquesManager);

            this.Title = "Line Shading Demo (HelixToolkitDX)";
            this.SubTitle = null;

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

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

            // floor plane grid
            this.Grid = LineBuilder.GenerateGrid();
            this.GridColor = SharpDX.Color.Black;
            this.GridTransform = new 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);
            this.Model = b1.ToMeshGeometry3D();

            // lines model3d
            var e1 = new LineBuilder();
            e1.AddBox(new Vector3(0, 0, 0), 1, 0.5, 2);
            //e1.AddLine(new Vector3(-1, 0, 0), new Vector3(1, 0, 0));
            this.Lines = e1.ToLineGeometry3D();

            // lines params
            this.LineThickness = 2;
            this.LineSmoothness = 2.0;
            this.LinesEnabled = true;
            this.GridEnabled = true;

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

            // model materials
            this.Material1 = PhongMaterials.PolishedGold;
            this.Material2 = PhongMaterials.Copper;
            this.Material3 = PhongMaterials.Glass;                        
        }
Esempio n. 3
0
        public MainViewModel()
        {
            RenderTechniquesManager = new DefaultRenderTechniquesManager();
            RenderTechnique = RenderTechniquesManager.RenderTechniques[DefaultRenderTechniqueNames.Blinn];
            EffectsManager = new DefaultEffectsManager(RenderTechniquesManager);

            this.Title = "Manipulator Demo";
            this.SubTitle = null;

            // camera setup
            this.Camera = new OrthographicCamera { Position = new Point3D(0, 0, 5), LookDirection = new Vector3D(0, 0, -5), UpDirection = new Vector3D(0, 1, 0) };

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

            // floor plane grid
            this.Grid = LineBuilder.GenerateGrid();
            this.GridColor = SharpDX.Color.Black;
            this.GridTransform = new 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, 1.5, BoxFaces.All);
            this.Model = b1.ToMeshGeometry3D();

            // lines model3d
            var e1 = new LineBuilder();
            e1.AddBox(new Vector3(0, 0, 0), 1, 0.5, 1.5);
            this.Lines = e1.ToLineGeometry3D();

            // model trafos
            this.Model1Transform = CreateAnimatedTransform(new Vector3D(0, 0, 0), new Vector3D(1, 1, 1), 20);
            this.Model2Transform = new TranslateTransform3D(-3, 0, 0);
            this.Model3Transform = new TranslateTransform3D(+3, 0, 0);

            // model materials
            this.Material1 = PhongMaterials.Orange;
            this.Material2 = PhongMaterials.Orange;
            this.Material3 = PhongMaterials.Red;

            var dr = Color.DarkRed;
            Console.WriteLine(dr);
        }
Esempio n. 4
0
        public MainViewModel()
        {
            Title = "Instancing Demo";            

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

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

            // scene model3d
            var b1 = new MeshBuilder(); 
            b1.AddBox(new Vector3(0, 0, 0), 0.8, 0.8, 0.5, BoxFaces.All);
            Model = b1.ToMeshGeometry3D();

            var l1 = new LineBuilder();
            l1.AddBox(new Vector3(0, 0, 0), 0.8, 0.8, 0.5);
            Lines = l1.ToLineGeometry3D();   

            int num = 10;
            var instances = new List<Matrix>();
            for (int i = -num; i < num; i++)
            {
                for (int j = -num; j < num; j++)
                {
                    instances.Add(Matrix.Translation(new Vector3(i / 1.0f, j / 1.0f, 0f)));
                }
            }
            ModelInstances = instances;
            SubTitle = "Number of Instances: " + instances.Count.ToString();

            // model trafo
            ModelTransform = Media3D.Transform3D.Identity;// new Media3D.RotateTransform3D(new Media3D.AxisAngleRotation3D(new Vector3D(0, 0, 1), 45));

            // model material
            ModelMaterial = PhongMaterials.Glass;

            RenderTechniquesManager = new DefaultRenderTechniquesManager();
            RenderTechnique = RenderTechniquesManager.RenderTechniques[DefaultRenderTechniqueNames.Blinn];
            EffectsManager = new DefaultEffectsManager(RenderTechniquesManager);
        }
Esempio n. 5
0
        public LineGeometryModel3D GetGrid()
        {
            var lineBilder = new LineBuilder();

            for (int x = -1000; x < 1000; x += 100)
            {
                lineBilder.AddLine(new Vector3(x, 0, -1000), new Vector3(x, 0, 1000));
            }

            for (int z = -1000; z < 1000; z += 100)
            {
                lineBilder.AddLine(new Vector3(-1000, 0, z), new Vector3(1000, 0, z));
            }

            LineGeometryModel3D m = new LineGeometryModel3D();
            m.Geometry = lineBilder.ToLineGeometry3D();
            m.Color = Color.Black;
            m.Transform = new TranslateTransform3D(new Vector3D(0, 0, 0));
            m.Attach(Viewport.RenderHost);

            return m;
        }
Esempio n. 6
0
        public CameraModel3D()
        {
            var b1 = new MeshBuilder();

            b1.AddBox(new Vector3(), 1f, 1f, 1.2f, BoxFaces.All);
            var body = new MeshGeometryModel3D()
            {
                CullMode = CullMode.Back
            };

            body.Geometry = b1.ToMeshGeometry3D();
            body.Material = new DiffuseMaterial()
            {
                DiffuseColor = Color.Gray
            };
            this.Children.Add(body);
            b1 = new MeshBuilder();
            b1.AddCone(new Vector3(0, 0, -1.2f), new Vector3(0, 0f, 0), 0.4f, true, 12);
            var lens = new MeshGeometryModel3D()
            {
                CullMode = CullMode.Back
            };

            lens.Geometry = b1.ToMeshGeometry3D();
            lens.Material = new DiffuseMaterial()
            {
                DiffuseColor = Color.Yellow
            };
            this.Children.Add(lens);

            var builder = new LineBuilder();

            builder.AddLine(Vector3.Zero, new Vector3(2, 0, 0));
            builder.AddLine(Vector3.Zero, new Vector3(0, 2, 0));
            builder.AddLine(Vector3.Zero, new Vector3(0, 0, -2));

            var mesh           = builder.ToLineGeometry3D();
            var arrowMeshModel = new LineGeometryModel3D
            {
                Geometry         = mesh,
                Color            = System.Windows.Media.Colors.White,
                IsHitTestVisible = false
            };
            int segment = mesh.Positions.Count / 3;
            var colors  = new Core.Color4Collection(Enumerable.Repeat <Color4>(Color.Black, mesh.Positions.Count));
            int i       = 0;

            for (; i < segment; ++i)
            {
                colors[i] = Color.Red;
            }
            for (; i < segment * 2; ++i)
            {
                colors[i] = Color.Green;
            }
            for (; i < segment * 3; ++i)
            {
                colors[i] = Color.Blue;
            }
            mesh.Colors = colors;
            this.Children.Add(arrowMeshModel);
            SceneNode.OnTransformChanged += SceneNode_OnTransformChanged;
        }
        /// <summary>
        /// The on children changed.
        /// </summary>
        protected virtual void OnChildrenChanged()
        {
            this.translateXL.Length = 0.5;
            this.translateYL.Length = 0.5;
            this.translateZL.Length = 0.5;
            this.translateXR.Length = 0.5;
            this.translateYR.Length = 0.5;
            this.translateZR.Length = 0.5;

            this.Children.Clear();

            if (this.CanTranslateX)
            {
                this.Children.Add(this.translateXL);
                this.Children.Add(this.translateXR);
            }

            if (this.CanTranslateY)
            {
                this.Children.Add(this.translateYL);
                this.Children.Add(this.translateYR);
            }

            if (this.CanTranslateZ)
            {
                this.Children.Add(this.translateZL);
                this.Children.Add(this.translateZR);
            }


            {
                var g = new LineBuilder();
                g.AddLine(new Vector3(0, 0, 0), new Vector3(1, 0, 0));
                g.AddLine(new Vector3(1, 0, 0), new Vector3(1, 1, 0));
                g.AddLine(new Vector3(1, 1, 0), new Vector3(0, 1, 0));
                g.AddLine(new Vector3(0, 1, 0), new Vector3(0, 0, 0));
                this.selectionBounds = new LineGeometryModel3D()
                {
                    Thickness = 3,
                    Smoothness = 2,
                    Color = Color.Red,
                    IsThrowingShadow = false,
                    Geometry = g.ToLineGeometry3D(),
                };
                this.Children.Add(this.selectionBounds);
            }            
        }
        /// <summary>
        /// Generate a simple Polygon and then triangulate it.
        /// The Result is then Displayed.
        /// </summary>
        /// <param name="sender">The Sender (i.e. the Button)</param>
        /// <param name="e">The routet Event Args</param>
        private void generatePolygonButton_Click(object sender, RoutedEventArgs e)
        {
            // Generate random Polygon
            var random = new Random();
            var cnt = mViewModel.PointCount;
            mPolygonPoints = new List<Vector2>();
            var angle = 0f;
            var angleDiff = 2f * (Single)Math.PI / cnt;
            var radius = 4f;
            // Random Radii for the Polygon
            var radii = new List<float>();
            var innerRadii = new List<float>();
            for (int i = 0; i < cnt; i++)
            {
                radii.Add(random.NextFloat(radius * 0.9f, radius * 1.1f));
                innerRadii.Add(random.NextFloat(radius * 0.2f, radius * 0.3f));
            }
            var hole1 = new List<Vector2>();
            var hole2 = new List<Vector2>();
            var holeDistance = 2f;
            var holeAngle = random.NextFloat(0, (float)Math.PI * 2);
            var cos = (float)Math.Cos(holeAngle);
            var sin = (float)Math.Sin(holeAngle);
            var offset1 = new Vector2(holeDistance * cos, holeDistance * sin);
            var offset2 = new Vector2(-holeDistance * cos, -holeDistance * sin);
            for (int i = 0; i < cnt; i++)
            {
                // Flatten a bit
                var radiusUse = radii[i];
                mPolygonPoints.Add(new Vector2(radii[i] * (Single)Math.Cos(angle), radii[i] * (Single)Math.Sin(angle)));
                hole1.Add(offset1 + new Vector2(innerRadii[i] * (Single)Math.Cos(-angle), innerRadii[i] * (Single)Math.Sin(-angle)));
                hole2.Add(offset2 + new Vector2(innerRadii[i] * (Single)Math.Cos(-angle), innerRadii[i] * (Single)Math.Sin(-angle)));
                angle += angleDiff;
            }

            var holes = new List<List<Vector2>>() { hole1, hole2 };

            // Triangulate and measure the Time needed for the Triangulation
            var before = DateTime.Now;
            var sLTI = SweepLinePolygonTriangulator.Triangulate(mPolygonPoints, holes);
            var after = DateTime.Now;

            // Generate the Output
            var geometry = new HelixToolkit.Wpf.SharpDX.MeshGeometry3D();
            geometry.Positions = new HelixToolkit.Wpf.SharpDX.Core.Vector3Collection();
            geometry.Normals = new HelixToolkit.Wpf.SharpDX.Core.Vector3Collection();
            foreach (var point in mPolygonPoints.Union(holes.SelectMany(h => h)))
            {
                geometry.Positions.Add(new Vector3(point.X, 0, point.Y + 5));
                geometry.Normals.Add(new Vector3(0, 1, 0));
            }
            geometry.Indices = new HelixToolkit.Wpf.SharpDX.Core.IntCollection(sLTI);
            triangulatedPolygon.Geometry = geometry;

            var lb = new LineBuilder();
            for (int i = 0; i < sLTI.Count; i += 3)
            {
                lb.AddLine(geometry.Positions[sLTI[i]], geometry.Positions[sLTI[i + 1]]);
                lb.AddLine(geometry.Positions[sLTI[i + 1]], geometry.Positions[sLTI[i + 2]]);
                lb.AddLine(geometry.Positions[sLTI[i + 2]], geometry.Positions[sLTI[i]]);
            }
            mViewModel.LineGeometry  = lb.ToLineGeometry3D();

            // Set the Lines if activated
            if (mViewModel.ShowTriangleLines)
            {
                lineTriangulatedPolygon.Geometry = mViewModel.LineGeometry;
            }
            else
            {
                lineTriangulatedPolygon.Geometry = null;
            }

            // Set the InfoLabel Text
            var timeNeeded = (after - before).TotalMilliseconds;
            infoLabel.Content = String.Format("Last triangulation of {0} Points took {1:0.##} Milliseconds!", triangulatedPolygon.Geometry.Positions.Count, timeNeeded);
        }
Esempio n. 9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="plane"></param>
        /// <param name="radius"></param>
        /// <param name="segments"></param>
        /// <returns></returns>
        public static LineGeometry3D GenerateCircle(Vector3 plane, float radius, int segments)
        {
            if (segments < 3)
            {
                throw new ArgumentNullException("too few segments, at least 3");
            }

            var circle = new LineBuilder();

            float sectionAngle = (float)(2.0 * Math.PI / segments);

            if (plane == Vector3.UnitX)
            {
                Point3D start   = new Point3D(0.0f, 0.0f, radius);
                Point3D current = new Point3D(0.0f, 0.0f, radius);
                Point3D next    = new Point3D(0.0f, 0.0f, 0.0f);

                for (int i = 1; i < segments; i++)
                {
                    next.Z = radius * (float)Math.Cos(i * sectionAngle);
                    next.Y = radius * (float)Math.Sin(i * sectionAngle);

                    circle.AddLine(current, next);

                    current = next;
                }

                circle.AddLine(current, start);
            }
            else if (plane == Vector3.UnitY)
            {
                Point3D start   = new Point3D(radius, 0.0f, 0.0f);
                Point3D current = new Point3D(radius, 0.0f, 0.0f);
                Point3D next    = new Point3D(0.0f, 0.0f, 0.0f);

                for (int i = 1; i < segments; i++)
                {
                    next.X = radius * (float)Math.Cos(i * sectionAngle);
                    next.Z = radius * (float)Math.Sin(i * sectionAngle);

                    circle.AddLine(current, next);

                    current = next;
                }

                circle.AddLine(current, start);
            }
            else
            {
                Point3D start   = new Point3D(0.0f, radius, 0.0f);
                Point3D current = new Point3D(0.0f, radius, 0.0f);
                Point3D next    = new Point3D(0.0f, 0.0f, 0.0f);

                for (int i = 1; i < segments; i++)
                {
                    next.Y = radius * (float)Math.Cos(i * sectionAngle);
                    next.X = radius * (float)Math.Sin(i * sectionAngle);

                    circle.AddLine(current, next);

                    current = next;
                }

                circle.AddLine(current, start);
            }

            return(circle.ToLineGeometry3D());
        }
Esempio n. 10
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="plane"></param>
        /// <param name="radius"></param>
        /// <param name="segments"></param>
        /// <returns></returns>
        public static LineGeometry3D GenerateCircle(Vector3 plane, float radius, int segments)
        {
            if (segments < 3)
            {
                throw new ArgumentNullException("too few segments, at least 3");
            }

            var circle = new LineBuilder();

            float sectionAngle = (float)(2.0 * Math.PI / segments);

            if (plane == Vector3.UnitX)
            {
                Point3D start = new Point3D(0.0f, 0.0f, radius);
                Point3D current = new Point3D(0.0f, 0.0f, radius);
                Point3D next = new Point3D(0.0f, 0.0f, 0.0f);

                for (int i = 1; i < segments; i++)
                {
                    next.Z = radius * (float)Math.Cos(i * sectionAngle);
                    next.Y = radius * (float)Math.Sin(i * sectionAngle);

                    circle.AddLine(current, next);

                    current = next;
                }

                circle.AddLine(current, start);
            }
            else if (plane == Vector3.UnitY)
            {
                Point3D start = new Point3D(radius, 0.0f, 0.0f);
                Point3D current = new Point3D(radius, 0.0f, 0.0f);
                Point3D next = new Point3D(0.0f, 0.0f, 0.0f);

                for (int i = 1; i < segments; i++)
                {
                    next.X = radius * (float)Math.Cos(i * sectionAngle);
                    next.Z = radius * (float)Math.Sin(i * sectionAngle);

                    circle.AddLine(current, next);

                    current = next;
                }

                circle.AddLine(current, start);
            }
            else
            {
                Point3D start = new Point3D(0.0f, radius, 0.0f);
                Point3D current = new Point3D(0.0f, radius, 0.0f);
                Point3D next = new Point3D(0.0f, 0.0f, 0.0f);

                for (int i = 1; i < segments; i++)
                {
                    next.Y = radius * (float)Math.Cos(i * sectionAngle);
                    next.X = radius * (float)Math.Sin(i * sectionAngle);

                    circle.AddLine(current, next);

                    current = next;
                }

                circle.AddLine(current, start);
            }

            return circle.ToLineGeometry3D();
        }
Esempio n. 11
0
        /// <summary>
        /// Returns a line geometry of the axis-aligned bounding-box of the given mesh.
        /// </summary>
        /// <param name="mesh">Input mesh for the computation of the b-box</param>
        /// <returns></returns>
        public static LineGeometry3D GenerateBoundingBox(global::SharpDX.BoundingBox bb)
        {            
            var cc = bb.GetCorners();
            var ll = new LineBuilder();
            ll.AddLine(cc[0], cc[1]);
            ll.AddLine(cc[1], cc[2]);
            ll.AddLine(cc[2], cc[3]);
            ll.AddLine(cc[3], cc[0]);

            ll.AddLine(cc[4], cc[5]);
            ll.AddLine(cc[5], cc[6]);
            ll.AddLine(cc[6], cc[7]);
            ll.AddLine(cc[7], cc[4]);

            ll.AddLine(cc[0], cc[4]);
            ll.AddLine(cc[1], cc[5]);
            ll.AddLine(cc[2], cc[6]);
            ll.AddLine(cc[3], cc[7]);
            return ll.ToLineGeometry3D();
        }
Esempio n. 12
0
        public MainViewModel()
        {
            // titles
            this.Title = "Simple Demo";
            this.SubTitle = "WPF & SharpDX";

            // camera setup
            this.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
            this.RenderTechnique = Techniques.RenderBlinn;

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

            // floor plane grid
            this.Grid = LineBuilder.GenerateGrid();
            this.GridColor = SharpDX.Color.Black;
            this.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()));
            this.Model = meshGeometry;

            // lines model3d
            var e1 = new LineBuilder();
            e1.AddBox(new Vector3(0, 0, 0), 1, 0.5, 2);
            this.Lines = e1.ToLineGeometry3D();

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

            // model materials
            this.RedMaterial = PhongMaterials.Red;
            this.GreenMaterial = PhongMaterials.Green;
            this.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)));
                }
            }
        }
Esempio n. 13
0
        public override bool HitTest(Ray rayWS, ref List <HitTestResult> hits)
        {
            LineGeometry3D lineGeometry3D;
            Viewport3DX    viewport;

            if (this.Visibility == Visibility.Collapsed ||
                this.IsHitTestVisible == false ||
                (viewport = FindVisualAncestor <Viewport3DX>(this.renderHost as DependencyObject)) == null ||
                (lineGeometry3D = this.Geometry as LineGeometry3D) == null)
            {
                return(false);
            }

            // revert unprojection; probably better: overloaded HitTest() for LineGeometryModel3D?
            var svpm        = viewport.GetScreenViewProjectionMatrix();
            var smvpm       = this.modelMatrix * svpm;
            var clickPoint4 = new Vector4(rayWS.Position + rayWS.Direction, 1);

            Vector4.Transform(ref clickPoint4, ref svpm, out clickPoint4);
            var clickPoint = clickPoint4.ToVector3();

            var result = new HitTestResult {
                IsValid = false, Distance = double.MaxValue
            };
            var maxDist  = this.HitTestThickness;
            var lastDist = double.MaxValue;
            var index    = 0;

            foreach (var line in lineGeometry3D.Lines)
            {
                var     p0 = Vector3.TransformCoordinate(line.P0, smvpm);
                var     p1 = Vector3.TransformCoordinate(line.P1, smvpm);
                Vector3 hitPoint;
                float   t;

                var dist = LineBuilder.GetPointToLineDistance2D(ref clickPoint, ref p0, ref p1, out hitPoint, out t);
                if (dist < lastDist && dist <= maxDist)
                {
                    lastDist = dist;
                    Vector4 res;
                    var     lp0 = line.P0;
                    Vector3.Transform(ref lp0, ref this.modelMatrix, out res);
                    lp0 = res.ToVector3();

                    var lp1 = line.P1;
                    Vector3.Transform(ref lp1, ref this.modelMatrix, out res);
                    lp1 = res.ToVector3();

                    var lv         = lp1 - lp0;
                    var hitPointWS = lp0 + lv * t;
                    result.Distance = (rayWS.Position - hitPointWS).Length();
                    result.PointHit = hitPointWS.ToPoint3D();
                    result.ModelHit = this;
                    result.IsValid  = true;
                    result.Tag      = index; // ToDo: LineHitTag with additional info
                }

                index++;
            }

            if (result.IsValid)
            {
                hits.Add(result);
            }

            return(result.IsValid);
        }
Esempio n. 14
0
        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
            };

            // Create a custom render techniques manager that 
            // only supports Phong and Blinn
            RenderTechniquesManager = new CustomRenderTechniquesManager();
            RenderTechnique = RenderTechniquesManager.RenderTechniques["RenderCustom"];
            EffectsManager = new CustomEffectsManager(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 = 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 transform
            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;

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

            Text = new BillboardText3D();

            for (int x = -5; x <= 5; x++)
            {
                for (int y = -5; y <= 5; y++)
                {
                    ptIdx.Add(ptPos.Count);
                    ptPos.Add(new Vector3(x, -1, y));
                    Text.TextInfo.Add(new TextInfo(string.Format("{0}:{1}", x, y), new Vector3(x, -1, y)));
                }
            }

            Points.Positions = ptPos;
            Points.Indices = ptIdx;
        }
Esempio n. 15
0
        public MainViewModel()
        {
            RenderTechniquesManager = new DefaultRenderTechniquesManager();
            RenderTechnique = RenderTechniquesManager.RenderTechniques[DefaultRenderTechniqueNames.Blinn];
            EffectsManager = new DefaultEffectsManager(RenderTechniquesManager);

            // titles
            this.Title = "Mouse Drag Demo";
            this.SubTitle = "WPF & SharpDX";

            // camera setup
            this.Camera = new PerspectiveCamera { Position = new Point3D(0, 0, 9), LookDirection = new Vector3D(-0, -0, -9), UpDirection = new Vector3D(0, 1, 0) };

            // default render technique
            this.RenderTechnique = RenderTechniquesManager.RenderTechniques[DefaultRenderTechniqueNames.Blinn];

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

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

            // scene model3d
            var b1 = new MeshBuilder();
            b1.AddSphere(new Vector3(0, 0, 0), 0.65);
            b1.AddBox(new Vector3(0, 0, 0), 1, 1, 1);
            var meshGeometry = b1.ToMeshGeometry3D();
            meshGeometry.Colors = new Color4Collection(meshGeometry.TextureCoordinates.Select(x => x.ToColor4()));
            this.MeshGeometry = meshGeometry;
            this.Model1Instances = new List<Matrix>();
            for (int i = 0; i < 5; i++)
            {
                this.Model1Instances.Add(Matrix.Translation(0, i, 0));
            }

            // lines model3d
            var e1 = new LineBuilder();
            e1.AddBox(new Vector3(0, 0, 0), 1, 0.5, 2);
            this.Lines = e1.ToLineGeometry3D();

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

            // model materials
            this.RedMaterial = PhongMaterials.Red;
            this.GreenMaterial = PhongMaterials.Green;
            this.BlueMaterial = PhongMaterials.Blue;

            // ---
            this.Shape3DCollection = new ObservableCollection<Shape3D>
            {
                new Shape3D()
                {
                    Geometry = this.MeshGeometry,
                    Material = this.BlueMaterial,
                    Transform = this.Model3Transform,
                    Instances = new List<Matrix>{Matrix.Identity},
                    DragZ = false,
                },
                new Shape3D()
                {
                    Geometry = this.MeshGeometry,
                    Material = this.RedMaterial,
                    Transform = this.Model1Transform,
                    Instances = new List<Matrix>{Matrix.Identity},
                    DragZ = true,
                },
            };

            this.Element3DCollection = new ObservableCollection<Element3D>()
            {
                new DraggableGeometryModel3D()
                {
                    Geometry = this.MeshGeometry,
                    Material = this.BlueMaterial,
                    Transform = this.Model3Transform,
                },
                                
                new DraggableGeometryModel3D()
                {
                    Geometry = this.MeshGeometry,
                    Material = this.RedMaterial,
                    Transform = this.Model1Transform,
                },
            };

            this.AddCmd = new RelayCommand((o) => AddShape());
            this.DelCmd = new RelayCommand((o) => DelShape());
        }
Esempio n. 16
0
        private void CreateWireFrameModels(IFCItem item, Vector3D center)
        {
            while (item != null)
            {
                if (item.ifcID != IntPtr.Zero && item.noVerticesForWireFrame != 0 && item.noPrimitivesForWireFrame != 0)
                {
                    var points = new Vector3Collection();
                    Vector3Collection positions;
                    if (item.verticesForWireFrame != null)
                    {
                        for (int i = 0; i < item.noVerticesForWireFrame; i++)
                        {
                            points.Add(new Vector3((float)(item.verticesForWireFrame[3 * i + 0] - center.X), (float)(item.verticesForWireFrame[3 * i + 1] - center.Y), (float)(item.verticesForWireFrame[3 * i + 2] - center.Z)));
                        }
                    }

                    if (item.indicesForWireFrameLineParts != null)
                    {
                        positions = new Vector3Collection();
                        for (int i = 0; i < item.noPrimitivesForWireFrame; i++)
                        {
                            var idx = item.indicesForWireFrameLineParts[2 * i + 0];
                            positions.Add(points[idx]);
                            idx = item.indicesForWireFrameLineParts[2 * i + 1];
                            positions.Add(points[idx]);
                        }
                    }
                    else {
                        positions = points;
                    }

                    var lineBuilder = new LineBuilder();
                    lineBuilder.Add(false,positions.ToArray());
                    LineGeometryModel3D line = new LineGeometryModel3D();
                    line.Geometry = lineBuilder.ToLineGeometry3D();
                    line.Color = new SharpDX.Color(0,0,0,0);
                    item.Wireframe = line;
                    model.Add(line as Element3D);
                }

                CreateFaceModels(item.child, center);
                item = item.next;
            }
        }
Esempio n. 17
0
        /// <summary>
        /// Generates a square grid with a step of 1.0 
        /// </summary>
        /// <returns></returns>
        public static LineGeometry3D GenerateGrid(Vector3 plane, int min = 0, int max = 10)
        {
            var grid = new LineBuilder();
            //int width = max - min;
            if (plane == Vector3.UnitX)
            {
                for (int i = min; i <= max; i++)
                {
                    grid.AddLine(new Vector3(0, i, min), new Vector3(0, i, max));
                    grid.AddLine(new Vector3(0, min, i), new Vector3(0, max, i));
                }
            }
            else if (plane == Vector3.UnitY)
            {
                for (int i = min; i <= max; i++)
                {
                    grid.AddLine(new Vector3(i, 0, min), new Vector3(i, 0, max));
                    grid.AddLine(new Vector3(min, 0, i), new Vector3(max, 0, i));
                }
            }
            else
            {
                for (int i = min; i <= max; i++)
                {
                    grid.AddLine(new Vector3(i, min, 0), new Vector3(i, max, 0));
                    grid.AddLine(new Vector3(min, i, 0), new Vector3(max, i, 0));
                }
            }

            return grid.ToLineGeometry3D();
        }