I donât think the LineBasicMaterial understands the attribute named edgeVertices that youâre binding to it. The default attribute that materials use to identify vertex positions should be named position.
edgeVertices.addAttribute(âpositionâ, new THREE.BufferAttribute(vertices, 3));
Thanks @marquizzo . I seem to be getting something on the screen now but it is not the rectangle I am looking for (the points above form a closed rectangle). Instead I am getting two parallel line segments , one formed by the 1st and 2nd point as well as the 2nd one formed by the 3rd and 4th points. Sorry I am new to three.js so I am not sure what BufferGeometry is doing to the points.
My main aim is to try and draw individual line segments for the for edges of a rectangle but later on be able to edit the colour of anyone of the edges when the mouse hovers over it. This is why I gave multiple materials. Not sure how to go about this though as LineSegments considers all the four lines at once (thus allowing me to use either ONLY blue or ONLY red for the edges).
That is how the LineSegments class works. It draws one segment for each pair of vertices. Line, on the other hand, will draw a consecutive line sequence (unless the API is changed with the new fat lines).
Thanks @EliasHasle . So how do I edit each line segment to have different colours ? In the geometry I see no property related to the line segments per se but only about position.
Maybe vertex colors will work on line segments? I have never tried. If it works, it will be like setting the same color on each end of the line segment, using a three-component attribute named âcolorâ.
Use an additional buffer attribute with colours for each vertex, setting the same colour values for pairs of vertices in your buffer geometry. I mean, if you want a rectangle with lines of different colours, then you have to have 8 vertices in your geometry.
Kind of :
var colors = [];
var c = new THREE.Color();
for (let i = 0; i < geometry.attributes.position.count / 2; i++) {
c.set(Math.random() * 0xffffff);
colors.push(c.x, c.y, c.z, c.x, c.y, c.z); // the same colour for two points
}
geometry.addAttribute("color", new THREE.BufferAttribute(new Float32Array(colors), 3));
and then var material = new THREE.LineBasicMaterial({vertexColors: THREE.VertexColors});
I only have two colours 0xff0000 and 0x0000ff . By default the edges of the rectangle need to be blue(0000ff) . When my mouse hovers to the edge, I need to use raycast to see which edge it intersects with and then change that colour to ff0000 to highlight that it has been selected or âmouse highlightedâ . I was thinking of doing this by addGroup() and dividing my 4 line segments into four groups and then updating the colour of the group in the mousemove() event handler. I am not familiar with groups but I think this definitely prove to be convenient.
So you mean to say that when it draws lines between the vertices, it gives colors based on an rgb color code ? I see 1, 0, 0 as a parameter and other places having 0.5 0 0 etc .