The custom created THREE.Curve is not rendering correctly

Following advice from this solution, I have implemented a linearly interpolated curve in the code below:

THREE.Linear3 = THREE.Curve.create(

  function ( points, label /* array of Vector3 */) {

    this.points = (points == undefined) ? [] : points;
    this.label = label;

  },

  function ( t ) {    
    var v = new THREE.Vector3();
    var c = [];
    var points = this.points, point, intPoint, weight;
    point = ( points.length - 1 ) * t;

    intPoint = Math.floor( point );
    weight = point - intPoint;

    c[ 1 ] = intPoint;
    c[ 2 ] = intPoint  > points.length - 2 ? points.length - 1 : intPoint + 1;

    var pt1 = points[ c[1] ],
      pt2 = points[ c[2] ];

    v.copy( pt1 ).lerp( pt2, weight );

    return v;

  }

);

When attempting to animate trajectories of varying lengths, I encountered unexpected behavior where the curve cuts through space instead of passing through the specified points as shown in the animated gif below:

I am struggling to understand the getPoint function and its intended output. Any assistance would be greatly appreciated.

JSFiddle

The provided JSFiddle demonstrates the issue, particularly the jerky motion in the right corner as the tube expands.

http://jsfiddle.net/ElDeveloper/3uyf3sq3/1/

Answer №1

Code Cleanup Process

My exploration of the issue was aided by the following steps.

  • Ensure you dispose of geometries to prevent memory leaks when removing meshes from the scene:

    scene.remove(c_mesh)
    c_tube && c_tube.dispose();
    
  • Switch to WebGLRenderer as CanvasRenderer may leak removed objects and create new objects on each frame:

  • (For the fiddle) Slow down the motion for better observation of the process:

    setTimeout(animate, 500);
    
  • Consider the purpose of a 0-segment tube and adjust the index accordingly:

    if (index >= points.length - 1){
        index = 1; //start with 2 points
    }
    

Results and Insights

  • The TubeGeometry creates a tube with N segments, defaulting to 16 segments. Consider customizing this value:

  • Curve.getPoinAt method returns equidistant points, affecting the segment lengths in the TubeGeometry construction:

  • The TubeGeometry builds a tube with 16 segments of equal length, leading to uniform tube structure irrespective of the input points:

Resolution Strategies

  • Modify the TubeGeometry behavior by overloading getUtoTmapping method and customizing getPoint method:

    THREE.Linear3.prototype.getUtoTmapping = function(u) {
        return u;
    };
    
  • Update the number of segments in the TubeGeometry constructor for desired results:

  • Adjust the tube's orientation and angle issue by manipulating segment count or exploring tangent handling methods:


For further experimentation, refer to the updated version of the code on http://jsfiddle.net/dqn73m98/5/. Reach out to three.js developers for feature inquiries or implementation suggestions via GitHub issues.

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

Cleaning up webpage content by removing specific characters using Python's Selenium

Currently, I am using Selenium with Firefox in Python and facing a challenge while matching elements on a webpage based on keywords from a list. To ensure successful element lookup, I need to remove special characters like ® and ™ from the web page. Un ...

Cannon.js - Ensuring objects don't intersect with the ground during updates

I am currently utilizing Cannon.js in conjunction with Three.js. Within my scene, I have set up a heightfield along with five balls. My objective is to have the balls move around the heightfield using the physics engine provided by cannon.js. By manipulat ...

Filtering a list in AngularJS based on a property of an object with a filter applied on it

I am facing an issue with filtering a list of objects based on their properties using the filter filter. The problem arises when certain object properties have filters applied to them that alter their displayed format (such as the formatDate filter in the ...

Avoid Updating State After Adding Row in material-table

When utilizing the material-table library, my goal is to display a different component upon adding a row. Although the code functions as expected, I encountered the following error in the console: Warning: Can't perform a React state update on an unm ...

How to apply unique styles to multiple elements with the same class using jQuery?

How can I add different classes to multiple div elements with the same class based on their content? <div class = "flag"> Yes </div> <div class = "flag"> No </div> <div class = "flag"> Yes </div> <div class = "flag"& ...

PhoneGap iOS application storing outdated information

My Phonegap 2.1 app running on iOS 6, with jQuery 1.7.2 and JQMobile 1.1.1 libraries, is experiencing an issue where old data from ajax responses gets cached if the app is unused for a few days. The only way to resolve this issue is by reinstalling the a ...

PHP displaying incorrect value after modifying value in JavaScript

One issue I am facing is with an html page that submits a form through javascript. Prior to the submission of the form, I modify the value of a hidden tag, which should then be retrievable in php. However, when attempting to retrieve the value in php, it a ...

Experiencing excessive delay in loading data into the DOM following a successful response from a service in AngularJS

I am facing a challenge in loading a large set of data into an HTML table. To begin, you need to click on a button: <a ng-click="get_product()">load data</a> This button triggers a function called get_product: $scope.get_product = func ...

Generating rows within Angular while implementing ng-repeat

In my code, I am facing an issue where posts from the API are being repeated and displayed in rows of 9. My objective is to create a grid layout with 3 rows, each containing 3 posts. Unfortunately, the solution I attempted did not work as expected. I also ...

Errors in production arise from building React applications

I am new to using React and recently built a ToDo web app following a tutorial. Everything seemed fine during development, but when I tried to view the production version locally using serve -s build, two errors popped up that were not present before. reac ...

Troubleshooting problem with AngularJS and jQuery plugin when using links with # navigation

I have integrated a specific jquery plugin into my angularjs single page application. The primary block can be found in the following menu: http://localhost:81/website/#/portfolio This menu contains the following code block: <li> <a href=" ...

I am experiencing issues with my JavaScript not functioning properly in conjunction with my HTML and CSS. I am uncertain about the root cause of the problem (The console is displaying an error message:

I am facing challenges in creating a content slider and encountering issues with its functionality. Specifically, when testing locally, I noticed that the current-slide fades out and back in upon clicking the arrows left or right, but the slide content is ...

Steer clear of changing props directly in VueJS to prevent unintended

I am working with a component that has props, and I need to change the value from false to true. However, I encountered a message in the Chrome console advising against mutating a prop directly because it will be overwritten whenever the parent component r ...

What are the steps to manipulate the data within an EJS file after passing an array through Node.js as an object?

I'm currently developing a simple calendar application using Node.js and ejs. I am working on passing an array of months through express to my ejs file, with the goal of being able to cycle through each month by clicking the next button. How can I imp ...

Efficiently refresh several DIV elements with a single JSON request

After extensive searching, I've come to the conclusion that due to my limited proficiency in javascript, I need some help. The issue at hand is with a json format output produced by an ASP page on a separate webserver, which looks something like this: ...

Convert TypeScript-specific statements into standard JavaScript code

For my nextjs frontend, I want to integrate authentication using a keycloak server. I came across this helpful example on how to implement it. The only issue is that the example is in typescript and I need to adapt it for my javascript application. Being u ...

Why does the getter consistently generate the previous value?

Check out this code snippet: function User(fullName) { this.fullName = fullName; Object.defineProperties(this, { firstName: { get: function () { return fullName.split(" ")[0]; ...

Issue with spacing when assigning a JavaScript array variable to a value within input tags during a loop

Having some trouble with JavaScript code where the value is not passing in its entirety if there are spaces between the words. How can I make sure the full string or array object gets passed into the input tag's value? This is how I am trying to assi ...

Tips for streamlining the transfer of essential features from a main project to its duplicate projects

In my Angular project, I have a core repository on GitHub that serves as the foundation for custom client projects. Each time a new client comes onboard, we create a clone of the core project and make customizations based on their requirements. The issue ...

Angular project icons not displaying in the browser

My current project in Angular was functioning properly until recently. I am facing an issue where the images are not being displayed on the browser when I run ng serve, resulting in a 404 error. Interestingly, everything else seems to be working fine witho ...