示例#1
0
        private void Viewport_OnStartRendering(object sender, EventArgs e)
        {
            bool isGoingOut   = true;
            bool isAddingNode = false;

            RenderLoop.Run(window, () =>
            {
                if (resizeRequested)
                {
                    viewport.Resize(window.ClientSize.Width, window.ClientSize.Height);
                    resizeRequested = false;
                    return;
                }
                var pos      = camera.Position;
                var t        = Stopwatch.GetTimestamp();
                var elapse   = t - previousTime;
                previousTime = t;
                cameraController.OnTimeStep();

                viewport.Render();


#if TESTADDREMOVE
                if (groupSphere.Items.Count > 0 && !isAddingNode)
                {
                    groupSphere.RemoveChildNode(groupSphere.Items.First());
                    if (groupSphere.Items.Count == 0)
                    {
                        isAddingNode = true;
                        Console.WriteLine($"{effectsManager.GetResourceCountSummary()}");
                        groupSphere.AddChildNode(groupBox);
                        groupSphere.AddChildNode(groupPoints);
                        groupPoints.AddChildNode(groupLines);
                    }
                }
                else
                {
                    var materialCount = materialList.Length;
                    var transform     = Matrix.Translation(new Vector3(rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50)));
                    groupSphere.AddChildNode(new MeshNode()
                    {
                        Geometry = box, Material = materialList[groupSphere.Items.Count % materialCount], ModelMatrix = transform, CullMode = SharpDX.Direct3D11.CullMode.Back
                    });
                    transform = Matrix.Translation(new Vector3(rnd.NextFloat(-20, 20), rnd.NextFloat(-20, 20), rnd.NextFloat(-20, 20)));
                    groupSphere.AddChildNode(new MeshNode()
                    {
                        Geometry = sphere, Material = materialList[groupSphere.Items.Count % materialCount], ModelMatrix = transform, CullMode = SharpDX.Direct3D11.CullMode.Back
                    });
                    if (groupSphere.Items.Count > NumItems)
                    {
                        isAddingNode = false;
                    }
                }
#endif
            });
        }
示例#2
0
        private static void LoadModel(GroupNode node, bool renderEnvironmentMap)
        {
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Filter = HelixToolkit.SharpDX.Core.Assimp.Importer.SupportedFormatsString;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                var path = dialog.FileName;
                exception   = "";
                currentTime = Stopwatch.GetTimestamp();
                loading     = true;
                modelName   = Path.GetFileName(path);
                Task.Run(() =>
                {
                    var importer = new Importer();
                    return(importer.Load(path));
                }).ContinueWith((x) =>
                {
                    loading = false;
                    if (x.IsCompleted && x.Result != null)
                    {
                        node.Clear();
                        foreach (var model in x.Result.Root.Traverse())
                        {
                            if (model is MeshNode mesh)
                            {
                                if (mesh.Material is PBRMaterialCore pbr)
                                {
                                    pbr.RenderEnvironmentMap = renderEnvironmentMap;
                                }
                                else if (mesh.Material is PhongMaterialCore phong)
                                {
                                    phong.RenderEnvironmentMap = renderEnvironmentMap;
                                }
                            }
                        }
                        node.AddChildNode(x.Result.Root);
                        scene = x.Result;
                        if (scene.Animations != null && scene.Animations.Count > 0)
                        {
                            animationUpdaters        = scene.Animations.CreateAnimationUpdaters().Values.ToArray();
                            animationSelection       = new bool[animationUpdaters.Count];
                            animationNames           = animationUpdaters.Select((ani) => ani.Name).ToArray();
                            currentSelectedAnimation = -1;
                        }
                    }
                    else if (x.Exception != null)
                    {
                        exception = x.Exception.Message;
                    }
                }, TaskScheduler.FromCurrentSynchronizationContext());
            }
        }
示例#3
0
        public static IEnumerable <GroupNode> LoadMapMGEO(MGEOFile mgeo, MapData mapData, string mapPath)
        {
            foreach (MGEOObject mgeoModel in mgeo.Objects)
            {
                IntCollection     indices  = new IntCollection(mgeoModel.Indices.Select(x => (int)x).AsEnumerable());
                Vector3Collection vertices = new Vector3Collection(mgeoModel.Vertices.Count);
                Vector2Collection uvs      = new Vector2Collection(mgeoModel.Vertices.Count);
                foreach (MGEOVertex vertex in mgeoModel.Vertices)
                {
                    vertices.Add(new dxVector3(vertex.Position.X, vertex.Position.Y, vertex.Position.Z));
                    uvs.Add(new dxVector2(vertex.DiffuseUV.X, vertex.DiffuseUV.Y));
                }

                foreach (MGEOSubmesh submesh in mgeoModel.Submeshes)
                {
                    GroupNode groupNode = new GroupNode()
                    {
                        Name = mgeoModel.Name
                    };

                    MeshGeometry3D submeshGeometry3D = new MeshGeometry3D()
                    {
                        Indices            = indices.GetRange((int)submesh.StartIndex, (int)submesh.IndexCount) as IntCollection,
                        Positions          = vertices,
                        TextureCoordinates = uvs
                    };

                    DiffuseMaterial diffuseMaterial = new DiffuseMaterial()
                    {
                        Name        = submesh.Material,
                        DiffuseMap  = CreateMaterial(submesh.Material, mapData, mapPath),
                        EnableUnLit = true
                    };

                    groupNode.AddChildNode(new MeshNode()
                    {
                        Name     = mgeoModel.Name + "|" + submesh.Material,
                        Geometry = submeshGeometry3D,
                        Material = diffuseMaterial
                    });

                    yield return(groupNode);
                }
            }
        }
示例#4
0
        private void GenerateSomeMesh(GroupNode root)
        {
            var builder = new MeshBuilder();

            builder.AddSphere(Vector3.Zero, 5);
            var mesh      = builder.ToMesh();
            var numSphere = random.Next(50, 100);

            for (int i = 0; i < numSphere; ++i)
            {
                var meshNode = new MeshNode()
                {
                    Geometry = mesh,
                    Material = new PhongMaterialCore()
                    {
                        DiffuseColor = random.NextColor()
                    },
                    ModelMatrix = Matrix.Translation(random.NextVector3(new Vector3(-50, -50, -50), new Vector3(50, 50, 50)))
                };
                root.AddChildNode(meshNode);
            }
        }
示例#5
0
        public static SceneNodeGroupModel3D LoadMapMGEO(MGEOFile mgeo)
        {
            List <MeshGeometryModel3D> meshGeometryModels = GenerateMeshGeometryModels(mgeo);
            SceneNodeGroupModel3D      mainModelGroup     = new SceneNodeGroupModel3D();

            foreach (MeshGeometryModel3D meshGeometryModel in meshGeometryModels)
            {
                GroupNode groupNode = new GroupNode()
                {
                    Name = meshGeometryModel.Name
                };

                groupNode.AddChildNode(new MeshNode()
                {
                    Name     = meshGeometryModel.Name,
                    Geometry = meshGeometryModel.Geometry,
                    Material = meshGeometryModel.Material,
                });

                mainModelGroup.AddNode(groupNode);
            }

            return(mainModelGroup);
        }
示例#6
0
        private void BuildImageGrid()
        {
            // Create a root node for the grid
            GroupNode = SCNNode.Create();

            // Retrieve the template node to replicate
            var scene        = SCNScene.FromFile("Contacts/contact");
            var templateNode = scene.RootNode.FindChildNode("people", true);

            for (int k = 0, j = 0; j < RowCount; j++)
            {
                for (var i = 0; i < ColumnCount; i++, k++)
                {
                    // Hierarchy : __groupNode > container > node
                    var container = SCNNode.Create();
                    var node      = templateNode.Clone();
                    node.Name = "contact" + k;

                    GroupNode.AddChildNode(container);
                    container.AddChildNode(node);

                    if (k == 28)
                    {
                        HeroNode = node;
                    }

                    // Curved layout
                    var angle = 0.12f * ((ColumnCount - 1) / 2.0f - i);
                    var x     = NMath.Cos(angle + (float)(Math.PI / 2)) * 500.0f;
                    var z     = NMath.Sin(angle + (float)(Math.PI / 2)) * 500.0f;
                    container.Position = new SCNVector3(x, j * 60, -z + 400);
                    container.Rotation = new SCNVector4(0, 1, 0, angle);

                    // We want a different image on each elemement and to do that we need to
                    // unshare the geometry first and then unshare the material

                    var geometryNode = node.ChildNodes [0];
                    geometryNode.Geometry = (SCNGeometry)geometryNode.Geometry.Copy();

                    var materialCopy = (SCNMaterial)geometryNode.Geometry.Materials [1].Copy();
                    materialCopy.Diffuse.Contents = new NSImage(NSBundle.MainBundle.PathForResource("Contacts/contact" + (k % ContactImageCount), "jpg"));
                    geometryNode.Geometry.ReplaceMaterial(1, materialCopy);

                    // Animate (rotate forever)
                    var animation = CAKeyFrameAnimation.GetFromKeyPath("rotation");
                    animation.Duration = 4.0f;
                    animation.KeyTimes = new NSNumber[] { 0.0f, 0.3f, 1.0f };
                    animation.Values   = new NSObject[] { NSValue.FromVector(new SCNVector4(0, 1, 0, 0)),
                                                          NSValue.FromVector(new SCNVector4(0, 1, 0, (float)(Math.PI * 2))),
                                                          NSValue.FromVector(new SCNVector4(0, 1, 0, (float)(Math.PI * 2))) };

                    var tf = CAMediaTimingFunction.FromName(CAMediaTimingFunction.EaseInEaseOut);
                    animation.TimingFunctions = new CAMediaTimingFunction[] { tf, tf, tf };
                    animation.RepeatCount     = float.MaxValue;
                    animation.BeginTime       = CAAnimation.CurrentMediaTime() + 1.0f + j * 0.1f + i * 0.05f;                // desynchronize the animations
                    node.AddAnimation(animation, new NSString("animation"));
                }
            }

            // Add the group to the scene
            GroupNode.Scale    = new SCNVector3(0.03f, 0.03f, 0.03f);
            GroupNode.Position = new SCNVector3(0, Altitude - 2.8f, 18);
            GroupNode.Opacity  = 0.0f;

            GroundNode.AddChildNode(GroupNode);
        }
示例#7
0
        private void InitializeScene()
        {
            camera = new PerspectiveCameraCore()
            {
                LookDirection     = new Vector3(0, 0, 50),
                Position          = new Vector3(0, 0, -50),
                FarPlaneDistance  = 1000,
                NearPlaneDistance = 0.1f,
                FieldOfView       = 45,
                UpDirection       = new Vector3(0, 1, 0)
            };
            viewport.CameraCore = camera;
            viewport.Items.Add(new DirectionalLightNode()
            {
                Direction = new Vector3(0, -1, 1), Color = Color.White
            });
            viewport.Items.Add(new PointLightNode()
            {
                Position = new Vector3(0, 0, -20), Color = Color.Yellow, Range = 20, Attenuation = Vector3.One
            });

            var builder = new MeshBuilder(true, true, true);

            builder.AddSphere(Vector3.Zero, 1, 12, 12);
            sphere  = builder.ToMesh();
            builder = new MeshBuilder(true, true, true);
            builder.AddBox(Vector3.Zero, 1, 1, 1);
            box    = builder.ToMesh();
            points = new PointGeometry3D()
            {
                Positions = sphere.Positions
            };
            var lineBuilder = new LineBuilder();

            lineBuilder.AddBox(Vector3.Zero, 2, 2, 2);
            lines       = lineBuilder.ToLineGeometry3D();
            groupSphere = new GroupNode();
            groupBox    = new GroupNode();
            groupLines  = new GroupNode();
            groupPoints = new GroupNode();
            InitializeMaterials();
            materialList = materials.Values.ToArray();
            var materialCount = materialList.Length;

            for (int i = 0; i < NumItems; ++i)
            {
                var transform = Matrix.Translation(new Vector3(rnd.NextFloat(-20, 20), rnd.NextFloat(-20, 20), rnd.NextFloat(-20, 20)));
                groupSphere.AddChildNode(new MeshNode()
                {
                    Geometry = sphere, Material = materialList[i % materialCount], ModelMatrix = transform, CullMode = SharpDX.Direct3D11.CullMode.Back
                });
            }

            for (int i = 0; i < NumItems; ++i)
            {
                var transform = Matrix.Translation(new Vector3(rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50)));
                groupBox.AddChildNode(new MeshNode()
                {
                    Geometry = box, Material = materialList[i % materialCount], ModelMatrix = transform, CullMode = SharpDX.Direct3D11.CullMode.Back
                });
            }

            //for(int i=0; i< NumItems; ++i)
            //{
            //    var transform = Matrix.Translation(new Vector3(rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50)));
            //    groupPoints.AddChildNode(new PointNode() { Geometry = points, ModelMatrix = transform, Material = new PointMaterialCore() { PointColor = Color.Red } });
            //}

            //for (int i = 0; i < NumItems; ++i)
            //{
            //    var transform = Matrix.Translation(new Vector3(rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50)));
            //    groupLines.AddChildNode(new LineNode() { Geometry = lines, ModelMatrix = transform, Material = new LineMaterialCore() { LineColor = Color.LightBlue } });
            //}

            viewport.Items.Add(groupSphere);
            groupSphere.AddChildNode(groupBox);
            groupSphere.AddChildNode(groupPoints);
            groupSphere.AddChildNode(groupLines);

            var viewbox = new ViewBoxNode();

            viewport.Items.Add(viewbox);
            var imGui = new ImGuiNode();

            viewport.Items.Add(imGui);
            imGui.UpdatingImGuiUI       += ImGui_UpdatingImGuiUI;
            io.KeyMap[GuiKey.Tab]        = (int)Keys.Tab;
            io.KeyMap[GuiKey.LeftArrow]  = (int)Keys.Left;
            io.KeyMap[GuiKey.RightArrow] = (int)Keys.Right;
            io.KeyMap[GuiKey.UpArrow]    = (int)Keys.Up;
            io.KeyMap[GuiKey.DownArrow]  = (int)Keys.Down;
            io.KeyMap[GuiKey.PageUp]     = (int)Keys.PageUp;
            io.KeyMap[GuiKey.PageDown]   = (int)Keys.PageDown;
            io.KeyMap[GuiKey.Home]       = (int)Keys.Home;
            io.KeyMap[GuiKey.End]        = (int)Keys.End;
            io.KeyMap[GuiKey.Delete]     = (int)Keys.Delete;
            io.KeyMap[GuiKey.Backspace]  = (int)Keys.Back;
            io.KeyMap[GuiKey.Enter]      = (int)Keys.Enter;
            io.KeyMap[GuiKey.Escape]     = (int)Keys.Escape;
        }
        private void InitializeScene()
        {
            camera = new PerspectiveCameraCore()
            {
                LookDirection     = new Vector3(0, 0, 50),
                Position          = new Vector3(0, 0, -50),
                FarPlaneDistance  = 1000,
                NearPlaneDistance = 0.1f,
                FieldOfView       = 45,
                UpDirection       = new Vector3(0, 1, 0)
            };
            viewport.CameraCore = camera;
            viewport.Items.Add(new DirectionalLightNode()
            {
                Direction = new Vector3(0, -1, 1), Color = Color.White
            });
            viewport.Items.Add(new PointLightNode()
            {
                Position = new Vector3(0, 0, -20), Color = Color.Yellow, Range = 20, Attenuation = Vector3.One
            });

            var builder = new MeshBuilder(true, true, true);

            builder.AddSphere(Vector3.Zero, 1, 12, 12);
            sphere  = builder.ToMesh();
            builder = new MeshBuilder(true, true, true);
            builder.AddBox(Vector3.Zero, 1, 1, 1);
            box    = builder.ToMesh();
            points = new PointGeometry3D()
            {
                Positions = sphere.Positions
            };
            var lineBuilder = new LineBuilder();

            lineBuilder.AddBox(Vector3.Zero, 2, 2, 2);
            lines       = lineBuilder.ToLineGeometry3D();
            groupSphere = new GroupNode();
            groupBox    = new GroupNode();
            groupLines  = new GroupNode();
            groupPoints = new GroupNode();
            InitializeMaterials();
            materialList = materials.Values.ToArray();
            var materialCount = materialList.Length;

            for (int i = 0; i < NumItems; ++i)
            {
                var transform = Matrix.Translation(new Vector3(rnd.NextFloat(-20, 20), rnd.NextFloat(-20, 20), rnd.NextFloat(-20, 20)));
                groupSphere.AddChildNode(new MeshNode()
                {
                    Geometry = sphere, Material = materialList[i % materialCount], ModelMatrix = transform, CullMode = SharpDX.Direct3D11.CullMode.Back
                });
            }

            for (int i = 0; i < NumItems; ++i)
            {
                var transform = Matrix.Translation(new Vector3(rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50)));
                groupBox.AddChildNode(new MeshNode()
                {
                    Geometry = box, Material = materialList[i % materialCount], ModelMatrix = transform, CullMode = SharpDX.Direct3D11.CullMode.Back
                });
            }

            for (int i = 0; i < NumItems; ++i)
            {
                var transform = Matrix.Translation(new Vector3(rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50)));
                groupPoints.AddChildNode(new PointNode()
                {
                    Geometry = points, ModelMatrix = transform, Color = Color.Red, Size = new Size2F(0.5f, 0.5f)
                });
            }

            for (int i = 0; i < NumItems; ++i)
            {
                var transform = Matrix.Translation(new Vector3(rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50)));
                groupLines.AddChildNode(new LineNode()
                {
                    Geometry = lines, ModelMatrix = transform, Color = Color.LightBlue, Thickness = 0.5f
                });
            }

            viewport.Items.Add(groupSphere);
            groupSphere.AddChildNode(groupBox);
            groupSphere.AddChildNode(groupPoints);
            groupSphere.AddChildNode(groupLines);

            var viewbox = new ViewBoxNode();

            viewport.Items.Add(viewbox);
        }
        private void Viewport_OnStartRendering(object sender, EventArgs e)
        {
            bool isGoingOut   = true;
            bool isAddingNode = false;

            RenderLoop.Run(window, () =>
            {
                if (resizeRequested)
                {
                    viewport.Resize(window.Width, window.Height);
                    resizeRequested = false;
                    return;
                }
                var pos              = camera.Position;
                var t                = Stopwatch.GetTimestamp();
                var elapse           = t - previousTime;
                previousTime         = t;
                var angle            = ((double)elapse / Stopwatch.Frequency) * 0.05;
                var camRotate        = Matrix.RotationAxis(Vector3.UnitY, (float)(angle * Math.PI));
                camera.Position      = Vector3.TransformCoordinate(pos, camRotate);
                camera.LookDirection = -camera.Position;
                if (isGoingOut)
                {
                    camera.Position += 0.05f * Vector3.Normalize(camera.Position);
                    if (camera.Position.LengthSquared() > 10000)
                    {
                        isGoingOut = false;
                    }
                }
                else
                {
                    camera.Position -= 0.05f * Vector3.Normalize(camera.Position);
                    if (camera.Position.LengthSquared() < 2500)
                    {
                        isGoingOut = true;
                    }
                }
                viewport.Render();
                viewport.InvalidateRender();
#if TESTADDREMOVE
                if (groupSphere.Items.Count > 0 && !isAddingNode)
                {
                    groupSphere.RemoveChildNode(groupSphere.Items.First());
                    if (groupSphere.Items.Count == 0)
                    {
                        isAddingNode = true;
                        Console.WriteLine($"{effectsManager.GetResourceCountSummary()}");
                        groupSphere.AddChildNode(groupBox);
                        groupSphere.AddChildNode(groupPoints);
                        groupPoints.AddChildNode(groupLines);
                    }
                }
                else
                {
                    var materialCount = materialList.Length;
                    var transform     = Matrix.Translation(new Vector3(rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50)));
                    groupSphere.AddChildNode(new MeshNode()
                    {
                        Geometry = box, Material = materialList[groupSphere.Items.Count % materialCount], ModelMatrix = transform, CullMode = SharpDX.Direct3D11.CullMode.Back
                    });
                    transform = Matrix.Translation(new Vector3(rnd.NextFloat(-20, 20), rnd.NextFloat(-20, 20), rnd.NextFloat(-20, 20)));
                    groupSphere.AddChildNode(new MeshNode()
                    {
                        Geometry = sphere, Material = materialList[groupSphere.Items.Count % materialCount], ModelMatrix = transform, CullMode = SharpDX.Direct3D11.CullMode.Back
                    });
                    if (groupSphere.Items.Count > NumItems)
                    {
                        isAddingNode = false;
                    }
                }
#endif
            });
        }