Is it not possible to add indices to a BufferGeometry?

My attempt to create a simple rectangle using THREE.BufferGeometry() has proven fruitless. Below is the code snippet I used:


var geometry = new THREE.BufferGeometry();
var material = new THREE.MeshBasicMaterial({color: 'rgb(255, 0, 0)'});

var verticesArray = [20, 0, 0, 0, 20, 0, -20, 0, 0, 0, -20, 0];
var vertices = new Float32Array(verticesArray, 0, 12);

var indicesArray = [0, 1, 2, 0, 2, 3];
var indices = new Uint16Array(indicesArray, 0, 6);

geometry.addAttribute('position', new THREE.BufferAttribute(vertices, 3));
//geometry.addAttribute('index', new THREE.BufferAttribute(indices, 3));
geometry.setIndex(new THREE.BufferAttribute(indices, 3));

var mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);

Despite following what I believe to be the correct process with four vertices and two sets of indices in counter-clockwise order, the result is not as expected. There are no errors thrown either. Can anyone identify where the issue might lie?

Answer №1

The correct syntax is

geometry.setIndex(new THREE.BufferAttribute(indices, 1));

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

The event handler onClick is unresponsive in the button element within a React component

Hey there, I'm new to React and I'm having an issue with a class-based component. Below is my component that I'm calling inside another React component. I just want the temp function to be called when onClick is triggered, but it's not ...

Struggling to arrange files in a neat array with Angular.js

Encountering an issue while attempting to upload multiple files using ng-file-upload in Angular.js. Below is a breakdown of my code: <input type="file" class="filestyle form-control" data-size="lg" name="bannerimage" id="bannerimage" ng-model="file" n ...

Guide to sending a POST request from within my application

I'm facing an issue with using $resource for making a post request in my app. Here is the code snippet from my Angular module: angular.module('myApp').factory('Product', ['$resource', function ($resource) { ...

What is the best way to retrieve and utilize various data from a JSON object? Is creating an array necessary for this task?

As a novice, I am currently utilizing $.get to fetch data from a REST API, the JSON response looks like this: [{"id":"1","url":"http:\/\/123.456.78.910\/workforce\/images\/item1.jpg","price":"99","description":"Mobile Phone"}, ...

Tips for retrieving a variable from within an arrow function?

I'm new to working with node and I'm struggling with a basic concept. redis_client.get('foo', (err, reply) => { if (err) throw err; console.log(reply); }); console.log(reply); I need to access the variable re ...

How to Preserve Scroll Position when Toggling a Div using jQuery

I've been struggling to find a solution for maintaining the scroll position of my page after a JQUERY toggle event. I've searched extensively but haven't found a fix yet. <script src="Scripts/_hideShowDiv/jquery-1.3.2.min.js" type="text/ ...

Menu options are neatly displayed alongside video player without any overlap

I have included an object tag in my page to play videos: <object id="Player" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" data="mms://TAL-BBSR-01/01_Debugging.wmv" width="100%" type="video/x-ms-asf" height="400" wmode="opaque" url="mms://TAL-BB ...

Display an error message when the button is clicked and the input field is left empty in a Vue 3 script setup

Hello, I am currently exploring Vue 3 and embarking on a new Vue 3 project venture. However, I seem to be encountering a challenge when it comes to displaying an error message if the button is clicked while the input field remains empty in my Vue 3 script ...

Incorrect pathing in express.js

I've encountered an issue with my filter while attempting to redirect packages using two express.js routes: app.get('/billdetails/:year/:month/:phoneId', function (req, res, next) { var db = req.db; var year = req.params.year; v ...

Retrieve the value from a particular JSON object based on the initial selection made

One intriguing challenge I have involves populating a set of dropdowns with JSON data. The goal is to populate the first dropdown with the name of each object, and then based on the selection from the first dropdown, populate the second dropdown with relat ...

What is the best way to verify in JavaScript whether a value is present at a specific position within an array?

Is this method effective for checking if a value exists at a specific position in the array, or is there a more optimal approach: if(arrayName[index]==""){ // perform actions } ...

What could be causing the issue with file_get_contents not loading the phpscript correctly in this scenario?

In my coding experience, I created a piece of code that essentially accomplishes the following: <div id="thisDiv"> </div> <?php echo "<script>document.getElementById('thisDiv').innerHTML = '".addslashes(str_replace(arr ...

Building a card carousel component in Vue JS

Struggling with creating a unique card slider using Vue JS? After exploring npm packages like Vue Carousel 3D and Vue Slick, I haven't found the ideal slider for my project. My specific card slider setup is showcased below: https://i.sstatic.net/GiNc ...

Are there any potential performance implications to passing an anonymous function as a prop?

Is it true that both anonymous functions and normal functions are recreated on every render? Since components are functions, is it necessary to recreate all functions every time they are called? And does using a normal function offer any performance improv ...

Postman is returning a 404 status when accessing the express router route

I've been testing routes in Express using Postman, but I keep getting a 404 error even though I have other routes that are functioning correctly. I'd rather not share the code for the working routes as it's quite lengthy and unnecessary to ...

Guide on incorporating a bootstrap 4 navigation item into an active class through jquery

I am working on a Jekyll site and I want to dynamically add the Bootstrap 4 class active using JavaScript. I have tried using the following code: $(document).ready(function() { // get current URL path and assign 'active' class var pathna ...

Window Function Implementation in AngularJS Controller

I’m encountering an issue with my AngularJS App involving a window function. Specifically, the problem lies with a window resize function. The function (someFunction()) in the controller is being triggered on every page when it should only be used with ...

Struggling to make the JavaScript addition operator function properly

I have a button that I want to increase the data attribute by 5 every time it is clicked. However, I am struggling to achieve this and have tried multiple approaches without success. var i = 5; $(this).attr('data-count', ++i); Unfortunately, th ...

Tips for restricting camera movement in threejs

Being new to working with threejs, I am struggling to set limits on the camera position within my scene. When using OrbitControls, I noticed that there are no restrictions on how far I can zoom in or out, which I would like to change. Ideally, I want the c ...

"Sending the selected pass selector as a parameter to the dispatched action is causing a typing

When a selector changes its value, I want to trigger an action. To achieve this, I passed the selector with a subscription instead of passing an observable. selectedSchedulingsOnPopup$ = this.store.pipe(select(selectSchedulingsByBranch)); this.store.disp ...