Skip to content

Commit

Permalink
saturation control for sprites, materials and some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
turanszkij authored Aug 30, 2024
1 parent 9ca4c91 commit c5c77b0
Show file tree
Hide file tree
Showing 28 changed files with 217 additions and 52 deletions.
12 changes: 8 additions & 4 deletions Content/Documentation/ScriptingAPI-Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,13 @@ Gives you the ability to render text with a custom font.


### Texture
Just holds texture information in VRAM.
A texture image data.
- [constructor]Texture(opt string filename) -- creates a texture from file
- [outer]texturehelper -- a global helper texture creation utility
- GetWidth() : int
- GetHeight() : int
- GetDepth() : int
- GetArraySize() : int
- GetLogo() : Texture -- returns the Wicked Engine logo texture
- CreateGradientTexture(
GradientType type = GradientType.Linear,
Expand Down Expand Up @@ -1963,11 +1967,11 @@ Playstation button codes:
- SetLinearVelocity(RigidBodyPhysicsComponent component, Vector velocity) -- Set the linear velocity manually
- SetAngularVelocity(RigidBodyPhysicsComponent component, Vector velocity) -- Set the angular velocity manually
- ApplyForce(RigidBodyPhysicsComponent component, Vector force) -- Apply force at body center
- ApplyForceAt(RigidBodyPhysicsComponent component, Vector force, Vector at) -- Apply force at body local position
- ApplyForceAt(RigidBodyPhysicsComponent component, Vector force, Vector at, bool at_local = true) -- Apply force at body local position (at_local controls whether the at is in body's local space or not)
- ApplyImpulse(RigidBodyPhysicsComponent component, Vector impulse) -- Apply impulse at body center
- ApplyImpulse(HumanoidComponent humanoid, HumanoidBone bone, Vector impulse) -- Apply impulse at body center of ragdoll bone
- ApplyImpulseAt(RigidBodyPhysicsComponent component, Vector impulse, Vector at) -- Apply impulse at body local position
- ApplyImpulseAt(HumanoidComponent humanoid, HumanoidBone bone, Vector impulse, Vector at) -- Apply impulse at body local position of ragdoll bone
- ApplyImpulseAt(RigidBodyPhysicsComponent component, Vector impulse, Vector at, bool at_local = true) -- Apply impulse at body local position (at_local controls whether the at is in body's local space or not)
- ApplyImpulseAt(HumanoidComponent humanoid, HumanoidBone bone, Vector impulse, Vector at, bool at_local = true) -- Apply impulse at body local position of ragdoll bone (at_local controls whether the at is in body's local space or not)
- ApplyTorque(RigidBodyPhysicsComponent component, Vector torque) -- Apply torque at body center
- ActivateAllRigidBodies(Scene scene) -- Activate all rigid bodies in the scene
- SetActivationState(RigidBodyPhysicsComponent component, int state) -- Force set activation state to rigid body. Use a value ACTIVATION_STATE_ACTIVE or ACTIVATION_STATE_INACTIVE
Expand Down
20 changes: 19 additions & 1 deletion Editor/MaterialWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void MaterialWindow::Create(EditorComponent* _editor)
{
editor = _editor;
wi::gui::Window::Create(ICON_MATERIAL " Material", wi::gui::Window::WindowControls::COLLAPSE | wi::gui::Window::WindowControls::CLOSE);
SetSize(XMFLOAT2(300, 1460));
SetSize(XMFLOAT2(300, 1500));

closeButton.SetTooltip("Delete MaterialComponent");
OnClose([=](wi::gui::EventArgs args) {
Expand Down Expand Up @@ -405,6 +405,22 @@ void MaterialWindow::Create(EditorComponent* _editor)
});
AddWidget(&emissiveSlider);

saturationSlider.Create(0, 2, 1, 1000, "Saturation: ");
saturationSlider.SetTooltip("Adjust the saturation of the material.");
saturationSlider.SetSize(XMFLOAT2(wid, hei));
saturationSlider.SetPos(XMFLOAT2(x, y += step));
saturationSlider.OnSlide([&](wi::gui::EventArgs args) {
wi::scene::Scene& scene = editor->GetCurrentScene();
for (auto& x : editor->translator.selected)
{
MaterialComponent* material = get_material(scene, x);
if (material == nullptr)
continue;
material->SetSaturation(args.fValue);
}
});
AddWidget(&saturationSlider);

cloakSlider.Create(0, 1.0f, 0.02f, 1000, "Cloak: ");
cloakSlider.SetTooltip("The cloak effect is a combination of transmission, refraction and roughness, without color tinging.");
cloakSlider.SetSize(XMFLOAT2(wid, hei));
Expand Down Expand Up @@ -1037,6 +1053,7 @@ void MaterialWindow::SetEntity(Entity entity)
transmissionSlider.SetValue(material->transmission);
refractionSlider.SetValue(material->refraction);
emissiveSlider.SetValue(material->emissiveColor.w);
saturationSlider.SetValue(material->saturation);
pomSlider.SetValue(material->parallaxOcclusionMapping);
anisotropyStrengthSlider.SetValue(material->anisotropy_strength);
anisotropyRotationSlider.SetValue(wi::math::RadiansToDegrees(material->anisotropy_rotation));
Expand Down Expand Up @@ -1215,6 +1232,7 @@ void MaterialWindow::ResizeLayout()
add(reflectanceSlider);
add(metalnessSlider);
add(emissiveSlider);
add(saturationSlider);
add(cloakSlider);
add(chromaticAberrationSlider);
add(transmissionSlider);
Expand Down
1 change: 1 addition & 0 deletions Editor/MaterialWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class MaterialWindow : public wi::gui::Window
wi::gui::Slider reflectanceSlider;
wi::gui::Slider metalnessSlider;
wi::gui::Slider emissiveSlider;
wi::gui::Slider saturationSlider;
wi::gui::Slider cloakSlider;
wi::gui::Slider chromaticAberrationSlider;
wi::gui::Slider transmissionSlider;
Expand Down
18 changes: 17 additions & 1 deletion Editor/SpriteWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void SpriteWindow::Create(EditorComponent* _editor)
editor = _editor;

wi::gui::Window::Create(ICON_SPRITE " Sprite", wi::gui::Window::WindowControls::COLLAPSE | wi::gui::Window::WindowControls::CLOSE);
SetSize(XMFLOAT2(670, 1500));
SetSize(XMFLOAT2(670, 1540));

closeButton.SetTooltip("Delete Sprite");
OnClose([=](wi::gui::EventArgs args) {
Expand Down Expand Up @@ -175,6 +175,20 @@ void SpriteWindow::Create(EditorComponent* _editor)
});
AddWidget(&rotationSlider);

saturationSlider.Create(0, 2, 1, 1000, "Saturation: ");
saturationSlider.SetTooltip("Modify saturation of the image.");
saturationSlider.OnSlide([=](wi::gui::EventArgs args) {
wi::scene::Scene& scene = editor->GetCurrentScene();
for (auto& x : editor->translator.selected)
{
wi::Sprite* sprite = scene.sprites.GetComponent(x.entity);
if (sprite == nullptr)
continue;
sprite->params.saturation = args.fValue;
}
});
AddWidget(&saturationSlider);

alphaStartSlider.Create(0, 1, 0, 10000, "Mask Alpha Start: ");
alphaStartSlider.SetTooltip("Constrain mask alpha to not go below this level.");
alphaStartSlider.OnSlide([=](wi::gui::EventArgs args) {
Expand Down Expand Up @@ -604,6 +618,7 @@ void SpriteWindow::SetEntity(wi::ecs::Entity entity)
pivotYSlider.SetValue(sprite->params.pivot.y);
intensitySlider.SetValue(sprite->params.intensity);
rotationSlider.SetValue(wi::math::RadiansToDegrees(sprite->params.rotation));
saturationSlider.SetValue(sprite->params.saturation);
alphaStartSlider.SetValue(sprite->params.mask_alpha_range_start);
alphaEndSlider.SetValue(sprite->params.mask_alpha_range_end);
borderSoftenSlider.SetValue(sprite->params.border_soften);
Expand Down Expand Up @@ -673,6 +688,7 @@ void SpriteWindow::ResizeLayout()
add(pivotYSlider);
add(intensitySlider);
add(rotationSlider);
add(saturationSlider);
add(alphaStartSlider);
add(alphaEndSlider);
add(borderSoftenSlider);
Expand Down
1 change: 1 addition & 0 deletions Editor/SpriteWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class SpriteWindow : public wi::gui::Window
wi::gui::Slider pivotYSlider;
wi::gui::Slider intensitySlider;
wi::gui::Slider rotationSlider;
wi::gui::Slider saturationSlider;
wi::gui::Slider alphaStartSlider;
wi::gui::Slider alphaEndSlider;
wi::gui::Slider borderSoftenSlider;
Expand Down
4 changes: 2 additions & 2 deletions WickedEngine/shaders/ShaderInterop_Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ struct ImageConstants
float2 b3;

float2 angular_softness_direction;
float angular_softness_scale;
float angular_softness_offset;
uint angular_softness_mad; // packed half2
float saturation;
};
CONSTANTBUFFER(image, ImageConstants, CBSLOT_IMAGE);

Expand Down
7 changes: 4 additions & 3 deletions WickedEngine/shaders/ShaderInterop_Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ struct alignas(16) ShaderMaterial
uint2 subsurfaceScattering_inv;

uint2 specular_chromatic;
uint2 sheenColor;
uint2 sheenColor_saturation;

float4 texMulAdd;

Expand Down Expand Up @@ -362,7 +362,7 @@ struct alignas(16) ShaderMaterial
subsurfaceScattering_inv = uint2(0, 0);

specular_chromatic = uint2(0, 0);
sheenColor = uint2(0, 0);
sheenColor_saturation = uint2(0, 0);

texMulAdd = float4(1, 1, 0, 0);

Expand Down Expand Up @@ -390,7 +390,8 @@ struct alignas(16) ShaderMaterial
inline half GetCloak() { return unpack_half4(emissive_cloak).a; }
inline half3 GetSpecular() { return unpack_half3(specular_chromatic); }
inline half GetChromaticAberration() { return unpack_half4(specular_chromatic).w; }
inline half3 GetSheenColor() { return unpack_half3(sheenColor); }
inline half3 GetSheenColor() { return unpack_half3(sheenColor_saturation); }
inline half GetSaturation() { return unpack_half4(sheenColor_saturation).w; }
inline half GetRoughness() { return unpack_half4(roughness_reflectance_metalness_refraction).x; }
inline half GetReflectance() { return unpack_half4(roughness_reflectance_metalness_refraction).y; }
inline half GetMetalness() { return unpack_half4(roughness_reflectance_metalness_refraction).z; }
Expand Down
17 changes: 17 additions & 0 deletions WickedEngine/shaders/globals.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -1939,4 +1939,21 @@ inline float3 get_right(float4x4 m)
return float3(m[0][0], m[0][1], m[0][2]);
}

half3x3 saturationMatrix(half saturation)
{
half3 luminance = half3(0.3086, 0.6094, 0.0820);
half oneMinusSat = 1.0 - saturation;

half3 red = half3(luminance * oneMinusSat);
red += half3(saturation, 0, 0);

half3 green = half3(luminance * oneMinusSat);
green += half3(0, saturation, 0);

half3 blue = half3(luminance * oneMinusSat);
blue += half3(0, 0, saturation);

return half3x3(red, green, blue);
}

#endif // WI_SHADER_GLOBALS_HF
7 changes: 5 additions & 2 deletions WickedEngine/shaders/imagePS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ float4 main(VertextoPixel input) : SV_TARGET
}

[branch]
if (image.angular_softness_scale > 0)
if (image.angular_softness_mad > 0)
{
float2 direction = normalize(uvsets.xy - 0.5);
float dp = dot(direction, image.angular_softness_direction);
Expand All @@ -91,14 +91,17 @@ float4 main(VertextoPixel input) : SV_TARGET
{
dp = saturate(dp);
}
float angular = saturate(mad(dp, image.angular_softness_scale, image.angular_softness_offset));
float2 angular_softness_mad = unpack_half2(image.angular_softness_mad);
float angular = saturate(mad(dp, angular_softness_mad.x, angular_softness_mad.y));
if (image.flags & IMAGE_FLAG_ANGULAR_INVERSE)
{
angular = 1 - angular;
}
angular = smoothstep(0, 1, angular);
color.a *= angular;
}

color.rgb = mul(saturationMatrix(image.saturation), color.rgb);

return color;
}
1 change: 1 addition & 0 deletions WickedEngine/shaders/objectHF.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,7 @@ float4 main(PixelInput input, in bool is_frontface : SV_IsFrontFace) : SV_Target
ApplyFog(dist, surface.V, color);
#endif // OBJECTSHADER_USE_POSITION3D

color.rgb = mul(saturationMatrix(material.GetSaturation()), color.rgb);

color = saturateMediump(color);

Expand Down
19 changes: 1 addition & 18 deletions WickedEngine/shaders/tonemapCS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,6 @@ float3 ACESFitted(float3 color)
return color;
}

float4x4 saturationMatrix(float saturation)
{
float3 luminance = float3(0.3086f, 0.6094f, 0.0820f);
float oneMinusSat = 1.0f - saturation;

float3 red = float3(luminance * oneMinusSat);
red += float3(saturation, 0, 0);

float3 green = float3(luminance * oneMinusSat);
green += float3(0, saturation, 0);

float3 blue = float3(luminance * oneMinusSat);
blue += float3(0, 0, saturation);

return float4x4(red, 0, green, 0, blue, 0, 0, 0, 0, 1);
}

[numthreads(POSTPROCESS_BLOCKSIZE, POSTPROCESS_BLOCKSIZE, 1)]
void main(uint3 DTid : SV_DispatchThreadID)
{
Expand Down Expand Up @@ -137,7 +120,7 @@ void main(uint3 DTid : SV_DispatchThreadID)
}

result.rgb = (result.rgb - 0.5f) * contrast + 0.5f + brightness;
result.rgb = (float3)(mul(saturationMatrix(saturation), result));
result.rgb = mul(saturationMatrix(saturation), result.rgb);

[branch]
if (tonemap_push.texture_output >= 0)
Expand Down
5 changes: 3 additions & 2 deletions WickedEngine/wiImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,11 @@ namespace wi::image
const float lightAngleScale = 1.0f / std::max(0.001f, innerConeAngleCos - outerConeAngleCos);
const float lightAngleOffset = -outerConeAngleCos * lightAngleScale;
image.angular_softness_direction = params.angular_softness_direction;
image.angular_softness_scale = lightAngleScale;
image.angular_softness_offset = lightAngleOffset;
image.angular_softness_mad = wi::math::pack_half2(lightAngleScale, lightAngleOffset);
}

image.saturation = params.saturation;

image.flags = 0;
if (params.isExtractNormalMapEnabled())
{
Expand Down
1 change: 1 addition & 0 deletions WickedEngine/wiImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ namespace wi::image
XMFLOAT2 angular_softness_direction = XMFLOAT2(0, 1);
float angular_softness_inner_angle = 0;
float angular_softness_outer_angle = 0;
float saturation = 1;

// you can deform the image by its corners (0: top left, 1: top right, 2: bottom left, 3: bottom right)
XMFLOAT2 corners[4] = {
Expand Down
20 changes: 20 additions & 0 deletions WickedEngine/wiImageParams_BindLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace wi::lua
lunamethod(ImageParams_BindLua, GetPivot),
lunamethod(ImageParams_BindLua, GetColor),
lunamethod(ImageParams_BindLua, GetOpacity),
lunamethod(ImageParams_BindLua, GetSaturation),
lunamethod(ImageParams_BindLua, GetFade),
lunamethod(ImageParams_BindLua, GetRotation),
lunamethod(ImageParams_BindLua, GetTexOffset),
Expand All @@ -28,6 +29,7 @@ namespace wi::lua
lunamethod(ImageParams_BindLua, SetPivot),
lunamethod(ImageParams_BindLua, SetColor),
lunamethod(ImageParams_BindLua, SetOpacity),
lunamethod(ImageParams_BindLua, SetSaturation),
lunamethod(ImageParams_BindLua, SetFade),
lunamethod(ImageParams_BindLua, SetStencil),
lunamethod(ImageParams_BindLua, SetStencilRefMode),
Expand Down Expand Up @@ -97,6 +99,11 @@ namespace wi::lua
wi::lua::SSetFloat(L, params.opacity);
return 1;
}
int ImageParams_BindLua::GetSaturation(lua_State* L)
{
wi::lua::SSetFloat(L, params.saturation);
return 1;
}
int ImageParams_BindLua::GetFade(lua_State* L)
{
wi::lua::SSetFloat(L, params.fade);
Expand Down Expand Up @@ -244,6 +251,19 @@ namespace wi::lua
}
return 0;
}
int ImageParams_BindLua::SetSaturation(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 0)
{
params.saturation = wi::lua::SGetFloat(L, 1);
}
else
{
wi::lua::SError(L, "SetSaturation(float x) not enough arguments!");
}
return 0;
}
int ImageParams_BindLua::SetFade(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
Expand Down
2 changes: 2 additions & 0 deletions WickedEngine/wiImageParams_BindLua.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace wi::lua
int GetPivot(lua_State* L);
int GetColor(lua_State* L);
int GetOpacity(lua_State* L);
int GetSaturation(lua_State* L);
int GetFade(lua_State* L);
int GetRotation(lua_State* L);
int GetTexOffset(lua_State* L);
Expand All @@ -41,6 +42,7 @@ namespace wi::lua
int SetPivot(lua_State* L);
int SetColor(lua_State* L);
int SetOpacity(lua_State* L);
int SetSaturation(lua_State* L);
int SetFade(lua_State* L);
int SetStencil(lua_State* L);
int SetStencilRefMode(lua_State* L);
Expand Down
13 changes: 8 additions & 5 deletions WickedEngine/wiPhysics.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ namespace wi::physics
wi::scene::RigidBodyPhysicsComponent& physicscomponent,
const XMFLOAT3& force
);
// Apply force at body local position
// Apply force at position
void ApplyForceAt(
wi::scene::RigidBodyPhysicsComponent& physicscomponent,
const XMFLOAT3& force,
const XMFLOAT3& at
const XMFLOAT3& at,
bool at_local = true // whether at is in local space of the body or not (global)
);

// Apply impulse at body center
Expand All @@ -80,17 +81,19 @@ namespace wi::physics
wi::scene::HumanoidComponent::HumanoidBone bone,
const XMFLOAT3& impulse
);
// Apply impulse at body local position
// Apply impulse at position
void ApplyImpulseAt(
wi::scene::RigidBodyPhysicsComponent& physicscomponent,
const XMFLOAT3& impulse,
const XMFLOAT3& at
const XMFLOAT3& at,
bool at_local = true // whether at is in local space of the body or not (global)
);
void ApplyImpulseAt(
wi::scene::HumanoidComponent& humanoid,
wi::scene::HumanoidComponent::HumanoidBone bone,
const XMFLOAT3& impulse,
const XMFLOAT3& at
const XMFLOAT3& at,
bool at_local = true // whether at is in local space of the body or not (global)
);

void ApplyTorque(
Expand Down
Loading

0 comments on commit c5c77b0

Please sign in to comment.