[AS3.0] ドットの光 (6)

光るドットの表現をAS3.0で! :boy:

dotLight36

:caution: 要 Flash Player 9 以上

This movie requires Flash Player 9.0.0

DotLight.as (jp.nya.project.effects.DotLight.as)
package jp.nya.project.effects {

  import flash.display.Sprite;
  import flash.display.Bitmap;
  import flash.display.BitmapData;
  import flash.display.PixelSnapping;
  import flash.display.BlendMode;
  import flash.geom.Matrix;
  import flash.geom.Rectangle;
  import flash.geom.Point;
  import flash.geom.ColorTransform;
  import flash.filters.BlurFilter;
  import flash.events.Event;

  import flash.utils.getTimer;
  import jp.nya.project.effects.EmitLight;

  public class DotLight extends Sprite {
    // プロパティ
    private var rect:Rectangle;
    private var canvas:BitmapData;
    private var map:BitmapData;
    private var sparkle:BitmapData;
    private var afterglow:BitmapData;
    private static var scale:uint = 4;
    private var aMatrix:Matrix;
    private var colorTrans:ColorTransform;
    private var blur:BlurFilter;
    private var sMatrix:Matrix;
    private var offsets:Array;
    private var seed:uint;
    private var light:EmitLight;

    // コンストラクタ
    public function DotLight(r:Rectangle) {
      rect = r;
      init();
    }

    // メソッド
    private function init():void {
      afterglow = new BitmapData(rect.width*2/scale, rect.height*2/scale, false, 0xFF000000);
      var aBitmap:Bitmap = new Bitmap(afterglow, PixelSnapping.AUTO, true);

      aBitmap.scaleX = aBitmap.scaleY = scale/2;
      addChild(aBitmap);
      canvas = new BitmapData(rect.width, rect.height, true, 0x00000000);
      var cBitmap:Bitmap = new Bitmap(canvas);

      cBitmap.blendMode = BlendMode.ADD;
      addChild(cBitmap);
      map = new BitmapData(rect.width/scale, rect.height/scale, false, 0xFF000000);
      offsets = [new Point(), new Point()];
      seed = Math.floor(Math.random()*1000);
      sparkle = new BitmapData(rect.width/scale, rect.height/scale, true, 0x00000000);
      var sBitmap:Bitmap = new Bitmap(sparkle);

      sBitmap.smoothing = true;
      sBitmap.blendMode = BlendMode.ADD;
      sBitmap.scaleX = sBitmap.scaleY = scale;

      addChild(sBitmap);
      aMatrix = new Matrix(2/scale, 0, 0, 2/scale, 0, 0);
      colorTrans = new ColorTransform(0.1, 0.1, 0.1);
      blur = new BlurFilter(2, 2, 1);
      sMatrix = new Matrix(1/scale, 0, 0, 1/scale, 0, 0);
      light = new EmitLight(canvas, map, scale);
    }
    public function start():void {
      addEventListener(Event.ENTER_FRAME, draw, false, 0, true);
    }
    public function stop():void {
      removeEventListener(Event.ENTER_FRAME, draw);
    }
    private function draw(evt:Event):void {
      var offset:Number = getTimer()*0.05;
      offsets[0].x = offsets[1].y = offset;
      map.perlinNoise(rect.width/scale, rect.height/scale, 2, seed, true, true, 1, false, offsets);
      light.create(10);
      canvas.lock();
      canvas.fillRect(canvas.rect, 0x00000000);
      light.emit();
      canvas.unlock();
      sparkle.lock();
      sparkle.fillRect(sparkle.rect, 0x00000000);
      sparkle.draw(canvas, sMatrix);
      sparkle.unlock();
      afterglow.lock();
      afterglow.draw(canvas, aMatrix, colorTrans, BlendMode.ADD);
      afterglow.applyFilter(afterglow, afterglow.rect, new Point(), blur);

      afterglow.unlock();
    }

  }

}

EmitLight.as (jp.nya.project.effects.EmitLight.as)
package jp.nya.project.effects {

  import flash.display.BitmapData;
  import flash.geom.Rectangle;

  import frocessing.color.ColorHSV;
  import jp.nya.project.geom.Bresenham;
  import jp.nya.project.particle.Dot;


  public class EmitLight {
    // プロパティ
    private var canvas:BitmapData;
    private var map:BitmapData;
    private var rect:Rectangle;
    private var scale:uint;
    private var cx:uint;
    private var cy:uint;
    private var radius:uint;
    private static var yScale:Number = 0.25;
    private var dots:Array;
    private static var acceleration:Number = 0.01;
    private static var gravity:Number = 0.03;
    private static var deceleration:Number = 0.008;
    private var color:ColorHSV;
    private static var length:Number = 2;
    private var manager:Bresenham;

    // コンストラクタ
    public function EmitLight(c:BitmapData, m:BitmapData, s:uint) {
      canvas = c;
      map = m;
      rect = canvas.rect;
      scale = s;
      cx = rect.width*0.5;
      cy = rect.height*0.85;
      radius = rect.width*0.4;
      init();
    }

    // メソッド
    private function init():void {
      dots = new Array();
      color = new ColorHSV(0, 0.4);
      manager = new Bresenham(canvas);

    }
    public function create(max:uint, time:uint):void {
      for (var n:uint = 0; n < max; n++) {
        var angle:Number = Math.random()*360;
        var power:Number = Math.random() + 0.5;
        var dot:Dot = new Dot(px, py, angle, power);
        dot.x = cx + Math.cos(angle*Math.PI/180)*radius;
        dot.y = cy + Math.sin(angle*Math.PI/180)*radius*yScale;
        dot.px = dot.x;
        dot.py = dot.y;
        color.h = 60;
        dot.rgb = color.value;
        dots.push(dot);
      }
    }
    public function emit():void {
      for (var n:uint = 0; n < dots.length; n++) {
        var dot:Dot = dots[n];
        var c:uint = map.getPixel(dot.x/scale, dot.y/scale);
        dot.cx += ((((c >> 16) & 0xFF) – 0x80) / 0x80)*5;
        dot.vy += gravity*dot.power;
        dot.vy *= 0.99;
        dot.vx += acceleration;
        dot.angle += dot.vx;
        dot.cy -= dot.vy;
        var px:Number = Math.cos(dot.angle*Math.PI/180)*radius;
        var py:Number = Math.sin(dot.angle*Math.PI/180)*radius*yScale;
        dot.x = dot.cx + px*(dot.energy*0.4 + 0.2);
        dot.y = dot.cy + py*(dot.energy*0.4 + 0.2);
        dot.energy -= deceleration;
        var x0:int = dot.x;
        var y0:int = dot.y;
        var x1:int = dot.x – (dot.x – dot.px)*length;
        var y1:int = dot.y – (dot.y – dot.py)*length;
        color.h = 60*dot.energy*0.5;
        dot.rgb = color.value;
        manager.draw(x0, y0, x1, y1, dot.rgb, dot.energy*0.5);
        dot.px = dot.x;
        dot.py = dot.y;
        if (dot.energy < 0) {
          dots.splice(n, 1);
          dot = null;
        }
      }
    }

  }

}

Bresenham.as (jp.nya.project.geom.Bresenham.as)
Main.as
:banana: [AS3.0] ドットの光 (3) 参照。

Dot.as (jp.nya.project.particle.Dot.as)
:banana: [AS3.0] ドットの光 (4) 参照。