这个例子展示的是一个球体,它上面由很多小三角面组成,这些小的三角面可以随着音乐的变化进而变换色彩。下面是这个例子的源代码。
import flash.display.*;
import flash.events.*;
import flash.media.Sound;
import flash.media.SoundMixer;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.display.Bitmap;
import flash.display.BitmapData;
import org.papervision3d.scenes.*;
import org.papervision3d.objects.*;
import org.papervision3d.cameras.*;
import org.papervision3d.materials.*;
import org.papervision3d.events.*;
import org.papervision3d.core.proto.DisplayObjectContainer3D;
import com.mrdoob.pv3d_phunky.objects.*;
import com.mrdoob.utils.*;
import caurina.transitions.Tweener;
// .. VARS .....................................................................................................
var container :Sprite;
var containerPos :Sprite;
var bitmapPos :BitmapData;
var mtr :Matrix;
var scene :MovieScene3D;
var camera :Camera3D;
var root3D :DisplayObject3D;
var sound :Sound;
var bytes :ByteArray;
var amount :Number = 15;
var values :Array;
var resetPos :Number = 0;
// .............................................................................................................
stage.quality = "MEDIUM";
stage.scaleMode = "noScale";
stage.align = "TL";
stage.addEventListener(Event.RESIZE, resizeHandler);
function resizeHandler(e)
{
mtr = new Matrix();
mtr.translate(stage.stageWidth * .5,stage.stageHeight * .5);
mtr.scale(.01,.01);
container.x = stage.stageWidth * .5;
container.y = stage.stageHeight * .5;
containerPos.width = stage.stageWidth;
containerPos.height = stage.stageHeight;
}
init3D();
init();
resizeHandler(null);
generateCameraPath();
this.addEventListener( Event.ENTER_FRAME, loop3D );
// .. INIT .....................................................................................................
function init()
{
addChild(np);
np.buttonMode = true;
np.addEventListener(MouseEvent.CLICK, goToURL);
}
// .. CUSTOM FUNCTIONS .........................................................................................
function generateCameraPath()
{
var bz = new Array();
var bz_t = new Array();
for (var i = 0; i < 100; i++)
{
bz.push({x:Math.random()*3000-1500,y:Math.random()*3000-1500,z:Math.random()*3000-1500});
bz_t.push({x:Math.random()*2000-1000,y:Math.random()*200-100,z:Math.random()*2000-1000});
}
Tweener.addTween(camera,{x:2000,y:2000,z:0,_bezier:bz,time:1000,transition:"linear"});
Tweener.addTween(camera.target,{x:0,y:0,z:0,_bezier:bz_t,time:1000,transition:"linear"});
Tweener.addTween(this,{time:1000,onComplete:function(){generateCameraPath()}});
}
function goToURL(e)
{
navigateToURL(new URLRequest("http://www.scene.org/~gloom/"));
}
// .. INIT 3D ...................................................................................................
function init3D():void
{
// Create canvas movieclip and center it
container = new Sprite();
addChild( container );
containerPos = new Sprite();
bitmapPos = new BitmapData(Math.floor(stage.stageWidth * .01), Math.floor(stage.stageHeight * .01), false, 0x000000);
containerPos.addChild(new Bitmap(bitmapPos,"auto", true));
containerPos.blendMode = BlendMode.ADD;
addChild( containerPos );
// Create scene
scene = new MovieScene3D( this.container );
// Create camera
camera = new Camera3D();
camera.x = 2000;
camera.y = 500;
camera.z = 500;
camera.zoom = 5;
camera.focus = 100;
camera.sort = false;
var sound:Sound = new Sound();
sound.load(new URLRequest("gloom_and_flipside-evoid_droid.mp3"));
sound.play();
bytes = new ByteArray();
var p:DisplayObject3D;
var spacingx:Number = 0;
var spacingz:Number = 0;
values = new Array();
this.scene.renderCamera( this.camera );
for (var x:Number = 0; x < amount; x++) {
for (var z:Number = 0; z < amount; z++) {
values[x+(z*100)] = 0;
p = scene.addChild( new Pyramid( new ColorMaterial( 0xffffff ), 100, 100, 1, 1 ), "tile"+x+"_"+z );
p.container.blendMode = BlendMode.ADD;
p.x = Math.random()*1000 - 500;
p.y = Math.random()*1000 - 500;
p.z = Math.random()*1000 - 500;
var l:DisplayObject3D = new DisplayObject3D();
l.x = 0;
l.y = 0;
l.z = 0;
p.lookAt(l);
p.pitch(-90);
}
}
}
// .. LOOP 3D ...................................................................................................
function loop3D( event :Event ):void
{
SoundMixer.computeSpectrum( bytes, true, 0 );
var value: Number = 0;
var scale: Number = 3;
var bias: Number = 1;
for (var x:Number = 0; x < amount; x++) {
for (var z:Number = 0; z < amount; z++) {
value = bytes.readFloat()* (scale+bias);
bias += 0.1;
var tile:DisplayObject3D = scene.getChildByName("tile"+x+"_"+z);
values[x+(z*100)] += 1;
if (-value < values[x+(z*100)]) {
values[x+(z*100)] = -value;
} else if (values[x+(z*100)] > 0) {
values[x+(z*100)] = 0;
}
tile.geometry.vertices[3].y = -values[x+(z*100)]*50;
tile.material = new ColorMaterial(ColorTools.getHex(tile.geometry.vertices[3].y*.002+.1,tile.geometry.vertices[3].y*.0008,tile.geometry.vertices[3].y*.0002+.05));
}
}
scene.renderCamera( camera );
bitmapPos.colorTransform(new Rectangle(0,0,bitmapPos.width,bitmapPos.height),new ColorTransform(1,1,1,1,-100,-100,-100,0));
bitmapPos.draw(container,mtr,null,null,new Rectangle(0,0,stage.stageWidth,stage.stageHeight));
}