Exemplo n.º 1
0
        void CreateBoxOffMeshConnections(DynamicNavigationMesh navMesh, Node boxGroup)
        {
            foreach (var box in boxGroup.GetChildren())
            {
                var boxPos = box.Position;
                float boxHalfSize = box.Scale.X / 2;
                var connectionStart = box.CreateChild("ConnectionStart");
                connectionStart.SetWorldPosition(navMesh.FindNearestPoint(boxPos + new Vector3(boxHalfSize, -boxHalfSize, 0), Vector3.One));
                var connectionEnd = box.CreateChild("ConnectionEnd");
                connectionEnd.SetWorldPosition(navMesh.FindNearestPoint(boxPos + new Vector3(boxHalfSize, boxHalfSize, 0), Vector3.One));

                OffMeshConnection connection = connectionStart.CreateComponent<OffMeshConnection>();
                connection.EndPoint = connectionEnd;
            }
        }
        void CreateScene()
        {
            var cache = GetSubsystem <ResourceCache>();

            scene = new Scene();

            // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
            // Also create a DebugRenderer component so that we can draw debug geometry
            scene.CreateComponent <Octree>();
            scene.CreateComponent <DebugRenderer>();

            // Create scene node & StaticModel component for showing a static plane
            Node planeNode = scene.CreateChild("Plane");

            planeNode.Scale = new Vector3(100.0f, 1.0f, 100.0f);
            StaticModel planeObject = planeNode.CreateComponent <StaticModel>();

            planeObject.Model = (cache.Get <Model>("Models/Plane.mdl"));
            planeObject.SetMaterial(cache.Get <Material>("Materials/StoneTiled.xml"));

            // 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 randomly sized boxes. If boxes are big enough, make them occluders
            const uint numBoxes = 20;
            Node       boxGroup = scene.CreateChild("Boxes");

            for (uint i = 0; i < numBoxes; ++i)
            {
                Node  boxNode = boxGroup.CreateChild("Box");
                float size    = 1.0f + NextRandom(10.0f);
                boxNode.Position = (new Vector3(NextRandom(80.0f) - 40.0f, size * 0.5f, NextRandom(80.0f) - 40.0f));
                boxNode.SetScale(size);
                StaticModel boxObject = boxNode.CreateComponent <StaticModel>();
                boxObject.Model = (cache.Get <Model>("Models/Box.mdl"));
                boxObject.SetMaterial(cache.Get <Material>("Materials/Stone.xml"));
                boxObject.CastShadows = true;
                if (size >= 3.0f)
                {
                    boxObject.Occluder = true;
                }
            }

            // Create a DynamicNavigationMesh component to the scene root
            DynamicNavigationMesh navMesh = scene.CreateComponent <DynamicNavigationMesh>();

            // Set the agent height large enough to exclude the layers under boxes
            navMesh.AgentHeight            = 10.0f;
            navMesh.CellHeight             = 0.05f;
            navMesh.DrawObstacles          = true;
            navMesh.DrawOffMeshConnections = true;
            // Create a Navigable component to the scene root. This tags all of the geometry in the scene as being part of the
            // navigation mesh. By default this is recursive, but the recursion could be turned off from Navigable
            scene.CreateComponent <Navigable>();
            // Add padding to the navigation mesh in Y-direction so that we can add objects on top of the tallest boxes
            // in the scene and still update the mesh correctly
            navMesh.Padding = new Vector3(0.0f, 10.0f, 0.0f);
            // Now build the navigation geometry. This will take some time. Note that the navigation mesh will prefer to use
            // physics geometry from the scene nodes, as it often is simpler, but if it can not find any (like in this example)
            // it will use renderable geometry instead
            navMesh.Build();

            // Create an off-mesh connection to each box to make them climbable (tiny boxes are skipped). A connection is built from 2 nodes.
            // Note that OffMeshConnections must be added before building the navMesh, but as we are adding Obstacles next, tiles will be automatically rebuilt.
            // Creating connections post-build here allows us to use FindNearestPoint() to procedurally set accurate positions for the connection
            CreateBoxOffMeshConnections(navMesh, boxGroup);

            // Create some mushrooms
            const uint numMushrooms = 100;

            for (uint i = 0; i < numMushrooms; ++i)
            {
                CreateMushroom(new Vector3(NextRandom(90.0f) - 45.0f, 0.0f, NextRandom(90.0f) - 45.0f));
            }


            // Create a CrowdManager component to the scene root
            crowdManager = scene.CreateComponent <CrowdManager>();
            var parameters = crowdManager.GetObstacleAvoidanceParams(0);

            // Set the params to "High (66)" setting
            parameters.VelBias       = 0.5f;
            parameters.AdaptiveDivs  = 7;
            parameters.AdaptiveRings = 3;
            parameters.AdaptiveDepth = 3;
            crowdManager.SetObstacleAvoidanceParams(0, parameters);

            // Create some movable barrels. We create them as crowd agents, as for moving entities it is less expensive and more convenient than using obstacles
            CreateMovingBarrels(navMesh);

            // Create Jack node that will follow the path
            SpawnJack(new Vector3(-5.0f, 0.0f, 20.0f), scene.CreateChild("Jacks"));

            // Create the camera. Limit far clip distance to match the fog
            CameraNode = new Node();
            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, 50.0f, 0.0f);
            Pitch = 80.0f;
            CameraNode.Rotation = new Quaternion(Pitch, Yaw, 0.0f);
        }
Exemplo n.º 3
0
        void CreateMovingBarrels(DynamicNavigationMesh navMesh)
        {
            var cache = GetSubsystem<ResourceCache>();

            Node barrel = scene.CreateChild("Barrel");
            StaticModel model = barrel.CreateComponent<StaticModel>();
            model.Model = cache.Get<Model>("Models/Cylinder.mdl");
            Material material = cache.Get<Material>("Materials/StoneTiled.xml");
            model.SetMaterial(material);
            material.SetTexture(TextureUnit.TU_DIFFUSE, cache.Get<Texture2D>("Textures/TerrainDetail2.dds"));
            model.CastShadows = true;
            for (int i = 0; i < 20; ++i)
            {
                Node clone = barrel.Clone(CreateMode.REPLICATED);
                float size = 0.5f + NextRandom(1.0f);
                clone.Scale = new Vector3(size / 1.5f, size * 2.0f, size / 1.5f);
                clone.Position = navMesh.FindNearestPoint(new Vector3(NextRandom(80.0f) - 40.0f, size * 0.5f, NextRandom(80.0f) - 40.0f), Vector3.One);
                CrowdAgent agent = clone.CreateComponent<CrowdAgent>();
                agent.Radius = clone.Scale.X * 0.5f;
                agent.Height = size;
                agent.NavigationQuality = NavigationQuality.NAVIGATIONQUALITY_LOW;
            }
            barrel.Remove();
        }
Exemplo n.º 4
0
        void OnGestureTapped(int argsX, int argsY)
        {
            // 3 touches at the same time kill everybody :-)
            if (Input.NumTouches == 3)
            {
                KillAll();
            }

            if (surfaceIsValid && !positionIsSelected)
            {
                UI.Root.RemoveChild(loadingLabel);
                loadingLabel          = null;
                PlaneDetectionEnabled = false;
                //hide planes:

                foreach (var node in anchorsNode.Children.ToArray())
                {
                    // if surface is higher than floor - mark as Obstacle
                    if (Math.Abs(node.WorldPosition.Y - LastHitTest.Value.Y) >= 0.1f)
                    {
                        node.CreateComponent <Obstacle>();
                    }

                    var model = node.GetChild("SubPlane").GetComponent <StaticModel>();
                    model.Material.SetShaderParameter("MeshColor", Color.Transparent);
                }


                var music = ResourceCache.GetSound("Sounds/theme.ogg");
                music.Looped = true;
                themeSoundSource.Play(music);

                ContinuesHitTestAtCenter = false;
                var hitPos = cursorNode.Position;                // - Vector3.UnitZ * 0.01f;
                positionIsSelected = true;

                navMesh = Scene.CreateComponent <DynamicNavigationMesh>();
                Scene.CreateComponent <Navigable>();

                navMesh.CellSize               = 0.01f;
                navMesh.CellHeight             = 0.05f;
                navMesh.DrawOffMeshConnections = true;
                navMesh.DrawNavAreas           = true;
                navMesh.TileSize               = 1;
                navMesh.AgentRadius            = 0.1f;

                navMesh.Build();

                crowdManager = Scene.CreateComponent <CrowdManager>();
                var parameters = crowdManager.GetObstacleAvoidanceParams(0);
                parameters.VelBias       = 0.5f;
                parameters.AdaptiveDivs  = 7;
                parameters.AdaptiveRings = 3;
                parameters.AdaptiveDepth = 3;
                crowdManager.SetObstacleAvoidanceParams(0, parameters);
                armyNode = Scene.CreateChild();

                SubscribeToEvents();

                int mutantIndex = 1;
                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        SpawnMutant(new Vector3(hitPos.X + 0.15f * i, hitPos.Y, hitPos.Z + 0.13f * j), "Mutant " + mutantIndex++);
                    }
                }

                return;
            }

            if (positionIsSelected)
            {
                var hitPos = Raycast((float)argsX / Graphics.Width, (float)argsY / Graphics.Height);
                if (hitPos == null)
                {
                    return;
                }

                cursorNode.Position = hitPos.Value + Vector3.UnitY * 0.1f;
                Vector3 pathPos = navMesh.FindNearestPoint(hitPos.Value, new Vector3(0.1f, 0.1f, 0.1f) * 5);
                Scene.GetComponent <CrowdManager>().SetCrowdTarget(pathPos, Scene);


                var sound = ResourceCache.GetSound($"Sounds/go{rand.Next(1, 6)}.wav");
                actionSoundSource.Play(sound);
                actionSoundSource.Gain = 0.4f;
            }
        }