Skip to content

Commit

Permalink
#123 Update sun position (SkySun use direction light or object orient…
Browse files Browse the repository at this point in the history
…ation)
  • Loading branch information
ducphamhong committed May 26, 2022
1 parent ac88f67 commit 2d4a7d8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
13 changes: 6 additions & 7 deletions Assets/BuiltIn/Shader/SkySun/SkySun.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
<uniform name="uWorldMatrix" type="WORLD" value="0" float="16" matrix="true"/>
</vs>
<fs>
<uniform name="uLightDirection" type="WORLD_LIGHT_DIRECTION" value="1.0, 1.0, 1.0, 1.0" float="4"/>
<uniform name="uLightDirection" type="MATERIAL_PARAM" valueIndex="0" value="1.0, 1.0, 1.0, 1.0" float="4"/>
<uniform name="uCamPosition" type="WORLD_CAMERA_POSITION" value="0.0, 0.0, 0.0, 1.0" float="4"/>

<uniform name="uIntensity" type="MATERIAL_PARAM" valueIndex="0" value="1.1, 800.0, 0.0, 0.0" float="4"/>
<uniform name="uAtmospheric" type="MATERIAL_PARAM" valueIndex="1" value="1.0, 0.8, 0.7, 0.1" float="4"/>
<uniform name="uSun" type="MATERIAL_PARAM" valueIndex="2" value="1.0, 0.6, 0.1, 0.5" float="4"/>
<uniform name="uGlare1" type="MATERIAL_PARAM" valueIndex="3" value="1.0, 0.6, 0.1, 0.4" float="4"/>
<uniform name="uGlare2" type="MATERIAL_PARAM" valueIndex="4" value="1.0, 0.4, 0.2, 0.2" float="4"/>
<uniform name="uIntensity" type="MATERIAL_PARAM" valueIndex="1" value="1.1, 800.0, 0.0, 0.0" float="4"/>
<uniform name="uAtmospheric" type="MATERIAL_PARAM" valueIndex="2" value="1.0, 0.8, 0.7, 0.1" float="4"/>
<uniform name="uSun" type="MATERIAL_PARAM" valueIndex="3" value="1.0, 0.6, 0.1, 0.5" float="4"/>
<uniform name="uGlare1" type="MATERIAL_PARAM" valueIndex="4" value="1.0, 0.6, 0.1, 0.4" float="4"/>
<uniform name="uGlare2" type="MATERIAL_PARAM" valueIndex="5" value="1.0, 0.4, 0.2, 0.2" float="4"/>
</fs>
</uniforms>
<customUI>
Expand Down
53 changes: 49 additions & 4 deletions Projects/Skylicht/Components/Source/SkySun/CSkySun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ This file is part of the "Skylicht Engine".
#include "GameObject/CGameObject.h"
#include "Entity/CEntityManager.h"

#include "Lighting/CDirectionalLight.h"
#include "Material/Shader/ShaderCallback/CShaderLighting.h"

namespace Skylicht
{
ACTIVATOR_REGISTER(CSkySun);
Expand All @@ -45,7 +48,9 @@ namespace Skylicht
m_glare1Intensity(0.3),
m_glare2Color(255, 255, 51, 25),
m_glare2Intensity(0.15),
m_changed(true)
m_changed(true),
m_useDirectionLight(true),
m_uniformDirection(NULL)
{
}

Expand All @@ -59,15 +64,51 @@ namespace Skylicht
{
m_skySunData = m_gameObject->getEntity()->addData<CSkySunData>();
m_gameObject->getEntityManager()->addRenderSystem<CSkySunRender>();

CMaterial* material = m_skySunData->SkySunMaterial;
m_uniformDirection = material->getUniform("uLightDirection");
}

void CSkySun::updateComponent()
{
CMaterial* material = m_skySunData->SkySunMaterial;

if (m_uniformDirection != NULL)
{
if (m_useDirectionLight)
{
// use direction light
CDirectionalLight* light = CShaderLighting::getDirectionalLight();
if (light != NULL)
{
const core::vector3df& d = -light->getDirection();
m_uniformDirection->FloatValue[0] = d.X;
m_uniformDirection->FloatValue[1] = d.Y;
m_uniformDirection->FloatValue[2] = d.Z;
m_uniformDirection->FloatValue[3] = 0.0f;
}
else
{
m_uniformDirection->FloatValue[0] = 0.0f;
m_uniformDirection->FloatValue[1] = -1.0f;
m_uniformDirection->FloatValue[2] = 0.0f;
m_uniformDirection->FloatValue[3] = 0.0f;
}
}
else
{
// use orientation
core::vector3df front = m_gameObject->getTransformEuler()->getFront();
m_uniformDirection->FloatValue[0] = front.X;
m_uniformDirection->FloatValue[1] = front.Y;
m_uniformDirection->FloatValue[2] = front.Z;
m_uniformDirection->FloatValue[3] = 0.0f;
}
}

if (m_changed)
{
CMaterial* material = m_skySunData->SkySunMaterial;
float value[4] = { 0 };

value[0] = m_skyIntensity;
value[1] = m_sunSize;
material->setUniform4("uIntensity", value);
Expand All @@ -84,9 +125,10 @@ namespace Skylicht
getUniformValue(m_glare2Color, m_glare2Intensity, value);
material->setUniform4("uGlare2", value);

material->updateShaderParams();
m_changed = false;
}

material->updateShaderParams();
}

void CSkySun::getUniformValue(const SColor& c, float intensity, float* value)
Expand Down Expand Up @@ -116,6 +158,7 @@ namespace Skylicht
object->autoRelease(sunProperty);
object->autoRelease(new CFloatProperty(object, "Sun Intensity", m_sunIntensity, 0.0f));
object->autoRelease(new CFloatProperty(object, "Sun Size", m_sunSize, 1.0f, 5000.0f));
object->autoRelease(new CBoolProperty(object, "Direction Light", m_useDirectionLight));

CColorProperty* glareProperty = new CColorProperty(object, "Glare1 Color", m_glare1Color);
glareProperty->setUIHeader("Glare");
Expand All @@ -132,6 +175,8 @@ namespace Skylicht
{
CComponentSystem::loadSerializable(object);

m_useDirectionLight = object->get<bool>("Direction Light", true);

m_skyIntensity = object->get<float>("Intensity", 1.1f);

m_atmosphericColor = object->get<SColor>("Atmospheric Color", SColor(255, 255, 204, 178));
Expand Down
4 changes: 4 additions & 0 deletions Projects/Skylicht/Components/Source/SkySun/CSkySun.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ namespace Skylicht
protected:
CSkySunData* m_skySunData;

bool m_useDirectionLight;

float m_skyIntensity;
float m_sunSize;

Expand All @@ -52,6 +54,8 @@ namespace Skylicht

bool m_changed;

CMaterial::SUniformValue* m_uniformDirection;

public:
CSkySun();

Expand Down

0 comments on commit 2d4a7d8

Please sign in to comment.