3D Earth
How do I make an 3d earth?
Using THREEjs to create a model of the Earth. Using color, bump, and spec maps to get a more realistic look. I used the base of this program for my International Space Station Tracker. Use your mouse or finger to rotate the scene.. What is a 3d earth? How do you make a 3d earth? This script and codes were developed by Bryan Jones on 13 July 2022, Wednesday.
3D Earth - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>3D Earth</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div id="container"></div> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>
<script src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/123879/three.min.js'></script>
<script src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/123879/OrbitControls.js'></script>
<script src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/123879/DeviceOrientationControls.js'></script>
<script src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/123879/StereoEffect.js'></script> <script src="js/index.js"></script>
</body>
</html>
3D Earth - Script Codes CSS Codes
/** * @file * Basic styling for the program. */
body { background: #000; margin: 0; padding: 0; overflow: hidden;
}
3D Earth - Script Codes JS Codes
/** * @file * The main scene. */
/** * Define constants. */
const TEXTURE_PATH = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/123879/';
/** * Create the animation request. */
if (!window.requestAnimationFrame) { window.requestAnimationFrame = (function() { return window.mozRequestAnimationFrame || window.msRequestAnimationFrame || window.oRequestAnimationFrame || window.webkitRequestAnimationFrame || function (callback, element) { // 60 FPS window.setTimeout(callback, 1000 / 60); }; })();
}
/** * Set our global variables. */
var camera, scene, renderer, effect, controls, element, container, sphere, sphereCloud, rotationPoint;
var degreeOffset = 90;
var earthRadius = 80;
var getEarthRotation = function() { // Get the current time. var d = new Date(); var h = d.getUTCHours(); var m = d.getUTCMinutes(); // Calculate total minutes. var minutes = h * 60; minutes += m; // Turn minutes into degrees. degrees = minutes/3.9907; // Add an offset to match UTC time. degrees += degreeOffset; return degrees;
}
var degrees = getEarthRotation();
// Calculate Earth's rotation position.
setInterval(function() { // Get the current time. var d = new Date(); var h = d.getUTCHours(); var m = d.getUTCMinutes(); // Calculate total minutes. var minutes = h * 60; minutes += m; // Turn minutes into degrees. degrees = minutes/3.9907; // Add an offset to match UTC time. degrees += degreeOffset;
}, 60000);
init();
animate();
/** * Initializer function. */
function init() { // Build the container container = document.createElement( 'div' ); document.body.appendChild( container ); // Create the scene. scene = new THREE.Scene(); // Create a rotation point. baseRotationPoint = new THREE.Object3D(); baseRotationPoint.position.set( 0, 0, 0 ); scene.add( baseRotationPoint ); // Create world rotation point. worldRotationPoint = new THREE.Object3D(); worldRotationPoint.position.set( 0, 0, 0 ); scene.add( worldRotationPoint ); rotationPoint = new THREE.Object3D(); rotationPoint.position.set( 0, 0, earthRadius * 4 ); baseRotationPoint.add( rotationPoint ); // Create the camera. camera = new THREE.PerspectiveCamera( 45, // Angle window.innerWidth / window.innerHeight, // Aspect Ratio. 1, // Near view. 10000 // Far view. ); rotationPoint.add( camera ); // Build the renderer. renderer = new THREE.WebGLRenderer(); element = renderer.domElement; renderer.setSize( window.innerWidth, window.innerHeight ); renderer.shadowMap.enabled; container.appendChild( element ); // Build the controls. controls = new THREE.OrbitControls( camera, element ); controls.enablePan = true; controls.enableZoom = true; controls.maxDistance = earthRadius * 8; controls.minDistance = earthRadius * 2; controls.target.copy( new THREE.Vector3( 0, 0, -1 * earthRadius * 4 )); function setOrientationControls(e) { if (!e.alpha) { return; } controls = new THREE.DeviceOrientationControls( camera ); controls.connect(); window.removeEventListener('deviceorientation', setOrientationControls, true); } window.addEventListener('deviceorientation', setOrientationControls, true); // Ambient lights var ambient = new THREE.AmbientLight( 0x222222 ); scene.add( ambient ); // The sun. var light = new THREE.PointLight( 0xffeecc, 1, 5000 ); light.position.set( -400, 0, 100 ); scene.add( light ); // Since the sun is much bigger than a point of light, add four fillers. var light2 = new THREE.PointLight( 0xffffff, 0.6, 4000 ); light2.position.set( -400, 0, 250 ); scene.add( light2 ); var light3 = new THREE.PointLight( 0xffffff, 0.6, 4000 ); light3.position.set( -400, 0, -150 ); scene.add( light3 ); var light4 = new THREE.PointLight( 0xffffff, 0.6, 4000 ); light4.position.set( -400, 150, 100 ); scene.add( light4 ); var light5 = new THREE.PointLight( 0xffffff, 0.6, 4000 ); light5.position.set( -400, -150, 100 ); scene.add( light5 ); // Add the Earth sphere model. var geometry = new THREE.SphereGeometry( earthRadius, 128, 128 ); // Create the Earth materials. loader = new THREE.TextureLoader(); loader.setCrossOrigin( 'https://s.codepen.io' ); var texture = loader.load( TEXTURE_PATH + 'ColorMap.jpg' ); var bump = null; bump = loader.load( TEXTURE_PATH + 'Bump.jpg' ); var spec = null; spec = loader.load( TEXTURE_PATH + 'SpecMask.jpg' ); var material = new THREE.MeshPhongMaterial({ color: "#ffffff", shininess: 5, map: texture, specularMap: spec, specular: "#666666", bumpMap: bump, }); sphere = new THREE.Mesh( geometry, material ); sphere.position.set( 0, 0, 0 ); sphere.rotation.y = Math.PI; // Focus initially on the prime meridian. sphere.rotation.y = -1 * (8.7 * Math.PI / 17); // Add the Earth to the scene.https://s3-us-west-2.amazonaws.com/s.cdpn.io/123879/Bump.jpg worldRotationPoint.add( sphere ); // Add the Earth sphere model. var geometryCloud = new THREE.SphereGeometry( earthRadius + 0.2, 128, 128 ); loader = new THREE.TextureLoader(); loader.setCrossOrigin( 'https://s.codepen.io' ); var alpha = loader.load( TEXTURE_PATH + "alphaMap.jpg" ); var materialCloud = new THREE.MeshPhongMaterial({ alphaMap: alpha, }); materialCloud.transparent = true; sphereCloud = new THREE.Mesh( geometryCloud, materialCloud ); scene.add( sphereCloud ); // Create a glow effect. loader = new THREE.TextureLoader(); loader.setCrossOrigin( 'https://s.codepen.io' ); var glowMap = loader.load( TEXTURE_PATH + "glow.png" ); // Create the sprite to add the glow effect. var spriteMaterial = new THREE.SpriteMaterial({ map: glowMap, color: 0x0099ff, transparent: false, blending: THREE.AdditiveBlending }); var sprite = new THREE.Sprite( spriteMaterial ); sprite.scale.set( earthRadius * 2.5, earthRadius * 2.5, 1.0); sphereCloud.add(sprite); // Add the skymap. addSkybox(); window.addEventListener('resize', onWindowResize, false);
}
/** * Events to fire upon window resizing. */
function onWindowResize() { camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix(); renderer.setSize(window.innerWidth, window.innerHeight);
}
/** * Add the sun to the scene. */
function createSun() { // Add the Sun sphere model. var sunGeometry = new THREE.SphereGeometry( 100, 16, 16 ); // Create the Sun materials. var sunMaterial = new THREE.MeshLambertMaterial({ color: '#ffff55', emissive: '#ffff55', }); sun = new THREE.Mesh( sunGeometry, sunMaterial ); sun.castShadow = false; sun.receiveShadow = false; sun.position.set( -9500, 0, 0 ); sun.rotation.y = Math.PI; // Add the Sun to the scene. scene.add( sun );
}
createSun();
/** * Updates to apply to the scene while running. */
function update() { camera.updateProjectionMatrix(); worldRotationPoint.rotation.y = degrees * Math.PI/180; sphereCloud.rotation.y += 0.00025; baseRotationPoint.rotation.y -= 0.00025;
}
/** * Render the scene. */
function render() { renderer.render(scene, camera);
}
/** * Animate the scene. */
function animate() { requestAnimationFrame(animate); update(); render();
}
function addSkybox() { var urlPrefix = TEXTURE_PATH; var urls = [ urlPrefix + 'test.jpg', urlPrefix + 'test.jpg', urlPrefix + 'test.jpg', urlPrefix + 'test.jpg', urlPrefix + 'test.jpg', urlPrefix + 'test.jpg', ]; var loader = new THREE.CubeTextureLoader(); loader.setCrossOrigin( 'https://s.codepen.io' ); var textureCube = loader.load( urls ); textureCube.format = THREE.RGBFormat; var shader = THREE.ShaderLib[ "cube" ]; shader.uniforms[ "tCube" ].value = textureCube; var material = new THREE.ShaderMaterial( { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: shader.uniforms, depthWrite: false, side: THREE.BackSide } ); var geometry = new THREE.BoxGeometry( 2000, 2000, 2000 ); var skybox = new THREE.Mesh( geometry, material ); //skybox.position.x = -30; scene.add( skybox );
}
Developer | Bryan Jones |
Username | bartuc |
Uploaded | July 13, 2022 |
Rating | 4.5 |
Size | 4,480 Kb |
Views | 68,816 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Atom | 3,816 Kb |
Card Flip | 5,712 Kb |
Reflections | 5,028 Kb |
Lens Flare | 4,199 Kb |
Hover Effects | 3,972 Kb |
See the Truth | 3,488 Kb |
Light Switch | 4,933 Kb |
Planet Awesome | 3,554 Kb |
Planet Rotation | 2,953 Kb |
Vermhole | 6,053 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
CSS3 Button Examples | Volusion | 3,377 Kb |
404 Page | Saransh | 2,666 Kb |
Arrow Navigation | Hinducows | 1,973 Kb |
Sticky notes with CSS3 | HaiNguyen007 | 2,146 Kb |
A Pen by Stan Williams | Stanssongs | 6,706 Kb |
Mini Profile | Frytyler | 3,828 Kb |
Pattern lab logo | TimPietrusky | 3,666 Kb |
Super Discount | Orrinward | 3,225 Kb |
Obligatory CSS3 UI Nav | Romandiaz | 9,017 Kb |
AngularJS Datalist Directive | M-e-conroy | 2,366 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!