move repeat material into materials module

main
Jordan Orelli 1 year ago
parent 76f93c22ea
commit faee438747

@ -1,16 +1,8 @@
mod materials;
use bevy::{
pbr::{MaterialPipeline, MaterialPipelineKey},
prelude::*,
reflect::TypeUuid,
render::{
mesh::MeshVertexBufferLayout,
render_resource::{
AsBindGroup, RenderPipelineDescriptor, ShaderRef, SpecializedMeshPipelineError,
},
},
};
use bevy::prelude::*;
use materials::{RepeatMaterial, SolidMaterial};
fn startup(
mut commands: Commands,
@ -18,7 +10,7 @@ fn startup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut tiled_materials: ResMut<Assets<RepeatMaterial>>,
mut solid_materials: ResMut<Assets<materials::SolidMaterial>>,
mut solid_materials: ResMut<Assets<SolidMaterial>>,
) {
let grid_texture = assets.load("grid-texture.png");
@ -28,7 +20,7 @@ fn startup(
alpha_mode: AlphaMode::Blend,
});
let solid_material = solid_materials.add(materials::SolidMaterial{});
let solid_material = solid_materials.add(materials::SolidMaterial {});
// a ground plane
commands.spawn(MaterialMeshBundle {
@ -80,48 +72,6 @@ fn startup(
});
}
#[derive(AsBindGroup, Clone, TypeUuid)]
#[uuid = "d57ae039-8c67-4004-9370-5c522e256515"]
pub struct RepeatMaterial {
#[uniform(0)]
color: Color,
#[texture(1)]
#[sampler(2)]
color_texture: Option<Handle<Image>>,
alpha_mode: AlphaMode,
}
impl Material for RepeatMaterial {
fn vertex_shader() -> ShaderRef {
"shaders/repeat.vert".into()
}
fn fragment_shader() -> ShaderRef {
"shaders/repeat.frag".into()
}
fn alpha_mode(&self) -> AlphaMode {
self.alpha_mode
}
fn specialize(
_: &MaterialPipeline<Self>,
descriptor: &mut RenderPipelineDescriptor,
_: &MeshVertexBufferLayout,
_: MaterialPipelineKey<Self>,
) -> Result<(), SpecializedMeshPipelineError> {
descriptor.vertex.entry_point = "main".into();
let mut frag = descriptor
.fragment
.as_mut()
.expect("should have a fragment entry point here");
frag.entry_point = "main".into();
Ok(())
}
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)

@ -1,3 +1,6 @@
mod solid;
mod repeat;
pub use solid::SolidMaterial;
pub use repeat::RepeatMaterial;

@ -0,0 +1,55 @@
use bevy::{
asset::Handle,
pbr::{AlphaMode, Material, MaterialPipeline, MaterialPipelineKey},
reflect::TypeUuid,
render::{
color::Color,
mesh::MeshVertexBufferLayout,
render_resource::{
AsBindGroup, RenderPipelineDescriptor, ShaderRef, SpecializedMeshPipelineError,
},
texture::Image,
},
};
#[derive(AsBindGroup, Clone, TypeUuid)]
#[uuid = "d57ae039-8c67-4004-9370-5c522e256515"]
pub struct RepeatMaterial {
#[uniform(0)]
pub color: Color,
#[texture(1)]
#[sampler(2)]
pub color_texture: Option<Handle<Image>>,
pub alpha_mode: AlphaMode,
}
impl Material for RepeatMaterial {
fn vertex_shader() -> ShaderRef {
"shaders/repeat.vert".into()
}
fn fragment_shader() -> ShaderRef {
"shaders/repeat.frag".into()
}
fn alpha_mode(&self) -> AlphaMode {
self.alpha_mode
}
fn specialize(
_: &MaterialPipeline<Self>,
descriptor: &mut RenderPipelineDescriptor,
_: &MeshVertexBufferLayout,
_: MaterialPipelineKey<Self>,
) -> Result<(), SpecializedMeshPipelineError> {
descriptor.vertex.entry_point = "main".into();
let mut frag = descriptor
.fragment
.as_mut()
.expect("should have a fragment entry point here");
frag.entry_point = "main".into();
Ok(())
}
}
Loading…
Cancel
Save