Skip to content

Commit

Permalink
[forest] minor lighting and grass tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
PanosK92 committed Feb 23, 2025
1 parent fd58b65 commit efcd9d5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
62 changes: 33 additions & 29 deletions data/shaders/common_vertex_processing.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -86,30 +86,31 @@ static float3 extract_position(matrix transform)
return float3(transform._31, transform._32, transform._33);
}

static float3 rotate_x(float angle, float3 v)
static float3 rotate_around_axis(float3 axis, float angle, float3 v)
{
float c = cos(angle);
float s = sin(angle);
float t = 1.0f - c;

// standard x-axis rotation
return float3(
v.x,
c * v.y - s * v.z,
s * v.y + c * v.z
);
}

static float3 rotate_y(float angle, float3 v)
{
float c = cos(angle);
float s = sin(angle);
// normalize the axis to ensure proper rotation
axis = normalize(axis);

// apply standard y-axis rotation
return float3(
c * v.x + s * v.z,
v.y,
-s * v.x + c * v.z
// rodrigues' rotation formula
float3x3 rotation = float3x3(
t * axis.x * axis.x + c,
t * axis.x * axis.y - s * axis.z,
t * axis.x * axis.z + s * axis.y,

t * axis.x * axis.y + s * axis.z,
t * axis.y * axis.y + c,
t * axis.y * axis.z - s * axis.x,

t * axis.x * axis.z - s * axis.y,
t * axis.y * axis.z + s * axis.x,
t * axis.z * axis.z + c
);

return mul(rotation, v);
}

struct vertex_processing
Expand Down Expand Up @@ -248,6 +249,9 @@ struct vertex_processing
{
if (surface.is_grass_blade())
{
const float3 up = float3(0, 1, 0);
const float3 right = float3(1, 0, 0);

// give it a nice color gradient
float3 color_base = float3(0.05f, 0.2f, 0.01f); // darker green
float3 color_tip = float3(0.5f, 0.5f, 0.1f); // yellowish
Expand All @@ -258,21 +262,21 @@ struct vertex_processing
float t = (width_percent - 0.5f) * 2.0f; // map [0, 1] to [-1, 1]
float harsh_factor = t * t * t; // cubic function for sharper transition
float curve_angle = harsh_factor * (total_curvature / 2.0f);
input.normal = rotate_y(curve_angle, input.normal);
input.tangent = rotate_y(curve_angle, input.tangent);
input.normal = rotate_around_axis(up, curve_angle, input.normal);
input.tangent = rotate_around_axis(up, curve_angle, input.tangent);

// bend due to gravity
float random_lean = hash_uint(instance_id) * 1.0f;
float curve_amount_gravity = random_lean * height_percent;
input.position.xyz = rotate_x(curve_amount_gravity, input.position.xyz);
input.normal = rotate_x(curve_amount_gravity, input.normal);
input.tangent = rotate_x(curve_amount_gravity, input.tangent);
float random_lean = hash_uint(instance_id) * 1.0f;
curve_angle = random_lean * height_percent;
input.position.xyz = rotate_around_axis(right, curve_angle, input.position.xyz);
input.normal = rotate_around_axis(right, curve_angle, input.normal);
input.tangent = rotate_around_axis(right, curve_angle, input.tangent);

// bend due to wind
float curve_amount_wind = perlin_noise((float)buffer_frame.time * 1.0f) * 0.2f;
input.position.xyz = rotate_x(curve_amount_wind, input.position.xyz);
input.normal = rotate_x(curve_amount_wind, input.normal);
input.tangent = rotate_x(curve_amount_wind, input.tangent);
curve_angle = perlin_noise((float)buffer_frame.time * 1.0f) * 0.2f;
input.position.xyz = rotate_around_axis(right, curve_angle, input.position.xyz);
input.normal = rotate_around_axis(right, curve_angle, input.normal);
input.tangent = rotate_around_axis(right, curve_angle, input.tangent);
}
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ namespace spartan

// mood adjustment
m_default_light_directional->GetComponent<Light>()->SetFlag(LightFlags::Volumetric, false);
m_default_light_directional->SetRotation(Quaternion::FromEulerAngles(0.0f, 15.0f, 0.0f));
m_default_light_directional->SetRotation(Quaternion::FromEulerAngles(15.0f, 5.0f, 0.0f));
Renderer::SetOption(Renderer_Option::Grid, 0.0f);
Renderer::SetOption(Renderer_Option::GlobalIllumination, 0.0f); // in an open-world it offers little yet it costs the same

Expand Down

0 comments on commit efcd9d5

Please sign in to comment.