This article includes:
- 3D basic element-ring
- Environment bitmap material envirobitmapmaterial.
Reprinted please indicate the source: nooon.cnblogs.com
Images used in this meeting:
First look at the final result of this example: Click to watch
From the examples, we can see that the golden luster in the ring reflection environment, but you can observe carefully to find that the real-time reflection is not true.
Let's look at the complete code (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: // define variables
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: // Insert the image. goldbackground.jpgis used in the background and environmental map. The adobe_ico.jpg is the skin of the ring.
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: // create Texture Materials for backup.
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: // The background texture is bitmapmaterial.
59: planeMaterial=new BitmapMaterial(Cast.bitmap(new goldImage()),{smooth:true, precision:5});
60: // use envirobitmapmaterial to reflect the environment of the ring
61: // envirobitmapmaterial constructor envirocolormaterial (color: *, enviromap: bitmapdata, init: Object = NULL)
62: // The second parameter must be bitmapdata, which is used to set the reflection environment graph. In this example, the same image goldbackground is used as the background plane.
63: torusMaterial=new EnviroBitmapMaterial(Cast.bitmap(new skinImage()),Cast.bitmap(new goldImage()));
64: // set the reflection coefficient of envirobitmapmaterial. The value ranges from 0 ~ 1. Set the value to see the effect.
65: torusMaterial.reflectiveness = 0.4;
66:
67: // set the cameraspeed parameter next to it here. This parameter affects the rotation speed of the entire scenario by dragging the mouse.
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: // create a background plane
86: plane=new Plane({material:planeMaterial,width:250,height:250,segmentsW:8,segmentsH:8});
87: // create a ring
88: // radius: Total ring radius; tube: circle radius of the ring
89: // the material used is torusmaterial, Which is 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: }