@@ -3,26 +3,26 @@ use crate::{
3
3
MaterialPlugin , StandardMaterial ,
4
4
} ;
5
5
use bevy_app:: { App , Plugin } ;
6
- use bevy_asset:: { uuid_handle, Asset , Assets , Handle } ;
7
- use bevy_ecs:: component:: Component ;
6
+ use bevy_asset:: { Asset , Assets , Handle } ;
7
+ use bevy_ecs:: {
8
+ component:: Component , lifecycle:: HookContext , resource:: Resource , world:: DeferredWorld ,
9
+ } ;
8
10
use bevy_math:: { prelude:: Rectangle , Quat , Vec2 , Vec3 } ;
9
11
use bevy_reflect:: { Reflect , TypePath } ;
10
12
use bevy_render:: load_shader_library;
13
+ use bevy_render:: mesh:: Mesh3d ;
11
14
use bevy_render:: render_asset:: RenderAssets ;
12
15
use bevy_render:: render_resource:: { AsBindGroupShaderType , ShaderType } ;
13
16
use bevy_render:: texture:: GpuImage ;
14
17
use bevy_render:: {
15
18
alpha:: AlphaMode ,
16
- mesh:: { Mesh , Mesh3d , MeshBuilder , MeshVertexBufferLayoutRef , Meshable } ,
19
+ mesh:: { Mesh , MeshBuilder , MeshVertexBufferLayoutRef , Meshable } ,
17
20
render_resource:: {
18
21
AsBindGroup , CompareFunction , RenderPipelineDescriptor , SpecializedMeshPipelineError ,
19
22
} ,
20
23
RenderDebugFlags ,
21
24
} ;
22
25
23
- const FORWARD_DECAL_MESH_HANDLE : Handle < Mesh > =
24
- uuid_handle ! ( "afa817f9-1869-4e0c-ac0d-d8cd1552d38a" ) ;
25
-
26
26
/// Plugin to render [`ForwardDecal`]s.
27
27
pub struct ForwardDecalPlugin ;
28
28
@@ -32,8 +32,7 @@ impl Plugin for ForwardDecalPlugin {
32
32
33
33
app. register_type :: < ForwardDecal > ( ) ;
34
34
35
- app. world_mut ( ) . resource_mut :: < Assets < Mesh > > ( ) . insert (
36
- FORWARD_DECAL_MESH_HANDLE . id ( ) ,
35
+ let mesh = app. world_mut ( ) . resource_mut :: < Assets < Mesh > > ( ) . add (
37
36
Rectangle :: from_size ( Vec2 :: ONE )
38
37
. mesh ( )
39
38
. build ( )
@@ -42,6 +41,8 @@ impl Plugin for ForwardDecalPlugin {
42
41
. unwrap ( ) ,
43
42
) ;
44
43
44
+ app. insert_resource ( ForwardDecalMesh ( mesh) ) ;
45
+
45
46
app. add_plugins ( MaterialPlugin :: < ForwardDecalMaterial < StandardMaterial > > {
46
47
prepass_enabled : false ,
47
48
shadows_enabled : false ,
@@ -63,7 +64,8 @@ impl Plugin for ForwardDecalPlugin {
63
64
/// * Looking at forward decals at a steep angle can cause distortion. This can be mitigated by padding your decal's
64
65
/// texture with extra transparent pixels on the edges.
65
66
#[ derive( Component , Reflect ) ]
66
- #[ require( Mesh3d ( FORWARD_DECAL_MESH_HANDLE ) ) ]
67
+ #[ require( Mesh3d ) ]
68
+ #[ component( on_add=forward_decal_set_mesh) ]
67
69
pub struct ForwardDecal ;
68
70
69
71
/// Type alias for an extended material with a [`ForwardDecalMaterialExt`] extension.
@@ -146,3 +148,20 @@ impl Default for ForwardDecalMaterialExt {
146
148
}
147
149
}
148
150
}
151
+
152
+ #[ derive( Resource ) ]
153
+ struct ForwardDecalMesh ( Handle < Mesh > ) ;
154
+
155
+ // Note: We need to use a hook here instead of required components since we cannot access resources
156
+ // with required components, and we can't otherwise get a handle to the asset from a required
157
+ // component constructor, since the constructor must be a function pointer, and we intentionally do
158
+ // not want to use `uuid_handle!`.
159
+ fn forward_decal_set_mesh ( mut world : DeferredWorld , HookContext { entity, .. } : HookContext ) {
160
+ let decal_mesh = world. resource :: < ForwardDecalMesh > ( ) . 0 . clone ( ) ;
161
+ let mut entity = world. entity_mut ( entity) ;
162
+ let mut entity_mesh = entity. get_mut :: < Mesh3d > ( ) . unwrap ( ) ;
163
+ // Only replace the mesh handle if the mesh handle is defaulted.
164
+ if * * entity_mesh == Handle :: default ( ) {
165
+ entity_mesh. 0 = decal_mesh;
166
+ }
167
+ }
0 commit comments