void AddOrRemoveObject() { // Raycast and check if we hit a mushroom node. If yes, remove it, if no, create a new one Vector3 hitPos; Drawable hitDrawable; if (Raycast(250.0f, out hitPos, out hitDrawable)) { // The part of the navigation mesh we must update, which is the world bounding box of the associated // drawable component BoundingBox updateBox; Node hitNode = hitDrawable.Node; if (hitNode.Name == "Mushroom") { updateBox = hitDrawable.WorldBoundingBox; hitNode.Remove(); } else { Node newNode = CreateMushroom(hitPos); updateBox = newNode.GetComponent <StaticModel>().WorldBoundingBox; } // Rebuild part of the navigation mesh, then recalculate path if applicable NavigationMesh navMesh = scene.GetComponent <NavigationMesh>(); navMesh.Build(updateBox); if (currentPath.Count > 0) { navMesh.FindPath(currentPath, jackNode.Position, endPos); } } }
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 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 randomly sized boxes. If boxes are big enough, make them occluders const uint numBoxes = 20; for (uint i = 0; i < numBoxes; ++i) { Node boxNode = scene.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 Jack node that will follow the path jackNode = scene.CreateChild("Jack"); jackNode.Position = new Vector3(-5.0f, 0.0f, 20.0f); AnimatedModel modelObject = jackNode.CreateComponent <AnimatedModel>(); modelObject.Model = cache.Get <Model>("Models/Jack.mdl"); modelObject.SetMaterial(cache.Get <Material>("Materials/Jack.xml")); modelObject.CastShadows = true; // Create a NavigationMesh component to the scene root NavigationMesh navMesh = scene.CreateComponent <NavigationMesh>(); // 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 the camera. Limit far clip distance to match the fog CameraNode = scene.CreateChild("Camera"); 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, 5.0f, 0.0f); }