Skip to content

Commit bfcb19a

Browse files
Add example showing how to use SpecializedMeshPipeline (#14370)
# Objective - A lot of mid-level rendering apis are hard to figure out because they don't have any examples - SpecializedMeshPipeline can be really useful in some cases when you want more flexibility than a Material without having to go to low level apis. ## Solution - Add an example showing how to make a custom `SpecializedMeshPipeline`. ## Testing - Did you test these changes? If so, how? - Are there any parts that need more testing? - How can other people (reviewers) test your changes? Is there anything specific they need to know? - If relevant, what platforms did you test these changes on, and are there any important ones you can't test? --- ## Showcase The examples just spawns 3 triangles in a triangle pattern. ![image](https://github.com/user-attachments/assets/c3098758-94c4-4775-95e5-1d7c7fb9eb86) --------- Co-authored-by: Alice Cecile <[email protected]>
1 parent 20264d0 commit bfcb19a

File tree

4 files changed

+418
-0
lines changed

4 files changed

+418
-0
lines changed

Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -2394,6 +2394,17 @@ description = "A shader that shows how to bind and sample multiple textures as a
23942394
category = "Shaders"
23952395
wasm = false
23962396

2397+
[[example]]
2398+
name = "specialized_mesh_pipeline"
2399+
path = "examples/shader/specialized_mesh_pipeline.rs"
2400+
doc-scrape-examples = true
2401+
2402+
[package.metadata.example.specialized_mesh_pipeline]
2403+
name = "Specialized Mesh Pipeline"
2404+
description = "Demonstrates how to write a specialized mesh pipeline"
2405+
category = "Shaders"
2406+
wasm = true
2407+
23972408
# Stress tests
23982409
[[package.metadata.example_category]]
23992410
name = "Stress Tests"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//! Very simple shader used to demonstrate how to get the world position and pass data
2+
//! between the vertex and fragment shader. Also shows the custom vertex layout.
3+
4+
// First we import everything we need from bevy_pbr
5+
// A 2d shader would be vevry similar but import from bevy_sprite instead
6+
#import bevy_pbr::{
7+
mesh_functions,
8+
view_transformations::position_world_to_clip
9+
}
10+
11+
struct Vertex {
12+
// This is needed if you are using batching and/or gpu preprocessing
13+
// It's a built in so you don't need to define it in the vertex layout
14+
@builtin(instance_index) instance_index: u32,
15+
// Like we defined for the vertex layout
16+
// position is at location 0
17+
@location(0) position: vec3<f32>,
18+
// and color at location 1
19+
@location(1) color: vec4<f32>,
20+
};
21+
22+
// This is the output of the vertex shader and we also use it as the input for the fragment shader
23+
struct VertexOutput {
24+
@builtin(position) clip_position: vec4<f32>,
25+
@location(0) world_position: vec4<f32>,
26+
@location(1) color: vec3<f32>,
27+
};
28+
29+
@vertex
30+
fn vertex(vertex: Vertex) -> VertexOutput {
31+
var out: VertexOutput;
32+
// This is how bevy computes the world position
33+
// The vertex.instance_index is very important. Esepecially if you are using batching and gpu preprocessing
34+
var world_from_local = mesh_functions::get_world_from_local(vertex.instance_index);
35+
out.world_position = mesh_functions::mesh_position_local_to_world(world_from_local, vec4(vertex.position, 1.0));
36+
out.clip_position = position_world_to_clip(out.world_position.xyz);
37+
38+
// We just use the raw vertex color
39+
out.color = vertex.color.rgb;
40+
41+
return out;
42+
}
43+
44+
@fragment
45+
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
46+
// output the color directly
47+
return vec4(in.color, 1.0);
48+
}

examples/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ Example | Description
392392
[Material Prepass](../examples/shader/shader_prepass.rs) | A shader that uses the various textures generated by the prepass
393393
[Post Processing - Custom Render Pass](../examples/shader/custom_post_processing.rs) | A custom post processing effect, using a custom render pass that runs after the main pass
394394
[Shader Defs](../examples/shader/shader_defs.rs) | A shader that uses "shaders defs" (a bevy tool to selectively toggle parts of a shader)
395+
[Specialized Mesh Pipeline](../examples/shader/specialized_mesh_pipeline.rs) | Demonstrates how to write a specialized mesh pipeline
395396
[Texture Binding Array (Bindless Textures)](../examples/shader/texture_binding_array.rs) | A shader that shows how to bind and sample multiple textures as a binding array (a.k.a. bindless textures).
396397

397398
## State

0 commit comments

Comments
 (0)