summaryrefslogtreecommitdiff
path: root/engine
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2026-03-27 16:14:45 +0100
committerHampusM <hampus@hampusmat.com>2026-03-27 16:14:45 +0100
commitee33567cae433707a57856c148cff9d53b8d19e3 (patch)
tree0cb7cc77a26a8aa5a5d2f27460cd7280e2810a8f /engine
parent5dbab7fce76997e800491b54ebeca612ac783094 (diff)
style(engine): improve formatting of default shader
Diffstat (limited to 'engine')
-rw-r--r--engine/res/default_shader.slang104
1 files changed, 44 insertions, 60 deletions
diff --git a/engine/res/default_shader.slang b/engine/res/default_shader.slang
index db318c8..04e897e 100644
--- a/engine/res/default_shader.slang
+++ b/engine/res/default_shader.slang
@@ -1,14 +1,10 @@
#define MAX_LIGHT_CNT 64
-
struct Material
{
float3 ambient;
float3 diffuse;
float3 specular;
- // Sampler2D ambient_map;
- // Sampler2D diffuse_map;
- // Sampler2D specular_map;
float shininess;
};
@@ -57,8 +53,7 @@ struct BlinnPhongLighting
float4 evaluate(in VertexData vertex_data)
{
- float3 ambient_light =
- this.calc_ambient_light(vertex_data.texture_coords);
+ float3 ambient_light = this.calc_ambient_light(vertex_data.texture_coords);
float3 directional_light_sum = this.calc_dir_light_sum(vertex_data);
float3 point_light_sum = this.calc_point_light_sum(vertex_data);
@@ -70,16 +65,17 @@ struct BlinnPhongLighting
{
float3 directional_light_sum = float3(0.0, 0.0, 0.0);
- for (uint index = 0; index < this.directional_light_cnt; index++)
- {
+ for (uint index = 0; index < this.directional_light_cnt; index++) {
CalculatedLight calculated_dir_light;
this.calc_light(
- // Negated since we want the light to point from the light direction
+ // Negated since we want the light to point from the light
+ // direction
normalize(-this.directional_lights[index].direction),
this.directional_lights[index].phong,
vertex_data,
- calculated_dir_light);
+ calculated_dir_light
+ );
directional_light_sum +=
calculated_dir_light.diffuse + calculated_dir_light.specular;
@@ -92,10 +88,10 @@ struct BlinnPhongLighting
{
float3 point_light_sum = float3(0.0, 0.0, 0.0);
- for (uint index = 0; index < this.point_light_cnt; index++)
- {
- float3 light_direction =
- normalize(this.point_lights[index].position - vertex_data.world_space_pos);
+ for (uint index = 0; index < this.point_light_cnt; index++) {
+ float3 light_direction = normalize(
+ this.point_lights[index].position - vertex_data.world_space_pos
+ );
CalculatedLight calculated_point_light;
@@ -103,10 +99,12 @@ struct BlinnPhongLighting
light_direction,
this.point_lights[index].phong,
vertex_data,
- calculated_point_light);
+ calculated_point_light
+ );
- float attenuation =
- this.calc_attenuation(this.point_lights[index], vertex_data.world_space_pos);
+ float attenuation = this.calc_attenuation(
+ this.point_lights[index], vertex_data.world_space_pos
+ );
calculated_point_light.diffuse *= attenuation;
calculated_point_light.specular *= attenuation;
@@ -122,40 +120,40 @@ struct BlinnPhongLighting
in float3 light_direction,
in LightPhong light_phong,
in VertexData vertex_data,
- out CalculatedLight calculated_light)
+ out CalculatedLight calculated_light
+ )
{
float3 norm = normalize(vertex_data.world_space_normal);
calculated_light.diffuse = this.calc_diffuse_light(
- light_phong,
- light_direction,
- norm,
- vertex_data.texture_coords);
+ light_phong, light_direction, norm, vertex_data.texture_coords
+ );
calculated_light.specular = this.calc_specular_light(
light_phong,
light_direction,
norm,
vertex_data.world_space_pos,
- vertex_data.texture_coords);
+ vertex_data.texture_coords
+ );
}
float3 calc_ambient_light(in float2 texture_coords)
{
return ambient_map.Sample(texture_coords).xyz * this.material.ambient;
- // return this.material.ambient_map.Sample(texture_coords).xyz * this.material.ambient;
}
float3 calc_diffuse_light(
in LightPhong light_phong,
in float3 light_dir,
in float3 norm,
- in float2 texture_coords)
+ in float2 texture_coords
+ )
{
float diff = max(dot(norm, light_dir), 0.0);
- return light_phong.diffuse * (diff * (diffuse_map.Sample(texture_coords).xyz * this.material.diffuse));
- // return light_phong.diffuse * (diff * (this.material.diffuse_map.Sample(texture_coords).xyz * this.material.diffuse));
+ return light_phong.diffuse *
+ (diff * (diffuse_map.Sample(texture_coords).xyz * this.material.diffuse));
}
float3 calc_specular_light(
@@ -163,24 +161,26 @@ struct BlinnPhongLighting
in float3 light_dir,
in float3 norm,
in float3 frag_pos,
- in float2 texture_coords)
+ in float2 texture_coords
+ )
{
float3 view_direction = normalize(this.view_pos - frag_pos);
float3 halfway_direction = normalize(light_dir + view_direction);
- float spec =
- pow(max(dot(norm, halfway_direction), 0.0), this.material.shininess);
+ float spec = pow(max(dot(norm, halfway_direction), 0.0), this.material.shininess);
- return light_phong.specular * (spec * (specular_map.Sample(texture_coords).xyz * this.material.specular));
- // return light_phong.specular * (spec * (this.material.specular_map.Sample(texture_coords).xyz * this.material.specular));
+ return light_phong.specular * (spec * (specular_map.Sample(texture_coords).xyz *
+ this.material.specular));
}
float calc_attenuation(in PointLight point_light, in float3 position)
{
float light_distance = length(point_light.position - position);
- return 1.0 / (point_light.attenuation_props.constant + point_light.attenuation_props.linear * light_distance + point_light.attenuation_props.quadratic * pow(light_distance, 2));
+ return 1.0 / (point_light.attenuation_props.constant +
+ point_light.attenuation_props.linear * light_distance +
+ point_light.attenuation_props.quadratic * pow(light_distance, 2));
}
};
@@ -192,24 +192,16 @@ struct Model3D
float4x4 projection;
}
-// ParameterBlock<BlinnPhongLighting> blinn_phong_lighting;
-
-// ConstantBuffer<BlinnPhongLighting> blinn_phong_lighting;
-
-// ConstantBuffer<Model3D> model_3d;
-
-// ParameterBlock<Model3D> model_3d;
-
-cbuffer Uniforms {
- Model3D model_3d;
- BlinnPhongLighting lighting;
+cbuffer Uniforms
+{
+ Model3D model_3d;
+ BlinnPhongLighting lighting;
}
Sampler2D ambient_map;
Sampler2D diffuse_map;
Sampler2D specular_map;
-
struct VertexData
{
float2 texture_coords;
@@ -236,23 +228,20 @@ struct Fragment
};
[shader("vertex")]
-VertexStageOutput vertex_main(
- Vertex vertex: VERTEX,
-)
+VertexStageOutput vertex_main(Vertex vertex: VERTEX)
{
VertexStageOutput stage_output;
- // TODO: Investigate why mul arguments need to be ordered this way.
- // The mul arguments are reordered in the GLSL output
+ // TODO: Investigate why mul arguments need to be ordered this way.
+ // The mul arguments are reordered in the GLSL output
- // float4x4 proj_view = mul(model_3d.projection, model_3d.view);
- float4x4 proj_view = mul(model_3d.view, model_3d.projection);
+ // float4x4 proj_view = mul(model_3d.projection, model_3d.view);
+ float4x4 proj_view = mul(model_3d.view, model_3d.projection);
// float4x4 proj_view_model =
// mul(proj_view, model_3d.model);
- float4x4 proj_view_model =
- mul(model_3d.model, proj_view);
+ float4x4 proj_view_model = mul(model_3d.model, proj_view);
// stage_output.sv_position = mul(proj_view_model, float4(vertex.pos, 1.0));
@@ -272,17 +261,12 @@ VertexStageOutput vertex_main(
}
[shader("fragment")]
-Fragment fragment_main(
- VertexData vertex_data: VertexData,
- // uniform ConstantBuffer<BlinnPhongLighting> lighting
-) : SV_Target
+Fragment fragment_main(VertexData vertex_data: VertexData) : SV_Target
{
Fragment fragment;
fragment.color = lighting.evaluate(vertex_data);
- // fragment.color = float4(1.0, 1.0, 1.0, 1.0);
-
return fragment;
}