#[derive(Debug, Clone)]
pub struct ShaderMaterial {
pub fragment: String,
pub vertex: Option<String>,
pub data: Vec<[f32; 4]>,
pub storage0: Vec<f32>,
pub storage1: Vec<f32>,
pub storage2: Vec<f32>,
pub textures: Vec<std::sync::Arc<wgpu::TextureView>>,
pub volumes: Vec<std::sync::Arc<wgpu::TextureView>>,
pub screen_space: bool,
pub opacity: f32,
pub transparent: bool,
pub side: u32,
pub tone_mapped: bool,
pub render_state: super::RenderState,
pub fallback_color: crate::math::Color,
pub alpha_to_coverage: bool,
}
impl Default for ShaderMaterial {
fn default() -> Self {
Self {
fragment: String::new(),
vertex: None,
data: Vec::new(),
storage0: Vec::new(),
storage1: Vec::new(),
storage2: Vec::new(),
textures: Vec::new(),
volumes: Vec::new(),
screen_space: false,
opacity: 1.0,
transparent: false,
side: 0,
tone_mapped: true,
render_state: super::RenderState::DEFAULT,
fallback_color: crate::math::Color::WHITE,
alpha_to_coverage: false,
}
}
}
impl ShaderMaterial {
pub fn new(fragment: impl Into<String>) -> Self {
Self {
fragment: fragment.into(),
..Default::default()
}
}
pub fn with_vertex(mut self, vertex: impl Into<String>) -> Self {
self.vertex = Some(vertex.into());
self
}
pub fn with_depth_write(mut self, write: bool) -> Self {
self.render_state.depth_write = Some(write);
self
}
pub const VALIDATION_AVAILABLE: bool = !cfg!(target_arch = "wasm32");
pub fn validate(&self, storage_buffers: bool) -> Result<(), String> {
for (coverage, samples) in [(false, 1), (self.alpha_to_coverage, 4)] {
let source = crate::renderer::custom_shader_module_source(
&self.fragment,
self.vertex.as_deref(),
storage_buffers,
coverage,
samples,
);
crate::renderer::validate_custom_shader(
&source,
crate::renderer::custom_preamble_lines(),
)?;
}
Ok(())
}
pub fn with_data(mut self, data: Vec<[f32; 4]>) -> Self {
self.data = data;
self
}
pub fn with_storage0(mut self, s: Vec<f32>) -> Self {
self.storage0 = s;
self
}
pub fn with_storage1(mut self, s: Vec<f32>) -> Self {
self.storage1 = s;
self
}
pub fn with_storage2(mut self, s: Vec<f32>) -> Self {
self.storage2 = s;
self
}
pub fn with_textures(mut self, t: Vec<std::sync::Arc<wgpu::TextureView>>) -> Self {
self.textures = t;
self
}
pub fn with_screen_space(mut self, on: bool) -> Self {
self.screen_space = on;
self
}
pub fn with_opacity(mut self, opacity: f32) -> Self {
self.opacity = opacity;
self
}
pub fn with_transparent(mut self, transparent: bool) -> Self {
self.transparent = transparent;
self
}
pub fn with_side(mut self, side: u32) -> Self {
self.side = side;
self
}
pub fn data_slots(&self) -> [[f32; 4]; 16] {
let mut out = [[0.0f32; 4]; 16];
for (i, v) in self.data.iter().take(16).enumerate() {
out[i] = *v;
}
out
}
}