I'm trying to draw a hollow rectangle (i.e. the border of a rectangle without the middle) using a unit square plus normals. The actual positions are being calculated in my vertex shader.
For example, without worrying about the outline for a moment, the unit square positions multiplied by my model matrix gives:
0,0 ___ 1,0 10,10 ___ 60,10
| | becomes | |
| | | |
0,1 --- 1,1 10,60 --- 60,60
I think multiplying a normalised position by the scale to get the real size is reasonably well understood. The problem I am having is how to apply this same principle to producing an outline made of 8 triangles.
Assuming that the outline increases the size of the shape, I can find the inside points using the above approach. But the outer points are problematic and I can't quite figure out how to calculate them with the information I have available.
The formula without using a transformation matrix is relatively straightforward:
((vertex_position * scale + vertex_normal * outline_thickness) * rotation) + translation
I've made quite a few attempts at this, and I thought I could simply add the vertex_normal * thickness calculation to the scale components of the model matrix:
let ty = r_pc.info.x;
let thickness = r_pc.info.y;
let is_fill = select(false, true, ty >= 0.5 && ty < 1.5);
let is_outline = select(false, true, ty >= 1.5 && ty < 2.5);
var model = r_pc.model;
var position = vec4(vertex.position, 0.0, 1.0);
if is_outline {
// Add thickness to model's scale components.
var width = vertex.normal * thickness;
model[0][0] += width.x;
model[1][1] += width.y;
}
position = r_camera.view_proj * model * position;
let color = r_pc.color;
let uv = vertex.uv;
return VsOut(position, color, uv);
But that left me missing the top-left half of the outline!
I understand why that would be - it's a multiplication by 0. I think combining the transformations in the way I have done is not quite accurate. I believe I need to scale the point first, then add the translation along the normal, before applying the rotation and translation.
I don't know how to construct such a matrix when the only information I have available is:
- vertex position (normalised)
- vertex normal (or mitre joint)
- model matrix describing the rectangle (not the outline extent)
- outline thickness (not normalised)
Given this information, is it possible to calculate the position of the outer vertices and how? If not, what additional information do I need and how?
