Utilizing THREE.js to rearrange vertex positions in RingGeometry

If you want to check out the Codepen, it's right here: http://codepen.io/pehrlich/pen/CogjG

I have a scenario where I need to display a ring geometry in the scene and then dynamically adjust its arc length (thetaLength). Initially, I am initializing it using the constructor, but when I try to modify the thetaLength through a separate function, the results are unexpected.

Below is the function call that I added named setThetaLength, which replicates part of the constructor logic:

THREE.RingGeometry.prototype.setThetaLength = function(thetaLength){

//  this.thetaLength = thetaLength;


  var i, o, uvs = [], radius = this.innerRadius, radiusStep = ( ( this.outerRadius - this.innerRadius ) / this.phiSegments );

    for ( i = 0; i < this.phiSegments + 1; i ++ ) { // concentric circles inside ring

        for ( o = 0; o < this.thetaSegments + 1; o ++ ) { // number of segments per circle

            var vertex = this.vertices[i + o]; // maybe i need to query vertex indices here.
            var segment = this.thetaStart + o / this.thetaSegments * this.thetaLength;
            vertex.x = radius * Math.cos( segment );
            vertex.y = radius * Math.sin( segment );

//          uvs.push( new THREE.Vector2( ( vertex.x / this.outerRadius + 1 ) / 2, ( vertex.y / this.outerRadius + 1 ) / 2 ) );
        }

        radius += radiusStep;

    }


    this.computeFaceNormals();

    this.boundingSphere = new THREE.Sphere( new THREE.Vector3(), radius );

  this.verticesNeedUpdate = true;

}

After calling the setThetaLength function without changing thetaLength, here is what the expected output should look like:

And below is the image showing the result after the function call:

Why is this happening? Could it be due to THREE.js caching some data or altering the order of vertices unexpectedly?

You can also view the codepen example here: http://codepen.io/pehrlich/pen/CogjG

Update: It appears to be related to vertex ordering. When I manually manage the vertex order, the rearrangement works fine.

Answer №1

Solved the issue on my own. Discovered and fixed an off by one error while iterating through segments.

Below is a method that can be used to adjust the phi of a THREE.JS sphere:

THREE.SphereGeometry.prototype.setPhiLength = function(phiLength){

  this.parameters.phiLength = phiLength;

  var heightSegments  = this.parameters.heightSegments,
      widthSegments   = this.parameters.widthSegments,
      radius          = this.parameters.radius,
      phiStart        = this.parameters.phiStart,
      thetaLength     = this.parameters.thetaLength,
      thetaStart      = this.parameters.thetaStart;


  var x, y;

    for ( y = 0; y <= heightSegments; y ++ ) {

        for ( x = 0; x <= widthSegments; x ++ ) {
        
            var u = x / widthSegments;
            var v = y / heightSegments;
            
            var vertex = this.vertices[(y * (widthSegments + 1)) + x];
            vertex.x = - radius * Math.cos( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
            vertex.y = radius * Math.cos( thetaStart + v * thetaLength );
            vertex.z = radius * Math.sin( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );


        }

    }

  this.verticesNeedUpdate = true;

};

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

I'm having trouble with my Rock Paper Scissors script. The console is showing an error message: "Uncaught SyntaxError: Identifier 'playerSelection' has already been declared."

Currently delving into the world of JavaScript, I've embarked on a project to create a console-based Rock Paper Scissors game. Here's the code snippet that I've come up with: <!DOCTYPE html> <html> <body> <script> ...

Effectively detect the 'scrollend' event on mobile devices

When implementing the -webkit-overflow-scrolling: touch; style on a mobile element, dealing with scroll events can be quite challenging as they are triggered by various actions such as 'flicking', 'panning' and when the scroll comes to ...

Looking for answers to a question about Handlebars in Node.js? Click on the following link to explore more

After successfully parsing my Shopify product items into a MongoDB database using a model, I am attempting to display them to the user using the express-handlebars package. However, I've encountered a small issue. I'm utilizing <td><a ...

Securing the connection between clients and servers through encryption

For my mobile client, I am using Xamarin, with node.js as my backend and MongoDB as the database. The main issue I am facing is how to securely store user data in the database. If I only do server-side encryption, there is a risk of hackers intercepting th ...

Nested tables in Datatables retrieving child table rows based on parent table

I have been struggling for the past three days to get my nested Datatables working properly. I have a parent table called MAINtable and a child table called adjlinesTable. The issue I am facing is that all lines from the adjlinesTable are being drawn to ...

Experimenting with AngularJS 1.6 class-oriented controllers through the lens of Jasmine/Karma testing

Struggling to use jasmine/karma to test my class-based controllers, unfortunately without any success. Most examples I come across are dated back to 2014 or older. I've made sure to load the angular and angular-mock files in my karma configuration fil ...

What is the reason that the 'mouseenter' event only applies to the initial element in each round of iteration within a spacebar loop?

My goal is to create an off-canvas menu within a template component. I found inspiration from this helpful article. The setup I have is quite common: A container tab where I loop through an items collection An item component that contains the off-canvas ...

Challenges with Tab navigation in React and Ionic 5

I am facing a challenge with getting the tabs navigation to function correctly. Here is my current code: App.tsx: const App: React.FC = () => <IonApp> <IonReactRouter> <IonRouterOutlet id="main"> < ...

Is it possible to test the JEST configuration for the node_modules even when it is turned

I'm currently integrating Jest tests into an existing codebase, but I've encountered an error that's giving me some trouble. Jest came across an unexpected token Typically, this means you're trying to import a file that Jest can& ...

Limits of the window in a d3 network diagram

I'm currently working with a network diagram that consists of circle elements and lines connecting them. However, I've run into an issue where sometimes there are so many circles that they extend beyond the edge of my screen (see image attached). ...

Connecting the input[date] and Moment.js in AngularJS

For the purpose of formulating a question, I have prepared a simplified example: ... <input type="date" ng-model="selectedMoment" /> ... <script> angular.module('dateInputExample', []) .controller('DateController', [& ...

Instructions for setting the value of a <input> element using scope

Having Trouble Passing Scope Value to Hidden Input Type I am facing an issue with passing the scope value to a hidden input type. Despite my efforts, I am unable to make it work as intended. Instead of displaying "PremiumVal", I need it to display "75" ba ...

React - Pagination with page number not functioning correctly

Currently, I am trying to set up pagination for my website. When I visit localhost:3000/profile/, it displays the first 3 elements correctly. The pagination also functions properly when I input a number like 3 for ${page}, taking me to page 3 seamlessly. H ...

Is there a way to use the property of an object to perform a merge sort, rather than relying on an Array?

Query About Sorting JSON Object in JavaScript In search of the most efficient method to sort a large JSON object based on a specific property, I turned to JavaScript. My initial thought was to utilize a merge sort algorithm for this task due to its speed. ...

Error: The module cannot be found due to a resolution issue when trying to import the Vue.js component './app/index.vue' using ES6 syntax

I am currently in the process of learning how to use webpack2 with vue.js and babel. However, I've encountered an error that I can't seem to resolve. I'm not sure what exactly is missing. ERROR in ./src/main.js Module not found: Error: Can ...

What is the reason behind the default disabling of bootstrap multiselect options?

Just diving into the world of bootstrap and I'm puzzled as to why my simple multiselect options aren't responding when clicked. UPDATE The first multiselect option on the test-site linked below is the one giving me trouble. I've tried it o ...

Conversion of UTC timestamp to a timestamp in the specified timezone

this.selectedTimezone="Pacific/Kiritimati"; //this value will come from a dropdown menu These records represent the data.body returned by an API call. Iterating through each record in the dataset: { We are creating a new Date object based on the ...

Issue with saving cookie from Express.js backend to Nuxt.js frontend

After successfully creating an authorization server using Express.js, I encountered a problem when trying to save the access and rotating refresh tokens as signed cookies. The issue arose from having separate backend and frontend servers with different dom ...

JavaScript exporting data to Excel and populating the top row with column

My code successfully exports a file via Excel without any errors. However, the issue I'm facing is that the exported Excel file contains a lot of unnecessary spaces. The problem is highlighted in the image provided: data from row 123 should be in col ...

Unlock encrypted files without allowing users to access them

Looking to incorporate model-viewer or three.js into my personal website to showcase 3D models. To display these models online, the client must retrieve files from the server (including the 3D mesh and texture images). However, I want to prevent my visito ...