Esempio n. 1
0
        void SpawnObject()
        {
            var cache = GetSubsystem <ResourceCache>();

            // Create a smaller box at camera position
            Node boxNode = scene.CreateChild("SmallBox");

            boxNode.Position = CameraNode.Position;
            boxNode.Rotation = CameraNode.Rotation;
            boxNode.SetScale(0.25f);
            StaticModel boxObject = boxNode.CreateComponent <StaticModel>();

            boxObject.Model = (cache.Get <Model>("Models/Box.mdl"));
            boxObject.SetMaterial(cache.Get <Material>("Materials/StoneSmall.xml"));
            boxObject.CastShadows = true;

            // Create physics components, use a smaller mass also
            RigidBody body = boxNode.CreateComponent <RigidBody>();

            body.Mass     = 0.25f;
            body.Friction = 0.75f;
            CollisionShape shape = boxNode.CreateComponent <CollisionShape>();

            shape.SetBox(Vector3.One, Vector3.Zero, Quaternion.Identity);

            const float objectVelocity = 10.0f;

            // Set initial velocity for the RigidBody based on camera forward vector. Add also a slight up component
            // to overcome gravity better
            body.SetLinearVelocity(CameraNode.Rotation * new Vector3(0.0f, 0.25f, 1.0f) * objectVelocity);
        }
Esempio n. 2
0
        void CreateRagdollBone(string boneName, ShapeType type, Vector3 size, Vector3 position, Quaternion rotation)
        {
            // Find the correct child scene node recursively
            Node boneNode = Node.GetChild(boneName, true);

            if (boneNode == null)
            {
                Log.Warn($"Could not find bone {boneName} for creating ragdoll physics components");
                return;
            }

            RigidBody body = boneNode.CreateComponent <RigidBody>();

            // Set mass to make movable
            body.Mass = 1.0f;
            // Set damping parameters to smooth out the motion
            body.LinearDamping  = 0.05f;
            body.AngularDamping = 0.85f;
            // Set rest thresholds to ensure the ragdoll rigid bodies come to rest to not consume CPU endlessly
            body.LinearRestThreshold  = 1.5f;
            body.AngularRestThreshold = 2.5f;

            CollisionShape shape = boneNode.CreateComponent <CollisionShape>();

            // We use either a box or a capsule shape for all of the bones
            if (type == ShapeType.SHAPE_BOX)
            {
                shape.SetBox(size, position, rotation);
            }
            else
            {
                shape.SetCapsule(size.X, size.Y, position, rotation);
            }
        }
Esempio n. 3
0
        public void Init()
        {
            var cache = GetSubsystem <ResourceCache>();

            // This function is called only from the main program when initially creating the vehicle, not on scene load
            var         node       = Node;
            StaticModel hullObject = node.CreateComponent <StaticModel>();

            hullBody = node.CreateComponent <RigidBody>();
            CollisionShape hullShape = node.CreateComponent <CollisionShape>();

            node.Scale       = new Vector3(1.5f, 1.0f, 3.0f);
            hullObject.Model = cache.Get <Model>("Models/Box.mdl");
            hullObject.SetMaterial(cache.Get <Material>("Materials/Stone.xml"));
            hullObject.CastShadows = true;
            hullShape.SetBox(Vector3.One, Vector3.Zero, Quaternion.Identity);
            hullBody.Mass           = 4.0f;
            hullBody.LinearDamping  = 0.2f; // Some air resistance
            hullBody.AngularDamping = 0.5f;
            hullBody.CollisionLayer = 1;

            InitWheel("FrontLeft", new Vector3(-0.6f, -0.4f, 0.3f), out frontLeft, out frontLeftId);
            InitWheel("FrontRight", new Vector3(0.6f, -0.4f, 0.3f), out frontRight, out frontRightId);
            InitWheel("RearLeft", new Vector3(-0.6f, -0.4f, -0.3f), out rearLeft, out rearLeftId);
            InitWheel("RearRight", new Vector3(0.6f, -0.4f, -0.3f), out rearRight, out rearRightId);

            GetWheelComponents();
        }
Esempio n. 4
0
        void UpdateAnchor(Node node, ARAnchor anchor)
        {
            var planeAnchor = anchor as ARPlaneAnchor;

            if (planeAnchor == null)
            {
                return;
            }

            Material tileMaterial = null;
            Node     planeNode    = null;

            if (node == null)
            {
                var id = planeAnchor.Identifier.ToString();
                node      = anchorsNode.CreateChild(id);
                planeNode = node.CreateChild("SubPlane");
                var plane = planeNode.CreateComponent <StaticModel>();
                planeNode.Position = new Vector3();
                plane.Model        = CoreAssets.Models.Plane;

                tileMaterial = new Material();
                tileMaterial.SetTexture(TextureUnit.Diffuse, ResourceCache.GetTexture2D("Textures/PlaneTile.png"));
                var tech = new Technique();
                var pass = tech.CreatePass("alpha");
                pass.DepthWrite   = false;
                pass.BlendMode    = BlendMode.Alpha;
                pass.PixelShader  = "PlaneTile";
                pass.VertexShader = "PlaneTile";
                tileMaterial.SetTechnique(0, tech);
                tileMaterial.SetShaderParameter("MeshColor", new Color(Randoms.Next(), 1, Randoms.Next()));
                tileMaterial.SetShaderParameter("MeshAlpha", 0.75f);                 // set 0.0f if you want to hide them
                tileMaterial.SetShaderParameter("MeshScale", 32.0f);

                var planeRb = planeNode.CreateComponent <RigidBody>();
                planeRb.Friction = 1.5f;
                CollisionShape shape = planeNode.CreateComponent <CollisionShape>();
                shape.SetBox(Vector3.One, Vector3.Zero, Quaternion.Identity);

                plane.Material = tileMaterial;
            }
            else
            {
                planeNode    = node.GetChild("SubPlane");
                tileMaterial = planeNode.GetComponent <StaticModel>().Material;
            }

            arkitComponent.ApplyOpenTkTransform(node, planeAnchor.Transform, true);

            planeNode.Scale    = new Vector3(planeAnchor.Extent.X, 0.1f, planeAnchor.Extent.Z);
            planeNode.Position = new Vector3(planeAnchor.Center.X, planeAnchor.Center.Y, -planeAnchor.Center.Z);

            //var animation = new ValueAnimation();
            //animation.SetKeyFrame(0.0f, 0.3f);
            //animation.SetKeyFrame(0.5f, 0.0f);
            //tileMaterial.SetShaderParameterAnimation("MeshAlpha", animation, WrapMode.Once, 1.0f);

            //Debug.WriteLine($"ARPlaneAnchor  Extent({planeAnchor.Extent}), Center({planeAnchor.Center}), Position({planeAnchor.Transform.Row3}");
        }
Esempio n. 5
0
        public void CreateEntity()
        {
            _baseEntity.CreateEntity();

            _node.Scale = _scale;

            if (!InDesign)
            {
                RigidBody rigidBody = _node.CreateComponent <RigidBody>();
                rigidBody.Mass = 1.0f;
                rigidBody.SetAngularFactor(Vector3.Zero);
                CollisionShape boxShape = _node.CreateComponent <CollisionShape>(CreateMode.Local);
                boxShape.SetBox(_scale, _baseEntity.Position, Quaternion.Identity);
                rigidBody.DrawDebugGeometry(GetDebugRenderer(), false);
            }
        }
Esempio n. 6
0
        protected Node CreateRigidBullet(bool byPlayer, Vector3 collisionBox)
        {
            var carrier    = Node;
            var bullet     = carrier.Scene.CreateChild(nameof(Weapon) + GetType().Name);
            var carrierPos = carrier.Position;

            bullet.Position = carrierPos;
            var            body  = bullet.CreateComponent <RigidBody>();
            CollisionShape shape = bullet.CreateComponent <CollisionShape>();

            shape.SetBox(collisionBox, Vector3.Zero, Quaternion.Identity);
            body.Kinematic      = true;
            body.CollisionLayer = byPlayer ? (uint)CollisionLayers.Enemy : (uint)CollisionLayers.Player;
            bullet.AddComponent(new WeaponReferenceComponent(this));
            return(bullet);
        }
Esempio n. 7
0
        protected Node CreatePhysicsFloor()
        {
            var floorNode = CreateFloor();
            // Make the floor physical by adding RigidBody and CollisionShape components
            RigidBody body = floorNode.CreateComponent <RigidBody>();

            // We will be spawning spherical objects in this sample. The ground also needs non-zero rolling friction so that
            // the spheres will eventually come to rest
            body.RollingFriction = 0.15f;
            CollisionShape shape = floorNode.CreateComponent <CollisionShape>();

            // Set a box shape of size 1 x 1 x 1 for collision. The shape will be scaled with the scene node scale, so the
            // rendering and physics representation sizes should match (the box model is also 1 x 1 x 1.)
            shape.SetBox(Vector3.One, Vector3.Zero, Quaternion.Identity);
            body.Restitution = 0.3f;
            return(floorNode);
        }
Esempio n. 8
0
        // l'aereo attende fino a quando non è esploso
        public Task Play()
        {
            liveTask = new TaskCompletionSource <bool>();
            Health   = MaxHealth;
            var node = Node;
            // gestione delle collisioni
            var body = node.CreateComponent <RigidBody>();

            body.Mass          = 1;
            body.Kinematic     = true;
            body.CollisionMask = (uint)CollisionLayer;
            CollisionShape shape = node.CreateComponent <CollisionShape>();

            shape.SetBox(CollisionShapeSize, Vector3.Zero, Quaternion.Identity);
            Init();
            node.SubscribeToNodeCollisionStart(OnCollided);
            return(liveTask.Task);
        }
Esempio n. 9
0
        /// <summary>
        /// Spawn the aircraft and wait until it's exploded
        /// </summary>
        public Task Play()
        {
            _liveTask = new TaskCompletionSource <bool>();
            Health    = MaxHealth;
            var node = Node;

            // Define physics for handling collisions
            var body = node.CreateComponent <RigidBody>();

            body.Mass          = 1;
            body.Kinematic     = true;
            body.CollisionMask = (uint)CollisionLayer;
            CollisionShape shape = node.CreateComponent <CollisionShape>();

            shape.SetBox(CollisionShapeSize, Vector3.Zero, Quaternion.Identity);

            Application.InvokeOnMain(Init);
            node.NodeCollisionStart += OnCollided;
            return(_liveTask.Task);
        }
Esempio n. 10
0
        void CreateScene()
        {
            var cache = GetSubsystem <ResourceCache>();

            scene = new Scene();

            // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
            // Create a physics simulation world with default parameters, which will update at 60fps. Like the Octree must
            // exist before creating drawable components, the PhysicsWorld must exist before creating physics components.
            // Finally, create a DebugRenderer component so that we can draw physics debug geometry
            scene.CreateComponent <Octree>();
            scene.CreateComponent <PhysicsWorld>();
            scene.CreateComponent <DebugRenderer>();

            // Create a Zone component for ambient lighting & fog control
            Node zoneNode = scene.CreateChild("Zone");
            Zone zone     = zoneNode.CreateComponent <Zone>();

            zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f));
            zone.AmbientColor = new Color(0.15f, 0.15f, 0.15f);
            zone.FogColor     = new Color(0.5f, 0.5f, 0.7f);
            zone.FogStart     = 100.0f;
            zone.FogEnd       = 300.0f;

            // Create a directional light to the world. Enable cascaded shadows on it
            Node lightNode = scene.CreateChild("DirectionalLight");

            lightNode.SetDirection(new Vector3(0.6f, -1.0f, 0.8f));
            Light light = lightNode.CreateComponent <Light>();

            light.LightType   = LightType.LIGHT_DIRECTIONAL;
            light.CastShadows = true;
            light.ShadowBias  = new BiasParameters(0.00025f, 0.5f);
            // Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance
            light.ShadowCascade = new CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f);

            {
                // Create a floor object, 500 x 500 world units. Adjust position so that the ground is at zero Y
                Node floorNode = scene.CreateChild("Floor");
                floorNode.Position = new Vector3(0.0f, -0.5f, 0.0f);
                floorNode.Scale    = new Vector3(500.0f, 1.0f, 500.0f);
                StaticModel floorObject = floorNode.CreateComponent <StaticModel>();
                floorObject.Model = cache.Get <Model>("Models/Box.mdl");
                floorObject.SetMaterial(cache.Get <Material>("Materials/StoneTiled.xml"));

                // Make the floor physical by adding RigidBody and CollisionShape components
                /*RigidBody* body = */
                floorNode.CreateComponent <RigidBody>();
                CollisionShape shape = floorNode.CreateComponent <CollisionShape>();
                shape.SetBox(Vector3.One, Vector3.Zero, Quaternion.Identity);
            }

            {
                // Create static mushrooms with triangle mesh collision
                const uint numMushrooms = 50;
                for (uint i = 0; i < numMushrooms; ++i)
                {
                    Node mushroomNode = scene.CreateChild("Mushroom");
                    mushroomNode.Position = new Vector3(NextRandom(400.0f) - 200.0f, 0.0f, NextRandom(400.0f) - 200.0f);
                    mushroomNode.Rotation = new Quaternion(0.0f, NextRandom(360.0f), 0.0f);
                    mushroomNode.SetScale(5.0f + NextRandom(5.0f));
                    StaticModel mushroomObject = mushroomNode.CreateComponent <StaticModel>();
                    mushroomObject.Model = (cache.Get <Model>("Models/Mushroom.mdl"));
                    mushroomObject.SetMaterial(cache.Get <Material>("Materials/Mushroom.xml"));
                    mushroomObject.CastShadows = true;

                    mushroomNode.CreateComponent <RigidBody>();
                    CollisionShape shape = mushroomNode.CreateComponent <CollisionShape>();
                    // By default the highest LOD level will be used, the LOD level can be passed as an optional parameter
                    shape.SetTriangleMesh(mushroomObject.Model, 0, Vector3.One, Vector3.Zero, Quaternion.Identity);
                }
            }

            {
                // Create a large amount of falling physics objects
                const uint numObjects = 1000;
                for (uint i = 0; i < numObjects; ++i)
                {
                    Node boxNode = scene.CreateChild("Box");
                    boxNode.Position = new Vector3(0.0f, i * 2.0f + 100.0f, 0.0f);
                    StaticModel boxObject = boxNode.CreateComponent <StaticModel>();
                    boxObject.Model = cache.Get <Model>("Models/Box.mdl");
                    boxObject.SetMaterial(cache.Get <Material>("Materials/StoneSmall.xml"));
                    boxObject.CastShadows = true;

                    // Give the RigidBody mass to make it movable and also adjust friction
                    RigidBody body = boxNode.CreateComponent <RigidBody>();
                    body.Mass     = 1.0f;
                    body.Friction = 1.0f;
                    // Disable collision event signaling to reduce CPU load of the physics simulation
                    body.CollisionEventMode = CollisionEventMode.COLLISION_NEVER;
                    CollisionShape shape = boxNode.CreateComponent <CollisionShape>();
                    shape.SetBox(Vector3.One, Vector3.Zero, Quaternion.Identity);
                }
            }

            // Create the camera. Limit far clip distance to match the fog. Note: now we actually create the camera node outside
            // the scene, because we want it to be unaffected by scene load / save
            CameraNode = new Node();
            Camera camera = CameraNode.CreateComponent <Camera>();

            camera.FarClip = 300.0f;

            // Set an initial position for the camera scene node above the floor
            CameraNode.Position = new Vector3(0.0f, 3.0f, -20.0f);
        }
Esempio n. 11
0
        void CreateScene()
        {
            var cache = GetSubsystem <ResourceCache>();

            scene = new Scene();

            // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
            // Create a physics simulation world with default parameters, which will update at 60fps. Like the Octree must
            // exist before creating drawable components, the PhysicsWorld must exist before creating physics components.
            // Finally, create a DebugRenderer component so that we can draw physics debug geometry
            scene.CreateComponent <Octree>();
            scene.CreateComponent <PhysicsWorld>();
            scene.CreateComponent <DebugRenderer>();

            // Create a Zone component for ambient lighting & fog control
            Node zoneNode = scene.CreateChild("Zone");
            Zone zone     = zoneNode.CreateComponent <Zone>();

            zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f));
            zone.AmbientColor = (new Color(0.15f, 0.15f, 0.15f));
            zone.FogColor     = new Color(0.5f, 0.5f, 0.7f);
            zone.FogStart     = 100.0f;
            zone.FogEnd       = 300.0f;

            // Create a directional light to the world. Enable cascaded shadows on it
            Node lightNode = scene.CreateChild("DirectionalLight");

            lightNode.SetDirection(new Vector3(0.6f, -1.0f, 0.8f));
            Light light = lightNode.CreateComponent <Light>();

            light.LightType   = LightType.LIGHT_DIRECTIONAL;
            light.CastShadows = true;
            light.ShadowBias  = new BiasParameters(0.00025f, 0.5f);
            // Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance
            light.ShadowCascade = new CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f);

            {
                // Create a floor object, 500 x 500 world units. Adjust position so that the ground is at zero Y
                Node floorNode = scene.CreateChild("Floor");
                floorNode.Position = new Vector3(0.0f, -0.5f, 0.0f);
                floorNode.Scale    = new Vector3(500.0f, 1.0f, 500.0f);
                StaticModel floorObject = floorNode.CreateComponent <StaticModel>();
                floorObject.Model = cache.Get <Model>("Models/Box.mdl");
                floorObject.SetMaterial(cache.Get <Material>("Materials/StoneTiled.xml"));

                // Make the floor physical by adding RigidBody and CollisionShape components
                RigidBody body = floorNode.CreateComponent <RigidBody>();
                // We will be spawning spherical objects in this sample. The ground also needs non-zero rolling friction so that
                // the spheres will eventually come to rest
                body.RollingFriction = 0.15f;
                CollisionShape shape = floorNode.CreateComponent <CollisionShape>();
                // Set a box shape of size 1 x 1 x 1 for collision. The shape will be scaled with the scene node scale, so the
                // rendering and physics representation sizes should match (the box model is also 1 x 1 x 1.)
                shape.SetBox(Vector3.One, Vector3.Zero, Quaternion.Identity);
            }

            // Create the camera. Limit far clip distance to match the fog
            CameraNode = new Node();
            CameraNode.SetName("Camera");
            camera         = CameraNode.CreateComponent <Camera>();
            camera.FarClip = 300.0f;
            // Set an initial position for the camera scene node above the plane
            CameraNode.Position = new Vector3(0.0f, 3.0f, -20.0f);
        }
Esempio n. 12
0
 public override void SetTo(CollisionShape shapeComponent)
 {
     shapeComponent.SetBox(size, position, rotation);
 }
Esempio n. 13
0
        void CreateScene()
        {
            var cache = GetSubsystem <ResourceCache>();

            scene = new Scene();

            // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
            // Create a physics simulation world with default parameters, which will update at 60fps. Like the Octree must
            // exist before creating drawable components, the PhysicsWorld must exist before creating physics components.
            // Finally, create a DebugRenderer component so that we can draw physics debug geometry
            scene.CreateComponent <Octree>();
            scene.CreateComponent <PhysicsWorld>();
            scene.CreateComponent <DebugRenderer>();

            // Create a Zone component for ambient lighting & fog control
            Node zoneNode = scene.CreateChild("Zone");
            Zone zone     = zoneNode.CreateComponent <Zone>();

            zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f));
            zone.AmbientColor = (new Color(0.15f, 0.15f, 0.15f));
            zone.FogColor     = new Color(0.5f, 0.5f, 0.7f);
            zone.FogStart     = 100.0f;
            zone.FogEnd       = 300.0f;

            // Create a directional light to the world. Enable cascaded shadows on it
            Node lightNode = scene.CreateChild("DirectionalLight");

            lightNode.SetDirection(new Vector3(0.6f, -1.0f, 0.8f));
            Light light = lightNode.CreateComponent <Light>();

            light.LightType   = LightType.LIGHT_DIRECTIONAL;
            light.CastShadows = true;
            light.ShadowBias  = new BiasParameters(0.00025f, 0.5f);
            // Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance
            light.ShadowCascade = new CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f);

            {
                // Create a floor object, 500 x 500 world units. Adjust position so that the ground is at zero Y
                Node floorNode = scene.CreateChild("Floor");
                floorNode.Position = new Vector3(0.0f, -0.5f, 0.0f);
                floorNode.Scale    = new Vector3(500.0f, 1.0f, 500.0f);
                StaticModel floorObject = floorNode.CreateComponent <StaticModel>();
                floorObject.Model = cache.Get <Model>("Models/Box.mdl");
                floorObject.SetMaterial(cache.Get <Material>("Materials/StoneTiled.xml"));

                // Make the floor physical by adding RigidBody and CollisionShape components
                RigidBody body = floorNode.CreateComponent <RigidBody>();
                // We will be spawning spherical objects in this sample. The ground also needs non-zero rolling friction so that
                // the spheres will eventually come to rest
                body.RollingFriction = 0.15f;
                CollisionShape shape = floorNode.CreateComponent <CollisionShape>();
                // Set a box shape of size 1 x 1 x 1 for collision. The shape will be scaled with the scene node scale, so the
                // rendering and physics representation sizes should match (the box model is also 1 x 1 x 1.)
                shape.SetBox(Vector3.One, Vector3.Zero, Quaternion.Identity);
            }

            // Create animated models
            for (int z = -1; z <= 1; ++z)
            {
                for (int x = -4; x <= 4; ++x)
                {
                    Node modelNode = scene.CreateChild("Jack");
                    modelNode.Position = new Vector3(x * 5.0f, 0.0f, z * 5.0f);
                    modelNode.Rotation = new Quaternion(0.0f, 180.0f, 0.0f);
                    AnimatedModel modelObject = modelNode.CreateComponent <AnimatedModel>();
                    modelObject.Model = cache.Get <Model>("Models/Jack.mdl");
                    modelObject.SetMaterial(cache.Get <Material>("Materials/Jack.xml"));
                    modelObject.CastShadows = true;
                    // Set the model to also update when invisible to avoid staying invisible when the model should come into
                    // view, but does not as the bounding box is not updated
                    modelObject.UpdateInvisible = true;

                    // Create a rigid body and a collision shape. These will act as a trigger for transforming the
                    // model into a ragdoll when hit by a moving object
                    RigidBody body = modelNode.CreateComponent <RigidBody>();
                    // The Trigger mode makes the rigid body only detect collisions, but impart no forces on the
                    // colliding objects
                    body.Trigger = true;
                    CollisionShape shape = modelNode.CreateComponent <CollisionShape>();
                    // Create the capsule shape with an offset so that it is correctly aligned with the model, which
                    // has its origin at the feet
                    shape.SetCapsule(0.7f, 2.0f, new Vector3(0.0f, 1.0f, 0.0f), Quaternion.Identity);

                    // Create a custom component that reacts to collisions and creates the ragdoll
                    modelNode.AddComponent(new Ragdoll());
                }
            }

            // Create the camera. Limit far clip distance to match the fog
            CameraNode     = new Node();
            camera         = CameraNode.CreateComponent <Camera>();
            camera.FarClip = 300.0f;
            // Set an initial position for the camera scene node above the plane
            CameraNode.Position = new Vector3(0.0f, 3.0f, -20.0f);
        }
Esempio n. 14
0
        void CreateScene()
        {
            var cache = ResourceCache;

            scene = new Scene();

            // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
            // Create a physics simulation world with default parameters, which will update at 60fps. Like the Octree must
            // exist before creating drawable components, the PhysicsWorld must exist before creating physics components.
            // Finally, create a DebugRenderer component so that we can draw physics debug geometry
            scene.CreateComponent <Octree>();
            scene.CreateComponent <PhysicsWorld>();
            scene.CreateComponent <DebugRenderer>();

            // Create a Zone component for ambient lighting & fog control
            Node zoneNode = scene.CreateChild("Zone");
            Zone zone     = zoneNode.CreateComponent <Zone>();

            zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f));
            zone.AmbientColor = new Color(0.15f, 0.15f, 0.15f);
            zone.FogColor     = new Color(1.0f, 1.0f, 1.0f);
            zone.FogStart     = 300.0f;
            zone.FogEnd       = 500.0f;

            // Create a directional light to the world. Enable cascaded shadows on it
            Node lightNode = scene.CreateChild("DirectionalLight");

            lightNode.SetDirection(new Vector3(0.6f, -1.0f, 0.8f));
            Light light = lightNode.CreateComponent <Light>();

            light.LightType   = LightType.Directional;
            light.CastShadows = true;
            light.ShadowBias  = new BiasParameters(0.00025f, 0.5f);
            // Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance
            light.ShadowCascade = new CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f);

            // Create skybox. The Skybox component is used like StaticModel, but it will be always located at the camera, giving the
            // illusion of the box planes being far away. Use just the ordinary Box model and a suitable material, whose shader will
            // generate the necessary 3D texture coordinates for cube mapping
            Node skyNode = scene.CreateChild("Sky");

            skyNode.SetScale(500.0f); // The scale actually does not matter
            Skybox skybox = skyNode.CreateComponent <Skybox>();

            skybox.Model = cache.GetModel("Models/Box.mdl");
            skybox.SetMaterial(cache.GetMaterial("Materials/Skybox.xml"));

            {
                // Create a floor object, 1000 x 1000 world units. Adjust position so that the ground is at zero Y
                Node floorNode = scene.CreateChild("Floor");
                floorNode.Position = new Vector3(0.0f, -0.5f, 0.0f);
                floorNode.Scale    = new Vector3(1000.0f, 1.0f, 1000.0f);
                StaticModel floorObject = floorNode.CreateComponent <StaticModel>();
                floorObject.Model = cache.GetModel("Models/Box.mdl");
                floorObject.SetMaterial(cache.GetMaterial("Materials/StoneTiled.xml"));

                // Make the floor physical by adding RigidBody and CollisionShape components. The RigidBody's default
                // parameters make the object static (zero mass.) Note that a CollisionShape by itself will not participate
                // in the physics simulation

                floorNode.CreateComponent <RigidBody>();
                CollisionShape shape = floorNode.CreateComponent <CollisionShape>();
                // Set a box shape of size 1 x 1 x 1 for collision. The shape will be scaled with the scene node scale, so the
                // rendering and physics representation sizes should match (the box model is also 1 x 1 x 1.)
                shape.SetBox(Vector3.One, Vector3.Zero, Quaternion.Identity);
            }

            {
                // Create a pyramid of movable physics objects
                for (int y = 0; y < 8; ++y)
                {
                    for (int x = -y; x <= y; ++x)
                    {
                        Node boxNode = scene.CreateChild("Box");
                        boxNode.Position = new Vector3((float)x, -(float)y + 8.0f, 0.0f);
                        StaticModel boxObject = boxNode.CreateComponent <StaticModel>();
                        boxObject.Model = cache.GetModel("Models/Box.mdl");
                        boxObject.SetMaterial(cache.GetMaterial("Materials/StoneEnvMapSmall.xml"));
                        boxObject.CastShadows = true;

                        // Create RigidBody and CollisionShape components like above. Give the RigidBody mass to make it movable
                        // and also adjust friction. The actual mass is not important; only the mass ratios between colliding
                        // objects are significant
                        RigidBody body = boxNode.CreateComponent <RigidBody>();
                        body.Mass     = 1.0f;
                        body.Friction = 0.75f;
                        CollisionShape shape = boxNode.CreateComponent <CollisionShape>();
                        shape.SetBox(Vector3.One, Vector3.Zero, Quaternion.Identity);
                    }
                }
            }

            // Create the camera. Limit far clip distance to match the fog. Note: now we actually create the camera node outside
            // the scene, because we want it to be unaffected by scene load / save
            CameraNode = new Node();
            Camera camera = CameraNode.CreateComponent <Camera>();

            camera.FarClip = 500.0f;

            // Set an initial position for the camera scene node above the floor
            CameraNode.Position = (new Vector3(0.0f, 5.0f, -20.0f));
        }
Esempio n. 15
0
        void CreateScene()
        {
            var cache = ResourceCache;

            scene = new Scene();

            // Create scene subsystem components
            scene.CreateComponent <Octree>();
            physicsWorld = scene.CreateComponent <PhysicsWorld>();

            // Create camera and define viewport. We will be doing load / save, so it's convenient to create the camera outside the scene,
            // so that it won't be destroyed and recreated, and we don't have to redefine the viewport on load
            CameraNode = new Node();
            Camera camera = CameraNode.CreateComponent <Camera>();

            camera.FarClip = 300.0f;
            Renderer.SetViewport(0, new Viewport(Context, scene, camera, null));

            // Create static scene content. First create a zone for ambient lighting and fog control
            Node zoneNode = scene.CreateChild("Zone");
            Zone zone     = zoneNode.CreateComponent <Zone>();

            zone.AmbientColor = new Color(0.15f, 0.15f, 0.15f);
            zone.FogColor     = new Color(0.5f, 0.5f, 0.7f);
            zone.FogStart     = 100.0f;
            zone.FogEnd       = 300.0f;
            zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f));

            // Create a directional light with cascaded shadow mapping
            Node lightNode = scene.CreateChild("DirectionalLight");

            lightNode.SetDirection(new Vector3(0.3f, -0.5f, 0.425f));
            Light light = lightNode.CreateComponent <Light>();

            light.LightType         = LightType.Directional;
            light.CastShadows       = true;
            light.ShadowBias        = new BiasParameters(0.00025f, 0.5f);
            light.ShadowCascade     = new CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f);
            light.SpecularIntensity = 0.5f;

            // Create the floor object
            Node floorNode = scene.CreateChild("Floor");

            floorNode.Position = new Vector3(0.0f, -0.5f, 0.0f);
            floorNode.Scale    = new Vector3(200.0f, 1.0f, 200.0f);
            StaticModel sm = floorNode.CreateComponent <StaticModel>();

            sm.Model = cache.GetModel("Models/Box.mdl");
            sm.SetMaterial(cache.GetMaterial("Materials/Stone.xml"));

            RigidBody body = floorNode.CreateComponent <RigidBody>();

            // Use collision layer bit 2 to mark world scenery. This is what we will raycast against to prevent camera from going
            // inside geometry
            body.CollisionLayer = 2;
            CollisionShape shape = floorNode.CreateComponent <CollisionShape>();

            shape.SetBox(Vector3.One, Vector3.Zero, Quaternion.Identity);

            // Create mushrooms of varying sizes
            const uint numMushrooms = 60;

            for (uint i = 0; i < numMushrooms; ++i)
            {
                Node objectNode = scene.CreateChild("Mushroom");
                objectNode.Position = new Vector3(NextRandom(180.0f) - 90.0f, 0.0f, NextRandom(180.0f) - 90.0f);
                objectNode.Rotation = new Quaternion(0.0f, NextRandom(360.0f), 0.0f);
                objectNode.SetScale(2.0f + NextRandom(5.0f));
                StaticModel o = objectNode.CreateComponent <StaticModel>();
                o.Model = cache.GetModel("Models/Mushroom.mdl");
                o.SetMaterial(cache.GetMaterial("Materials/Mushroom.xml"));
                o.CastShadows = true;

                body = objectNode.CreateComponent <RigidBody>();
                body.CollisionLayer = 2;
                shape = objectNode.CreateComponent <CollisionShape>();
                shape.SetTriangleMesh(o.Model, 0, Vector3.One, Vector3.Zero, Quaternion.Identity);
            }

            // Create movable boxes. Let them fall from the sky at first
            const uint numBoxes = 100;

            for (uint i = 0; i < numBoxes; ++i)
            {
                float scale = NextRandom(2.0f) + 0.5f;

                Node objectNode = scene.CreateChild("Box");
                objectNode.Position = new Vector3(NextRandom(180.0f) - 90.0f, NextRandom(10.0f) + 10.0f, NextRandom(180.0f) - 90.0f);
                objectNode.Rotation = new Quaternion(NextRandom(360.0f), NextRandom(360.0f), NextRandom(360.0f));
                objectNode.SetScale(scale);
                StaticModel o = objectNode.CreateComponent <StaticModel>();
                o.Model = cache.GetModel("Models/Box.mdl");
                o.SetMaterial(cache.GetMaterial("Materials/Stone.xml"));
                o.CastShadows = true;

                body = objectNode.CreateComponent <RigidBody>();
                body.CollisionLayer = 2;
                // Bigger boxes will be heavier and harder to move
                body.Mass = scale * 2.0f;
                shape     = objectNode.CreateComponent <CollisionShape>();
                shape.SetBox(Vector3.One, Vector3.Zero, Quaternion.Identity);
            }
        }