Adjusting the vertices of a threejs geometry over time

I am currently working on updating the vertices of a SphereGeometry based on the current value of performance.now(), incorporating perlin noise into the equation.

The expected result is shown in this image: https://i.sstatic.net/zwAAm.gif

To see my code in action, check out this working example: http://jsfiddle.net/8L4bktyw/1/

However, I am encountering an issue where the vertices are not updating visually, even though the colors on the MeshNormalMaterial appear to change as if they have been updated.

An interesting observation is that upon refreshing or running the fiddle multiple times, the vertices eventually do update as intended.

I am wondering if I may be incorrectly updating the geometry. Any insights would be greatly appreciated!

Answer №1

The vertices aren't updating for an unknown reason

Initially, you instructed the normal vectors to be updated:

sphere.geometry.normalsNeedUpdate = true;

However, you forgot to update the vertex coordinates as well:

sphere.geometry.verticesNeedUpdate = true;

Refer to THREE.Geometry.


Check out the example below, which demonstrates how the advice from the answer is implemented in the original code of your question:

var renderer = new THREE.WebGLRenderer({ canvas : document.getElementById('myCanvas'), antialias:true});
renderer.setClearColor(0x7b7b7b);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 45, window.innerWidth/window.innerHeight, 0.1, 1000 );
camera.position.z = 5;

var sphere_geometry = new THREE.SphereGeometry(1, 128, 128);
var material = new THREE.MeshNormalMaterial();

var sphere = new THREE.Mesh(sphere_geometry, material);
scene.add(sphere);
window.onresize = resize;

var update = function() {

  var time = performance.now() * 0.003;
  
  //go through vertices here and reposition them 
  var k = 3;
  for (var i = 0; i < sphere.geometry.vertices.length; i++) {
      var p = sphere.geometry.vertices[i];
      p.normalize().multiplyScalar(1 + 0.3 * noise.perlin3(p.x * k + time, p.y * k, p.z * k));
  }
  sphere.geometry.computeVertexNormals();
  sphere.geometry.normalsNeedUpdate = true;
  sphere.geometry.verticesNeedUpdate = true;
}

function resize() {
    
    var aspect = window.innerWidth / window.innerHeight;
    renderer.setSize(window.innerWidth, window.innerHeight);
    camera.aspect = aspect;
    camera.updateProjectionMatrix();
    //controls.handleResize();
  }

function animate() {
  //sphere.rotation.x += 0.01;
  //sphere.rotation.y += 0.01;

  update();
  /* render scene and camera */
  renderer.render(scene,camera);
  requestAnimationFrame(animate);
}

requestAnimationFrame(animate);

(function(global){
  var module = global.noise = {};

  function Grad(x, y, z) {
    this.x = x; this.y = y; this.z = z;
  }
  
  Grad.prototype.dot2 = function(x, y) {
    return this.x*x + this.y*y;
  };

  Grad.prototype.dot3 = function(x, y, z) {
    return this.x*x + this.y*y + this.z*z;
  };

... (remaining content omitted for brevity)
<script src="https://threejs.org/build/three.min.js"></script>
<canvas id="myCanvas"></canvas>

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

Basic use of AJAX for sending the value from jQuery's datepicker

As a novice in JavaScript and AJAX, I'm hoping for your patience as I navigate my way through. In the scope of this project, I am utilizing the CodeIgniter framework. My objective is to implement a datepicker and transmit its value via AJAX. Below is ...

Guide to verifying a value within a JSON object in Ionic 2

Is there a way to check the value of "no_cover" in thumbnail[0] and replace it with asset/sss.jpg in order to display on the listpage? I have attempted to include <img src="{{item.LINKS.thumbnail[0]}}"> in Listpage.html, but it only shows the thumbna ...

What steps are needed to integrate the FreshworksWidget in a next.js project?

When referring to the Freshdesk documentation, there is a provided script that looks like this: <script> window.fwSettings={ 'widget_id':12000000025, 'locale': 'en' }; !function(){if("function"!=typeof w ...

What is the best way to remove a nested JSON key in a dynamic manner

Here is a sample json for reference: { "search": { "facets": { "author": [ ], "language": [ { "value": "nep", "count": 3 }, { "value": "urd", "count": 1 } ], "source": [ { "value": "West Bengal ...

Unable to access variables beyond the function scope results in an undefined value

I'm currently working with an npm package that shortens URLs but I'm struggling because there isn't much documentation available. The package takes the "this.src" URL and shortens it, but when I try to use the "url" element in HTML, it retur ...

What is the reasoning behind AngularJS 2 HTTP Post only allowing string data as input?

Can someone explain to me why the HTTP Post method in AngularJS 2 (2.0.0-beta.13) only accepts string data as body, unlike in AngularJS 1 where it can accept a JavaScript object? AngularJS-1: $http.post(someUrl,someObject) AngularJS-2: http.post(someUr ...

Using Javascript to dynamically add and remove elements on click

Whenever I click on a link, I am able to generate input, label, and text. However, I want to be able to delete these elements with the next click event on the same link: I've tried updating my code like this: var addAnswer = (function() { var la ...

What is the reason behind the HTML button producing the 'ul' node just once even after being clicked multiple times?

Just to clarify, the code below is successfully doing what I intended. I am currently developing a basic todo application. Each time the button on the page is clicked, it triggers the createListElem() function. The initial line of code in this function ap ...

Learn how to run javascript code using Nodejs child_process and Meteor

Is it possible to use the child_process module to execute Meteor server-side code instead of just Linux commands? I am interested in using spawn to create a separate process for running my loop. The loop involves logging a message every minute. myLoop(){ ...

"Enhance your website with a backspace button using jquery - here's

Recently, I delved into the world of jQuery and decided to test my skills by creating a jQuery calculator. Everything worked perfectly except for the backspace button. This is what I tried: if (value === backspace) { $(input).val($(input).val().substring ...

Issues with pagination and navigation in Joomla when using the Swiper JS carousel with Bootstrap 5

Initially, I attempted to incorporate Swiper JS into my Joomla 4 template using Twitter Bootstrap 5. I connected it from CDN. If you visit this link: , you will see the desired layout from a Figma design: Screenshot However, I encountered some issues: A ...

The submission of the form is prevented upon updating the inner HTML

I am in the process of creating a website that will incorporate search, add, update, and delete functionalities all on a single webpage without any modals. The main focus of this webpage is facility maintenance. Adding facilities works smoothly; however, i ...

What is the best way to conceal a class that possesses a particular value?

What is the best way to conceal the text located within a class called amount using either javascript or php? <span class="amount">$0.00</span> I have attempted the following approach without success <script language="javascript"> $ ...

What steps need to be taken to set up Node.js to accommodate requests from external sources beyond just localhost?

After creating an application using NextJs, I successfully built it and ran it on a node server by executing 'npm run start' in Powershell. Everything works perfectly when accessing it locally through port 80. However, my Windows Server 2019 does ...

I have written code in JavaScript, but now I am interested in converting it to jQuery

What is the best way to convert this to jQuery? showDish("balkandish1") function showDish(dishName) { var i; $(".city").css('display', 'none'); $("#" + dishName).css('display', 'block'); } ...

Styling JSON data in a jQuery Mobile list display

Currently working on a jQuery Mobile application that aims to achieve a similar output as shown in this image: https://i.sstatic.net/CfUHB.png The HTML code responsible for generating the image is as follows: <ul data-role="listview" data-i ...

Is there a method in AngularJS to have $http.post send request parameters rather than JSON?

I have come across some older code that utilizes an AJAX POST request using jQuery's post method. The code looks something like this: $.post("/foo/bar", requestData, function(responseData) { //do stuff with response } The request ...

Switching Out Bootstrap Dropdown with Dropup (Varying functionality on two almost identical implementations)

In the midst of my project on Github Pages, I encountered an interesting issue involving replacing a bootstrap .dropdown with .dropup when the div's overflow-y: scroll causes the dropdown menu to be cut off or overflow. The functionality can be viewed ...

What sets server-side development apart from API creation?

As I dive into the world of web development, I find myself facing some confusion in my course material. The instructor has introduced concepts like promise objects and fetch, followed by axios, and now we're delving into the "express" package for buil ...

The React-native application initialized using npx create-react-app encountered an error and could not launch

Hello there, A couple of months back, I developed an app using create-react-app. However, when I tried to run it with npm start, I encountered the following error message: react-scripts start It seems like there's an issue with the project depende ...