Expanding the Girth of a Curved Trajectory in Three.js

I successfully crafted a racetrack-inspired curve using three.js by employing the given code snippet:

var path = new THREE.Path();

    path.moveTo(150,50);
    path.lineTo(150,150);
    path.quadraticCurveTo(150,175,100,175);
    path.quadraticCurveTo(50,175,50,150);
    path.lineTo(50,50);
    path.quadraticCurveTo(50,25,100,25);
    path.quadraticCurveTo(150,25,150,50);

    var pathPoints = path.getPoints();

    var pathGeometry = new THREE.BufferGeometry().setFromPoints(pathPoints);
    var pathMaterial = new THREE.LineBasicMaterial({color:0xFF1493});

    var raceTrack = new THREE.Line(pathGeometry,pathMaterial);
    scene.add(raceTrack);

}

Presently, the track appears as a single line. I am curious to know if there's a way to transform this line into a wider geometry (still in 2D) so that I can apply a texture to it.

The current rendition of the path resembles something like this:

I aim to achieve a similar shape but with an increased width for the line:

Answer №1

Introducing the new R91, three.js has implemented support for triangulated lines. This innovative method allows for line widths greater than 1px to be achieved reliably.

Check out a demo here:

In this example, textures have not been used, but uv-coordinates are generated for the underlying geometry.

Answer №2

There is no straightforward way to achieve this with regular Line objects.

If you're looking to create a custom race-track, here are a couple of options you can consider:

  • Create the entire "race-track" as a geometry, including both the inner and outer lines. This method allows for flexibility in defining different widths at various points, controlling bends, turns, and more. Here's a simple example:

    // Code snippet generating race-track geometry
    const shape = new THREE.Shape();
    
    // Define track outline
    shape.moveTo(150, 50);
    // Add more points and curves...
    ...
    
    // Create inner track shape
    const innerShape = new THREE.Path();
    // Add inner track points and curves...
    ...
    
    // Combine shapes to form race-track mesh
    const raceTrack = new THREE.Mesh(
      new THREE.ShapeGeometry(shape),
      new THREE.MeshBasicMaterial({ color: 0xff1493 })
    );
    

    You can view the full code example on CodePen: https://codepen.io/usefulthink/pen/eMBgmE

  • Alternatively, explore specialized fat-lines implementations like the ones showcased in these resources: Three.js Fat Lines Example or the MeshLine library available on GitHub: GitHub - THREE.MeshLine. While these may not perfectly fit your needs, they offer alternative approaches to creating complex line geometries.

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

No Response from Ajax Request

I am working with the following Script; function FetchNews(user_id,api_token){ $.ajax({ type:'GET', url: 'http://192.168.**.**/mysite/public/index.php/api/v1/news/'+user_id, headers: {'X- ...

Retrieving information from a public Google spreadsheet using client-side JavaScript

How can I retrieve cell values from a public Google spreadsheet? GET https://sheets.googleapis.com/v4/spreadsheets/1vW01Y46DcpCC7aKLIUwV_W4RXLbeukVwF-G9AA7P7R0/values/A1A4?key=abcdef The above request returns a 403 error. I also included the Referrer ...

Implementing dynamic title rendering based on image in vue.js

This is the code I'm working with, aiming to display slider data. My goal is that if image[0] appears on the slider, it should return title [0]. I'm quite curious about what could be missing in my setup.code-image ...

Sharing React components in projects

Two of my upcoming projects will require sharing components such as headers, footers, and inputs. To facilitate this, I have moved these shared components into a separate repository, which has been working well so far. In the common repository, I have set ...

Summernote - When validating text, it is shown as raw HTML

I have integrated Codeigniter with Summernote on the frontend. In a form, if certain fields are left empty, the same page reloads for validation checking. The JavaScript and CodeIgniter code I am using is as follows: $(window).load(function(){ &l ...

Rect cannot be resized using mouse events

I am currently working on resizing the rectangle inside the SVG using mouse events. To achieve this, I have created another circle shape at the right bottom edge of the rectangle and implemented resize events on that shape. However, I'm facing an issu ...

When trying to add a react-bootstrap modal, the react app crashes abruptly

Encountering errors while attempting to integrate react-bootstrap Modal component into my react project: ERROR in ./~/react-dom/lib/ReactDOMUnknownPropertyHook.js Module not found: Error: Cannot resolve module 'react/lib/ReactComponentTreeHook' ...

Saving information in node.js

My latest project involves creating an address book app using HTML, CSS, and JavaScript. The company provided me with a zip file containing the necessary resources to implement the app using node.js. However, my knowledge of node.js is limited and I have ...

Unable to reach redux actions through props while using react router

As someone who is new to working with React and Redux, I've been encountering an issue with accessing my Redux actions from the props when using them alongside React Router. Despite trying various configurations, the props only seem to contain the Rea ...

I am looking to apply the Normal texture to the Fbx model, but I am encountering issues with the normal not functioning

This is an example. I am in need of a model with both a color map and a normal map. Just like the one seen in Microsoft 3D Viewer. https://i.sstatic.net/DU2vo.png Furthermore, I already have a model with a normal map. My approach involves using the re ...

Using Vue.js, separate the values that are separated by commas

I am looking to extract values from a string and store them in an array for use in displaying values in a dropdown format within Vuejs String str = "abc,123,676,uuu". Once I have iterated through the values <li v-for = "value i ...

Stripping /r /n tags from TinyMCE content stored in MySQL

Whenever I retrieve content from the database and display it on the screen, I notice that my output contains /r/n tags. Even though I managed to remove the slashes while handling other tags using: htmlspecialchars_decode(stripslashes($row[Content])) The ...

Enhancing SEO Performance with React Server Components

Exploring the new topic of React Server Components, which has recently been released, how does it impact SEO compared to SSR/Next.js? Unlike Next.js and traditional SSR where components are rendered statically on the server, React Server Components are re ...

What strategies can be used to scale up a website's images in proportion to their page views or traffic?

The situation: On my simple HTML website, I have a group of images placed side by side. Each image is connected to a specific article, and when you hover over the image, a detailed description of the linked article pops up in the center of the page. The o ...

The `process` variable is not recognized in a Vue/TypeScript component

I've encountered an issue trying to use .env variables in my Vue app. When I ran npm install process, the only syntax that didn't result in an error when importing was: import * as process from 'process'; Prior to this, I received the ...

Organizing and managing one-on-one table tennis matches using a customized data structure. Leveraging the power of Vue, JavaScript, and

Seeking advice on the best approach for storing table tennis match data in a web application. I currently have a method in place that works, but I'm open to suggestions for improvement. Here is my current idea: matches: [ { id: 1 datePlayed ...

Showing error messages to users with Angular and Firebase

As a beginner in Angular JS, html, and css, I am facing a challenge with routing in my login component. When the user clicks submit, the application should redirect to the landing page upon successful authentication or return to the login page with an erro ...

Using maxCDN to deliver static files within a Node application

Our current project is built using keystone and nunjucks, with all paths to static files following the format /stc/img/someimage.jpg. I am looking for a way to serve these files through middleware in our node server from maxCDN. Is there a solution that ...

How to retrieve a string from a regular expression in Javascript without [Object object] output

Within my code, there exists a parent form component and a child component used for auto-completing text input. The Parent component passes an array of objects named autoCompTxt, consisting of name and id fields, to the Child component. //Parent: const [ob ...

Monitor the Ajax for any updates in the database and trigger the code accordingly

My objective is to send requests to a php file with specific parameters in this format: <!DOCTYPE html> <html> <head> <script> function onsubmit() { var id = $_SESSION['user']['id']; var xmlhttp; if ...