When creating BufferGeometry lines in Three.js, the colors tend to not adhere to individual segments

Having an issue with BufferGeometry line drawing, specifically with setting the indices and colors. Even though I set the indices as [1,2,2,3,3,4] and the colors as

[r1,g1,b1,r1,g1,b1, r2,g2,b2,r2,g2,b2, r3,g3,b3,r3,g3]
, the colors seem to extend past the segments and blend into the next color instead of staying within their designated areas. It appears that only the first color is being applied, almost like each segment is being colored halfway through. To better illustrate this problem, I have created a sample on JSFiddle: http://jsfiddle.net/0f1oxdjx/

var positions = new Float32Array( MAX_POINTS * 3 );
var colors = new Float32Array(2*(MAX_POINTS-1)*3);
var indices = new Uint16Array(2*(MAX_POINTS-1));
var x = y = z = index = 0;

for ( var i = 0, l = MAX_POINTS; i < l; i ++ ) {
    positions[ index ++ ] = x;
    positions[ index ++ ] = y;
    positions[ index ++ ] = z;
    x += ( Math.random() - 0.5 ) * 300;
    y += ( Math.random() - 0.5 ) * 300;
    z += ( Math.random() - 0.5 ) * 300;
}
var iindex = 0, cindex = 0;
for ( var i = 1, l = MAX_POINTS; i < l; i ++ ) {
    indices[iindex++] = i-1;
    indices[iindex++] = i;
    x = ( Math.random() );
    y = ( Math.random() );
    z = ( Math.random() );
    colors[ cindex ++ ] = x;
    colors[ cindex ++ ] = y;
    colors[ cindex ++ ] = z;

    colors[ cindex ++ ] = x;
    colors[ cindex ++ ] = y;
    colors[ cindex ++ ] = z;
}
geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ));
geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
geometry.setIndex(new THREE.BufferAttribute( indices, 1 ));

// material
var material = new THREE.LineBasicMaterial({vertexColors:THREE.VertexColors, linewidth:2});    

I have made some edits to the fiddle to try to address the issue.

Answer №1

Following WestLangley's suggestion, I successfully resolved the issue by utilizing non-indexed geometry. To view a demonstration, visit this functioning fiddle:

http://jsfiddle.net/ed00u25s/

positions[ index ++ ] = x;
positions[ index ++ ] = y;
positions[ index ++ ] = z;
for ( var i = 1, l = MAX_POINTS-1; i < l; i ++ ) {
    x += ( Math.random() - 0.5 ) * 300;
    y += ( Math.random() - 0.5 ) * 300;
    z += ( Math.random() - 0.5 ) * 300;
    positions[ index ++ ] = x;
    positions[ index ++ ] = y;
    positions[ index ++ ] = z;

    positions[ index ++ ] = x;
    positions[ index ++ ] = y;
    positions[ index ++ ] = z;
}
positions[ index ++ ] = x;
positions[ index ++ ] = y;
positions[ index ++ ] = z;
var iindex = 0, cindex = 0;
for ( var i = 1, l = MAX_POINTS; i < l; i ++ ) {
    x = ( Math.random() );
    y = ( Math.random() );
    z = ( Math.random() );
    colors[ cindex ++ ] = x;
    colors[ cindex ++ ] = y;
    colors[ cindex ++ ] = z;

    colors[ cindex ++ ] = x;
    colors[ cindex ++ ] = y;
    colors[ cindex ++ ] = z;
}

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

Utilizing Google Chrome Extension: Sharing variables between injected scripts via chrome.tabs.executeScript to another script injected in a similar manner

Can anyone help me with a coding challenge? I have two scripts injected in my popup.js and I need to make a variable defined in one script accessible to the other. Here's how I'm injecting the scripts: let sortFunction = function (goSortParam) { ...

Checking all checkboxes in a list using Angular 5: A simple guide

I have a list that includes a checkbox for each item, and I want to confirm if all of them are checked off. This is the HTML code snippet: <ul class="ingredient-list" > <li class="btn-list" *ngFor="let ingredient of recipe.ingredients"> ...

Guide on transferring Vue form input data to Adonis controller through an http request

I have a Vue component that prompts the user to enter their email address. I've utilized v-model for data binding. <template> <input v-model="email" type="text" placeholder="Email" /> <button class="tiny">Send</button> ...

Utilizing JavaScript scope effectively in Express.js within a Node environment

I'm currently diving into the world of JavaScript and full-stack development and one concept that's giving me some trouble is understanding how to properly utilize express with JavaScript .... Here is my question: In all the tutorials I've ...

CSS Class Not Updating in WordPress Using JavaScript

Looking to incorporate a small JavaScript code directly into The Twenty Sixteen theme in WordPress. This HTML Code needs to be modified on the Page: <p class="site-description">Example Text</p> The goal is to change a classname: document.g ...

What could be causing the information from the ajax call to not appear in this popover?

Hovering over a link will trigger a popover with a 1 second delay containing user profile information, loaded using AJAX. $(function() { var timer = null; var xhr = null; $('.user_popup').hover( function(event) { ...

The top row of a Bootstrap table generated with JavaScript does not display the background color as intended

I am currently using JavaScript to generate some HTML that displays a Bootstrap table. Below is the code for creating this: function displayTableData(d){ let html = ''; html += '<table id="my-table" class="table&q ...

The timer countdown is encountering an issue where it cannot update the 'textContent' property of a null element

I'm encountering errors with a countdown script that I can't seem to resolve. The error message is generating 1 error every second: Uncaught TypeError: Cannot set property 'textContent' of null at (index):181 (anonymous) @ (index):181 ...

Accessing the selected value from a dropdown list filled with data from a PHP array in HTML

My HTML and PHP code consists of a select dropdown that looks like this: <select name="category" id="_category" class="_cateogry" onchange="submitTheForm() > option value="">Please select</option> <?php foreach ($categories as $cont ...

Windows npm configuration settings

After receiving helpful answers to my previous question about using a named parameter in an npm run script, I encountered a new problem. It seems that the $npm_config_variable doesn't function correctly on Windows OS. I am in search of a solution that ...

Displaying PDF files on the internet without allowing them to be downloaded

How can I display PDF files on my website without allowing them to be saved or downloaded? Is there a way to prevent browsers from saving or downloading the PDF files? ...

Optimal method for detecting changes in a React webpage

As I create React hook components that utilize Material-UI input controls, I have searched extensively but have not found a satisfactory example. My goal is to display a popup message to the user if they have made changes to an input field, checkbox, rad ...

Updating an array of data in D3

Seeking guidance on implementing the general update pattern in D3... My goal is to create a basic bar chart displaying data from an array, with an input form to add new data and dynamically update the chart. Struggling with repetition while trying to ref ...

Utilizing a PHP-scripted multidimensional array in an AJAX success function

I've encountered an issue while trying to access a multidimensional array passed to my AJAX success function. Here's how I'm sending the data: $response = array(); $response['message']['name'] = $name; $response['m ...

Tips on maintaining the parent's scope in CoffeeScript while making an AJAX call

I need to iterate through an object in CoffeeScript and make an AJAX call for each item in the object using jQuery. However, I'm facing an issue with losing the reference to the initial context in the callback function of the AJAX call. The context al ...

What is the best way to apply a background image to a DIV element using CSS

The main aim is to change the background image of a DIV using AJAX. $.getJSON("https://itunes.apple.com/search?term=" + searchTerm + '&limit=1' + '&callback=?', function(data) { $.each(data.results, fun ...

What methods can be used to report errors with Sentry while offline?

One key feature of my app is its ability to function both offline and online. However, I'm wondering how I can ensure that errors are still sent to my Sentry dashboard even when a user is offline. ...

Steps for organizing a particular key in a map

I need assistance sorting my map based on the "time" value, but so far, I haven't been successful in finding a solution after searching online extensively. If anyone has any insights or recommendations, please share them with me. This is how my Map ...

Issue with Angular table display cell overloading(produces excessive rendering of cells)

In my project, I am working with 3 arrays of data - DATA_CENT, DATA_NORTH, and DATA_WEST. Each of these arrays contains another array called data, which I need to extract and display in a table format. For each new column, I create a new table and then po ...

Issue with Angular ng-model not functioning as expected within ng-repeat block

Encountering an issue when trying to bind Json data into ng-model within ng-repeat. html : <div ng-controller="Ctrl"> <div> <table> <th> <td>add</td> <td>ed ...