Skip to content

Instantly share code, notes, and snippets.

@StarArawn
Last active March 27, 2021 15:11
Show Gist options
  • Save StarArawn/f4da80e06dfcefda90fde11d7f2d2c5d to your computer and use it in GitHub Desktop.
Save StarArawn/f4da80e06dfcefda90fde11d7f2d2c5d to your computer and use it in GitHub Desktop.
Bevy Render Resources temp docs
// In bevy there are the following macros/derive traits:
// RenderResources, RenderResource, render_resources
// These allow you to link data from bevy to your shader directly.
//
// Struct data is linked to the shader via a naming convention.
//
// render_resources allows you to specify some specifics:
// from_self - Specifies that the following struct is a single binding.
// buffer - Specifies the following field is a uniform Buffer
// ignore - Ignores the following field and wont be sent to the GPU
// You'll also need to initallize the RenderResource using:
AssetRenderResourcesNode::<MyMaterial>::new(true),
// Note the true here represents the fact that you have dynamic bindings.
// If you have dynamic bindings you'll need to tell the pipeline using the PipelineSpecialization
// Example 1 - A single struct that is passed into a single binding
/* glsl
layout(set = 0, binding = 0) uniform MyMaterial {
vec4 albedo;
float metallic;
float roughness;
};
*/
// Bevy uniform struct:
#[derive(RenderResource, RenderResources)]
#[render_resources(from_self)]
struct MyMaterial {
albedo: Color,
metallic: f32,
roughness: f32,
}
unsafe impl Byteable for MyMaterial {}
// Example 2 - A struct that is passed into multiple bindings
/* glsl
// Note that the first uniform here is named: "MyMateiral_albedo" which matches the struct as in: "StructName_fieldname"
// When using from_self the struct name is only used.
layout(set = 0, binding = 0) uniform MyMaterial_albedo {
vec4 Albedo;
};
// Here we create our texture uniforms. Bevy will auto matically link two for a texture. The naming convention is as follows:
// "StructName_fieldname_texture" and "StructName_fieldname_sampler".
layout(set = 3, binding = 1) uniform texture2D MyMaterial_albedo_texture;
layout(set = 3, binding = 2) uniform sampler MyMaterial_albedo_texture_sampler;
*/
// Bevy uniform struct:
// Note the lack of from_self here as we are actually creating multiple "bindings".
#[derive(RenderResources)]
struct MyMaterial {
albedo: Color,
albedo_texture: Option<Handle<Texture>>,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment