Preproc

A simple C# like pre-processor for any source file.

Usage

With a build script you can pre-preprocess your shaders and output the needed variants

```rust // build.rs use std::{env, fs};

fn main() { let outdir = env::var("OUTDIR").unwrap(); let cratedir = env::var("CARGOMANIFEST_DIR").unwrap();

let mut pp = preproc::PP::default().search_path(crate_dir + "/shaders/include");

println!("cargo:rerun-if-changed=shaders/vertex_lit.wgsl");
fs::write(out_dir + "/unlit_vertex_color.wgsl", pp.parse_file(crate_dir + "/shaders/vertex_color.wgsl"));
fs::write(out_dir + "/lambert_vertex_color.wgsl", pp.define("LAMBERT").parse_file(crate_dir + "/shaders/vertex_color.wgsl"));

} ```

```wgsl // vertex_color.wgsl

include

include

@fragment fn fsmain(in: VertOut) -> @location(0) vec4 { return in.color * shading(in.normal, lightdir); } ```

```wgsl // shading.wgsl

if LAMBERT

fn shading(normal: vec3, lightdir: vec3) -> f32 { // lambert max(dot(normal, lightdir), 0.0) }

else

fn shading(normal: vec3, light_dir: vec3) -> f32 { // unlit 1.0 }

endif

```

outputs:

``wgsl // unlit_vertex_color.wgsl fn shading(normal: vec3<f32>, light_dir: vec3<f32>) -> f32 { // unlit 1.0 } //vert.wgsl` contents ...

@fragment fn fsmain(in: VertOut) -> @location(0) vec4 { return in.color * shading(in.normal, lightdir); } ``` and

``wgsl // lambert_vertex_color.wgsl fn shading(normal: vec3<f32>, light_dir: vec3<f32>) -> f32 { // lambert max(dot(normal, light_dir), 0.0) } //vert.wgsl` contents ...

@fragment fn fsmain(in: VertOut) -> @location(0) vec4 { return in.color * shading(in.normal, lightdir); } ```

Milestones