Creating a mesh using Three.jsWould you like to learn how to

I have an .obj file

v 1 2 3
v 4 5 6
v 7 8 9

vt 0 1
vt 1 0

vn 0 0 1
vn 0 1 0
vn 0 0 1

f 1/1/1 2/2/2 3/3/3
f 1/1/2 2/2/3 1/2/3

My goal is to generate a THREE.Mesh. Here's what I'm doing:

var geometry = new THREE.BufferGeometry();
geometry.addAttribute('position', new THREE.BufferAttribute(vertices, 3));
geometry.addAttribute('normal', new THREE.BufferAttribute(normals, 3));
geometry.addAttribute('uv', new THREE.BufferAttribute(uvs, 2));
geometry.setIndex(new THREE.BufferAttribute(indices, 1));

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

I believe I need the following data stored in arrays:

var vertices = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var normals = [0, 0, 1, 0, 1, 0, 0, 0, 1];
var uvs = [0, 1, 1, 0]
var indices = // ... ?

However, I am uncertain about what needs to be saved in the indices array. Can anyone provide clarification?

Answer №1

My unique example showcases the intricacies of defining faces without vertices sharing the same texture and normals indices. Unlike traditional Geometry, in BufferGeometry, each index specified in the indices array corresponds to vertices, uvs, and normals arrays. This distinction prevents vertices from being reused, as elaborated by @gaitat.

v 1 2 3
v 4 5 6
v 7 8 9

vt 0 1
vt 0.5 0.5
vt 1 0

vn 0 0 1
vn 0 1 0
vn 1 0 0

f 1/1/1 2/2/2 3/3/3
f 1/1/2 2/2/3 1/2/3    

var vertices = [1,2,3,  4,5,6,    7,8,9,  1,2,3,  4,5,6,    1,2,1];   // itemSize: 3
var uvs =      [0,1,    0.5,0.5,  1,0,    0,1,    0.5,0.5,  0.5,0.5]; // itemSize: 2
var normals =  [0,0,1,  0,1,0,    1,0,0,  0,1,0,  1,0,0];   // itemSize: 3

var indices = [0,1,2, 3,4,5]; // itemSize: 1

Edit: In the revised example, the second vertex of the first face (2/2/2) is indexed with 1. Thus, retrieving the corresponding items from vertices, uvs, and normals array yields 4,5,6 0.5,0.5 0,1,0. Similarly, the second vertex of the second face (2/2/3) is indexed with 4, resulting in the retrieval of the 5th item set from each array: 4,5,6 0.5,0.5 1,0,0. Although the vertex positions and uvs are identical, differing normals prevent reuse due to the single index assigned for all three attributes.

f 1/1/1 2/2/2 3/3/3
f 1/1/2 2/2/2 1/2/3    

var vertices = [1,2,3,  4,5,6,    7,8,9,  1,2,3,  1,2,1];   // itemSize: 3
var uvs =      [0,1,    0.5,0.5,  1,0,    0,1,    0.5,0.5]; // itemSize: 2
var normals =  [0,0,1,  0,1,0,    1,0,0,  0,1,0,  1,0,0];   // itemSize: 3

var indices = [0,1,2, 3,1,5]; // itemSize: 1

By examining this scenario, it becomes evident that when both faces share the same second vertex (2/2/2), efficient reusability can be achieved. The shorter arrays reflect this optimization, reinforced by the consistent indexing of the shared vertex in the second face as 1.

Answer №2

In this specific scenario:

let indicesArray = [5, 8, 3, 5, 8, 9];

This would result in a degenerate triangle since index 5 is repeated for the second triangle.

There are various ways to specify face elements in the .obj file format:

f v1 v2 v3 ....                        // vertex indices
f v1/vt1 v2/vt2 v3/vt3 ...             // include texture indices
f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3 ... // include normal indices
f v1//vn1 v2//vn2 v3//vn3 ...          // exclude texture indices

Since buffers use vertex indices as their attributes, you need to extract the first number (value1) from each triplet value1/value2/value3 and populate your indices array 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

Firebase: Saving data to a nonexistent object

I am currently facing a challenge in saving the result of a serviceId to a services object within a parent entity named provider1, especially since the services object has not been initialized yet. The structure of my Firebase data is as follows: "provid ...

Incorporating click functionality in React for a to-do list feature

I've recently developed a task management application using React. However, I've encountered a couple of issues with the functionality. I am unable to mark tasks as completed by simply clicking on them, and the option to clear completed tasks by ...

The information within the ajax request remains static

I made changes to my ajax data, saved it and even double-checked the file location. However, the old version is still appearing as if it's cached. Here is the original code: function setMessages(roomId, username, message){ $.ajax({ type: ...

Numerous modals implemented within a React component

Currently, I am working on mapping different products, each with its own "report" button. The issue I am facing is that when I click on the "report" button for one product, all the other modals also pop up showing irrelevant details. It should only display ...

JavaScript Map Incorrectly Holding Value

My project is a basic checklist that looks like this https://i.sstatic.net/OC9Rml.png I've encountered some strange behavior while using the map function in reactJS. import React from "react" import "./App.css" import todosData f ...

Making an AJAX call to a PHP script to add data

Could you assist me in understanding why my code is resulting in a double insert? I have a JavaScript function that makes an AJAX request to a save.php file to insert data into a database. However, each time I submit it, it performs the insertion twice, al ...

Issue with special characters preventing Custom TinyMCE button from functioning properly

I have integrated a custom button into the visual editor of TinyMCE. This button is designed to enclose any selected words in <em> tags with the class tle. For example, if you select the term Pulp Fiction and click the button, it will wrap it as < ...

How to disable specific multiple dates in Material-UI datepickers?

Is there a way to disable specific dates on the calendar, not just past or future dates? I want to be able to choose which dates are disabled, such as June 29th, June 27th, and July 4th. I know you can use 'shouldDisableDates' to achieve this, b ...

Is there a way to activate the autoplay feature for this?

I'm really new to Jquery and most of this code isn't mine, I'm just using it as a learning tool for creating sliders. If someone could give me some guidance on how to make this slider work automatically when the page loads, that would be gre ...

What is the process for generating an array of objects using JavaScript?

I am struggling to create an array of objects using JavaScript and facing errors with new lines added where I need to split the messages and collect row numbers. The row numbers should be comma-separated if it is a repetitive error message. I found a solu ...

What is the reason for TypeScript allowing this promise chain without any compilation errors?

Although I am still relatively new to Typescript, I find myself grappling with a particular issue that has been perplexing me. I am unsure why the following code snippet triggers a compilation error: // An example without Promises (does not compile) funct ...

Tips for saving all models retrieved using the `DB.find({})` method in Mongoose

Is it possible to edit values in an array of query results returned from the database? We know that we can use model.save() for a single Query/row, but what about multiple Query results in an array? How can we achieve this? Would something like the follo ...

Add United States as an additional attribute to the countries retrieved from the API

I am working with an API that provides data in a specific format: [ { "id": 12, "acf": { "address": { "city": "Bandar Penawar", "state": "Johor", "country ...

Unable to understand the reason behind why the button is not being displayed

I'm having trouble with my quiz app. I have a button to submit answers and move on to the next question, but whenever I try to place it within the div that contains the quiz, the button disappears. Can someone please help me figure out what's wro ...

Guide on creating a similar encryption function in Node JS that is equivalent to the one written in Java

In Java, there is a function used for encryption. public static String encryptionFunction(String fieldValue, String pemFileLocation) { try { // Read key from file String strKeyPEM = ""; BufferedReader br = new Buffer ...

Send back alternate HTML content if the request is not made via AJAX

Last time I asked this question, I received several negative responses. This time, I will try to be more clear. Here is the structure of a website: Mainpage (Containing all resources and scripts) All Other pages (HTML only consists of elements of that ...

Should you opt for returning [something] or (nothing) in Javascript with ExpressJS?

Is there a distinct contrast between returning something and returning nothing? Let's take a look at an example: user.save(function(err){ if ( err && err.code !== 11000 ) { console.log(err); console.log(err.code); res.send(&apo ...

Some places may not have detailed information available when using the Google Places API

Greetings I am currently in the process of developing a page on my website that utilizes the Google Places API along with the user's geographical coordinates (latitude, longitude) to locate nearby restaurants. In my code, I have implemented a functio ...

How can I create an asynchronous route in AngularJS?

I implemented route and ngView to display dynamic content, however I received a warning message: The use of Synchronous XMLHttpRequest on the main thread is deprecated due to its negative impact on user experience. For more assistance, please refer to ...

What is the reason behind caching form data in an ajax call?

After the first successful submission, I am facing an issue where the original form data is being re-sent even when new values are submitted for a second attempt. How can I reset and reload the data to avoid this problem? I have tried several approaches i ...