コード例 #1
0
        public DualTextureEffectSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            SampleFramework.IsMouseVisible = false;
            GraphicsScreen.ClearBackground = true;
            GraphicsScreen.BackgroundColor = Color.CornflowerBlue;
            SetCamera(new Vector3F(8, 6, 8), ConstantsF.PiOver4, -0.4f);

            // Load the dual-textured model. This model is processed using the DigitalRune
            // Model Processor - not the default XNA model processor!
            // In the folder that contains model_lightmap.fbx, there are several XML files (*.drmdl and *.drmat)
            // which define the materials of the model. These material description files are
            // automatically processed by the DigitalRune Model Processor. Please browse
            // to the content folder and have a look at the *.drmdl and *.drmat files.
            // The model itself is a tree of scene nodes. This model contains several
            // mesh nodes for the floor plane and the cubes.
            ModelNode modelNode = ContentManager.Load <ModelNode>("DualTextured/model_lightmap");

            // The XNA ContentManager manages a single instance of each model. We clone
            // the models, to get a copy that we can modify without changing the original
            // instance. - This is good practice, even if we do not currently modify the
            // model in this sample.
            modelNode = modelNode.Clone();

            modelNode.ScaleLocal = new Vector3F(0.5f);

            // Add the model to the scene.
            GraphicsScreen.Scene.Children.Add(modelNode);
        }
コード例 #2
0
        public SkinnedEffectSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            GraphicsScreen.ClearBackground = true;
            GraphicsScreen.BackgroundColor = Color.CornflowerBlue;
            SetCamera(new Vector3F(1, 1, 3), 0.2f, 0);

            // Create a sky mesh and add an instance of this mesh to the scene.
            var skyMesh = ProceduralSkyDome.CreateMesh(GraphicsService, ContentManager.Load <Texture2D>("sky"));

            _sky      = new MeshNode(skyMesh);
            _sky.Name = "Sky"; // Always set a name - very useful for debugging!
            GraphicsScreen.Scene.Children.Add(_sky);

            // Load the skinned model. This model is processed using the DigitalRune Model
            // Processor - not the default XNA model processor!
            // In the folder that contains dude.fbx, there are several XML files (*.drmdl and *.drmat)
            // which define the materials of the model. These material description files are
            // automatically processed by the DigitalRune Model Processor. Please browse
            // to the content folder and have a look at the *.drmdl and *.drmat files.
            _dudeModel           = ContentManager.Load <ModelNode>("Dude/Dude");
            _dudeModel           = _dudeModel.Clone();
            _dudeModel.PoseWorld = new Pose(Matrix33F.CreateRotationY(ConstantsF.Pi));
            GraphicsScreen.Scene.Children.Add(_dudeModel);

            // The dude model consists of a single mesh.
            var dudeMeshNode = _dudeModel.GetSubtree().OfType <MeshNode>().First();
            var mesh         = dudeMeshNode.Mesh;

            /*
             * // The dude mesh consists of different materials (head, eyes, torso, ...).
             * // We could change some of the material properties...
             * foreach (var material in mesh.Materials)
             * {
             *  // Get all SkinnedEffectBindings which wrap the XNA SkinnedEffect.
             *  // A material can consist of several effects - one effect for each render pass.
             *  // (Per default there is only one render pass called "Default".)
             *  foreach (var effectBinding in material.EffectBindings.OfType<SkinnedEffectBinding>())
             *  {
             *    // We could change effect parameters here, for example:
             *    effectBinding.PreferPerPixelLighting = true;
             *  }
             * }
             */

            // The DigitalRune Model Processor also loads animations.
            // Start the first animation of the dude and let it loop forever.
            // (We keep the animation controller to be able to stop the animation in
            // Dispose() below.)
            var timeline = new TimelineClip(mesh.Animations.Values.First())
            {
                Duration     = TimeSpan.MaxValue,
                LoopBehavior = LoopBehavior.Cycle,
            };

            _animationController = AnimationService.StartAnimation(timeline, (IAnimatableProperty)dudeMeshNode.SkeletonPose);
        }
コード例 #3
0
        private void InitializeModel()
        {
            Debug.Assert(GraphicsScreens.Count != 0, "Initialize graphics screens before calling InitializeModel().");

            if (Document.ModelNode != null)
            {
                ModelNode = Document.ModelNode;

                // The first view model can use the original model node. All other view models
                // use clones because we cannot put one model node into several scenes.
                if (Document.ViewModels[0] != this)
                {
                    ModelNode = ModelNode.Clone();
                }

                if (Document.HasAnimations)
                {
                    // We want to animate all clones together. --> Make all clones use the skeleton
                    // pose instance of the original mesh node.
                    var originalMeshNode = Document.ModelNode.GetDescendants().OfType <MeshNode>().FirstOrDefault();
                    var clonedMeshNode   = ModelNode.GetDescendants().OfType <MeshNode>().FirstOrDefault();
                    if (originalMeshNode != null && clonedMeshNode != null)
                    {
                        clonedMeshNode.SkeletonPose = originalMeshNode.SkeletonPose;
                    }

                    HasSkeleton = true;
                }
                else
                {
                    HasSkeleton = false;
                }

                var scene = UseDeferredLighting
                            ? ((DeferredGraphicsScreen)GraphicsScreens[0]).Scene
                            : ((BasicGraphicsScreen)GraphicsScreens[0]).Scene;
                scene.Children.Add(ModelNode);
            }
            else if (Document.Model != null)
            {
                HasSkeleton = false;
                ((BasicGraphicsScreen)GraphicsScreens[0]).Model = Document.Model;
            }
        }
コード例 #4
0
        public EnvironmentMapEffectSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            GraphicsScreen.ClearBackground = true;
            GraphicsScreen.BackgroundColor = Color.CornflowerBlue;
            SetCamera(new Vector3F(8, 6, 8), ConstantsF.PiOver4, -0.4f);

            // Load the saucer model. This model is processed using the DigitalRune Model
            // Processor - not the default XNA model processor!
            // In the folder that contains saucer.fbx, there are several XML files (*.drmdl and *.drmat)
            // which define the materials of the model. These material description files are
            // automatically processed by the DigitalRune Model Processor. Please browse
            // to the content folder and have a look at the *.drmdl and *.drmat files.
            _model = ContentManager.Load <ModelNode>("EnvironmentMapped/saucer");

            // The XNA ContentManager manages a single instance of each model. We clone
            // the models, to get a copy that we can modify without changing the original
            // instance.
            _model = _model.Clone();

            // Position the model and add it to the scene.
            _model.PoseWorld  = new Pose(RandomHelper.Random.NextQuaternionF());
            _model.ScaleLocal = new Vector3F(0.4f);
            GraphicsScreen.Scene.Children.Add(_model);

            //// Here is another example showing how you can change material properties
            //// of the model:
            //// Get the mesh (the saucer model contains only a single mesh node).
            //MeshNode meshNode = _model.GetDescendants().OfType<MeshNode>().First();
            //Mesh mesh = meshNode.Mesh;
            //// Get the second material (which is the material of the cockpit).
            //Material material = mesh.Materials[1];
            //// Get the effect used in the "Default" render pass.
            //EffectBinding effectBindings = material["Default"];
            //// Change some parameters.
            //effectBindings.Set("EnvironmentMapAmount", 1f);
            //effectBindings.Set("FresnelFactor", 0.5f);
        }
コード例 #5
0
        public void Spawn()
        {
            var scene      = _services.GetInstance <IScene>();
            var simulation = _services.GetInstance <Simulation>();

            // Create a new instance by cloning the prototype.
            var model = _modelPrototype.Clone();
            var body  = _bodyPrototype.Clone();

            // Spawn at random position.
            var randomPosition = new Vector3F(
                RandomHelper.Random.NextFloat(-10, 10),
                RandomHelper.Random.NextFloat(2, 5),
                RandomHelper.Random.NextFloat(-20, 0));

            body.Pose       = new Pose(randomPosition, RandomHelper.Random.NextQuaternionF());
            model.PoseWorld = _bodyPrototype.Pose;
            scene.Children.Add(model);
            simulation.RigidBodies.Add(body);

            _models.Add(model);
            _bodies.Add(body);
        }
コード例 #6
0
        public BasicEffectSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            GraphicsScreen.ClearBackground = true;
            GraphicsScreen.BackgroundColor = Color.CornflowerBlue;
            SetCamera(new Vector3F(8, 6, 8), ConstantsF.PiOver4, -0.4f);

            // Load the models. The models are processed using the DigitalRune Model
            // Processor - not the default XNA model processor!
            // In the folder that contains tank.fbx, there is an XML file tank.drmdl which defines
            // properties of the model. These XML files are automatically processed by
            // the DigitalRune Model Processor. Please browse to the content folder and
            // have a look at the *.drmdl file. For the grid model there is no such file but
            // the DigitalRune model content processor will create one automatically with the default
            // materials found in the model.
            // Each model itself is a tree of scene nodes. The grid model
            // contains one mesh node. The tank model contains several mesh nodes (turret,
            // cannon, hatch, wheels, ...).
            _grid = ContentManager.Load <ModelNode>("Ground/Ground");
            _tank = ContentManager.Load <ModelNode>("Tank/tank");

            // The XNA ContentManager manages a single instance of each model. We clone
            // the models, to get a copy that we can modify without changing the original
            // instance.
            // Cloning is fast because it only duplicates the scene nodes - but not the
            // mesh and material information.
            _grid = _grid.Clone();
            _tank = _tank.Clone();

            // The grid is a bit too large. We can scale it to make it smaller.
            _grid.ScaleLocal = new Vector3F(0.3f);

            // No need to scale the tank model - the tank was already scaled by the
            // DigitalRune Model Processor because a scale factor is defined in the
            // Tank.drmdl file.

            // Add the models to the scene.
            GraphicsScreen.Scene.Children.Add(_grid);
            GraphicsScreen.Scene.Children.Add(_tank);

            /*
             * // If you want to turn off some of the default lights, you can get them by
             * // their name and change IsEnabled flags.
             * var keyLight = (LightNode)_graphicsScreen.Scene.GetSceneNode("KeyLight");
             * var fillLight = (LightNode)_graphicsScreen.Scene.GetSceneNode("FillLight");
             * fillLight.IsEnabled = false;
             * var backLight = (LightNode)_graphicsScreen.Scene.GetSceneNode("BackLight");
             * backLight.IsEnabled = false;
             */

            /*
             * // If you want to change the material properties of the tank, you can do this:
             * // Loop through all mesh nodes of the tank.
             * foreach (var meshNode in _tank.GetSubtree().OfType<MeshNode>())
             * {
             *  // Loop through all materials of this mesh (each mesh can consist of several
             *  // submeshes with different materials).
             *  foreach (var material in meshNode.Mesh.Materials)
             *  {
             *    // Get all BasicEffectBindings which wrap the XNA BasicEffect.
             *    // A material can consist of several effects - one effect for each render
             *    // pass. (Per default there is only one render pass called "Default".)
             *    foreach (var effectBinding in material.EffectBindings.OfType<BasicEffectBinding>())
             *    {
             *      effectBinding.PreferPerPixelLighting = true;
             *      effectBinding.Set("SpecularColor", new Vector3(1, 0, 0));
             *    }
             *  }
             * }
             */

            CreateAndStartAnimations();
        }
コード例 #7
0
        public MeshNodeSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            SampleFramework.IsMouseVisible = false;
            var delegateGraphicsScreen = new DelegateGraphicsScreen(GraphicsService)
            {
                RenderCallback = Render,
            };

            GraphicsService.Screens.Insert(0, delegateGraphicsScreen);

            // Add a custom game object which controls the camera.
            _cameraObject = new CameraObject(Services);
            GameObjectService.Objects.Add(_cameraObject);

            // For advanced users: Set this flag if you want to analyze the imported opaque data of
            // effect bindings.
            //EffectBinding.KeepOpaqueData = true;

            // Load a model. The model is processed using the DigitalRune Model Processor - not
            // the default XNA model processor!
            // In the folder that contains tank.fbx, there is an XML file tank.drmdl which defines
            // properties of the model. These XML files are automatically processed by the
            // DigitalRune Model Processor.
            // Each model itself is a tree of scene nodes. The grid model contains one mesh
            // node. The tank model contains several mesh nodes (turret, cannon, hatch,
            // wheels, ...).
            _model = ContentManager.Load <ModelNode>("Tank/tank");

            // The XNA ContentManager manages a single instance of each model. We clone
            // the model, to get a copy that we can modify without changing the original
            // instance. Cloning is fast because it only duplicates the scene nodes - but
            // not the mesh and material information.
            _model = _model.Clone();

            // _model is the root of a tree of scene nodes. The mesh nodes are the child
            // nodes. When we scale or move the _model, we automatically scale and move
            // all child nodes.
            _model.ScaleLocal = new Vector3F(0.8f);
            _model.PoseWorld  = new Pose(new Vector3F(0, 0, -2), Matrix33F.CreateRotationY(-0.3f));

            // Let's loop through the mesh nodes of the model:
            foreach (var meshNode in _model.GetSubtree().OfType <MeshNode>())
            {
                // Each MeshNode references a Mesh.
                Mesh mesh = meshNode.Mesh;

                // The mesh consists of several submeshes and several materials - usually
                // one material per submesh, but several submeshes could reference the same
                // materials.

                // Let's loop through the materials of this mesh.
                foreach (var material in mesh.Materials)
                {
                    // A material is a collection of EffectBindings - one EffectBinding for each
                    // render pass. For example, a complex game uses several render passes, like
                    // a pre-Z pass, a G-buffer pass, a shadow map pass, a deferred material pass,
                    // etc.In simple games there is only one pass which is called "Default".
                    var effectBinding = material["Default"];

                    // An EffectBinding references an Effect (the XNA Effect class) and it has
                    // "parameter bindings" and "technique bindings". These bindings select the
                    // values for the shader parameters when the mesh node is rendered.

                    // Let's change the binding for the DiffuseColor of the shader to give tank
                    // a red color.
                    effectBinding.Set("DiffuseColor", new Vector4(1, 0.7f, 0.7f, 1));

                    // The tank uses the default effect binding which is a BasicEffectBinding - this
                    // effect binding uses the XNA BasicEffect.
                    // In this sample we do not define any lights, therefore we disable the lighting
                    // in the shader.
                    ((BasicEffectBinding)effectBinding).LightingEnabled = false;
                }
            }

            _meshRenderer = new MeshRenderer();

            var spriteFont = UIContentManager.Load <SpriteFont>("UI Themes/BlendBlue/Default");

            _debugRenderer = new DebugRenderer(GraphicsService, spriteFont);
        }
コード例 #8
0
        // OnLoad() is called when the GameObject is added to the IGameObjectService.
        protected override void OnLoad()
        {
            var contentManager = _services.GetInstance <ContentManager>();

            if (_type == 1)
            {
                // A simple cube.
                _rigidBody = new RigidBody(new BoxShape(1, 1, 1));
                _modelNode = contentManager.Load <ModelNode>("RustyCube/RustyCube").Clone();
            }
            else if (_type == 2)
            {
                // Another simple cube.
                _rigidBody = new RigidBody(new BoxShape(1, 1, 1));
                _modelNode = contentManager.Load <ModelNode>("MetalGrateBox/MetalGrateBox").Clone();
            }
            else if (_type == 3)
            {
                // A TV-like box.
                _rigidBody = new RigidBody(new BoxShape(1, 0.6f, 0.8f))
                {
                    UserData = "TV"
                };
                _modelNode = contentManager.Load <ModelNode>("TVBox/TVBox");

                if (_modelNode.Children.OfType <LightNode>().Count() == 0)
                {
                    // This is the first time the "TVBox" is loaded.

                    // Add a projector light to the model that projects the TV screen. The
                    // TV screen is the emissive part of the TV mesh.
                    var meshNode = _modelNode.Children.OfType <MeshNode>().First();
                    var material = meshNode.Mesh.Materials.First(m => m.Name == "TestCard");

                    // Get texture from material.
                    // Note: In XNA the effect parameter type is Texture. In MonoGame it is Texture2D.
                    Texture2D texture;
                    EffectParameterBinding parameterBinding = material["Material"].ParameterBindings["EmissiveTexture"];
                    if (parameterBinding is EffectParameterBinding <Texture> )
                    {
                        texture = (Texture2D)((EffectParameterBinding <Texture>)parameterBinding).Value;
                    }
                    else
                    {
                        texture = ((EffectParameterBinding <Texture2D>)parameterBinding).Value;
                    }

                    var projection = new PerspectiveProjection();
                    projection.Near = 0.55f;
                    projection.Far  = 3.0f;
                    projection.SetFieldOfView(MathHelper.ToRadians(60), 0.76f / 0.56f);

                    var projectorLight = new ProjectorLight(texture, projection);
                    projectorLight.Attenuation = 4;
                    var projectorLightNode = new LightNode(projectorLight);
                    projectorLightNode.LookAt(new Vector3F(0, 0.2f, 0), Vector3F.Zero, Vector3F.UnitZ);

                    // Attach the projector light to the model.
                    _modelNode.Children.Add(projectorLightNode);
                }

                _modelNode = _modelNode.Clone();
            }
            else if (_type == 4)
            {
                // A "magic" sphere with a colored point light.
                _rigidBody = new RigidBody(new SphereShape(0.25f));
                _modelNode = contentManager.Load <ModelNode>("MagicSphere/MagicSphere");

                if (_modelNode.Children.OfType <LightNode>().Count() == 0)
                {
                    // This is the first time the "MagicSphere" is loaded.

                    // Change the size of the sphere.
                    var meshNode = _modelNode.Children.OfType <MeshNode>().First();
                    meshNode.ScaleLocal = new Vector3F(0.5f);

                    // Disable shadows. (The sphere acts as a light source.)
                    meshNode.CastsShadows = false;

                    // Add a point light.
                    var pointLight = new PointLight
                    {
                        Color             = new Vector3F(1, 1, 1),
                        DiffuseIntensity  = 4,
                        SpecularIntensity = 4,
                        Range             = 3,
                        Attenuation       = 1,
                        Texture           = contentManager.Load <TextureCube>("MagicSphere/ColorCube"),
                    };
                    var pointLightNode = new LightNode(pointLight)
                    {
                        // The point light uses shadow mapping to cast an omnidirectional shadow.
                        Shadow = new CubeMapShadow
                        {
                            PreferredSize   = 64,
                            DepthBiasScale  = 0.9f,
                            DepthBiasOffset = -0.01f,
                        }
                    };

                    _modelNode.Children.Add(pointLightNode);
                }

                _modelNode = _modelNode.Clone();
            }
            else if (_type == 5)
            {
                // A sphere of glass (or "bubble").
                _rigidBody = new RigidBody(new SphereShape(0.3f));
                _modelNode = contentManager.Load <ModelNode>("Bubble/Bubble").Clone();
                _modelNode.GetDescendants().OfType <MeshNode>().First().ScaleLocal = new Vector3F(0.3f);
            }
            else if (_type == 6)
            {
                // A rusty barrel with multiple levels of detail (LODs).
                _rigidBody = new RigidBody(new CylinderShape(0.35f, 1));
                _modelNode = contentManager.Load <ModelNode>("Barrel/Barrel").Clone();
            }
            else
            {
                // A cube consisting of a frame and transparent sides.
                _rigidBody = new RigidBody(new BoxShape(1, 1, 1));
                _modelNode = contentManager.Load <ModelNode>("GlassBox/GlassBox").Clone();
            }

            SampleHelper.EnablePerPixelLighting(_modelNode);

            // Set a random pose.
            var randomPosition = new Vector3F(
                RandomHelper.Random.NextFloat(-10, 10),
                RandomHelper.Random.NextFloat(2, 5),
                RandomHelper.Random.NextFloat(-20, 0));

            _rigidBody.Pose      = new Pose(randomPosition, RandomHelper.Random.NextQuaternionF());
            _modelNode.PoseWorld = _rigidBody.Pose;

            // Add rigid body to physics simulation and model to scene.
            var simulation = _services.GetInstance <Simulation>();

            simulation.RigidBodies.Add(_rigidBody);

            var scene = _services.GetInstance <IScene>();

            scene.Children.Add(_modelNode);
        }