78 lines
17 KiB
HTML
78 lines
17 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Materials are used alongside `Material2dPlugin`, `Mesh2d`, and `MeshMaterial2d` to spawn entities that are rendered with a specific `Material2d` type. They serve as an easy to use high level way to render `Mesh2d` entities with custom shader logic."><title>Material2d in bevy_sprite - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-42caa33d.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="bevy_sprite" data-themes="" data-resource-suffix="" data-rustdoc-version="1.84.0 (9fc6b4312 2025-01-07)" data-channel="1.84.0" data-search-js="search-92e6798f.js" data-settings-js="settings-0f613d39.js" ><script src="../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-893ab5e7.css"></noscript><link rel="icon" href="https://bevyengine.org/assets/icon.png"></head><body class="rustdoc trait"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button><a class="logo-container" href="../bevy_sprite/index.html"><img src="https://bevyengine.org/assets/icon.png" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../bevy_sprite/index.html"><img src="https://bevyengine.org/assets/icon.png" alt="logo"></a><h2><a href="../bevy_sprite/index.html">bevy_<wbr>sprite</a><span class="version">0.15.1</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Material2d</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#example" title="Example">Example</a></li></ul><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.alpha_mode" title="alpha_mode">alpha_mode</a></li><li><a href="#method.depth_bias" title="depth_bias">depth_bias</a></li><li><a href="#method.fragment_shader" title="fragment_shader">fragment_shader</a></li><li><a href="#method.specialize" title="specialize">specialize</a></li><li><a href="#method.vertex_shader" title="vertex_shader">vertex_shader</a></li></ul><h3><a href="#dyn-compatibility">Dyn Compatibility</a></h3><h3><a href="#implementors">Implementors</a></h3></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate bevy_<wbr>sprite</a></h2></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><span class="rustdoc-breadcrumbs"><a href="index.html">bevy_sprite</a></span><h1>Trait <span class="trait">Material2d</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/bevy_sprite/mesh2d/material.rs.html#118-151">Source</a> </span></div><pre class="rust item-decl"><code>pub trait Material2d:
|
||
<a class="trait" href="../bevy_render/render_resource/bind_group/trait.AsBindGroup.html" title="trait bevy_render::render_resource::bind_group::AsBindGroup">AsBindGroup</a>
|
||
+ <a class="trait" href="../bevy_asset/trait.Asset.html" title="trait bevy_asset::Asset">Asset</a>
|
||
+ <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>
|
||
+ <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> {
|
||
// Provided methods
|
||
fn <a href="#method.vertex_shader" class="fn">vertex_shader</a>() -> <a class="enum" href="../bevy_render/render_resource/shader/enum.ShaderRef.html" title="enum bevy_render::render_resource::shader::ShaderRef">ShaderRef</a> { ... }
|
||
<span class="item-spacer"></span> fn <a href="#method.fragment_shader" class="fn">fragment_shader</a>() -> <a class="enum" href="../bevy_render/render_resource/shader/enum.ShaderRef.html" title="enum bevy_render::render_resource::shader::ShaderRef">ShaderRef</a> { ... }
|
||
<span class="item-spacer"></span> fn <a href="#method.depth_bias" class="fn">depth_bias</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.f32.html">f32</a> { ... }
|
||
<span class="item-spacer"></span> fn <a href="#method.alpha_mode" class="fn">alpha_mode</a>(&self) -> <a class="enum" href="enum.AlphaMode2d.html" title="enum bevy_sprite::AlphaMode2d">AlphaMode2d</a> { ... }
|
||
<span class="item-spacer"></span> fn <a href="#method.specialize" class="fn">specialize</a>(
|
||
descriptor: &mut <a class="struct" href="../bevy_render/render_resource/pipeline/struct.RenderPipelineDescriptor.html" title="struct bevy_render::render_resource::pipeline::RenderPipelineDescriptor">RenderPipelineDescriptor</a>,
|
||
layout: &<a class="struct" href="../bevy_mesh/vertex/struct.MeshVertexBufferLayoutRef.html" title="struct bevy_mesh::vertex::MeshVertexBufferLayoutRef">MeshVertexBufferLayoutRef</a>,
|
||
key: <a class="struct" href="struct.Material2dKey.html" title="struct bevy_sprite::Material2dKey">Material2dKey</a><Self>,
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/1.84.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.unit.html">()</a>, <a class="enum" href="../bevy_render/render_resource/pipeline_specializer/enum.SpecializedMeshPipelineError.html" title="enum bevy_render::render_resource::pipeline_specializer::SpecializedMeshPipelineError">SpecializedMeshPipelineError</a>> { ... }
|
||
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Materials are used alongside <a href="struct.Material2dPlugin.html" title="struct bevy_sprite::Material2dPlugin"><code>Material2dPlugin</code></a>, <a href="../bevy_render/mesh/components/struct.Mesh2d.html" title="struct bevy_render::mesh::components::Mesh2d"><code>Mesh2d</code></a>, and <a href="struct.MeshMaterial2d.html" title="struct bevy_sprite::MeshMaterial2d"><code>MeshMaterial2d</code></a>
|
||
to spawn entities that are rendered with a specific <a href="trait.Material2d.html" title="trait bevy_sprite::Material2d"><code>Material2d</code></a> type. They serve as an easy to use high level
|
||
way to render <a href="../bevy_render/mesh/components/struct.Mesh2d.html" title="struct bevy_render::mesh::components::Mesh2d"><code>Mesh2d</code></a> entities with custom shader logic.</p>
|
||
<p>Materials must implement <a href="../bevy_render/render_resource/bind_group/trait.AsBindGroup.html" title="trait bevy_render::render_resource::bind_group::AsBindGroup"><code>AsBindGroup</code></a> to define how data will be transferred to the GPU and bound in shaders.
|
||
<a href="../bevy_render/render_resource/bind_group/trait.AsBindGroup.html" title="trait bevy_render::render_resource::bind_group::AsBindGroup"><code>AsBindGroup</code></a> can be derived, which makes generating bindings straightforward. See the <a href="../bevy_render/render_resource/bind_group/trait.AsBindGroup.html" title="trait bevy_render::render_resource::bind_group::AsBindGroup"><code>AsBindGroup</code></a> docs for details.</p>
|
||
<h2 id="example"><a class="doc-anchor" href="#example">§</a>Example</h2>
|
||
<p>Here is a simple <a href="trait.Material2d.html" title="trait bevy_sprite::Material2d"><code>Material2d</code></a> implementation. The <a href="../bevy_render/render_resource/bind_group/trait.AsBindGroup.html" title="trait bevy_render::render_resource::bind_group::AsBindGroup"><code>AsBindGroup</code></a> derive has many features. To see what else is available,
|
||
check out the <a href="../bevy_render/render_resource/bind_group/trait.AsBindGroup.html" title="trait bevy_render::render_resource::bind_group::AsBindGroup"><code>AsBindGroup</code></a> documentation.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(AsBindGroup, Debug, Clone, Asset, TypePath)]
|
||
</span><span class="kw">pub struct </span>CustomMaterial {
|
||
<span class="comment">// Uniform bindings must implement `ShaderType`, which will be used to convert the value to
|
||
// its shader-compatible equivalent. Most core math types already implement `ShaderType`.
|
||
</span><span class="attr">#[uniform(<span class="number">0</span>)]
|
||
</span>color: LinearRgba,
|
||
<span class="comment">// Images can be bound as textures in shaders. If the Image's sampler is also needed, just
|
||
// add the sampler attribute with a different binding index.
|
||
</span><span class="attr">#[texture(<span class="number">1</span>)]
|
||
#[sampler(<span class="number">2</span>)]
|
||
</span>color_texture: Handle<Image>,
|
||
}
|
||
|
||
<span class="comment">// All functions on `Material2d` have default impls. You only need to implement the
|
||
// functions that are relevant for your material.
|
||
</span><span class="kw">impl </span>Material2d <span class="kw">for </span>CustomMaterial {
|
||
<span class="kw">fn </span>fragment_shader() -> ShaderRef {
|
||
<span class="string">"shaders/custom_material.wgsl"</span>.into()
|
||
}
|
||
}
|
||
|
||
<span class="comment">// Spawn an entity with a mesh using `CustomMaterial`.
|
||
</span><span class="kw">fn </span>setup(
|
||
<span class="kw-2">mut </span>commands: Commands,
|
||
<span class="kw-2">mut </span>meshes: ResMut<Assets<Mesh>>,
|
||
<span class="kw-2">mut </span>materials: ResMut<Assets<CustomMaterial>>,
|
||
asset_server: Res<AssetServer>,
|
||
) {
|
||
commands.spawn((
|
||
Mesh2d(meshes.add(Circle::new(<span class="number">50.0</span>))),
|
||
MeshMaterial2d(materials.add(CustomMaterial {
|
||
color: RED.into(),
|
||
color_texture: asset_server.load(<span class="string">"some_image.png"</span>),
|
||
})),
|
||
));
|
||
}</code></pre></div>
|
||
<p>In WGSL shaders, the material’s binding would look like this:</p>
|
||
<div class="example-wrap"><pre class="language-wgsl"><code>struct CustomMaterial {
|
||
color: vec4<f32>,
|
||
}
|
||
|
||
@group(2) @binding(0) var<uniform> material: CustomMaterial;
|
||
@group(2) @binding(1) var color_texture: texture_2d<f32>;
|
||
@group(2) @binding(2) var color_sampler: sampler;</code></pre></div></div></details><h2 id="provided-methods" class="section-header">Provided Methods<a href="#provided-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="method.vertex_shader" class="method"><a class="src rightside" href="../src/bevy_sprite/mesh2d/material.rs.html#121-123">Source</a><h4 class="code-header">fn <a href="#method.vertex_shader" class="fn">vertex_shader</a>() -> <a class="enum" href="../bevy_render/render_resource/shader/enum.ShaderRef.html" title="enum bevy_render::render_resource::shader::ShaderRef">ShaderRef</a></h4></section></summary><div class="docblock"><p>Returns this material’s vertex shader. If <a href="../bevy_render/render_resource/shader/enum.ShaderRef.html#variant.Default" title="variant bevy_render::render_resource::shader::ShaderRef::Default"><code>ShaderRef::Default</code></a> is returned, the default mesh vertex shader
|
||
will be used.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.fragment_shader" class="method"><a class="src rightside" href="../src/bevy_sprite/mesh2d/material.rs.html#127-129">Source</a><h4 class="code-header">fn <a href="#method.fragment_shader" class="fn">fragment_shader</a>() -> <a class="enum" href="../bevy_render/render_resource/shader/enum.ShaderRef.html" title="enum bevy_render::render_resource::shader::ShaderRef">ShaderRef</a></h4></section></summary><div class="docblock"><p>Returns this material’s fragment shader. If <a href="../bevy_render/render_resource/shader/enum.ShaderRef.html#variant.Default" title="variant bevy_render::render_resource::shader::ShaderRef::Default"><code>ShaderRef::Default</code></a> is returned, the default mesh fragment shader
|
||
will be used.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.depth_bias" class="method"><a class="src rightside" href="../src/bevy_sprite/mesh2d/material.rs.html#133-135">Source</a><h4 class="code-header">fn <a href="#method.depth_bias" class="fn">depth_bias</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.f32.html">f32</a></h4></section></summary><div class="docblock"><p>Add a bias to the view depth of the mesh which can be used to force a specific render order.</p>
|
||
</div></details><section id="method.alpha_mode" class="method"><a class="src rightside" href="../src/bevy_sprite/mesh2d/material.rs.html#137-139">Source</a><h4 class="code-header">fn <a href="#method.alpha_mode" class="fn">alpha_mode</a>(&self) -> <a class="enum" href="enum.AlphaMode2d.html" title="enum bevy_sprite::AlphaMode2d">AlphaMode2d</a></h4></section><details class="toggle method-toggle" open><summary><section id="method.specialize" class="method"><a class="src rightside" href="../src/bevy_sprite/mesh2d/material.rs.html#144-150">Source</a><h4 class="code-header">fn <a href="#method.specialize" class="fn">specialize</a>(
|
||
descriptor: &mut <a class="struct" href="../bevy_render/render_resource/pipeline/struct.RenderPipelineDescriptor.html" title="struct bevy_render::render_resource::pipeline::RenderPipelineDescriptor">RenderPipelineDescriptor</a>,
|
||
layout: &<a class="struct" href="../bevy_mesh/vertex/struct.MeshVertexBufferLayoutRef.html" title="struct bevy_mesh::vertex::MeshVertexBufferLayoutRef">MeshVertexBufferLayoutRef</a>,
|
||
key: <a class="struct" href="struct.Material2dKey.html" title="struct bevy_sprite::Material2dKey">Material2dKey</a><Self>,
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/1.84.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.unit.html">()</a>, <a class="enum" href="../bevy_render/render_resource/pipeline_specializer/enum.SpecializedMeshPipelineError.html" title="enum bevy_render::render_resource::pipeline_specializer::SpecializedMeshPipelineError">SpecializedMeshPipelineError</a>></h4></section></summary><div class="docblock"><p>Customizes the default <a href="../bevy_render/render_resource/pipeline/struct.RenderPipelineDescriptor.html" title="struct bevy_render::render_resource::pipeline::RenderPipelineDescriptor"><code>RenderPipelineDescriptor</code></a>.</p>
|
||
</div></details></div><h2 id="dyn-compatibility" class="section-header">Dyn Compatibility<a href="#dyn-compatibility" class="anchor">§</a></h2><div class="dyn-compatibility-info"><p>This trait is <b>not</b> <a href="https://doc.rust-lang.org/1.84.0/reference/items/traits.html#object-safety">dyn compatible</a>.</p><p><i>In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.</i></p></div><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"><section id="impl-Material2d-for-ColorMaterial" class="impl"><a class="src rightside" href="../src/bevy_sprite/mesh2d/color_material.rs.html#151-159">Source</a><a href="#impl-Material2d-for-ColorMaterial" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.Material2d.html" title="trait bevy_sprite::Material2d">Material2d</a> for <a class="struct" href="struct.ColorMaterial.html" title="struct bevy_sprite::ColorMaterial">ColorMaterial</a></h3></section><section id="impl-Material2d-for-Wireframe2dMaterial" class="impl"><a class="src rightside" href="../src/bevy_sprite/mesh2d/wireframe2d.rs.html#227-244">Source</a><a href="#impl-Material2d-for-Wireframe2dMaterial" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.Material2d.html" title="trait bevy_sprite::Material2d">Material2d</a> for <a class="struct" href="struct.Wireframe2dMaterial.html" title="struct bevy_sprite::Wireframe2dMaterial">Wireframe2dMaterial</a></h3></section></div><script src="../trait.impl/bevy_sprite/mesh2d/material/trait.Material2d.js" async></script></section></div></main></body></html> |