I am facing an issue with ThreeJs where my objects are not getting rendered after being added to a

After refactoring some code, I encountered an issue where nothing was being displayed. Surprisingly, there were no errors in the console to guide me towards a solution. The code in question is supposed to generate the same output as a previously functioning version, but it fails to do so. Below is the code that's causing trouble.

<!DOCTYPE html>
<html>
<head>
</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 );

var objects = [
{
    name : "earth",
    geometry : new THREE.SphereGeometry(1, 32, 32),
    material : new THREE.MeshPhongMaterial(),
    mesh : new THREE.Mesh(this.geometry, this.material),
    init : function(scene){
    console.log("Wtf");
    this.mesh.position.set(0,0,0);
    //this.material.map = THREE.ImageUtils.loadTexture('../earth.jpg');
    scene.add(this.mesh);
    console.log("ugh");
    },
    animate : function(t){this.mesh.position.set(0+t/100,0+t/100,0+t/100);}
},
{
    name : "satellite",
    geometry : new THREE.SphereGeometry(0.1, 32, 32),
    material : new THREE.MeshPhongMaterial(),
    mesh : new THREE.Mesh(this.geometry, this.material),
    init : function(scene){
    console.log("Wtf");
    this.mesh.position.set(1.5,0,0);
    //this.material.map = THREE.ImageUtils.loadTexture('../earth.jpg');
    scene.add(this.mesh);
    console.log("ugh");
    },
    animate : function(t){this.mesh.position.set(1.5+t/100,0+t/100,0+t/100);}
}];
objects.forEach(object => object.init(scene));

var light = new THREE.HemisphereLight(0xf6e86d, 0x404040, 0.5);
scene.add(light);

camera.position.x = 0;
camera.position.y = -5;
camera.position.z = 0;
camera.lookAt(new THREE.Vector3( 0, 0, 0));

var render = function () {
    requestAnimationFrame( render );
    renderer.render(scene, camera);
}

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

Below is the code from a working example for comparison.

        <!DOCTYPE html>
        <html>
        <head>
        </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 );

        
        var earth_geometry = new THREE.SphereGeometry(1, 32, 32);
        var earth_material = new THREE.MeshPhongMaterial();
        var earth_mesh = new THREE.Mesh(earth_geometry, earth_material);
        earth_mesh.position.set(0,0,0);
        //earth_material.map = THREE.ImageUtils.loadTexture('../earth.jpg');
        scene.add(earth_mesh);
        

        var satellite_geometry = new THREE.SphereGeometry(0.1, 32, 32);
        var satellite_material = new THREE.MeshPhongMaterial();
        var satellite_mesh = new THREE.Mesh(satellite_geometry, satellite_material);
        satellite_mesh.position.set(1.5,0,0);
        //satellite_material.map = THREE.ImageUtils.loadTexture('../earth.jpg');
        scene.add(satellite_mesh);
        
        
        var light = new THREE.HemisphereLight(0xf6e86d, 0x404040, 0.5);
        scene.add(light);
        
        camera.position.x = 0;
        camera.position.y = -5;
        camera.position.z = 0;
        camera.lookAt(new THREE.Vector3( 0, 0, 0));
        
        var render = function () {
            requestAnimationFrame( render );
            renderer.render(scene, camera);
        }

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

I've attempted moving the scene.add function outside of the init method and manually updating it with scene.add(object[0].mesh), as well as changing 'objects' to 'const' and 'let', but none of these changes have resolved the issue.

Answer №1

The problem lies in this section:

mesh : new THREE.Mesh(this.geometry, this.material),

When the instantiation is evaluated as a property of an object without being called inside a function, `this` will refer to the global context or the parent `window`. In this case, this does not have the properties "geometry" or "material" defined. The value of `this` only changes when inside a function, where it refers to the object that the function is a property of (or whatever has bound it)

To resolve this issue without rewriting your code extensively, you can simply instantiate the geometry and material along with the mesh on the same line like so:

mesh: new THREE.Mesh(new THREE.SphereGeometry(1, 32, 32), new THREE.MeshPhongMaterial()),

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

Generating a JSON file using JavaScript

Currently, I am attempting to create a JSON file through JavaScript. Since I haven't found the appropriate code yet, I am experimenting with creating a text file instead. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html&g ...

Send the function with parameters, but do not pass its output

Within my component, there is a need property that holds a static function. This function is executed by middleware to handle asynchronous API calls before rendering the component. Here's an example: static need = [ myFunc(token) ] The issue arise ...

Separate a string by individual words and identify/insert line breaks

For the purpose of animating headings word by word, I am attempting to divide a string by spaces. However, my CMS outputs line breaks as \n within the string. const text = "Aenean non odio erat.\n Suspendisse vestibulum vulputate nulla et moll ...

Apply a specific CSS class to a division depending on the currently active cookie

I have implemented cookies on specific pages using https://github.com/carhartl/jquery-cookie. Could someone guide me on how to apply a CSS class to an ID (e.g., adding class .red to #mouse if the cookie named "socks" is active)? For example: If there is ...

The integration of hapi.js with mysql appears to be experiencing difficulty displaying values on the browser

My attempt to retrieve a list of data from mysql using hapi.js resulted in an error message: Error: method did not return a value, a promise, or throw an error However, I can see the data in my console. [ RowDataPacket { id: 1, code: 'test' ...

Load various types of classes and run functions with matching names

I have encountered a challenging issue that needs to be resolved. Imagine having multiple classes located in a directory named services. These classes all include a constructor() and send() method. Examples of such classes can be Discord, Slack, SMS, etc. ...

Can a gulpfile be written in ES6 syntax?

Question: Is there a way to write my gulp file in ES6 to utilize import instead of require, and use => syntax in place of function()? I have the option to use io.js or any version of node.js. gulpfile.js: import gulp from "./node_modules/gulp/index.j ...

Cease the use of jQuery animations

My JavaScript code snippet looks like this: $.get("/<page>.php", "userid='.$userid.'&"+status, function(data){ $("#status").show("fast").html(data).delay(4000).hide("fast"); }); On a page with multiple links triggering thi ...

Is React Context suitable for use with containers too?

React provides an explanation for the use of Context feature Context in React allows data sharing that can be seen as "global" within a tree of components, like the authenticated user, theme, or language preference. Although this concept works well for ...

Invalid prop type: A form field received a `checked` prop without a corresponding `onChange` handler

In my project, I have a Parent Component called CandidateList and a Child Component called Candidate. The CandidateList component has a Select All checkbox that triggers a function to set the state when candidates are being selected, and then passes that s ...

I am interested in displaying all the items on the list instead of just one

<html> <head> <link rel="stylesheet" type="text/css" href="mango.css"> <script> function showItems(){ var items = document.querySelectorAll("#main li"); items.forEach(i ...

The jQuery code I'm using is incorrectly selecting all elements with the class name "a2" when I click on them, instead of providing me with

I was hoping to see one a2 element displayed at a time when clicking on a div element, but instead, all a2 elements are appearing simultaneously. I would like to see only one a2 class element at a time upon clicking. <!DOCTYPE html> <html> < ...

Acquiring an array of fields using MongoDB Aggregate

Currently exploring the MongoDB aggregation framework, let's consider a collection structured like this: [ { _id: ObjectId(123) name: john, sessionDuration: 29 }, { _id: ObjectId(456) name: moore, sessionDuration: 45 }, ...

In my efforts, I am attempting to retrieve a value using JavaScript in order to send it back to PHP in JSON format

I have successfully developed a date selection feature with the chosen date displayed at the bottom (Second box in green). Now, I am looking to extract the user's selection in PHP and then send it in JSON format. Any suggestions or assistance on this ...

inserting information into HTML document

I've noticed that this particular method hasn't been addressed yet due to the document.GetElementsByClassName I'm using, but for some reason it's not working. My goal is to add a specific value (a string) to the page. I have located an ...

Having trouble with d3 / svg layering when introducing new nodes overtime

Struggling with a frustrating issue on my d3 force directed map project. Initially, I render the necessary nodes and links, then periodically check for updates through AJAX. The problem arises when adding new nodes and links – they appear over existing c ...

How can I implement a toggle button to display additional details for a specific row within a table created using React.js?

I'm currently working on a project using Next.js and have come across an unexpected issue. Upon reading a JSON file, I populate a table with the data retrieved from it. Each piece of information in the table has hidden details that should only be reve ...

How can I adjust the alignment of the text in a select menu in jQuery Mobile?

Exploring the world of jquery for the first time, I am excited about building a mobile web application. Utilizing jquery mobile (1.4.5), I encountered a simple issue - I couldn't figure out how to change the location of the text in the select menu. De ...

ajax duplicator and reset form tool

Hello everyone, I have a website where users can add their experiences. While adding an experience, they can dynamically add and remove more fields. One of the input fields is for a date, but when the data is submitted, the same date appears for all entrie ...

In my Nuxt project, I'm attempting to map a texture onto a spherical object using the Three.js library

I have been attempting to apply a texture to a sphere by using TextureLoader to map an image onto it However, I am encountering an issue where I am able to add the material successfully, but not the image. If I replace the line ""map: TextureLoader() ...