How to make thick line in 3D graphics (webGL)?

In summary, the author is trying to do something that's not possible with the current WebGL renderer. He's trying to make thick lines, but he's not sure how to do it. He uses a three line approach, but it's not very good. He then looks at something called a stereoscopic model of a Clifford Square Torus. He notices that the three lines intersect at the center of gravity of the torus. He then looks at GeoGebra, a program that can help with accurate working sketches for this type of rendering.
  • #1
aheight
321
109
Hi guys,

I have a webGL application and my understanding is there is no option or at least the option to make thick lines is not working so I thought I would try to write my own routine. I suppose I could just make a double plot of the data just off-set a bit but I'm not exactly sure how to do this for a complex contour in 3D space. Suppose I have a set of 3D points that I draw lines between to make my contour:

firstContour={{x1,y1,z1},{x2,y2,z2},...{xn,yn,zn}}

I can make some progress by drawing multiple lines:

secondContour={{x1+dx,y1+dy,z1+dz},{x2+dx,y2+dy,z2+dz},...,{xn+dx,yn+dy,zn+dz}}
and a third likewise. Not too bad except when the plot is zoomed you can see the three contours so looks a bit unprofessional.I was just wondering if anyone here knows of a standard better more elegant way of accomplishing this task?

Thanks for reading.
 
Last edited:
Technology news on Phys.org
  • #2
There's an article on line drawing in WebGL here.
 
  • Like
Likes aheight
  • #3
aheight said:
Thanks for that! Looks like I would like to expand the lines in the shader from what I've read and I'm actually pretty good with shader code. Unfortunately, I have to admit I find github a bit difficult to navigate. Would you happen to know what the following instructions at the github site means? Where do I even type in those commands?

Code:
First you need to git clone and install dependencies:

git clone https://github.com/mattdesl/webgl-lines.git
cd webgl-lines
npm install
To start developing a demo, use one of the following:

  npm run native
  npm run triangles
  npm run expanded
  npm run projected
And open localhost:9966/[demo], for example localhost:9966/native.

production
For the bundle splitting and uglify step, use npm run build-all.
Those are linux or similar unix commands. If you're using windows, or similar, you can go to https://github.com/mattdesl/webgl-lines to see all the demo files or you can do a direct download via this link: https://github.com/mattdesl/webgl-lines/archive/master.zip

Your question was helpful to me, because although I haven't done much WebGL, I've spent some time on SVGs, and because it's easy to specify line width via SVG <path> statements, I thought of conversion of SVG to WebGL, and my searching on that led me to:
https://css-tricks.com/rendering-svg-paths-in-webgl/
=> http://mattdesl.github.io/svg-mesh-3d/
=> https://mattdesl.svbtle.com/drawing-lines-is-hard

Matt DesLauriers' CSS Tricks guest article (the first link) is fun and intriguing, and the demo it links to (second link) is quite impressive, and the link to his WebGL line drawing article seemed very much on point for response to your post here, so there you have it.
 
  • #4
toruswithcontour.png
Thanks for those references sysprog. I was able to download the code from github and I have been reviewing it: Think I'll try first to just trianglize the contour and send it through as (very narrow) polygons using gl.LINE_STRIP and see how it looks. This is what the contour looks like using the three line approach. It's the orange contour but if I zoom in, I can see the three separate lines but if I use only one line the contour does not show up well. All of that torus is webGL.
 

Attachments

  • toruswithcontour.png
    toruswithcontour.png
    22.1 KB · Views: 2,103
Last edited:
  • #5
aheight said:
Thanks for those references sysprog. I was able to download the code from github and I have been reviewing it: Think I'll try first to just trianglize the contour and send it through as (very narrow) polygons using gl.LINE_STRIP and see how it looks.
As you are no doubt already aware, you need only two inter-inversely postioned congruent right triangles to make a line segment of arbitrary thickness and length, and you can of course break up a curve into an arbitrary number of line segments (and perhaps do successive partial integrations and redifferentiations (unless it's continuous but not differentiable) to determine local densities of separations of functions -- i.e. how many line segments to use per curve part, varying depending on variance of curvature, for a target resolution) for triangle rendering of a spline curve. You might also want to look at the section of the first reference that references Bézier curves, which are reasonably easy to specify in SVG paths, and perhaps not terribly difficult to convert to WebGL.
This is what the contour looks like using the three line approach. It's the orange contour but if I zoom in, I can see the three separate lines but if I use only one line the contour does not show up well. All of that torus is webGL.

toruswithcontour-png.png


Wow. That's pretty impressive, aheight. Is that a stereoscopic model of a Clifford square torus using a Nash embedding? I notice that your three axial lines intersect at apparently what would be the center of gravity of the torus. Have you looked at GeoGebra? You can use it online or download it -- it can facilitate some mathematically accurate adjustable precursors that can serve as working sketches for the kind of thing you're rendering in WebGL. Here's an example (viewable on this page):

upload_2018-4-10_7-49-41.png
 

Attachments

  • upload_2018-4-10_7-49-41.png
    upload_2018-4-10_7-49-41.png
    55.2 KB · Views: 1,971
  • toruswithcontour-png.png
    toruswithcontour-png.png
    22.1 KB · Views: 1,833
Last edited:
  • #6
sysprog said:
Is that a stereoscopic model of a Clifford square torus using a Nash embedding? I notice that your three axial lines intersect at apparently what would be the center of gravity of the torus. Have you looked at GeoGebra? You can use it online or download it -- it can facilitate some mathematically accurate adjustable precursors that can serve as working sketches for the kind of thing you're rendering in WebGL. Here's an example (viewable on this page):

Hey sysprog, the torus is part of my website on algebraic functions you guys in this forum help me build! :) I am working on the thick lines but unfortunately my job takes me away from coding so can't do much of anything now. Here's the link to the webGL I wrote: algebraicfunction.com . You can see the torus by selecting it from the function drop-down menu and then selecting contours in the ring table drop down menu. It's 3D-interactive. The plot is described in the website :http://www.jujusdiaries.com/. Just select Introduction/Riemann Surfaces.
 
Last edited:

1. How do I adjust the thickness of a line in webGL?

To adjust the thickness of a line in webGL, you can use the "lineWidth" attribute in the WebGLRenderingContext object. This attribute accepts a floating-point value that represents the desired thickness of the line in pixels. You can set this attribute before drawing the line using the "gl.lineWidth()" method.

2. Can I make a line thicker in specific areas of a 3D graphic?

Yes, you can make a line thicker in specific areas of a 3D graphic by using different techniques such as using a shader program or modifying the geometry of the line. You can also achieve this effect by drawing multiple lines with varying thicknesses on top of each other.

3. How do I create a thick line with a custom shape in webGL?

To create a thick line with a custom shape in webGL, you can use primitive shapes such as triangles or quads to create the desired shape. Then, you can apply a texture to this shape to give it the appearance of a line. You can also use a shader program to create a custom shape and adjust its thickness.

4. Is it possible to animate the thickness of a line in webGL?

Yes, it is possible to animate the thickness of a line in webGL by using techniques such as morphing or interpolation. These techniques involve modifying the geometry or attributes of the line over time to create the illusion of a changing thickness. You can also use a shader program to dynamically adjust the thickness based on certain conditions.

5. Can I make a line thicker in response to user interaction?

Yes, you can make a line thicker in response to user interaction by using event listeners and modifying the "lineWidth" attribute or the geometry of the line based on the user's actions. For example, you can increase the line's thickness when the user hovers over it or clicks on it. This can be achieved using JavaScript in conjunction with webGL.

Similar threads

  • Programming and Computer Science
Replies
14
Views
2K
  • Precalculus Mathematics Homework Help
Replies
17
Views
977
  • Programming and Computer Science
Replies
6
Views
870
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Classical Physics
Replies
2
Views
949
  • General Math
Replies
3
Views
868
  • Programming and Computer Science
Replies
2
Views
1K
Replies
2
Views
2K
Back
Top