Away3D实例教程 - 贴图(EnviroBitmapMaterial)
本篇包含内容:
- 3D基本元素 - 圆环
- 环境位图材质EnviroBitmapMaterial。
转载请注明出处:nooon.cnblogs.com
本例会用到的图片:
先看本例子最终效果:点击观看
从例子里我们看到,圆环反射环境里的金色光泽,但你细心观察可以发现不是真的实时反射。
再看完整代码(Flex):
1: package
2: {
3: import away3d.cameras.HoverCamera3D;
4: import away3d.containers.ObjectContainer3D;
5: import away3d.containers.View3D;
6: import away3d.core.math.Number3D;
7: import away3d.core.utils.Cast;
8: import away3d.materials.BitmapMaterial;
9: import away3d.materials.EnviroBitmapMaterial;
10: import away3d.primitives.Plane;
11: import away3d.primitives.Sphere;
12: import away3d.primitives.Torus;
13:
14: import flash.display.Sprite;
15: import flash.events.Event;
16: import flash.events.MouseEvent;
17:
18: [SWF(width="600", height="500", frameRate="60", backgroundColor="#FFFFFF")]
19: public class enviroBitmapMaterial extends Sprite
20: {
21: //定义 变量
22: private var view:View3D;
23: private var cam:HoverCamera3D;
24:
25: private var planeMaterial:BitmapMaterial;
26: private var torusMaterial:EnviroBitmapMaterial;
27: private var plane:Plane;
28: private var group:ObjectContainer3D;
29: private var torus:Torus;
30:
31: private var mouseDown:Boolean;
32: private var lastMouseX:Number;
33: private var lastMouseY:Number;
34: private var lastPanangle:Number;
35: private var lastTiltangle:Number;
36: private var cameraSpeed:Number;
37:
38: //嵌入图片,goldbackground.jpg用于背景及环境图;adobe_ico.jpg 是圆环的皮肤
39: [Embed(source="resources/goldbackground.jpg")] private var goldImage:Class;
40: [Embed(source="resources/nooon.jpg")] private var skinImage:Class;
41:
42: public function enviroBitmapMaterial()
43: {
44: //新建各贴图材质,备用。
45: createMaterial();
46: //inital 3D
47: init3D();
48: //create 3D Scene
49: createScene();
50: //addLsitener
51: addEventListener(Event.ENTER_FRAME,update);
52: stage.addEventListener(MouseEvent.MOUSE_DOWN,m_down_h);
53: stage.addEventListener(MouseEvent.MOUSE_UP,m_up_h);
54:
55: }
56: private function createMaterial():void
57: {
58: //背景的贴图材质为 BitmapMaterial
59: planeMaterial=new BitmapMaterial(Cast.bitmap(new goldImage()),{smooth:true, precision:5});
60: //圆环要反射环境,用EnviroBitmapMaterial
61: //EnviroBitmapMaterial构造函数EnviroColorMaterial(color:*, enviroMap:BitmapData, init:Object = null)
62: //第二个参数必须是BitmapData,用于设置反射环境图,本例与背景平面用同一张图goldBackground
63: torusMaterial=new EnviroBitmapMaterial(Cast.bitmap(new skinImage()),Cast.bitmap(new goldImage()));
64: //设置EnviroBitmapMaterial的反射系数,数值为0~1,自行设定数值大小看看效果。
65: torusMaterial.reflectiveness = 0.4;
66:
67: //在这里顺便设置下后面要用到的一个参数cameraSpeed,这个参数影响:拖动鼠标是整个场景旋转速度。
68: cameraSpeed=.3;
69: };
70: private function init3D():void
71: {
72: cam = new HoverCamera3D({focus:300});
73: cam.lookAt( new Number3D(0, 0, 0) );
74: cam.distance=900;
75: cam.panangle=cam.targetpanangle=30;
76: cam.tiltangle=cam.targettiltangle=15;
77: cam.mintiltangle=5;
78:
79: view = new View3D({camera:cam,x:300,y:250});
80: addChild(view);
81: };
82: private function createScene():void
83: {
84: group=new ObjectContainer3D();
85: //创建背景平面
86: plane=new Plane({material:planeMaterial,width:250,height:250,segmentsW:8,segmentsH:8});
87: //创建圆环
88: //radius:圆环总半径;tube:环的圆管半径
89: //所使用的材质为 torusMaterial,它是EnviroBitmapMaterial
90: torus= new Torus({material:torusMaterial,radius:30,tube:20,segmentsR:15,segmentsT:12});
91: torus.y=55;
92:
93: view.scene.addChild(group);
94:
95: group.addChild(plane);
96: group.addChild(torus);
97: };
98: private function update(e:Event):void
99: {
100: if(mouseDown)
101: {
102: cam.targetpanangle=cameraSpeed*(stage.mouseX-lastMouseX)+lastPanangle;
103: cam.targettiltangle=cameraSpeed*(stage.mouseY-lastMouseY)+lastTiltangle;
104: };
105: torus.yaw(-1);
106: torus.pitch(-1);
107: torus.roll(-1);
108: cam.hover();
109: view.render();
110:
111: };
112: private function m_down_h(e:MouseEvent):void
113: {
114: mouseDown=true;
115: lastMouseX=stage.mouseX;
116: lastMouseY=stage.mouseY;
117: lastPanangle=cam.targetpanangle;
118: lastTiltangle=cam.targettiltangle;
119: };
120: private function m_up_h(e:MouseEvent):void
121: {
122: mouseDown=false;
123: };
124:
125: }
126: }