Creating a Prismoid Shape Using Three.js

Currently, I am utilizing Three.js and my objective is to construct a pyramid shape with its apex removed. The base of the pyramid should be a rectangle instead of a square, while the top should also be a rectangle, but not in equal proportions.

Here is an image from Wikipedia for reference, although I am aiming to customize it to accommodate rectangle bases:

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

I believe the term for this modified shape is Prismoid, except in my design the base will be rectangular rather than squared. Additionally, I intend to have the apex positioned off-center from the base to create shapes like Parallelepiped.

My exploration led me to CylinderGeometry and PolyhedronGeometry, however, both necessitate that all sides of the base are of equal length.

What method would you recommend as the most efficient approach to creating these complex shapes?

Answer №1

In case you prefer not to generate a shape using tools like Blender (which is the method I find simplest), you have the option to manually adjust vertices and faces to achieve the desired shape. Experiment with the vertices by uncommenting them to visualize the changes...

const vertices = [
    new THREE.Vector3(-1.5, -1, -1),
    new THREE.Vector3(1.5, -1, -1),
    new THREE.Vector3(1.5, -1, 1),
    new THREE.Vector3(-1.5, -1, 1),
    new THREE.Vector3(-1, 1, -0.5),

    // Top rectangle 
    new THREE.Vector3(1, 1, -0.5),
    new THREE.Vector3(1, 1, 0.5),
    new THREE.Vector3(-1, 1, 0.5)
];

// Sample adjustments:

// vertices[1].y += 0.2;
// vertices[6].y += 0.2;
// vertices[4].y -= 0.2;
// vertices[7].y -= 0.2;

const faces = [
    [0, 1, 4],
    [1, 2, 5],
    [2, 3, 6],
    [3, 0, 7],
    [1, 0, 3],
    [3, 2, 1],
    [5, 2, 1],
    [5, 6, 2],
    [7, 6, 3],
    [7, 3, 0],
    [4, 5, 6],
    [4, 6, 7]
];

const geometry = new THREE.BufferGeometry();
const verticesArray = [];
vertices.forEach(function(vertex) {
    verticesArray.push(vertex.x, vertex.y, vertex.z);
});
geometry.setAttribute('position', new THREE.Float32BufferAttribute(verticesArray, 3));

const indices = [];
faces.forEach(function(face) {
    indices.push(face[0], face[1], face[2]);
});
geometry.setIndex(indices);

const material = new THREE.MeshBasicMaterial({ color: 0xffff00, wireframe: true });
const pyramid = new THREE.Mesh(geometry, material);
scene.add(pyramid);

Answer №2

After much effort and a complex process, I successfully resolved this issue with the help of @Łukasz D. Mastalerz!

Below is the code snippet:

function createGeometry(width, depth, height, widthTop, depthTop, posTop = new THREE.Vector3(0, 0, 0)) {
    // Function logic here...
}
  1. To begin, define the positioning of vertices in the vertices array.
  2. Next, establish the faces in the indices array, ensuring adherence to the right-hand rule for point declaration.
  3. The most challenging part was configuring the UVs in the uvs array. Assign two float values between 0 and 1 to each point in every triangle, without using indexing, requiring the use of toNonIndexed().

Once done, combine all elements to create a prismoid geometry that can handle parallelepipeds as well.

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

Issue with Date Picker not properly storing selected date in MySQL database

My goal is to save the datepicker date to MySQL by selecting the date format using JavaScript. I have verified that the date format appears correct as YYYY-MM-DD when logging to the console. However, when I try to execute an INSERT query to MySQL, the date ...

Guide to arranging components in two columns using VueJS Vuetify Grid

My goal is to align two components in order to display data on two columns. I followed the official Vuetify Grid tutorial, but encountered some issues with fixed row components. Despite trying to change from row to column, it still doesn't work as exp ...

405 status code returned for CORS request

Hello everyone, I need assistance with my CORS issue. I am trying to make an API request from another domain and encountering an error with the following code: var headers = { host: host, path: url + instance + '?action=reset', ...

Adding the classname "active" in ReactJS can be achieved by utilizing the `className` attribute within

I am facing an issue with adding the active classname in my code. Can anyone suggest a solution to add the active classname for this section: <li onClick = {() => onChangeStatus({status: 'on-hold'})} className = {appState === {'status& ...

Using Typescript to iterate through an array of objects and modifying their keys using the forEach method

I have an object called 'task' in my code: const task = ref<Task>({ name: '', description: '', type: undefined, level: 'tactic', participants: undefined, stages: undefined, }); export interface Tas ...

Click the div to fold it

I would like the div to fold down when clicked on and then fold back up when clicked again. Here is my jQuery code: $(".fold_reply").click(function() { if ($('.reply').css('display') === 'none') { $(".reply").sh ...

Updating the text of one option within a selected option using jQuery while iterating through each element in a function

Hey there, I have multiple rooms each with two select dropdowns. My problem is that when I choose the option with a value of 4 in the number of persons dropdown, I only want the text of the next option (the dinner select) to change under the room I selecte ...

Creating custom methods or functions within imported models in Sequelize: A guide

As a newcomer to the world of Node.js, I am embarking on my initial project involving node development. While my background primarily revolves around utilizing Laravel for various projects, I am well-versed in the concepts of MVC (Model View Controller). I ...

Obtain the input elements and attach a click event to them

I have a form utilizing Bootstrap with three elements. When the button is clicked, another line with three elements is dynamically added. <div class="row align-items-center mb-2 mt-2 ms-1 "> <div class="col-5 ps-1 pe-1"> ...

In Angular 8 Router, the original parent route will remain unchanged even when navigating to a route with a different parent route

I'm currently developing a project using Angular version 8.2.8 and implementing routing with the same version. The structure of the routing is as follows: Within my app-routing.module.ts, I have defined 3 entries in the routes array: Shell.childRoute ...

Guide on integrating the reload feature in a React Native Expo app for production environments

I attempted to restart using react-native-restart, however I encountered an issue when trying to call RNRestart.Restart(): [Unhandled promise rejection: TypeError: null is not an object (evaluating '_reactNativeRestart.default.Restart')] Is th ...

Ways to retrieve the value of a field using ng-change

I have been working with ng-change in AngularJS to react to user input in a textarea. However, I am struggling to figure out how to access the current input inside the Angular controller without using something like $(this).value(); in jQuery. <scrip ...

Inform the PHP backend that the browser has been closed by the frontend Angular application

Currently, I am working on an Angular project that is interacting with a backend project created using PHP and ZF3. I am trying to figure out the most efficient method of informing the backend project when the user closes the browser window. Initially, I ...

I included an onPress prop where I am triggering a handleEvent function, but unfortunately no output is being displayed within the method

I'm currently working on implementing a textbox similar to Google Flights. As a result, I've developed a React autocomplete prototype. However, I've encountered an issue with it. In the Google Flights search box, all results are displayed wh ...

Troubleshooting the 'npm ERR! ERESOLVE could not resolve' and 'peer dependency' issues: A guide to fixing the ERESOLVE error in npm

After several days of encountering an error while trying to set up a website for remote training, I have not been able to find a solution that fits my needs. Requesting your help to resolve the issue and get the site live on Vercel. Thank you in advance f ...

Finding your way to a particular section within a webpage through an external source

Hey there! I'm currently working on creating a link that will direct users to a specific section within my webpage. For example, redirecting them to https://blabla.github.io/my-website. My code is quite straightforward and it functions properly when ...

Is it considered fundamentally inappropriate to call $scope.$digest within $scope.$on?

I recently inherited some AngularJS code, and my knowledge of both the codebase and Angular itself is limited. Within the code I inherited, there are instances where $scope.$digest is being called inside a $scope.$on method within a controller. Here's ...

Issue with Laravel: unable to send data through ajax requests

I have developed a custom jQuery plugin specifically tailored for Laravel to facilitate sending data via Ajax to the server. However, I seem to be facing an issue where no data is being sent successfully. When I use dd($request->all()) to inspect what h ...

Using lambda expressions to sort through an array of objects in React

My goal is to create a delete button that removes items from a list and updates the state variable accordingly. public OnDeleteClick = (): void => { const selectionCount = this._selection.getSelectedCount(); let newArray = this.state.items; for ...

Why does the process of joining and replacing elements in a character array using the JavaScript functions stop when it reaches the "<" character?

In my code, I am utilizing the JavaScript function join("") to convert a character array into a string while removing comma separators between characters. Typically, this method works flawlessly, but recently I encountered an issue when there is a "less th ...