Make alterations in THREE.JS by adjusting the default buffer geometry index, transforming a geometry specifically crafted for a THREE.PointsMaterial into a THREE.MeshBasicMaterial or another suitable material

If you take a look at this code pen,

You will see a parametric flower created with a red THREE.MeshBasicMaterial and wireframe. When rendered, it looks like this:

https://i.sstatic.net/TP9jP.png

Now, when the wireframe is set to false, the flower appears like this:

https://i.sstatic.net/ay4ew.png

My goal is to make the geometry solid without any grid lines.

I attempted manually setting the indices of the buffer geometry using the following code snippet:

let faces = []
      for (var i=0; i<positions.length; i+=3) {

     faces.push(i, i+3, i+2)
     faces.push(i, i+1, i+3)

  }

var indices = new Uint16Array(faces);

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

However, despite my efforts, applying these indices just results in a black screen. The default view of the buffergeometry with position-based indices can be seen on the codepen.

The main issue I'm facing is determining the correct index values and understanding why using them leads to a blank screen.

I've researched similar topics on stackoverflow such as:

how to understand setindex and index in buffer geometry

and

threejs correct way to setindex indices for buffergeometry

Unfortunately, these resources only confirm what I'm already attempting, offering no real solution.

The original code is based on a parametric equation for a rose from another stackoverflow post linked here. However, adapting it to use a solid THREE.PointsMaterial proved unsuccessful. My endeavors led me to modify the code myself, but I'm stuck. If anyone knows how to address this issue, please share your expertise.

I also experimented with a version using THREE.ParametricGeometry, but found it highly inefficient for my purposes. Manually generating points and adding them to a buffergeometry has proven to be much more efficient. If you have insights on properly configuring the point indices to display as solid faces, I would greatly appreciate your assistance in resolving this challenge.

Answer №1

Success at last!

https://codepen.io/ricky1280/pen/mdjOYba?editors=1010

This is the optimized 2D loop:

  for(let r=0; r < v.length; r++){
    // fill(340,100, -20+r*r_D*120)
    for(let theta =0; theta < v[r].length; theta++){
      if(r < v.length-1 && theta< v[r].length-1){
        positions.push(v[r][theta].x*1, v[r][theta].y*1, v[r][theta].z*1);
        positions.push(v[r+1][theta].x*1, v[r+1][theta].y*1, v[r+1][theta].z*1);
        positions.push(v[r+1][theta+1].x*1,v[r+1][theta+1].y*1,v[r+1][theta+1].z*1); 

        positions.push(v[r][theta].x*1, v[r][theta].y*1, v[r][theta].z*1);
        positions.push(v[r][theta+1].x*1,v[r][theta+1].y*1,v[r][theta+1].z*1);
        positions.push(v[r+1][theta+1].x*1,v[r+1][theta+1].y*1,v[r+1][theta+1].z*1);
        
 
      }
    }
  }
  

Instead of adding positions to and then setting an index, add them directly respecting an index.

Following this advice, adjust the vertices of the p5js geometry accordingly.

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 postal code using longitude and latitude data from Google's API

I am currently working on a project that requires obtaining a postcode from longitude and latitude coordinates. Utilizing the Google Maps API, I'm able to retrieve the longitude and latitude based on a search term like 'Manchester'. After t ...

Enhance the HTML content using a JavaScript function

Here is the code that I have: <label>Brand</label></br> <select name="brand" id="brand" onChange="changecat(this.value);"> <option value="" selected>Select Brand</option> <option value="A">AMD</option&g ...

Troubleshooting issue: Bootstrap button onclick function not firing

My node express app is using EJS for templating. Within the app, there is a button: <button type="button" class="btn btn-success" onclick="exportWithObjectId('<%= user.id %>')">Export to csv</button> Accompanying the button i ...

Vue.js: Iterating over objects using v-for causes keys to be displayed in unexpected order

I've encountered an issue where Vue is not honoring the sequence of keys. The initial data I provide to the Vue component as a prop appears as follows: [ { id: 1, event_name: "event 1", scheduled_at: "2021-01-01" }, { id: 2, ev ...

JavaScript array containing objects remains static

I'm facing an issue with a complex array of objects (I will only display the necessary nodes to explain the problem): var arr = [ { json: { doc: { id: 1, atualizacao:{ ...

Express callback delaying with setTimeout

I'm working on a route that involves setting a data attribute called "active" to true initially, but then switching it to false after an hour. I'm curious if it's considered bad practice or feasible to use a setTimeout function within the ex ...

Unexpected behavior of Codeigniter sessions

In my system, I am storing various session data such as id, type, isloggedin and a session called comp_city where I keep the name of a city. Recently, I set the value of comp_city to 'San Francisco' successfully. However, when I redirected to an ...

Check if an element is currently within the visible portion of the viewport by scrolling

I am currently working on a script to check if an element is within the viewport as the user scrolls. This script functions correctly when placed outside of a Modal, but encounters issues when placed inside one. I suspect that the problem lies with the ...

How can you effectively transfer arguments from one component to another using router.push() in Vue.js?

I need help with implementing a feature where upon clicking the edit button in a row, all the details from that particular row should be passed to a form component. I want to populate the form fields with default values from the parameters provided. Can ...

Analyzing differences in id usage within HTML5

I have created a setup using section and article to display content. Here is the code snippet: <section id = "examples"> <article id="item_1"> ... </article> <article id="item_2"> ... </article> <article id=" ...

Highlighting in ThreeJS/Projector Interface

Is there a way to incorporate a 2D sprite underneath a 3D object on top of a plane in order to indicate that my objects are highlighted or selected? Additionally, how can this be achieved on uneven terrain? To provide further clarification, I have includ ...

What could be the reason for my dropdown not submitting the form?

Check out my code below: <div class="center"> <p> Choose an item: </p> <div class="dropdown"> @using (Html.BeginForm("Start", "Home", FormMethod.Post, new { name = "form", id = "form" })) { @Html ...

Tips for creating unique names for JSON data modification functions

I have some data stored in JSON format that I need to slightly rearrange before sending it to the client. What should I name the function responsible for this reordering? Is serializeSomething a suitable choice? From what I understand, serialization invo ...

Having trouble implementing delays for waypoints while replaying a GPX route in javascript

I have multiple GPX files containing routes that I need to replay within a node.js script. The purpose of this is to assist in testing a geo application, so I want to replay the waypoints with the same time intervals as when the route was originally captur ...

Error occurred in parsing SQL injection in Node.js

I was almost done with my registration process when I encountered a vulnerability. Whenever I input: ?Smith into the registration form, I get the following error message: Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that ...

Devices such as CAC cards and smart cards are not being recognized by Chrome's WebUSB feature

I recently developed a script to identify all USB devices connected to Chrome using chrome.usb.getDevices. Surprisingly, the script successfully detected a second-generation iPod touch, a mouse, keyboard, and two unidentified items from Intel. However, it ...

Angular 2 - Error: Regular expression missing forward slash syntax

Recently, I began working on an Angular 2 tutorial app using this repository. While I can successfully launch the app and display static content, I am facing challenges with rendering dynamic content from the component. I have a feeling that the error migh ...

Display HTML containing a <script> tag within a REACT component

Looking to embed the following HTML code within a React component: <form action="/process-payment" method="POST"> <script src="https://www.example.com/payment-script.js" data-preference-id="$$id$$"> </script> </form> I ...

All Event Monitor

Is it possible to use Event Listeners in jQuery to display information when a specific word is clicked? For example, showing a definition when a word is clicked. Thanks, Adam. I need help with creating a feature where clicking on a person's name in a ...

What is the best way to create an object using a string value as a reference?

I am seeking a way to efficiently utilize a string within an API factory to dynamically create the appropriate object from a class. Here is the script: import StoryApiService from './story' import AssignmentApiService from './assignment&apo ...