Exploring the linewidth feature in Three.js when working with LineSegmentsGeometry and LineSegments2

My goal is to create lines wider than one pixel using the LineMaterial and LineSegments2 classes from the threejs examples library.

To achieve this, I've been following the guidelines provided in the response to a similar inquiry found here: Three.js r91 - how do I use the new linewidth property to fatten/widen lines?

However, I'm encountering difficulties in getting the line segments to display correctly.

When my code is structured like this:

const sourcePoint = new THREE.Vector3(ctx.x * window.sceneSettings.size_adjustment,
                                      ctx.y * window.sceneSettings.size_adjustment,
                                      ctx.z * window.sceneSettings.size_adjustment)
const line_geom = new THREE.Geometry()
window.meshObjects.map(sphere => {
  if (sphere.userData.strain == targetStrain && sphere.userData.group != "Nationwide") {
    const targetPoint = sphere.position
    line_geom.vertices.push(sourcePoint, targetPoint)
  }
})
const edges = new THREE.WireframeGeometry(line_geom)
var lsgeom = new THREE.LineSegmentsGeometry().setPositions(edges.attributes.position.array)
const lineMaterial = new THREE.LineMaterial({ color: 0x000000, linewidth: 10 })
lineMaterial.resolution.set( window.innerWidth, window.innerHeight );
var lines = new THREE.LineSegments2(lsgeom, lineMaterial)
lines.computeLineDistances()
scene.add(lines)

The lines fail to appear.

Alternatively, when I attempt the following approach (presumably the correct way):

const edges = new THREE.LineSegments(line_geom)
var lsgeom = new THREE.LineSegmentsGeometry().fromLineSegements(edges)
const lineMaterial = new THREE.LineMaterial({ color: 0x000000, linewidth: 10 })
lineMaterial.resolution.set( window.innerWidth, window.innerHeight );
var lines = new THREE.LineSegments2(lsgeom, lineMaterial)

I encounter the error message:

THREE.LineSegmentsGeometry.computeBoundingSphere(): Computed radius is NaN. The instanced position data is likely to have NaN values

If I opt to use an EdgeGeometry as the intermediary, I end up with a box at 0, 0, 0 instead of the desired line segments. I've tried various other strategies, such as converting the geometry to a BufferGeometry and then applying setPositions on LineSegmentsGeometry, but this only leads to the same error related to computeBoundingSphere.

Any assistance on this issue would be greatly appreciated.

Answer №1

If you're looking to create a 3-line segment connecting three specific coordinates:

const coordinates = [
    0, 0, 0,   // Coordinate for the start of the first segment
    1, 0, 0,   // Coordinate for the end of the first segment
    1, 1, 0,   // Coordinate for the end of the second segment
  ];

To achieve this, use the setPositions method with the following values:

const positions = [
    0, 0, 0,   // Start point of the first segment
    1, 0, 0,   // End point of the first segment

    1, 0, 0,   // Start point of the second segment
    1, 1, 0,   // End point of the second segment

    1, 1, 0,   // Start point of the third segment
    0, 1, 0    // End point of the third segment
  ];

Your edges.attributes.position.array likely contains values similar to those in the coordinates array provided, or it could be an array of Vector3 objects.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Discover the size of objects in three.js

Looking for a way to determine the dimensions of a mesh in three.js? I have Collada (.dae) files and need to know their size in units (x,y,z). I've heard about using geometry.computeBoundingBox(), but could use more guidance on how to implement it. ...

Error: Attempting to access index '0' of an undefined property in firebase and vuex

When using a vuex action to upload an image to Firebase and save the URL, everything seems fine until trying to retrieve the downloadUrl and adding it to the meetup database reference. The code I have looks like this: actions: { createMeetup ({commit ...

Enhancing Navigation in React Native by Passing Multiple Values to the Next Screen

I have a challenge where I can successfully pass the first value of (postONE) from the current screen to Screen Two, and it displays perfectly there. However, I am unable to show the second value of (postTwo). This means that the Next screen only shows the ...

What is the best method for clearing three.js (or WebGL) when refreshing a web page?

Our three.js application is quite complex, with numerous materials, scenes, and render buffers. After only a few refreshes/restarts, it would encounter various issues, all related to running out of WebGL resources. To address this, I have implemented a cl ...

Is it possible to deselect a jQuery checkbox while keeping the other checkboxes checked and their results accessible?

Once the "ALL GAMES" checkbox is unchecked in the provided link, all results disappear, even though there are still checkboxes selected with the relevant list items to display. I am attempting to prevent all results from being removed when deselecting the ...

Implementing a JQuery function to generate a popup whenever a user clicks on a table row (tr) in an

I am working on a JSP page that contains a table, and I want to implement a pop-up window functionality when clicking on a specific row in the table. I have attempted to use JavaScript to connect with the row but so far, I have not been successful in creat ...

Retrieving all users in Sqlite database with a specific value

I am looking to identify and access each user who has a specific value in their row. Here is an example code snippet of what I want: sql.prepare("SELECT * FROM raid WHERE raid1 > 0 AND NOT id='685337576810610734'").get().forEach(async (user) ...

Ways to ensure that JavaScript code is executed after making AJAX requests

Greetings! I must admit, I am still in the early stages of learning AJAX and dynamic web technologies. My current dilemma bears resemblance to a discussion thread I came across, but it seems to involve a framework, which I am not utilizing. Possibly relat ...

AngularJS and Gulp: Enhancing Static Assets with Revisioning

I have implemented the gulp-rev module to re-vision static assets in my source files. It generates new file names for CSS, JS, and HTML files by appending a hash code to it. Before : app.js After : app-2cba45c.js The issue I am facing is that in my An ...

Looping through AJAX requests in JavaScript

I am attempting to make REST API requests in order to retrieve data and display it on a website. Although I have created a for loop to gather the data, I am encountering an issue where the data is not being displayed on the website. Upon checking with th ...

Steps for embedding the code into your website

I'm facing an issue with integrating a .jsx file into my website. I tried testing it on a single-page demo site, but nothing is showing up. Can someone guide me through the steps to successfully integrate it onto my site? I've also attached the . ...

The page keeps scrolling to the top on its own, without any input from me

Whenever I reach the bottom of the page, my function loads new items seamlessly. However, an issue arises when the new items load, causing the scrolling position to abruptly return to the top of the page. This disrupts the user experience and is not the de ...

Dynamically Add Routes in ExpressJS During Runtime

I am interested in creating routes dynamically at runtime, but I'm not entirely sure how to do it. Currently, I have the following code snippet: var app = express(); function CreateRoute(route){ app.use(route, require('./routes/customchat.js&ap ...

Tips for traversing through jquery ui accordion when a single panel has been disabled

I currently have a jQuery UI accordion set up with Next and Previous buttons in each panel to navigate through the sections. Additionally, I have a select HTML element in one of the panels where I can choose an option. If Option 1 is selected, the second a ...

Pattern to prevent consecutive hyphens and identical digits next to one another in a series

Here is a regular expression that can validate all numbers not being the same even after a hyphen: ^(\d)(?!\1+$)\d{3}-\d{1}$ For example, in the pattern: 0000-0 would not be allowed (all digits are the same) 0000-1 would be allowed 111 ...

What could be the reason why my POST endpoint isn't able to receive this AJAX request?

I am currently working on a JavaScript function that is supposed to send JSON data to my escreve POST REST method. $(document).ready(function() { $("#idform").on('submit', function(e) { e.preventDefault(); alert($("#idform"). ...

What is the purpose of sorting an object using the sequence defined in an array?

Have you ever wondered how the sortWeekFunction function can rearrange an object based on a predefined array order? It may seem complex at first glance, but let's break down how this code actually works. const weeksArr = ['sunday', ' ...

Error message: Unable to split path as a function when utilizing React hook forms in conjunction with Material UI

Check out this code snippet: <TextField name="name" required className='my-2 mx-auto' label="Full Name" variant="standard" style={{ "width": "60%" }} value={name} onChange={(event) => { set ...

How can I access the marker's on-screen location in react-native-maps?

Looking to create a unique custom tooltip with a semi-transparent background that can overlay a map. The process involves drawing the MapView first, then upon pressing a marker on top of the MapView, an overlay with a background color of "#00000033" is dra ...

Using AngularJS to send a $http.post request with Paypal integration

This form utilizes the standard PayPal format for making purchases. <form action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_xclick"> <input type="hidden" name="business" value="<a href= ...