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; }
void CreateScene() { var cache = GetSubsystem<ResourceCache>(); scene = new Scene(); // Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will // show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates; it // is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically // optimizing manner scene.CreateComponent<Octree>(); // Create a child scene node (at world origin) and a StaticModel component into it. Set the StaticModel to show a simple // plane mesh with a "stone" material. Note that naming the scene nodes is optional. Scale the scene node larger // (100 x 100 world units) var planeNode = scene.CreateChild("Plane"); planeNode.Scale = new Vector3(100, 1, 100); var planeObject = planeNode.CreateComponent<StaticModel>(); planeObject.Model = cache.Get<Model>("Models/Plane.mdl"); planeObject.SetMaterial(cache.Get<Material>("Materials/StoneTiled.xml")); // Create a directional light to the world so that we can see something. The light scene node's orientation controls the // light direction; we will use the SetDirection() function which calculates the orientation from a forward direction vector. // The light will use default settings (white light, no shadows) var lightNode = scene.CreateChild("DirectionalLight"); lightNode.SetDirection(new Vector3(0.6f, -1.0f, 0.8f)); // The direction vector does not need to be normalized var light = lightNode.CreateComponent<Light>(); light.LightType = LightType.LIGHT_DIRECTIONAL; var rand = new Random(); for (int i = 0; i < 200; i++) { var mushroom = scene.CreateChild("Mushroom"); mushroom.Position = new Vector3(rand.Next(90) - 45, 0, rand.Next(90) - 45); mushroom.Rotation = new Quaternion(0, rand.Next(360), 0); mushroom.SetScale(0.5f + rand.Next(20000) / 10000.0f); var mushroomObject = mushroom.CreateComponent<StaticModel>(); mushroomObject.Model = cache.Get<Model>("Models/Mushroom.mdl"); mushroomObject.SetMaterial(cache.Get<Material>("Materials/Mushroom.xml")); } CameraNode = scene.CreateChild("camera"); camera = CameraNode.CreateComponent<Camera>(); CameraNode.Position = new Vector3(0, 5, 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); }
void CreateScene() { var cache = GetSubsystem<ResourceCache>(); { rttScene = new Scene(); // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000) rttScene.CreateComponent<Octree>(); // Create a Zone for ambient light & fog control Node zoneNode = rttScene.CreateChild("Zone"); Zone 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.0f; zone.FogEnd = 100.0f; // Create randomly positioned and oriented box StaticModels in the scene const uint numObjects = 2000; for (uint i = 0; i < numObjects; ++i) { Node boxNode = rttScene.CreateChild("Box"); boxNode.Position = new Vector3(NextRandom(200.0f) - 100.0f, NextRandom(200.0f) - 100.0f, NextRandom(200.0f) - 100.0f); // Orient using random pitch, yaw and roll Euler angles boxNode.Rotation = new Quaternion(NextRandom(360.0f), NextRandom(360.0f), NextRandom(360.0f)); StaticModel 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. // Simply set same rotation speed for all objects Rotator rotator = new Rotator(); boxNode.AddComponent(rotator); rotator.SetRotationSpeed(new Vector3(10.0f, 20.0f, 30.0f)); } // Create a camera for the render-to-texture scene. Simply leave it at the world origin and let it observe the scene rttCameraNode = rttScene.CreateChild("Camera"); Camera camera = rttCameraNode.CreateComponent<Camera>(); camera.FarClip = 100.0f; // Create a point light to the camera scene node Light light = rttCameraNode.CreateComponent<Light>(); light.LightType = LightType.LIGHT_POINT; light.Range = 30.0f; } { // Create the scene in which we move around scene = new Scene(); // Create octree, use also default volume (-1000, -1000, -1000) to (1000, 1000, 1000) scene.CreateComponent<Octree>(); // 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.1f, 0.1f, 0.1f); zone.FogStart = 100.0f; zone.FogEnd = 300.0f; // Create a directional light without shadows Node lightNode = scene.CreateChild("DirectionalLight"); lightNode.SetDirection(new Vector3(0.5f, -1.0f, 0.5f)); Light light = lightNode.CreateComponent<Light>(); light.LightType = LightType.LIGHT_DIRECTIONAL; light.Color = new Color(0.2f, 0.2f, 0.2f); light.SpecularIntensity = 1.0f; // Create a "floor" consisting of several tiles for (int y = -5; y <= 5; ++y) { for (int x = -5; x <= 5; ++x) { Node floorNode = scene.CreateChild("FloorTile"); floorNode.Position = new Vector3(x*20.5f, -0.5f, y*20.5f); floorNode.Scale = new Vector3(20.0f, 1.0f, 20.0f); StaticModel floorObject = floorNode.CreateComponent<StaticModel>(); floorObject.Model = cache.Get<Model>("Models/Box.mdl"); floorObject.SetMaterial(cache.Get<Material>("Materials/Stone.xml")); } } // Create a "screen" like object for viewing the second scene. Construct it from two StaticModels, a box for the frame // and a plane for the actual view { Node boxNode = scene.CreateChild("ScreenBox"); boxNode.Position = new Vector3(0.0f, 10.0f, 0.0f); boxNode.Scale = new Vector3(21.0f, 16.0f, 0.5f); StaticModel boxObject = boxNode.CreateComponent<StaticModel>(); boxObject.Model = cache.Get<Model>("Models/Box.mdl"); boxObject.SetMaterial(cache.Get<Material>("Materials/Stone.xml")); Node screenNode = scene.CreateChild("Screen"); screenNode.Position = new Vector3(0.0f, 10.0f, -0.27f); screenNode.Rotation = new Quaternion(-90.0f, 0.0f, 0.0f); screenNode.Scale = new Vector3(20.0f, 0.0f, 15.0f); StaticModel screenObject = screenNode.CreateComponent<StaticModel>(); screenObject.Model = cache.Get<Model>("Models/Plane.mdl"); // Create a renderable texture (1024x768, RGB format), enable bilinear filtering on it Texture2D renderTexture = new Texture2D(); renderTexture.SetSize(1024, 768, Graphics.GetRGBFormat(), TextureUsage.TEXTURE_RENDERTARGET); renderTexture.FilterMode = TextureFilterMode.FILTER_BILINEAR; // Create a new material from scratch, use the diffuse unlit technique, assign the render texture // as its diffuse texture, then assign the material to the screen plane object Material renderMaterial = new Material(); renderMaterial.SetTechnique(0, cache.Get<Technique>("Techniques/DiffUnlit.xml"), 0, 0); renderMaterial.SetTexture(TextureUnit.TU_DIFFUSE, renderTexture); screenObject.SetMaterial(renderMaterial); // Get the texture's RenderSurface object (exists when the texture has been created in rendertarget mode) // and define the viewport for rendering the second scene, similarly as how backbuffer viewports are defined // to the Renderer subsystem. By default the texture viewport will be updated when the texture is visible // in the main view RenderSurface surface = renderTexture.RenderSurface; Viewport rttViewport = new Viewport(rttScene, rttCameraNode.GetComponent<Camera>()); surface.SetViewport(0, rttViewport); } // Create the camera. Limit far clip distance to match the fog CameraNode = scene.CreateChild("Camera"); var 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, 7.0f, -30.0f); } }
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); }
void CreateScene() { scene = new Scene(); scene.CreateComponent<Octree>(); spriteNodes = new List<NodeInfo>((int) NumSprites); // Create camera node CameraNode = scene.CreateChild("Camera"); // Set camera's position CameraNode.Position = (new Vector3(0.0f, 0.0f, -10.0f)); Camera camera = CameraNode.CreateComponent<Camera>(); camera.Orthographic = true; var graphics = GetSubsystem<Graphics>(); camera.OrthoSize=(float)graphics.Height * PixelSize; var cache = GetSubsystem<ResourceCache>(); // Get sprite Sprite2D sprite = cache.Get<Sprite2D>("Urho2D/Aster.png"); if (sprite == null) return; float halfWidth = graphics.Width * 0.5f * PixelSize; float halfHeight = graphics.Height * 0.5f * PixelSize; for (uint i = 0; i < NumSprites; ++i) { Node spriteNode = scene.CreateChild("StaticSprite2D"); spriteNode.Position = (new Vector3(NextRandom(-halfWidth, halfWidth), NextRandom(-halfHeight, halfHeight), 0.0f)); StaticSprite2D staticSprite = spriteNode.CreateComponent<StaticSprite2D>(); // Set random color staticSprite.Color = (new Color(NextRandom(1.0f), NextRandom(1.0f), NextRandom(1.0f), 1.0f)); // Set blend mode staticSprite.BlendMode = BlendMode.BLEND_ALPHA; // Set sprite staticSprite.Sprite=sprite; // Add to sprite node vector spriteNodes.Add(new NodeInfo(spriteNode, new Vector3(NextRandom(-2.0f, 2.0f), NextRandom(-2.0f, 2.0f), 0.0f), NextRandom(-90.0f, 90.0f))); } // Get animation set AnimationSet2D animationSet = cache.Get<AnimationSet2D>("Urho2D/GoldIcon.scml"); if (animationSet == null) return; var spriteNode2 = scene.CreateChild("AnimatedSprite2D"); spriteNode2.Position = (new Vector3(0.0f, 0.0f, -1.0f)); AnimatedSprite2D animatedSprite = spriteNode2.CreateComponent<AnimatedSprite2D>(); // Set animation animatedSprite.AnimationSet = animationSet; animatedSprite.SetAnimation("idle", LoopMode2D.LM_DEFAULT); }
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(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.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 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.Get<Model>("Models/Box.mdl"); skybox.SetMaterial(cache.Get<Material>("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.Get<Model>("Models/Box.mdl"); floorObject.SetMaterial(cache.Get<Material>("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.Get<Model>("Models/Box.mdl"); boxObject.SetMaterial(cache.Get<Material>("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)); }
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>(); scene.CreateComponent<DebugRenderer>(); // Create scene node & StaticModel component for showing a static plane var planeNode = scene.CreateChild("Plane"); planeNode.Scale = new Vector3(100, 1, 100); var 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 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.15f, 0.15f, 0.15f); zone.FogColor = new Color(0.5f, 0.5f, 0.7f); zone.FogStart = 100; zone.FogEnd = 300; // 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); // 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 animated models const int numModels = 100; const float modelMoveSpeed = 2.0f; const float modelRotateSpeed = 100.0f; var bounds = new BoundingBox(new Vector3(-47.0f, 0.0f, -47.0f), new Vector3(47.0f, 0.0f, 47.0f)); for (var i = 0; i < numModels; ++i) { var modelNode = scene.CreateChild("Jack"); modelNode.Position = new Vector3(NextRandom(-45, 45), 0.0f, NextRandom(-45, 45)); modelNode.Rotation = new Quaternion(0, NextRandom(0, 360), 0); //var modelObject = modelNode.CreateComponent<AnimatedModel>(); var modelObject = new AnimatedModel(); modelNode.AddComponent(modelObject); modelObject.Model = cache.Get<Model>("Models/Jack.mdl"); //modelObject.Material = cache.GetMaterial("Materials/Jack.xml"); modelObject.CastShadows = true; // Create an AnimationState for a walk animation. Its time position will need to be manually updated to advance the // animation, The alternative would be to use an AnimationController component which updates the animation automatically, // but we need to update the model's position manually in any case var walkAnimation = cache.Get<Animation>("Models/Jack_Walk.ani"); var state = modelObject.AddAnimationState(walkAnimation); // The state would fail to create (return null) if the animation was not found if (state != null) { // Enable full blending weight and looping state.Weight = 1; state.Looped = true; } // Create our custom Mover component that will move & animate the model during each frame's update var mover = new Mover(modelMoveSpeed, modelRotateSpeed, bounds); modelNode.AddComponent(mover); } // Create the camera. Limit far clip distance to match the fog CameraNode = scene.CreateChild("Camera"); camera = CameraNode.CreateComponent<Camera>(); camera.FarClip = 300; // Set an initial position for the camera scene node above the plane CameraNode.Position = new Vector3(0.0f, 5.0f, 0.0f); }
public override void Start() { Art.Load(); var graphics = AtomicNET.GetSubsystem<Graphics>(); float width = graphics.Width; float height = graphics.Height; ScreenSize = new Vector2(width, height); ScreenBounds = new IntRect(0, 0, (int)ScreenSize.X, (int)ScreenSize.Y); var renderer = AtomicNET.GetSubsystem<Renderer>(); var viewport = renderer.GetViewport(0); renderer.HDRRendering = true; var renderpath = viewport.GetRenderPath().Clone(); renderpath.Append(AtomicNET.Cache.GetResource<XMLFile>("RenderPath/BloomHDR.xml")); renderpath.Append(AtomicNET.Cache.GetResource<XMLFile>("RenderPath/Blur.xml")); viewport.SetRenderPath(renderpath); Scene = new Scene(); Scene.CreateComponent<Octree>(); var camera = Scene.CreateChild("Camera").CreateComponent<Camera>(); camera.Node.Position = new Vector3(width / 2.0f, height / 2.0f, 0.0f); camera.Orthographic = true; camera.OrthoSize = height; viewport.Scene = Scene; viewport.Camera = camera; CustomRenderer.Initialize(); ParticleManager = new ParticleManager<ParticleState>(1024 * 20, ParticleState.UpdateParticle); #if ATOMIC_DESKTOP const int maxGridPoints = 1600; #else const int maxGridPoints = 400; #endif float amt = (float)Math.Sqrt(ScreenBounds.Width * ScreenBounds.Height / maxGridPoints); Vector2 gridSpacing = new Vector2(amt, amt); IntRect expandedBounds = ScreenBounds; expandedBounds.Inflate((int)gridSpacing.X, (int)gridSpacing.Y); Grid = new Grid(expandedBounds, gridSpacing); EntityManager.Add(PlayerShip.Instance); SubscribeToEvent("Update", HandleUpdate); SubscribeToEvent("RenderPathEvent", HandleRenderPathEvent); }
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); }
void CreateScene() { var cache = GetSubsystem<ResourceCache>(); scene = new Scene(); // Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will // show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates; it // is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically // optimizing manner scene.CreateComponent<Octree>(); // Create a child scene node (at world origin) and a StaticModel component into it. Set the StaticModel to show a simple // plane mesh with a "stone" material. Note that naming the scene nodes is optional. Scale the scene node larger // (100 x 100 world units) 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 directional light to the world so that we can see something. The light scene node's orientation controls the // light direction; we will use the SetDirection() function which calculates the orientation from a forward direction vector. // The light will use default settings (white light, no shadows) Node lightNode = scene.CreateChild("DirectionalLight"); lightNode.SetDirection(new Vector3(0.6f, -1.0f, 0.8f)); // The direction vector does not need to be normalized Light light = lightNode.CreateComponent<Light>(); light.LightType = LightType.LIGHT_DIRECTIONAL; // Create more StaticModel objects to the scene, randomly positioned, rotated and scaled. For rotation, we construct a // quaternion from Euler angles where the Y angle (rotation about the Y axis) is randomized. The mushroom model contains // LOD levels, so the StaticModel component will automatically select the LOD level according to the view distance (you'll // see the model get simpler as it moves further away). Finally, rendering a large number of the same object with the // same material allows instancing to be used, if the GPU supports it. This reduces the amount of CPU work in rendering the // scene. Material mushroomMat = cache.Get<Material>("Materials/Mushroom.xml"); // Apply shader parameter animation to material ValueAnimation specColorAnimation=new ValueAnimation(); specColorAnimation.SetKeyFrame(0.0f, new Color(0.1f, 0.1f, 0.1f, 16.0f)); specColorAnimation.SetKeyFrame(1.0f, new Color(1.0f, 0.0f, 0.0f, 2.0f)); specColorAnimation.SetKeyFrame(2.0f, new Color(1.0f, 1.0f, 0.0f, 2.0f)); specColorAnimation.SetKeyFrame(3.0f, new Color(0.1f, 0.1f, 0.1f, 16.0f)); // Optionally associate material with scene to make sure shader parameter animation respects scene time scale mushroomMat.Scene=scene; mushroomMat.SetShaderParameterAnimation("MatSpecColor", specColorAnimation, WrapMode.WM_LOOP, 1.0f); const uint numObjects = 200; for (uint i = 0; i < numObjects; ++i) { Node mushroomNode = scene.CreateChild("Mushroom"); mushroomNode.Position = (new Vector3(NextRandom(90.0f) - 45.0f, 0.0f, NextRandom(90.0f) - 45.0f)); mushroomNode.Rotation=new Quaternion(0.0f, NextRandom(360.0f), 0.0f); mushroomNode.SetScale(0.5f + NextRandom(2.0f)); StaticModel mushroomObject = mushroomNode.CreateComponent<StaticModel>(); mushroomObject.Model = (cache.Get<Model>("Models/Mushroom.mdl")); mushroomObject.SetMaterial(mushroomMat); } // Create a scene node for the camera, which we will move around // The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically) CameraNode = scene.CreateChild("Camera"); CameraNode.CreateComponent<Camera>(); // Set an initial position for the camera scene node above the plane CameraNode.Position = (new Vector3(0.0f, 5.0f, 0.0f)); }
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 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); }
void CreateScene() { scene = new Scene(); scene.CreateComponent<Octree>(); scene.CreateComponent<DebugRenderer>(); PhysicsWorld2D physicsWorld = scene.CreateComponent<PhysicsWorld2D>(); // Create 2D physics world component physicsWorld.DrawJoint = true; // Display the joints (Note that DrawDebugGeometry() must be set to true to acually draw the joints) drawDebug = true; // Set DrawDebugGeometry() to true // Create camera CameraNode = scene.CreateChild("Camera"); // Set camera's position CameraNode.Position = (new Vector3(0.0f, 0.0f, 0.0f)); // Note that Z setting is discarded; use camera.zoom instead (see MoveCamera() below for example) camera = CameraNode.CreateComponent<Camera>(); camera.Orthographic = true; var graphics = GetSubsystem<Graphics>(); camera.OrthoSize = (float)graphics.Height * PixelSize; camera.Zoom = 1.2f * Math.Min((float)graphics.Width / 1280.0f, (float)graphics.Height / 800.0f); // Set zoom according to user's resolution to ensure full visibility (initial zoom (1.2) is set for full visibility at 1280x800 resolution) // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen Viewport viewport = new Viewport(scene, camera); Renderer renderer = GetSubsystem<Renderer>(); renderer.SetViewport(0, viewport); Zone zone = renderer.DefaultZone; zone.FogColor = (new Color(0.1f, 0.1f, 0.1f)); // Set background color for the scene // Create 4x3 grid for (uint i = 0; i < 5; ++i) { Node edgeNode = scene.CreateChild("VerticalEdge"); RigidBody2D edgeBody = edgeNode.CreateComponent<RigidBody2D>(); if (dummyBody == null) dummyBody = edgeBody; // Mark first edge as dummy body (used by mouse pick) CollisionEdge2D edgeShape = edgeNode.CreateComponent<CollisionEdge2D>(); edgeShape.SetVertices(new Vector2(i * 2.5f - 5.0f, -3.0f), new Vector2(i * 2.5f - 5.0f, 3.0f)); edgeShape.Friction = 0.5f; // Set friction } for (uint j = 0; j < 4; ++j) { Node edgeNode = scene.CreateChild("HorizontalEdge"); /*RigidBody2D edgeBody = */ edgeNode.CreateComponent<RigidBody2D>(); CollisionEdge2D edgeShape = edgeNode.CreateComponent<CollisionEdge2D>(); edgeShape.SetVertices(new Vector2(-5.0f, j * 2.0f - 3.0f), new Vector2(5.0f, j * 2.0f - 3.0f)); edgeShape.Friction = 0.5f; // Set friction } var cache = GetSubsystem<ResourceCache>(); // Create a box (will be cloned later) Node box = scene.CreateChild("Box"); box.Position = (new Vector3(0.8f, -2.0f, 0.0f)); StaticSprite2D boxSprite = box.CreateComponent<StaticSprite2D>(); boxSprite.Sprite = cache.Get<Sprite2D>("Urho2D/Box.png"); RigidBody2D boxBody = box.CreateComponent<RigidBody2D>(); boxBody.BodyType = BodyType2D.BT_DYNAMIC; boxBody.LinearDamping = 0.0f; boxBody.AngularDamping = 0.0f; CollisionBox2D shape = box.CreateComponent<CollisionBox2D>(); // Create box shape shape.Size = new Vector2(0.32f, 0.32f); // Set size shape.Density = 1.0f; // Set shape density (kilograms per meter squared) shape.Friction = 0.5f; // Set friction shape.Restitution = 0.1f; // Set restitution (slight bounce) // Create a ball (will be cloned later) Node ball = scene.CreateChild("Ball"); ball.Position = (new Vector3(1.8f, -2.0f, 0.0f)); StaticSprite2D ballSprite = ball.CreateComponent<StaticSprite2D>(); ballSprite.Sprite = cache.Get<Sprite2D>("Urho2D/Ball.png"); RigidBody2D ballBody = ball.CreateComponent<RigidBody2D>(); ballBody.BodyType = BodyType2D.BT_DYNAMIC; ballBody.LinearDamping = 0.0f; ballBody.AngularDamping = 0.0f; CollisionCircle2D ballShape = ball.CreateComponent<CollisionCircle2D>(); // Create circle shape ballShape.Radius = 0.16f; // Set radius ballShape.Density = 1.0f; // Set shape density (kilograms per meter squared) ballShape.Friction = 0.5f; // Set friction ballShape.Restitution = 0.6f; // Set restitution: make it bounce // Create a polygon Node polygon = scene.CreateChild("Polygon"); polygon.Position = (new Vector3(1.6f, -2.0f, 0.0f)); polygon.SetScale(0.7f); StaticSprite2D polygonSprite = polygon.CreateComponent<StaticSprite2D>(); polygonSprite.Sprite = cache.Get<Sprite2D>("Urho2D/Aster.png"); RigidBody2D polygonBody = polygon.CreateComponent<RigidBody2D>(); polygonBody.BodyType = BodyType2D.BT_DYNAMIC; CollisionPolygon2D polygonShape = polygon.CreateComponent<CollisionPolygon2D>(); polygonShape.VertexCount = 6; // Set number of vertices (mandatory when using SetVertex()) polygonShape.SetVertex(0, new Vector2(-0.8f, -0.3f)); polygonShape.SetVertex(1, new Vector2(0.5f, -0.8f)); polygonShape.SetVertex(2, new Vector2(0.8f, -0.3f)); polygonShape.SetVertex(3, new Vector2(0.8f, 0.5f)); polygonShape.SetVertex(4, new Vector2(0.5f, 0.9f)); polygonShape.SetVertex(5, new Vector2(-0.5f, 0.7f)); polygonShape.Density = 1.0f; // Set shape density (kilograms per meter squared) polygonShape.Friction = 0.3f; // Set friction polygonShape.Restitution = 0.0f; // Set restitution (no bounce) // Create a ConstraintDistance2D CreateFlag("ConstraintDistance2D", -4.97f, 3.0f); // Display Text3D flag Node boxDistanceNode = box.Clone(CreateMode.REPLICATED); Node ballDistanceNode = ball.Clone(CreateMode.REPLICATED); RigidBody2D ballDistanceBody = ballDistanceNode.GetComponent<RigidBody2D>(); boxDistanceNode.Position = (new Vector3(-4.5f, 2.0f, 0.0f)); ballDistanceNode.Position = (new Vector3(-3.0f, 2.0f, 0.0f)); ConstraintDistance2D constraintDistance = boxDistanceNode.CreateComponent<ConstraintDistance2D>(); // Apply ConstraintDistance2D to box constraintDistance.OtherBody = ballDistanceBody; // Constrain ball to box constraintDistance.OwnerBodyAnchor = boxDistanceNode.Position2D; constraintDistance.OtherBodyAnchor = ballDistanceNode.Position2D; // Make the constraint soft (comment to make it rigid, which is its basic behavior) constraintDistance.FrequencyHz = 4.0f; constraintDistance.DampingRatio = 0.5f; // Create a ConstraintFriction2D ********** Not functional. From Box2d samples it seems that 2 anchors are required, Urho2D only provides 1, needs investigation *********** CreateFlag("ConstraintFriction2D", 0.03f, 1.0f); // Display Text3D flag Node boxFrictionNode = box.Clone(CreateMode.REPLICATED); Node ballFrictionNode = ball.Clone(CreateMode.REPLICATED); boxFrictionNode.Position = (new Vector3(0.5f, 0.0f, 0.0f)); ballFrictionNode.Position = (new Vector3(1.5f, 0.0f, 0.0f)); ConstraintFriction2D constraintFriction = boxFrictionNode.CreateComponent<ConstraintFriction2D>(); // Apply ConstraintDistance2D to box constraintFriction.OtherBody = ballFrictionNode.GetComponent<RigidBody2D>(); // Constraint ball to box // Create a ConstraintGear2D CreateFlag("ConstraintGear2D", -4.97f, -1.0f); // Display Text3D flag Node baseNode = box.Clone(CreateMode.REPLICATED); RigidBody2D tempBody = baseNode.GetComponent<RigidBody2D>(); // Get body to make it static tempBody.BodyType = BodyType2D.BT_STATIC; baseNode.Position = (new Vector3(-3.7f, -2.5f, 0.0f)); Node ball1Node = ball.Clone(CreateMode.REPLICATED); ball1Node.Position = (new Vector3(-4.5f, -2.0f, 0.0f)); RigidBody2D ball1Body = ball1Node.GetComponent<RigidBody2D>(); Node ball2Node = ball.Clone(CreateMode.REPLICATED); ball2Node.Position = (new Vector3(-3.0f, -2.0f, 0.0f)); RigidBody2D ball2Body = ball2Node.GetComponent<RigidBody2D>(); ConstraintRevolute2D gear1 = baseNode.CreateComponent<ConstraintRevolute2D>(); // Apply constraint to baseBox gear1.OtherBody = ball1Body; // Constrain ball1 to baseBox gear1.Anchor = ball1Node.Position2D; ConstraintRevolute2D gear2 = baseNode.CreateComponent<ConstraintRevolute2D>(); // Apply constraint to baseBox gear2.OtherBody = ball2Body; // Constrain ball2 to baseBox gear2.Anchor = ball2Node.Position2D; ConstraintGear2D constraintGear = ball1Node.CreateComponent<ConstraintGear2D>(); // Apply constraint to ball1 constraintGear.OtherBody = ball2Body; // Constrain ball2 to ball1 constraintGear.OwnerConstraint = gear1; constraintGear.OtherConstraint = gear2; constraintGear.Ratio = 1.0f; ball1Body.ApplyAngularImpulse(0.015f, true); // Animate // Create a vehicle from a compound of 2 ConstraintWheel2Ds CreateFlag("ConstraintWheel2Ds compound", -2.45f, -1.0f); // Display Text3D flag Node car = box.Clone(CreateMode.REPLICATED); car.Scale = new Vector3(4.0f, 1.0f, 0.0f); car.Position = (new Vector3(-1.2f, -2.3f, 0.0f)); StaticSprite2D tempSprite = car.GetComponent<StaticSprite2D>(); // Get car Sprite in order to draw it on top tempSprite.OrderInLayer = 0; // Draw car on top of the wheels (set to -1 to draw below) Node ball1WheelNode = ball.Clone(CreateMode.REPLICATED); ball1WheelNode.Position = (new Vector3(-1.6f, -2.5f, 0.0f)); Node ball2WheelNode = ball.Clone(CreateMode.REPLICATED); ball2WheelNode.Position = (new Vector3(-0.8f, -2.5f, 0.0f)); ConstraintWheel2D wheel1 = car.CreateComponent<ConstraintWheel2D>(); wheel1.OtherBody = ball1WheelNode.GetComponent<RigidBody2D>(); wheel1.Anchor = ball1WheelNode.Position2D; wheel1.Axis = new Vector2(0.0f, 1.0f); wheel1.MaxMotorTorque = 20.0f; wheel1.FrequencyHz = 4.0f; wheel1.DampingRatio = 0.4f; ConstraintWheel2D wheel2 = car.CreateComponent<ConstraintWheel2D>(); wheel2.OtherBody = ball2WheelNode.GetComponent<RigidBody2D>(); wheel2.Anchor = ball2WheelNode.Position2D; wheel2.Axis = new Vector2(0.0f, 1.0f); wheel2.MaxMotorTorque = 10.0f; wheel2.FrequencyHz = 4.0f; wheel2.DampingRatio = 0.4f; // ConstraintMotor2D CreateFlag("ConstraintMotor2D", 2.53f, -1.0f); // Display Text3D flag Node boxMotorNode = box.Clone(CreateMode.REPLICATED); tempBody = boxMotorNode.GetComponent<RigidBody2D>(); // Get body to make it static tempBody.BodyType = BodyType2D.BT_STATIC; Node ballMotorNode = ball.Clone(CreateMode.REPLICATED); boxMotorNode.Position = (new Vector3(3.8f, -2.1f, 0.0f)); ballMotorNode.Position = (new Vector3(3.8f, -1.5f, 0.0f)); ConstraintMotor2D constraintMotor = boxMotorNode.CreateComponent<ConstraintMotor2D>(); constraintMotor.OtherBody = ballMotorNode.GetComponent<RigidBody2D>(); // Constrain ball to box constraintMotor.LinearOffset = new Vector2(0.0f, 0.8f); // Set ballNode position relative to boxNode position = (0,0) constraintMotor.AngularOffset = 0.1f; constraintMotor.MaxForce = 5.0f; constraintMotor.MaxTorque = 10.0f; constraintMotor.CorrectionFactor = 1.0f; constraintMotor.CollideConnected = true; // doesn't work // ConstraintMouse2D is demonstrated in HandleMouseButtonDown() function. It is used to "grasp" the sprites with the mouse. CreateFlag("ConstraintMouse2D", 0.03f, -1.0f); // Display Text3D flag // Create a ConstraintPrismatic2D CreateFlag("ConstraintPrismatic2D", 2.53f, 3.0f); // Display Text3D flag Node boxPrismaticNode = box.Clone(CreateMode.REPLICATED); tempBody = boxPrismaticNode.GetComponent<RigidBody2D>(); // Get body to make it static tempBody.BodyType = BodyType2D.BT_STATIC; Node ballPrismaticNode = ball.Clone(CreateMode.REPLICATED); boxPrismaticNode.Position = new Vector3(3.3f, 2.5f, 0.0f); ballPrismaticNode.Position = new Vector3(4.3f, 2.0f, 0.0f); ConstraintPrismatic2D constraintPrismatic = boxPrismaticNode.CreateComponent<ConstraintPrismatic2D>(); constraintPrismatic.OtherBody = ballPrismaticNode.GetComponent<RigidBody2D>(); // Constrain ball to box constraintPrismatic.Axis = new Vector2(1.0f, 1.0f); // Slide from [0,0] to [1,1] constraintPrismatic.Anchor = new Vector2(4.0f, 2.0f); constraintPrismatic.LowerTranslation = -1.0f; constraintPrismatic.UpperTranslation = 0.5f; constraintPrismatic.EnableLimit = true; constraintPrismatic.MaxMotorForce = 1.0f; constraintPrismatic.MotorSpeed = 0.0f; // ConstraintPulley2D CreateFlag("ConstraintPulley2D", 0.03f, 3.0f); // Display Text3D flag Node boxPulleyNode = box.Clone(CreateMode.REPLICATED); Node ballPulleyNode = ball.Clone(CreateMode.REPLICATED); boxPulleyNode.Position = (new Vector3(0.5f, 2.0f, 0.0f)); ballPulleyNode.Position = (new Vector3(2.0f, 2.0f, 0.0f)); ConstraintPulley2D constraintPulley = boxPulleyNode.CreateComponent<ConstraintPulley2D>(); // Apply constraint to box constraintPulley.OtherBody = ballPulleyNode.GetComponent<RigidBody2D>(); // Constrain ball to box constraintPulley.OwnerBodyAnchor = boxPulleyNode.Position2D; constraintPulley.OtherBodyAnchor = ballPulleyNode.Position2D; constraintPulley.OwnerBodyGroundAnchor = boxPulleyNode.Position2D + new Vector2(0.0f, 1.0f); constraintPulley.OtherBodyGroundAnchor = ballPulleyNode.Position2D + new Vector2(0.0f, 1.0f); constraintPulley.Ratio = 1.0f; // Weight ratio between ownerBody and otherBody // Create a ConstraintRevolute2D CreateFlag("ConstraintRevolute2D", -2.45f, 3.0f); // Display Text3D flag Node boxRevoluteNode = box.Clone(CreateMode.REPLICATED); tempBody = boxRevoluteNode.GetComponent<RigidBody2D>(); // Get body to make it static tempBody.BodyType = BodyType2D.BT_STATIC; Node ballRevoluteNode = ball.Clone(CreateMode.REPLICATED); boxRevoluteNode.Position = (new Vector3(-2.0f, 1.5f, 0.0f)); ballRevoluteNode.Position = (new Vector3(-1.0f, 2.0f, 0.0f)); ConstraintRevolute2D constraintRevolute = boxRevoluteNode.CreateComponent<ConstraintRevolute2D>(); // Apply constraint to box constraintRevolute.OtherBody = ballRevoluteNode.GetComponent<RigidBody2D>(); // Constrain ball to box constraintRevolute.Anchor = new Vector2(-1.0f, 1.5f); constraintRevolute.LowerAngle = -1.0f; // In radians constraintRevolute.UpperAngle = 0.5f; // In radians constraintRevolute.EnableLimit = true; constraintRevolute.MaxMotorTorque = 10.0f; constraintRevolute.MotorSpeed = 0.0f; constraintRevolute.EnableMotor = true; // Create a ConstraintRope2D CreateFlag("ConstraintRope2D", -4.97f, 1.0f); // Display Text3D flag Node boxRopeNode = box.Clone(CreateMode.REPLICATED); tempBody = boxRopeNode.GetComponent<RigidBody2D>(); tempBody.BodyType = BodyType2D.BT_STATIC; Node ballRopeNode = ball.Clone(CreateMode.REPLICATED); boxRopeNode.Position = (new Vector3(-3.7f, 0.7f, 0.0f)); ballRopeNode.Position = (new Vector3(-4.5f, 0.0f, 0.0f)); ConstraintRope2D constraintRope = boxRopeNode.CreateComponent<ConstraintRope2D>(); constraintRope.OtherBody = ballRopeNode.GetComponent<RigidBody2D>(); // Constrain ball to box constraintRope.OwnerBodyAnchor = new Vector2(0.0f, -0.5f); // Offset from box (OwnerBody) : the rope is rigid from OwnerBody center to this ownerBodyAnchor constraintRope.MaxLength = 0.9f; // Rope length constraintRope.CollideConnected = true; // Create a ConstraintWeld2D CreateFlag("ConstraintWeld2D", -2.45f, 1.0f); // Display Text3D flag Node boxWeldNode = box.Clone(CreateMode.REPLICATED); Node ballWeldNode = ball.Clone(CreateMode.REPLICATED); boxWeldNode.Position = (new Vector3(-0.5f, 0.0f, 0.0f)); ballWeldNode.Position = (new Vector3(-2.0f, 0.0f, 0.0f)); ConstraintWeld2D constraintWeld = boxWeldNode.CreateComponent<ConstraintWeld2D>(); constraintWeld.OtherBody = ballWeldNode.GetComponent<RigidBody2D>(); // Constrain ball to box constraintWeld.Anchor = boxWeldNode.Position2D; constraintWeld.FrequencyHz = 4.0f; constraintWeld.DampingRatio = 0.5f; // Create a ConstraintWheel2D CreateFlag("ConstraintWheel2D", 2.53f, 1.0f); // Display Text3D flag Node boxWheelNode = box.Clone(CreateMode.REPLICATED); Node ballWheelNode = ball.Clone(CreateMode.REPLICATED); boxWheelNode.Position = (new Vector3(3.8f, 0.0f, 0.0f)); ballWheelNode.Position = (new Vector3(3.8f, 0.9f, 0.0f)); ConstraintWheel2D constraintWheel = boxWheelNode.CreateComponent<ConstraintWheel2D>(); constraintWheel.OtherBody = ballWheelNode.GetComponent<RigidBody2D>(); // Constrain ball to box constraintWheel.Anchor = ballWheelNode.Position2D; constraintWheel.Axis = new Vector2(0.0f, 1.0f); constraintWheel.EnableMotor = true; constraintWheel.MaxMotorTorque = 1.0f; constraintWheel.MotorSpeed = 0.0f; constraintWheel.FrequencyHz = 4.0f; constraintWheel.DampingRatio = 0.5f; constraintWheel.CollideConnected = true; // doesn't work }
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 var planeNode = scene.CreateChild("Plane"); planeNode.Scale =new Vector3(100.0f, 1.0f, 100.0f); var 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 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(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 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); // 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 = 240; for (uint i = 0; i < numMushrooms; ++i) { var mushroomNode = scene.CreateChild("Mushroom"); mushroomNode.Position = new Vector3(NextRandom(90.0f) - 45.0f, 0.0f, NextRandom(90.0f) - 45.0f); mushroomNode.Rotation = new Quaternion(0.0f, NextRandom(360.0f), 0.0f); mushroomNode.SetScale(0.5f + NextRandom(2.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; } // Create randomly sized boxes. If boxes are big enough, make them occluders const uint numBoxes = 20; for (uint i = 0; i < numBoxes; ++i) { var 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 the cameras. Limit far clip distance to match the fog CameraNode = scene.CreateChild("Camera"); Camera camera = CameraNode.CreateComponent<Camera>(); camera.FarClip = 300.0f; // Parent the rear camera node to the front camera node and turn it 180 degrees to face backward // Here, we use the angle-axis constructor for Quaternion instead of the usual Euler angles rearCameraNode = CameraNode.CreateChild("RearCamera"); rearCameraNode.Rotate(Quaternion.FromAxisAngle(Vector3.UnitY, 180.0f), TransformSpace.TS_LOCAL); Camera rearCamera = rearCameraNode.CreateComponent<Camera>(); rearCamera.FarClip = 300.0f; // Because the rear viewport is rather small, disable occlusion culling from it. Use the camera's // "view override flags" for this. We could also disable eg. shadows or force low material quality // if we wanted rearCamera.ViewOverrideFlags = Constants.VO_DISABLE_OCCLUSION; // Set an initial position for the front camera scene node above the plane CameraNode.Position = new Vector3(0.0f, 5.0f, 0.0f); }
void CreateScene() { var cache = GetSubsystem<ResourceCache>(); scene = new Scene(); // Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will // show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates; it // is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically // optimizing manner scene.CreateComponent<Octree>(); // Create a child scene node (at world origin) and a StaticModel component into it. Set the StaticModel to show a simple // plane mesh with a "stone" material. Note that naming the scene nodes is optional. Scale the scene node larger // (100 x 100 world units) 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 point light to the world so that we can see something. Node lightNode = scene.CreateChild("PointLight"); Light light = lightNode.CreateComponent<Light>(); light.LightType = LightType.LIGHT_POINT; light.Range = (10.0f); // Create light animation ObjectAnimation lightAnimation=new ObjectAnimation(); // Create light position animation ValueAnimation positionAnimation=new ValueAnimation(); // Use spline interpolation method positionAnimation.InterpolationMethod= InterpMethod.IM_SPLINE; // Set spline tension positionAnimation.SplineTension=0.7f; positionAnimation.SetKeyFrame(0.0f, new Vector3(-30.0f, 5.0f, -30.0f)); positionAnimation.SetKeyFrame(1.0f, new Vector3(30.0f, 5.0f, -30.0f)); positionAnimation.SetKeyFrame(2.0f, new Vector3(30.0f, 5.0f, 30.0f)); positionAnimation.SetKeyFrame(3.0f, new Vector3(-30.0f, 5.0f, 30.0f)); positionAnimation.SetKeyFrame(4.0f, new Vector3(-30.0f, 5.0f, -30.0f)); // Set position animation lightAnimation.AddAttributeAnimation("Position", positionAnimation, WrapMode.WM_LOOP, 1f); // Create text animation /* ValueAnimation textAnimation=new ValueAnimation(); textAnimation.SetKeyFrame(0.0f, "WHITE"); textAnimation.SetKeyFrame(1.0f, "RED"); textAnimation.SetKeyFrame(2.0f, "YELLOW"); textAnimation.SetKeyFrame(3.0f, "GREEN"); textAnimation.SetKeyFrame(4.0f, "WHITE"); var uiElement = UI.Root.GetChild("animatingText", false); uiElement.SetAttributeAnimation("Text", textAnimation, WrapMode.Loop, 1f); */ // Create light color animation ValueAnimation colorAnimation=new ValueAnimation(); colorAnimation.SetKeyFrame(0.0f, Color.White); colorAnimation.SetKeyFrame(1.0f, Color.Red); colorAnimation.SetKeyFrame(2.0f, Color.Yellow); colorAnimation.SetKeyFrame(3.0f, Color.Green); colorAnimation.SetKeyFrame(4.0f, Color.White); // Set Light component's color animation lightAnimation.AddAttributeAnimation("@Light/Color", colorAnimation, WrapMode.WM_LOOP, 1f); // Apply light animation to light node lightNode.ObjectAnimation=lightAnimation; // Create more StaticModel objects to the scene, randomly positioned, rotated and scaled. For rotation, we construct a // quaternion from Euler angles where the Y angle (rotation about the Y axis) is randomized. The mushroom model contains // LOD levels, so the StaticModel component will automatically select the LOD level according to the view distance (you'll // see the model get simpler as it moves further away). Finally, rendering a large number of the same object with the // same material allows instancing to be used, if the GPU supports it. This reduces the amount of CPU work in rendering the // scene. const uint numObjects = 200; for (uint i = 0; i < numObjects; ++i) { Node mushroomNode = scene.CreateChild("Mushroom"); mushroomNode.Position = (new Vector3(NextRandom(90.0f) - 45.0f, 0.0f, NextRandom(90.0f) - 45.0f)); mushroomNode.Rotation=new Quaternion(0.0f, NextRandom(360.0f), 0.0f); mushroomNode.SetScale(0.5f + NextRandom(2.0f)); StaticModel mushroomObject = mushroomNode.CreateComponent<StaticModel>(); mushroomObject.Model = (cache.Get<Model>("Models/Mushroom.mdl")); mushroomObject.SetMaterial(cache.Get<Material>("Materials/Mushroom.xml")); } // Create a scene node for the camera, which we will move around // The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically) CameraNode = scene.CreateChild("Camera"); CameraNode.CreateComponent<Camera>(); // Set an initial position for the camera scene node above the plane CameraNode.Position = (new Vector3(0.0f, 5.0f, 0.0f)); }
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); }
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>(); var zoneNode = scene.CreateChild("Zone"); Zone zone = zoneNode.CreateComponent<Zone>(); zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f)); zone.AmbientColor = new Color(0.1f, 0.1f, 0.1f); zone.FogStart = 100.0f; zone.FogEnd = 300.0f; var lightNode = scene.CreateChild("DirectionalLight"); lightNode.SetDirection(new Vector3(0.5f, -1.0f, 0.5f)); var light = lightNode.CreateComponent<Light>(); light.LightType = LightType.LIGHT_DIRECTIONAL; light.Color = new Color(0.2f, 0.2f, 0.2f); light.SpecularIntensity = 1.0f; for (int y = -5; y <= 5; ++y) { for (int x = -5; x <= 5; ++x) { var floorNode = scene.CreateChild("FloorTile"); floorNode.Position = new Vector3(x * 20.5f, -0.5f, y * 20.5f); floorNode.Scale = new Vector3(20.0f, 1.0f, 20.0f); var floorObject = floorNode.CreateComponent<StaticModel>(); floorObject.Model = cache.Get<Model>("Models/Box.mdl"); floorObject.SetMaterial(cache.Get<Material>("Materials/Stone.xml")); } } // Create groups of mushrooms, which act as shadow casters const uint numMushroomgroups = 25; const uint numMushrooms = 25; for (uint i = 0; i < numMushroomgroups; ++i) { var groupNode = scene.CreateChild("MushroomGroup"); groupNode.Position = new Vector3(NextRandom(190.0f) - 95.0f, 0.0f, NextRandom(190.0f) - 95.0f); for (uint j = 0; j < numMushrooms; ++j) { var mushroomNode = groupNode.CreateChild("Mushroom"); mushroomNode.Position = new Vector3(NextRandom(25.0f) - 12.5f, 0.0f, NextRandom(25.0f) - 12.5f); mushroomNode.Rotation = new Quaternion(0.0f, NextRandom() * 360.0f, 0.0f); mushroomNode.SetScale(1.0f + NextRandom() * 4.0f); var mushroomObject = mushroomNode.CreateComponent<StaticModel>(); mushroomObject.Model = cache.Get<Model>("Models/Mushroom.mdl"); mushroomObject.SetMaterial(cache.Get<Material>("Materials/Mushroom.xml")); mushroomObject.CastShadows = true; } } // Create billboard sets (floating smoke) const uint numBillboardnodes = 25; const uint numBillboards = 10; for (uint i = 0; i < numBillboardnodes; ++i) { var smokeNode = scene.CreateChild("Smoke"); smokeNode.Position = new Vector3(NextRandom(200.0f) - 100.0f, NextRandom(20.0f) + 10.0f, NextRandom(200.0f) - 100.0f); var billboardObject = smokeNode.CreateComponent<BillboardSet>(); billboardObject.NumBillboards = numBillboards; billboardObject.Material = cache.Get<Material>("Materials/LitSmoke.xml"); billboardObject.Sorted = true; for (uint j = 0; j < numBillboards; ++j) { var bb = billboardObject.GetBillboard(j); bb.Position = new Vector3(NextRandom(12.0f) - 6.0f, NextRandom(8.0f) - 4.0f, NextRandom(12.0f) - 6.0f); bb.Size = new Vector2(NextRandom(2.0f) + 3.0f, NextRandom(2.0f) + 3.0f); bb.Rotation = NextRandom() * 360.0f; bb.Enabled = true; } // After modifying the billboards, they need to be "commited" so that the BillboardSet updates its internals billboardObject.Commit(); } // Create shadow casting spotlights const uint numLights = 9; for (uint i = 0; i < numLights; ++i) { lightNode = scene.CreateChild("SpotLight"); light = lightNode.CreateComponent<Light>(); float angle = 0.0f; Vector3 position = new Vector3((i % 3) * 60.0f - 60.0f, 45.0f, (i / 3) * 60.0f - 60.0f); Color color = new Color(((i + 1) & 1) * 0.5f + 0.5f, (((i + 1) >> 1) & 1) * 0.5f + 0.5f, (((i + 1) >> 2) & 1) * 0.5f + 0.5f); lightNode.Position = position; lightNode.SetDirection(new Vector3((float)Math.Sin(angle), -1.5f, (float)Math.Cos(angle))); light.LightType = LightType.LIGHT_SPOT; light.Range = 90.0f; light.RampTexture = cache.Get<Texture2D>("Textures/RampExtreme.png"); light.Fov = 45.0f; light.Color = color; light.SpecularIntensity = 1.0f; light.CastShadows = true; light.ShadowBias = new BiasParameters(0.00002f, 0.0f); // Configure shadow fading for the lights. When they are far away enough, the lights eventually become unshadowed for // better GPU performance. Note that we could also set the maximum distance for each object to cast shadows light.ShadowFadeDistance = 100.0f; // Fade start distance light.ShadowDistance = 125.0f; // Fade end distance, shadows are disabled // Set half resolution for the shadow maps for increased performance light.ShadowResolution = 0.5f; // The spot lights will not have anything near them, so move the near plane of the shadow camera farther // for better shadow depth resolution light.ShadowNearFarRatio = 0.01f; } // Create the camera. Limit far clip distance to match the fog CameraNode = scene.CreateChild("Camera"); var 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); }