[three.js] 地球儀 (3)

three.jsを使って、地球儀を作るよ。 :boy:

threejs_earth3

WebGLRendererで描画するよ。
SphereGeometry, MeshPhongMaterial, Mesh を使ってみたよ。 :bouzu:

さらに、DirectionalLight も使ったよ。 :cake:
んでもって、ImageUtils, Texture も使ったりして。 :beer:

index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0">
<title>地球儀 (3) | three.js</title>
<script src="js/three.min.js"></script>
<script src="js/detector.js"></script>
<script src="js/stats.min.js"></script>
<style type="text/css">
  html { overflow:hidden; }
  body { margin:0; padding:0; }
</style>
</head>
<body style="background-color:#000000">
<script>
var scene, camera, renderer, stats;
var texture, ctexture, ntexture;
var container, content, earth, clouds;
var light;
var radius = 500;
var inclination = 23.4;
var sun = {x: 0.649, y: 0.281, z: -0.707};
var radian = Math.PI/180;
var center = new THREE.Object3D();
var filePath = "assets/world.jpg";
var cloudsPath = "assets/world_clouds.png";
var normalsPath = "assets/world_normals.jpg";

window.onload = init;

function init() {
  if (!Detector.webgl) Detector.addGetWebGLMessage();

  scene = new THREE.Scene();
  camera = new THREE.PerspectiveCamera(60, window.innerWidth/window.innerHeight, 0.1, 1000);
  scene.add(camera);
  light = new THREE.DirectionalLight(0xFFFFFF);
  scene.add(light);
  renderer = new THREE.WebGLRenderer({antialias: true});
  renderer.setSize(window.innerWidth, window.innerHeight);
  document.body.appendChild(renderer.domElement);
  setup();
  //initialize();
  THREE.ImageUtils.loadTexture(filePath, undefined, loaded);
  THREE.ImageUtils.loadTexture(cloudsPath, undefined, loaded);
  THREE.ImageUtils.loadTexture(normalsPath, undefined, loaded);

  stats = new Stats();
  stats.setMode(0);
  stats.domElement.style.position = "fixed";
  stats.domElement.style.right = "0px";
  stats.domElement.style.top = "0px";
  document.body.appendChild(stats.domElement);

  //render();
  window.addEventListener("resize", resize, false);
}
function setup() {
  renderer.setClearColor(0x000000, 1);
  camera.position.z = -radius;
  light.position.set(sun.x, sun.y, sun.z).normalize();

  container = new THREE.Group();
  scene.add(container);
  container.rotation.z = inclination*radian;
  content = new THREE.Group();
  container.add(content);
}
function loaded(data) {
  switch (data.sourceFile) {
    case filePath :
      texture = data;
      break;
    case cloudsPath :
      ctexture = data;
      break;
    case normalsPath :
      ntexture = data;
      break;
  }
  if (texture && ctexture && ntexture) {
    initialize();
    render();
  }
}
function initialize() {
  var geometry = new THREE.SphereGeometry(160, 40, 20);
  var material = new THREE.MeshPhongMaterial({map: texture, color: 0xFFFFFF, normalMap: ntexture, normalScale: new THREE.Vector2(1, 1)});
  material.emissive.set(0x333333);
  material.shininess = 0;
  earth = new THREE.Mesh(geometry, material);
  content.add(earth);

  var cgeometry = new THREE.SphereGeometry(164, 40, 20);
  var cmaterial = new THREE.MeshLambertMaterial({map: ctexture, color: 0xFFFFFF});
  cmaterial.emissive.set(0x333333);
  cmaterial.transparent = true;
  clouds = new THREE.Mesh(cgeometry, cmaterial);
  content.add(clouds);
}
function render() {
  requestAnimationFrame(render);

  content.rotation.y += 0.5*radian;
  clouds.rotation.y += 0.2*radian;

  camera.lookAt(center.position);

  renderer.render(scene, camera);

  stats.update();
}
function resize(event) {
  camera.aspect = window.innerWidth/window.innerHeight;
  camera.updateProjectionMatrix();
  renderer.setSize(window.innerWidth, window.innerHeight);
}
</script>
</body>
</html>


地球表面の画像(1024*512)をJPGで、雲の画像(1024*512)を透過PNGで用意。
さらに、凹凸用の画像(1024*512)をJPGで用意。
:check: 「Planet Earth Texture Maps」の画像を使わせていただきました。

凹凸用の画像は、こんな感じ。

awayjs_earth_normals


[修正] (14/12/27 Sat 21:23)
HTML記述を変更。