本文介绍怎么在flex
项目中使用Pv3d
。
需要你对flex有一定的了解。
方法1 直接在mxml文件中加入Pv3D组件
1。下载pv3组件,(Papervision3d
最新源代码和swc库文件:http://code.google.com/p/papervision3d
/
)
2。把Papervision3D_2.0.883.swc和header.swc加入工程。
3。源程序如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
width="640"
height="480"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
applicationComplete="applicationComplete()">
<mx:Script>
<![CDATA[
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.view.BasicView;
import org.papervision3d.lights.PointLight3D;
import org.papervision3d.materials.BitmapMaterial;
import org.papervision3d.materials.shaders.PhongShader;
import org.papervision3d.materials.shaders.ShadedMaterial;
import org.papervision3d.materials.shaders.Shader;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.core.utils.Mouse3D;
import org.papervision3d.cameras.Camera3D;
import org.papervision3d.view.layer.ViewportLayer;
import org.papervision3d.objects.DisplayObject3D;
private var myMouse3D:Mouse3D;
[Embed(source="../assets/1.jpg")]
private var frontAsset:Class;
[Embed(source="../assets/2.jpg")]
private var backAsset:Class;
[Embed(source="../assets/4.jpg")]
private var bottomAsset:Class;
[Embed(source="../assets/5.jpg")]
private var leftAsset:Class;
[@Embed(source='../assets/6.jpg')]
private var rightAsset:Class;
[Embed(source="../assets/3.jpg")]
private var topAsset:Class;
private var light:PointLight3D;
private var cube:MyCube;
private var basicView:BasicView = new BasicView();
private var materialsList:MaterialsList = new MaterialsList();
private function applicationComplete():void
{
materialsList.addMaterial(createShadedMaterial( new frontAsset(),true ), "front");
materialsList.addMaterial(createShadedMaterial( new backAsset() ), "back");
materialsList.addMaterial(createShadedMaterial( new bottomAsset() ), "bottom");
materialsList.addMaterial(createShadedMaterial( new leftAsset() ), "left");
materialsList.addMaterial(createShadedMaterial( new rightAsset() ), "right");
materialsList.addMaterial(createShadedMaterial( new topAsset() ), "top");
//定义立方体
cube = new MyCube(materialsList, 500, 500, 500, 5, 5, 5);
basicView.scene.addChild(cube);
Mouse3D.enabled = true;
basicView.viewport.interactive=true;
myMouse3D =this.basicView.viewport.interactiveSceneManager.mouse3D;
this.stage.addEventListener(MouseEvent.MOUSE_WHEEL,onWheels);
this.stage.addEventListener(MouseEvent.MOUSE_MOVE,onRenderTick);
basicView.startRendering();
pv3dPanel.rawChildren.addChild(basicView); //method #1
}
protected function onRenderTick(event:Event=null):void
{
cube.rotationY += (basicView.viewport.containerSprite.mouseX - cube.rotationY) * .1;
cube.rotationX += (basicView.viewport.containerSprite.mouseY - cube.rotationX) * .1;
basicView.renderer.renderScene(basicView.scene,basicView.camera, basicView.viewport);
}
private function createShadedMaterial(bitmap:Bitmap,precise:Boolean=false):ShadedMaterial
{
var bitmapMaterial:BitmapMaterial = new BitmapMaterial(bitmap.bitmapData,precise);
bitmapMaterial.smooth=true;
bitmapMaterial.interactive=true;
var shader:Shader = new PhongShader(light, 0xffffff, 0x333333, 10, bitmap.bitmapData, bitmap.bitmapData);
var shadedMaterial:ShadedMaterial = new ShadedMaterial(bitmapMaterial, shader);
return shadedMaterial;
}
//滚动鼠标中间轮,进行缩放
private function onWheels(event:MouseEvent):void
{
basicView.camera.moveForward(10*event.delta);
}
]]>
</mx:Script>
<mx:Panel id="pv3dPanel" width="100%" height="100%">
<mx:Label text="Label" id="testLabel"/>
</mx:Panel>
</mx:Application>
4。运行效果如下:
方法2:使用自定义控件,把3d窗体封装到自定义控件中, 在主应用中引入这个自定义控件。
根据方法1进行自定义,源程序略。