/// /// 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 ParticleScaleNode = away.animators.ParticleScaleNode; 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/cloud_5.png", "imgs/lens_flare.jpg", "imgs/star.jpg"]; 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); this.onResize(); this.render(); // アセットの読み込み 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() { // カメラの設定 this.camera.lens.far = 15000; // 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); // レンズフレアの作成 var mat:TextureMaterial = new TextureMaterial( AssetLibrary.getAsset(RESOURCE_LIST[5])); mat.blendMode = BlendMode.ADD; var flare:Sprite3D = new Sprite3D(mat, 1024, 1024); this.followTarget.addChild(flare); // パーティクルを作成 this.createParticle(this.followTarget, AssetLibrary.getAsset(RESOURCE_LIST[0]), 64, 134, 255); // スモーク風パーティクル this.createParticle(this.followTarget, AssetLibrary.getAsset(RESOURCE_LIST[1]), 64, 150, 255); // スモーク風パーティクル this.createParticle(this.followTarget, AssetLibrary.getAsset(RESOURCE_LIST[2]), 80, 153, 255); // スモーク風パーティクル this.createParticle(this.followTarget, AssetLibrary.getAsset(RESOURCE_LIST[3]), 102, 150, 255); // スモーク風パーティクル this.createParticle(this.followTarget, AssetLibrary.getAsset(RESOURCE_LIST[4]), 180, 200, 255); // キラキラ風パーティクル // 宇宙の星を作成 this.createStarField(); // アニメーションさせるためにループイベントを指定します 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.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(60, 60, 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() * 2.6; properties.duration = 1.5 + 1 * Math.random(); // 継続時間 // 移動量 properties[ParticleVelocityNode.VELOCITY_VECTOR3D] = new Vector3D( (Math.random() - 0.5) * 300, (Math.random() - 0.5) * 300, (Math.random() - 0.5) * 300 - 300); } /** * Create Particles * @param followTarget 追随させたいターゲット * @param bitmap ビットマップオブジェクト */ private createParticle(followTarget:Object3D, bitmap:Texture2DBase, colorR:number, colorG:number, colorB:number):void { var particleMaterial:TextureMaterial = new TextureMaterial(bitmap); 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 ParticleScaleNode(ParticlePropertiesMode.GLOBAL, false, true, 1, 0)); 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 createStarField():void { // Define material to be shared by all stars. var starMaterial:TextureMaterial = new TextureMaterial( AssetLibrary.getAsset(RESOURCE_LIST[6])); starMaterial.alphaBlending = true; for (var i:number = 0; i < 1000; i++) { // Define unique star. var star:Sprite3D = new Sprite3D(starMaterial, 100, 100); this.scene.addChild(star); // Randomly position in spherical coordinates. var radius:number = rand(2000, 15000); var elevation:number = rand(-Math.PI, Math.PI); var azimuth:number = rand(-Math.PI, Math.PI); // Spherical to cartesian. star.x = radius * Math.cos(elevation) * Math.sin(azimuth); star.y = radius * Math.sin(elevation); star.z = radius * Math.cos(elevation) * Math.cos(azimuth); } } /** 毎フレーム時に実行されるループイベントです */ 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.4 * (event.clientX - this.lastMouseX) + this.lastPanAngle; this.cameraController.tiltAngle = 0.4 * (event.clientY - this.lastMouseY) + this.lastTiltAngle; } } private onResize() { this.width = window.innerWidth; this.height = window.innerHeight; } } function rand(min:number, max:number):number { return (max - min) * Math.random() + min; } } // ページが読み込まれてから実行します window.onload = () => new example.Main();