The rendered output in three.js is failing to appear on the screen

I recently started experimenting with three.js (and don't have much experience with it). I followed a tutorial that instructed a green box to be displayed on the screen. Despite not encountering any errors in the console, all I see is a blank black screen. https://i.sstatic.net/vkmZy.png

This is my index.html

   <!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <link rel="icon" type="image/svg+xml" href="/vite.svg" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Vite App</title>

  <style>
    body {
      margin: 0;
    }
  </style>
</head>

<body>
  <div id="app"></div>

  <script type="module" src="/main.js">




  </script>

  <script async src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f2a3c6222202b3a232a623c2726223c0f7e617c6179">[email protected]</a>/dist/es-module-shims.js"></script>

  <script type="importmap">
    {
      "imports": {
        "three": "https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="097d617b6c6c493927383b3f3a48393430395467783f393d">[email protected]</a>/build/three.module.js"
      }
    }
  </script>


</body>

</html>

This is my MAIN.JS file

import * as THREE from "https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="95e1fde7f0f0d5a5bba4a7a3bba4788bc0cdcccaccdccfcdcbc6ece1ccd0cfc8cacadc78ededfbddfdf4feefeae8fbeabaafaacabaaefdecbe7ebbfbbccececdbdcfaeeaeeeaea">[email protected]</a>/build/three.module.js";

  const scene = new THREE.Scene();


  
  const camera =new THREE.PerspectiveCamera(75, innerWidth / innerHeight, 0.1,1000)
        
  const renderer= new THREE.WebGL1Renderer();
  
  console.log(scene);
  console.log(camera);
  console.log(renderer);

  document.body.appendChild(renderer.domElement);

  renderer.setSize(innerWidth , innerHeight); 

  const boxGeometery=new THREE.BoxGeometry(1,1,1) // width length and height

  const material= new THREE.MeshBasicMaterial({ color : 0x00f00 })

  const mesh=new THREE.Mesh(boxGeometery , material);

  scene.add(mesh);

  renderer.render(scene,camera)

  camera.position.z=5;


  console.log(mesh)
  console.log(material)
  console.log(boxGeometery);

NOTE = I did not install three.js with npm, but rather included the cdn link.

The instructor's output showed a green box, while mine does not display such a box on the screen.https://i.sstatic.net/72CNO.png

If anyone could offer some assistance, I would greatly appreciate it

Answer №1

You should incorporate an animation loop and ensure you are using the correct color code (make sure to include another "f").

import * as THREE from "https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fc88948e9999bcccd2cdcecad2cd">[email protected]</a>/build/three.module.js";


let scene, renderer, camera;


scene = new THREE.Scene();

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

renderer = new THREE.WebGL1Renderer();

document.body.appendChild(renderer.domElement);

renderer.setSize(innerWidth, innerHeight);

const boxGeometery = new THREE.BoxGeometry(1, 1, 1) // width length and height

const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 })

const mesh = new THREE.Mesh(boxGeometery, material);

scene.add(mesh);

camera.position.z = 5;
renderer.setAnimationLoop(loop);


function loop() {
    renderer.render(scene, camera)
}

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 background image is not appearing on the div as expected

I've been attempting to create a div and set a background image for it using jQuery, but I seem to be facing issues. When I try setting the background color to white, it works fine. Here's the code snippet: function appendToDom(poster) { ...

Using Three.js to display a PerspectiveCamera with a visible bounding Sphere

My challenge is to create a Scene that loads a single .obj file where the object is fully visible in the PerspectiveCamera upon scene initialization. The field of view (FOV) is set at 60 The objects vary in size TrackballControls are utilized for camera ...

using JavaScript to send numerous text box values to various views

I have a dilemma with passing values between two views. In View1, there are text boxes for entering basic information. After the customer enters this data and clicks on 'add more details', I want to transfer these details to the text boxes in Vie ...

When dynamically adding input data, the serialized array may not successfully pass through Ajax

function SubmitData(){ var postData = $("#xForm").serializeArray(); $.ajax({ type: "POST", url: "mail.php", data: postData, success:function(data){ console.log(data); }, error: function(jqXHR, textStatus, errorThrown ...

Performing a PHP Curl request and an ajax query to an ASP.NET page

I am attempting to send an ajax query to an ASP.NET page. Here is the algorithm I am following: 1. There is a form on my webpage; 2. When the user fills in all the fields, they click the submit button; 3. Upon clicking the submit button, JavaScript sends ...

Customized HTML Template Tag

While I understand that using custom html tags is generally frowned upon for various reasons, I have a specific scenario that I believe might warrant the use of a custom html tag. I hope to either be proven wrong or discover a better way of achieving my ob ...

Refresh jQuery CSS for active button whenever a different button is clicked

I want to enhance a group of buttons using jQuery. I aim to make the active button visually stand out so that users can easily identify it. These buttons are being created through a PHP foreach loop, as shown below: foreach($result as $row){ echo "< ...

The function from a different .js file cannot be located in NodeJS

I'm brand new to working with Javascript and NodeJS. This is the structure of my website's code: |source | | stylesheets | | | index.styl | | templates | | default.jade | | homepage.jade |static | | [.css generated] | | ...

Find elements in an array that match certain key values

Currently, I am working with an array that requires me to filter out specific keys without looping through them. I have created separate filters for each key needed: this.filteredCampaigns = this.allCampaigns.filter( (item) => item.status?.includes(tr ...

Switch background color multiple times on click using JavaScript loop

Hello amazing people! I have a container with 52 small boxes and one large box containing a letter. The smaller boxes are arranged around the larger one using CSS grid, and when hovered over, they turn light grey. My Objective When any of the 52 small b ...

Creating a dynamic 3D pie chart with Highcharts and JSON data

I am attempting to create a 3D pie chart using HighChart with JSON data. Despite enabling the 3D option, it only displays in 2D. Here is an example of the 3D pie chart I am trying to achieve: https://www.highcharts.com/demo/3d-pie Could someone please he ...

Develop a fresh behavior on-the-fly

Here is the HTML code snippet: <div class="bold knowmore login" id="j-6"> <span>...</span> </div> and this jQuery script: $(function(){ $(".login").on("click", function(){ console.log('login clicked!'); $(" ...

Breaking down and analyzing XML information

I have data that I need to retrieve from an XML file, split the result, parse it, and display it in an HTML element. Here is a snippet of the XML file: <Root> <Foo> <Bar> <BarType>Green</BarType> &l ...

Leveraging Ajax with Google Analytics

Currently, I am working on a website that utilizes Ajax calls to update the main content. In order to integrate Google Analytics tracking code using the async _gaq method, I need to push a _trackPageview event with the URI to _gaq. There are two approaches ...

Exception thrown by the 'upload' method of the Krajee file-input widget

I have been using the fileinput widget from Krajee at this link: However, I seem to be encountering an issue with the 'upload' method. Everything works perfectly fine when I upload files by clicking the upload button. But when I try to use the u ...

AngularJS variable assignment with HTTP GET operation

The angular filter I have set up is functioning perfectly: categorieFilter = angular.module("categorieFilter", []) categorieFilter.controller("catFilter", ["$scope", "store", function($scope, store){ $scope.search = ""; $scope.products = []; $ ...

Looking for guidance on JavaScript - Implementing a different font family for each div element

I need help with customizing the font family for each individual post on my website. Currently, I am able to assign a cursive font to all posts within a class, but I would like each post to have its own unique font. Since I am doing this on load, I am fa ...

Storing information in the DOM: Choosing between Element value and data attribute

When it comes to storing values in a DOM element, we have options like using the data attribute. For example, $("#abc").data("item", 1) can be used to store values and then retrieve them with $("#abc").data("item"). However, I recently discovered that th ...

The absence of a closing parentheses in the argument is causing an issue when rendering

Apologies for the basic mistake, but I could really use some assistance with this syntax error. I've spent over an hour searching for it and still haven't been able to locate the issue. It seems to be around the success function(data) section. Th ...

Transform the Nodejs server into a reusable Node module

Can a nodejs API server be transformed into a node module for use in other projects with minimal code modifications? Additional Information: The node js server contains various APIs (get, post, put). If I integrate this server as a node module within anot ...