Implementing changes to velocity and acceleration within BufferGeometry using Three.js

let pointsM = [];

  let img = new THREE.TextureLoader().load( 'image' );
  let pointMaterial = new THREE.PointsMaterial({
  map: img,transparent:true
  });
  let bufferGeometry = new THREE.BufferGeometry()
  for (let i = 0; i < 10; i++) {
    pointsM.push( new THREE.Vector3(Math.random() * 20 - 10, Math.random() * 20 - 10, Math.random() * 20 - 10));
  }
  bufferGeometry.setFromPoints(pointsM);
  bufferGeometry.computeVertexNormals();

  pointsmesh = new THREE.Points(bufferGeometry, pointMaterial);
  scene.add(pointsmesh)

The section above is functioning correctly, it has been converted from Geometry to BufferGeometry

My goal is to introduce velocity and acceleration to the previously created points, then adjust them within the render loop

I would like to modify the following code to function with buffer geometry as it currently operates with Geometry

  render(){

  Geo.vertices.forEach(p => {
   p.vel += p.accel; 
   p.z -= p.vel;

  });
  Geo.verticesNeedUpdate = true;
}

Thank you for your assistance

Answer №1

Below is a code snippet that demonstrates how to work with vertex velocity and acceleration stored in separate arrays.

// Create BufferGeometry

const bufferGeometry = new THREE.BufferGeometry();
const vertices = [];
const velocities = [];
const accelerations = [];

for (let i = 0; i < 10; i++) {
    // Generate random vertex coordinates
    vertices.push(Math.random() * 20 - 10); // x
    vertices.push(Math.random() * 20 - 10); // y
    vertices.push(Math.random() * 20 - 10); // z
    
    velocities.push(0); // Initialize velocity
    accelerations.push(Math.random()); // Random acceleration
}

bufferGeometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));

// Update positions based on velocity and acceleration

const positionAttribute = geometry.getAttribute('position');

for (let i = 0; i < positionAttribute.count; i++) {
    const z = positionAttribute.getZ(i);
    
    const vel = velocities[i];
    const accel = accelerations[i];
    vel *= accel;
    velocities[i] = vel;
    z -= vel;
    
    positionAttribute.setZ(i, z);
}

positionAttribute.needsUpdate = true;

render();

Note: In this specific case, computeVertexNormals() is not required as points and lines do not need vertex normals.

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

Accessing variables from an external script in jsdom

Here is a simple example of jsdom code using the script parameter. Despite my best efforts to reference external JS files, I keep running into this issue: ReferenceError: exVar is not defined Does anyone know what might be causing this problem and how ...

Troubleshooting Problem with Parsing Float in Angular

Currently, I am facing an issue while using a filter to calculate totals from dynamic data in ng-repeat. The problem lies in my inability to limit the decimals to 2 places. Below is the code snippet of my filter: app.filter('sumByKey', function( ...

Is there a way to trigger a jQuery function using the onRowSelect event in ajax?

I have a jQuery function that is triggered by the 'change' event when a value is selected in a <select> element. However, I no longer need the <select> because I will manually set the value within my jQuery function. My page utilizes ...

Locating the right selector for adding a div to its parent element in jQuery

I have come across an interesting HTML structure: <div class="dropdownedit"> <div class="dropbtn">textxyz</div> <div class="dropdown-content" style="display: none;"> <div href="#" class="ocond" id="text1">text1</div> &l ...

Separate every variable with a comma, excluding the final one, using jQuery

I've developed a script that automatically inserts checked checkboxes and radio options into the value() of an input field. var burgerName = meat + " with " + bread + " and " + onion + tomato + cheese + salad; $('#burger-name').val(burger ...

Vue Functional Calendar - Easily manipulate selectedDates with the power of v-model

I am trying to set specific dates as selected in the vue functional-calendar (version 2.8.87) component (https://github.com/Digital-Threads/vue-functional-calendar). I have set up the v-model and can successfully detect dates selected by a mouse-click. Ho ...

Ways to fix a "define is not defined" issue when utilizing jasmine karma with compiled typescript for component testing

I'm faced with an issue in my typescript app where the compiled code is stored in a single file named myjs.js within the js folder. Additionally, I have karma jasmine configured on my workspace. Inside myjs.js, there's a segment of code like thi ...

Guide to locating the recursive function in Node.js runtime

As a beginner in the world of node and angular development, I have encountered a major issue - "FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory". Is there anyone who can help me identify the function ...

Choosing Text with JavaScript

I am looking to enhance the appearance of text on an HTML page by making it bold. I have implemented the following code: <script type="text/javascript" > function getSelectedText(){ if(window.getSelection){ ; return window.getSelect ...

Using Angular 7 to trigger a file download from the server with proper Authorization

I'm facing some challenges while trying to implement a specific functionality with Angular 7 and I could use some assistance. Here's the scenario: We have a service that returns static files (PDFs), but it requires authorization to access the AP ...

What is the best way to navigate to the top of a form panel?

I'm currently developing a Sencha Touch mobile app. I have a form panel with multiple fields, so I made it scrollable. However, every time I open the screen (panel), scroll down, navigate to other screens, and then return to the form panel, it stays s ...

Slower CSS rendering as Javascript finishes its tasks

I am currently dealing with a jQuery plugin that is quite large and complex, but I will not be sharing it here to keep things simple. The issue I am facing is relatively straightforward, so I will focus on the essential code snippets: There is a click eve ...

Mocking the fetch() function in Jest to retrieve the response's blob data

Is there a way to mimic the fetch function by adding blob extraction using jest.fn().mockimplementation() but without relying on fetch-mock or jest-fetch-mock? fetch(url) .then((response) => response.blob) .then((data) => imageHandler(data)) ...

Loading items into multiple containers using PHP AJAX pagination

I'm looking to implement a "load more" button on my website, but I'm facing a challenge due to my three-column layout. I want to load more items into their respective parent elements without disrupting the design. While I've used infinite aj ...

Searching for multiple array elements based on their values can be achieved by using various techniques

I am trying to find a way to select multiple elements from an array that share the same value. When I use array.find(), it only returns the first element that matches the condition. For example, in the code below, only "Donald Trump" is displayed in the co ...

What are the distinctions between utilizing util.inherits() and prototypes for inheritance in node.js?

The method util.inherits() allows for the inheritance of methods from one function to another. Prototypes are also a way to achieve inheritance in JavaScript. I am wondering when it is best to use .inherits() versus changing the prototype chain. Any advi ...

What are the benefits of using Lifery Ajax URLs?

I'm currently using the Grails portlets plugin and I'm exploring how to properly route AJAX methods. It seems like <portlet:actionURL> is only able to map to methods that return models for GSPs, while <portlet:resourceURL> doesn&apos ...

I'm curious how my useContext component is able to retrieve the value from App.js

I am currently working through a tutorial and here is an example code snippet: App.js import React from 'react'; import { BrowserRouter as Router, Route, Link } from "react-router-dom"; import { Index } from './pages/index'; ...

What is the best way to tile textures in Three.js without causing distortion?

Despite trying various solutions, I am still facing an issue where my textures appear to be stretched or scaled based on the mesh size. In a bid to seek a resolution, here are some key points to note: All textures have dimensions of 64x64 pixels I have p ...

Challenges Encountered with jQuery/AJAX Form Processing

I am currently facing an issue with two files in my project. The main file, index.php, contains JavaScript code within the head section. Here is the snippet of JavaScript: <script src="http://code.jquery.com/jquery-latest.min.js" type= ...