void EnvironmentLight::UpdateSignals()
{
    if (!ViewEnabled())
        return;

    Entity* parent = ParentEntity();
    if (!parent)
        return;

    if (parent->ParentScene())
        world_ = parent->ParentScene()->Subsystem<GraphicsWorld>();

    if (world_)
    {
        Urho3D::Scene* urhoScene = world_->UrhoScene();
        node_ = urhoScene->CreateChild("EnvironmentLight");
        light_ = node_->CreateComponent<Urho3D::Light>();
        light_->SetLightType(Urho3D::LIGHT_DIRECTIONAL);
        
        Urho3D::Zone* zone = world_->UrhoZone();
        if (zone)
            zone->SetAmbientColor(ambientColor.Get());

        node_->SetDirection(sunDirection.Get());
        light_->SetColor(sunColor.Get());
        light_->SetBrightness(brightness.Get());
        light_->SetCastShadows(sunCastShadows.Get());
        // Setup basic shadow cascade for desktops
        #ifndef ANDROID
        light_->SetShadowBias(Urho3D::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(Urho3D::CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f));
        #endif
    }
}
void EnvironmentLight::AttributesChanged()
{
    if (!ViewEnabled() || !light_)
        return;

    if (sunColor.ValueChanged())
        light_->SetColor(sunColor.Get());
    if (ambientColor.ValueChanged() && world_)
    {
        Urho3D::Zone* zone = world_->UrhoZone();
        if (zone)
            zone->SetAmbientColor(ambientColor.Get());
    }
    if (sunDirection.ValueChanged())
        node_->SetDirection(sunDirection.Get());
    if (sunCastShadows.ValueChanged())
        light_->SetCastShadows(sunCastShadows.Get());
    if (brightness.ValueChanged())
        light_->SetBrightness(brightness.Get());
}
GraphicsWorld::GraphicsWorld(UrhoRenderer* owner, Scene* scene) :
    Object(owner->GetContext()),
    framework_(scene->GetFramework()),
    renderer_(owner),
    scene_(scene)
{
    urhoScene_ = new Urho3D::Scene(context_);
    urhoScene_->CreateComponent<Urho3D::Octree>();
    urhoScene_->CreateComponent<Urho3D::DebugRenderer>();

    // Set a default ambient color that matches the default ambient color of EnvironmentLight, in case there is no environmentlight component.
    Urho3D::Zone* zone = urhoScene_->CreateComponent<Urho3D::Zone>();
    // Just set up a large bounding zone for the whole scene
    /// \todo The octree size is not adjusted yet, so objects outside the default octree range always end up in the root octqant
    zone->SetBoundingBox(Urho3D::BoundingBox(-100000.0f, 100000.0f));
    zone->SetAmbientColor(DefaultSceneAmbientLightColor());
    
    SetDefaultSceneFog();

    SubscribeToEvent(Urho3D::E_POSTRENDERUPDATE, URHO3D_HANDLER(GraphicsWorld, HandlePostRenderUpdate));
}
void HelloWorld::Start()
{
	Urho3D::Graphics* graphics = GetSubsystem<Urho3D::Graphics>();

	helloScene_ = new Urho3D::Scene(context_);
	helloScene_->CreateComponent<Urho3D::Octree>();
	
	auto objectNode = helloScene_->CreateChild();
	Urho3D::Node* lightNode = helloScene_->CreateChild();
	Urho3D::Node* cameraNode = helloScene_->CreateChild();
	//Urho3D::StaticModel* object = objectNode->CreateComponent<Urho3D::StaticModel>();
	//object->SetModel(GetSubsystem<Urho3D::ResourceCache>()->GetResource<Urho3D::Model>("Models/Mushroom.mdl"));
	//object->SetMaterial(GetSubsystem<Urho3D::ResourceCache>()->GetResource<Urho3D::Material>("Materials/Mushroom.xml"));
	Urho3D::Light* light = lightNode->CreateComponent<Urho3D::Light>();
	light->SetLightType(Urho3D::LIGHT_DIRECTIONAL);
	lightNode->SetDirection(Urho3D::Vector3(-1.0f, -1.0f, -1.0f));
	Urho3D::Camera* camera = cameraNode->CreateComponent<Urho3D::Camera>();
	camera->SetFarClip(10000.f);
	cameraNode->SetPosition(Urho3D::Vector3(0.0f, 0.f, 0.0f));
	cameraNode->SetRotation(Urho3D::Quaternion(5.f, Urho3D::Vector3(0.f, 1.f, 0.f)));
	Urho3D::Viewport* vp = new Urho3D::Viewport(context_, helloScene_, camera);
	GetSubsystem<Urho3D::Renderer>()->SetViewport(0, vp);
	//CreateText();
	SubscribeToEvents();

	//Urho3D::SharedPtr<Urho3D::RenderPath> effectRenderPath = vp->GetRenderPath()->Clone();
	//effectRenderPath->Append(GetSubsystem<Urho3D::ResourceCache>()->GetResource<Urho3D::XMLFile>("PostProcess/ColorCorrection.xml"));
	//effectRenderPath->Append(GetSubsystem<Urho3D::ResourceCache>()->GetResource<Urho3D::XMLFile>("PostProcess/FXAA3.xml"));
	//// Make the bloom mixing parameter more pronounced
	//effectRenderPath->SetShaderParameter("BloomMix", Urho3D::Vector2(0.9f, 0.6f));
	//effectRenderPath->SetEnabled("Bloom", false);
	//effectRenderPath->SetEnabled("FXAA3", false);
	//vp->SetRenderPath(effectRenderPath);

	Urho3D::Node* node = helloScene_->CreateChild();
	//node->SetPosition(Urho3D::Vector3(0.0f, 0.0f, 0.0f));
	//node->SetRotation(Urho3D::Quaternion(0.0f, 0.f, 0.0f));
	//node->CreateComponent<Urho3D::Rocket::RocketDocument2D>();

	// Left
	
	node = helloScene_->CreateChild();
	node->SetPosition(Urho3D::Vector3(0.0f, 0.0f, 250.0f));
	// This will orient the node to "billboard" the document
	node->SetWorldRotation(camera->GetFaceCameraRotation(node->GetPosition(), node->GetRotation(), Urho3D::FC_ROTATE_XYZ));
	//node->SetRotation(Urho3D::Quaternion(30.0f, -60.f, 0.0f));
	node->CreateComponent<Urho3D::Rocket::RocketDocument2D>();


	Urho3D::Node* zoneNode = helloScene_->CreateChild("Zone");
	Urho3D::Zone* zone = zoneNode->CreateComponent<Urho3D::Zone>();
	zone->SetBoundingBox(Urho3D::BoundingBox(-10000.0f, 10000.0f));
	zone->SetAmbientColor(Urho3D::Color(0.35f, 0.35f, 0.25f));
	zone->SetFogColor(Urho3D::Color(0.5f, 0.6f, 0.7f));
	zone->SetFogStart(100.0f);
//	zone->SetFogEnd(300.0f);
	zone->SetFogEnd(3000.0f);

	//node->LookAt(cameraNode->GetPosition());
	//Right
	//node = helloScene_->CreateChild();
	//node->SetPosition(Urho3D::Vector3(100.0f, 0.0f, 0.0f));
	//node->SetRotation(Urho3D::Quaternion(0.0f, 45.f, 0.0f));
	//node->CreateComponent<Urho3D::Rocket::RocketDocument3D>();

	//graphics->SetDefaultTextureFilterMode(Urho3D::FILTER_ANISOTROPIC);
}