Grid Box



We left off last article rendering a plane with a nice grid on it. It should look something like this.

Grid Plane With 10 Subdivisions

Grid Box

This is starting to look pretty good. Our shader antialises the lines very smoothly over our UV coordinates and we are able to easily draw grids. Let’s make it even nicer though, let’s render a box instead of a plane! In order to do this, we need to write some code to build a mesh for a box with the proper UV coordinates.

main.rs

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
pub struct GridBox {
    size: Vec3,
    subdivisions: UVec3,
}

impl From<GridBox> for Mesh {
    fn from(value: GridBox) -> Self {
        let x_vertex_count = value.subdivisions.x + 2;
        let y_vertex_count = value.subdivisions.y + 2;
        let z_vertex_count = value.subdivisions.z + 2;

        let num_vertices = (((z_vertex_count * x_vertex_count)
            + (z_vertex_count * y_vertex_count)
            + (x_vertex_count * y_vertex_count))
            * 2) as usize;
        let num_indices = ((((z_vertex_count - 1) * (x_vertex_count - 1))
            + ((z_vertex_count - 1) * (y_vertex_count - 1))
            + ((x_vertex_count - 1) * (y_vertex_count - 1)))
            * 6
            * 2) as usize;
        let x_up = Vec3::X.to_array();
        let x_down = (Vec3::X * -1.0).to_array();
        let y_up = Vec3::Y.to_array();
        let y_down = (Vec3::Y * -1.0).to_array();
        let z_up = Vec3::Z.to_array();
        let z_down = (Vec3::Z * -1.0).to_array();

        let mut positions: Vec<[f32; 3]> = Vec::with_capacity(num_vertices);
        let mut normals: Vec<[f32; 3]> = Vec::with_capacity(num_vertices);
        let mut uvs: Vec<[f32; 2]> = Vec::with_capacity(num_vertices);
        let mut indices: Vec<u32> = Vec::with_capacity(num_indices);
        let mut index_offset: u32 = 0;

        // Front Mesh
        for z in 0..z_vertex_count {
            for x in 0..x_vertex_count {
                let tx = x as f32 / (x_vertex_count - 1) as f32;
                let ty = 1.0 as f32;
                let tz = z as f32 / (z_vertex_count - 1) as f32;
                let ux = (x % 2) as f32;
                let uz = (z % 2) as f32;
                positions.push([
                    (-0.5 + tx) * value.size.x,
                    (-0.5 + ty) * value.size.y,
                    (-0.5 + tz) * value.size.z,
                ]);
                normals.push(y_up);
                uvs.push([ux, uz]);
            }
        }

        // Front Indices
        for z in 0..z_vertex_count - 1 {
            for x in 0..x_vertex_count - 1 {
                let quad = z * x_vertex_count + x;
                indices.push(quad + x_vertex_count + 1);
                indices.push(quad + 1);
                indices.push(quad + x_vertex_count);
                indices.push(quad);
                indices.push(quad + x_vertex_count);
                indices.push(quad + 1);
            }
        }

        // Back Mesh
        index_offset = positions.len() as u32;
        for z in 0..z_vertex_count {
            for x in 0..x_vertex_count {
                let tx = x as f32 / (x_vertex_count - 1) as f32;
                let ty = 0.0 as f32;
                let tz = z as f32 / (z_vertex_count - 1) as f32;
                let ux = (x % 2) as f32;
                let uz = (z % 2) as f32;
                positions.push([
                    (-0.5 + tx) * value.size.x,
                    (-0.5 + ty) * value.size.y,
                    (-0.5 + tz) * value.size.z,
                ]);
                normals.push(y_down);
                uvs.push([ux, uz]);
            }
        }

        // Back Indices
        for z in 0..z_vertex_count - 1 {
            for x in 0..x_vertex_count - 1 {
                let quad = index_offset + z * x_vertex_count + x;
                indices.push(quad + 1);
                indices.push(quad + x_vertex_count + 1);
                indices.push(quad + x_vertex_count);
                indices.push(quad + x_vertex_count);
                indices.push(quad);
                indices.push(quad + 1);
            }
        }

        // Top Mesh
        index_offset = positions.len() as u32;
        for y in 0..y_vertex_count {
            for x in 0..x_vertex_count {
                let tx = x as f32 / (x_vertex_count - 1) as f32;
                let ty = y as f32 / (y_vertex_count - 1) as f32;
                let tz = 1.0 as f32;
                let ux = (x % 2) as f32;
                let uy = (y % 2) as f32;
                positions.push([
                    (-0.5 + tx) * value.size.x,
                    (-0.5 + ty) * value.size.y,
                    (-0.5 + tz) * value.size.z,
                ]);
                normals.push(z_up);
                uvs.push([ux, uy]);
            }
        }

        // Top Indices
        for y in 0..y_vertex_count - 1 {
            for x in 0..x_vertex_count - 1 {
                let quad = index_offset + y * x_vertex_count + x;
                indices.push(quad + 1);
                indices.push(quad + x_vertex_count + 1);
                indices.push(quad + x_vertex_count);
                indices.push(quad + x_vertex_count);
                indices.push(quad);
                indices.push(quad + 1);
            }
        }

        // Bottom Mesh
        index_offset = positions.len() as u32;
        for y in 0..y_vertex_count {
            for x in 0..x_vertex_count {
                let tx = x as f32 / (x_vertex_count - 1) as f32;
                let ty = y as f32 / (y_vertex_count - 1) as f32;
                let tz = 0.0 as f32;
                let ux = (x % 2) as f32;
                let uy = (y % 2) as f32;
                positions.push([
                    (-0.5 + tx) * value.size.x,
                    (-0.5 + ty) * value.size.y,
                    (-0.5 + tz) * value.size.z,
                ]);
                normals.push(z_down);
                uvs.push([ux, uy]);
            }
        }

        // Bottom Indices
        for y in 0..y_vertex_count - 1 {
            for x in 0..x_vertex_count - 1 {
                let quad = index_offset + y * x_vertex_count + x;
                indices.push(quad + x_vertex_count + 1);
                indices.push(quad + 1);
                indices.push(quad + x_vertex_count);
                indices.push(quad + x_vertex_count);
                indices.push(quad + 1);
                indices.push(quad);
            }
        }

        // Right Mesh
        index_offset = positions.len() as u32;
        for y in 0..y_vertex_count {
            for z in 0..z_vertex_count {
                let tx = 1.0 as f32;
                let ty = y as f32 / (y_vertex_count - 1) as f32;
                let tz = z as f32 / (z_vertex_count - 1) as f32;
                let uz = (z % 2) as f32;
                let uy = (y % 2) as f32;
                positions.push([
                    (-0.5 + tx) * value.size.x,
                    (-0.5 + ty) * value.size.y,
                    (-0.5 + tz) * value.size.z,
                ]);
                normals.push(x_up);
                uvs.push([uz, uy]);
            }
        }

        // Right Indices
        for y in 0..y_vertex_count - 1 {
            for z in 0..z_vertex_count - 1 {
                let quad = index_offset + y * x_vertex_count + z;
                indices.push(quad + z_vertex_count + 1);
                indices.push(quad + 1);
                indices.push(quad + z_vertex_count);
                indices.push(quad + z_vertex_count);
                indices.push(quad + 1);
                indices.push(quad);
            }
        }

        // Left Mesh
        index_offset = positions.len() as u32;
        for y in 0..y_vertex_count {
            for z in 0..z_vertex_count {
                let tx = 0.0 as f32;
                let ty = y as f32 / (y_vertex_count - 1) as f32;
                let tz = z as f32 / (z_vertex_count - 1) as f32;
                let uz = (z % 2) as f32;
                let uy = (y % 2) as f32;
                positions.push([
                    (-0.5 + tx) * value.size.x,
                    (-0.5 + ty) * value.size.y,
                    (-0.5 + tz) * value.size.z,
                ]);
                normals.push(x_down);
                uvs.push([uz, uy]);
            }
        }

        // Left Indices
        for y in 0..y_vertex_count - 1 {
            for z in 0..z_vertex_count - 1 {
                let quad = index_offset + y * x_vertex_count + z;
                indices.push(quad + 1);
                indices.push(quad + z_vertex_count + 1);
                indices.push(quad + z_vertex_count);
                indices.push(quad + 1);
                indices.push(quad + z_vertex_count);
                indices.push(quad);
            }
        }

        Mesh::new(PrimitiveTopology::TriangleList)
            .with_indices(Some(Indices::U32(indices)))
            .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
            .with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
            .with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs)
    }
}

Update Render Scene

Now that we have a nice way to build our meshes, let’s update the scene and see what it looks like.

main.rs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
impl TextGem {
    ...
    fn startup(
        mut commands: Commands,
        mut meshes: ResMut<Assets<Mesh>>,
        mut grid_materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, GridMaterial>>>,
    ) {
        ...

        // Replace Graph Paper with a box
        // Grid Box
        commands.spawn(MaterialMeshBundle {
            mesh: meshes.add(
                GridBox {
                    size: Vec3::new(300.0, 30.0, 300.0),
                    subdivisions: UVec3::new(10, 0, 10),
                }
                .into(),
            ),
            material: grid_materials.add(ExtendedMaterial {
                base: Color::BLUE.into(),
                extension: GridMaterial {
                    color: Color::ORANGE,
                    subdivisions: UVec2::new(0, 0),
                    line_widths: Vec2::new(0.01, 0.01),
                },
            }),
            ..Default::default()
        });
    }
    ...
}

Run our application with cargo run and we should see a 3d shape that looks like this.

Grid Box Small

Large Grid Box

Let’s make a larger grid box. Unless we are making games that use a small grid like Into the Breach, we’ll probably want a fairly large grid to play on. For instance, RimWorld map sizes range from 200x200 to 300x300. Let’s create a 200x200 grid and see how it looks.

main.rs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
impl TextGem {
    ...
    fn startup(
        mut commands: Commands,
        mut meshes: ResMut<Assets<Mesh>>,
        mut grid_materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, GridMaterial>>>,
    ) {
        ...

        // Camera
        commands.spawn(Camera3dBundle {
            transform: Transform::from_xyz(4000.0, 1000.0, 4000.0)
                .looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y),
            ..default()
        });

        ...

        // Grid Box
        commands.spawn(MaterialMeshBundle {
            mesh: meshes.add(
                GridBox {
                    size: Vec3::new(6000.0, 30.0, 6000.0),
                    subdivisions: UVec3::new(200, 0, 200),
                }
                .into(),
            ),
            material: grid_materials.add(ExtendedMaterial {
                base: Color::BLUE.into(),
                extension: GridMaterial {
                    color: Color::ORANGE,
                    subdivisions: UVec2::new(0, 0),
                    line_widths: Vec2::new(0.01, 0.01),
                },
            }),
            ..Default::default()
        });
    }
    ...
}

Run our application with cargo run and we now see a much larger grid box.

Grid Box Large

Next Up…

This is great! We can now render grids of any size for our games. We still need a camera to move around our field of view and we’ll also need to render text to the grid to represent our game objects. In the next post, we’ll create a camera that is capable of zooming around and looking at our grids.

Full code for this post can be found here: TextGem Post 3.