void VehicleDemo::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // 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(context_); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(500.0f); GetSubsystem<Renderer>()->SetViewport(0, new Viewport(context_, 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->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(0.5f, 0.5f, 0.7f)); zone->SetFogStart(300.0f); zone->SetFogEnd(500.0f); zone->SetBoundingBox(BoundingBox(-2000.0f, 2000.0f)); // Create a directional light with cascaded shadow mapping Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.3f, -0.5f, 0.425f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetCastShadows(true); light->SetShadowBias(BiasParameters(0.00025f, 0.5f)); light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f)); light->SetSpecularIntensity(0.5f); // Create heightmap terrain with collision Node* terrainNode = scene_->CreateChild("Terrain"); terrainNode->SetPosition(Vector3::ZERO); Terrain* terrain = terrainNode->CreateComponent<Terrain>(); terrain->SetPatchSize(64); terrain->SetSpacing(Vector3(2.0f, 0.1f, 2.0f)); // Spacing between vertices and vertical resolution of the height map terrain->SetSmoothing(true); terrain->SetHeightMap(cache->GetResource<Image>("Textures/HeightMap.png")); terrain->SetMaterial(cache->GetResource<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->SetOccluder(true); RigidBody* body = terrainNode->CreateComponent<RigidBody>(); body->SetCollisionLayer(2); // Use layer bitmask 2 for static geometry CollisionShape* shape = terrainNode->CreateComponent<CollisionShape>(); shape->SetTerrain(); // Create 1000 mushrooms in the terrain. Always face outward along the terrain normal const unsigned NUM_MUSHROOMS = 1000; for (unsigned i = 0; i < NUM_MUSHROOMS; ++i) { Node* objectNode = scene_->CreateChild("Mushroom"); Vector3 position(Random(2000.0f) - 1000.0f, 0.0f, Random(2000.0f) - 1000.0f); position.y_ = terrain->GetHeight(position) - 0.1f; objectNode->SetPosition(position); // Create a rotation quaternion from up vector to terrain normal objectNode->SetRotation(Quaternion(Vector3::UP, terrain->GetNormal(position))); objectNode->SetScale(3.0f); StaticModel* object = objectNode->CreateComponent<StaticModel>(); object->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl")); object->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml")); object->SetCastShadows(true); RigidBody* body = objectNode->CreateComponent<RigidBody>(); body->SetCollisionLayer(2); CollisionShape* shape = objectNode->CreateComponent<CollisionShape>(); shape->SetTriangleMesh(object->GetModel(), 0); } }
void Water::CreateScene() { ResourceCache* cache = GetContext()->m_ResourceCache.get(); scene_ = new Scene(GetContext()); // Create octree, use 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(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(1.0f, 1.0f, 1.0f)); zone->SetFogStart(500.0f); zone->SetFogEnd(750.0f); // Create a directional light to the world. Enable cascaded shadows on it Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetCastShadows(true); light->SetShadowBias(BiasParameters(0.00025f, 0.5f)); light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f)); light->SetSpecularIntensity(0.5f); // Apply slightly overbright lighting to match the skybox light->SetColor(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 Node* skyNode = scene_->CreateChild("Sky"); skyNode->SetScale(500.0f); // The scale actually does not matter Skybox* skybox = skyNode->CreateComponent<Skybox>(); skybox->SetModel(cache->GetResource<Model>("Models/Box.mdl")); skybox->SetMaterial(cache->GetResource<Material>("Materials/Skybox.xml")); // Create heightmap terrain Node* terrainNode = scene_->CreateChild("Terrain"); terrainNode->SetPosition(Vector3(0.0f, 0.0f, 0.0f)); Terrain* terrain = terrainNode->CreateComponent<Terrain>(); terrain->SetPatchSize(64); terrain->SetSpacing(Vector3(2.0f, 0.5f, 2.0f)); // Spacing between vertices and vertical resolution of the height map terrain->SetSmoothing(true); terrain->SetHeightMap(cache->GetResource<Image>("Textures/HeightMap.png")); terrain->SetMaterial(cache->GetResource<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->SetOccluder(true); // Create 1000 boxes in the terrain. Always face outward along the terrain normal unsigned NUM_OBJECTS = 1000; for (unsigned i = 0; i < NUM_OBJECTS; ++i) { Node* objectNode = scene_->CreateChild("Box"); Vector3 position(Random(2000.0f) - 1000.0f, 0.0f, Random(2000.0f) - 1000.0f); position.y_ = terrain->GetHeight(position) + 2.25f; objectNode->SetPosition(position); // Create a rotation quaternion from up vector to terrain normal objectNode->SetRotation(Quaternion(Vector3(0.0f, 1.0f, 0.0f), terrain->GetNormal(position))); objectNode->SetScale(5.0f); StaticModel* object = objectNode->CreateComponent<StaticModel>(); object->SetModel(cache->GetResource<Model>("Models/Box.mdl")); object->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); object->SetCastShadows(true); } Node* shipNode = scene_->CreateChild("Ship"); shipNode->SetPosition(Vector3(0.0f, 4.6f, 0.0f)); //shipNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f)); shipNode->SetScale(0.5f + Random(2.0f)); StaticModel* shipObject = shipNode->CreateComponent<StaticModel>(); shipObject->SetModel(cache->GetResource<Model>("Models/ship04.mdl")); shipObject->SetMaterial(0,cache->GetResource<Material>("Materials/ship04_Material0.xml")); shipObject->SetMaterial(1,cache->GetResource<Material>("Materials/ship04_Material1.xml")); shipObject->SetMaterial(2,cache->GetResource<Material>("Materials/ship04_Material2.xml")); shipObject->SetCastShadows(true); // Create a water plane object that is as large as the terrain waterNode_ = scene_->CreateChild("Water"); waterNode_->SetScale(Vector3(2048.0f, 1.0f, 2048.0f)); waterNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f)); StaticModel* water = waterNode_->CreateComponent<StaticModel>(); water->SetModel(cache->GetResource<Model>("Models/Plane.mdl")); water->SetMaterial(cache->GetResource<Material>("Materials/Water.xml")); // Set a different viewmask on the water plane to be able to hide it from the reflection camera water->SetViewMask(0x80000000); // Create the camera. Set far clip 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(GetContext()); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->setFarClipDistance(750.0f); // Set an initial position for the camera scene node above the ground cameraNode_->SetPosition(Vector3(0.0f, 7.0f, -20.0f)); }
void TestApp::Run() { if (*mCore << "Config/T08.txt") { mCore->Lock(); // First we need to create a heightmap we'll use to create the terrain. R5 has a fairly flexible // noise library with a variety of simple filters that we can use for just that purpose. Noise noise; // We want to generate a 256x256 heightmap noise.SetSize(256, 256); // You can combine a variety of filters to create the terrain's "final" look, but for the sake // of simplicity, let's only use one -- a perlin noise. The numbers that follow are optional // parameters. In this case '8' means generate an 8-octave noise, and 0.65 means that the noise // with values above 0.65 will be mirrored, turning high peaks into volcano-like crevices. // This type of noise is also known as ridged multifractal due to the ridges it tends to produce. noise.ApplyFilter("Perlin").Set(8.0f, 0.65f); // Now that we have our heightmap, we should create our terrain. mTerrain = mCore->GetRoot()->AddObject<Terrain>("First Terrain"); // We want to partition our terrain into an 8 by 8 grid. This will create 64 subdivisions // that the terrain will use together with frustum culling to automatically discard portions // of the terrain that are not visible. We can actually see what percentage of the terrain // is being rendered by using the Terrain::GetVisibility() function after the scene has been // culled... but more on that later. mTerrain->PartitionInto(8, 8); // In order to fill the terrain's partitions with geometry we need to provide additional // information about the heightmap that will be used and how it will be used to begin with. // Terrain::Heightmap struct exists for just this purpose. // Provide the heightmap itself Terrain::Heightmap hm (noise.GetBuffer(), noise.GetWidth(), noise.GetHeight()); // We want each subdivided mesh to be 32 by 32 quads. As you might recall there are 64 subdivisions // in total, and now each of those 64 will contain (32 x 32) = 1024 quads, or 2048 triangles. // When the terrain is generated the provided heightmap will be sampled using bicubic filtering, // so you can make the mesh much more tessellated than the heightmap, if you wish. hm.mMeshSize.Set(32, 32); // By default the terrain will be generated with dimensions of (0, 0, 0) to (1, 1, 1). Of course // that's not what we want. Let's apply a different scaling property here, stretching the terrain // along the horizontal plane (and a little bit along the vertical as well). hm.mTerrainScale.Set(20.0f, 20.0f, 4.0f); // By default the terrain starts at (0, 0, 0). Let's somwhat-center it instead. hm.mTerrainOffset.Set(-10.0f, -10.0f, -3.0f); // Time to fill the actual geometry. One last important thing to note is the optional bounding // box padding parameter that QuadTree::Fill function accepts. This parameter is used to extrude // the height of the bounding box vertically in both directions so that child objects can // fit easier. Objects that "fit" into the bounding box of the terrain's subdivisioned // nodes will get culled faster, speeding up your game. I recommend setting this property to // the height of the tallest building or tree you expect to place on your map. In this // example we don't have any objects placed as children of the terrain, but it's worth // noting nonetheless. mTerrain->FillGeometry(&hm, 0.0f); // And now... we need to be able to see the terrain we've just created. // The best way to visualize a terrain without any textures on it is to display it in wireframe. // You can easily do that in R5 by using a material that has a "Wireframe" technique as one of // its draw methods. As long as you won't forget to use that technique in the OnDraw function, // your wireframe object will show up in your scene. In this case it will be used for our terrain. // Wireframe is an R5-recognized technique so we don't need to set up any states. ITechnique* wireframe = mGraphics->GetTechnique("Wireframe"); // Save it for our Draw function //mTechniques.Expand() = wireframe; // We'll be using a custom material to draw our terrain. Let's just give it the same name. IMaterial* mat = mGraphics->GetMaterial("Terrain"); // Se need to change the material's color as all newly created materials start invisible (alpha of 0) mat->SetDiffuse( Color4ub(255, 255, 255, 255) ); // Add this technique to the material mat->GetDrawMethod(wireframe, true); // Tell the terrain to use this material mTerrain->SetMaterial(mat); // Last thing we should do is find the label I've added to the "T08.txt" configuration file. // The reason it's not created via code is to simplify this tutorial. If you're curious, // have a look at that resource file and see how it was created inside. Since the label is // part of the configuration file that we've loaded at the top of this function, it's already // in memory and all we have to do is find it using this handy template: mLabel = mUI->FindWidget<UILabel>("Status"); // Add a custom draw function that will update the label showing us how much of the terrain is // actually visible at any given time. Look below to see exactly what it does. mCore->AddOnDraw( bind(&TestApp::OnDraw, this) ); // Enter the message processing loop mCore->Unlock(); while (mCore->Update()); //*mCore >> "Config/T08.txt"; } }
void VehicleDemo::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // Create scene subsystem components scene_->CreateComponent<Octree>(); scene_->CreateComponent<PhysicsWorld>(); scene_->CreateComponent<DebugRenderer>(); // 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(context_); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(500.0f); GetSubsystem<Renderer>()->SetViewport(0, new Viewport(context_, scene_, camera)); //GetSubsystem<Renderer>()->SetHDRRendering(true); RenderPath* effectRenderPath=GetSubsystem<Renderer>()->GetViewport(0)->GetRenderPath(); //effectRenderPath->Append(cache->GetResource<XMLFile>("PostProcess/AutoExposure.xml")); effectRenderPath->Append(cache->GetResource<XMLFile>("PostProcess/Blur.xml")); effectRenderPath->SetShaderParameter("BlurRadius", Variant(0.002f) ); effectRenderPath->SetShaderParameter("BlurSigma", Variant(0.001f) ); effectRenderPath->SetEnabled("Blur", false); // 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->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(0.2f, 0.2f, 0.3f)); zone->SetFogStart(300.0f); zone->SetFogEnd(500.0f); zone->SetBoundingBox(BoundingBox(-2000.0f, 2000.0f)); /* // Create a directional light with cascaded shadow mapping Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.3f, -0.5f, 0.425f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetCastShadows(true); light->SetShadowBias(BiasParameters(0.00025f, 0.5f)); light->SetShadowCascade(CascadeParameters(20.0f, 50.0f, 200.0f, 0.0f, 0.8f)); light->SetSpecularIntensity(0.5f); */ // Create heightmap terrain with collision Node* terrainNode = scene_->CreateChild("Terrain"); terrainNode->SetPosition(Vector3::ZERO); Terrain* terrain = terrainNode->CreateComponent<Terrain>(); terrain->SetPatchSize(64); terrain->SetSpacing(Vector3(2.0f, 0.1f, 2.0f)); // Spacing between vertices and vertical resolution of the height map terrain->SetSmoothing(true); terrain->SetHeightMap(cache->GetResource<Image>("Textures/HeightMap.png")); terrain->SetMaterial(cache->GetResource<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->SetOccluder(true); RigidBody* body = terrainNode->CreateComponent<RigidBody>(); body->SetCollisionLayer(2); // Use layer bitmask 2 for static geometry CollisionShape* shape = terrainNode->CreateComponent<CollisionShape>(); shape->SetTerrain(); // 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* skyNode2 = scene_->CreateChild("Sky"); skyNode2->SetScale(80.0f); // The scale actually does not matter Skybox* skybox2 = skyNode2->CreateComponent<Skybox>(); skybox2->SetModel(cache->GetResource<Model>("Models/Box.mdl")); skybox2->SetMaterial(cache->GetResource<Material>("Materials/Skybox.xml")); */ Node* skyNode = scene_->CreateChild("ProcSkyNode"); skyNode->SetEnabled(true); skyNode->SetName("ProcSkyNode"); skyNode->SetPosition(Urho3D::Vector3(0.0, 0.0, 0.0)); skyNode->SetRotation(Urho3D::Quaternion(1, 0, 0, 0)); skyNode->SetScale(Urho3D::Vector3(100.0, 100.0, 100.0)); ProcSky* procSky = skyNode->CreateComponent<ProcSky>(); procSky->SetEnabled(true); Node* skyLightNode = skyNode->CreateChild("ProcSkyLight"); skyLightNode->SetEnabled(true); skyLightNode->SetPosition(Urho3D::Vector3(0.0, 0.0, 0.0)); skyLightNode->SetRotation(Urho3D::Quaternion(0.707107, 0, -0.707107, 0)); skyLightNode->SetScale(Urho3D::Vector3(1, 1, 1)); Light* skyLight = skyLightNode->CreateComponent<Light>(); skyLight->SetLightType(LIGHT_DIRECTIONAL); skyLight->SetColor(Urho3D::Color(0.753, 0.749, 0.678, 1)); skyLight->SetSpecularIntensity(0); skyLight->SetOccludee(false); skyLight->SetOccluder(false); skyLight->SetCastShadows(true); skyLight->SetShadowCascade(Urho3D::CascadeParameters(20, 50, 100, 500, 0.8f)); skyLight->SetShadowFocus(Urho3D::FocusParameters(true, true, true, 1.0f, 5.0f)); skyLight->SetShadowBias(Urho3D::BiasParameters(1e-005, 0.001)); if (skyNode) { //ProcSky* procSky(skyNode->GetComponent<ProcSky>()); if (procSky) { // Can set other parameters here; e.g., SetUpdateMode(), SetUpdateInterval(), SetRenderSize() procSky->Initialize(); URHO3D_LOGINFO("ProcSky Initialized."); } else { URHO3D_LOGERROR("ProcSky node missing ProcSky component."); } } else { URHO3D_LOGERROR("ProcSky node not found in scene."); } // Create 1000 mushrooms in the terrain. Always face outward along the terrain normal /* const unsigned NUM_MUSHROOMS = 0; for (unsigned i = 0; i < NUM_MUSHROOMS; ++i) { Node* objectNode = scene_->CreateChild("SafetyCone"); Vector3 position(Random(2000.0f) - 1000.0f, 0.0f, Random(2000.0f) - 1000.0f); position.y_ = terrain->GetHeight(position); objectNode->SetPosition(position); // Create a rotation quaternion from up vector to terrain normal objectNode->SetRotation(Quaternion(Vector3::UP, terrain->GetNormal(position))); objectNode->SetScale(3.0f); StaticModel* object = objectNode->CreateComponent<StaticModel>(); object->SetModel(cache->GetResource<Model>("MyProjects/SafetyCone/SafetyCone.mdl")); object->SetMaterial(cache->GetResource<Material>("MyProjects/SafetyCone/ConeBase.xml")); object->SetMaterial(cache->GetResource<Material>("MyProjects/SafetyCone/SafetyCone.xml")); object->SetCastShadows(true); RigidBody* body = objectNode->CreateComponent<RigidBody>(); //body->SetCollisionLayer(2); body->SetMass(2.0f); body->SetFriction(0.75f); CollisionShape* shape = objectNode->CreateComponent<CollisionShape>(); //shape->SetTriangleMesh(object->GetModel(), 0); shape->SetConvexHull(object->GetModel(), 0); } */ }