public BoundingBox(Vector3 min, Vector3 max) { DummyMax = 0; DummyMin = 0; Min = min; Max = max; }
public BoundingBox(float min, float max) { DummyMax = 0; DummyMin = 0; Min = new Vector3(min, min, min); Max = new Vector3(max, max, max); }
public Intersection IsInside(Vector3 point) { if (point.X < Min.X || point.X > Max.X || point.Y < Min.Y || point.Y > Max.Y || point.Z < Min.Z || point.Z > Max.Z) return Intersection.OUTSIDE; return Intersection.INSIDE; }
void SpawnJack(Vector3 pos, Node jackGroup) { var cache = GetSubsystem<ResourceCache>(); Node jackNode = jackGroup.CreateChild("Jack"); jackNode.Position = pos; AnimatedModel modelObject = jackNode.CreateComponent<AnimatedModel>(); modelObject.Model = (cache.Get<Model>("Models/Jack.mdl")); modelObject.SetMaterial(cache.Get<Material>("Materials/Jack.xml")); modelObject.CastShadows = true; jackNode.CreateComponent<AnimationController>(); // Create the CrowdAgent var agent = jackNode.CreateComponent<CrowdAgent>(); agent.Height = 2.0f; agent.MaxSpeed = 3.0f; agent.MaxAccel = 3.0f; }
bool Raycast(float maxDistance, out Vector3 hitPos, out Drawable hitDrawable) { var input = GetSubsystem<Input>(); hitDrawable = null; hitPos = new Vector3(); var graphics = GetSubsystem<Graphics>(); Camera camera = CameraNode.GetComponent<Camera>(); IntVector2 pos = input.MousePosition; Ray cameraRay = camera.GetScreenRay((float)pos.X / graphics.Width, (float)pos.Y / graphics.Height); RayOctreeQuery query = new RayOctreeQuery(cameraRay, RayQueryLevel.RAY_TRIANGLE, maxDistance, Constants.DRAWABLE_GEOMETRY); // Pick only geometry objects, not eg. zones or lights, only get the first (closest) hit scene.GetComponent<Octree>().RaycastSingle(query); if (query.Results.Count > 0) { var first = query.Results.First(); hitPos = first.Position; hitDrawable = first.Drawable; return true; } return false; }
void CreateMushroom(Vector3 pos) { var cache = GetSubsystem<ResourceCache>(); Node mushroomNode = scene.CreateChild("Mushroom"); mushroomNode.Position = (pos); mushroomNode.Rotation = new Quaternion(0.0f, NextRandom(360.0f), 0.0f); mushroomNode.SetScale(2.0f + NextRandom(0.5f)); StaticModel mushroomObject = mushroomNode.CreateComponent<StaticModel>(); mushroomObject.Model = (cache.Get<Model>("Models/Mushroom.mdl")); mushroomObject.SetMaterial(cache.Get<Material>("Materials/Mushroom.xml")); mushroomObject.CastShadows = true; // Create the navigation obstacle Obstacle obstacle = mushroomNode.CreateComponent<Obstacle>(); obstacle.Radius = mushroomNode.Scale.X; obstacle.Height = mushroomNode.Scale.Y; }
public void SetRotationSpeed(Vector3 vector) { rotationSpeed = vector; }
void CreateScene() { var cache = GetSubsystem<ResourceCache>(); scene = new Scene(); // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000) scene.CreateComponent<Octree>(); // Create a Zone component for ambient lighting & fog control var zoneNode = scene.CreateChild("Zone"); var 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 = 500.0f; zone.FogEnd = 750.0f; // Create a directional light to the world. Enable cascaded shadows on it var lightNode = scene.CreateChild("DirectionalLight"); lightNode.SetDirection(new Vector3(0.6f, -1.0f, 0.8f)); var light = lightNode.CreateComponent<Light>(); light.LightType = LightType.LIGHT_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; // Apply slightly overbright lighting to match the skybox light.Color = new Color(1.2f, 1.2f, 1.2f); // 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 var skyNode = scene.CreateChild("Sky"); skyNode.SetScale(500.0f); // The scale actually does not matter var skybox = skyNode.CreateComponent<Skybox>(); skybox.Model = cache.Get<Model>("Models/Box.mdl"); skybox.SetMaterial(cache.Get<Material>("Materials/Skybox.xml")); // Create heightmap terrain var terrainNode = scene.CreateChild("Terrain"); terrainNode.Position = new Vector3(0.0f, 0.0f, 0.0f); var terrain = terrainNode.CreateComponent<Terrain>(); terrain.PatchSize = 64; terrain.Spacing = new Vector3(2.0f, 0.5f, 2.0f); // Spacing between vertices and vertical resolution of the height map terrain.Smoothing =true; terrain.SetHeightMap(cache.Get<Image>("Textures/HeightMap.png")); terrain.Material = cache.Get<Material>("Materials/Terrain.xml"); // The terrain consists of large triangles, which fits well for occlusion rendering, as a hill can occlude all // terrain patches and other objects behind it terrain.Occluder = true; // Create 1000 boxes in the terrain. Always face outward along the terrain normal uint numObjects = 1000; for (uint i = 0; i < numObjects; ++i) { var objectNode = scene.CreateChild("Box"); Vector3 position = new Vector3(NextRandom(2000.0f) - 1000.0f, 0.0f, NextRandom(2000.0f) - 1000.0f); position.Y = terrain.GetHeight(position) + 2.25f; objectNode.Position = position; // Create a rotation quaternion from up vector to terrain normal objectNode.Rotation = Quaternion.FromRotationTo(new Vector3(0.0f, 1.0f, 0.0f), terrain.GetNormal(position)); objectNode.SetScale(5.0f); var obj = objectNode.CreateComponent<StaticModel>(); obj.Model = cache.Get<Model>("Models/Box.mdl"); obj.SetMaterial(cache.Get<Material>("Materials/Stone.xml")); obj.CastShadows = true; } // Create a water plane object that is as large as the terrain waterNode = scene.CreateChild("Water"); waterNode.Scale = new Vector3(2048.0f, 1.0f, 2048.0f); waterNode.Position = new Vector3(0.0f, 5.0f, 0.0f); var water = waterNode.CreateComponent<StaticModel>(); water.Model = cache.Get<Model>("Models/Plane.mdl"); water.SetMaterial(cache.Get<Material>("Materials/Water.xml")); // Set a different viewmask on the water plane to be able to hide it from the reflection camera water.ViewMask = 0x80000000; // Create the camera. Limit far clip distance to match the fog CameraNode = new Node(); var camera = CameraNode.CreateComponent<Camera>(); camera.FarClip = 750.0f; // Set an initial position for the camera scene node above the plane CameraNode.Position = new Vector3(0.0f, 7.0f, -20.0f); }
public NodeInfo(Node node, Vector3 moveSpeed, float rotateSpeed) { Node = node; MoveSpeed = moveSpeed; RotateSpeed = rotateSpeed; }
void CreateScene() { var cache = GetSubsystem<ResourceCache>(); scene = new Scene(); // Create scene subsystem components scene.CreateComponent<Octree>(); 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 = 500.0f; GetSubsystem<Renderer>().SetViewport(0, new Viewport(scene, camera)); // 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 = 300.0f; zone.FogEnd = 500.0f; zone.SetBoundingBox(new BoundingBox(-2000.0f, 2000.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.LIGHT_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 heightmap terrain with collision Node terrainNode = scene.CreateChild("Terrain"); terrainNode.Position = (Vector3.Zero); Terrain terrain = terrainNode.CreateComponent<Terrain>(); terrain.PatchSize = 64; terrain.Spacing = new Vector3(2.0f, 0.1f, 2.0f); // Spacing between vertices and vertical resolution of the height map terrain.Smoothing = true; terrain.SetHeightMap(cache.Get<Image>("Textures/HeightMap.png")); terrain.Material = cache.Get<Material>("Materials/Terrain.xml"); // The terrain consists of large triangles, which fits well for occlusion rendering, as a hill can occlude all // terrain patches and other objects behind it terrain.Occluder = true; RigidBody body = terrainNode.CreateComponent<RigidBody>(); body.CollisionLayer = 2; // Use layer bitmask 2 for static geometry CollisionShape shape = terrainNode.CreateComponent<CollisionShape>(); shape.SetTerrain(0); // Create 1000 mushrooms in the terrain. Always face outward along the terrain normal const uint numMushrooms = 1000; for (uint i = 0; i < numMushrooms; ++i) { Node objectNode = scene.CreateChild("Mushroom"); Vector3 position = new Vector3(NextRandom(2000.0f) - 1000.0f, 0.0f, NextRandom(2000.0f) - 1000.0f); position.Y = terrain.GetHeight(position) - 0.1f; objectNode.Position = (position); // Create a rotation quaternion from up vector to terrain normal objectNode.Rotation = Quaternion.FromRotationTo(Vector3.UnitY, terrain.GetNormal(position)); objectNode.SetScale(3.0f); StaticModel sm = objectNode.CreateComponent<StaticModel>(); sm.Model = (cache.Get<Model>("Models/Mushroom.mdl")); sm.SetMaterial(cache.Get<Material>("Materials/Mushroom.xml")); sm.CastShadows = true; body = objectNode.CreateComponent<RigidBody>(); body.CollisionLayer = 2; shape = objectNode.CreateComponent<CollisionShape>(); shape.SetTriangleMesh(sm.Model, 0, Vector3.One, Vector3.Zero, Quaternion.Identity); } }
void SubscribeToEvents() { SubscribeToEvent<PostRenderUpdateEvent>(e => { // If draw debug mode is enabled, draw viewport debug geometry, which will show eg. drawable bounding boxes and skeleton // bones. Note that debug geometry has to be separately requested each frame. Disable depth test so that we can see the // bones properly if (drawDebug) GetSubsystem<Renderer>().DrawDebugGeometry(false); if (currentPath.Count > 0) { // Visualize the current calculated path DebugRenderer debug = scene.GetComponent<DebugRenderer>(); debug.AddBoundingBox(new BoundingBox(endPos - new Vector3(0.1f, 0.1f, 0.1f), endPos + new Vector3(0.1f, 0.1f, 0.1f)), new Color(1.0f, 1.0f, 1.0f), true); // Draw the path with a small upward bias so that it does not clip into the surfaces Vector3 bias = new Vector3(0.0f, 0.05f, 0.0f); debug.AddLine(jackNode.Position + bias, currentPath[0] + bias, new Color(1.0f, 1.0f, 1.0f), true); if (currentPath.Count > 1) { for (int i = 0; i < currentPath.Count - 1; ++i) debug.AddLine(currentPath[i] + bias, currentPath[i + 1] + bias, new Color(1.0f, 1.0f, 1.0f), true); } } }); }
void SetPathPoint() { var input = GetSubsystem<Input>(); Vector3 hitPos; Drawable hitDrawable; NavigationMesh navMesh = scene.GetComponent<NavigationMesh>(); if (Raycast(250.0f, out hitPos, out hitDrawable)) { Vector3 pathPos = navMesh.FindNearestPoint(hitPos, new Vector3(1.0f, 1.0f, 1.0f)); if (input.GetQualifierDown(Constants.QUAL_SHIFT)) { // Teleport currentPath.Clear(); jackNode.LookAt(new Vector3(pathPos.X, jackNode.Position.Y, pathPos.Z), Vector3.UnitY, TransformSpace.TS_WORLD); jackNode.Position = (pathPos); } else { // Calculate path from Jack's current position to the end point endPos = pathPos; var result = navMesh.FindPath(currentPath, jackNode.Position, endPos); } } }
Node CreateMushroom(Vector3 pos) { var cache = GetSubsystem<ResourceCache>(); Node mushroomNode = scene.CreateChild("Mushroom"); mushroomNode.Position = pos; mushroomNode.Rotation = new Quaternion(0.0f, NextRandom(360.0f), 0.0f); mushroomNode.SetScale(2.0f + NextRandom(0.5f)); StaticModel mushroomObject = mushroomNode.CreateComponent<StaticModel>(); mushroomObject.Model = (cache.Get<Model>("Models/Mushroom.mdl")); mushroomObject.SetMaterial(cache.Get<Material>("Materials/Mushroom.xml")); mushroomObject.CastShadows = true; return mushroomNode; }
public void Merge(Vector3 point) { if (point.X < Min.X) Min.X = point.X; if (point.Y < Min.Y) Min.Y = point.Y; if (point.Z < Min.Z) Min.Z = point.Z; if (point.X > Max.X) Max.X = point.X; if (point.Y > Max.Y) Max.Y = point.Y; if (point.Z > Max.Z) Max.Z = point.Z; }
void CreateScene() { var cache = GetSubsystem<ResourceCache>(); scene = new Scene(); // Create the Octree component to the scene so that drawable objects can be rendered. Use default volume // (-1000, -1000, -1000) to (1000, 1000, 1000) scene.CreateComponent<Octree>(); // Create a Zone component into a child scene node. The Zone controls ambient lighting and fog settings. Like the Octree, // it also defines its volume with a bounding box, but can be rotated (so it does not need to be aligned to the world X, Y // and Z axes.) Drawable objects "pick up" the zone they belong to and use it when rendering; several zones can exist var zoneNode = scene.CreateChild("Zone"); var zone = zoneNode.CreateComponent<Zone>(); // Set same volume as the Octree, set a close bluish fog and some ambient light zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f)); zone.AmbientColor = new Color(0.05f, 0.1f, 0.15f); zone.FogColor = new Color(0.1f, 0.2f, 0.3f); zone.FogStart = 10; zone.FogEnd = 100; var boxesNode = scene.CreateChild("Boxes"); const int numObjects = 2000; for (var i = 0; i < numObjects; ++i) { Node boxNode = new Node(); boxesNode.AddChild(boxNode, 0); boxNode.Position = new Vector3(NextRandom(200f) - 100f, NextRandom(200f) - 100f, NextRandom(200f) - 100f); // Orient using random pitch, yaw and roll Euler angles boxNode.Rotation = new Quaternion(NextRandom(360.0f), NextRandom(360.0f), NextRandom(360.0f)); var boxObject = boxNode.CreateComponent<StaticModel>(); boxObject.Model = cache.Get<Model>("Models/Box.mdl"); boxObject.SetMaterial(cache.Get<Material>("Materials/Stone.xml")); // Add our custom Rotator component which will rotate the scene node each frame, when the scene sends its update event. // The Rotator component derives from the base class CSComponent, which has convenience functionality to subscribe // to the various update events // Now we simply set same rotation speed for all objects var rotationSpeed = new Vector3(10.0f, 20.0f, 30.0f); // First style: use a Rotator instance, which is a component subclass, and // add it to the boxNode. var rotator = new Rotator() { RotationSpeed = rotationSpeed }; boxNode.AddComponent(rotator); } // Create the camera. Let the starting position be at the world origin. As the fog limits maximum visible distance, we can // bring the far clip plane closer for more effective culling of distant objects CameraNode = scene.CreateChild("Camera"); var camera = CameraNode.CreateComponent<Camera>(); camera.FarClip = 100.0f; // Create a point light to the camera scene node var light = CameraNode.CreateComponent<Light>(); light.LightType = LightType.LIGHT_POINT; light.Range = 30.0f; }