示例#1
0
        // OnLoad() is called when the GameObject is added to the IGameObjectService.
        protected override void OnLoad()
        {
            var contentManager = _services.GetInstance <ContentManager>();

            _modelNode           = contentManager.Load <ModelNode>(_assetName).Clone();
            _modelNode.PoseWorld = _defaultPose;
            SampleHelper.EnablePerPixelLighting(_modelNode);

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

            scene.Children.Add(_modelNode);

            // Create looping animation.
            var meshNode   = (MeshNode)_modelNode.Children[0]; // The dude model has a single mesh node as its child.
            var animations = meshNode.Mesh.Animations;
            var timeline   = new TimelineClip(animations.Values.First())
            {
                Duration     = TimeSpan.MaxValue,
                LoopBehavior = LoopBehavior.Cycle,
            };

            // Start animation.
            var animationService = _services.GetInstance <IAnimationService>();

            AnimationController = animationService.StartAnimation(timeline, (IAnimatableProperty)meshNode.SkeletonPose);
            AnimationController.UpdateAndApply();
        }
示例#2
0
        // OnLoad() is called when the GameObject is added to the IGameObjectService.
        protected override void OnLoad()
        {
            var contentManager = _services.GetInstance <ContentManager>();

            // A rusty barrel with multiple levels of detail (LODs).
            _rigidBody  = new RigidBody(new CylinderShape(0.35f, 1));
            _modelNode0 = contentManager.Load <ModelNode>("Barrel/Barrel").Clone();
            SampleHelper.EnablePerPixelLighting(_modelNode0);

            // Mark the LOD nodes with UserFlags = 1.
            _modelNode0.GetDescendants()
            .OfType <LodGroupNode>()
            .SelectMany(lodGroupNode => lodGroupNode.Levels)
            .Select(level => level.Node)
            .ForEach(node =>
            {
                node.UserFlags = 1;
            });

            // Add a second model where each LOD has a different color.
            _modelNode1 = contentManager.Load <ModelNode>("Barrel/Barrel_Colored").Clone();
            SampleHelper.EnablePerPixelLighting(_modelNode1);

            // Mark the LOD nodes with UserFlags = 2.
            _modelNode1.GetDescendants()
            .OfType <LodGroupNode>()
            .SelectMany(lodGroupNode => lodGroupNode.Levels)
            .Select(level => level.Node)
            .ForEach(node =>
            {
                node.UserFlags = 2;
            });


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

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

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

            simulation.RigidBodies.Add(_rigidBody);

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

            scene.Children.Add(_modelNode0);
            scene.Children.Add(_modelNode1);
        }
示例#3
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);
        }