forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example showing how to use SpecializedMeshPipeline (bevyengine#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]>
- Loading branch information
1 parent
20264d0
commit bfcb19a
Showing
4 changed files
with
418 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//! Very simple shader used to demonstrate how to get the world position and pass data | ||
//! between the vertex and fragment shader. Also shows the custom vertex layout. | ||
|
||
// First we import everything we need from bevy_pbr | ||
// A 2d shader would be vevry similar but import from bevy_sprite instead | ||
#import bevy_pbr::{ | ||
mesh_functions, | ||
view_transformations::position_world_to_clip | ||
} | ||
|
||
struct Vertex { | ||
// This is needed if you are using batching and/or gpu preprocessing | ||
// It's a built in so you don't need to define it in the vertex layout | ||
@builtin(instance_index) instance_index: u32, | ||
// Like we defined for the vertex layout | ||
// position is at location 0 | ||
@location(0) position: vec3<f32>, | ||
// and color at location 1 | ||
@location(1) color: vec4<f32>, | ||
}; | ||
|
||
// This is the output of the vertex shader and we also use it as the input for the fragment shader | ||
struct VertexOutput { | ||
@builtin(position) clip_position: vec4<f32>, | ||
@location(0) world_position: vec4<f32>, | ||
@location(1) color: vec3<f32>, | ||
}; | ||
|
||
@vertex | ||
fn vertex(vertex: Vertex) -> VertexOutput { | ||
var out: VertexOutput; | ||
// This is how bevy computes the world position | ||
// The vertex.instance_index is very important. Esepecially if you are using batching and gpu preprocessing | ||
var world_from_local = mesh_functions::get_world_from_local(vertex.instance_index); | ||
out.world_position = mesh_functions::mesh_position_local_to_world(world_from_local, vec4(vertex.position, 1.0)); | ||
out.clip_position = position_world_to_clip(out.world_position.xyz); | ||
|
||
// We just use the raw vertex color | ||
out.color = vertex.color.rgb; | ||
|
||
return out; | ||
} | ||
|
||
@fragment | ||
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> { | ||
// output the color directly | ||
return vec4(in.color, 1.0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.