|
| 1 | +//! `FieldColorMaterial` — unlit material that reads a per-particle color from a |
| 2 | +//! storage buffer indexed by the per-instance tag (set to slot index by the pack pass). |
| 3 | +
|
| 4 | +use std::ops::Deref; |
| 5 | + |
| 6 | +use bevy::asset::embedded_asset; |
| 7 | +use bevy::pbr::{ExtendedMaterial, MaterialExtension, MaterialPlugin}; |
| 8 | +use bevy::prelude::*; |
| 9 | +use bevy::render::{render_resource::AsBindGroup, storage::ShaderBuffer}; |
| 10 | +use bevy::shader::ShaderRef; |
| 11 | + |
| 12 | +use crate::render::material::UntypedMaterial; |
| 13 | + |
| 14 | +pub struct FieldColorMaterialPlugin; |
| 15 | + |
| 16 | +impl Plugin for FieldColorMaterialPlugin { |
| 17 | + fn build(&self, app: &mut App) { |
| 18 | + embedded_asset!(app, "field_color.wgsl"); |
| 19 | + embedded_asset!(app, "field_pbr.wgsl"); |
| 20 | + app.add_plugins(MaterialPlugin::<FieldColorMaterial>::default()); |
| 21 | + app.add_plugins(MaterialPlugin::<FieldPbrMaterial>::default()); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +#[derive(Asset, AsBindGroup, Reflect, Debug, Clone)] |
| 26 | +pub struct FieldColorMaterial { |
| 27 | + #[storage(0, read_only)] |
| 28 | + pub colors: Handle<ShaderBuffer>, |
| 29 | +} |
| 30 | + |
| 31 | +impl Material for FieldColorMaterial { |
| 32 | + fn vertex_shader() -> ShaderRef { |
| 33 | + "embedded://processing_render/field/field_color.wgsl".into() |
| 34 | + } |
| 35 | + |
| 36 | + fn fragment_shader() -> ShaderRef { |
| 37 | + "embedded://processing_render/field/field_color.wgsl".into() |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +#[derive(Component, Clone)] |
| 42 | +pub struct FieldColorMaterial3d(pub Handle<FieldColorMaterial>); |
| 43 | + |
| 44 | +impl bevy::asset::AsAssetId for FieldColorMaterial3d { |
| 45 | + type Asset = FieldColorMaterial; |
| 46 | + fn as_asset_id(&self) -> AssetId<Self::Asset> { |
| 47 | + self.0.id() |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/// Sibling to `add_processing_materials` / `add_custom_materials`. Promotes |
| 52 | +/// `UntypedMaterial(handle)` entities whose handle is a [`FieldColorMaterial`] |
| 53 | +/// to having the typed `MeshMaterial3d<FieldColorMaterial>` component required |
| 54 | +/// by the render pipeline. |
| 55 | +pub fn add_field_color_materials( |
| 56 | + mut commands: Commands, |
| 57 | + meshes: Query<(Entity, &UntypedMaterial)>, |
| 58 | +) { |
| 59 | + for (entity, handle) in meshes.iter() { |
| 60 | + let handle = handle.deref().clone(); |
| 61 | + if let Ok(handle) = handle.try_typed::<FieldColorMaterial>() { |
| 62 | + commands |
| 63 | + .entity(entity) |
| 64 | + .insert(MeshMaterial3d::<FieldColorMaterial>(handle)); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// PBR-lit per-particle color material. Wraps `StandardMaterial` via |
| 70 | +/// `ExtendedMaterial` so the user gets standard PBR lighting behavior on top |
| 71 | +/// of per-particle albedo from a storage buffer. |
| 72 | +pub type FieldPbrMaterial = ExtendedMaterial<StandardMaterial, FieldPbrExtension>; |
| 73 | + |
| 74 | +#[derive(Asset, AsBindGroup, Reflect, Debug, Clone)] |
| 75 | +pub struct FieldPbrExtension { |
| 76 | + #[storage(100, read_only)] |
| 77 | + pub colors: Handle<ShaderBuffer>, |
| 78 | +} |
| 79 | + |
| 80 | +impl MaterialExtension for FieldPbrExtension { |
| 81 | + fn fragment_shader() -> ShaderRef { |
| 82 | + "embedded://processing_render/field/field_pbr.wgsl".into() |
| 83 | + } |
| 84 | + |
| 85 | + fn deferred_fragment_shader() -> ShaderRef { |
| 86 | + "embedded://processing_render/field/field_pbr.wgsl".into() |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +pub fn add_field_pbr_materials( |
| 91 | + mut commands: Commands, |
| 92 | + meshes: Query<(Entity, &UntypedMaterial)>, |
| 93 | +) { |
| 94 | + for (entity, handle) in meshes.iter() { |
| 95 | + let handle = handle.deref().clone(); |
| 96 | + if let Ok(handle) = handle.try_typed::<FieldPbrMaterial>() { |
| 97 | + commands |
| 98 | + .entity(entity) |
| 99 | + .insert(MeshMaterial3d::<FieldPbrMaterial>(handle)); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments