Combining two geometries with indexes into a BufferGeometry

Currently, I am utilizing a fixed set of data that contains indices, vertices, and colors, along with multiple instances of THREE.Geometry to populate a scene with objects. This process is quite slow, as it involves adding and removing numerous objects at once or merging them together.

However, by employing a single THREE.BufferGeometry, I can leverage _gl.bufferSubData to add and remove objects, which should ideally have minimal impact on performance.

The challenge I am facing lies in implementing this solution. While I have successfully implemented the bufferSubData function, I am struggling to incorporate two sets of data within the same BufferGeometry. It seems that the data does not seamlessly flow into each other (due to being separate objects), resulting in both sets using the same indices. You can observe the outcome in this image.

I have created a JSFiddle that contains an array named section with the chunk data. If anyone could review it and modify it to incorporate both sets of data, it would be highly appreciated:

http://jsfiddle.net/dUqwT/

Additionally, I have been unable to discern the purpose of the index offset. If someone could provide a link or explanation regarding its usage, it would be immensely beneficial.

Thank you for the assistance!

Answer №1

After figuring out the process, I have made some changes to the JSFiddle link:
http://jsfiddle.net/dUqwT/1/

The solution turned out to be simpler than expected, and it was not related to the index offset (still unsure about its purpose). The key was to ensure that each array appends correctly to avoid overwriting positions, indices, and colors. This was achieved by using two variables set to the appropriate lengths.

Considering that the objects added to the BufferGeometry will be dynamic, I plan to allocate a specific portion of the buffer to each object instead of setting two variables to the length. This approach will allow for removal and modification of each object using _gl.bufferSubData.

for (var chunkID = 0; chunkID < 2; chunkID++) {

    var chunkIndices = section[chunkID].indices;
    var chunkVertices = section[chunkID].vertices;
    var chunkColors = section[chunkID].colors;

    var sectionXPos = chunkID * 32;
    var sectionYPos = 0;
    var sectionZPos = 0;

    // Add indices to BufferGeometry
    for ( var i = 0; i < chunkIndices.length; i ++ ) {
        var q = chunkIndices[i];

        var j = i * 3 + iLength;

        indices[ j ]     = (q[0] + vLength / 3) % chunkSize;
        indices[ j + 1 ] = (q[1] + vLength / 3) % chunkSize;
        indices[ j + 2 ] = (q[2] + vLength / 3) % chunkSize;

    }

    // Add vertices to BufferGeometry
    for ( var i = 0; i < chunkVertices.length; i ++ ) {
        var q = chunkVertices[i];

        var j = i * 3 + vLength;

        // positions
        positions[ j ]     = q[0] + sectionXPos;
        positions[ j + 1 ] = q[1] + sectionYPos;
        positions[ j + 2 ] = q[2] + sectionZPos;

        // colors
        var hexColor = chunkColors[i / 4];
        color.set(hexColor);

        colors[ j ]     = color.r;
        colors[ j + 1 ] = color.g;
        colors[ j + 2 ] = color.b;
    }

    iLength += chunkIndices.length * 3;
    vLength += chunkVertices.length * 3;
}

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

Retrieve all elements from an array using jQuery

How do I extract all the elements from the array outside of the function? $.each(Basepath.Templates, function(i){ templateArray = new Array({title: Basepath.Templates[i].Template.name, src: 'view/'+Basepath.Templates[i].Template.id, descri ...

Issue with grunt-closure-tools: ERROR - There is a parse error due to using a reserved word as an

Currently, I am utilizing the "grunt-closure-tools" tool. Initially, minifying a simple JS file was successful without any issues. However, encountering an exception arose when attempting to minify either the AngularJS or Bootstrap libraries: Error: Comm ...

Parsing JSON data received through a URL using Ruby

When attempting to send a JSON object from JavaScript to Ruby, I am encountering issues with parsing it in Ruby. Despite trying various methods and conducting extensive research, I have not been able to find a solution. It is important to note that I am r ...

Dividing the JSON dataset into smaller segments

Here is an example demonstrating how to split the JSON data: var data = [ { BankName: 'SBI', IFSC: 'SBIN0002688' }, { BankName: 'ICICI', IFSC: 'ICIC0003931', MICR: '500229094'}, { BankName: 'RBI ...

Interactive calendar feature displaying events upon hovering over a date

I need some assistance with displaying a drop-down list on full calendar events when hovering over the events. Can someone provide guidance? Here is a glimpse of what I currently have: https://i.sstatic.net/fZXMH.png I've attempted setting the z-in ...

The hidden textbox loses its value when submitting

I have a form with fields and a RequiredFieldValidator. Additionally, I have another textbox that is hidden which gets populated by a JavaScript function: <script type="text/javascript"> $(document).ready(function (sender, args) { $("#myTextFiel ...

Tips for including an external babel JS (ES6) file in an HTML document:

To include the babel js file in an HTML file, take a look at the code snippet below: <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudfl ...

What is the method for utilizing Jquery to target the width value of an inline property?

I am struggling with selecting the "style=:width: 10%" value using jquery for a progress bar element in bootstrap. <div class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 10% ...

Testing Vue watcher that observes a computed property in VueX: Tips and Tricks

Consider the code snippet below: import { mapState } from 'vuex'; import externalDependency from '...'; export default { name: 'Foo', computed: { ...mapState(['bar']) }, watch: { bar () { exter ...

Mistakes in validating email form

I'm having some difficulties creating a basic contact form. I'm following the guidelines outlined in and the initial step involves using JavaScript to highlight the input box with a red border when invalid input is entered. Unfortunately, the ...

Difficulty in sharing cookies among subdomains

I have successfully stored my visitors' style sheet preference in a cookie, but I am facing an issue with sharing the cookie across subdomains. Even after specifying the domain, the cookie does not seem to be shared. What could be causing this proble ...

What is the reason for this JavaScript code not activating the input field (aspx)?

Thank you for taking the time to review this. I understand that for most of you client side scripting experts, this task is a breeze. However, all I am trying to achieve is to display a message at the top notifying the user that their click has been regist ...

What is causing an empty box to appear due to padding? Is there a way to conceal it

There seems to be an issue with adding padding to the results id div box. The padding is causing a blank yellow box to appear at the bottom before any results are submitted. I've tried to hide this box without success by adding a displayResult() funct ...

Python Selenium- Extract and print text from a dynamic list within a scrolling dropdown element

I am currently working on a project using Selenium and Python to extract a list of company names from a dropdown menu on a javascript-driven webpage. I have successfully managed to reveal the list of company names by clicking the navigation button, and eve ...

Trigger an alert when a button is clicked and redirect the user to an newly opened tab

I recently created a button with a link that opens in a new tab. I also implemented some JavaScript to display an alert. Everything is working as expected, but after the user clicks "OK" on the alert, they remain on the same page. I would like to automati ...

Ways to soften a section of a canvas component?

What is the simplest way to apply a blur effect to a specific portion of my canvas element? I am utilizing the JavaScript 3D Library known as three.js. This library simplifies the use of WebGL. While they offer examples of blur and depth of field effects, ...

Understanding how flex-wrap property works in flexbox is essential for creating

Take a look at the code snippet below: .flex-container { padding: 20px; margin: 20px; list-style: none; border: 1px solid silver; -ms-box-orient: horizontal; display: -webkit-box; display: -moz-box; display: -ms-flexbox; display: -moz- ...

Show an SVG overlay when hovering over an image

Is there a way to create a hexagon shape around an image when it is hovered over using CSS only, even if the image itself has a circular border-radius of 50%? ...

Conquering disparities in standards mode with Javascript

Having an issue with this code snippet: function scrollLeft() { document.body.scrollLeft -= scrollSpeed; } While it works fine in Chrome and Safari, it doesn't seem to work in IE and Firefox. Found out that the problem lies in the fact that Fire ...

Prop validation error: prop type mismatch occurred

My Vue.js countdown isn't displaying the values correctly. Despite defining everything as numbers, I keep getting an error in the console: [Vue warn]: Invalid prop: type check failed for prop "date". Expected Number, got String. I've gone th ...