Skip to content

Commit 813ecce

Browse files
committed
Add texturecubemap rust shaders
1 parent 2083c5f commit 813ecce

11 files changed

Lines changed: 139 additions & 1 deletion

File tree

shaders/rust/Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

shaders/rust/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ members = [
6262
"instancing/planet",
6363
"instancing/starfield",
6464
"texture3d",
65+
"texturecubemap/reflect",
66+
"texturecubemap/skybox",
6567
]
6668

6769
[workspace.dependencies]

shaders/rust/compileshaders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def compile_shader(shader_dir):
6868
print(f" Created {final_path.name} (entry point: {entry_point})")
6969

7070
# Special case: move to parent directory for nested shader structures
71-
if shader_dir.parent.name in ["base", "descriptorsets", "dynamicuniformbuffer", "multisampling", "pipelines", "specializationconstants", "computeshader", "texturearray", "screenshot", "negativeviewportheight", "stencilbuffer", "parallaxmapping", "computecloth", "ssao", "shadowmapping", "deferred", "computenbody", "bloom", "hdr", "radialblur", "pbribl", "indirectdraw", "instancing"]:
71+
if shader_dir.parent.name in ["base", "descriptorsets", "dynamicuniformbuffer", "multisampling", "pipelines", "specializationconstants", "computeshader", "texturearray", "screenshot", "negativeviewportheight", "stencilbuffer", "parallaxmapping", "computecloth", "ssao", "shadowmapping", "deferred", "computenbody", "bloom", "hdr", "radialblur", "pbribl", "indirectdraw", "instancing", "texturecubemap"]:
7272
parent_dir = shader_dir.parent
7373
parent_final_path = parent_dir / f"{shader_name}.{shader_type}.spv"
7474
shutil.move(str(final_path), str(parent_final_path))
3.93 KB
Binary file not shown.
5.41 KB
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "reflect"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["dylib"]
8+
9+
[dependencies]
10+
spirv-std = { workspace = true }
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#![no_std]
2+
3+
use spirv_std::spirv;
4+
use spirv_std::{glam::{vec3, vec4, Mat3, Mat4, Vec3, Vec4}, image::{Cubemap, SampledImage}};
5+
use spirv_std::num_traits::Float;
6+
7+
#[repr(C)]
8+
pub struct UBO {
9+
pub projection: Mat4,
10+
pub model: Mat4,
11+
pub inv_model: Mat4,
12+
pub lod_bias: f32,
13+
}
14+
15+
#[spirv(vertex)]
16+
pub fn main_vs(
17+
#[spirv(vertex_index)] _vertex_index: i32,
18+
in_pos: Vec3,
19+
in_normal: Vec3,
20+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &UBO,
21+
#[spirv(position)] out_pos: &mut Vec4,
22+
out_world_pos: &mut Vec3,
23+
out_normal: &mut Vec3,
24+
out_view_vec: &mut Vec3,
25+
out_light_vec: &mut Vec3,
26+
) {
27+
*out_pos = ubo.projection * ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);
28+
29+
*out_world_pos = (ubo.model * vec4(in_pos.x, in_pos.y, in_pos.z, 1.0)).truncate();
30+
*out_normal = Mat3::from_mat4(ubo.model) * in_normal;
31+
32+
let light_pos = vec3(0.0, -5.0, 5.0);
33+
*out_light_vec = light_pos - *out_world_pos;
34+
*out_view_vec = -*out_world_pos;
35+
}
36+
37+
#[spirv(fragment)]
38+
pub fn main_fs(
39+
in_pos: Vec3,
40+
in_normal: Vec3,
41+
in_view_vec: Vec3,
42+
in_light_vec: Vec3,
43+
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &UBO,
44+
#[spirv(descriptor_set = 0, binding = 1)] sampler_color: &SampledImage<Cubemap>,
45+
out_frag_color: &mut Vec4,
46+
) {
47+
let ci = in_pos.normalize();
48+
let cr = ci.reflect(in_normal.normalize());
49+
50+
let mut cr_transformed = (ubo.inv_model * vec4(cr.x, cr.y, cr.z, 0.0)).truncate();
51+
cr_transformed.x *= -1.0;
52+
cr_transformed.y *= -1.0;
53+
54+
let color = sampler_color.sample_by_lod(cr_transformed, ubo.lod_bias);
55+
56+
let n = in_normal.normalize();
57+
let l = in_light_vec.normalize();
58+
let v = in_view_vec.normalize();
59+
let r = (-l).reflect(n);
60+
61+
let ambient = vec3(0.5, 0.5, 0.5) * color.truncate();
62+
let diffuse = n.dot(l).max(0.0) * vec3(1.0, 1.0, 1.0);
63+
let specular = r.dot(v).max(0.0).powf(16.0) * vec3(0.5, 0.5, 0.5);
64+
65+
let final_color = ambient + diffuse * color.truncate() + specular;
66+
*out_frag_color = vec4(final_color.x, final_color.y, final_color.z, 1.0);
67+
}
464 Bytes
Binary file not shown.
3.34 KB
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "texturecubemap-skybox"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["dylib"]
8+
9+
[dependencies]
10+
spirv-std = { workspace = true }

0 commit comments

Comments
 (0)