[AwayJS] パーティクル (3)

AwayJSを使って、パーティクルを扱うよ。 :boy:

awayjs_particle3

ParticleAnimationSet, ParticleAnimator を使ってみるよ。 :bouzu:
Mesh, ParticleGeometry, PrimitivePlanePrefab も使うよ。 :boy:
そして、AssetLibrary で、外部画像を読み込んでみたよ。 :sun:

パーティクルの動きとして、ParticleBillboardNode, ParticlePositionNode, ParticleVelocityNode, ParticleColorNode も使ってみた! :cake:
BlendMode, ColorTransform も使ってるよ! :bear:

ParticleAccelerationNode も使おうとしたけど、変なエラー出るん。 :rain:
TypeError: undefined is not an object (evaluating ‘this.indexDictionary[e.id][t]’)

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 Vector3D = require("awayjs-core/lib/geom/Vector3D");
import RequestAnimationFrame = require("awayjs-core/lib/utils/RequestAnimationFrame");
import ParticleAnimationSet = require("awayjs-renderergl/lib/animators/ParticleAnimationSet");
import ParticleAnimator = require("awayjs-renderergl/lib/animators/ParticleAnimator");
import ParticlePropertiesMode = require("awayjs-renderergl/lib/animators/data/ParticlePropertiesMode");
import ParticleProperties = require("awayjs-renderergl/lib/animators/data/ParticleProperties");
import ParticleBillboardNode = require("awayjs-renderergl/lib/animators/nodes/ParticleBillboardNode");
import ParticlePositionNode = require("awayjs-renderergl/lib/animators/nodes/ParticlePositionNode");
import ParticleVelocityNode = require("awayjs-renderergl/lib/animators/nodes/ParticleVelocityNode");
import ParticleAccelerationNode = require("awayjs-renderergl/lib/animators/nodes/ParticleAccelerationNode");
import ParticleColorNode = require("awayjs-renderergl/lib/animators/nodes/ParticleColorNode");
import Mesh = require("awayjs-display/lib/entities/Mesh");
import Geometry = require("awayjs-display/lib/base/Geometry");
import ParticleGeometry = require("awayjs-renderergl/lib/base/ParticleGeometry");
import ParticleGeometryHelper = require("awayjs-renderergl/lib/utils/ParticleGeometryHelper");
import PrimitivePlanePrefab = require("awayjs-display/lib/prefabs/PrimitivePlanePrefab");
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");
import ColorTransform = require("awayjs-core/lib/geom/ColorTransform");
import BlendMode = require("awayjs-display/lib/base/BlendMode");

class Main {

  private renderer:DefaultRenderer;
  private view:View;
  private scene:Scene;
  private camera:Camera;
  private timer:RequestAnimationFrame;
  private animation:ParticleAnimationSet;
  private animator:ParticleAnimator;
  private particle:Mesh;
  private material:TriangleMethodMaterial;
  private texture:Texture2DBase;
  private radius:number = 1000;
  private angle:number = - 90;
  private degree:number = 0;
  private depression:number = 30;
  private radian:number = Math.PI/180;
  private center:Vector3D = new Vector3D();
  private filePath:string = "assets/particle.png";

  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.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;
  }
  private loaded(evt:LoaderEvent):void {
    switch (evt.url) {
      case this.filePath :
        this.texture = <Texture2DBase> evt.assets[0];
        break;
    }
    if (this.texture) {
      this.initialize();
    }
  }
  private initialize():void {
    this.material = new TriangleMethodMaterial(this.texture);
    this.material.blendMode = BlendMode.ADD;

    var plane:PrimitivePlanePrefab = new PrimitivePlanePrefab(20, 20, 1, 1, false, false);
    var planes:Array<Geometry> = new Array<Geometry>();
    for (var n:number = 0; n < 10000; n++) {
      planes.push(plane.geometry);
    }
    var geometry:ParticleGeometry = ParticleGeometryHelper.generateGeometry(planes);
    this.particle = new Mesh(geometry, this.material);
    this.scene.addChild(this.particle);

    var startColorTrans:ColorTransform = new ColorTransform(0.36, 0.54, 1, 1);
    var endColorTrans:ColorTransform = new ColorTransform(0, 0, 1, 0);

    this.animation = new ParticleAnimationSet(true, true);
    this.animation.addAnimation(new ParticleBillboardNode());
    this.animation.addAnimation(new ParticlePositionNode(ParticlePropertiesMode.LOCAL_STATIC));
    this.animation.addAnimation(new ParticleVelocityNode(ParticlePropertiesMode.LOCAL_STATIC));
    //this.animation.addAnimation(new ParticleAccelerationNode(ParticlePropertiesMode.GLOBAL, new Vector3D(0, - 100, - 40, 0)));
    this.animation.addAnimation(new ParticleColorNode(ParticlePropertiesMode.GLOBAL, true, false, false, false, startColorTrans, endColorTrans));
    this.animation.initParticleFunc = this.initParticle;

    this.animator = new ParticleAnimator(this.animation);
    this.particle.animator = this.animator;
    this.animator.start();
  }
  private initParticle(prop:ParticleProperties):void {
    prop.startTime = - 2.4 + Math.random()*2.4;
    prop.duration = 2.4;
    prop[ParticlePositionNode.POSITION_VECTOR3D] = new Vector3D((Math.random() - 0.5)*800, 400, 0);
    prop[ParticleVelocityNode.VELOCITY_VECTOR3D] = new Vector3D((Math.random() - 0.5)*40, - 300, 100 + (Math.random() - 0.5)*40);
  }
  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] パーティクル (3)</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:51)
HTML記述を変更。