aboutsummaryrefslogtreecommitdiff
path: root/src/shaders
diff options
context:
space:
mode:
authorvimene <vincent.menegaux@gmail.com>2025-12-29 21:21:09 +0100
committervimene <vincent.menegaux@gmail.com>2025-12-29 21:21:09 +0100
commit8ab4780f134c33a9e11ac0fe0bd866e17d3ee650 (patch)
treee59efd8a466312b790a2d68e679700b47e748c75 /src/shaders
parentdd187445972989dc44428e8cf185964da9e5c0c4 (diff)
downloadengine-8ab4780f134c33a9e11ac0fe0bd866e17d3ee650.tar.gz
starting to merge vulkan with the engine
- refactored scene_main to be able to share code between software and hardware renderers, while still sharing code between terminal software renderer and graphical software renderer - made the hardware renderer use scene objects vertices and transforms, instead of hardcoded ones - made the hardware renderer use the scene's camera - made .obj parser create Vector3 instead of Vector4, which didn't made sense - removed unnecessary std:: - lots of small changes
Diffstat (limited to 'src/shaders')
-rw-r--r--src/shaders/shader.slang27
1 files changed, 19 insertions, 8 deletions
diff --git a/src/shaders/shader.slang b/src/shaders/shader.slang
index 1f055c1..5598e1e 100644
--- a/src/shaders/shader.slang
+++ b/src/shaders/shader.slang
@@ -1,28 +1,28 @@
struct VertexInput {
float3 pos;
- float3 col;
- float2 uv;
+ float3 normal;
};
struct UniformBuffer {
float4x4 model, view, proj;
+ float4x4 camera_rot;
+ float3 camera_loc;
};
ConstantBuffer<UniformBuffer> ubo;
struct VertexOutput {
float4 pos : SV_Position;
- float3 col;
- float2 uv;
+ float3 world_loc;
+ float3 normal;
};
[shader("vertex")]
VertexOutput vert_main(VertexInput vi) {
VertexOutput vo;
- // vo.pos = float4(vi.pos, 0., 1.);
vo.pos = mul(ubo.proj, mul(ubo.view, mul(ubo.model, float4(vi.pos, 1.))));
- vo.col = vi.col;
- vo.uv = vi.uv;
+ vo.world_loc = vi.pos;
+ vo.normal = vi.normal;
return vo;
}
@@ -30,5 +30,16 @@ Sampler2D texture;
[shader("fragment")]
float4 frag_main(VertexOutput vo) : SV_Target {
- return texture.Sample(vo.uv);
+ float3 u = vo.world_loc - ubo.camera_loc;
+ float u_len_sq = dot(u, u);
+ u = normalize(u);
+ float attenuation = dot(mul(ubo.camera_rot, float4(0., 0., -1., 0.)).xyz, u);
+ if (attenuation < .7) attenuation = 0.f;
+ else if (attenuation > .8) attenuation = 1.;
+ else attenuation = (attenuation - .7) / .1;
+ float light = -dot(vo.normal, u) / u_len_sq;
+ if (light < 0.) light = 0.;
+ float final_light = .05 + light * attenuation * .8 * .95;
+ if (final_light > 1.) final_light = 1.;
+ return float4(final_light, final_light, final_light, 1.);
}