Try   HackMD

CAGE Dev Log - Day 3

image
(I'm using Bevy 0.13)

Today I add the mesh to the curve.

let mut vertices: Vec<Vec3> = Vec::new();
    for p in new_curve.iter_positions(1024) {
        vertices.push(p);
    }
    for p in new_curve_r.iter_positions(1024) {
        vertices.push(p);
    }

    // generate indices
    let mut indices = Vec::new();

    for i in 0..1023 {
        indices.push(i as u32);
        indices.push((i + 1) as u32);
        indices.push((i + 1024) as u32);
        indices.push((i + 1024) as u32);
        indices.push((i + 1) as u32);
        indices.push((i + 1025) as u32);
    }
    let mut mesh = Mesh::new(TriangleList, RenderAssetUsages::default());

    // 法向量,跟光源反射有關。沒設定好會黑黑的。
    mesh.insert_attribute(
        Mesh::ATTRIBUTE_NORMAL,
        vec![Vec3::new(0., 1., 0.)].repeat((&vertices).len()),
    );
    mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, vertices);
    mesh.insert_indices(Indices::U32(indices));

    let asset = Color::DARK_GRAY;

    commands.spawn(PbrBundle {
        mesh: meshes.add(mesh),
        material: materials.add(asset),
        transform: Transform::from_translation(vec3(0., 0.1, 0.)),
        ..Default::default()
    });

The first step is to calculate vertices, and then set triangles. The indices are the indices of vertices, indicating the three vertices of triangles.

For a real use case, perhaps I could load the 3D model and transform the asset and mesh to fit the curve.

REF