Exploring vertex geometries in BufferGeometry within Three.js

Initially, I thought I wanted a simple, quiet solution, but it turned out to be more complex than I anticipated.

All I'm aiming for are meshes on every vertex of a THREE.BufferGeometry (specifically a sphere)!

You can view the CODEPEN here: codepen.io/cheesyeyes/pen/YwBZGz

To visualize, here's a reference image of what I'm aiming for: this is what I want to have

This snippet showcases my buffer geometry:

var bufferGeometry = new THREE.SphereBufferGeometry(1,1,1);

My objective is to access the positions of the vertices:

bufferGeometry.getAttribute('position',0);

What I'm struggling with is properly reading out the positions of all (in this scenario, 5) vertices of my bufferGeometry.

I've attempted this approach which does function, but I find it quite messy:

vertexMesh.position.x=position.array[0];
vertexMesh.position.y=position.array[1];
vertexMesh.position.z=position.array[2];

for(i=12;i<position.array.length-9;i=i+3)
            { 

            //vertexGeometry
            var vertexGeometry = new THREE.SphereGeometry(0.1,32,32);

            var vertexMaterial = new THREE.MeshBasicMaterial({color:0xff0000});   

            var vertexMesh = new THREE.Mesh(vertexGeometry,vertexMaterial);    

            vertexMesh.position.x=position.array[i];
            vertexMesh.position.y=position.array[i+1];
            vertexMesh.position.z=position.array[i+2]; 

             scene.add(vertexMesh);

            }

I'm having trouble understanding how this array is structured. Any assistance would be greatly appreciated! Thank you!

Answer №1

BufferGeometry organizes vertex data in a linear array, where each set of three indices corresponds to a single Vector3 (representing a vertex's position). For example, if you have the following array:

var verticesArray = [0, 0, 0, 0, 1, 0, 1, 1, 1];

You should interpret it as (x, y, z) triplets:

Vertex 1: (0, 0, 0)
Vertex 2: (0, 1, 0)
Vertex 3: (1, 1, 1)

The concept is illustrated in the documentation of three.js; refer to the BufferGeometry page for more details, and you may find the answer to your question within that information:

// Position vector components for each vertex are stored in contiguous buffers
for ( var i = 0; i < vertexPositions.length; i++ )
{
    vertices[ i*3 + 0 ] = vertexPositions[i][0];
    vertices[ i*3 + 1 ] = vertexPositions[i][1];
    vertices[ i*3 + 2 ] = vertexPositions[i][2];
}

Considering this, have you thought about why you're utilizing BufferGeometry? It should be used only when absolutely necessary for optimization purposes. BufferGeometry stores data in raw number arrays, making it more challenging to access and modify compared to the Geometry class, which conveniently stores vertices as Vector3 arrays. Unless you have a strong reason to do otherwise, stick with Geometry over BufferGeometry.

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

Interacting with YouTube Data API without requiring user input

I'm currently developing a music website that enables users to create YouTube playlists. Initially, I experimented with JavaScript: https://developers.google.com/youtube/v3/code_samples/javascript The procedure involves an initial authorization ste ...

Guide on accessing the final value of a text file using JavaScript

My server side script outputs results in a txt file, where the values stored look like this: 1 2 5 7 10 In order to create a real-time progress bar, I need to fetch the last value from the file using an ajax request while updating the txt file with the l ...

Prevent form submission with jQuery during validation process

Currently, I am working on validating a form using jQuery. My main objective now is to have the submit button disabled until all fields are correctly filled in. To achieve this, I have implemented the following approach: http://jsfiddle.net/w57hq430/ < ...

Functionality that can be utilized repeatedly

I've been struggling to implement a feature for repeatable blocks in my web form. The issue I'm facing is that when I click the buttons, nothing happens even though they work fine when tested in the console. I've been stuck on this problem f ...

Leveraging backstretch.js in combination with blur.js (or equivalent)

Query Update I provided a response to the query below, however, I am experiencing some lag and a noticeable stepped transition between images. Can anyone offer advice on how to optimize this issue? Perhaps it would be better to go back to using a static ...

Clicking randomly on the page to conduct test trials

Currently, I am seeking a way to ensure that my web page is flawless, specifically in terms of broken paths. Given that my website will utilize a touch screen interface, it is crucial to test the entire page thoroughly. While I have limited experience in a ...

Utilizing v-model alongside various JavaScript plugins within a single select element

I've incorporated the jQuery plugins select2 and datepicker into my project, utilizing custom directives for them. Everything was functioning smoothly until I attempted to retrieve the selected value using v-model, which resulted in a failure to bind ...

Transform the Curly Braces within a string into an HTML span Element using JSX

Looking to parameterize a string like 'Hello {name}, how are you?' in React Component? Want to replace curly braces with variable text and highlight it using span/strong tag. Here's an example of the desired final result: Hello <span cla ...

Tips for implementing a settimeout function in a VUEJS script

I'm currently working on my first Vue.js application and I'm facing an issue with the initial data upload in the script. After modifying the data received from a database call, I encounter an error during the page's initial load which resolv ...

"error: unable to retrieve message channel name, result is undefined

Hey there, I'm currently experiencing an issue while trying to retrieve the name of a channel that I created. Strangely enough, it's returning undefined, even though I am certain that the channel exists. Let me share with you the code snippet wh ...

What is the best way to eliminate excess white space on the right side in WordPress for a mobile perspective?

Is there a quick way to identify which element has shifted beyond the border? It seems like there is excess margin. How can I pinpoint the issue? The link to the broken page on mobile is I applied this style * {border: 2px solid red;} and no elements shif ...

Unable to focus on the same DOM element multiple times - Vue.js

Apologies for any syntax errors, although the code is functioning perfectly. There may have been a mistake during the copying process. Situation: I am facing an issue with a component called 'dropdown', which is repeated three times using v-for= ...

What is the best way to neatly import multiple images in Next.js?

I have a dilemma involving 10 images located in my public directory that I need to use in a component. Instead of individually importing each image like this: import imgurl1 from "../../public/celsius.gif"; import imgurl2 from "../../public/ ...

Revamping ng-model in AngularJS

Here is my scenario: cols = [{field="product.productId"},{field="product.productPrice"}]; data = {products:[{product:{productId:1,productPrice:10}, {product:{productId:2, productPrice:15}}]} This is what I want to achieve: <div ng-repeat="product in ...

Working with Firestore database in React Js may result in an infinite loop

I have connected my React JS app to Cloud Firestore and I am facing an issue with displaying the objects in my JavaScript file. Even though I only have one object in Firestore, it seems to be reading in a loop and I can't seem to identify the cause. ...

Tips for preventing curved links in a radial tree diagram created with d3

I am currently utilizing this code to generate a radial tree diagram for my data. However, I am looking to make a modification to eliminate curved links. My preference is for linear connections instead of curved ones as the curved links can make the visual ...

Countdown Widget - Transform "0 Days" to "Today" with jQuery

I am currently working on a countdown page using the Keith Woods Countdown jQuery plugin which can be found at . Here is the code I have implemented so far: $(document).ready(function () { var segera = new Date(); segera = new Date(segera.getFullYear(), 2 ...

The performance of Node.js Knex SQL insert queries leaves much to be desired

I have created a simple login system that performs the following actions: Verifies if the username exists (works fine) Adds the username to the database After adding the username, I retrieve the id of the added username. Everything works smoothly on the ...

Preload-webpack-plugin does not support pre-fetching files

I have a query about prefetching and preloading content. In my vue app, I noticed that after building, I have duplicate files loaded in my dist/index.html file. Here is an example: Additionally, the "scripts" are not being preloaded/prefetched as expec ...

Should one consider building a web application by relying on browser AJAX requests a sound practice?

When developing a standard MVC web application, is it acceptable to rely heavily on ajax calls for rendering content? For instance, looking at Facebook as an example, they dynamically load a majority of their content. Should we emulate this strategy? What ...