Utilizing a specific set of 3D coordinates to position a Polygon in ThreeJS

Creating a polygon (wall) in 3D space using Three.JS has been quite challenging for me.

I need to add thickness to my wall, which is why I want to utilize ExtrudeGeometry. Additionally, the number of points can vary from three to infinite, ruling out a simple BoxGeometry.

Take a look at this example: https://i.sstatic.net/QB4sj.png

Assuming the given points are always on the same plane and moving clockwise from bottom left, here are the coordinates of the green points:

[
  [1, 0, -1],
  [1, 3, -1],
  [5, 3, -4],
  [5, 0, -4],
]

The challenge arises because Shape works with 2D points. In a 2D plane, it looks like this:

const shape = new THREE.Shape();
shape.lineTo(0, 0);
shape.lineTo(0, 5);
shape.lineTo(5, 5);
shape.lineTo(5, 0);

const geometry = new THREE.ExtrudeGeometry(shape, { steps: 1, depth: 1 });
const material = new THREE.MeshStandardMaterial();
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);

The current solution involves rotating the points to a 2D plane, creating the shape, and then rotating the geometry back. This method feels overly complicated, especially if rotation about multiple axes is required.

Is there a simpler way to position a polygon in 3D space based on a set of three or more points?

Answer №1

At the beginning of this discussion, you can find the initial part of the answer: as mentioned by @prisoner849 in the comment.

Prior to utilizing reverseQuaternion, it's important to note that when creating a Shape, the Z coordinate of each point in the ShapeGeometry defaults to 0. This is because the Shape constructor utilizes Vector2[] values instead of Vector3[]. Therefore, after applying the quaternion for the first time, the Z coordinate needs to be updated:

const tempPoints = [];
points.forEach(p => {
  tempPoints.push(p.clone().applyQuaternion(quaternion));
});
const z = tempPoints[0].z; // z coordinate will remain constant for all points
const shape = new Shape(tempPoints); // although they are treated as Vector2[] values, not Vector3[] values

... proceed to apply this updated Z value to each vertex:

const geometry = new ShapeGeometry(shape);
const position: Float32BufferAttribute = geometry.attributes['position'];
for (let i = 0; i < position.array.length; i++) {
  position.setZ(i, z);
}
position.needsUpdate = true;

... finally, don't forget to use reverseQuaternion on the geometry or mesh containing the geometry in order to achieve the correct positioning of your mesh.

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 expo-location feature is failing to accurately record and store all of the positions within the array

Incorporating expo-location in my react-native app, I utilize it to track the user's positions and store them in a redux object. While debugging the object reveals that all positions have been successfully inserted, upon retrieving this array, it turn ...

Leveraging Node.js to establish a connection between two pug files

Recently, I decided to delve into the world of pug and JavaScript. However, I seem to be facing a small issue that I can't quite figure out on my own. My project involves creating multiple .pug files with various text content that I can navigate betwe ...

Accordion is having trouble expanding fully

My accordion is causing some trouble. When I try to open one section, both sections end up opening. I want it to toggle, so that only one section opens at a time based on my click. Switching the display from flex to column fixes the issue, but I don' ...

Error in Asp.net: 'test' is not defined in Microsoft JScript runtime

I'm encountering an issue with an ImageButton that has an onclientclick() method. Whenever I click the button, I receive the error Microsoft JScript runtime error: 'test' is undefined. Below is the full code snippet: <%@ Page Langua ...

Creating an event emitter instance

I'm intrigued by the process of objects transitioning into event emitters. The documentation showcases code that resembles the following: var EventEmitter = require('events').EventEmitter; function Job(){ EventEmitter.call(this); } I fi ...

Trigger a keypress event following the activation of a button

I've been trying to simulate the press of the backspace key on the keyboard after clicking a button, but all my attempts have been unsuccessful. Here is the code I'm using: function rtf(){ document.getElementById("u0u").focus(); $('#u0u ...

Guide on looping through an array of objects and eliminating each item using JavaScript

someArray = [{name:"Ibrahim", cid:322}, {name:"Ismail", cid:423}]; Exploring this task further, I am seeking a reliable method to iterate over the array, perform certain actions, and eventually produce the desired output like so: someArray = []; This is ...

What is the reason for needing to refresh when submitting form data in a Node application with an HTTP POST

Code Snippet - Angular .state('studentInfo.newStudent', { url : '/new/student', templateUrl: 'students/new-student.html', controller : function($state, $http){ this.saveStudent = func ...

The quality of Three.js models is subpar

Lately, I've been facing an issue with the quality of 3D models in my Angular project that uses three.js. Despite my efforts, the models appear to be of poor quality when loaded. Surprisingly, when I load the same models on a platform like , they look ...

PDF Embed Whitelist

Is there a way to embed PDFs on a specific domain in a read-only format without allowing saving or downloading? While services like Scribd and Slideshare offer private options, I haven't found one that allows whitelisting for embeds on certain domains ...

There seems to be an issue with the bootstrap datepicker as it is throwing an error stating that $(

Currently in the process of developing a web application using AngularJS with MVC, I have integrated Bootstrap's datepicker into my project, Here is the code snippet: <div class="container"> <div class="row"> <div class=& ...

The synchronization of sessions between Socket.IO and Express is proving to be a challenge as

I am currently facing an issue with connecting the sessions of my express API and socket.IO server. It appears that both are storing their sessions independently. The socket.IO server has the connections session, while the express server has the user qid s ...

Getting a user's group name from Azure Active Directory in an Angular application - a step-by-step guide

I have connected to Azure directory using ng2-adal (https://github.com/mazhisai/ng2-adal-QuickStart) and successfully obtained a group id in the format: "groups":["4ba2649e-20d2-40f4-a406-2ed897686403","43e19f05-c077-4716-b001-0ffb0d75fff8"]. Is there a w ...

The TextGeometry and PointCloud components work together to create dynamic

I am trying to generate vertices for a given text by using TextGeometry and then instantiating PointCloud: const textGeo = new THREE.TextGeometry( "three.js", {...}); const textMaterial = new THREE.MeshBasicMaterial({ color:0x8080FF, ...

What is the best way to import code snippets from THREE.js examples?

Currently, I am in the process of developing a React application that utilizes THREE.js. I am interested in importing some code from the THREE.js library that is not included in the official distribution. Within the official repository for THREE.js, there ...

How can we enable a sitemap for web crawlers in a nodejs/express application?

Looking to enable sitemap for web crawlers in nodejs/express? I am trying to figure out where I should place my sitemap folder/files within the application flow and how to allow access for web crawlers. Currently, when visiting domain/sitemap/sitemap.xml, ...

Cyber-election platform

Currently running an election website with PHP Seeking guidance on enabling multiple users to access from a single browser simultaneously Implemented a database column where logging in sets the value to 1 and logging out sets it to 0 Encountering an iss ...

Once an item is added, the table vanishes into thin air

I have a DataTables setup to show a list of project names, and I have a button that should add an item to the displayed array. However, when I click the button, the table disappears. Below is the code snippet: view.html <button ng-click="vm.add()"> ...

What is the best way to merge two sets of data in JavaScript?

There are two sources from which I am retrieving data. Once the data is fetched, I need to merge them into a single source. Here's an example; //data structure looks like: //from url1 { "data": [ { "id" ...

I aim to cross-reference words within a matrix, yet I struggle to pinpoint every word in all eight directions in the matrix

I have a matrix of characters arranged in such a way that they form meaningful words. Additionally, I also have an array of words with their respective starting and ending positions on the matrix. My goal is to extract an array of strings by combining all ...