root/as3/Flash3D/Demo2.as

リビジョン 299, 7.2 kB (コミッタ: yossy, コミット時期: 5 ヶ月 前)

Imported Lab code.

Line 
1 /*
2  * Copyright(c) 2006 the Spark project.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific language
14  * governing permissions and limitations under the License.
15  */
16
17 package
18 {
19     import org.libspark.geom.Matrix4;
20     import org.libspark.geom.Vector3D;
21     import org.libspark.geom.Vector4D;
22     import org.libspark.thunder.*;
23     import flash.display.Sprite;
24     import flash.display.BitmapData;
25     import flash.display.Bitmap;
26     import flash.display.Loader;
27     import flash.net.URLRequest;
28     import flash.text.TextField;
29     import flash.events.Event;
30     import flash.utils.getTimer;
31    
32     public class Demo2 extends Sprite
33     {
34         private var render:Render2;
35         private var worldMatrix:Matrix4;
36         private var vertices:Array;
37         private var indices:Array;
38         private var len:uint;
39         private var fps:uint;
40         private var fpsField:TextField;
41         private var rx:Number;
42         private var ry:Number;
43         private var rz:Number;
44         private var textureSource:Loader;
45         private var texture:BitmapData;
46        
47         public function Demo2()
48         {
49             stage.scaleMode = 'noScale';
50            
51             var target:BitmapData = new BitmapData(320, 240, false);
52             addChild(new Bitmap(target));
53            
54             texture = new BitmapData(128, 128, false);
55            
56             render = new Render2();
57             render.setRenderTarget(target);
58             render.setTexture(texture);
59            
60             worldMatrix = new Matrix4();
61             render.setWorldMatrix(worldMatrix);
62            
63             var view:Matrix4 = new Matrix4();
64             var vCam:Vector3D = new Vector3D(0, 0, 192);
65             var vCamEye:Vector3D = new Vector3D(0, 0, 0);
66             var vCamTop:Vector3D = new Vector3D(0, 1, 0);
67             var camDiv:Number = 0.1;
68             MatrixUtil.lookAt(view, vCam, vCamEye, vCamTop);
69             render.setViewMatrix(view);
70            
71             var projection:Matrix4 = new Matrix4();
72             MatrixUtil.perspective(projection, 60 * Math.PI / 180, 3 / 4, 0.01, 100);
73             render.setProjectionMatrix(projection);
74            
75             var d:Vector3D = new Vector3D(0, -5, -5);
76             d.normalize();
77             render.setLight(new Light(d, new ColorValue(0.3, 0.3, 0.3), new ColorValue(0.7, 0.7, 0.7)));
78             render.setViewport(new Viewport(0, 0, 320, 240));
79            
80             /**
81             vertices = new Array(8);
82             vertices[0] = new Vertex(-64, 64, 0, 0, 0);
83             vertices[1] = new Vertex(64, 64, 0, 1, 0);
84             vertices[2] = new Vertex(-64, -64, 0, 0, 1);
85             vertices[3] = new Vertex(64, -64, 0, 1, 1);
86             vertices[4] = new Vertex(-64, 0, -64, 0, 0);
87             vertices[5] = new Vertex(64, 0, -64, 1, 0);
88             vertices[6] = new Vertex(-64, 0, 64, 0, 1);
89             vertices[7] = new Vertex(64, 0, 64, 1, 1);
90             indices = new Array(12);
91             indices[0] = 0; indices[1] = 2; indices[2] = 1;
92             indices[3] = 1; indices[4] = 2; indices[5] = 3;
93             indices[6] = 4; indices[7] = 6; indices[8] = 5;
94             indices[9] = 5; indices[10] = 6; indices[11] = 7;
95             len = 4;
96             /**/
97             vertices = new Array(24);
98             vertices[0] = new Vertex(-64, 64, -64, 0, 0);
99             vertices[1] = new Vertex(64, 64, -64, 1, 0);
100             vertices[2] = new Vertex(-64, 64, 64, 0, 1);
101             vertices[3] = new Vertex(64, 64, 64, 1, 1);
102             vertices[4] = new Vertex(-64, -64, 64, 0, 0);
103             vertices[5] = new Vertex(64, -64, 64, 1, 0);
104             vertices[6] = new Vertex(-64, -64, -64, 0, 1);
105             vertices[7] = new Vertex(64, -64, -64, 1, 1);
106             vertices[8] = new Vertex(-64, 64, 64, 0, 0);
107             vertices[9] = new Vertex(64, 64, 64, 1, 0);
108             vertices[10] = new Vertex(-64, -64, 64, 0, 1);
109             vertices[11] = new Vertex(64, -64, 64, 1, 1);
110             vertices[12] = new Vertex(64, 64, 64, 0, 0);
111             vertices[13] = new Vertex(64, 64, -64, 1, 0);
112             vertices[14] = new Vertex(64, -64, 64, 0, 1);
113             vertices[15] = new Vertex(64, -64, -64, 1, 1);
114             vertices[16] = new Vertex(64, 64, -64, 0, 0);
115             vertices[17] = new Vertex(-64, 64, -64, 1, 0);
116             vertices[18] = new Vertex(64, -64, -64, 0, 1);
117             vertices[19] = new Vertex(-64, -64, -64, 1, 1);
118             vertices[20] = new Vertex(-64, 64, -64, 0, 0);
119             vertices[21] = new Vertex(-64, 64, 64, 1, 0);
120             vertices[22] = new Vertex(-64, -64, -64, 0, 1);
121             vertices[23] = new Vertex(-64, -64, 64, 1, 1);
122             indices = new Array(36);
123             for (var i:uint = 0; i < 6; ++i) {
124                 indices[i * 6] = i * 4;
125                 indices[i * 6 + 1] = i * 4 + 2;
126                 indices[i * 6 + 2] = i * 4 + 1;
127                 indices[i * 6 + 3] = i * 4 + 1;
128                 indices[i * 6 + 4] = i * 4 + 2;
129                 indices[i * 6 + 5] = i * 4 + 3;
130             }
131             len = 12;
132             /**/
133            
134             fpsField = new TextField();
135             fpsField.x = 240;
136             fpsField.y = 0;
137             addChild(fpsField);
138            
139             fps = getTimer();
140            
141             rx = ry = rz = 0;
142            
143             textureSource = new Loader();
144             textureSource.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
145             textureSource.load(new URLRequest('texture.swf'));
146         }
147        
148         private function completeHandler(event:Event):void
149         {
150             textureSource.removeEventListener(Event.COMPLETE, completeHandler);
151             textureSource.content.scaleX = 128 / textureSource.contentLoaderInfo.width;
152             textureSource.content.scaleY = 128 / textureSource.contentLoaderInfo.height;
153            
154             /**
155             enterFrameHandler(null);
156             /**/
157             addEventListener(Event.ENTER_FRAME, enterFrameHandler);
158             /**/
159         }
160        
161         private function enterFrameHandler(event:Event):void
162         {
163             var m:Matrix4 = worldMatrix;
164             var r:Render2 = render;
165            
166             m.identity();
167             m.rotateX(rx += 0.01);
168             m.rotateY(ry += 0.02);
169             m.rotateZ(rz += 0.05);
170            
171             texture.draw(textureSource);
172            
173             r.updateMatrix();
174             r.clear(0xffffff);
175             r.beginScene();
176             r.drawIndexedPrimitive(indices, vertices, len);
177             r.endScene();
178             r.present();
179            
180             fpsField.text = Math.floor(1000 / (getTimer() - fps)) + 'fps';
181             fps = getTimer();
182         }
183     }
184 }
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。