Thứ tư, ngày 04 tháng ba năm 2009

Rotation Angle Issue

When transitioning an angle with value > 180 or < 0, using this function to fix the problem.

function properRotation(target_rotation,my_rotation,smooth) {
var rot =target_rotation-my_rotation;
while (rot<0) {
rot += 360;
}
while (rot>180) {
rot -= 360;
}
my_rotation += rot/smooth;
return my_rotation;
}

Thanks the thread from http://www.mail-archive.com/away3d-dev@googlegroups.com/msg02572.html

Thứ sáu, ngày 27 tháng hai năm 2009

Water Effect AS2

import flash.display.BitmapData;
import flash.filters.ConvolutionFilter;
import flash.filters.DisplacementMapFilter;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import mx.utils.Delegate;
/**
* ...
* @author binhdocco
*/
class WaterEffect extends Object {
private var mc: MovieClip;
private var IMAGE_WIDTH: Number;
private var IMAGE_HEIGHT: Number;
private var IMAGE_ID: String;
private var damper: BitmapData;
private var result: BitmapData;
private var result2: BitmapData;
private var source: BitmapData;
private var buffer: BitmapData;
private var output: BitmapData;
private var surface: BitmapData;
private var bounds: Rectangle;
private var origin: Point;
private var wave: ConvolutionFilter;
private var damp: ColorTransform;
private var water: DisplacementMapFilter;
private var matrix: Matrix;
private var matrix2: Matrix;
private var mouseUp: Boolean = true;

public function WaterEffect(mc: MovieClip, imageId: String) {
this.mc = mc;
this.IMAGE_ID = imageId;
surface= BitmapData.loadBitmap(this.IMAGE_ID);
var tempMC: MovieClip = mc.createEmptyMovieClip("dummy_mc", mc.getNextHighestDepth());
tempMC.attachBitmap(surface, 1);
this.IMAGE_WIDTH = tempMC._width;
this.IMAGE_HEIGHT = tempMC._height;
tempMC.removeMovieClip();
damper= new BitmapData(IMAGE_WIDTH/2, IMAGE_HEIGHT/2, false, 128);
result= new BitmapData(IMAGE_WIDTH/2, IMAGE_HEIGHT/2, false, 128);
result2= new BitmapData(IMAGE_WIDTH, IMAGE_HEIGHT, false, 128);
source= new BitmapData(IMAGE_WIDTH/2, IMAGE_HEIGHT/2, false, 128);
buffer= new BitmapData(IMAGE_WIDTH/2, IMAGE_HEIGHT/2, false, 128);
output= new BitmapData(IMAGE_WIDTH, IMAGE_HEIGHT, true, 128);

bounds= new Rectangle(0, 0, IMAGE_WIDTH/2, IMAGE_HEIGHT/2);
origin= new Point();
matrix= new Matrix();
matrix2= new Matrix();
matrix2.a = matrix2.d = 2;
wave= new ConvolutionFilter(3, 3, [1, 1, 1, 1, 1, 1, 1, 1, 1], 9, 0);
damp= new ColorTransform(0, 0, 9.960937E-001, 1, 0, 0, 2, 0);
water= new DisplacementMapFilter(result2, origin, 4, 4, 32, 32, "ignore");

this.mc.attachBitmap(output, 0);

var thisObj = this;
mc.onEnterFrame = Delegate.create(this, enterFrame);
mc.onMouseUp = function() {
thisObj.mouseUp = true;
}
mc.onMouseDown = function() {
thisObj.mouseUp = false;
}
}

private function enterFrame():Void {
if (mouseUp)
{
var xMouse = this.mc._xmouse / 2;
var yMouse = this.mc._ymouse / 2;
source.setPixel(xMouse + 1, yMouse, 16777215);
source.setPixel(xMouse - 1, yMouse, 16777215);
source.setPixel(xMouse, yMouse + 1, 16777215);
source.setPixel(xMouse, yMouse - 1, 16777215);
source.setPixel(xMouse, yMouse, 16777215);
} // end if
result.applyFilter(source, bounds, origin, wave);
result.draw(result, matrix, null, "add");
result.draw(buffer, matrix, null, "difference");
result.draw(result, matrix, damp);
result2.draw(result, matrix2, null, null, null, true);
output.applyFilter(surface, new Rectangle(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT), origin, water);

buffer = source;
source = result.clone();
}

}

ImageGallery 1

package blueroad.todo.imagegallery1 { //blueroad.todo.imagegallery1.ImageGallery1
import blueroad.base.PaperBase;
import caurina.transitions.Tweener;
import com.pixelfumes.reflect.ReflectBitmap;
import flash.display.Bitmap;
import flash.display.MovieClip;
import org.papervision3d.core.utils.InteractiveSceneManager;
import org.papervision3d.events.FileLoadEvent;
import org.papervision3d.events.InteractiveScene3DEvent;
import org.papervision3d.materials.BitmapFileMaterial;
import org.papervision3d.materials.BitmapMaterial;
import org.papervision3d.objects.primitives.Plane;

/**
* ...
* @author binhdocco
*/
public class ImageGallery1 extends PaperBase {

private var num:uint = 0;
private var totalThumbs:uint = 0;
private var planes: Array;
private var currentIndex: int = 0;
public var isShowingLargeImage: Boolean = false;
//PLANE STATUS
public const STATUS_HOVER: String = "STATUS_HOVER";
public const STATUS_NORMAL: String = "STATUS_NORMAL";
public const STATUS_ACTIVE: String = "STATUS_ACTIVE";
public const STATUS_TWEENING: String = "STATUS_TWEENING";
public function ImageGallery1() {
this.init();
}

override protected function init2d():void {
super.init2d();
totalThumbs = Global_ImageGallery1.resourceManager.total;
planes = new Array();
}

override protected function init3d():void {
//super.init3d();
current_camera.zoom = 10;
current_camera.z = 1000;
current_camera.x = 8000;

createThumb();
}

private function createThumb():void {
num = 0;
for (var i:int = 0; i < totalThumbs; i++) {
var thumbXML: XML = Global_ImageGallery1.resourceManager.imageXMLList[i];
var thumb: String = Global_ImageGallery1.resourceManager.sourceImagePath + "/" + thumbXML.@image_icon;
var image: String = Global_ImageGallery1.resourceManager.sourceImagePath + "/" + thumbXML.@image;

var mat: BitmapFileMaterial = new BitmapFileMaterial(thumb as String);
mat.updateBitmap();
//mat.doubleSided = true;
mat.smooth = true;
mat.interactive = true;
mat.extra = {id: i, image: image, index: i };
mat.addEventListener(FileLoadEvent.LOAD_COMPLETE, createPlane);
}
}

private function onOutPlane(e:InteractiveScene3DEvent):void {
if (isShowingLargeImage) return;
current_viewport.buttonMode = false;
var plane: Plane = e.displayObject3D as Plane;
if (plane.extra.status == STATUS_HOVER) {
if (!Tweener.isTweening(current_camera)) {
var x = plane.extra.pos.x;
var z = plane.extra.pos.z;
var refPlane: Plane = current_scene.getChildByName("refPlane" + plane.extra.id) as Plane;
Tweener.addTween(plane, { x:x, z:z, time:1, transition:"easeOutCubic" } );
Tweener.addTween(refPlane, { x:x, z:z, time:1, transition:"easeOutCubic" } );
}
plane.extra.status = STATUS_NORMAL;
}

}

private function onOverPlane(e:InteractiveScene3DEvent):void {
if (isShowingLargeImage) return;
current_viewport.buttonMode = true;
var plane: Plane = e.displayObject3D as Plane;
if (plane.extra.status == STATUS_NORMAL) {
if (!Tweener.isTweening(current_camera)) {
var x = plane.extra.pos.x + 200 * Math.sin(55 * (Math.PI / 180));
var z = plane.extra.pos.z - 200 * Math.cos(55 * (Math.PI / 180));

var refPlane: Plane = current_scene.getChildByName("refPlane" + plane.extra.id) as Plane;

Tweener.addTween(plane, { x:x, z:z, time:1, transition:"easeOutCubic" } );
Tweener.addTween(refPlane, { x:x, z:z, time:1, transition:"easeOutCubic" } );
}
plane.extra.status = STATUS_HOVER;
}
}

private function onPressPlane(e:InteractiveScene3DEvent):void {
if (isShowingLargeImage) return;
var plane: Plane = e.displayObject3D as Plane;
//if (plane.extra.index != currentIndex) {
if (plane.extra.status != STATUS_ACTIVE) {
currentIndex = plane.extra.index;
arrangeThumbAt(currentIndex);
} else {
var imageDisplay: ImageDisplay = MovieClip(this.parent).imageLoader_mc as ImageDisplay;
imageDisplay.loadImage(plane.extra.image);
}
//}
}

private function createPlane(e: FileLoadEvent):void {
var mat: BitmapFileMaterial = e.target as BitmapFileMaterial;
var plane: Plane = new Plane(mat, mat.bitmap.width, mat.bitmap.height, 4, 4);
plane.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, onOverPlane);
plane.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, onOutPlane);
plane.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, onPressPlane);
plane.extra = mat.extra;
plane.extra.status = STATUS_NORMAL;
planes.push(plane);
//reflect
var refBitmap: Bitmap = ReflectBitmap.getReflectBitmap(mat.bitmap, 100, 0.5);
refBitmap.cacheAsBitmap = true;
var refMat: BitmapMaterial = new BitmapMaterial(refBitmap.bitmapData);
refMat.smooth = true;
//refMat.doubleSided = true;
var refPlane: Plane = new Plane(refMat, refMat.bitmap.width, refMat.bitmap.height, 4, 4);
refPlane.extra = { };
//
default_scene.addChild(plane, "plane" + plane.extra.id);
default_scene.addChild(refPlane, "refPlane" + plane.extra.id);
//num ++;
/*if (num >= totalThumbs - 1) */arrangeThumb();
}

private function arrangeThumb():void {
for (var i:int = 0; i < planes.length; i++) {
var plane: Plane = planes[i] as Plane;
var rotationY: Number = 0;
if (i > 0)
{
rotationY = 35;
} else {
plane.extra.status = STATUS_ACTIVE;
}

var x = -100 + Math.cos(55 * (Math.PI / 180)) * 200 * i;
var z = Math.sin(55 * (Math.PI / 180)) * 200 * i;
var y = 0;
plane.extra.pos = { x: x, z: z };
var refPlane: Plane = current_scene.getChildByName("refPlane" + plane.extra.id) as Plane;
refPlane.extra.pos = { x: x, z: z };
Tweener.addTween(plane, { rotationY: rotationY, x: x, y: y, z: z, time: .5 , transition:"easeOutBack" } );
Tweener.addTween(refPlane, { rotationY: rotationY, x: x, y: y - refPlane.material.bitmap.height - 2, z: z, time: .5 , transition:"easeOutBack" } );
}
if (i == (totalThumbs - 1)) {
Tweener.addTween(current_camera, { z: -1000, x:0, time:6, delay:0.3, transition:"easeOutBack" } );
}
}

//
private function arrangeThumbAt(index: int):void {
for (var i:int = 0; i < planes.length; i++) {
var plane: Plane = planes[i] as Plane;
var dis: int = plane.extra.index - index;
var rotationY: Number = 0;

if (dis != 0)
{
rotationY = 35;
plane.extra.status = STATUS_TWEENING;
} else {
plane.extra.status = STATUS_ACTIVE;
}
var pos: int = 0;
if (dis > 0) {
pos = dis;
} else if (dis < 0) {
pos = planes.length + dis;
}

var x = -100 + Math.cos(55 * (Math.PI / 180)) * 200 * pos;
var z = Math.sin(55 * (Math.PI / 180)) * 200 * pos;
var y = 0;
plane.extra.pos = { x: x, z: z};
plane.extra.index = pos;

var refPlane: Plane = current_scene.getChildByName("refPlane" + plane.extra.id) as Plane;
refPlane.extra.pos = { x: x, z: z };

Tweener.removeTweens(plane);
Tweener.removeTweens(refPlane);
if (dis > 0) {
Tweener.addTween(plane, { rotationY: rotationY , x: x, z: z, time: 1.5 , transition:"easeOutBack", onComplete: turnNormal, onCompleteParams: [plane] } );
Tweener.addTween(refPlane, { rotationY: rotationY , x: x, z: z, time: 1.5 , transition:"easeOutBack"} );
} else if (dis < 0) {
Tweener.addTween(plane, {rotationY: rotationY , x: plane.x - 2500, z: plane.z - 2500, time: .8 , transition:"easeOutBack", onComplete: continue1, onCompleteParams: [plane]} );
Tweener.addTween(refPlane, {rotationY: rotationY , x: refPlane.x - 2500, z: refPlane.z - 2500, time: .8 , transition:"easeOutBack", onComplete: continue1, onCompleteParams: [refPlane]} );
} else {
Tweener.addTween(plane, { rotationY: rotationY , x: x, z: z, time: 1.5 , transition:"easeOutBack"} );
Tweener.addTween(refPlane, { rotationY: rotationY , x: x, z: z, time: 1.5 , transition:"easeOutBack"} );
}
}
}

private function continue1(plane: Plane):void {
plane.x = plane.extra.pos.x + 1000;
plane.z = plane.extra.pos.z + 1000;

Tweener.addTween(plane, {x: plane.extra.pos.x, z: plane.extra.pos.z, time: .7 , transition:"easeOutBack", onComplete: turnNormal, onCompleteParams: [plane]} );
}

private function turnNormal(plane: Plane):void {
plane.extra.status = STATUS_NORMAL;
}
}

}javascript:void(0)

CoverFlow AS3

package com.drca.coverflow { //com.drca.coverflow.CoverFlowManager
import blueroad.base.PaperBase;
import caurina.transitions.Tweener;
import com.pixelfumes.reflect.Reflect;
import flash.display.MovieClip;
import flash.net.getClassByAlias;
import gs.TweenMax;
import org.papervision3d.events.InteractiveScene3DEvent;
import org.papervision3d.materials.MovieAssetMaterial;
import org.papervision3d.materials.MovieMaterial;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.Plane;

/**
* ...
* @author binhdocco
*/
public class CoverFlowManager extends PaperBase {
private var pics: Array = [new MOV_1(), new MOV_2(), new MOV_3(), new MOV_4(), new MOV_5()];// , new MOV_6(), new MOV_7()];
private var planes: Array = [];
//for tweener
private var time: Number = 1;
private const TRANSITION_ANI: String = "easeoutback";
//for position
private var distance: uint = 300;
private var roAngle: int = 60;
//camera position
private var camera_z: int = -850;
private var camera_y: int = 200;

public function CoverFlowManager() {
init(1024, 726);
}

override protected function init3d():void {
super.init3d();
for (var i:int = 0; i < pics.length; i++) {
//var linkageId: String = pics[i];

var mov: MovieClip = pics[i] as MovieClip;
new Reflect( { mc:mov, alpha:50, ratio:50, distance:0, updateTime:0, reflectionDropoff:1 } );
var mat: MovieMaterial = new MovieMaterial(mov, true);
mat.interactive = true;
mat.smooth = true;

//get width & height
var mc: MovieClip = MovieClip(mat.movie);
var plane: Plane = new Plane(mat, mc.width, mc.height, 10, 10);
plane.x = i * distance;
plane.y -= mc.height / 4;
plane.extra = { status: "normal", id: i };
planes.push(plane);
plane.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, onClickPlane);
plane.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, onOverPlane);
plane.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, onOutPlane);
default_scene.addChild(plane);
}

default_camera.zoom = 6;
//default_camera.z = camera_z;
//default_camera.y = camera_y;

//show active plane
var activePlane: Plane = planes[Math.floor(planes.length/2)];
setPlaneActive(activePlane);
}

private function onOutPlane(e:InteractiveScene3DEvent):void {
viewport.buttonMode = false;
}

private function onOverPlane(e:InteractiveScene3DEvent):void {
viewport.buttonMode = true;
}

private function onClickPlane(e:InteractiveScene3DEvent):void {
var plane: Plane = e.displayObject3D as Plane;
switch (plane.extra.status) {
case "active":
zoomPlane(plane);
break;
case "zoom":
deZoomPlane(plane);
break;
case "normal":
setPlaneActive(plane);
break;
}
}

private function setPlaneActive(pic:Plane):void {
var id:Number = pic.extra.id;
var top:Number=-50;
var depth:Number;
var i:Number;
var dis:Number = distance / 3;
//TweenMax.to(pic, .5, { rotationY:0, rotationX:0, rotationY:0, x:0, z: -300, scale:1 } );
Tweener.addTween(pic, { time: time - 0.2, rotationY:0, rotationX:0, rotationY:0, x:0, z: -300, scale:1 });//, onComplete: zoomPlane, onCompleteParams: [pic] } );

for (i = id-1; i >=0; i--) {
dis += distance / (id - i);
depth = top + Math.abs(id - i) * 20;
planes[i].extra.status = "normal";
//TweenMax.to(planes[i], .5, { rotationY: -roAngle, x:0 - dis, z:depth } );
Tweener.addTween(planes[i], { time: time , rotationY: -roAngle, x:0 - dis, z:depth } );
}

dis = distance / 3;
i = 0;
for (i = id+1; i dis += distance / (i - id);
depth = top + Math.abs(id - i) * 20;
planes[i].extra.status = "normal";
//TweenMax.to(planes[i], .5, { rotationY:roAngle, x:0 + dis, z:depth } );
Tweener.addTween(planes[i], { time: time, rotationY:roAngle, x:0 + dis, z:depth } );
}
pic.extra.status = "active";
}

private function zoomPlane(pic:Plane):void {
var o:Number = 0;
//TweenMax.to(pic, .5, { z: -600, rotationY:o } );
Tweener.addTween(pic, { time: .7, z: -650, rotationY:o , transition: TRANSITION_ANI} );
pic.extra.status = "zoom";
}

private function deZoomPlane(pic:Plane):void {
//TweenMax.to(pic, .5, { z: -300, rotationY:0 } );
Tweener.addTween(pic, { time: time, z: -300, rotationY:0 , transition: TRANSITION_ANI} );
pic.extra.status = "active";
}
}

}