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