Created
November 12, 2015 15:38
-
-
Save 1vanK/5abb23c0d092616e8142 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "Urho3DAll.h" | |
| #include "Game.h" | |
| #include "SmoothFocus.h" | |
| URHO3D_DEFINE_APPLICATION_MAIN(Game) | |
| Game::Game(Context* context) : | |
| Application(context), | |
| yaw_(0.0f), | |
| pitch_(0.0f) | |
| { | |
| SmoothFocus::RegisterObject(context); | |
| } | |
| void Game::Setup() | |
| { | |
| engineParameters_["WindowTitle"] = GetTypeName(); | |
| // engineParameters_["LogName"] = GetSubsystem<FileSystem>()->GetAppPreferencesDir("urho3d", "logs") + GetTypeName() + ".log"; | |
| engineParameters_["FullScreen"] = false; | |
| engineParameters_["Headless"] = false; | |
| engineParameters_["WindowWidth"] = 800; | |
| engineParameters_["WindowHeight"] = 600; | |
| // engineParameters_["ResourcePaths"] = "Data;CoreData;MyData"; | |
| } | |
| void Game::Start() | |
| { | |
| CreateScene(); | |
| SetupViewport(); | |
| SubscribeToEvents(); | |
| ResourceCache* cache = GetSubsystem<ResourceCache>(); | |
| XMLFile* xmlFile = cache->GetResource<XMLFile>("UI/DefaultStyle.xml"); | |
| DebugHud* debugHud = engine_->CreateDebugHud(); | |
| debugHud->SetDefaultStyle(xmlFile); | |
| } | |
| void Game::SetupViewport() | |
| { | |
| ResourceCache* cache = GetSubsystem<ResourceCache>(); | |
| Renderer* renderer = GetSubsystem<Renderer>(); | |
| SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>())); | |
| viewport->SetRenderPath(cache->GetResource<XMLFile>("CoreData/RenderPaths/ForwardHWDepthDOF.xml")); | |
| SmoothFocus* sf = cameraNode_->CreateComponent<SmoothFocus>(); | |
| sf->SetViewport(viewport); | |
| sf->SetFocusTime(0.5f); | |
| renderer->SetViewport(0, viewport); | |
| } | |
| void Game::CreateScene() | |
| { | |
| ResourceCache* cache = GetSubsystem<ResourceCache>(); | |
| scene_ = new Scene(context_); | |
| scene_->CreateComponent<Octree>(); | |
| 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")); | |
| Node* lightNode = scene_->CreateChild("DirectionalLight"); | |
| lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f)); | |
| Light* light = lightNode->CreateComponent<Light>(); | |
| light->SetColor(Color(0.5f, 0.5f, 0.5f)); | |
| 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->SetShadowIntensity(0.5f); | |
| Node* zoneNode = scene_->CreateChild("Zone"); | |
| Zone* zone = zoneNode->CreateComponent<Zone>(); | |
| zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f)); | |
| zone->SetAmbientColor(Color(0.5f, 0.5f, 0.5f)); | |
| zone->SetFogColor(Color(0.4f, 0.5f, 0.8f)); | |
| zone->SetFogStart(100.0f); | |
| zone->SetFogEnd(300.0f); | |
| const unsigned NUM_OBJECTS = 200; | |
| for (unsigned i = 0; i < NUM_OBJECTS; ++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); | |
| } | |
| cameraNode_ = scene_->CreateChild("Camera"); | |
| cameraNode_->CreateComponent<Camera>(); | |
| cameraNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f)); | |
| } | |
| void Game::MoveCamera(float timeStep) | |
| { | |
| Input* input = GetSubsystem<Input>(); | |
| const float MOVE_SPEED = 20.0f; | |
| const float MOUSE_SENSITIVITY = 0.1f; | |
| IntVector2 mouseMove = input->GetMouseMove(); | |
| yaw_ += MOUSE_SENSITIVITY * mouseMove.x_; | |
| pitch_ += MOUSE_SENSITIVITY * mouseMove.y_; | |
| pitch_ = Clamp(pitch_, -90.0f, 90.0f); | |
| cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f)); | |
| if (input->GetKeyDown('W')) | |
| cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep); | |
| if (input->GetKeyDown('S')) | |
| cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep); | |
| if (input->GetKeyDown('A')) | |
| cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep); | |
| if (input->GetKeyDown('D')) | |
| cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep); | |
| if (input->GetKeyPress(KEY_F2)) | |
| GetSubsystem<DebugHud>()->ToggleAll(); | |
| } | |
| void Game::SubscribeToEvents() | |
| { | |
| SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(Game, HandleUpdate)); | |
| } | |
| void Game::HandleUpdate(StringHash eventType, VariantMap& eventData) | |
| { | |
| using namespace Update; | |
| float timeStep = eventData[P_TIMESTEP].GetFloat(); | |
| MoveCamera(timeStep); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "Urho3DAll.h" | |
| #include "SmoothFocus.h" | |
| SmoothFocus::SmoothFocus(Context* context) : LogicComponent(context) | |
| { | |
| SetUpdateEventMask(USE_POSTUPDATE | USE_UPDATE); | |
| } | |
| void SmoothFocus::RegisterObject(Context* context) | |
| { | |
| context->RegisterFactory<SmoothFocus>(); | |
| } | |
| void SmoothFocus::Start() | |
| { | |
| camera = GetNode()->GetComponent<Camera>(); | |
| octree = GetScene()->GetComponent<Octree>(); | |
| smoothTimeSec = 0.5f; | |
| smoothTimeElapsed = 0.001f; | |
| } | |
| void SmoothFocus::Update(float timeStep) | |
| { | |
| } | |
| void SmoothFocus::PostUpdate(float timeStep) | |
| { | |
| if (!vp | !rp) return; | |
| float targetFocus = GetNearestFocus(camera->GetFarClip()); | |
| smoothFocus = Lerp(smoothFocus, targetFocus, timeStep * 10.0f / smoothTimeSec); | |
| rp->SetShaderParameter("SmoothFocus", Variant(smoothFocus)); | |
| } | |
| void SmoothFocus::SetViewport(SharedPtr<Viewport> viewport) | |
| { | |
| vp = SharedPtr<Viewport>(viewport); | |
| rp = SharedPtr<RenderPath>(vp->GetRenderPath()); | |
| rp->SetShaderParameter("SmoothFocusEnabled", Variant(true)); | |
| } | |
| float SmoothFocus::GetNearestFocus(float zCameraFarClip) | |
| { | |
| if (!octree | !camera) return zCameraFarClip; | |
| PODVector<RayQueryResult> results; | |
| //Graphics* g = GetSubsystem<Graphics>(); | |
| //Ray ray = vp->GetScreenRay(g->GetWidth() / 2, g->GetHeight() / 2); | |
| //Ray ray = Ray(camera->GetNode()->GetWorldPosition(), camera->GetNode()->GetWorldDirection()); | |
| Ray ray = camera->GetScreenRay(0.5f, 0.5f); | |
| RayOctreeQuery query(results, ray, RAY_TRIANGLE, zCameraFarClip, DRAWABLE_GEOMETRY, -1); | |
| octree->RaycastSingle(query); | |
| if (results.Size()) | |
| { | |
| for (unsigned int i = 0; i < results.Size(); i++) | |
| { | |
| RayQueryResult& result = results[i]; | |
| Vector3 hitPoint = result.position_; | |
| float distance = (camera->GetNode()->GetWorldPosition() - hitPoint).Length(); | |
| return distance; | |
| } | |
| } | |
| return zCameraFarClip; | |
| } | |
| void SmoothFocus::SetFocusTime(float time) | |
| { | |
| smoothTimeSec = time; | |
| } | |
| void SmoothFocus::SetSmoothFocusEnabled(bool enabled) | |
| { | |
| rp->SetShaderParameter("SmoothFocusEnabled", Variant(enabled)); | |
| } | |
| bool SmoothFocus::GetSmoothFocusEnabled() | |
| { | |
| Variant v = rp->GetShaderParameter("SmoothFocusEnabled"); | |
| return v.GetBool(); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
MonkeyFirst/urho3d-post-process-dof#1