Issue with rendering both triangles of square mesh in Three.js

As a newcomer to three.js, I have encountered an issue that I would appreciate some assistance with. I am attempting to draw a triangle and square next to each other, but the problem arises with the square mesh. Only the first vertex push seems to occur and it is not getting rendered. Is there something wrong in my approach here?

<!DOCTYPE html>
<html>
    <head>
        <script src="three.js" type="text/javascript"></script>
        <script src="Detector.js" type="text/javascript"></script>
        <script src="Projector.js" type="text/javascript"></script>
        <script src="CanvasRenderer.js" type="text/javascript"></script>

    </head>
    <body>
        <div id="myCanvas">
            <script>
                var scene;
                var camera;
                var renderer;
                initializeScene();
                renderScene();

                function initializeScene()
                {
                    if(Detector.webgl)
                        {
                            renderer=new THREE.WebGLRenderer({antialias:true});
                        }
                    else
                        {
                            renderer=new THREE.CanvasRenderer();
                        }
                    renderer.setClearColor(0x000000, 1); 
                    canvasWidth=window.innerWidth;
                    canvasHeight=window.innerHeight;
                    renderer.setSize(canvasWidth,canvasHeight);
                    document.getElementById("myCanvas").appendChild(renderer.domElement);
                    scene=new THREE.Scene();

                    camera = new THREE.PerspectiveCamera(45, canvasWidth / canvasHeight, 1, 100); 
                    camera.position.set(0, 0, 10); 
                    camera.lookAt(scene.position); 
                    scene.add(camera); 

                    var triangleGeometry=new THREE.Geometry();
                    triangleGeometry.vertices.push(new THREE.Vector3(0.0,1.0,0.0));
                    triangleGeometry.vertices.push(new THREE.Vector3(-1.0,-1.0,0.0));
                    triangleGeometry.vertices.push(new THREE.Vector3(1.0,-1.0,0.0));
                    triangleGeometry.faces.push(new THREE.Face3(0,1,2));

                    var triangleMaterial=new THREE.MeshBasicMaterial({
                        color:0xFFFFFF,
                        side:THREE.DoubleSide
                    });

                    var triangleMesh=new THREE.Mesh(triangleGeometry,triangleMaterial);
                    triangleMesh.position.set(-1.5,0.0,4.0);
                    scene.add(triangleMesh);

                    var squareGeometry=new THREE.Geometry();
                    squareGeometry.vertices.push(new THREE.Vector3(-1.0,1.0,0.0));
                    squareGeometry.vertices.push(new THREE.Vector3(1.0,1.0,0.0));
                    squareGeometry.vertices.push(new THREE.Vector3(1.0,-1.0,0.0));
                    squareGeometry.vertices.push(new THREE.Vector3(-1.0,-1.0,0.0)); // Updated vertices for square
                    squareGeometry.faces.push(new THREE.Face3(0,1,2));
                    squareGeometry.faces.push(new THREE.Face3(0,2,3)); 
                    var squareMaterial=new THREE.MeshBasicMaterial({
                        color:0xFFFFFF,
                        side:THREE.DoubleSide
                    });                 
                    var squareMesh=new THREE.Mesh(squareGeometry,squareMaterial);
                    squareMesh.position.set(1.5,0.0,4.0);
                    scene.add(squareMesh);


                }
                function renderScene()
                    {
                        renderer.render(scene,camera);
                    }
            </script>
        </div>
    </body>
</html>

Answer №1

One issue to note is that you are adding the same point twice:

squareGeometry.vertices.push(new THREE.Vector3(-1.0,1.0,0.0)); // <--- repeat
squareGeometry.vertices.push(new THREE.Vector3(1.0,1.0,0.0));  //    | this
squareGeometry.vertices.push(new THREE.Vector3(1.0,-1.0,0.0)); //    | same
squareGeometry.vertices.push(new THREE.Vector3(-1.0,1.0,0.0)); // <--- point

To correct this, consider replacing the duplicated point with:

squareGeometry.vertices.push(new THREE.Vector3(-1.0,-1.0,0.0));

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

Getting started with JavaScript arguments can be overwhelming for newcomers

Can you figure out why the variable 'a' doesn't increase by 1 in the following code snippet? var a = 5; function abc(y) { y++; } abc(a); // The value of 'a' remains 5, instead of increasing to 6. Why is that? However, when ...

Interacting with nested React components through the onMouse event

I am currently working on building a React component that will allow for highlighting when hovering over specific components. The challenge I am facing is with nested versions of these components - when the mouse hovers over the outer component, I want to ...

Utilize AJAX to update the quantity

I have attempted to update the rows in a table using AJAX, and it works for the first row but not for subsequent rows. Can someone assist me in implementing this functionality with AJAX for multiple rows? Here is an example code snippet from cart.php: &l ...

Obtain the selected node in FancyTree

When a button is clicked, I need to grab the current node that is in focus. In my attempt to achieve this, I utilized the getFocusNode() method within a click event handler like so: function retrieveFocusedNode() { var currentNode = $("#tree").fancy ...

Updating values within an ng-repeat loop by using the ng-change directive to increment or decrement

Below is an example code snippet that demonstrates a checkbox feature: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Welcome to LearnKode - A code learning platform</title> ...

Tips for transferring information between .vue files

After successfully logging in with the Prime Vue template, I have stored the user data in an object variable. Now, I need to access this user data on the profile page without importing a component. Can someone guide me on how to achieve this? Below are th ...

Expiration Date of Third-Party Cookies

I need help retrieving the expiration date of a third-party cookie programmatically using JavaScript. Even though I can see the expiry time in the browser's DevTools (refer to the screenshot at https://i.sstatic.net/BW072.png), I am struggling to figu ...

What is the correct way to define a variable in Jade?

My app functionality includes exporting user information once they are signed in, allowing the router to update the profile template. However, I am looking to enhance this by dynamically rendering different navigation bars based on the user's sign-in ...

Adding data to a bootstrap table dynamically with javascript

I came across some other posts that discuss a similar issue using jQuery, but I'm determined to stick with pure JavaScript for this. My goal is to construct a table using Bootstrap, however, I've hit a roadblock when it comes to adding new data t ...

Utilizing regex patterns within Jqgrid

I am currently utilizing the JqGrid plugin (version 4.6.0 - jQuery Grid) to filter my results using specific fields. Here is an example of how the GUI appears: https://i.sstatic.net/BqGai.png With a large list of employees, I want the ability to filter t ...

Assistance with Javascript and Jquery for Wordpress

Here's a snippet of my JavaScript code: jQuery(document).ready(function() { jQuery.wiseguys(); }); // To safely use the "$" sign, we are using a plugin structure (function($) { // This is the class constructor or "init" function $.wiseguys = fun ...

What is preventing the Delete icon button from being associated with the <select> element in this particular situation?

Here is an HTML markup snippet for a <table>: <table id="blacklistgrid_1" class="table table-bordered table-hover table-striped"> <thead> <tr> <th style="vertical-align:middle">Products</th> ...

Is there a way for me to retrieve the element that is linked to the ng-disabled attribute?

Is there a way to access the element with the ng-disabled attribute inside the function without passing it as a parameter? Here is the HTML element: <input class="needsDisabling" ng-disabled="isFieldDisabled()" type="text"> The isFieldDisabled fu ...

Utilizing Node.js alongside Express to efficiently serve a link for a .CSV file instead of triggering an immediate download

I have a project in progress where I am developing an application that retrieves JSON data from a database, converts it into a CSV file, and then presents it to the user based on specific parameters. Below is a snippet of code from the router in my Node se ...

Incorporate JSON information into a sleek Bootstrap modal interface

I am looking to load a JSON file to generate a list within a Bootstrap Modal. The setup I have allows for the modal to appear when clicking on a person's image. <li class="project span3" data-type="pfa"> <a data-toggle="modal" data-targe ...

Issue with submitting input fields that were dynamically added using jquery

I created a table where clicking on a td converts it into an input field with both name and value. The input fields are successfully generated, but they do not get submitted along with the form. In my HTML/PHP : <tr> <f ...

How can the Passport 'done' function be called in the rest of the code during authentication with passport.js?

I am currently working on integrating authentication into my API using passport.js with the passport_jwt strategy. Below is the code snippet for reference: const JwtStrategy = require('passport-jwt').Strategy, ExtractJwt = require('pa ...

What is the most effective way to display the length of an audio file while continuously decreasing the duration by one second

I've created a function where I intend to reduce every second audio duration by one. However, when I implement it, I keep getting NaN as the result. I even tried removing the colon from the time format so that it's just numbers but then the setIn ...

Reviewing for the presence of "Undefined" in the conditional statement within Transpiled Javascript code for

While perusing through some transpiled Angular code, I came across this snippet: var __decorate = (undefined && undefined.__decorate) || function (decorators, target, key, desc) { I'm puzzled by the usage of undefined in this context. Can an ...

What is the reason behind Node's error callback returning undefined first?

Whenever I try to run the error callback function, the code below returns undefined. I'm puzzled by why the JSON isn't being returned when the console log displays the entire query. Take a look at the function that is triggering the json to be l ...