Looking at a 3D wireframe cube using three.js

I am a new three.js user and still learning Javascript. I successfully completed the "Getting Started" project on threejs.org, which consisted of a rotating cube, with no issues. However, when I attempted to add a wireframe to the project, it suddenly stopped working.

Can someone please assist me in identifying what went wrong? The code snippet is provided below.

Thank you!

<html> 
<head> 
<title>My first Three.js app</title> 
<style> 
body { margin: 0; } canvas { width: 100%; height: 100% } 
</style> 
</head> 
<body>
<script src="http://threejs.org/build/three.min.js"></script> 
<script> 
var scene = new THREE.Scene(); 
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 ); 

var renderer = new THREE.WebGLRenderer(); renderer.setSize( window.innerWidth, window.innerHeight ); 
document.body.appendChild( renderer.domElement );

geometry = new THREE.BoxGeometry( 10, 10, 10, 2, 2, 2 ); 
material = new THREE.MeshBasicMaterial( { color: 0xff0000 } ); 
object = new THREE.Mesh( geometry, material ); 

wireframe = new THREE.WireframeHelper( object, 0x00ff00 );
 
scene.add( object ); 
scene.add( wireframe );

camera.position.z = 5; 

var render = function () { 
requestAnimationFrame( render ); 
cube.rotation.x += 0.05; 
cube.rotation.y += 0.05; 
renderer.render(scene, camera); 
}; 

render(); 
</script> 
</body> 
</html>

Answer №1

Make sure to update your variable names, as you still had a reference to a cube in the render loop even though you changed it to object. Be sure to check your JavaScript console for any errors. I also adjusted the camera position so it is not inside the cube.

var scene = new THREE.Scene(); 
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 ); 

var renderer = new THREE.WebGLRenderer(); renderer.setSize( window.innerWidth, window.innerHeight ); 
document.body.appendChild( renderer.domElement );

geometry = new THREE.BoxGeometry( 10, 10, 10, 2, 2, 2 ); 
material = new THREE.MeshBasicMaterial( { color: 0xff0000 } ); 
object = new THREE.Mesh( geometry, material ); 
wireframe = new THREE.WireframeHelper( object, 0x00ff00 );

scene.add( object );
scene.add( wireframe );

camera.position.z = 15; 

var render = function () { 
requestAnimationFrame( render ); 
object.rotation.x += 0.05; 
object.rotation.y += 0.05; 
renderer.render(scene, camera); 
}; 

render();
html, body, canvas { margin: 0; padding: 0; display: block; width: 100%; height: 100% }
<html> 
<head> 
<title>My first Three.js app</title>
</head> 
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r73/three.min.js"></script> 
</body> 
</html>

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 issue arises in React when input elements fail to render correctly following a change in value, specifically when the keys remain identical

Click here to view the code sandbox showcasing the issue The code sandbox demonstrates two versions - a working one where Math.random() is used as the key, and a not working one where the index of the array is used as the key. When the array this.state.v ...

The process of compressing font files (such as ArialMT.ttf) using JSZip is experiencing issues

While zipping images and HTML files works smoothly, I encountered an issue when trying to add font files for CSS. The font file is only 1kb in size, but it cannot be opened. Even after attempting to zip the font without any other files, the problem persis ...

Retrieving data efficiently: utilizing caching in an AngularJS service to store data fetched from a single $http request

Forgive me if this question seems simple or silly, but I find myself in a new scenario and need some guidance. In my AngularJS app, I have a service with 4 methods that share about 80% of the same functionality and code. I want to optimize this and make it ...

Make sure the auto import feature in TypeScript Visual Studio Code Editor is set to always use the ".js" extension

At times, the auto-import completion feature includes the .js extension, but inconsistently. When this extension is missing in the TypeScript source, the emitted JavaScript file may encounter runtime issues like module not found error since the tsc compile ...

What causes the appearance of a nested URL like '/auth/auth/ ' when the original URL does not exist?

While following a tutorial, I encountered an issue where if the URL is not included in the routes.ts file, it redirects to a nested /auth/auth/...../login/ instead of redirecting to localhost:3000/auth/login middleware.ts import authConfig from "./au ...

Tips for styling tooltips in rc-slider

I am currently using the react library rc-slider and I am looking to add a tooltip feature to display the current value. I found guidance on how to do this from a post on the rc-slider GitHub page. However, I encountered an issue where my slider position b ...

The Bootstrap modal is making the content below shift to the left

Encountering an issue with our Bootstrap modal causing the content to align left, specifically on certain Macbooks using Chrome. Interestingly, the problem is present on my co-worker's computer but not on mine, even though the specifications are the s ...

Clearing selections from a multiple-choice input field

I am seeking to implement a delete feature in a multi-select element similar to the functionality seen here on stackoverflow when selecting multiple tags for a question. Once an item is selected, I would like to display a close icon next to it so that user ...

Is it possible to send emails from a local server to Gmail, Yahoo, or Rediff?

Currently, I am developing a feature that allows users to send emails to any recipient including Yahoo and Gmail. Below is the code snippet for my contact form: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1 ...

Forcing UTF-8 Encoding in JavaScript

I came across this helpful article: While following the advice of many others suggesting to use unescape(encodeURIComponent(srt)), I encountered issues. Attempting to convert the file obtained through XMLHttpRequest did not yield the desired results. Pri ...

Wait for the scope value to become available before executing the directive or div

I have a unique directive created for SoundCloud that necessitates the SoundCloud URL. The URL is fetched from the database using the $http service, but the issue arises when the div for the directive is loaded before the URL value is defined. The code fo ...

Steps for programmatically closing a Dialog Window in a showmodeldialog window

When opening the window, I follow this approach: var MyArgs = new Array(ParmA, ParmB, ParmC, ParmD, ParmE, ParmF); var leftpost = getWindow_TotalWidth() - 1000 - 100; var WinSettings1 = "dialogHeight:580px; dialogWidth:950px;edge:Raised; center:Yes; resi ...

Using conditional rendering within the map function in React

I am working with the code snippet below and I am looking to implement a conditional rendering to exclude index 0 from being displayed. How can I achieve this? return ( <section> {pokemonCards.map((pokemon, index) => ...

Index similar to JavaScript-Meadow

Is there a way to create a table of contents like the ones seen on sites such as JavaScript Gardens? How do they determine which section is currently active and are there any recommended JavaScript libraries that can help achieve this functionality? Edit: ...

"Enhance your webpage with a captivating opaque background image using Bootstrap

I'm new to exploring Bootstrap and I am currently experimenting with options for displaying content with a semi-transparent background image. Currently, I am using a "well" but I am open to other suggestions. I have managed to place the image inside t ...

steps for including preset text in an input field within a form on WordPress

I am currently utilizing a WordPress plugin that automatically generates a form in the admin area based on user input. The form includes five fields that require URL validation. Users have the option to enter optional link text which will be displayed as t ...

JavaScript module encounters an uncaught error: Attempting to assign a value to a constant variable

In another module, I defined a variable in the following manner: // module1.js let directory; export { directory }; Now, I am trying to access it in a separate module like so: // module2.js import { directory } from '../js/module1.js'; directo ...

Error encountered with structured array of objects in React Typescript

What is the reason for typescript warning me about this specific line of code? <TimeSlots hours={[{ dayIndex: 1, day: 'monday', }]}/> Can you please explain how I can define a type in JSX? ...

There was an issue with the JSON parsing process due to an unexpected token 'o' at position

Trying to extract titles from a JSON object for a specific feature, here's an example of the JSON structure: [ { "title": "Example 1", "url": "http:\/\/www.example1.com\/" }, { "title": "Example 2", "url": "http:& ...

Express router parameter matching

Let's consider a scenario where I have two routes - one with parameters and one without: /foo?bar /foo I aim to assign different handlers for these routes. Instead of the conventional approach, I am looking for a way to simplify the code. app.use(&a ...