/// <summary>
        /// Adds bigger spheres with different diffuse and specular properties.
        /// </summary>
        private void AddTestSpheres()
        {
            const int NumX = 5;
            const int NumZ = 11;

            for (int x = 0; x < NumX; x++)
            {
                var mesh = SampleHelper.CreateMesh(
                    ContentManager,
                    GraphicsService,
                    new SphereShape(0.2f),
                    new Vector3F(1 - (float)x / (NumX - 1)), // Diffuse goes from 1 to 0.
                    new Vector3F((float)x / (NumX - 1)),     // Specular goes from 0 to 1.
                    (float)Math.Pow(10, x));                 // Specular power

                for (int z = 0; z < NumZ; z++)
                {
                    _graphicsScreen.Scene.Children.Add(
                        new MeshNode(mesh)
                    {
                        PoseWorld = _globalRotation * new Pose(new Vector3F(x - 2, 1.5f, 3 - z))
                    });
                }
            }
        }
        /// <summary>
        /// Adds small reflective spheres at the IBL positions to visualize the environment maps.
        /// </summary>
        private void AddLightProbes()
        {
            // Create sphere mesh with high specular exponent.
            var mesh = SampleHelper.CreateMesh(
                ContentManager,
                GraphicsService,
                new SphereShape(0.1f),
                new Vector3F(0, 0, 0),
                new Vector3F(1, 1, 1),
                1000000.0f);

            // Add a sphere under each image-based light node.
            for (int i = 0; i < _lightNodes.Length; i++)
            {
                _lightNodes[i].Children = new SceneNodeCollection(1);
                _lightNodes[i].Children.Add(new MeshNode(mesh));
            }
        }
Exemplo n.º 3
0
        // Creates a test scene with a lot of randomly placed objects.
        internal static void CreateScene(ServiceContainer services, ContentManager content, DeferredGraphicsScreen graphicsScreen)
        {
            var gameObjectService = services.GetInstance <IGameObjectService>();
            var graphicsService   = services.GetInstance <IGraphicsService>();

            gameObjectService.Objects.Add(new DynamicSkyObject(services, true, false, true)
            {
                EnableAmbientLight = false, // Disable ambient light of sky to make shadows more visible.
                EnableCloudShadows = false
            });

            gameObjectService.Objects.Add(new GroundObject(services));
            gameObjectService.Objects.Add(new DynamicObject(services, 1));
            gameObjectService.Objects.Add(new DynamicObject(services, 2));
            gameObjectService.Objects.Add(new DynamicObject(services, 3));
            gameObjectService.Objects.Add(new DynamicObject(services, 5));
            gameObjectService.Objects.Add(new DynamicObject(services, 6));
            gameObjectService.Objects.Add(new DynamicObject(services, 7));
            gameObjectService.Objects.Add(new ObjectCreatorObject(services));
            gameObjectService.Objects.Add(new LavaBallsObject(services));

            var random = new Random();

            // Spheres
            var sphereMesh = SampleHelper.CreateMesh(content, graphicsService, new SphereShape(1));

            for (int i = 0; i < 100; i++)
            {
                Vector3 position = new Vector3(random.NextFloat(-100, 100), random.NextFloat(0, 3), random.NextFloat(-100, 100));
                float   scale    = random.NextFloat(0.5f, 3f);
                var     meshNode = new MeshNode(sphereMesh)
                {
                    PoseLocal  = new Pose(position),
                    ScaleLocal = new Vector3(scale),
                    IsStatic   = true,
                };
                graphicsScreen.Scene.Children.Add(meshNode);
            }

            // Boxes
            var boxMesh = SampleHelper.CreateMesh(content, graphicsService, new BoxShape(1, 1, 1));

            for (int i = 0; i < 100; i++)
            {
                Vector3    position    = new Vector3(random.NextFloat(-100, 100), random.NextFloat(0, 3), random.NextFloat(-100, 100));
                Quaternion orientation = random.NextQuaternion();
                Vector3    scale       = random.NextVector3(0.1f, 4f);
                var        meshNode    = new MeshNode(boxMesh)
                {
                    PoseLocal  = new Pose(position, orientation),
                    ScaleLocal = scale,
                    IsStatic   = true,
                };
                graphicsScreen.Scene.Children.Add(meshNode);
            }

            // Height field with smooth hills.
            var numberOfSamplesX = 20;
            var numberOfSamplesZ = 20;
            var samples          = new float[numberOfSamplesX * numberOfSamplesZ];

            for (int z = 0; z < numberOfSamplesZ; z++)
            {
                for (int x = 0; x < numberOfSamplesX; x++)
                {
                    if (x == 0 || z == 0 || x == 19 || z == 19)
                    {
                        // Set this boundary elements to a low height, so that the height field is connected
                        // to the ground.
                        samples[z * numberOfSamplesX + x] = -1;
                    }
                    else
                    {
                        // A sine/cosine function that creates some interesting waves.
                        samples[z * numberOfSamplesX + x] = 1 + (float)(Math.Cos(z / 2f) * Math.Sin(x / 2f) * 1);
                    }
                }
            }
            var heightField         = new HeightField(0, 0, 20, 20, samples, numberOfSamplesX, numberOfSamplesZ);
            var heightFieldMesh     = SampleHelper.CreateMesh(content, graphicsService, heightField);
            var heightFieldMeshNode = new MeshNode(heightFieldMesh)
            {
                PoseLocal  = new Pose(new Vector3(20, 0, -20)),
                ScaleLocal = new Vector3(1, 2, 1),
                IsStatic   = true,
            };

            graphicsScreen.Scene.Children.Add(heightFieldMeshNode);

            // Dudes.
            for (int i = 0; i < 10; i++)
            {
                Vector3 position    = new Vector3(random.NextFloat(-20, 20), 0, random.NextFloat(-20, 20));
                Matrix  orientation = Matrix.CreateRotationY(random.NextFloat(0, ConstantsF.TwoPi));
                gameObjectService.Objects.Add(new DudeObject(services)
                {
                    Pose = new Pose(position, orientation)
                });
            }

            // Palm trees.
            for (int i = 0; i < 100; i++)
            {
                Vector3 position    = new Vector3(random.NextFloat(-80, 80), 0, random.NextFloat(-100, 100));
                Matrix  orientation = Matrix.CreateRotationY(random.NextFloat(0, ConstantsF.TwoPi));
                float   scale       = random.NextFloat(0.5f, 1.2f);
                gameObjectService.Objects.Add(new StaticObject(services, "PalmTree/palm_tree", scale, new Pose(position, orientation)));
            }

            // Rocks
            for (int i = 0; i < 100; i++)
            {
                Vector3    position    = new Vector3(random.NextFloat(-80, 80), 1, random.NextFloat(-100, 100));
                Quaternion orientation = RandomHelper.Random.NextQuaternion();
                float      scale       = random.NextFloat(0.5f, 1.2f);
                gameObjectService.Objects.Add(new StaticObject(services, "Rock/rock_05", scale, new Pose(position, orientation)));
            }

            // Grass
            for (int i = 0; i < 100; i++)
            {
                Vector3 position    = new Vector3(random.NextFloat(-20, 20), 0, random.NextFloat(-20, 20));
                Matrix  orientation = Matrix.CreateRotationY(random.NextFloat(0, ConstantsF.TwoPi));
                float   scale       = random.NextFloat(0.5f, 1.2f);
                gameObjectService.Objects.Add(new StaticObject(services, "Grass/Grass", scale, new Pose(position, orientation)));
            }

            // More plants
            for (int i = 0; i < 100; i++)
            {
                Vector3 position    = new Vector3(random.NextFloat(-20, 20), 0, random.NextFloat(-20, 20));
                Matrix  orientation = Matrix.CreateRotationY(random.NextFloat(0, ConstantsF.TwoPi));
                float   scale       = random.NextFloat(0.5f, 1.2f);
                gameObjectService.Objects.Add(new StaticObject(services, "Parviflora/Parviflora", scale, new Pose(position, orientation)));
            }

            // "Skyscrapers"
            for (int i = 0; i < 20; i++)
            {
                Vector3 position    = new Vector3(random.NextFloat(90, 100), 0, random.NextFloat(-100, 100));
                Matrix  orientation = Matrix.CreateRotationY(random.NextFloat(0, ConstantsF.TwoPi));
                Vector3 scale       = new Vector3(random.NextFloat(6, 20), random.NextFloat(10, 100), random.NextFloat(6, 20));
                var     meshNode    = new MeshNode(boxMesh)
                {
                    PoseLocal  = new Pose(position, orientation),
                    ScaleLocal = scale,
                    IsStatic   = true,
                    UserFlags  = 1, // Mark the distant huge objects. Used in render callbacks in the CompositeShadowSample.
                };
                graphicsScreen.Scene.Children.Add(meshNode);
            }

            // "Hills"
            for (int i = 0; i < 20; i++)
            {
                Vector3 position = new Vector3(random.NextFloat(-90, -100), 0, random.NextFloat(-100, 100));
                Vector3 scale    = new Vector3(random.NextFloat(10, 20), random.NextFloat(10, 30), random.NextFloat(10, 20));
                var     meshNode = new MeshNode(sphereMesh)
                {
                    PoseLocal  = new Pose(position),
                    ScaleLocal = scale,
                    IsStatic   = true,
                    UserFlags  = 1, // Mark the distant huge objects. Used in render callbacks in the CompositeShadowSample.
                };
                graphicsScreen.Scene.Children.Add(meshNode);
            }
        }