When attempting to display a basic mesh in Three.js, the render does not showcase a simple cube as expected

My objective is to display a simple plane and a cube, however, the cube is being hidden by the plane. The position of the cube is between the camera and the plane, but for some reason, the plane appears to be blocking the view of the cube.

This is how my scene looks without the plane:

And this is the scene with the added plane:

I believe that they are placed in such a way that the cube should be visible.

Below is the code I am using:


var scene = new THREE.Scene(),
    camera = new THREE.PerspectiveCamera(45, Window.innerWidth,window.innerHeight,0.1,1000),
    renderer = new THREE.WebGLRenderer({ antialias: true });

renderer.shadowMapEnabled = true;
renderer.shadowMapSoft = true;

var axes = new THREE.AxisHelper(20);
scene.add(axes);

var cubeGeometry = new THREE.CubeGeometry(4,4,4);
var cubeMaterial = new THREE.MeshBasicMaterial({color: 0x777777});
var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.position.x = 0;
cube.position.y = 10;
cube.position.z = 5;
scene.add(cube);

var planeGeometry = new THREE.PlaneGeometry(60,20);
var planeMaterial = new THREE.MeshBasicMaterial({color: 0x55cc88});
var plane = new THREE.Mesh(planeGeometry,planeMaterial);
plane.rotation.x = -0.5 * Math.PI;;
plane.position.x = 15;
plane.position.y = 0;
plane.position.z = 0;

camera.position.x = -30;
camera.position.y = 40;
camera.position.z = 30;
camera.lookAt(scene.position);

var spotLight = new THREE.SpotLight(0xffffff);
spotLight.position.set(-40,60,-10);
scene.add(spotLight);

renderer.setSize(window.innerWidth, window.innerHeight);
renderer.render(scene, camera);

$("#WebGl-salida").append(renderer.domElement);

Answer №1

Always be mindful of the names of objects and the correct order and parameters in methods:

camera = new THREE.PerspectiveCamera(45,Window.innerWidth,window.innerHeight,0.1,1000)

Instead, it should look like this:

camera = new THREE.PerspectiveCamera(45,window.innerWidth / window.innerHeight,0.1,1000)

Check out this jsfiddle for an example.

Answer №2

Thank you so much! I appreciate both the helpful answer and the suggestion to improve my question for better clarity. It's now working flawlessly thanks to your guidance.

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

Conceal all elements until a search query is entered using the search bar

I want to create a search bar that hides all elements until the user actually searches for them, you can check out my JSfiddle for reference: `JSfiddle Link <div id="search"> <form> <input type="text" name="search" id="m ...

Look for identical values within a nested array

My data consists of a nested array where each element has a property called name, which can only be either A or B. I need to compare all elements and determine if they are all either A or B. Here is an example of the input: [ { "arr": { "teach ...

What is the process of exporting a Vue component into a Word document?

My Vue template structure is as follows- <template> <div id="printContainer"> <componentA></componentA> <div style="page-break-before:always">&nbsp;</div> <componentB></comp ...

Transform JavaScript Object into a string representation

I am currently working on a project involving Angular 8. I need to post values from a textArea and receive an Object in response from an API, which looks like this: API response. My challenge is accessing the "raisonSociale" property within the object. Be ...

The issue of passing state in React Router v4 Redirect unresolved

I have a specific private route, /something, that I only want to be accessible when logged in. I've used Redirect with the state parameter set, however, when I try to access it at the destination, location.state is showing as undefined. Here is how I ...

Stop the promise chain when the first error callback is encountered

Here is a sequence of promises I am dealing with: Transaction.findPublic({}).then(function(transactions) { combined = combined.concat(transactions); return JoinEvent.find().exec(); }, function(err) { return res.status(503).send(er ...

Unable to retrieve data from a nested object within a JSON structure

In my React project, I have created a function component like the one below: function FetchData() { const [randomUserData, setRandomUserData] = useState(''); const url = 'https://randomuser.me/api/'; const fetchData = () => { ...

Using Selenium to assign properties to JavaScript Objects

I have a JavaScript Object that needs to be set using Selenium WebDriver. var examplePlayResponse = { "prizeIndex" : 1, "mode" : "NORMAL", "id" : "abc123", "version" : "1.0", "gameCode" : "xyz789", "randomSeed" ...

Which is better for integrating Google Maps API - JavaScript or PHP?

Is it better to use JavaScript or PHP with the Google Maps API? What are the advantages and disadvantages of each? If I have geocodes stored in a database, should I retrieve them using Ajax and process them with JavaScript, or is PHP a better option? The ...

I'm struggling with an issue of being undefined in my React.js project

This code snippet is from my app.js file import React, { useState, useEffect } from "react"; import { v4 as uuid } from "uuid"; import "./App.css"; import Header from "./Header"; import AddContact from "./AddCo ...

Determine which button is active using Selenium

I am looking to determine which option is currently selected in the code snippet below: <div class="col-xs-12 noPadding details AdminDetails" data-value="INVITE" xpath="1"> <div class="col-xs-8 left"> ...

How do I iterate through my state in React Redux?

Currently, I am delving into Redux by creating a to-do list in React. I have been pondering on the utilization of the map function to iterate over the state so that I can display the data stored within different div elements. Here is my initial state: cons ...

When the request's credentials mode is set to 'include', the 'Access-Control-Allow-Origin' header in the response should not be using the wildcard '*'

I am encountering an issue with my socket.io server as I am unable to connect to it from my local HTML file on my Mac. Error: Failed to load : The 'Access-Control-Allow-Origin' header in the response is causing a problem due to the wildcard ...

Organizing your project with VueJS, VuelidateJS, and NodeJS/Express for optimal structure

Primarily, I specialize in developing intranet sites and web front-ends for embedded devices using NodeJS. Currently, all my code is contained within one NPM package. I have NodeJS running behind Nginx, allowing Nginx to serve css/image/client-side-javasc ...

The session data is not persisting in the express-session package

I am currently learning about HTTPS and working on implementing a login/logout function. In this function, I store the userId in the session when I login using the POST method. However, when I try to retrieve user information for the next components usin ...

What is the process for transferring an existing collection or data in Firestore to a subcollection?

My firestore database currently has the following structure with documents: root | |---transactions/... I am looking to transfer all transactions to a new subcollection as shown below: root | |---users/user/transactions/... Any suggestions on how I can a ...

Enhance your HTML audio player with added timeline functionality

I'm currently working on incorporating an HTML audio player into my project. I've managed to get the play/pause functionality to work, but now I'm stuck on adding a timeline feature. Additionally, I'm not sure how to implement the play/ ...

Encountered a null error while utilizing ES6 computed state in React components

In my .jsx file, I am encountering an issue with the following code block: <tr> {!isEmpty(content) && content.map(o => { if(o.sortKey){ console.log(this.state[`order${o.sortKey}`]) } })} </tr> After making chan ...

A guide to displaying a DIV element upon clicking a button in ReactJs

While developing a form, I encountered the need to render the same div in the "Active Membership" section whenever the "Add More" button is pressed. Since users can have multiple active memberships, each time the button is clicked, the input section shou ...

Having a parameter that contains the characters '&' and '&' can potentially disrupt an AJAX call

Even though there is a similar question here: Parameter with '&' breaking $.ajax request, the solutions provided do not apply to my specific issue. This is because both the question and answers involve jQuery, which I am not familiar with. I ...