[AwayJS] 立方体 (2)

AwayJSを使って、立方体を作るよ。 :boy:

awayjs_cube2

Mesh を使ってみるよ。 :bouzu:
PrimitiveCubePrefab と TriangleMethodMaterial を使ってみたよ。
さらに、PointLight も使ってみたよ。 :cake:

立方体200個だと重いので、100個にしてあるの。 :rain:

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 PointLight = require("awayjs-display/lib/entities/PointLight");
import StaticLightPicker = require("awayjs-display/lib/materials/lightpickers/StaticLightPicker");
import Vector3D = require("awayjs-core/lib/geom/Vector3D");
import RequestAnimationFrame = require("awayjs-core/lib/utils/RequestAnimationFrame");
import Mesh = require("awayjs-display/lib/entities/Mesh");
import PrimitiveCubePrefab = require("awayjs-display/lib/prefabs/PrimitiveCubePrefab");
import TriangleMethodMaterial = require("awayjs-methodmaterials/lib/TriangleMethodMaterial");

class Main {

  private renderer:DefaultRenderer;
  private view:View;
  private scene:Scene;
  private camera:Camera;
  private timer:RequestAnimationFrame;
  private light:PointLight;
  private max:number = 100;
  private radius:number = 500;
  private angle:number = - 90;
  private degree:number = 0;
  private depression:number = 30;
  private radian:number = Math.PI/180;
  private center:Vector3D = new Vector3D();

  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 PointLight();
    this.setup();
    this.initialize();
    window.onresize = (evt:UIEvent) => this.resize(evt);
    this.resize();
    this.timer = new RequestAnimationFrame(this.render, this);
    this.timer.start();
  }

  private setup():void {
    this.view.backgroundColor = 0x000000;
    this.camera.x = 0;
    this.camera.y = 0;
    this.camera.z = - this.radius;
    this.light.x = 0;
    this.light.y = 0;
    this.light.z = 0;
    this.light.ambient = 0.3;
    this.light.diffuse = 0.8;
    this.light.specular = 0.2;
  }
  private initialize():void {
    var material:TriangleMethodMaterial = new TriangleMethodMaterial();
    var lightPicker:StaticLightPicker = new StaticLightPicker([this.light]);
    material.lightPicker = lightPicker;
    var cube:Mesh;
    for (var n:number = 0; n < this.max; n++) {
      cube = <Mesh> new PrimitiveCubePrefab(20, 20, 20, 1, 1, 1, false).getNewObject();
      cube.material = material;
      cube.x = (Math.random() - 0.5)*500;
      cube.y = (Math.random() - 0.5)*500;
      cube.z = (Math.random() - 0.5)*500;
      cube.rotationX = Math.random()*360;
      cube.rotationY = Math.random()*360;
      cube.rotationZ = Math.random()*360;
      this.scene.addChild(cube);
    }
  }
  private render(dt:number):void {
    this.angle += 0.5;
    this.degree += 1;
    var dip:number = this.depression*Math.sin(this.degree*this.radian);
    this.camera.x = this.radius*Math.cos(this.angle*this.radian)*Math.cos(dip*this.radian);
    this.camera.y = this.radius*Math.sin(dip*this.radian);
    this.camera.z = this.radius*Math.sin(this.angle*this.radian)*Math.cos(dip*this.radian);
    this.camera.lookAt(this.center);
    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] 立方体 (2)</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を始めよう! 参照。


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