What is the best way to add two strings with indexes and points to a Three.js BufferGeometry?

I have a massive database containing tens of thousands of objects with various types of information, including their 3D meshes. Currently, I am attempting to display these objects in web browsers using Three.js.

The object information is provided by another source, so I can only access it as it is given to me. When querying the database, I receive data such as the following:

indices: [data here];
points: [data here];

Now, my challenge lies in integrating this data into the BufferGeometry of Three.js. Referring to the example at , it seems that I need to use the following snippet:

var triangles = 276;
var geometry = new THREE.BufferGeometry();
var indices = new Uint32Array(triangles * [number]);

But how do I go about inputting my indices string/array and points into the Buffer?

EDIT

Thanks to the suggestion from @mlkn, I was able to resolve my issue. Since my indices and vertices are sourced from a database, I modified the string on the server-side using PHP. This way, the client-side does not need to handle any loops. My code now appears as follows:

var vertices = new Float32Array([-155.751724243164,106.251846313477,-20000,...]);
var indices = new Uint16Array([0,1,2,3,0,2,4,5,2,...]);

var geometry = new THREE.BufferGeometry();
geometry.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
geometry.setIndex( new THREE.BufferAttribute( indices, 1 ) );
var material = new THREE.MeshBasicMaterial({color: 0xffbb0f});
var mesh = new THREE.Mesh( geometry, material );
scene.add(mesh);

This solution works perfectly!

Answer №1

Initially, it is important to convert strings into typed arrays for efficient processing. Assuming the data is accurate:

var i, length;
var tmp = indicesData.split(';');
var indices = new Uint16Array(tmp.length);
for (i = 0; i < tmp.length; i++) {
    indices[i] = parseInt(tmp[i]);
}

tmp = verticesData.split(';');
var vertices = new Float32Array(tmp.length);
for (i = 0; i < tmp.length; i++) {
    vertices[i] = parseFloat(tmp[i]);
}

Subsequently, generate geometry/material, create a mesh, and append it to the scene:

var geometry = new THREE.BufferGeometry();
geometry.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
geometry.setIndex( new THREE.BufferAttribute( indices, 1 ) );
var material = new THREE.MeshBasicMaterial({color: 0xffbb0f});
var mesh = new THREE.Mesh( geometry, material );
scene.add(mesh);

Upon receiving new data, you can include additional meshes or update buffers of existing geometries. Verify the data's size correctness and ensure that no indices exceed the vertices buffer range to prevent runtime errors.

Furthermore, check out this informative article about indexed drawing.

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

Sort div elements based on checkbox filters

I have a set of checkboxes that I want to use for filtering purposes. <div class="filter"> <div class="checkbox"> <label><input type="checkbox" rel="canada"/>Canada</label> </div> <div class="chec ...

Capture data from a Telegram bot and store it in a Google Sheet

I am trying to use a spreadsheet through a Telegram bot as a TODO list so that when I input something on my phone, it is saved in the spreadsheet. (I'm following this tutorial https://www.youtube.com/watch?v=XoTpdxbkGGk, which seems accurate with Goog ...

Retrieving an HTML page from one location and automatically populating textboxes with preexisting values on the receiving end

I'm currently facing a dilemma. Here's the issue: I need to load an HTML page (let's call it test.html) when a button is clicked on another page (referred to as home page). The test.html page has input boxes that I want to populate with p ...

React Router's default component for nested routes

In React Router, I am facing a challenge with nested Routes <Route path='about' component={{main: About, header: Header}}> <Route path='team' component={Team} /> </Route> Currently, the Team component is displayed ...

safari application is processing audio recordings with incorrect mime type as application/octate-stream

When attempting to capture a user's recording from a microphone using the Media Recorder API, everything works smoothly in Chrome. However, when trying to record in Safari, the MIME type is reported as application/octet-stream. I need to transmit the ...

The div element is overflowing beyond the available space

I have already searched for a solution to this question, but despite trying everything, I have not been able to find one. Therefore, I am asking again, my apologies :-). My challenge is to make a row fill all the remaining screen space on the page, but no ...

Trigger the UseEffect() function in a Material UI Dialog only when the dialog is open

In my project, I have a parent component that includes a Material UI Dialog as a child component. The purpose of this dialog is to fetch and display data from a REST API. Currently, I am using the UseEffect() function in the Dialog component to achieve th ...

Exploring the functionalities of MongoDB through Populate, Aggregate, and Un

My user model contains the following data structure: { _id: object, name: string, quotes: [ { quote: string, createdAt: date } ], friends: [ { _id: object } ] (referring to the _id from the user model) } My goal is to extract and return the following ...

What are some common reasons for nodejs to fail in establishing a connection with

In my Node.js code snippet, I am using the following: "use strict" var MongoClient = require('mongodb').MongoClient; var UserGateway = function(mongoURL) { this.mongoURL = mongoURL; } UserGateway.prototype.connect = function() { return ne ...

Get the @html.dropdownlistfor filtered for me

Currently, I am working on a project that requires me to filter the options in my second dropdown list based on the selection made in the first dropdown list. While the concept is clear, implementing it becomes tricky as I lack expertise in jQuery or JavaS ...

"Maintaining the jQuery carousel slider above the lightbox image for a seamless user experience

Being a newcomer to jQuery, I am currently relying solely on plugins. I recently installed a carousel slider that allows manual sliding to view images accompanied by text information underneath. By clicking on "more" at the bottom of the image/text box, an ...

Acquiring JSON data nested within another JSON object in D3

After looking at this reference, I am attempting to integrate similar JSON data into my webpage. The challenge I am facing is that my JSON contains nested JSON. Here is an example of how my JSON structure looks: { "nodes": [ {"fixed":true,"classes": null, ...

How to use the route.navigate() method in Angular 9 to open a URL in a new tab with a query string

When a button is clicked within a table in our application, I have to open a new tab with details of a specific record from the table. Currently, the code I am using navigates to a new URL and uses resolvers to fetch data from the backend on the new page. ...

Is it possible to implement addEventListener.delay() functionality?

Hello, I am working on a Qualtrics code that utilizes jQuery. I am looking to incorporate a delay function for event listening in my code, which currently allows key presses as inputs. Specifically, I need to disable the keys for the initial 2000ms before ...

Showing or hiding child content based on the selected state of a radio button

This is a follow-up question from a previous one on changing check boxes to radio buttons. Now, I need to display child radio buttons and change the background color when the parent radio button is active. The child radio buttons should be hidden and the b ...

What is the best way to save input field values when a reload occurs before submitting?

html: <form name="shippingForm" class="form-horizontal" role="form"> <div class="form-group"> <p class="control-p col-sm-2" for="Address">Address Line1<b class="h4-color">*</b>:</p> <div class="col-sm-10"& ...

Exploring the Django admin panel's settings for MEDIA_URL and database URL

Recently, I migrated a website from one server to another. Now, I am in the process of completing the migration by updating the database on the new server. However, I encountered a strange issue after making the necessary changes - the images are not disp ...

What is the best way to implement rate limiting or throttling on a Strapi API?

Our company relies on a simple strapi API implemented in node.js and hosted on Heroku. Despite our efforts, we have not been able to find a solution to implement rate limiting, as it appears that Heroku does not offer throttling add-ons and strapi lacks bu ...

Problem with npm cache causing failure to rebuild the project

While working with Laravel 5.4 and Vue.js, I encountered a syntax error between the 'created' and 'methods' sections in my code. Although I initially fixed this error, it continued to persist. Even after restarting Apache, the error re ...

Winston is set up to only record .info errors and does not save any errors to a file or a MongoDB database

Currently, I am utilizing express-mongoose as my backend framework and winston as my logging tool. However, I have encountered an issue where winston only seems to log info messages and not errors. The logs can be found in server.log https://i.stack.imgur. ...