Transitioning from using THREE.Geometry to THREE.BufferGeometry in THREE JS

Hey there, I'm a beginner in the world of Three JS and could really use some assistance. I stumbled upon a piece of code that allows me to generate a sphere with random points, but it seems like I need to make an adjustment. The problem is that THREE.Geometry has been deprecated, so I have to switch to using THREE.BufferGeometry instead.

The original code looks like this:

var pointsGeometry = new THREE.Geometry();

for (var i = 0; i < 1000; i++) {


    var vector = new THREE.Vector3();
     
    // some code for generating random values for vector.x, vector.y, vector.z etc

    pointsGeometry.vertices.push(vector);

}

Now, it seems like I should be using:

const pointsGeometry = new THREE.BufferGeometry();

But how can I push each vector into an array within an attribute called 'vertices' of pointsGeometry?

Within the loop, I tried:

pointsGeometry.setAttribute( 'vertices', new THREE.Float32BufferAttribute( vector ) );

I thought maybe I needed to manually create an array named 'vectorsArray', push each vector into it within the loop, and then set it afterwards:

pointsGeometry.setAttribute( 'vertices', new THREE.Float32BufferAttribute( vectorsArray ) );

Even though this approach does create a vertices attribute and adds an array of 1000, each value turns out to be NaN when it should be something like:

0: Object { x: -21.16441539757467, y: 112.77250047881454, z: -37.63426937227097, … } etc

I've double-checked that vectorsArray contains the correct values, but they don't seem to be passed into pointsGeometry.setAttribute( 'vertices'). Any idea what I might be doing wrong? Thanks a lot!

Answer №1

Here's one way to give it a try:

let geom = new THREE.BufferGeometry();

let pts = [];
let pt = new THREE.Vector3();

for ( let i = 0; i < 1000; i ++ ) {

    pt.random();
    pts.push( pt.x, pt.y, pt.z );

}

geom.setAttribute( 'position', new THREE.Float32BufferAttribute( pts, 3 ) );

Utilizing this geometry in conjunction with THREE.Points will result in a randomized point cloud.

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

Getting started with React Native project

Just dipping my toes into the world of React Native and looking for recommendations on the best starter kit/generator to kickstart my projects. Tried out "create-react-native-app" already, but curious if there are any other generators or kits out there tha ...

Ways to prevent the mousedown event from firing when reaching a particular number during incrementation

Is there a way to disable the mousedown event once a specific condition is met? For instance, in the code below, how can I prevent the Key and event from functioning when the temp reaches 30 $(document).ready(function() { var temp = 0; var to = n ...

Form Validation in JavaScript Continues to Submit Form Even When 'False' is Detected

Here is the issue at hand - I am restricted to using older browsers (IE8 and FF8) by corporate policy. Despite my research indicating otherwise, it seems like this might be the root cause of my troubles. My current setup includes PHP 5.5.12 on Apache 2.4. ...

JavaScript for loop similar to Python'sIn JavaScript, the

As someone who is new to programming, I primarily use Python but have now encountered a situation where I need to work with JavaScript for a project utilizing Phonegap. The query retrieved from the server looks like this: [["CompanyName", "lat", "long", ...

Injecting environment variables into webpack configuration

My goal is to set a BACKEND environment variable in order for our VueJS project to communicate with the API hosted there, but I keep receiving an error message saying Unexpected token :. Here is the current code snippet from our config/dev.env.js, and I&a ...

Leverage the Google Drive API for the storage of app-specific data

I'm currently developing a JavaScript application that runs on the client side and need to store a JSON object containing configuration details in a Google Drive Appdata file. Through the Google Drive API, I can successfully check for the file within ...

Initiate gapi.auth2 upon VueJs initialization

I am currently developing a Vue.js web application that allows users to connect with their Google accounts. The login process, both front-end and back-end, is functioning properly: the Google sign-in button is displayed, the user clicks on it, and their a ...

Troubleshooting issue with the spread operator and setState in React, Typescript, and Material-ui

I recently developed a custom Snackbar component in React with Material-ui and Typescript. While working on it, I encountered some confusion regarding the usage of spread operators and await functions. (Example available here: https://codesandbox.io/s/gift ...

Can you please explain the distinction between the statements var a = b = 2 and var a = 2; var b = 2;

Whenever I try to declare a variable within a function, I encounter an issue. var b = 44; function test(){ var a = b = 2; } However, the following code works without any problems: var b = 44; function test(){ var a; var b = 2; } The global ...

Tips on how to update a table following each button click during an ajax request

I am encountering an issue while trying to display data in a table by clicking on a search button. The problem arises when there is no data between the specified "Fromdate - Todate" range; the error message appears correctly. However, even after entering t ...

Implementing Image Tagging in JavaScript

Is there a way to create a drag selection box with JavaScript? I'm referring to the functionality where clicking and dragging the left mouse button draws a "wire" (not sure of the technical term) similar to your desktop. Ideally, this action would t ...

Utilizing ReactJS and PHP in Harmony

I'm currently facing issues with establishing communication between a fetch call in React and a PHP script running on my MAMP server. Unexpectedly, I am encountering errors that I am unable to troubleshoot effectively. This snippet depicts the code ...

Adjusting Grid Posts to a Consistent Size (Featuring Photo Posts with Captions Below)

I'm attempting to adjust the size of posts in grid mode on Tumblr. I've discovered that I can modify the width of each post using CSS. However, I'm struggling to locate where I can set the height of a grid post to a specific size. For inst ...

Webpack encountered an error due to a missing file path

When attempting to run Webpack, I keep encountering an error that simply states "ERROR in missing path." However, the error disappears when I remove the module key from the configuration provided below: module.exports = { ...

Utilize Meteor's ability to import async functions for seamless integration into method calls

Encountering an issue with this Meteor app where the error TypeError: vinXXX is not a function occurs when attempting to call an exported async function named "vinXXX" from within a method call in a sibling folder, which has been imported in the methods f ...

Replacing Class Attribute Using JavaScript

Is it possible to use Javascript to create a button that can switch the layout from wide to boxed by changing the class="container" to class="container-fluid"? If so, how would I go about implementing this functionality? ...

Understanding the method of recovering a nested promise

I am facing an issue with returning the result parameter from the getColumn function. Despite my attempts, it keeps logging as undefined. The connection function establishes a connection to a SQL database and retrieves a data set through a query. Is ther ...

Adjust slider value dynamically with jQuery

I am working on a UI slider that ranges from 0 million to 20000 million. My goal is to change the displayed value when it reaches 1000 to show '1 milliard', and at 20000 to show '20 milliard'. For instance, if the value is 1100 millio ...

adjusting the height of a div using jQuery UI's layout feature

Recently, I have been playing around with the adjustable grids plugin (jquery ui layout) to set the width of each div using the plugins. However, I've encountered a challenge when it comes to adjusting the height of inner divs within the layout. Speci ...

Dealing with distinct form actions for 'new' and 'edit' in Rails using simple_form, remote_true, and custom controller

I've been following a tutorial on how Ajax works with Ruby on Rails. However, I decided to take a slightly different approach. After setting up a scaffold for 'tasks' (including the controller, model, and views), I created a new controller c ...