void MasterControl::CreateScene() { world_.scene = new Scene(context_); //Create octree, use default volume (-1000, -1000, -1000) to (1000,1000,1000) { world_.scene->CreateComponent<Octree>(); } //Create the physics { PhysicsWorld * const physicsWorld = world_.scene->CreateComponent<PhysicsWorld>(); physicsWorld->SetGravity(Vector3::ZERO); } world_.scene->CreateComponent<DebugRenderer>(); //Create an invisible plane for mouse raycasting world_.voidNode = world_.scene->CreateChild("Void"); //Location is set in update since the plane moves with the camera. world_.voidNode->SetScale(Vector3(1000.0f, 1.0f, 1000.0f)); StaticModel* planeModel = world_.voidNode->CreateComponent<StaticModel>(); planeModel->SetModel(cache_->GetResource<Model>("Models/Plane.mdl")); planeModel->SetMaterial(cache_->GetResource<Material>("Materials/Terrain.xml")); CreateBackground(); { // 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 = world_.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 a directional light to the world. Enable cascaded shadows on it { Node* lightNode = world_.scene->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.0f, -1.0f, 0.0f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetBrightness(1.0f); light->SetColor(Color(1.0f, 0.8f, 0.7f)); light->SetCastShadows(true); light->SetShadowBias(BiasParameters(0.00025f, 0.5f)); //Set cascade splits at 10, 50, 200 world unitys, fade shadows at 80% of maximum shadow distance light->SetShadowCascade(CascadeParameters(7.0f, 23.0f, 42.0f, 500.0f, 0.8f)); } //Create a second directional light without shadows { Node * const lightNode = world_.scene->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.0, 1.0, 0.0)); Light * const light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetBrightness(0.25); light->SetColor(Color(1.0, 1.0, 1.0)); light->SetCastShadows(true); light->SetShadowBias(BiasParameters(0.00025f, 0.5f)); } //Create camera world_.camera = new CameraMaster(context_, this); }
void Physics::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // 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(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(300.0f); zone->SetFogEnd(500.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)); // Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance light->SetShadowCascade(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->SetModel(cache->GetResource<Model>("Models/Box.mdl")); skybox->SetMaterial(cache->GetResource<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->SetPosition(Vector3(0.0f, -0.5f, 0.0f)); floorNode->SetScale(Vector3(1000.0f, 1.0f, 1000.0f)); StaticModel* floorObject = floorNode->CreateComponent<StaticModel>(); floorObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); floorObject->SetMaterial(cache->GetResource<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 /*RigidBody* body = */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); } { // 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->SetPosition(Vector3((float)x, -(float)y + 8.0f, 0.0f)); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/StoneEnvMapSmall.xml")); boxObject->SetCastShadows(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->SetMass(1.0f); body->SetFriction(0.75f); CollisionShape* shape = boxNode->CreateComponent<CollisionShape>(); shape->SetBox(Vector3::ONE); } } } // 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(context_); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(500.0f); // Set an initial position for the camera scene node above the floor cameraNode_->SetPosition(Vector3(0.0f, 5.0f, -20.0f)); }
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)); }