Issue with retrieving objects from Three.js functions

Let's take a look at the function I've created.

function Planet(radius, c) {

    var sphere = new THREE.Mesh(
        new THREE.SphereGeometry(radius, 64, 64),
        new THREE.MeshLambertMaterial({ color: c })
    );

}

Using this function, I then instantiate an object.

var planet = new Planet(1, 0xffffff);

Next, I try to add the sphere mesh of the newly created planet object to the three.js scene.

scene.add(planet.sphere);

Although there are no errors in the Chrome JavaScript console, the sphere doesn't appear. Interestingly, if I create a sphere mesh outside of the Planet function and add it to the scene, it works perfectly fine. However, placing everything outside functions might not be ideal for managing multiple planets in the future. I'll need to figure out how to handle organizing arrays within functions moving forward.

Answer №1

experiment with

function SpaceBody(diameter, color) {

    this.orb = new THREE.Mesh(
        new THREE.SphereGeometry(diameter, 64, 64),
        new THREE.MeshLambertMaterial({ color: color })
    );

}

var planetoid = new SpaceBody(1, 0xffffff );
sky.add(planetoid.orb);

or

function SpaceBody(diameter, color) {

    return new THREE.Mesh(
        new THREE.SphereGeometry(diameter, 64, 64),
        new THREE.MeshLambertMaterial({ color: color })
    );

}

var planetoid = new SpaceBody(1, 0xffffff);
sky.add(planetoid);

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

Instructions for adding the more-vert icon from material-ui into a react project

I've been searching tirelessly, but I can't seem to locate it. Where exactly is the location of this in material-ui? I've seen others using it. Any assistance would be greatly appreciated. My initial thought was: import MoreVertIcon from & ...

Ensuring Jquery validation is triggered before submitting a form to PHP

I have been struggling with a particular issue and decided to seek help. I have a form filled with user data that I have successfully validated using js/jQuery before sending it to php for processing. Here is how I am achieving this: form.submit(functio ...

"Subscribing to a Meteor collection results in the entire collection being returned

I've been working on implementing a publish/subscribe architecture for my Meteor application. Although I have not removed the autopublish package yet, everything seems to be working smoothly with React and plugins like kadira:flow-router and kadira:re ...

Issue in Vue.js: Struggling to compile SASS styles for an HTML element generated dynamically

I'm working with a basic Vue.js component where I'm rendering a snippet of HTML: ... <div class="sml-button" v-on:click="toggleButton()" v-html="button"></div> ... When the button is clicked, the toggleButton() function updates the ...

Is there a built-in method called router.reload in vue-router?

Upon reviewing this pull request: The addition of a router.reload() method is proposed. This would enable reloading with the current path and triggering the data hook again. However, when attempting to execute the command below from a Vue component: th ...

How can the data from a factory be utilized within the same controller but in a separate file in an AngularJS application?

When trying to route from home.html to profile.html, there seems to be an issue with the execution of the profile.js script. I have data in script.js that is written in a factory. When the profile button is clicked, the page routes to profile.html, but the ...

Encountering error ORA-12514 when utilizing the node oracle-db npm package

At the moment, I am immersed in a project that relies on utilizing oracle for its backend. Following the instructions provided in this link, I successfully installed node-oracledb on my Mac using npm. The contents of my file are as follows: var oracledb = ...

What is the best way to retrieve a floating point array from a function?

The values of k in the output display as 0.000, but they should reflect the true values returned by funcTest(). #include <stdio.h> #include <stdlib.h> float *funcTest(int *a, int size) { float p[size]; int i; for(i=0; i< size; ...

Troubleshooting Millisecond Problems in JavaScript

According to the explanation on w3schools, Date.parse() is supposed to provide "the number of milliseconds between the date string and midnight of January 1, 1970." This means that if I enter Date.parse("January 1, 1970 00:00:00"), the result should be ...

Why is the axios.get promise not getting resolved?

I am currently working on fetching data from MongoDB atlas within a React app. However, despite using .then and .catch with axios.get(), I am encountering an unresolved promise. Here is the code snippet: const entry = axios.get('http://localhost:3001 ...

What is the reason behind this build error I am encountering while using react-three-xr?

I'm having trouble understanding this error message. What steps can I take to resolve it? Although I have included three-xr in my react app, I am encountering the following error: Failed to compile. ../../node_modules/@react-three/xr/src/DefaultXRCon ...

VARIABLE_NAME isn't functioning properly on the window

The code window.VARIABLE_NAME is not functioning properly and is causing the following error. Can you please provide assistance? Uncaught SyntaxError: Unexpected token. This is the code I have written: var window.testing ...

Understanding how to access a variable outside of a function in Node.js can be crucial for successfully passing

I am trying to figure out how to access the TRADE_OUT variable outside of a specific function in my application. Despite my efforts, I haven't been successful so far. I need the value of TRADE_OUT to be globally accessible within the entire applicatio ...

Discover the best practices for implementing MongoDB's latest Schema Validation functionality within your Express.js application

Need help implementing MongoDB's Schema Validation in an Express server? While working on a simple todo app, I opted for the native MongoClient over mongoose but still require a schema for my data. According to MongoDB's documentation available ...

Generate a Customized Modal for a Memo Capturer Program

I am currently developing a basic note-taking application using vanilla JavaScript. I want the program to add notes with a modal that displays the text when clicked. However, my current implementation adds the note below the input box and includes the moda ...

Injecting universal data into ValidationErrors in Angular

Trying to bind a value into ValidationErrors. Having this method: isUniqueEmail(control: FormControl): ValidationErrors { if (control.value != null) { console.log(control.value) if(control.value == this.foundEmail){ console ...

Turn off the chrome react DevTools when deploying to production to ensure the

I have successfully browserified my react app for production using gulp and envify to set up NODE_ENV. This has allowed me to remove react warnings, error reporting in the console, and even disable some features like the require of react-addons-perf. Afte ...

What steps should I take to ensure that the variable t is accessible globally, allowing it to be utilized in all functions?

I have set t as a global variable and assigned its value from the s1 and s2 functions, however, after person1, it does not seem to be transferring to person2. The error message is displayed below: File "C:\Users\Teja kaipa\Desktop\esti ...

The iFrame is set to a standard width of 300 pixels, with no specific styling to dictate the size

My current challenge involves utilizing the iframe-resizer package to adjust the size of an iframe dynamically based on its content. However, even before attempting any dynamic resizing, I encounter a fundamental issue with the basic iframe: it stubbornly ...

Experiencing a 403 Error while using request-promise to extract price data from a website

My current challenge involves using request-promise to scrape the price of an item from Asos.com. Whenever I try to execute the code provided below, I encounter a 403 error. Could it be possible for this error to occur even though the URL I am attempting t ...