Using Three.js to render an octahedron shape

I attempted to create an Octahedron shape, but the final result did not meet my expectations. I am unsure of the reason behind this. Instead, I now plan to draw a Dodecahedron. Any suggestions or advice on how to achieve this? Click here to view the outcome of the Octahedron.

Here is my code:

var mesh, renderer, scene, camera, controls;

init();
animate();

function init() {

// Setting up the renderer
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// Creating the scene
scene = new THREE.Scene();

// Setting up the camera
camera = new THREE.PerspectiveCamera(40, window.innerWidth / 
window.innerHeight, 1, 10000);
camera.position.set(20, 20, 20);

// Adding controls for user interaction
controls = new THREE.OrbitControls(camera);

// Adding ambient light to the scene
scene.add(new THREE.AmbientLight(0x222222));

// Adding directional light
var light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(20, 20, 0);
scene.add(light);

// Adding axes as a visual guide
scene.add(new THREE.AxisHelper(20));

// Defining the geometry for the Polyhedron shape
var vertices = [
                0.5,0,0, 0,0.3,0, -0.5,0,0, 0,-0.3,0, 0,0,0.3, 0,0,-0.3
            ];
var faces = [
                0,1,4, 1,2,4, 2,3,4, 3,0,4, 0,1,5, 1,2,5, 2,3,5, 3,0,5
            ];
var geometry = new THREE.PolyhedronGeometry(vertices, faces, 5, 1);

// Defining material properties for the shape
var material = new THREE.MeshPhongMaterial({
    color: 0x00ffff,
    shading: THREE.FlatShading,
    wireframe:true,
    transparent: true,
    opacity: 0.7,
});

// Creating the mesh object
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);

}

function animate() {

requestAnimationFrame(animate);

// Updating controls for user interaction

renderer.render(scene, camera);

}

Answer №1

Learn how to utilize the following code snippet:

var shape = new THREE.OctahedronGeometry( 5, 0 ); // Alternatively, use DodecahedronGeometry

Gain insight into the inner workings of OctahedronGeometry.js in order to employ PolyhedronGeometry directly.

If you wish to display a wireframe, opt for MeshBasicMaterial.

Compatible with three.js r.85

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

Is it possible to delay Vue rules from validating until a user interacts with an element?

For a project I am working on, I have implemented a v-date-picker where users can select a departure date and a return date. Interestingly, while most of the input field validations only occur after user interaction, the departure date picker displays an e ...

Updating votes on the client side in WebForms can be achieved by following these steps

I am currently working on implementing a voting system similar to Stack Overflow's. Each item has a vote button next to it, and I have it functioning server side causing a delay in reloading the data. Here is the current flow: Clicking the vote butt ...

How can I customize the styling of an SVG pseudo element using Font Awesome 5?

I've implemented font awesome 5 pseudo elements to attach an :after tag to my element as shown below: &:after { content: "\f068"; font-weight:400; color:$brandRed; float:right; font-family: "Font Awesome 5 Pro"; } The c ...

I'm looking to include a field card into the to-do table I built using .Net, but I'm not sure where I made a mistake

HTML Challenge I have set a goal to dynamically add a DOM element using JavaScript when the "Add HTML Element" button is clicked. The process involves clicking the button, which opens a modal for inputting necessary information. After fil ...

The use of script src with vue.js is causing issues

I am looking to move my Vue code into an external .js file. Since I am new to Vue, I am trying to keep it simple without using webpack. However, I have encountered an issue where Vue does not work when using the script src tag in the html file. For instanc ...

What is the best way to divide an array of objects into three separate parts using JavaScript?

I am looking to arrange an array of objects in a specific order: The first set should include objects where the favorites array contains only one item. The second set should display objects where the favorites array is either undefined or empty. The third ...

What is the best method for obtaining the HTML content of a webpage from a different domain?

I'm in the process of creating a website where I have the requirement to retrieve the HTML content of a different site that is cross-domain. Upon researching, I came across YQL. However, I don't have much experience with YQl. Is it possible to ad ...

Switch out 2 Bootstrap columns for 2 concealed columns with just a click. Utilizing Rails 4 and Bootstrap

Using Twitter Bootstrap 3 for a column system showcasing four similar advertisements at the bottom of the page. Code Snippet: <div class="row similar"> <% @recomended_ads.each do |advertisement| %> <div class="col- ...

How can I set the textbox focus to the end of the text after a postback?

In my ASP.Net form, there is a text box and a button. When the user clicks the button, it adds text to an ASP:TextBox during a postback (it adds a specific "starter text"). After the postback, I want the focus to be set to the end of the text in the textbo ...

Initial position of jQuery slider

A while back, I came across some slider code on a website and copied it. Unfortunately, I can't seem to locate the source now. Here is the code snippet: slides.min.jquery.js $(function(){ $('#slides').slides({ preload: true, ...

React - the state fluctuates

The Goal I'm Striving For: Transmitting data from child to parent. My Approach: Utilizing this.state as outlined here Having trouble summarizing the issue: Upon calling console.log(this.state) in the function where I update the state, the correct va ...

Create a separate server session specifically for handling an ajax request?

Currently, I am working with a collection of PHP server-side scripts that manage user session state by utilizing PHP sessions extensively for authenticated users. For the client side within a mobile application and using Jquery ajax, I am striving to esta ...

Encountering a null value after making changes to the state in Reactjs

I'm currently working on a drag-and-drop web application using reactjs. The issue I'm encountering is that when I initiate the drag action, it triggers the handleDragStart function to update the state variable called selectedCard. However, when I ...

Leveraging the power of the Vivid.JS library with Internet Explorer

Recently, I stumbled upon Vivid.JS, an SVG Icons library that caught my attention due to its lightweight nature and customization options. The usage instructions are simple and straightforward: <i data-vi="icon-name"></i> Unfortunately, I enc ...

Exploring methods to access specific values from an array containing multiple values using Lodash in Angular 4

Hey, I have an array that looks like this: [ 0: "Migration, MD" 1: "Lution, MD" 2: "Mover, MD" 3: "Dee" 4: "Prov10A" ] I would like to extract the values that contain the word "MD" in them. In other words, I want a result like this: [ 0: "Migratio ...

Leverage the power of dynamic type implementations within Angular framework

Recently, I developed a typescript module that contains type definitions and JavaScript implementations in the dist folder. This typescript module serves as an npm package dependency hosted on an internal HTTP link. Below is a basic diagram depicting the c ...

Identify whether the file is suitable for downloading via UIWebview

I am currently working on a project where I need to detect audio files (mp3, mp4, m4a, and wav) when clicking a link within a UIWebview. I have implemented the delegate call -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)re ...

What are some techniques for locating an element in Selenium when it has no tags?

I'm facing a challenge in my code where I need to ensure that the message "Product successfully added to your shopping cart" appears after adding an item to the cart. Here, there is no specific tag enclosing the text, making it tricky to target for ve ...

Error message: "Angular dependencies and provider not accessible"

I recently came across a file upload script on Github that I decided to implement. <script type="text/javascript" src="{% static 'bower_components/angular-file-upload/angular-file-upload.min.js' %}"></script> Furthermore, I have cre ...

I am experiencing an issue where the data on the server is not being updated when I click the "back" button in the browser using jQuery, Ajax,

Here's the issue at hand: Upon clicking the "purchase" button, it changes color, updates the link, and adds the product to the cart: $(document).ready(function(c) { $('.item_add').click(function (event) { var addButton = $(this).c ...