commit 848206676e741c9356e82718989aef6f01f93f42
parent fb35bee28d5c4798cede5817c569558d364ab959
Author: Christian Ermann <christianermann@gmail.com>
Date: Wed, 8 May 2024 20:19:57 -0400
Add normals to shaders
Diffstat:
1 file changed, 19 insertions(+), 4 deletions(-)
diff --git a/src/main.zig b/src/main.zig
@@ -568,6 +568,12 @@ const UnlitRenderPipeline = struct {
const vs =
\\ struct VertexInput {
\\ @location(0) position: vec4<f32>,
+ \\ @location(1) normal: vec3<f32>,
+ \\ };
+ \\
+ \\ struct VertexOutput {
+ \\ @builtin(position) clip_position: vec4<f32>,
+ \\ @location(0) normal: vec3<f32>,
\\ };
\\
\\ @group(0) @binding(0)
@@ -576,9 +582,12 @@ const UnlitRenderPipeline = struct {
\\ model_matrix: mat4x4<f32>,
\\ };
\\
- \\ @vertex fn main(in: VertexInput, @builtin(instance_index) idx: u32) -> @builtin(position) vec4<f32> {
+ \\ @vertex fn main(
+ \\ in: VertexInput,
+ \\ @builtin(instance_index) idx: u32,
+ \\ ) -> VertexOutput {
\\ let model_matrix = instances[idx].model_matrix;
- \\ return model_matrix * in.position;
+ \\ return VertexOutput(model_matrix * in.position, in.normal);
\\ }
;
const vs_module = app.device.createShaderModuleWGSL("default vertex shader", vs);
@@ -592,8 +601,14 @@ const UnlitRenderPipeline = struct {
});
const fs =
- \\ @fragment fn main() -> @location(0) vec4<f32> {
- \\ return vec4<f32>(1.0, 0.0, 0.0, 1.0);
+ \\ struct VertexOutput {
+ \\ @builtin(position) position: vec4<f32>,
+ \\ @location(0) normal: vec3<f32>,
+ \\ };
+ \\
+ \\ @fragment fn main(in: VertexOutput) -> @location(0) vec4<f32> {
+ \\ let color = (in.normal + 1.0) * 0.5;
+ \\ return vec4<f32>(color, 1.0);
\\ }
;
const fs_module = app.device.createShaderModuleWGSL("default fragment shader", fs);