Home >Web Front-end >JS Tutorial >Building the Earth with WebGL and JavaScript

Building the Earth with WebGL and JavaScript

Joseph Gordon-Levitt
Joseph Gordon-LevittOriginal
2025-02-20 08:35:10778browse

Building the Earth with WebGL and JavaScript

WebGL offers exciting possibilities for creating immersive 3D experiences directly within web browsers. This tutorial demonstrates building a rotatable 3D Earth model using WebGL and the three.js library.

Key Concepts:

  • Leveraging three.js, a WebGL plugin, simplifies 3D model creation.
  • The process involves defining a scene, camera, lighting, renderer, and the Earth model itself within a JavaScript file.
  • Realistic Earth visuals are achieved using diffuse and bump maps for surface texture and topography.
  • Animation is implemented through self-calling functions, subtly rotating the Earth and continuously rendering the scene.

Prerequisites:

This tutorial uses the three.js library. You can include it via a CDN in your HTML:

<code class="language-html"></code>

A div element will serve as the rendering target:

<code class="language-html"><div id="container"></div>
</code>

Setting Up the 3D Environment:

The JavaScript file (earth.js) initializes essential components:

<code class="language-javascript">var scene, camera, light, renderer, earthObject;

var WIDTH = window.innerWidth - 30,
    HEIGHT = window.innerHeight - 30;

var angle = 45,
    aspect = WIDTH / HEIGHT,
    near = 0.1,
    far = 3000;

var container = document.getElementById('container');

camera = new THREE.PerspectiveCamera(angle, aspect, near, far);
camera.position.set(0, 0, 0);
scene = new THREE.Scene();

light = new THREE.SpotLight(0xFFFFFF, 1, 0, Math.PI / 2, 1);
light.position.set(4000, 4000, 1500);
light.target.position.set(1000, 3800, 1000);

// ... (Renderer setup and Earth mesh creation will follow)</code>

This code sets up the camera, scene, and lighting. The renderer will be added later.

Creating the Earth Mesh:

A sphere geometry and Phong material are used to create the Earth model:

<code class="language-javascript">var earthGeo = new THREE.SphereGeometry(30, 40, 400),
    earthMat = new THREE.MeshPhongMaterial();

var earthMesh = new THREE.Mesh(earthGeo, earthMat);
earthMesh.position.set(-100, 0, 0);
earthMesh.rotation.y = 5;

scene.add(earthMesh);</code>

Adding Textures:

Diffuse and bump maps enhance realism:

<code class="language-javascript">// Diffuse map
earthMat.map = THREE.ImageUtils.loadTexture('images/earthmap1k.jpg');

// Bump map
earthMat.bumpMap = THREE.ImageUtils.loadTexture('images/elev_bump_16ka.jpg');
earthMat.bumpScale = 8;</code>

Remember to replace placeholders with actual image paths.

Animating the Earth:

The animate() and render() functions create the rotation effect:

<code class="language-javascript">function animate() {
    requestAnimationFrame(animate);
    render();
}

function render() {
    var clock = new THREE.Clock(),
        delta = clock.getDelta();

    earthMesh.rotation.y += rotationSpeed * delta; // rotationSpeed needs to be defined
    renderer.render(scene, camera);
}

animate(); // Start the animation</code>

Complete Example (Illustrative): A full, runnable example would require more code, including renderer setup and potentially additional libraries for controls. The snippets above provide the core elements. Refer to the original article for a complete, functional example.

Further Enhancements:

The tutorial suggests adding features like:

  • Orbit controls for interactive rotation.
  • A starfield background.
  • Atmospheric effects.

This revised response provides a more structured and detailed explanation of the WebGL Earth model creation process, while maintaining the core information and avoiding unnecessary repetition.

The above is the detailed content of Building the Earth with WebGL and JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn