- 最后登录
- 2018-12-19
- 注册时间
- 2012-8-20
- 阅读权限
- 90
- 积分
- 54706
- 纳金币
- 32328
- 精华
- 41
|
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Shape;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import org.papervision3d.materials.BitmapAssetMaterial;
import org.papervision3d.materials.BitmapFileMaterial;
import org.papervision3d.materials.BitmapMaterial;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.MovieAssetMaterial;
import org.papervision3d.materials.MovieMaterial;
import org.papervision3d.materials.special.CompositeMaterial;
import org.papervision3d.materials.WireframeMaterial;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.objects.primitives.PaperPlane;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.objects.primitives.Sphere
import org.papervision3d.view.BasicView;
import org.papervision3d.materials.BitmapColorMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.events.InteractiveScene3DEvent;
/**
* ...
* @author 张首尚
*/
public class vb extends BasicView
{
private var shpere:Sphere;
private var shpere2:Sphere;
private var planelane;
private var cube:Cube;
private var mList:MaterialsList;
private var paperPlaneaperPlane;
private var do3DisplayObject3D;
/**图片*/
private var imageBitmap:Bitmap;
/**图片加载*/
private var imageLoaderoader;
public function vb()
{
super(640, 480, false, ***e); //***e 可交互
init();
}
override protected function onRenderTick(e:Event=null):void
{
//cube.localRotationX += 1;
//plane.localRotationY += 1;
super.onRenderTick();
}
private function init():void
{
imageLoader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
imageLoader.load(new URLRequest("assets/1.jpg"));
}
private function onComplete(evt:Event):void
{
//图片数据
imageBitmap = imageLoader.content as Bitmap;
mList = new MaterialsList();
//线条材质
var xt:WireframeMaterial = new WireframeMaterial(0xFF00FF, 1, 2);
xt.interactive = ***e;
//颜色材质
var cl:ColorMaterial = new ColorMaterial(0x11FF55, 1);
//shape 图形
var shap:Shape = new Shape();
shap.graphics.beginFill(0x334455, 1);
shap.graphics.drawCircle(100, 100, 100);
var bitdata:BitmapData = new BitmapData(200, 200, ***e, 0x0);
bitdata.draw(shap);
//位图材质一 程序绘制
var bitM:BitmapMaterial = new BitmapMaterial(bitdata, ***e);
bitM.smooth = ***e;
//位图材质二 导入图片
var bitM2:BitmapMaterial = new BitmapMaterial(imageBitmap.bitmapData, ***e);
bitM2.smooth = ***e;
//位图材质三 导入图片
var bitM3:BitmapFileMaterial = new BitmapFileMaterial("assets/2.jpg");
bitM3.smooth = ***e;
//元件位图材质 IMG_0010库里的图片元件类名
var bitM4:BitmapAssetMaterial = new BitmapAssetMaterial("IMG_0010");
bitM4.smooth = ***e;
/*在Flex Builder and Flash Builder中使用元件是将元件转换成BitmapAsset对象 ,然后用
BitmapAsset的bitmapData属性然创建一个常规的BitmapMaterial.对象。
嵌入也可以实现。使用元数据标签[Embed]:如下面的代码所示:
[Embed (source="assets/mill.jpg")] public var mill:Class;
我们将mill.jpg文件导入为元件。命名为mill类,下面这行代码将创建mill
对象并将它转换成BitmapAsset格式。
var millAsset:BitmapAsset = new mill() as BitmapAsset;
然后创建位图材质,就像我们前面的做法一样。
var material:BitmapMaterial = new BitmapMaterial(millAsset.bitmapData);
注意这种方法只能在flex Builder 和Flash Builder 中使用。并且没有用到BitmapAssetMaterial
类。*/
var materialSprite:Sprite = new Sprite();
materialSprite.graphics.beginFill(0x333333);
materialSprite.graphics.drawRect(0,0,200,200);
var textfield:TextField = new TextField();
textfield.text = "Hello 3D world!";
textfield.autoSize = TextFieldAutoSize.LEFT;
textfield.setTextFormat(new TextFormat("Arial",25));
textfield.x = (materialSprite.width - textfield.width) / 2;
textfield.y = (materialSprite.height - textfield.height) / 2;
materialSprite.addChild(textfield);
//displayObject 子类做材质
var bitM5:MovieMaterial = new MovieMaterial(materialSprite);
bitM5.doubleSided = ***e;
bitM5.smooth = ***e;
//影片剪辑做材质
var bitM6:MovieAssetMaterial = new MovieAssetMaterial("mc1",false,***e,false,***e);
bitM6.doubleSided = ***e;
bitM6.smooth = ***e;
bitM6.interactive = ***e;
//bitM6.rect = new Rectangle(100,100,500,500);
/*mList.addMaterial(bitM5, "front");
mList.addMaterial(cl, "back");
mList.addMaterial(bitM4, "left");
mList.addMaterial(bitM3, "right");
mList.addMaterial(bitM2, "top");
mList.addMaterial(bitM, "bottom");
cube = new Cube(mList, 600, 600, 200, 3, 3,3,Cube.ALL);
this.scene.addChild(cube);*/
//组合材质 材质重叠
var cm:CompositeMaterial = new CompositeMaterial();
cm.addMaterial(xt);
cm.addMaterial(bitM6);
cm.doubleSided = ***e;
cm.interactive = ***e;
MovieClip(bitM6.movie).addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
MovieClip(bitM6.movie).addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
plane = new Plane(cm, 500, 500);
plane.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, onClick);
plane.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, onMouseOver);
plane.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, onMouseOut);
this.scene.addChild(plane);
this.startRendering();
}
//点击事件
private function onClick(evt:InteractiveScene3DEvent):void
{
evt.displayObject3D.localRotationY += 12;
}
//鼠标移上去
private function onMouseOver(evt:InteractiveScene3DEvent):void
{
viewport.buttonMode = ***e;
}
//鼠标移出来
private function onMouseOut(evt:InteractiveScene3DEvent):void
{
viewport.buttonMode = false;
}
/*平滑材质使用 material.smooth = ***e.
双面材质使用 material.doubleSided = ***e.
动画材质使用 material.animated= ***e.
阻止材质扭曲使用 material.precise = ***e.
材质和物体可以交互使用 viewport.interactive = ***e
material.interactive = ***e.
翻转材质使用DisplayObject的 scaleX属性,或者使用BitmapMaterialTools功能类的
静态方法来翻转bitmap数据:BitmapMaterialTools.mirrorBitmapX( material.bitmap).
平铺材质使用 material.tiled = ***e并设置 material.maxU 和 material.maxV这两个
横线和纵向重复数.*/
}
}
【来源:互联网】
更多精彩教程,尽在web3D纳金网http://www.narkii.com/college/ |
|