1
1
use bevy:: {
2
+ core:: Time ,
2
3
core_pipeline:: Transparent3d ,
3
4
ecs:: system:: { lifetimeless:: * , SystemParamItem } ,
5
+ math:: Quat ,
4
6
pbr:: {
5
7
DrawMesh , MeshPipeline , MeshPipelineFlags , MeshPipelineKey , MeshUniform , SetMeshBindGroup ,
6
8
SetMeshViewBindGroup ,
@@ -28,6 +30,7 @@ fn main() {
28
30
. add_plugins ( DefaultPlugins )
29
31
. add_plugin ( CustomMaterialPlugin )
30
32
. add_startup_system ( setup)
33
+ . add_system ( rotate_mesh)
31
34
. run ( ) ;
32
35
}
33
36
@@ -37,9 +40,13 @@ fn setup(
37
40
mut meshes : ResMut < Assets < Mesh > > ,
38
41
mut materials : ResMut < Assets < CustomMaterial > > ,
39
42
) {
40
- // cube
43
+ // cube with custom vertex attributes
44
+ let mut mesh = Mesh :: from ( shape:: Cube { size : 1.0 } ) ;
45
+ mesh. vertex_layout_mut ( )
46
+ . push ( Mesh :: ATTRIBUTE_COLOR , VertexFormat :: Float32x4 ) ;
47
+ mesh. set_attribute ( Mesh :: ATTRIBUTE_COLOR , cube_vertex_colors ( ) ) ;
41
48
commands. spawn ( ) . insert_bundle ( (
42
- meshes. add ( Mesh :: from ( shape :: Cube { size : 1.0 } ) ) ,
49
+ meshes. add ( mesh ) ,
43
50
Transform :: from_xyz ( 0.0 , 0.5 , 0.0 ) ,
44
51
GlobalTransform :: default ( ) ,
45
52
Visibility :: default ( ) ,
@@ -56,6 +63,48 @@ fn setup(
56
63
} ) ;
57
64
}
58
65
66
+ fn rotate_mesh ( time : Res < Time > , mut query : Query < & mut Transform , With < Handle < Mesh > > > ) {
67
+ let angle = std:: f32:: consts:: TAU / 12.0 ;
68
+ let mut transform = query. single_mut ( ) ;
69
+ transform. rotate ( Quat :: from_rotation_x ( angle * time. delta_seconds ( ) ) ) ;
70
+ transform. rotate ( Quat :: from_rotation_y ( angle * time. delta_seconds ( ) ) ) ;
71
+ }
72
+
73
+ fn cube_vertex_colors ( ) -> Vec < [ f32 ; 4 ] > {
74
+ vec ! [
75
+ // Front
76
+ [ 0.0 , 0.5 , 0.0 , 1.0 ] , // Green
77
+ [ 0.5 , 0.0 , 0.0 , 1.0 ] , // Red
78
+ [ 0.0 , 0.5 , 0.5 , 1.0 ] , // Cyan
79
+ [ 0.5 , 0.0 , 0.5 , 1.0 ] , // Magenta
80
+ // Back
81
+ [ 0.5 , 0.5 , 0.0 , 1.0 ] , // Yellow
82
+ [ 0.0 , 0.0 , 0.0 , 1.0 ] , // Black
83
+ [ 0.5 , 0.5 , 0.5 , 1.0 ] , // White
84
+ [ 0.0 , 0.0 , 0.5 , 1.0 ] , // Blue
85
+ // Right
86
+ [ 0.5 , 0.5 , 0.5 , 1.0 ] , // White
87
+ [ 0.0 , 0.0 , 0.0 , 1.0 ] , // Black
88
+ [ 0.0 , 0.5 , 0.5 , 1.0 ] , // Cyan
89
+ [ 0.5 , 0.0 , 0.0 , 1.0 ] , // Red
90
+ // Left
91
+ [ 0.0 , 0.5 , 0.0 , 1.0 ] , // Green
92
+ [ 0.5 , 0.0 , 0.5 , 1.0 ] , // Magenta
93
+ [ 0.5 , 0.5 , 0.0 , 1.0 ] , // Yellow
94
+ [ 0.0 , 0.0 , 0.5 , 1.0 ] , // Blue
95
+ // Top
96
+ [ 0.0 , 0.0 , 0.0 , 1.0 ] , // Black
97
+ [ 0.5 , 0.5 , 0.0 , 1.0 ] , // Yellow
98
+ [ 0.5 , 0.0 , 0.5 , 1.0 ] , // Magenta
99
+ [ 0.0 , 0.5 , 0.5 , 1.0 ] , // Cyan
100
+ // Bottom
101
+ [ 0.5 , 0.0 , 0.0 , 1.0 ] , // Red
102
+ [ 0.0 , 0.5 , 0.0 , 1.0 ] , // Green
103
+ [ 0.0 , 0.0 , 0.5 , 1.0 ] , // Blue
104
+ [ 0.5 , 0.5 , 0.5 , 1.0 ] , // White
105
+ ]
106
+ }
107
+
59
108
#[ derive( Debug , Clone , TypeUuid ) ]
60
109
#[ uuid = "4ee9c363-1124-4113-890e-199d81b00281" ]
61
110
pub struct CustomMaterial {
0 commit comments