Models of Earth and the Sun are failing to render

After numerous attempts at debugging, I am stuck on an issue with my Earth and Sun models not rendering correctly. They are displaying as a dull filled color despite having a texture applied to the sphere.

If you could take a look at lines 104 - 141 where I am creating the Sun and Earth models, that would be greatly appreciated.

Additionally, I would appreciate some guidance on my Animate function.

Here is the current code:

// Standard Variables / To be changed later.
var scene, camera, renderer //, container;
var W, H;
var delta = Math.delta;

W = parseInt(window.innerWidth);
H = parseInt(window.innerHeight);


camera = new THREE.PerspectiveCamera(45, W / H, 1, 1000000);
camera.position.z = 36300;
scene = new THREE.Scene();

// renderer
renderer = new THREE.WebGLRenderer();
renderer.setSize(W, H);
document.body.appendChild(renderer.domElement);

// Adding Stars.
var starsGeometry = new THREE.Geometry();
var starsMaterial = new THREE.ParticleBasicMaterial({
  color: 0xbbbbbbb,
  opacity: 0.6,
  size: 1,
  sizeAttenuation: false
});
var stars;

// Adding stars to the Scene.
for (var i = 0; i < 45000; i++) {
  var vertex = new THREE.Vector3();
  vertex.x = Math.random() * 2 - 1;
  vertex.y = Math.random() * 2 - 1;
  vertex.z = Math.random() * 2 - 1;
  vertex.multiplyScalar(7000);
  starsGeometry.vertices.push(vertex);
}

stars = new THREE.ParticleSystem(starsGeometry, starsMaterial);
stars.scale.set(50, 50, 50);
scene.add(stars);


// ------------------------------------------------------------ ...
// - The rest of the code remains the same -

body {
  background: whitesmoke;
  margin: 0px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/109/three.min.js"></script>

For reference:

https://i.sstatic.net/dO4ry.png

Answer №1

Upon running your code, I encountered numerous errors.

THREE.ParticleBasicMaterial has been renamed to THREE.PointsMaterial.
THREE.ParticleSystem has been renamed to THREE.Points.
THREE.ParticleBasicMaterial has been renamed to THREE.PointsMaterial.
THREE.ParticleSystem has been renamed to THREE.Points.
THREE.ImageUtils.loadTexture has been deprecated. Use THREE.TextureLoader() instead.
THREE.ImageUtils.loadTexture has been deprecated. Use THREE.TextureLoader() instead.

It is crucial to rectify these errors.

Otherwise, the intended purpose of this code remains unclear.

sun_mat = new THREE.MeshPhongMaterial();
sun = new THREE.Mesh(sun_geom, sun_mat);
sun_mat = THREE.ImageUtils.loadTexture('images/sunmap.jpg');

The code creates a material, assigns it to THREE.Mesh, then attempts to create a texture that is not utilized and reassigns sun_mat to that texture, which serves no purpose.

To address this, I made the following modifications:

const loader = new THREE.TextureLoader();

//Sun
var sun, gun_geom, sun_mat;
sun_geom = new THREE.SphereGeometry(2300, 80, 80);
sun_mat = new THREE.MeshPhongMaterial({
  emissive: 0xffffff,
  emissiveMap: loader.load('https://threejs.org/examples/textures/waterdudv.jpg'),
});
sun = new THREE.Mesh(sun_geom, sun_mat);
scene.add(sun);


var geometry = new THREE.SphereGeometry(2300, 80, 80);
var texture2 = loader.load('https://threejs.org/examples/textures/planets/earth_atmos_2048.jpg');

var material = new THREE.MeshPhongMaterial({
  emissiveMap: texture2,
  emissive: 0xffffff,
});
var earth = new THREE.Mesh(geometry, material);
earth.position.set(5000, 0, 0);

scene.add(earth);

Furthermore, I replaced map with emissiveMap. Additional lighting sources, other than AmbientLight, must be added for map to function correctly.

To ensure smooth rendering, I shifted the earth like so:

earth.position.set(5000, 0, 0);

Additionally, in the render loop, the following line was found:

earth.rotation.y += 1 / 16 * delta;

However, delta was defined as:

var delta = Math.delta;

Since there is no such thing as Math.detla, delta remains undefined, causing

earth.rotation.y += 1 / 16 * delta;
to result in NaN, leading to the disappearance of the earth mesh. I set delta = 1 as a quick fix.

For thorough guidance on three.js, consider referencing this informative article, which aligns with version 109 of the library without using outdated classes referenced in your initial code snippet.

// Standard Variables / To be changed later.
var scene, camera, renderer //, container;
var W, H;
var delta = 1.; // Math.delta;

W = parseInt(window.innerWidth);
H = parseInt(window.innerHeight);

camera = new THREE.PerspectiveCamera(45, W / H, 1, 1000000);
camera.position.z = 36300;
scene = new THREE.Scene();

renderer = new THREE.WebGLRenderer();
renderer.setSize(W, H);
document.body.appendChild(renderer.domElement);

// Adding Stars.
var starsGeometry = new THREE.Geometry();
var starsMaterial = new THREE.PointsMaterial({
  color: 0xbbbbbbb,
  opacity: 0.6,
  size: 1,
  sizeAttenuation: false
});
var stars;

// Adding stars to the Scene.
for (var i = 0; i < 45000; i++) {
  var vertex = new THREE.Vector3();
  vertex.x = Math.random() * 2 - 1;
  vertex.y = Math.random() * 2 - 1;
  vertex.z = Math.random() * 2 - 1;
  vertex.multiplyScalar(7000);
  starsGeometry.vertices.push(vertex);
}

stars = new THREE.Points(starsGeometry, starsMaterial);
stars.scale.set(50, 50, 50);
scene.add(stars);

// ------------------------------------------------------------

var starsGeometry2 = new THREE.Geometry();
var starsMaterial2 = new THREE.PointsMaterial({
  color: 0xbbbbbbb,
  opacity: 1,
  size: 1,
  sizeAttenuation: false
});
var stars2;

// Adding stars to the Scene.
for (var i = 0; i < 10000; i++) {
  var vertex = new THREE.Vector3();
  vertex.x = Math.random() * 2 - 1;
  vertex.y = Math.random() * 2 - 1;
  vertex.z = Math.random() * 2 - 1;
  vertex.multiplyScalar(7000);
  starsGeometry2.vertices.push(vertex);
}

stars2 = new THREE.Points(starsGeometry2, starsMaterial2);
stars2.scale.set(70, 150, 100);
scene.add(stars2);

var ambient = new THREE.AmbientLight(0x222222);
scene.add(ambient);

// ------------------------------------------------------------

const loader = new THREE.TextureLoader();

//Sun
var sun, gun_geom, sun_mat;
sun_geom = new THREE.SphereGeometry(2300, 80, 80);
sun_mat = new THREE.MeshPhongMaterial({
  emissive: 0xffffff,
  emissiveMap: loader.load('https://i.imgur.com/gl8zBLI.jpg'),
});
sun = new THREE.Mesh(sun_geom, sun_mat);
scene.add(sun);

var geometry = new THREE.SphereGeometry(2300, 80, 80);
var texture2 = loader.load('https://i.imgur.com/BpldqPj.jpg');

var material = new THREE.MeshPhongMaterial({
  emissiveMap: texture2,
  emissive: 0xffffff,
});
var earth = new THREE.Mesh(geometry, material);
earth.position.set(5000, 0, 0);

scene.add(earth);

var t = 0;

document.addEventListener('mousemove', function(event) {
  y = parseInt(event.offsetY);
});

animate();

function animate() {
  requestAnimationFrame(animate);

  sun.rotation.y += 0.001;
  earth.rotation.y += 1 / 16 * delta;

  camera.lookAt(scene.position);

  t += Math.PI / 180 * 2;

  renderer.render(scene, camera);
}
body {
  background: whitesmoke;
  margin: 0px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/109/three.min.js"></script>

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

To use the ModuleWithProviders<T> in the angular-autofocus-fix package, you must provide 1 type argument

Upon successful installation of angular-autofocus-fix I have imported the AutofocusModule However, upon running the Angular project, I encountered the following error: ERROR in node_modules/angular-autofocus-fix/index.d.ts:4:23 - error TS2314: Generic ty ...

A marker popup in React with Leaflet closes immediately upon clicking when using leaflet-pixi-overlay

Currently, I am developing a leaflet map using React and PixiOverlay for better performance while drawing markers. However, I have encountered an issue with handling popups while working on the code below: The Marker's click event triggers correctly ...

Redirecting users from one page to another using Javascript along with

I am currently working on a project that employs the use of a PHP <? header('Location: <a href="http://url.com" rel="nofollow">http://url.com</a>'); ?> for handling redirects. I find this method particularly effective because of ...

The functionality of using multiple inputs with Google Places API is not functioning as expected

Having trouble with the Google Place API. I am unable to set up 2 input fields with autocomplete. The first input is populated from a payload received from the backend, while the second input is within a Bootstrap modal. I have tried various solutions fou ...

Functional Components with Methods in ReactJS

When creating a functional stateless component that requires methods to access props, is there a recommended approach or best practice to follow? For instance: function Stateless(props) { function doSomething(props) { console.log(props); } ...

Encountering a client component error with the app router in Next.js version 13.4.9

Encountering an error in Nextjs that persists until the 'use client' directive is removed. Warning: Rendering <Context.Consumer.Consumer> is not supported and will be removed in a future major release. Did you mean to render <Context.Con ...

Utilizing Vue alongside Laravel by passing null values as props

Is it permissible to provide a null prop? I have created a main component that accepts the user prop. <div id="app"> <master :user="{{ $user }}"></master> </div> The prop is declared as follows: props : { user: { ...

The process of updating a nested object property in Redux and React

Initially, the user object is established with properties such as name, color, and age using the SET_USER method. I need to modify the name property within the user object utilizing UPDATE_USER_NAME. However, despite trying a nested loop within UPDATE_USER ...

Processing the results from a recursive function call in Node.js

I have an objects array that needs to be processed with delayed calls to an external server, then collect all results into an array for postprocessing. Here is an example: (async () => { const serverOperation = () => new Promise(resolve => setT ...

Discontinuing the fieldset tab interface feature from a Dexterity content type

I am looking to modify a condition to prevent the loading of certain javascript code when inserting an object of my content type. The current condition works only when editing the object: <?xml version="1.0"?> <object name="portal_javascripts"> ...

Reacting - page is not refreshing

Describing my current scenario: I have a requirement where I need to be able to navigate to a /details page by clicking on an image. However, when I click on the image, the URL gets refreshed but my component does not load. I attempted various solutions ...

Can you identify the HTML table format and suggest effective web scraping methods?

I have been attempting to retrieve data from the following link, http://www.rchsd.org/doctors/index.htm? strt = 0 & ln = & fn = & sp = & grp = & loc = & lng = & gen = , using R but finding it quite challenging. I have observed that the URL remains constan ...

display a dual-column list using ngFor in Angular

I encountered a situation where I needed to display data from an object response in 2 columns. The catch is that the number of items in the data can vary, with odd and even numbers. To illustrate, let's assume I have 5 data items to display using 2 co ...

Is the jQuery form plugin not passing any data to Node.js?

Check out the HTML form below: <form id="importForm" enctype="multipart/form-data"> <p> <label for="ownerName">Owner Name<pow class="requiredForm ...

Is your JQuery Gallery experiencing issues with the next button function?

I'm working on developing a simple gallery using JQuery. The main concept is to have all image files named x.png (where x is a number), and the program will then add a number to the current one, creating x+1.png and so forth. Here's the code I ...

The perplexing orientation of text nestled within a celestial background

I'm having trouble understanding the behavior of the a-text primitive in A-Frame 0.8.0. I've noticed two simple cases: When a-text is directly inside the scene, it works as expected (view fiddle) <a-scene> <a-text value="some tex ...

Sending data to server using Ajax and jQuery

Hey there, experiencing a little hiccup in the back-end of my system when I try to submit my form. It keeps showing me an error saying Unidentified index: file1 . Can't seem to pinpoint where the issue lies in my code. Even though I'm no beginner ...

What are the steps for releasing a Next.js app as an npm package?

Currently, my web application is developed using Next.js. I am interested in distributing it as an npm package for others to utilize in their projects. Despite my efforts to search and seek assistance through Google, I have not come across any valuable ins ...

Instructions on activating dark mode with the darkreader plugin on a Vue.js website

Is there a way to implement DarkMode in a Vue.js application? I attempted to integrate darkmode using this npm package, but I kept encountering the error message: DarkMode not defined. ...

Retrieve all default and user-defined fields associated with contacts in the CRM 2011 system

In CRM 2011, the contact entities come with a variety of default fields, and I have also included some custom fields. My goal is to retrieve all field names in a JavaScript list. When creating an email template in CRM, you can select fields from a dialog. ...