///
///
declare var Stats;
module example {
// Away3Dライブラリを読み込み
import View3D = away.containers.View3D;
import TextureMaterial = away.materials.TextureMaterial;
import ColorMaterial = away.materials.ColorMaterial;
import SphereGeometry = away.primitives.SphereGeometry;
import PlaneGeometry = away.primitives.PlaneGeometry;
import Geometry = away.base.Geometry;
import Mesh = away.entities.Mesh;
import DirectionalLight = away.lights.DirectionalLight;
import RequestAnimationFrame = away.utils.RequestAnimationFrame;
import StaticLightPicker = away.materials.StaticLightPicker;
import HTMLImageElementTexture = away.textures.HTMLImageElementTexture;
import HoverController = away.controllers.HoverController;
import Sprite3D = away.entities.Sprite3D;
import ParticleAnimationSet = away.animators.ParticleAnimationSet;
import ParticleAnimator = away.animators.ParticleAnimator;
import ParticleBezierCurveNode = away.animators.ParticleBezierCurveNode;
import ParticleBillboardNode = away.animators.ParticleBillboardNode;
import ParticleInitialColorNode = away.animators.ParticleInitialColorNode;
import ParticlePositionNode = away.animators.ParticlePositionNode;
import ParticleProperties = away.animators.ParticleProperties;
import ParticlePropertiesMode = away.animators.ParticlePropertiesMode;
import ParticleVelocityNode = away.animators.ParticleVelocityNode;
import ParticleColorNode = away.animators.ParticleColorNode;
import ParticleFollowNode = away.animators.ParticleFollowNode;
import ParticleGeometry = away.base.ParticleGeometry;
import ParticleGeometryHelper = away.tools.ParticleGeometryHelper;
import ParticleGeometryTransform = away.tools.ParticleGeometryTransform;
import Vector3D = away.geom.Vector3D;
import ColorTransform = away.geom.ColorTransform;
import Object3D = away.base.Object3D;
import ObjectContainer3D = away.containers.ObjectContainer3D;
import BlendMode = away.display.BlendMode;
import Cast = away.utils.Cast;
import WireframePlane = away.primitives.WireframePlane;
import AssetLibrary = away.library.AssetLibrary;
import Texture2DBase = away.textures.Texture2DBase;
var RESOURCE_LIST:string[] = [
"imgs/cloud_1.png",
"imgs/cloud_2.png",
"imgs/cloud_3.png",
"imgs/cloud_4.png",
"imgs/star.png"];
export class Main extends View3D {
lastPanAngle:number;
lastTiltAngle:number;
lastMouseX:number;
lastMouseY:number;
isMouseDown:boolean;
cameraController:HoverController;
mesh:Mesh;
particleGeometry:ParticleGeometry;
particleFollowNode:ParticleFollowNode;
followTarget:ObjectContainer3D;
image:HTMLImageElement;
_loadedCount = 0;
stats:any;
constructor() {
super();
var touchManager = new utils.TouchManager();
touchManager.enableTouch();
touchManager.addListener(document.body);
AssetLibrary.addEventListener(away.events.LoaderEvent.RESOURCE_COMPLETE, this.onResourceCompelte, this);
for (var i = 0; i < RESOURCE_LIST.length; i++) {
away.library.AssetLibrary.load(new away.net.URLRequest(RESOURCE_LIST[i]));
}
}
onResourceCompelte(event:away.events.LoaderEvent) {
this._loadedCount++;
if (this._loadedCount < RESOURCE_LIST.length)
return;
this.onLoadImage();
}
onLoadImage() {
// Floor
var floor:WireframePlane = new WireframePlane(2000, 2000, 40, 40, 0x999999, 1, WireframePlane.ORIENTATION_XZ);
floor.y = -450;
this.scene.addChild(floor);
// Particles
this.initParticles();
this.followTarget = new ObjectContainer3D();
this.scene.addChild(this.followTarget);
this.createParticle(this.followTarget, AssetLibrary.getAsset(RESOURCE_LIST[0]), 229, 102, 255); // スモーク風パーティクル
this.createParticle(this.followTarget, AssetLibrary.getAsset(RESOURCE_LIST[1]), 102, 204, 255); // スモーク風パーティクル
this.createParticle(this.followTarget, AssetLibrary.getAsset(RESOURCE_LIST[2]), 102, 153, 255); // スモーク風パーティクル
this.createParticle(this.followTarget, AssetLibrary.getAsset(RESOURCE_LIST[3]), 178, 102, 255); // スモーク風パーティクル
this.createParticle(this.followTarget, AssetLibrary.getAsset(RESOURCE_LIST[3]), 255, 102, 255); // スモーク風パーティクル
// アニメーションさせるためにループイベントを指定します
var raf = new RequestAnimationFrame(this.onEnterFrame, this);
raf.start();
// カメラコントローラーを用意します
this.cameraController = new HoverController(this.camera, null, -80, 0, 1000, 0, 90);
document.onmousedown = (event) => this.onMouseDown(event);
document.onmouseup = (event) => this.onMouseUp(event);
document.onmousemove = (event) => this.onMouseMove(event);
window.onresize = (event) => this.onResize();
this.onResize();
// 計測用
this.stats = new Stats();
document.body.appendChild(this.stats.domElement);
}
/**
* Initialise the particles
*/
private initParticles():void {
//setup the base geometry for one particle
var plane = new PlaneGeometry(64, 64, 1, 1, false);
//create the particle geometry
var geometrySet:Geometry[] = [];
var setTransforms:ParticleGeometryTransform[] = new Array();
var particleTransform:ParticleGeometryTransform;
for (var i:number = 0; i < 500; i++) {
geometrySet.push(plane);
particleTransform = new ParticleGeometryTransform();
setTransforms.push(particleTransform);
}
this.particleGeometry = ParticleGeometryHelper.generateGeometry(geometrySet, setTransforms);
}
/**
* Initialiser function for particle properties
*/
private initParticleProperties(properties:ParticleProperties):void {
properties.startTime = Math.random() * 4.1;
properties.duration = 2 + 2 * Math.random(); // 継続時間
// 移動量
properties[ParticleVelocityNode.VELOCITY_VECTOR3D] = new Vector3D(
(Math.random() - 0.5) * 200,
(Math.random() - 0.5) * 200,
(Math.random() - 0.5) * 200 - 200);
}
/**
* Create Particles
* @param followTarget 追随させたいターゲット
* @param bitmap ビットマップオブジェクト
*/
private createParticle(followTarget:Object3D, bitmap:Texture2DBase, colorR:number, colorG:number, colorB:number):void {
// HTMLのオブジェクトからテクスチャを作成
// var ts = new HTMLImageElementTexture(bitmap, false);
var particleMaterial:TextureMaterial = new TextureMaterial(bitmap);
particleMaterial.alphaBlending = true;
particleMaterial.blendMode = BlendMode.ADD;
//create the particle animation set
var particleAnimationSet:ParticleAnimationSet = new ParticleAnimationSet(true, true, true);
colorR /= 255;
colorG /= 255;
colorB /= 255;
//define the particle animations and init function
particleAnimationSet.addAnimation(new ParticleBillboardNode());
particleAnimationSet.addAnimation(new ParticleVelocityNode(ParticlePropertiesMode.LOCAL_STATIC));
particleAnimationSet.addAnimation(new ParticleColorNode(ParticlePropertiesMode.GLOBAL, true, true, false, false,
new ColorTransform(colorR, colorG, colorB, 1),
new ColorTransform(colorR, colorG, colorB, 0.0)));
particleAnimationSet.addAnimation(this.particleFollowNode = new ParticleFollowNode(true, false));
particleAnimationSet.initParticleFunc = this.initParticleProperties;
//create the particle meshes
var particleMesh:Mesh = new Mesh(this.particleGeometry, particleMaterial);
this.scene.addChild(particleMesh);
//create and start the particle animators
var animator:ParticleAnimator = new ParticleAnimator(particleAnimationSet);
particleMesh.animator = animator;
animator.start();
this.particleFollowNode.getAnimationState(animator).followTarget = followTarget;
}
/** 毎フレーム時に実行されるループイベントです */
private onEnterFrame(time:number) {
this.stats.begin();
// Update Particles
var time:number = new Date().getTime();
var rot:number = time / 3;
var distance:number = (time * 0.25) % 2000 - 1000;
// run circle
this.followTarget.x = 300 * Math.cos(-rot * Math.PI / 180);
this.followTarget.y = 300 * Math.sin(-rot * Math.PI / 180);
this.followTarget.z = distance;
this.render(); // レンダリング
this.stats.end();
}
/** マウスを押したとき */
private onMouseDown(event) {
this.lastPanAngle = this.cameraController.panAngle;
this.lastTiltAngle = this.cameraController.tiltAngle;
this.lastMouseX = event.clientX;
this.lastMouseY = event.clientY;
this.isMouseDown = true;
}
/** マウスを離したとき */
private onMouseUp(event) {
this.isMouseDown = false;
}
/** マウスを動かした時 */
private onMouseMove(event) {
if (this.isMouseDown) {
this.cameraController.panAngle = 0.3 * (event.clientX - this.lastMouseX) + this.lastPanAngle;
this.cameraController.tiltAngle = 0.3 * (event.clientY - this.lastMouseY) + this.lastTiltAngle;
}
}
private onResize() {
// 実験的に解像度対応をしてみる
var ratio = window.devicePixelRatio;
this.width = window.innerWidth * ratio;
this.height = window.innerHeight * ratio;
this.render();
this.canvas.style.width = window.innerWidth + "px";
this.canvas.style.height = window.innerHeight + "px";
}
}
}
// ページが読み込まれてから実行します
window.onload = () => new example.Main();