Flash AS3 3d Fireworks effect implementation code

Source: Internet
Author: User
Tags addchild explode
The code is as follows Copy Code
Package {
Import Flash.display.Bitmap;
Import Flash.display.BitmapData;
Import Flash.display.Shape;
Import Flash.display.Sprite;
Import flash.events.Event;
Import flash.events.MouseEvent;
Import Flash.filters.BlurFilter;
Import Flash.geom.ColorTransform;
Import Flash.geom.Matrix3D;
Import flash.geom.PerspectiveProjection;
Import Flash.geom.Point;
Import Flash.geom.Utils3D;
Import Flash.geom.Vector3D;

[SWF (width = 465, height = 465, BackgroundColor = 0x0, framerate = 30)]
/**
* ...
* @author Lizhi
*/
public class Fireworks3d extends Sprite {
private var cm:matrix3d = new Matrix3D ();
private var cvm:matrix3d = new Matrix3D ();
private var m:matrix3d = new Matrix3D ();
private var vm:matrix3d = new Matrix3D ();
private var p:perspectiveprojection = new Perspectiveprojection ();
private Var Pm:matrix3d;
private var vins:vector.<number> = new vector.<number> ();
private var vouts:vector.<number> = new vector.<number> ();
private var uvts:vector.<number> = new vector.<number> ();
private var v2ds:vector.<number> = new vector.<number> ();

private var view2:sprite = new Sprite ();
private var view:shape = new Shape ();
private var bmd:bitmapdata = new BitmapData (465, 465, True, 0);
private var particles:array = [];

Public Function Fireworks3d () {
PM = P.tomatrix3d ();
AddEventListener (Event.enter_frame, Render);
Stage.addeventlistener (Mouseevent.click, OnClick);
AddChild (view);
AddChild (New Bitmap (BMD));
view.x = 228;
VIEW.Y = 228;
}

Private Function OnClick (e:event=null): void {
Explode ((Math.random () -0.5) *400, (Math.random () -0.5) *400, (Math.random () -0.5) *400, 0xffffff*math.random ());
}

private var t:int = 0;

Private function Render (e:event): void {
if (Math.random () <0.05) {
OnClick ();
}
Cm.identity ();
Cm.appendtranslation (0, 200,-550);
Cm.appendrotation (VIEW.MOUSEX/10, vector3d.y_axis,cm.position);
Cm.appendrotation (-VIEW.MOUSEY/10, vector3d.x_axis,cm.position);
Cvm.rawdata = Cm.rawdata;
Cvm.invert ();
Vm.rawdata = M.rawdata;
Vm.append (CVM);
View.graphics.clear ();
for (var i:int = particles.length-1 i >= 0; i--) {
var p:particle = particles[i];
P.VX *= p.deceleration;
P.vy *= p.deceleration;
P.vz *= p.deceleration;
P.vy + = p.gravity;
p.position.x = p.position.x + P.VX;
P.POSITION.Y = P.position.y + P.vy;
P.position.z = p.position.z + p.vz;
P.energy *= p.deceleration;
if (P.energy < 0.05) {
Particles.splice (i, 1);
} else {
P.vposition = Vm.transformvector (p.position);
p.v2d = Utils3d.projectvector (PM, p.vposition);
View.graphics.lineStyle (400/P.V2D.W, P.color);
View.graphics.moveTo (p.v2d.x, P.V2D.Y);
var c:int = 0;
var tc:int = 4;
for (var j:int = p.tails.length-1 J >= 0; J--, C + +) {
if (C&GT;P.ENERGY*TC) {
P.tails.splice (J, 1);
Continue
}
var v:vector3d = p.tails[j];
var vp:vector3d = Vm.transformvector (v);
var v2d:vector3d = utils3d.projectvector (PM, VP);
View.graphics.lineStyle (500/P.V2D.W, P.color, (p.energy*tc-c)/P.ENERGY*TC);
View.graphics.lineTo (v2d.x, V2D.Y);
}
if (P.tails[0]) {
v = p.tails[p.tails.length-1];
if (MATH.SQRT (v.x-p.position.x) * (v.x-p.position.x) + (V.Y-P.POSITION.Y) * (V.Y-P.POSITION.Y) + (v.z-p.position.z) * ( V.Z-P.POSITION.Z) >p.length) {
P.tails.push (P.position.clone ());
}
}else {
P.tails.push (P.position.clone ());
}
}
}
Bmd.colortransform (Bmd.rect, New ColorTransform (0.095, 0.095, 0.095, 0.095));
Bmd.applyfilter (BMD, Bmd.rect, New Point, New Blurfilter ());
Bmd.draw (VIEW2);
}

Private function Explode (X:number, Y:number, Z:number, color:uint): void {
var c:int = 400*math.random ();
while (c--> 0) {
var p:particle = new particle;
P.energy = Math.random () * 5;
var m:matrix3d = new Matrix3D ();
M.appendtranslation (0, 0, (Math.random () *.2+0.8) * 5);
M.appendrotation (360 * math.random (), Vector3d.x_axis);
M.appendrotation (360 * math.random (), Vector3d.y_axis);
var v:vector3d = m.transformvector (new Vector3D);
P.VX = v.x;
P.vy = V.Y;
P.vz = v.z;
P.position = new Vector3D (x, y, z);
P.color = color;
P.deceleration = 0.95;
P.gravity = 0.05;
P.length = 2;
Particles.push (P);
}
}
}
}

Import Flash.geom.Vector3D;

Class Particle {
public var tails:array = [];
public Var Vx:number;
public Var Vy:number;
public Var Vz:number;
public Var Position:vector3d;
public Var Vposition:vector3d;
public Var V2d:vector3d;
public Var Energy:number;
public Var color:uint;
public Var Deceleration:number;
public Var Gravity:number;
public Var Length:number;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.