[Stage3D] StarlingでFFParticleSystemを試すのだ! (5) ~光~

Flash Player 11 から採用された Stage3D を学習するよ。 :bouzu:
Starling Framework を試しちゃうんだからね。 :girl:

starling_FFParticleSystem5

FFParticleSystemクラスで、高速パーティクルシステムを試してみちゃうよ。 :boy:

:caution: 要 Flash Player 11.8 以上

This movie requires Flash Player 11.8.0

Main.as
package {

  import flash.display.Sprite;
  import flash.display.StageScaleMode;
  import flash.display.StageAlign;
  import flash.display3D.Context3DRenderMode;
  import flash.display3D.Context3DProfile;

  import starling.core.Starling;

  [SWF(backgroundColor="#FFFFFF", width="600", height="300", frameRate="60")]

  public class Main extends Sprite {
    // プロパティ
    private var starling:Starling;

    // コンストラクタ
    public function Main() {
      stage.scaleMode = StageScaleMode.NO_SCALE;
      stage.align = StageAlign.TOP_LEFT;
      init();
    }

    // メソッド
    private function init():void {
      starling = new Starling(MainView, stage, null, null, Context3DRenderMode.AUTO, Context3DProfile.BASELINE);
      starling.showStats = true;
      starling.enableErrorChecking = true;
      starling.start();
    }
  }

}



import starling.core.Starling;
import starling.display.Sprite;
import starling.display.MovieClip;
import starling.textures.Texture;
import starling.textures.TextureAtlas;
import starling.events.Event;

import de.flintfabrik.starling.display.FFParticleSystem;
import de.flintfabrik.starling.display.core.SystemOptions;

import a24.tween.Tween24;
import a24.tween.events.Tween24Event;


internal class MainView extends Sprite {
  [Embed(source="assets/piyo.png")]
  private var PiyoTexture:Class;
  [Embed(source="assets/piyo.xml", mimeType="application/octet-stream")]
  private var PiyoAtlas:Class;
  [Embed(source="assets/light.png")]
  private static const LightTexture:Class;
  [Embed(source="assets/light.pex", mimeType="application/octet-stream")]
  private static const LightConfig:Class;
  // プロパティ
  private var basic:Quad;
  private var night:Quad;
  private var atween:Tween24;
  private var texture:Texture;
  private var atlas:TextureAtlas;
  private var piyo:MovieClip;
  private var light:FFParticleSystem;

  // コンストラクタ
  public function MainView() {
    FFParticleSystem.init(2048, false, 2048, 16);
    addEventListener(Event.ADDED_TO_STAGE, init);
    addEventListener(Event.REMOVED_FROM_STAGE, remove);
  }

  // メソッド
  private function init(evt:Event):void {
    removeEventListener(Event.ADDED_TO_STAGE, init);
    basic = new Quad(600, 300, 0x002267);
    addChild(basic);
    night = new Quad(600, 300, 0x008A42);
    addChild(night);
    night.alpha = 0;
    initialize();
    texture = Texture.fromBitmap(new PiyoTexture());
    atlas = new TextureAtlas(texture, XML(new PiyoAtlas()));

    piyo = new MovieClip(atlas.getTextures(), 60);
    piyo.currentFrame = Math.random()*60;
    piyo.pivotX = 26;
    piyo.pivotY = 59;
    piyo.x = 300;
    piyo.y = 245;
    addChild(piyo);

    Starling.juggler.add(piyo);
    start();
  }
  private function remove(evt:Event):void {
    removeEventListener(Event.REMOVED_FROM_STAGE, remove);
    Starling.juggler.remove(piyo);
  }
  override public function dispose():void {
    removeEventListener(Event.ADDED_TO_STAGE, init);
    removeEventListener(Event.REMOVED_FROM_STAGE, remove);
    super.dispose();
  }
  private function initialize():void {
    var lightconfig:XML = XML(new LightConfig());
    var lighttexture:Texture = Texture.fromBitmap(new LightTexture());

    var lightoptions:SystemOptions = SystemOptions.fromXML(lightconfig, lighttexture);
    lightoptions.appendFromObject({isAnimated: true, randomStartFrames: true, sourceX: 0, sourceY: 0});
    light = new FFParticleSystem(lightoptions);
    addChild(light);

    light.emitter.x = 640;
    light.emitter.y = 215;

    light.start();
  }
  private function start():void {
    atween = Tween24.parallel(
      Tween24.serial(
        Tween24.wait(4.0),
        Tween24.func(wind, 120),
        Tween24.tween(night, 1.6, Tween24.ease.Linear).fadeIn(),
        Tween24.func(wind, 0),
        Tween24.wait(4.0),
        Tween24.func(wind, 120),
        Tween24.tween(night, 1.6, Tween24.ease.Linear).fadeOut(),
        Tween24.func(wind, 0)
      ),
      Tween24.loop(4,
        Tween24.serial(
          Tween24.parallel(
            Tween24.prop(piyo).x(640),
            Tween24.prop(light.emitter).x(640)
          ),
          Tween24.parallel(
            Tween24.tween(piyo, 2.8, Tween24.ease.QuadInOut).x(-40),
            Tween24.tween(light.emitter, 2.8, Tween24.ease.QuadInOut).x(-40)
          )
        )
      )
    );
    atween.addEventListener(Tween24Event.COMPLETE, complete, false, 0, true);
    atween.play();
  }
  private function wind(vx:Number):void {
    light.gravityX = vx;
  }
  private function complete(evt:Tween24Event):void {
    atween.removeEventListener(Tween24Event.COMPLETE, complete);
    start();
  }

}

light.png (透過PNG)

starling_FFParticleSystem5_light

light.pex
<?xml version="1.0"?>
<particleEmitterConfig>
  <animation>
    <isAnimated value="0"/>
    <loops value="1"/>
    <firstFrame value="0"/>
    <lastFrame value="-1"/>
    <randomStartFrames value="0"/>
  </animation>

  <tinted value="1"/>
  <spawnTime value="0.1" />
  <fadeInTime value="0.2" />
  <fadeOutTime value="0.2" />

  <emitterType value="0"/>
  <maxParticles value="160"/>
  <texture name="light.png"/>
  <sourcePosition x="0" y="0"/>
  <sourcePositionVariance x="16.00" y="16.00"/>

  <particleLifeSpan value="2.0000"/>
  <particleLifespanVariance value="0.0000"/>
  <angle value="270.00"/>
  <angleVariance value="0.00"/>
  <startParticleSize value="40.00"/>
  <startParticleSizeVariance value="10.00"/>
  <finishParticleSize value="0.00"/>
  <FinishParticleSizeVariance value="0.00"/>

  <rotationStart value="0.00"/>
  <rotationStartVariance value="0.00"/>
  <rotationEnd value="0.00"/>
  <rotationEndVariance value="0.00"/>
  <duration value="-1.00"/>
  <gravity x="0" y="0"/>
  <speed value="60.00"/>
  <speedVariance value="15.00"/>

  <radialAcceleration value="20.00"/>
  <radialAccelVariance value="5.00"/>

  <tangentialAcceleration value="0.00"/>
  <tangentialAccelVariance value="0.00"/>
  <maxRadius value="0.00"/>
  <maxRadiusVariance value="0.00"/>
  <minRadius value="0.00"/>
  <rotatePerSecond value="0.00"/>
  <rotatePerSecondVariance value="0.00"/>
  <startColor red="4" green="3" blue="2" alpha="0"/>
  <startColorVariance red="0" green="0" blue="0" alpha="0"/>
  <finishColor red="0" green="0" blue="0" alpha="0"/>
  <finishColorVariance red="0" green="0" blue="0" alpha="0"/>

  <blendFuncSource value="ONE"/>
  <blendFuncDestination value="ONE_MINUS_SOURCE_ALPHA"/>

</particleEmitterConfig>

参考資料「Improved Particle System for Starling | code·math·motion·jutsu
:check: 「shin10/Starling-FFParticleSystem · GitHub

そのままのソースだとエラーが出るので、ちょっとごにょごにょする。 😀


[修正] (14/06/25 Wed 15:30)
Starling 1.5 にバージョンアップ。
[修正] (14/06/25 Wed 17:19)
Starling 1.5.1 にバージョンアップ。
[修正] (14/10/08 Wed 15:02)
Tween24 2.1 にバージョンアップ。
[修正] (14/12/14 Sun 21:04)
Starling 1.6 にバージョンアップ。