void RTTScene::CreateScene() { rttScene_ = new Scene(context_); rttScene_->CreateComponent<Octree>(); Node* zoneNode = rttScene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.05f, 0.1f, 0.15f)); zone->SetFogColor(Color(0.1f, 0.2f, 0.3f)); zone->SetFogStart(10.0f); zone->SetFogEnd(100.0f); //用来指定ViewPort时的定位 rttCameraNode_ = rttScene_->CreateChild("Camera"); Camera* camera = rttCameraNode_->CreateComponent<Camera>(); Light* light = rttCameraNode_->CreateComponent<Light>(); light->SetLightType(LIGHT_POINT); renderTexture = new Texture2D(context_); //设置当前场景的ViewPort renderTexture->SetSize(_width,_height,Graphics::GetRGBFormat(),TEXTURE_RENDERTARGET); RenderSurface* surface = renderTexture->GetRenderSurface(); renderTexture->SetFilterMode(FILTER_BILINEAR); Viewport* rttViewPort = new Viewport(context_,rttScene_,camera); surface->SetViewport(0,rttViewPort); }
void LuaIntegration::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // 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 Node* zoneNode = scene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); // Set same volume as the Octree, set a close bluish fog and some ambient light zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.05f, 0.1f, 0.15f)); zone->SetFogColor(Color(0.1f, 0.2f, 0.3f)); zone->SetFogStart(10.0f); zone->SetFogEnd(100.0f); // Create randomly positioned and oriented box StaticModels in the scene const unsigned NUM_OBJECTS = 2000; for (unsigned i = 0; i < NUM_OBJECTS; ++i) { Node* boxNode = scene_->CreateChild("Box"); boxNode->SetPosition(Vector3(Random(200.0f) - 100.0f, Random(200.0f) - 100.0f, Random(200.0f) - 100.0f)); // Orient using random pitch, yaw and roll Euler angles boxNode->SetRotation(Quaternion(Random(360.0f), Random(360.0f), Random(360.0f))); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); // Add our custom Rotator script object (using the LuaScriptInstance C++ component to instantiate / store it) which will // rotate the scene node each frame, when the scene sends its update event LuaScriptInstance* instance = boxNode->CreateComponent<LuaScriptInstance>(); instance->CreateObject("LuaScripts/Rotator.lua", "Rotator"); // Call the script object's "SetRotationSpeed" function. WeakPtr<LuaFunction> function = instance->GetScriptObjectFunction("SetRotationSpeed"); if (function && function->BeginCall(instance)) { function->PushUserType(Vector3(10.0f, 20.0f, 30.0f), "Vector3"); function->EndCall(); } } // 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"); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(100.0f); // Create a point light to the camera scene node Light* light = cameraNode_->CreateComponent<Light>(); light->SetLightType(LIGHT_POINT); light->SetRange(30.0f); }
void Labyrinth::CreateScene() { // ADDED ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // TODO vzdialenost kamery podla rozlisenia obrazovky // 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 // TODO treb vytunit poziciu kamery a jej rotaciu cameraNode_ = new Node(context_); cameraNode_->SetName("cameraNode"); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(300.0f); // aby to vyzeralo ako 2.5D treba prepnut ortho na true /*camera->SetOrthographic(false); cameraNode_->SetPosition(Vector3(10, 90, 10)); cameraNode_->SetRotation(Quaternion(75, 0, 0)); GetSubsystem<Renderer>()->SetViewport(0, new Viewport(context_, scene_, camera));*/ camera->SetOrthographic(false); cameraNode_->SetPosition(Vector3(0, 10, 0)); cameraNode_->SetRotation(Quaternion(90, 0, 0)); // Create static scene content. First create a zone for ambient lightning 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(1000.0f); zone->SetFogEnd(1000.0f); zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.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(1.0f); // ------------------------------------------ CreateFloor(20, 14); }
void Sample::Start() { context_->RegisterSubsystem(new NanoGUI(context_)); NanoGUI* nanogui = GetSubsystem<NanoGUI>(); if (nanogui) { nanogui->Initialize(); } cache_ = GetSubsystem<ResourceCache>(); ui_ = GetSubsystem<UI>(); graphics_ = GetSubsystem<Graphics>(); ////////////////////////////////////////////////////////////////////////// // Get default styles XMLFile* styleFile = cache_->GetResource<XMLFile>("UI/DefaultStyle.xml"); ui_->GetRoot()->SetDefaultStyle(styleFile); // create main ui rootUI_ = ui_->GetRoot()->CreateChild<UIElement>("IDERoot"); rootUI_->SetSize(ui_->GetRoot()->GetSize()); rootUI_->SetTraversalMode(TM_DEPTH_FIRST); // This is needed for root-like element to prevent artifacts rootUI_->SetDefaultStyle(styleFile); ////////////////////////////////////////////////////////////////////////// /// Create console console_ = engine_->CreateConsole(); console_->SetDefaultStyle(styleFile); ////////////////////////////////////////////////////////////////////////// /// Create debug HUD. debugHud_ = engine_->CreateDebugHud(); debugHud_->SetDefaultStyle(styleFile); ////////////////////////////////////////////////////////////////////////// /// Subscribe key down event SubscribeToEvent(E_KEYDOWN, HANDLER(Sample, HandleKeyDown)); // edit clear color, set background color Renderer* renderer = GetSubsystem<Renderer>(); Zone* zone = renderer->GetDefaultZone(); zone->SetFogColor(Color(0.3f, 0.3f, 0.4f)); // Set background color for the scene ////////////////////////////////////////////////////////////////////////// /// Create Cursor // Cursor* cursor_ = new Cursor(context_); // cursor_->SetStyleAuto(styleFile); // ui_->SetCursor(cursor_); // if (GetPlatform() == "Android" || GetPlatform() == "iOS") // ui_->GetCursor()->SetVisible(false); /// Show Platform Cursor Input* input = GetSubsystem<Input>(); input->SetMouseVisible(true); }
void AnimatingScene::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // 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 Node* zoneNode = scene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); // Set same volume as the Octree, set a close bluish fog and some ambient light zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.05f, 0.1f, 0.15f)); zone->SetFogColor(Color(0.1f, 0.2f, 0.3f)); zone->SetFogStart(10.0f); zone->SetFogEnd(100.0f); // Create randomly positioned and oriented box StaticModels in the scene const unsigned NUM_OBJECTS = 2000; for (unsigned i = 0; i < NUM_OBJECTS; ++i) { Node* boxNode = scene_->CreateChild("Box"); boxNode->SetPosition(Vector3(Random(200.0f) - 100.0f, Random(200.0f) - 100.0f, Random(200.0f) - 100.0f)); // Orient using random pitch, yaw and roll Euler angles boxNode->SetRotation(Quaternion(Random(360.0f), Random(360.0f), Random(360.0f))); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<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 LogicComponent, which has convenience functionality to subscribe // to the various update events, and forward them to virtual functions that can be implemented by subclasses. This way // writing logic/update components in C++ becomes similar to scripting. // Now we simply set same rotation speed for all objects Rotator* rotator = boxNode->CreateComponent<Rotator>(); rotator->SetRotationSpeed(Vector3(10.0f, 20.0f, 30.0f)); } // 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"); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(100.0f); // Create a point light to the camera scene node Light* light = cameraNode_->CreateComponent<Light>(); light->SetLightType(LIGHT_POINT); light->SetRange(30.0f); }
void Typography::HandleWhiteBackground(StringHash eventType, VariantMap& eventData) { auto* box = static_cast<CheckBox*>(eventData[Toggled::P_ELEMENT].GetPtr()); bool checked = box->IsChecked(); Color fg = checked ? Color::BLACK : Color::WHITE; Color bg = checked ? Color::WHITE : Color::BLACK; auto* renderer = GetSubsystem<Renderer>(); Zone* zone = renderer->GetDefaultZone(); zone->SetFogColor(bg); PODVector<UIElement*> text = uielement_->GetChildrenWithTag(TEXT_TAG, true); for (int i = 0; i < text.Size(); i++) { text[i]->SetColor(fg); } }
/// Setup the screen UI void GameEconomicGameClient::SetupScreenUI(void) { /// Create a new scene UI scenePlayerUI_= new Scene(context_); scenePlayerUI_-> CreateComponent<Octree>(); scenePlayerUI_-> CreateComponent<DebugRenderer>(); /// Add a lightdelightNode Node* lightNode = scenePlayerUI_->CreateChild("uidirectionallight"); lightNode->SetDirection(Vector3(0.0f, .8f, 3.20f)); /// The direction vector does not need to be normalized Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); /// Create a Zone component for ambient lighting & fog control Node* zoneNode = scenePlayerUI_->CreateChild("uizone"); Zone* zone = zoneNode->CreateComponent<Zone>(); Vector3 boundingBoxMin(-20.0f,-20.0f,-20.0f); Vector3 boundingBoxMax(20.0f,20.0f,20.0f); /// change bounding box to something more accurate zone->SetBoundingBox(BoundingBox(boundingBoxMin,boundingBoxMax)); zone->SetFogColor(Color(.08f, .08f, .08f)); zone->SetFogStart(10.0f); zone->SetFogEnd(20.0f); zone->SetHeightFog (false); /// Add Camera Node * cameraNodePlayerUI_ = scenePlayerUI_->CreateChild("uicamera"); cameraNodePlayerUI_->CreateComponent<Camera>(); /// Set an initial position for the camera scene node above the plane cameraNodePlayerUI_->SetPosition(Vector3(0.0f, 0.8f, 3.0f)); Node * emptyNode= scenePlayerUI_->CreateChild("uiempty"); emptyNode->SetPosition(Vector3(0.0f,0.73f,0.0f)); /// Create character node; Node * characterNode= scenePlayerUI_->CreateChild("Character"); characterNode->SetPosition(Vector3(0.0f,0.0f,0.0f)); cameraNodePlayerUI_ -> LookAt(Vector3(emptyNode->GetPosition())); lightNode -> Rotate(Quaternion(.398377,0.854323,0.141073,-0.302532)); }
void Navigation::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // 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->SetScale(Vector3(100.0f, 1.0f, 100.0f)); StaticModel* planeObject = planeNode->CreateComponent<StaticModel>(); planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl")); planeObject->SetMaterial(cache->GetResource<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(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(0.5f, 0.5f, 0.7f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.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.0001f, 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 some mushrooms const unsigned NUM_MUSHROOMS = 100; for (unsigned i = 0; i < NUM_MUSHROOMS; ++i) CreateMushroom(Vector3(Random(90.0f) - 45.0f, 0.0f, Random(90.0f) - 45.0f)); // Create randomly sized boxes. If boxes are big enough, make them occluders const unsigned NUM_BOXES = 20; for (unsigned i = 0; i < NUM_BOXES; ++i) { Node* boxNode = scene_->CreateChild("Box"); float size = 1.0f + Random(10.0f); boxNode->SetPosition(Vector3(Random(80.0f) - 40.0f, size * 0.5f, Random(80.0f) - 40.0f)); boxNode->SetScale(size); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); boxObject->SetCastShadows(true); if (size >= 3.0f) boxObject->SetOccluder(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->SetPadding(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->SetFarClip(300.0f); // Set an initial position for the camera scene node above the plane cameraNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f)); }
void SkeletalAnimation::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // 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->SetScale(Vector3(100.0f, 1.0f, 100.0f)); StaticModel* planeObject = planeNode->CreateComponent<StaticModel>(); planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl")); planeObject->SetMaterial(cache->GetResource<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(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(0.5f, 0.5f, 0.7f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.0f); // Create a directional light to the world. Enable cascaded shadows on it Node* lightNode = scene_->CreateChild("Directional light"); 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.0001f, 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 animated models const unsigned NUM_MODELS = 100; const float MODEL_MOVE_SPEED = 2.0f; const float MODEL_ROTATE_SPEED = 100.0f; const BoundingBox bounds(Vector3(-47.0f, 0.0f, -47.0f), Vector3(47.0f, 0.0f, 47.0f)); for (unsigned i = 0; i < NUM_MODELS; ++i) { Node* modelNode = scene_->CreateChild("Jack"); modelNode->SetPosition(Vector3(Random(90.0f) - 45.0f, 0.0f, Random(90.0f) - 45.0f)); modelNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f)); AnimatedModel* modelObject = modelNode->CreateComponent<AnimatedModel>(); modelObject->SetModel(cache->GetResource<Model>("Models/Jack.mdl")); modelObject->SetMaterial(cache->GetResource<Material>("Materials/Jack.xml")); modelObject->SetCastShadows(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 Animation* walkAnimation = cache->GetResource<Animation>("Models/Jack_Walk.ani"); AnimationState* state = modelObject->AddAnimationState(walkAnimation); // Enable full blending weight and looping state->SetWeight(1.0f); state->SetLooped(true); // Create our custom Mover component that will move & animate the model during each frame's update Mover* mover = modelNode->CreateComponent<Mover>(); mover->SetParameters(MODEL_MOVE_SPEED, MODEL_ROTATE_SPEED, bounds); } // Create the camera. Limit far clip distance to match the fog cameraNode_ = scene_->CreateChild("Camera"); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(300.0f); // Set an initial position for the camera scene node above the plane cameraNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f)); }
void HugeObjectCount::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); if (!scene_) scene_ = new Scene(context_); else { scene_->Clear(); boxNodes_.Clear(); } // 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 for ambient light & fog control Node* zoneNode = scene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetFogColor(Color(0.2f, 0.2f, 0.2f)); zone->SetFogStart(200.0f); zone->SetFogEnd(300.0f); // Create a directional light Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(-0.6f, -1.0f, -0.8f)); // The direction vector does not need to be normalized Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); if (!useGroups_) { light->SetColor(Color(0.7f, 0.35f, 0.0f)); // Create individual box StaticModels in the scene for (int y = -125; y < 125; ++y) { for (int x = -125; x < 125; ++x) { Node* boxNode = scene_->CreateChild("Box"); boxNode->SetPosition(Vector3(x * 0.3f, 0.0f, y * 0.3f)); boxNode->SetScale(0.25f); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxNodes_.Push(SharedPtr<Node>(boxNode)); } } } else { light->SetColor(Color(0.6f, 0.6f, 0.6f)); light->SetSpecularIntensity(1.5f); // Create StaticModelGroups in the scene StaticModelGroup* lastGroup = 0; for (int y = -125; y < 125; ++y) { for (int x = -125; x < 125; ++x) { // Create new group if no group yet, or the group has already "enough" objects. The tradeoff is between culling // accuracy and the amount of CPU processing needed for all the objects. Note that the group's own transform // does not matter, and it does not render anything if instance nodes are not added to it if (!lastGroup || lastGroup->GetNumInstanceNodes() >= 25 * 25) { Node* boxGroupNode = scene_->CreateChild("BoxGroup"); lastGroup = boxGroupNode->CreateComponent<StaticModelGroup>(); lastGroup->SetModel(cache->GetResource<Model>("Models/Box.mdl")); } Node* boxNode = scene_->CreateChild("Box"); boxNode->SetPosition(Vector3(x * 0.3f, 0.0f, y * 0.3f)); boxNode->SetScale(0.25f); boxNodes_.Push(SharedPtr<Node>(boxNode)); lastGroup->AddInstanceNode(boxNode); } } } // Create the camera. Create it outside the scene so that we can clear the whole scene without affecting it if (!cameraNode_) { cameraNode_ = new Node(context_); cameraNode_->SetPosition(Vector3(0.0f, 10.0f, -100.0f)); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(300.0f); } }
void Urho2DConstraints::CreateScene() { scene_ = new Scene(context_); scene_->CreateComponent<Octree>(); scene_->CreateComponent<DebugRenderer>(); auto* physicsWorld = scene_->CreateComponent<PhysicsWorld2D>(); // Create 2D physics world component physicsWorld->SetDrawJoint(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_->SetPosition(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_->SetOrthographic(true); auto* graphics = GetSubsystem<Graphics>(); camera_->SetOrthoSize((float)graphics->GetHeight() * PIXEL_SIZE); camera_->SetZoom(1.2f * Min((float)graphics->GetWidth() / 1280.0f, (float)graphics->GetHeight() / 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 ea::shared_ptr<Viewport> viewport(new Viewport(context_, scene_, camera_)); auto* renderer = GetSubsystem<Renderer>(); renderer->SetViewport(0, viewport); Zone* zone = renderer->GetDefaultZone(); zone->SetFogColor(Color(0.1f, 0.1f, 0.1f)); // Set background color for the scene // Create 4x3 grid for (unsigned i = 0; i<5; ++i) { Node* edgeNode = scene_->CreateChild("VerticalEdge"); auto* edgeBody = edgeNode->CreateComponent<RigidBody2D>(); if (!dummyBody) dummyBody = edgeBody; // Mark first edge as dummy body (used by mouse pick) auto* edgeShape = edgeNode->CreateComponent<CollisionEdge2D>(); edgeShape->SetVertices(Vector2(i*2.5f -5.0f, -3.0f), Vector2(i*2.5f -5.0f, 3.0f)); edgeShape->SetFriction(0.5f); // Set friction } for (unsigned j = 0; j<4; ++j) { Node* edgeNode = scene_->CreateChild("HorizontalEdge"); /*RigidBody2D* edgeBody = */edgeNode->CreateComponent<RigidBody2D>(); auto* edgeShape = edgeNode->CreateComponent<CollisionEdge2D>(); edgeShape->SetVertices(Vector2(-5.0f, j*2.0f -3.0f), Vector2(5.0f, j*2.0f -3.0f)); edgeShape->SetFriction(0.5f); // Set friction } auto* cache = GetSubsystem<ResourceCache>(); // Create a box (will be cloned later) Node* box = scene_->CreateChild("Box"); box->SetPosition(Vector3(0.8f, -2.0f, 0.0f)); auto* boxSprite = box->CreateComponent<StaticSprite2D>(); boxSprite->SetSprite(cache->GetResource<Sprite2D>("Urho2D/Box.png")); auto* boxBody = box->CreateComponent<RigidBody2D>(); boxBody->SetBodyType(BT_DYNAMIC); boxBody->SetLinearDamping(0.0f); boxBody->SetAngularDamping(0.0f); auto* shape = box->CreateComponent<CollisionBox2D>(); // Create box shape shape->SetSize(Vector2(0.32f, 0.32f)); // Set size shape->SetDensity(1.0f); // Set shape density (kilograms per meter squared) shape->SetFriction(0.5f); // Set friction shape->SetRestitution(0.1f); // Set restitution (slight bounce) // Create a ball (will be cloned later) Node* ball = scene_->CreateChild("Ball"); ball->SetPosition(Vector3(1.8f, -2.0f, 0.0f)); auto* ballSprite = ball->CreateComponent<StaticSprite2D>(); ballSprite->SetSprite(cache->GetResource<Sprite2D>("Urho2D/Ball.png")); auto* ballBody = ball->CreateComponent<RigidBody2D>(); ballBody->SetBodyType(BT_DYNAMIC); ballBody->SetLinearDamping(0.0f); ballBody->SetAngularDamping(0.0f); auto* ballShape = ball->CreateComponent<CollisionCircle2D>(); // Create circle shape ballShape->SetRadius(0.16f); // Set radius ballShape->SetDensity(1.0f); // Set shape density (kilograms per meter squared) ballShape->SetFriction(0.5f); // Set friction ballShape->SetRestitution(0.6f); // Set restitution: make it bounce // Create a polygon Node* polygon = scene_->CreateChild("Polygon"); polygon->SetPosition(Vector3(1.6f, -2.0f, 0.0f)); polygon->SetScale(0.7f); auto* polygonSprite = polygon->CreateComponent<StaticSprite2D>(); polygonSprite->SetSprite(cache->GetResource<Sprite2D>("Urho2D/Aster.png")); auto* polygonBody = polygon->CreateComponent<RigidBody2D>(); polygonBody->SetBodyType(BT_DYNAMIC); auto* polygonShape = polygon->CreateComponent<CollisionPolygon2D>(); // TODO: create from ea::vector<Vector2> using SetVertices() polygonShape->SetVertexCount(6); // Set number of vertices (mandatory when using SetVertex()) polygonShape->SetVertex(0, Vector2(-0.8f, -0.3f)); polygonShape->SetVertex(1, Vector2(0.5f, -0.8f)); polygonShape->SetVertex(2, Vector2(0.8f, -0.3f)); polygonShape->SetVertex(3, Vector2(0.8f, 0.5f)); polygonShape->SetVertex(4, Vector2(0.5f, 0.9f)); polygonShape->SetVertex(5, Vector2(-0.5f, 0.7f)); polygonShape->SetDensity(1.0f); // Set shape density (kilograms per meter squared) polygonShape->SetFriction(0.3f); // Set friction polygonShape->SetRestitution(0.0f); // Set restitution (no bounce) // Create a ConstraintDistance2D CreateFlag("ConstraintDistance2D", -4.97f, 3.0f); // Display Text3D flag Node* boxDistanceNode = box->Clone(); Node* ballDistanceNode = ball->Clone(); auto* ballDistanceBody = ballDistanceNode->GetComponent<RigidBody2D>(); boxDistanceNode->SetPosition(Vector3(-4.5f, 2.0f, 0.0f)); ballDistanceNode->SetPosition(Vector3(-3.0f, 2.0f, 0.0f)); auto* constraintDistance = boxDistanceNode->CreateComponent<ConstraintDistance2D>(); // Apply ConstraintDistance2D to box constraintDistance->SetOtherBody(ballDistanceBody); // Constrain ball to box constraintDistance->SetOwnerBodyAnchor(boxDistanceNode->GetPosition2D()); constraintDistance->SetOtherBodyAnchor(ballDistanceNode->GetPosition2D()); // Make the constraint soft (comment to make it rigid, which is its basic behavior) constraintDistance->SetFrequencyHz(4.0f); constraintDistance->SetDampingRatio(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(); Node* ballFrictionNode = ball->Clone(); boxFrictionNode->SetPosition(Vector3(0.5f, 0.0f, 0.0f)); ballFrictionNode->SetPosition(Vector3(1.5f, 0.0f, 0.0f)); auto* constraintFriction = boxFrictionNode->CreateComponent<ConstraintFriction2D>(); // Apply ConstraintDistance2D to box constraintFriction->SetOtherBody(ballFrictionNode->GetComponent<RigidBody2D>()); // Constraint ball to box //constraintFriction->SetOwnerBodyAnchor(boxNode->GetPosition2D()); //constraintFriction->SetOtherBodyAnchor(ballNode->GetPosition2D()); //constraintFriction->SetMaxForce(10.0f); // ballBody.mass * gravity //constraintDistance->SetMaxTorque(10.0f); // ballBody.mass * radius * gravity // Create a ConstraintGear2D CreateFlag("ConstraintGear2D", -4.97f, -1.0f); // Display Text3D flag Node* baseNode = box->Clone(); auto* tempBody = baseNode->GetComponent<RigidBody2D>(); // Get body to make it static tempBody->SetBodyType(BT_STATIC); baseNode->SetPosition(Vector3(-3.7f, -2.5f, 0.0f)); Node* ball1Node = ball->Clone(); ball1Node->SetPosition(Vector3(-4.5f, -2.0f, 0.0f)); auto* ball1Body = ball1Node->GetComponent<RigidBody2D>(); Node* ball2Node = ball->Clone(); ball2Node->SetPosition(Vector3(-3.0f, -2.0f, 0.0f)); auto* ball2Body = ball2Node->GetComponent<RigidBody2D>(); auto* gear1 = baseNode->CreateComponent<ConstraintRevolute2D>(); // Apply constraint to baseBox gear1->SetOtherBody(ball1Body); // Constrain ball1 to baseBox gear1->SetAnchor(ball1Node->GetPosition2D()); auto* gear2 = baseNode->CreateComponent<ConstraintRevolute2D>(); // Apply constraint to baseBox gear2->SetOtherBody(ball2Body); // Constrain ball2 to baseBox gear2->SetAnchor(ball2Node->GetPosition2D()); auto* constraintGear = ball1Node->CreateComponent<ConstraintGear2D>(); // Apply constraint to ball1 constraintGear->SetOtherBody(ball2Body); // Constrain ball2 to ball1 constraintGear->SetOwnerConstraint(gear1); constraintGear->SetOtherConstraint(gear2); constraintGear->SetRatio(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(); car->SetScale(Vector3(4.0f, 1.0f, 0.0f)); car->SetPosition(Vector3(-1.2f, -2.3f, 0.0f)); auto* tempSprite = car->GetComponent<StaticSprite2D>(); // Get car Sprite in order to draw it on top tempSprite->SetOrderInLayer(0); // Draw car on top of the wheels (set to -1 to draw below) Node* ball1WheelNode = ball->Clone(); ball1WheelNode->SetPosition(Vector3(-1.6f, -2.5f, 0.0f)); Node* ball2WheelNode = ball->Clone(); ball2WheelNode->SetPosition(Vector3(-0.8f, -2.5f, 0.0f)); auto* wheel1 = car->CreateComponent<ConstraintWheel2D>(); wheel1->SetOtherBody(ball1WheelNode->GetComponent<RigidBody2D>()); wheel1->SetAnchor(ball1WheelNode->GetPosition2D()); wheel1->SetAxis(Vector2(0.0f, 1.0f)); wheel1->SetMaxMotorTorque(20.0f); wheel1->SetFrequencyHz(4.0f); wheel1->SetDampingRatio(0.4f); auto* wheel2 = car->CreateComponent<ConstraintWheel2D>(); wheel2->SetOtherBody(ball2WheelNode->GetComponent<RigidBody2D>()); wheel2->SetAnchor(ball2WheelNode->GetPosition2D()); wheel2->SetAxis(Vector2(0.0f, 1.0f)); wheel2->SetMaxMotorTorque(10.0f); wheel2->SetFrequencyHz(4.0f); wheel2->SetDampingRatio(0.4f); // ConstraintMotor2D CreateFlag("ConstraintMotor2D", 2.53f, -1.0f); // Display Text3D flag Node* boxMotorNode = box->Clone(); tempBody = boxMotorNode->GetComponent<RigidBody2D>(); // Get body to make it static tempBody->SetBodyType(BT_STATIC); Node* ballMotorNode = ball->Clone(); boxMotorNode->SetPosition(Vector3(3.8f, -2.1f, 0.0f)); ballMotorNode->SetPosition(Vector3(3.8f, -1.5f, 0.0f)); auto* constraintMotor = boxMotorNode->CreateComponent<ConstraintMotor2D>(); constraintMotor->SetOtherBody(ballMotorNode->GetComponent<RigidBody2D>()); // Constrain ball to box constraintMotor->SetLinearOffset(Vector2(0.0f, 0.8f)); // Set ballNode position relative to boxNode position = (0,0) constraintMotor->SetAngularOffset(0.1f); constraintMotor->SetMaxForce(5.0f); constraintMotor->SetMaxTorque(10.0f); constraintMotor->SetCorrectionFactor(1.0f); constraintMotor->SetCollideConnected(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(); tempBody = boxPrismaticNode->GetComponent<RigidBody2D>(); // Get body to make it static tempBody->SetBodyType(BT_STATIC); Node* ballPrismaticNode = ball->Clone(); boxPrismaticNode->SetPosition(Vector3(3.3f, 2.5f, 0.0f)); ballPrismaticNode->SetPosition(Vector3(4.3f, 2.0f, 0.0f)); auto* constraintPrismatic = boxPrismaticNode->CreateComponent<ConstraintPrismatic2D>(); constraintPrismatic->SetOtherBody(ballPrismaticNode->GetComponent<RigidBody2D>()); // Constrain ball to box constraintPrismatic->SetAxis(Vector2(1.0f, 1.0f)); // Slide from [0,0] to [1,1] constraintPrismatic->SetAnchor(Vector2(4.0f, 2.0f)); constraintPrismatic->SetLowerTranslation(-1.0f); constraintPrismatic->SetUpperTranslation(0.5f); constraintPrismatic->SetEnableLimit(true); constraintPrismatic->SetMaxMotorForce(1.0f); constraintPrismatic->SetMotorSpeed(0.0f); // ConstraintPulley2D CreateFlag("ConstraintPulley2D", 0.03f, 3.0f); // Display Text3D flag Node* boxPulleyNode = box->Clone(); Node* ballPulleyNode = ball->Clone(); boxPulleyNode->SetPosition(Vector3(0.5f, 2.0f, 0.0f)); ballPulleyNode->SetPosition(Vector3(2.0f, 2.0f, 0.0f)); auto* constraintPulley = boxPulleyNode->CreateComponent<ConstraintPulley2D>(); // Apply constraint to box constraintPulley->SetOtherBody(ballPulleyNode->GetComponent<RigidBody2D>()); // Constrain ball to box constraintPulley->SetOwnerBodyAnchor(boxPulleyNode->GetPosition2D()); constraintPulley->SetOtherBodyAnchor(ballPulleyNode->GetPosition2D()); constraintPulley->SetOwnerBodyGroundAnchor(boxPulleyNode->GetPosition2D() + Vector2(0.0f, 1.0f)); constraintPulley->SetOtherBodyGroundAnchor(ballPulleyNode->GetPosition2D() + Vector2(0.0f, 1.0f)); constraintPulley->SetRatio(1.0); // Weight ratio between ownerBody and otherBody // Create a ConstraintRevolute2D CreateFlag("ConstraintRevolute2D", -2.45f, 3.0f); // Display Text3D flag Node* boxRevoluteNode = box->Clone(); tempBody = boxRevoluteNode->GetComponent<RigidBody2D>(); // Get body to make it static tempBody->SetBodyType(BT_STATIC); Node* ballRevoluteNode = ball->Clone(); boxRevoluteNode->SetPosition(Vector3(-2.0f, 1.5f, 0.0f)); ballRevoluteNode->SetPosition(Vector3(-1.0f, 2.0f, 0.0f)); auto* constraintRevolute = boxRevoluteNode->CreateComponent<ConstraintRevolute2D>(); // Apply constraint to box constraintRevolute->SetOtherBody(ballRevoluteNode->GetComponent<RigidBody2D>()); // Constrain ball to box constraintRevolute->SetAnchor(Vector2(-1.0f, 1.5f)); constraintRevolute->SetLowerAngle(-1.0f); // In radians constraintRevolute->SetUpperAngle(0.5f); // In radians constraintRevolute->SetEnableLimit(true); constraintRevolute->SetMaxMotorTorque(10.0f); constraintRevolute->SetMotorSpeed(0.0f); constraintRevolute->SetEnableMotor(true); // Create a ConstraintRope2D CreateFlag("ConstraintRope2D", -4.97f, 1.0f); // Display Text3D flag Node* boxRopeNode = box->Clone(); tempBody = boxRopeNode->GetComponent<RigidBody2D>(); tempBody->SetBodyType(BT_STATIC); Node* ballRopeNode = ball->Clone(); boxRopeNode->SetPosition(Vector3(-3.7f, 0.7f, 0.0f)); ballRopeNode->SetPosition(Vector3(-4.5f, 0.0f, 0.0f)); auto* constraintRope = boxRopeNode->CreateComponent<ConstraintRope2D>(); constraintRope->SetOtherBody(ballRopeNode->GetComponent<RigidBody2D>()); // Constrain ball to box constraintRope->SetOwnerBodyAnchor(Vector2(0.0f, -0.5f)); // Offset from box (OwnerBody) : the rope is rigid from OwnerBody center to this ownerBodyAnchor constraintRope->SetMaxLength(0.9f); // Rope length constraintRope->SetCollideConnected(true); // Create a ConstraintWeld2D CreateFlag("ConstraintWeld2D", -2.45f, 1.0f); // Display Text3D flag Node* boxWeldNode = box->Clone(); Node* ballWeldNode = ball->Clone(); boxWeldNode->SetPosition(Vector3(-0.5f, 0.0f, 0.0f)); ballWeldNode->SetPosition(Vector3(-2.0f, 0.0f, 0.0f)); auto* constraintWeld = boxWeldNode->CreateComponent<ConstraintWeld2D>(); constraintWeld->SetOtherBody(ballWeldNode->GetComponent<RigidBody2D>()); // Constrain ball to box constraintWeld->SetAnchor(boxWeldNode->GetPosition2D()); constraintWeld->SetFrequencyHz(4.0f); constraintWeld->SetDampingRatio(0.5f); // Create a ConstraintWheel2D CreateFlag("ConstraintWheel2D", 2.53f, 1.0f); // Display Text3D flag Node* boxWheelNode = box->Clone(); Node* ballWheelNode = ball->Clone(); boxWheelNode->SetPosition(Vector3(3.8f, 0.0f, 0.0f)); ballWheelNode->SetPosition(Vector3(3.8f, 0.9f, 0.0f)); auto* constraintWheel = boxWheelNode->CreateComponent<ConstraintWheel2D>(); constraintWheel->SetOtherBody(ballWheelNode->GetComponent<RigidBody2D>()); // Constrain ball to box constraintWheel->SetAnchor(ballWheelNode->GetPosition2D()); constraintWheel->SetAxis(Vector2(0.0f, 1.0f)); constraintWheel->SetEnableMotor(true); constraintWheel->SetMaxMotorTorque(1.0f); constraintWheel->SetMotorSpeed(0.0f); constraintWheel->SetFrequencyHz(4.0f); constraintWheel->SetDampingRatio(0.5f); constraintWheel->SetCollideConnected(true); // doesn't work }
void CharacterDemo::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(300.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(100.0f); zone->SetFogEnd(300.0f); zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.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 the floor object Node* floorNode = scene_->CreateChild("Floor"); floorNode->SetPosition(Vector3(0.0f, -0.5f, 0.0f)); floorNode->SetScale(Vector3(200.0f, 1.0f, 200.0f)); StaticModel* object = floorNode->CreateComponent<StaticModel>(); object->SetModel(cache->GetResource<Model>("Models/Box.mdl")); object->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); RigidBody* body = floorNode->CreateComponent<RigidBody>(); // Use collision layer bit 2 to mark world scenery. This is what we will raycast against to prevent camera from going // inside geometry body->SetCollisionLayer(2); CollisionShape* shape = floorNode->CreateComponent<CollisionShape>(); shape->SetBox(Vector3::ONE); // Create mushrooms of varying sizes const unsigned NUM_MUSHROOMS = 60; for (unsigned i = 0; i < NUM_MUSHROOMS; ++i) { Node* objectNode = scene_->CreateChild("Mushroom"); objectNode->SetPosition(Vector3(Random(180.0f) - 90.0f, 0.0f, Random(180.0f) - 90.0f)); objectNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f)); objectNode->SetScale(2.0f + Random(5.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); } // Create movable boxes. Let them fall from the sky at first const unsigned NUM_BOXES = 100; for (unsigned i = 0; i < NUM_BOXES; ++i) { float scale = Random(2.0f) + 0.5f; Node* objectNode = scene_->CreateChild("Box"); objectNode->SetPosition(Vector3(Random(180.0f) - 90.0f, Random(10.0f) + 10.0f, Random(180.0f) - 90.0f)); objectNode->SetRotation(Quaternion(Random(360.0f), Random(360.0f), Random(360.0f))); objectNode->SetScale(scale); StaticModel* object = objectNode->CreateComponent<StaticModel>(); object->SetModel(cache->GetResource<Model>("Models/Box.mdl")); object->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); object->SetCastShadows(true); RigidBody* body = objectNode->CreateComponent<RigidBody>(); body->SetCollisionLayer(2); // Bigger boxes will be heavier and harder to move body->SetMass(scale * 2.0f); CollisionShape* shape = objectNode->CreateComponent<CollisionShape>(); shape->SetBox(Vector3::ONE); } }
void Sample::Start() { cache_ = GetSubsystem<ResourceCache>(); ui_ = GetSubsystem<UI>(); graphics_ = GetSubsystem<Graphics>(); ////////////////////////////////////////////////////////////////////////// // Get default styles XMLFile* styleFile = cache_->GetResource<XMLFile>("UI/DefaultStyle.xml"); ui_->GetRoot()->SetDefaultStyle(styleFile); // create main ui rootUI_ = ui_->GetRoot()->CreateChild<UIElement>("IDERoot"); rootUI_->SetSize(ui_->GetRoot()->GetSize()); rootUI_->SetTraversalMode(TM_DEPTH_FIRST); // This is needed for root-like element to prevent artifacts rootUI_->SetDefaultStyle(styleFile); ////////////////////////////////////////////////////////////////////////// /// Create console console_ = engine_->CreateConsole(); console_->SetDefaultStyle(styleFile); ////////////////////////////////////////////////////////////////////////// /// Create debug HUD. debugHud_ = engine_->CreateDebugHud(); debugHud_->SetDefaultStyle(styleFile); ////////////////////////////////////////////////////////////////////////// /// Subscribe key down event SubscribeToEvent(E_KEYDOWN, HANDLER(Sample, HandleKeyDown)); // edit clear color, set background color Renderer* renderer = GetSubsystem<Renderer>(); Zone* zone = renderer->GetDefaultZone(); zone->SetFogColor(Color(0.3f, 0.3f, 0.4f)); // Set background color for the scene ////////////////////////////////////////////////////////////////////////// /// Create Cursor // Cursor* cursor_ = new Cursor(context_); // cursor_->SetStyleAuto(styleFile); // ui_->SetCursor(cursor_); // if (GetPlatform() == "Android" || GetPlatform() == "iOS") // ui_->GetCursor()->SetVisible(false); /// Show Platform Cursor Input* input = GetSubsystem<Input>(); input->SetMouseVisible(true); ////////////////////////////////////////////////////////////////////////// /// create an svg image rootUI_->AddChild(CreateSVGSprite("GameData/svg/23.svg")); Sprite* drawing = CreateSVGSprite("GameData/svg/drawing.svg"); if (drawing) { // Set logo sprite hot spot drawing->SetHotSpot(0, drawing->GetHeight()); drawing->SetAlignment(HA_LEFT, VA_BOTTOM); rootUI_->AddChild(drawing); } Sprite* nano = CreateSVGSprite("GameData/svg/nano.svg"); if (nano) { // Set logo sprite hot spot nano->SetHotSpot(0, -nano->GetHeight()); nano->SetAlignment(HA_LEFT, VA_TOP); rootUI_->AddChild(nano); } }
void GameApplication::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // 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->SetScale(Vector3(100.0f, 1.0f, 100.0f)); StaticModel* planeObject = planeNode->CreateComponent<StaticModel>(); planeObject->SetModel(cache->GetResource<Model>("Models/dikuang.mdl")); Material* material = cache->GetResource<Material>("Materials/Water.xml"); planeObject->SetMaterial(material); //dikuang // 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.3f, 0.3f, 0.3f)); zone->SetFogColor(Color(0.5f, 0.5f, 0.7f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.0f); // Create a directional light to the world. Enable cascaded shadows on it Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.2f, -0.5f, -0.1f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); // light->SetSpecularIntensity(3); light->SetBrightness(1); light->SetCastShadows(true); light->SetShadowBias(BiasParameters(0.0001f, 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)); InitGridModels(); // Create the camera. Limit far clip distance to match the fog cameraNode_ = scene_->CreateChild("Camera"); Camera* camera = cameraNode_->CreateComponent<Camera>(); //float fZoom = camera->GetFov(); //camera->SetFov(fZoom); camera->SetFarClip(300.0f); // Set an initial position for the camera scene node above the plane cameraNode_->SetPosition(Vector3(0.2318,7.5248,-0.2721)); yaw_ = 0.10003410; pitch_ = 90; // cameraNode_->SetPosition(Vector3(5.0f, 5.0f, -15.0f)); //pitch_ = 19; cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f)); }
void Urho2DPlatformer::CreateScene() { scene_ = new Scene(context_); sample2D_->scene_ = scene_; // Create the Octree, DebugRenderer and PhysicsWorld2D components to the scene scene_->CreateComponent<Octree>(); scene_->CreateComponent<DebugRenderer>(); /*PhysicsWorld2D* physicsWorld =*/ scene_->CreateComponent<PhysicsWorld2D>(); // Create camera cameraNode_ = scene_->CreateChild("Camera"); auto* camera = cameraNode_->CreateComponent<Camera>(); camera->setProjectionType(PT_ORTHOGRAPHIC); auto* graphics = GetContext()->m_Graphics.get(); camera->SetOrthoSize((float)graphics->GetHeight() * PIXEL_SIZE); camera->SetZoom(2.0f * Min((float)graphics->GetWidth() / 1280.0f, (float)graphics->GetHeight() / 800.0f)); // Set zoom according to user's resolution to ensure full visibility (initial zoom (2.0) is set for full visibility at 1280x800 resolution) // Setup the viewport for displaying the scene SharedPtr<Viewport> viewport(new Viewport(context_, scene_, camera)); auto* renderer = GetContext()->m_Renderer.get(); renderer->SetViewport(0, viewport); // Set background color for the scene Zone* zone = renderer->GetDefaultZone(); zone->SetFogColor(Color(0.2f, 0.2f, 0.2f)); // Create tile map from tmx file auto* cache = GetContext()->m_ResourceCache.get(); SharedPtr<Node> tileMapNode(scene_->CreateChild("TileMap")); auto* tileMap = tileMapNode->CreateComponent<TileMap2D>(); tileMap->SetTmxFile(cache->GetResource<TmxFile2D>("Urho2D/Tilesets/Ortho.tmx")); const TileMapInfo2D& info = tileMap->GetInfo(); // Create Spriter Imp character (from sample 33_SpriterAnimation) Node* spriteNode = sample2D_->CreateCharacter(info, 0.8f, Vector3(1.0f, 8.0f, 0.0f), 0.2f); character2D_ = spriteNode->CreateComponent<Character2D>(); // Create a logic component to handle character behavior // Generate physics collision shapes from the tmx file's objects located in "Physics" (top) layer TileMapLayer2D* tileMapLayer = tileMap->GetLayer(tileMap->GetNumLayers() - 1); sample2D_->CreateCollisionShapesFromTMXObjects(tileMapNode, tileMapLayer, info); // Instantiate enemies and moving platforms at each placeholder of "MovingEntities" layer (placeholders are Poly Line objects defining a path from points) sample2D_->PopulateMovingEntities(tileMap->GetLayer(tileMap->GetNumLayers() - 2)); // Instantiate coins to pick at each placeholder of "Coins" layer (placeholders for coins are Rectangle objects) TileMapLayer2D* coinsLayer = tileMap->GetLayer(tileMap->GetNumLayers() - 3); sample2D_->PopulateCoins(coinsLayer); // Init coins counters character2D_->remainingCoins_ = coinsLayer->GetNumObjects(); character2D_->maxCoins_ = coinsLayer->GetNumObjects(); //Instantiate triggers (for ropes, ladders, lava, slopes...) at each placeholder of "Triggers" layer (placeholders for triggers are Rectangle objects) sample2D_->PopulateTriggers(tileMap->GetLayer(tileMap->GetNumLayers() - 4)); // Create background sample2D_->CreateBackgroundSprite(info, 3.5, "Textures/HeightMap.png", true); // Check when scene is rendered g_graphicsSignals.endRendering.Connect(this,&Urho2DPlatformer::HandleSceneRendered); }
void DynamicGeometry::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // 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 for ambient light & fog control Node* zoneNode = scene_->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); zone->SetFogColor(Color(0.2f, 0.2f, 0.2f)); zone->SetFogStart(200.0f); zone->SetFogEnd(300.0f); // Create a directional light Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(-0.6f, -1.0f, -0.8f)); // The direction vector does not need to be normalized Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetColor(Color(0.4f, 1.0f, 0.4f)); light->SetSpecularIntensity(1.5f); // Get the original model and its unmodified vertices, which are used as source data for the animation Model* originalModel = cache->GetResource<Model>("Models/Box.mdl"); if (!originalModel) { ATOMIC_LOGERROR("Model not found, cannot initialize example scene"); return; } // Get the vertex buffer from the first geometry's first LOD level VertexBuffer* buffer = originalModel->GetGeometry(0, 0)->GetVertexBuffer(0); const unsigned char* vertexData = (const unsigned char*)buffer->Lock(0, buffer->GetVertexCount()); if (vertexData) { unsigned numVertices = buffer->GetVertexCount(); unsigned vertexSize = buffer->GetVertexSize(); // Copy the original vertex positions for (unsigned i = 0; i < numVertices; ++i) { const Vector3& src = *reinterpret_cast<const Vector3*>(vertexData + i * vertexSize); originalVertices_.Push(src); } buffer->Unlock(); // Detect duplicate vertices to allow seamless animation vertexDuplicates_.Resize(originalVertices_.Size()); for (unsigned i = 0; i < originalVertices_.Size(); ++i) { vertexDuplicates_[i] = i; // Assume not a duplicate for (unsigned j = 0; j < i; ++j) { if (originalVertices_[i].Equals(originalVertices_[j])) { vertexDuplicates_[i] = j; break; } } } } else { ATOMIC_LOGERROR("Failed to lock the model vertex buffer to get original vertices"); return; } // Create StaticModels in the scene. Clone the model for each so that we can modify the vertex data individually for (int y = -1; y <= 1; ++y) { for (int x = -1; x <= 1; ++x) { Node* node = scene_->CreateChild("Object"); node->SetPosition(Vector3(x * 2.0f, 0.0f, y * 2.0f)); StaticModel* object = node->CreateComponent<StaticModel>(); SharedPtr<Model> cloneModel = originalModel->Clone(); object->SetModel(cloneModel); // Store the cloned vertex buffer that we will modify when animating animatingBuffers_.Push(SharedPtr<VertexBuffer>(cloneModel->GetGeometry(0, 0)->GetVertexBuffer(0))); } } // Finally create one model (pyramid shape) and a StaticModel to display it from scratch // Note: there are duplicated vertices to enable face normals. We will calculate normals programmatically { const unsigned numVertices = 18; float vertexData[] = { // Position Normal 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f }; const unsigned short indexData[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }; // Calculate face normals now for (unsigned i = 0; i < numVertices; i += 3) { Vector3& v1 = *(reinterpret_cast<Vector3*>(&vertexData[6 * i])); Vector3& v2 = *(reinterpret_cast<Vector3*>(&vertexData[6 * (i + 1)])); Vector3& v3 = *(reinterpret_cast<Vector3*>(&vertexData[6 * (i + 2)])); Vector3& n1 = *(reinterpret_cast<Vector3*>(&vertexData[6 * i + 3])); Vector3& n2 = *(reinterpret_cast<Vector3*>(&vertexData[6 * (i + 1) + 3])); Vector3& n3 = *(reinterpret_cast<Vector3*>(&vertexData[6 * (i + 2) + 3])); Vector3 edge1 = v1 - v2; Vector3 edge2 = v1 - v3; n1 = n2 = n3 = edge1.CrossProduct(edge2).Normalized(); } SharedPtr<Model> fromScratchModel(new Model(context_)); SharedPtr<VertexBuffer> vb(new VertexBuffer(context_)); SharedPtr<IndexBuffer> ib(new IndexBuffer(context_)); SharedPtr<Geometry> geom(new Geometry(context_)); // Shadowed buffer needed for raycasts to work, and so that data can be automatically restored on device loss vb->SetShadowed(true); // We could use the "legacy" element bitmask to define elements for more compact code, but let's demonstrate // defining the vertex elements explicitly to allow any element types and order PODVector<VertexElement> elements; elements.Push(VertexElement(TYPE_VECTOR3, SEM_POSITION)); elements.Push(VertexElement(TYPE_VECTOR3, SEM_NORMAL)); vb->SetSize(numVertices, elements); vb->SetData(vertexData); ib->SetShadowed(true); ib->SetSize(numVertices, false); ib->SetData(indexData); geom->SetVertexBuffer(0, vb); geom->SetIndexBuffer(ib); geom->SetDrawRange(TRIANGLE_LIST, 0, numVertices); fromScratchModel->SetNumGeometries(1); fromScratchModel->SetGeometry(0, 0, geom); fromScratchModel->SetBoundingBox(BoundingBox(Vector3(-0.5f, -0.5f, -0.5f), Vector3(0.5f, 0.5f, 0.5f))); // Though not necessary to render, the vertex & index buffers must be listed in the model so that it can be saved properly Vector<SharedPtr<VertexBuffer> > vertexBuffers; Vector<SharedPtr<IndexBuffer> > indexBuffers; vertexBuffers.Push(vb); indexBuffers.Push(ib); // Morph ranges could also be not defined. Here we simply define a zero range (no morphing) for the vertex buffer PODVector<unsigned> morphRangeStarts; PODVector<unsigned> morphRangeCounts; morphRangeStarts.Push(0); morphRangeCounts.Push(0); fromScratchModel->SetVertexBuffers(vertexBuffers, morphRangeStarts, morphRangeCounts); fromScratchModel->SetIndexBuffers(indexBuffers); Node* node = scene_->CreateChild("FromScratchObject"); node->SetPosition(Vector3(0.0f, 3.0f, 0.0f)); StaticModel* object = node->CreateComponent<StaticModel>(); object->SetModel(fromScratchModel); } // Create the camera cameraNode_ = new Node(context_); cameraNode_->SetPosition(Vector3(0.0f, 2.0f, -20.0f)); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(300.0f); }
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 Ragdolls::CreateScene() { ResourceCache* cache = GetContext()->m_ResourceCache.get(); scene_ = new Scene(GetContext()); // 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(0.5f, 0.5f, 0.7f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.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 a floor object, 500 x 500 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(500.0f, 1.0f, 500.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 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->SetRollingFriction(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); } // Create animated models for (int z = -1; z <= 1; ++z) { for (int x = -4; x <= 4; ++x) { Node* modelNode = scene_->CreateChild("Jack"); modelNode->SetPosition(Vector3(x * 5.0f, 0.0f, z * 5.0f)); modelNode->SetRotation(Quaternion(0.0f, 180.0f, 0.0f)); AnimatedModel* modelObject = modelNode->CreateComponent<AnimatedModel>(); modelObject->SetModel(cache->GetResource<Model>("Models/Jack.mdl")); modelObject->SetMaterial(cache->GetResource<Material>("Materials/Jack.xml")); modelObject->SetCastShadows(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->SetUpdateInvisible(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->SetTrigger(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, Vector3(0.0f, 1.0f, 0.0f)); // Create a custom component that reacts to collisions and creates the ragdoll modelNode->CreateComponent<CreateRagdoll>(); } } // 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(GetContext()); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->setFarClipDistance(300.0f); // Set an initial position for the camera scene node above the floor cameraNode_->SetPosition(Vector3(0.0f, 3.0f, -20.0f)); }
void MasterControl::CreateScene() { world.scene = new Scene(context_); world.octree = world.scene->CreateComponent<Octree>(); physicsWorld_ = world.scene->CreateComponent<PhysicsWorld>(); physicsWorld_->SetGravity(Vector3::ZERO); world.scene->CreateComponent<DebugRenderer>(); //Create a Zone component for ambient ing & fog control Node* zoneNode = world.scene->CreateChild("Zone"); Zone* zone = zoneNode->CreateComponent<Zone>(); zone->SetBoundingBox(BoundingBox(Vector3(-100.0f, -50.0f, -100.0f),Vector3(100.0f, 0.0f, 100.0f))); zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(0.0f, 0.0f, 0.0f)); zone->SetFogStart(56.8f); zone->SetFogEnd(61.8f); //Add a directional light to the world. Enable cascaded shadows on it Node* lightNode = world.scene->CreateChild("PointLight"); lightNode->SetPosition(Vector3::UP*5.0); lightNode->SetRotation(Quaternion(90.0f, 0.0f, 0.0f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetBrightness(1.0f); light->SetRange(7.0f); light->SetColor(Color(1.0f, 0.9f, 0.95f)); light->SetCastShadows(false); 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 cursor world.cursor.sceneCursor = world.scene->CreateChild("Cursor"); //world.cursor.sceneCursor->SetPosition(Vector3(0.0f,0.0f,0.0f)); StaticModel* cursorObject = world.cursor.sceneCursor->CreateComponent<StaticModel>(); cursorObject->SetModel(cache_->GetResource<Model>("Resources/Models/Hexagon.mdl")); cursorObject->SetMaterial(cache_->GetResource<Material>("Resources/Materials/Glow.xml")); world.cursor.sceneCursor->SetEnabled(false); //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* planeObject = world.voidNode->CreateComponent<StaticModel>(); planeObject->SetModel(cache_->GetResource<Model>("Models/Plane.mdl")); planeObject->SetMaterial(cache_->GetResource<Material>("Resources/Materials/Invisible.xml")); //Create camera world.camera = new heXoCam(context_, this); //Create arena tileMaster_ = new TileMaster(context_, this); for (int i = 0; i < 6; i++){ new ArenaEdge(context_, this, (60.0f * i)+30.0f); } spawnMaster_ = new SpawnMaster(context_, this); player_ = new Player(context_, this); apple_ = new Apple(context_, this); heart_ = new Heart(context_, this); }
//------------------- //------------------- void Stage2::Setup(SharedPtr<Scene> scene, SharedPtr<Node> cameraNode) { const String states_[50] = { String("state_1.001.mdl"), String("state_2.001.mdl"), String("state_3.001.mdl"), String("state_4.001.mdl"), String("state_5.001.mdl"), String("state_6.001.mdl"), String("state_7.001.mdl"), String("state_8.001.mdl"), String("state_9.001.mdl"), String("state_10.001.mdl"), String("state_11.001.mdl"), String("state_12.001.mdl"), String("state_13.001.mdl"), String("state_14.001.mdl"), String("state_15.001.mdl"), String("state_16.001.mdl"), String("state_17.001.mdl"), String("state_18.001.mdl"), String("state_19.001.mdl"), String("state_20.001.mdl"), String("state_21.001.mdl"), String("state_22.001.mdl"), String("state_23.001.mdl"), String("state_24.001.mdl"), String("state_25.001.mdl"), String("state_26.001.mdl"), String("state_27.001.mdl"), String("state_28.001.mdl"), String("state_29.001.mdl"), String("state_30.001.mdl"), String("state_31.001.mdl"), String("state_32.001.mdl"), String("state_33.001.mdl"), String("state_34.001.mdl"), String("state_35.001.mdl"), String("state_36.001.mdl"), String("state_37.001.mdl"), String("state_38.001.mdl"), String("state_39.001.mdl"), String("state_40.001.mdl"), String("state_41.001.mdl"), String("state_42.001.mdl"), String("state_43.001.mdl"), String("state_44.001.mdl"), String("state_45.001.mdl"), String("state_46.001.mdl"), String("state_47.001.mdl"), String("state_48.001.mdl"), String("state_49.001.mdl"), String("state_50.001.mdl") }; const Vector3 positions_[50] = { Vector3(0.21888011694f,0.0156500004232f,2.09723997116f), Vector3(2.54229521751f,-0.00904999952763f,1.75292992592f), Vector3(2.41885995865f,-0.00999999977648f,1.90676009655f), Vector3(0.673485100269f,0.0166000016034f,2.30860519409f), Vector3(0.369050145149f,0.0175500009209f,1.11237001419f), Vector3(0.516425132751f,0.0185000002384f,0.181779891253f), Vector3(0.423860132694f,0.0185000002384f,0.481095075607f), Vector3(0.577625155449f,0.0185000002384f,1.04367494583f), Vector3(0.837990105152f,0.0185000002384f,1.52935504913f), Vector3(0.935640096664f,0.0185000002384f,1.02947998047f), Vector3(0.837135195732f,0.0185000002384f,0.446817427874f), Vector3(1.17873501778f,0.0185000002384f,0.342575073242f), Vector3(1.26694011688f,0.0185000002384f,0.743340015411f), Vector3(1.27813506126f,0.0185000002384f,1.56136512756f), Vector3(1.73834013939f,0.0175500009209f,1.89084005356f), Vector3(1.36196017265f,0.0185000002384f,1.13154006004f), Vector3(1.85952007771f,0.0185000002384f,1.51079499722f), Vector3(1.88827514648f,0.000450000166893f,1.21628499031f), Vector3(1.77227497101f,0.0175500009209f,0.656964957714f), Vector3(1.77993512154f,-0.00144999939948f,0.365104973316f), Vector3(2.22174501419f,-0.0052499989979f,0.468360185623f), Vector3(2.69981503487f,-0.0052499989979f,0.614735066891f), Vector3(2.4618601799f,-0.0052499989979f,0.627650141716f), Vector3(2.53034496307f,-0.00810000021011f,1.0825150013f), Vector3(2.75453519821f,-0.00714999902993f,1.07339000702f), Vector3(2.99251008034f,-0.00714999902993f,0.980669975281f), Vector3(3.74992036819f,-0.00810000021011f,0.702120065689f), Vector3(3.66952991486f,-0.00619999971241f,0.732804954052f), Vector3(3.72557497025f,-0.00619999971241f,0.641584992409f), Vector3(3.53317499161f,-0.00904999952763f,1.00545012951f), Vector3(3.5650601387f,-0.00619999971241f,0.886059999466f), Vector3(3.40220499039f,-0.00429999921471f,1.02564501762f), Vector3(3.82462024689f,-0.00239999918267f,0.349364906549f), Vector3(3.70352506638f,-0.0052499989979f,0.491724967957f), Vector3(3.60558986664f,-0.0052499989979f,0.511260032654f), Vector3(3.44979000092f,-0.00429999921471f,0.630430102348f), Vector3(3.3455851078f,-0.0052499989979f,0.875915110111f), Vector3(2.3500752449f,-0.0052499989979f,1.56273007393f), Vector3(3.1991353035f,-0.00144999939948f,1.08977997303f), Vector3(3.26395010948f,-0.00144999939948f,1.17536497116f), Vector3(2.79603528976f,-0.00714999902993f,1.73529994488f), Vector3(2.81455516815f,-0.00144999939948f,1.42764496803f), Vector3(3.25786995888f,-0.00144999939948f,1.38491988182f), Vector3(3.21765995026f,-0.00619999971241f,1.58336496353f), Vector3(3.04369020462f,-0.00429999921471f,1.68953490257f), Vector3(3.07175517082f,-0.00999999977648f,2.11033010483f), Vector3(1.79783010483f,0.0185000002384f,0.926185011864f), Vector3(2.24017524719f,-0.00619999971241f,0.882490038872f), Vector3(2.32092523575f,-0.0052499989979f,1.23307991028f), Vector3(2.81634521484f,-0.00429999921471f,1.25435996056f) }; scene_ = scene; cameraNode_ = cameraNode; ResourceCache* cache = GetSubsystem<ResourceCache>(); // Create scene node & StaticModel component for showing a static plane /*Node* planeNode = scene_->CreateChild("Plane"); planeNode->SetScale(Vector3(100.0f, 1.0f, 100.0f)); StaticModel* planeObject = planeNode->CreateComponent<StaticModel>(); planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl")); planeObject->SetMaterial(cache->GetResource<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(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(0.5f, 0.5f, 0.7f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.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 animated models /*const unsigned NUM_MODELS = 100; const float MODEL_MOVE_SPEED = 2.0f; const float MODEL_ROTATE_SPEED = 100.0f; const BoundingBox bounds(Vector3(-47.0f, 0.0f, -47.0f), Vector3(47.0f, 0.0f, 47.0f)); for (unsigned i = 0; i < NUM_MODELS; ++i) { Node* modelNode = scene_->CreateChild("Jack"); modelNode->SetPosition(Vector3(Random(90.0f) - 45.0f, 0.0f, Random(90.0f) - 45.0f)); modelNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f)); AnimatedModel* modelObject = modelNode->CreateComponent<AnimatedModel>(); modelObject->SetModel(cache->GetResource<Model>("Models/Jack.mdl")); modelObject->SetMaterial(cache->GetResource<Material>("Materials/Jack.xml")); modelObject->SetCastShadows(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 Animation* walkAnimation = cache->GetResource<Animation>("Models/Jack_Walk.ani"); AnimationState* state = modelObject->AddAnimationState(walkAnimation); // The state would fail to create (return null) if the animation was not found if (state) { // Enable full blending weight and looping state->SetWeight(1.0f); state->SetLooped(true); } // Create our custom Mover component that will move & animate the model during each frame's update //Mover* mover = modelNode->CreateComponent<Mover>(); //mover->SetParameters(MODEL_MOVE_SPEED, MODEL_ROTATE_SPEED, bounds); }*/ { Node* floorNode = scene_->CreateChild("Floor"); floorNode->SetPosition(Vector3(0.0f, -1.0f, 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); } for (unsigned j=0; j<50; ++j) { Node* stateNode = scene_->CreateChild("state"); Vector3 corrected = Vector3(positions_[j].z_,positions_[j].y_,positions_[j].x_)*10.0f; stateNode->SetPosition(corrected); StaticModel* stateModel = stateNode->CreateComponent<StaticModel>(); //stateModel->SetModel( cache->GetResource<Model>(String("Models/States/")+states_[j]) ); stateModel->SetModel( cache->GetResource<Model>(String("Models/States/state_"+String(j+1)+".001.mdl") ) ); RigidBody* body = stateNode->CreateComponent<RigidBody>(); body->SetMass(1.0f); body->SetFriction(0.75f); CollisionShape* sshape = stateNode->CreateComponent<CollisionShape>(); sshape->SetConvexHull(cache->GetResource<Model>(String("Models/States/state_convex_"+String(j+1)+".001.mdl") )); //LOGINFO(positions_[j].ToString()); stateNode->SetPosition(stateNode->GetWorldPosition()+Vector3(0.0f,20.0f+float(j)*0.5,0.0f)); } /*Node* stateNode = scene_->CreateChild("state"); stateNode->SetPosition(positions_[0]); StaticModel* stateModel = stateNode->CreateComponent<StaticModel>(); stateModel->SetModel( cache->GetResource<Model>(String("Models/States/")+states_[0]) );*/ // Create the camera. Limit far clip distance to match the fog //cameraNode_ = scene_->CreateChild("Camera"); //Camera* camera = cameraNode_->CreateComponent<Camera>(); //camera->SetFarClip(300.0f); // Set an initial position for the camera scene node above the plane cameraNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f)); //give the camera the logic I want //CameraLogic* cameralogic = cameraNode_->CreateComponent<CameraLogic>(); }
//------------------- //------------------- void VaniaDebugEnv::Setup(SharedPtr<Scene> scene, SharedPtr<Node> cameraNode) { scene_ = scene; cameraNode_ = cameraNode; ResourceCache* cache = GetSubsystem<ResourceCache>(); // Create scene node & StaticModel component for showing a static plane /*Node* planeNode = scene_->CreateChild("Plane"); planeNode->SetScale(Vector3(100.0f, 1.0f, 100.0f)); StaticModel* planeObject = planeNode->CreateComponent<StaticModel>(); planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl")); planeObject->SetMaterial(cache->GetResource<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(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(0.5f, 0.5f, 0.7f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.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 animated models /*const unsigned NUM_MODELS = 100; const float MODEL_MOVE_SPEED = 2.0f; const float MODEL_ROTATE_SPEED = 100.0f; const BoundingBox bounds(Vector3(-47.0f, 0.0f, -47.0f), Vector3(47.0f, 0.0f, 47.0f)); for (unsigned i = 0; i < NUM_MODELS; ++i) { Node* modelNode = scene_->CreateChild("Jack"); modelNode->SetPosition(Vector3(Random(90.0f) - 45.0f, 0.0f, Random(90.0f) - 45.0f)); modelNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f)); AnimatedModel* modelObject = modelNode->CreateComponent<AnimatedModel>(); modelObject->SetModel(cache->GetResource<Model>("Models/Jack.mdl")); modelObject->SetMaterial(cache->GetResource<Material>("Materials/Jack.xml")); modelObject->SetCastShadows(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 Animation* walkAnimation = cache->GetResource<Animation>("Models/Jack_Walk.ani"); AnimationState* state = modelObject->AddAnimationState(walkAnimation); // The state would fail to create (return null) if the animation was not found if (state) { // Enable full blending weight and looping state->SetWeight(1.0f); state->SetLooped(true); } // Create our custom Mover component that will move & animate the model during each frame's update //Mover* mover = modelNode->CreateComponent<Mover>(); //mover->SetParameters(MODEL_MOVE_SPEED, MODEL_ROTATE_SPEED, bounds); }*/ { Node* floorNode = scene_->CreateChild("Floor"); floorNode->SetPosition(Vector3(0.0f, -1.0f, 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>(); body->SetCollisionLayer(32); body->SetCollisionMask(63); // 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); } /*Node* stateNode = scene_->CreateChild("state"); stateNode->SetPosition(positions_[0]); StaticModel* stateModel = stateNode->CreateComponent<StaticModel>(); stateModel->SetModel( cache->GetResource<Model>(String("Models/States/")+states_[0]) );*/ // Create the camera. Limit far clip distance to match the fog //cameraNode_ = scene_->CreateChild("Camera"); //Camera* camera = cameraNode_->CreateComponent<Camera>(); //camera->SetFarClip(300.0f); // Set an initial position for the camera scene node above the plane cameraNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f)); //give the camera the logic I want //CameraLogic* cameralogic = cameraNode_->CreateComponent<CameraLogic>(); }
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 Urho3DTemplate::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); //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->SetScale(Vector3(100.0f, 1.0f, 100.0f)); StaticModel* planeObject = planeNode->CreateComponent<StaticModel>(); planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl")); planeObject->SetMaterial(cache->GetResource<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(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(0.5f, 0.5f, 0.7f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.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, 200 world unitys, fade shadows at 80% of maximum shadow distance light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f)); //Create some mushrooms const unsigned NUM_MUSHROOMS = 100; for (unsigned i = 0; i < NUM_MUSHROOMS; ++i) CreateMushroom(Vector3(Random(90.0f) - 45.0f, 0.0f, Random(90.0f) - 45.0f)); //Create randomly sized boxes. If boxes are big enough make them occluders const unsigned NUM_BOXES = 20; for (unsigned i = 0; i <NUM_BOXES; ++i) { Node* boxNode = scene_->CreateChild("Box"); float size = 1.0f + Random(10.0f); boxNode->SetPosition(Vector3(Random(80.0f) - 40.0f, size * 0.5f, Random(80.0f) - 40.0f)); boxNode->SetScale(size); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); boxObject->SetCastShadows(true); if (size >= 3.0f) boxObject->SetOccluder(true); } //Create Jack node that will follow the path jackNode_ = scene_->CreateChild("Jack"); jackNode_->SetPosition(Vector3(-5.0f, 0.0f, 20.0f)); AnimatedModel* modelObject = jackNode_->CreateComponent<AnimatedModel>(); modelObject->SetModel(cache->GetResource<Model>("Model/Jack.mdl")); modelObject->SetMaterial(cache->GetResource<Material>("Materials/Jack.xml")); modelObject->SetCastShadows(true); //Create the camera. Limit far clip distance to match the fog cameraNode_ = scene_->CreateChild("Camera"); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(300.0f); //Set an initial position for the camera scene node above the plane cameraNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f)); }
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 MultipleViewports::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // 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->SetScale(Vector3(100.0f, 1.0f, 100.0f)); StaticModel* planeObject = planeNode->CreateComponent<StaticModel>(); planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl")); planeObject->SetMaterial(cache->GetResource<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(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(0.5f, 0.5f, 0.7f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.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 some mushrooms const unsigned NUM_MUSHROOMS = 240; for (unsigned i = 0; i < NUM_MUSHROOMS; ++i) { Node* mushroomNode = scene_->CreateChild("Mushroom"); mushroomNode->SetPosition(Vector3(Random(90.0f) - 45.0f, 0.0f, Random(90.0f) - 45.0f)); mushroomNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f)); mushroomNode->SetScale(0.5f + Random(2.0f)); StaticModel* mushroomObject = mushroomNode->CreateComponent<StaticModel>(); mushroomObject->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl")); mushroomObject->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml")); mushroomObject->SetCastShadows(true); } // Create randomly sized boxes. If boxes are big enough, make them occluders const unsigned NUM_BOXES = 20; for (unsigned i = 0; i < NUM_BOXES; ++i) { Node* boxNode = scene_->CreateChild("Box"); float size = 1.0f + Random(10.0f); boxNode->SetPosition(Vector3(Random(80.0f) - 40.0f, size * 0.5f, Random(80.0f) - 40.0f)); boxNode->SetScale(size); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); boxObject->SetCastShadows(true); if (size >= 3.0f) boxObject->SetOccluder(true); } // Create the cameras. Limit far clip distance to match the fog cameraNode_ = scene_->CreateChild("Camera"); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(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(180.0f, Vector3::UP)); Camera* rearCamera = rearCameraNode_->CreateComponent<Camera>(); rearCamera->SetFarClip(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->SetViewOverrideFlags(VO_DISABLE_OCCLUSION); // Set an initial position for the front camera scene node above the plane cameraNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f)); }
void Decals::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); scene_ = new Scene(context_); // 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->SetScale(Vector3(100.0f, 1.0f, 100.0f)); StaticModel* planeObject = planeNode->CreateComponent<StaticModel>(); planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl")); planeObject->SetMaterial(cache->GetResource<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(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f)); zone->SetFogColor(Color(0.5f, 0.5f, 0.7f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.0f); // Create a directional light to the world. Enable cascaded shadows on it Node* lightNode = scene_->CreateChild("Directional light"); 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.0001f, 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 some mushrooms const unsigned NUM_MUSHROOMS = 240; for (unsigned i = 0; i < NUM_MUSHROOMS; ++i) { Node* mushroomNode = scene_->CreateChild("Mushroom"); mushroomNode->SetPosition(Vector3(Random(90.0f) - 45.0f, 0.0f, Random(90.0f) - 45.0f)); mushroomNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f)); mushroomNode->SetScale(0.5f + Random(2.0f)); StaticModel* mushroomObject = mushroomNode->CreateComponent<StaticModel>(); mushroomObject->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl")); mushroomObject->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml")); mushroomObject->SetCastShadows(true); } // Create randomly sized boxes. If boxes are big enough, make them occluders. Occluders will be software rasterized before // rendering to a low-resolution depth-only buffer to test the objects in the view frustum for visibility const unsigned NUM_BOXES = 20; for (unsigned i = 0; i < NUM_BOXES; ++i) { Node* boxNode = scene_->CreateChild("Box"); float size = 1.0f + Random(10.0f); boxNode->SetPosition(Vector3(Random(80.0f) - 40.0f, size * 0.5f, Random(80.0f) - 40.0f)); boxNode->SetScale(size); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); boxObject->SetCastShadows(true); if (size >= 5.0f) boxObject->SetOccluder(true); } // Create the camera. Limit far clip distance to match the fog cameraNode_ = scene_->CreateChild("Camera"); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(300.0f); // Set an initial position for the camera scene node above the plane cameraNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f)); }
void PhysicsStressTest::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(0.5f, 0.5f, 0.7f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.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 a floor object, 500 x 500 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(500.0f, 1.0f, 500.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 RigidBody* body = floorNode->CreateComponent<RigidBody>(); CollisionShape* shape = floorNode->CreateComponent<CollisionShape>(); shape->SetBox(Vector3::ONE); } { // Create static mushrooms with triangle mesh collision const unsigned NUM_MUSHROOMS = 50; for (unsigned i = 0; i < NUM_MUSHROOMS; ++i) { Node* mushroomNode = scene_->CreateChild("Mushroom"); mushroomNode->SetPosition(Vector3(Random(400.0f) - 200.0f, 0.0f, Random(400.0f) - 200.0f)); mushroomNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f)); mushroomNode->SetScale(5.0f + Random(5.0f)); StaticModel* mushroomObject = mushroomNode->CreateComponent<StaticModel>(); mushroomObject->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl")); mushroomObject->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml")); mushroomObject->SetCastShadows(true); RigidBody* body = 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->GetModel()); } } { // Create a large amount of falling physics objects const unsigned NUM_OBJECTS = 1000; for (unsigned i = 0; i < NUM_OBJECTS; ++i) { Node* boxNode = scene_->CreateChild("Box"); boxNode->SetPosition(Vector3(0.0f, i * 2.0f + 100.0f, 0.0f)); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/StoneSmall.xml")); boxObject->SetCastShadows(true); // Give the RigidBody mass to make it movable and also adjust friction RigidBody* body = boxNode->CreateComponent<RigidBody>(); body->SetMass(1.0f); body->SetFriction(1.0f); // Disable collision event signaling to reduce CPU load of the physics simulation body->SetCollisionEventMode(COLLISION_NEVER); CollisionShape* shape = boxNode->CreateComponent<CollisionShape>(); shape->SetBox(Vector3::ONE); } } // 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(context_); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(300.0f); // Set an initial position for the camera scene node above the floor cameraNode_->SetPosition(Vector3(0.0f, 3.0f, -20.0f)); }
void Sample::Start() { cache_ = GetSubsystem<ResourceCache>(); ui_ = GetSubsystem<UI>(); graphics_ = GetSubsystem<Graphics>(); ////////////////////////////////////////////////////////////////////////// // Get default styles XMLFile* styleFile = cache_->GetResource<XMLFile>("UI/IDEStyle.xml"); XMLFile* iconstyleFile = cache_->GetResource<XMLFile>("UI/EditorIcons.xml"); ui_->GetRoot()->SetDefaultStyle(styleFile); // create scene main ui rootUI_ = ui_->GetRoot()->CreateChild<UIElement>("UI"); rootUI_->SetSize(ui_->GetRoot()->GetSize()); rootUI_->SetTraversalMode(TM_DEPTH_FIRST); // This is needed for root-like element to prevent artifacts rootUI_->SetDefaultStyle(styleFile); // All user - created UI elements have lowest priority so they do not cover editor's windows rootUI_->SetPriority(-1000); ////////////////////////////////////////////////////////////////////////// /// Create HierarchyWindow uihierarchyWindow_ = rootUI_->CreateChild<HierarchyWindow>("UIHierarchyWindow"); uihierarchyWindow_->SetSize(200, 200); uihierarchyWindow_->SetDefaultStyle(styleFile); uihierarchyWindow_->SetStyleAuto(); uihierarchyWindow_->SetMovable(true); uihierarchyWindow_->SetIconStyle(iconstyleFile); uihierarchyWindow_->SetTitle("UI Hierarchy"); uihierarchyWindow_->SetAlignment(HA_RIGHT, VA_TOP); /// \todo // dont know why the auto style does not work ... uihierarchyWindow_->SetTexture(cache_->GetResource<Texture2D>("Textures/UI.png")); uihierarchyWindow_->SetImageRect(IntRect(48, 0, 64, 16)); uihierarchyWindow_->SetBorder(IntRect(4, 4, 4, 4)); uihierarchyWindow_->SetResizeBorder(IntRect(8, 8, 8, 8)); sceneHierarchyWindow_ = rootUI_->CreateChild<HierarchyWindow>("SceneHierarchyWindow"); sceneHierarchyWindow_->SetSize(200, 200); sceneHierarchyWindow_->SetDefaultStyle(styleFile); sceneHierarchyWindow_->SetStyleAuto(); sceneHierarchyWindow_->SetMovable(true); sceneHierarchyWindow_->SetIconStyle(iconstyleFile); sceneHierarchyWindow_->SetTitle("Scene Hierarchy"); sceneHierarchyWindow_->SetAlignment(HA_LEFT, VA_TOP); /// \todo // dont know why the auto style does not work ... sceneHierarchyWindow_->SetTexture(cache_->GetResource<Texture2D>("Textures/UI.png")); sceneHierarchyWindow_->SetImageRect(IntRect(48, 0, 64, 16)); sceneHierarchyWindow_->SetBorder(IntRect(4, 4, 4, 4)); sceneHierarchyWindow_->SetResizeBorder(IntRect(8, 8, 8, 8)); ////////////////////////////////////////////////////////////////////////// /// Create console console_ = engine_->CreateConsole(); console_->SetDefaultStyle(styleFile); ////////////////////////////////////////////////////////////////////////// /// Create debug HUD. debugHud_ = engine_->CreateDebugHud(); debugHud_->SetDefaultStyle(styleFile); ////////////////////////////////////////////////////////////////////////// /// Subscribe key down event SubscribeToEvent(E_KEYDOWN, HANDLER(Sample, HandleKeyDown)); // edit clear color, set background color Renderer* renderer = GetSubsystem<Renderer>(); Zone* zone = renderer->GetDefaultZone(); zone->SetFogColor(Color(0.3f, 0.3f, 0.4f)); // Set background color for the scene ////////////////////////////////////////////////////////////////////////// /// Create Cursor // Cursor* cursor_ = new Cursor(context_); // cursor_->SetStyleAuto(styleFile); // ui_->SetCursor(cursor_); // if (GetPlatform() == "Android" || GetPlatform() == "iOS") // ui_->GetCursor()->SetVisible(false); /// Show Platform Cursor Input* input = GetSubsystem<Input>(); input->SetMouseVisible(true); // Create the scene content CreateScene(); // Create the UI content CreateInstructions(); // Setup the viewport for displaying the scene SetupViewport(); // Hook up to the frame update and render post-update events SubscribeToEvents(); uihierarchyWindow_->SetUIElement(rootUI_); }
void RenderToTexture::CreateScene() { ResourceCache* cache = GetSubsystem<ResourceCache>(); { // Create the scene which will be rendered to a texture rttScene_ = new Scene(context_); // 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(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.05f, 0.1f, 0.15f)); zone->SetFogColor(Color(0.1f, 0.2f, 0.3f)); zone->SetFogStart(10.0f); zone->SetFogEnd(100.0f); // Create randomly positioned and oriented box StaticModels in the scene const unsigned NUM_OBJECTS = 2000; for (unsigned i = 0; i < NUM_OBJECTS; ++i) { Node* boxNode = rttScene_->CreateChild("Box"); boxNode->SetPosition(Vector3(Random(200.0f) - 100.0f, Random(200.0f) - 100.0f, Random(200.0f) - 100.0f)); // Orient using random pitch, yaw and roll Euler angles boxNode->SetRotation(Quaternion(Random(360.0f), Random(360.0f), Random(360.0f))); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<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 = boxNode->CreateComponent<Rotator>(); rotator->SetRotationSpeed(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->SetFarClip(100.0f); // Create a point light to the camera scene node Light* light = rttCameraNode_->CreateComponent<Light>(); light->SetLightType(LIGHT_POINT); light->SetRange(30.0f); } { // Create the scene in which we move around scene_ = new Scene(context_); // 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(BoundingBox(-1000.0f, 1000.0f)); zone->SetAmbientColor(Color(0.1f, 0.1f, 0.1f)); zone->SetFogStart(100.0f); zone->SetFogEnd(300.0f); // Create a directional light without shadows Node* lightNode = scene_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.5f, -1.0f, 0.5f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetColor(Color(0.2f, 0.2f, 0.2f)); light->SetSpecularIntensity(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->SetPosition(Vector3(x * 20.5f, -0.5f, y * 20.5f)); floorNode->SetScale(Vector3(20.0f, 1.0f, 20.f)); StaticModel* floorObject = floorNode->CreateComponent<StaticModel>(); floorObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); floorObject->SetMaterial(cache->GetResource<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->SetPosition(Vector3(0.0f, 10.0f, 0.0f)); boxNode->SetScale(Vector3(21.0f, 16.0f, 0.5f)); StaticModel* boxObject = boxNode->CreateComponent<StaticModel>(); boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl")); boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); Node* screenNode = scene_->CreateChild("Screen"); screenNode->SetPosition(Vector3(0.0f, 10.0f, -0.27f)); screenNode->SetRotation(Quaternion(-90.0f, 0.0f, 0.0f)); screenNode->SetScale(Vector3(20.0f, 0.0f, 15.0f)); StaticModel* screenObject = screenNode->CreateComponent<StaticModel>(); screenObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl")); // Create a renderable texture (1024x768, RGB format), enable bilinear filtering on it SharedPtr<Texture2D> renderTexture(new Texture2D(context_)); renderTexture->SetSize(1024, 768, Graphics::GetRGBFormat(), TEXTURE_RENDERTARGET); renderTexture->SetFilterMode(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 SharedPtr<Material> renderMaterial(new Material(context_)); renderMaterial->SetTechnique(0, cache->GetResource<Technique>("Techniques/DiffUnlit.xml")); renderMaterial->SetTexture(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->GetRenderSurface(); SharedPtr<Viewport> rttViewport(new Viewport(context_, rttScene_, rttCameraNode_->GetComponent<Camera>())); surface->SetViewport(0, rttViewport); } // Create the camera which we will move around. Limit far clip distance to match the fog cameraNode_ = scene_->CreateChild("Camera"); Camera* camera = cameraNode_->CreateComponent<Camera>(); camera->SetFarClip(300.0f); // Set an initial position for the camera scene node above the plane cameraNode_->SetPosition(Vector3(0.0f, 7.0f, -30.0f)); } }
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); } */ }