[AwayJS] 地球儀 (1)

AwayJSを使って、地球儀を作るよ。 :boy:

awayjs_earth1

Mesh を使ってみるよ。 :bouzu:
PrimitiveSpherePrefab と TriangleMethodMaterial を使ってみたよ。
さらに、DirectionalLight も使ってみたよ。 :cake:
そして、AssetLibrary で、外部画像を読み込んでみたよ。 :sun:

Main.ts
/// <reference path="libs/awayjs-core/build/awayjs-core.d.ts" />
/// <reference path="libs/awayjs-stagegl/build/awayjs-stagegl.d.ts" />
/// <reference path="libs/awayjs-renderergl/build/awayjs-renderergl.d.ts" />
/// <reference path="libs/awayjs-display/build/awayjs-display.d.ts" />
/// <reference path="libs/awayjs-methodmaterials/build/awayjs-methodmaterials.d.ts" />


import ContextGLProfile = require("awayjs-stagegl/lib/base/ContextGLProfile");
import DefaultRenderer = require("awayjs-renderergl/lib/DefaultRenderer");
import View = require("awayjs-display/lib/containers/View");
import Scene = require("awayjs-display/lib/containers/Scene");
import Camera = require("awayjs-display/lib/entities/Camera");
import DirectionalLight = require("awayjs-display/lib/entities/DirectionalLight");
import StaticLightPicker = require("awayjs-display/lib/materials/lightpickers/StaticLightPicker");
import Vector3D = require("awayjs-core/lib/geom/Vector3D");
import Matrix3D = require("awayjs-core/lib/geom/Matrix3D");
import RequestAnimationFrame = require("awayjs-core/lib/utils/RequestAnimationFrame");
import DisplayObjectContainer = require("awayjs-display/lib/containers/DisplayObjectContainer");
import Mesh = require("awayjs-display/lib/entities/Mesh");
import PrimitiveSpherePrefab = require("awayjs-display/lib/prefabs/PrimitiveSpherePrefab");
import TriangleMethodMaterial = require("awayjs-methodmaterials/lib/TriangleMethodMaterial");
import Texture2DBase = require("awayjs-core/lib/textures/Texture2DBase");
import AssetLibrary = require("awayjs-core/lib/library/AssetLibrary");
import LoaderEvent = require("awayjs-core/lib/events/LoaderEvent");
import URLRequest = require("awayjs-core/lib/net/URLRequest");

class Main {

  private renderer:DefaultRenderer;
  private view:View;
  private scene:Scene;
  private camera:Camera;
  private timer:RequestAnimationFrame;
  private light:DirectionalLight;
  private container:DisplayObjectContainer;
  private earth:Mesh;
  private material:TriangleMethodMaterial;
  private texture:Texture2DBase;
  private radius:number = 500;
  private inclination:number = 23.4;
  private center:Vector3D = new Vector3D();
  private filePath:string = "assets/world.jpg";

  constructor() {
    this.renderer = new DefaultRenderer(false, ContextGLProfile.BASELINE);
    this.renderer.antiAlias = 4;
    this.view = new View(this.renderer);
    this.scene = this.view.scene;
    this.camera = this.view.camera;
    this.light = new DirectionalLight();
    this.setup();
    //this.initialize();
    window.onresize = (evt:UIEvent) => this.resize(evt);
    this.resize();
    this.timer = new RequestAnimationFrame(this.render, this);
    //this.timer.start();
    AssetLibrary.addEventListener(LoaderEvent.RESOURCE_COMPLETE, (evt:LoaderEvent) => this.loaded(evt));
    AssetLibrary.load(new URLRequest(this.filePath));
  }

  private setup():void {
    this.view.backgroundColor = 0x000000;
    this.camera.x = 0;
    this.camera.y = 0;
    this.camera.z = - this.radius;
    this.camera.lookAt(this.center);
    var matrix:Matrix3D = new Matrix3D();
    matrix.appendRotation(45, Vector3D.Y_AXIS);
    matrix.appendRotation(- this.inclination, Vector3D.Z_AXIS);
    var sun:Vector3D = matrix.transformVector(new Vector3D(0, 0, 1));
    this.light.direction = sun;
    this.light.ambient = 0;
    this.light.diffuse = 1;
    this.light.specular = 0.1;
  }
  private loaded(evt:LoaderEvent):void {
    switch (evt.url) {
      case this.filePath :
        this.texture = <Texture2DBase> evt.assets[0];
        break;
    }
    if (this.texture) {
      this.initialize();
      this.timer.start();
    }
  }
  private initialize():void {
    this.container = new DisplayObjectContainer();
    this.scene.addChild(this.container);
    this.container.rotationZ = - this.inclination;

    this.material = new TriangleMethodMaterial();
    this.material.texture = this.texture;
    var lightPicker:StaticLightPicker = new StaticLightPicker([this.light]);
    this.material.lightPicker = lightPicker;
    this.earth = <Mesh> new PrimitiveSpherePrefab(160, 40, 20).getNewObject();
    this.earth.material = this.material;
    this.container.addChild(this.earth);
  }
  private render(dt:number):void {
    this.container.rotationY -= 0.5;

    this.view.render();
  }
  private resize(evt:UIEvent = null):void {
    this.view.width = window.innerWidth;
    this.view.height = window.innerHeight;
  }
}

window.onload = function() {
  new Main();
}

build.txt
src/Main.ts
-target ES5
-module commonjs
-outDir bin

build.txt 上で、[Command + B] して、ビルド。

index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0">
<title>[AwayJS] 地球儀 (1)</title>
<script type="text/javascript" src="js/awayjs-core.min.js">/script>
<script type="text/javascript" src="js/awayjs-stagegl.min.js">/script>
<script type="text/javascript" src="js/awayjs-renderergl.min.js">/script>
<script type="text/javascript" src="js/awayjs-display.min.js">/script>
<script type="text/javascript" src="js/awayjs-methodmaterials.min.js">/script>
<script type="text/javascript" src="Main.js">/script>
<style type="text/css">
  html { overflow:hidden; }
  body { margin:0; padding:0; }
</style>
</head>
<body style="background-color:#000000;">
</body>
</html>


:banana: [TypeScript] AwayJSを始めよう! 参照。


地球表面の画像(1024*512)をJPGで用意。
:check: 「Planet Earth Texture Maps」の画像を使わせていただきました。


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