| 1 |
package |
|---|
| 2 |
{ |
|---|
| 3 |
import flash.display.Bitmap; |
|---|
| 4 |
import flash.display.BitmapData; |
|---|
| 5 |
import flash.display.Sprite; |
|---|
| 6 |
import flash.display.StageAlign; |
|---|
| 7 |
import flash.display.StageScaleMode; |
|---|
| 8 |
import flash.display.TriangleCulling; |
|---|
| 9 |
import flash.events.Event; |
|---|
| 10 |
import flash.events.KeyboardEvent; |
|---|
| 11 |
import flash.geom.Matrix3D; |
|---|
| 12 |
import flash.geom.PerspectiveProjection; |
|---|
| 13 |
import flash.geom.Vector3D; |
|---|
| 14 |
import flash.text.TextField; |
|---|
| 15 |
import flash.text.TextFieldAutoSize; |
|---|
| 16 |
import net.unbland.debug.FPS; |
|---|
| 17 |
import primitives.Torus; |
|---|
| 18 |
|
|---|
| 19 |
[SWF(width=320, height=320, backgroundColor=0x000000, frameRate=30)] |
|---|
| 20 |
public class Document extends Sprite |
|---|
| 21 |
{ |
|---|
| 22 |
private var canvas:Sprite = new Sprite(); |
|---|
| 23 |
|
|---|
| 24 |
private var projection:PerspectiveProjection = new PerspectiveProjection(); |
|---|
| 25 |
private var scene:Sprite = new Sprite(); |
|---|
| 26 |
private var primitive:Torus = new Torus(20, 40); |
|---|
| 27 |
|
|---|
| 28 |
private var texture:BitmapData; |
|---|
| 29 |
|
|---|
| 30 |
[Embed(source = "images/texture.jpg")] |
|---|
| 31 |
private var image:Class; |
|---|
| 32 |
|
|---|
| 33 |
public function Document():void |
|---|
| 34 |
{ |
|---|
| 35 |
setupStage(); |
|---|
| 36 |
|
|---|
| 37 |
canvas.x = stage.stageWidth / 2; |
|---|
| 38 |
canvas.y = stage.stageHeight / 2; |
|---|
| 39 |
addChild(canvas); |
|---|
| 40 |
|
|---|
| 41 |
projection.fieldOfView = 60; |
|---|
| 42 |
|
|---|
| 43 |
scene.transform.matrix3D = new Matrix3D(); |
|---|
| 44 |
scene.addEventListener(Event.ENTER_FRAME, renderScene); |
|---|
| 45 |
|
|---|
| 46 |
texture = Bitmap(new image()).bitmapData; |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
private function renderScene(e:Event):void |
|---|
| 50 |
{ |
|---|
| 51 |
var world:Matrix3D = scene.transform.matrix3D.clone(); |
|---|
| 52 |
world.append(projection.toMatrix3D()); |
|---|
| 53 |
|
|---|
| 54 |
var vertices:Vector.<Number> = new Vector.<Number>(); |
|---|
| 55 |
var indices:Vector.<int> = new Vector.<int>(); |
|---|
| 56 |
|
|---|
| 57 |
var i:int; |
|---|
| 58 |
var l:int; |
|---|
| 59 |
|
|---|
| 60 |
var temp:Vector.<Object> = new Vector.<Object>(); |
|---|
| 61 |
|
|---|
| 62 |
for (i = 0, l = primitive.vertices.length; i < l; i++) |
|---|
| 63 |
{ |
|---|
| 64 |
var vertex:Vector3D = primitive.vertices[i]; |
|---|
| 65 |
var vector:Vector3D = world.transformVector(vertex); |
|---|
| 66 |
|
|---|
| 67 |
vector.w = projection.focalLength / (projection.focalLength + vector.z); |
|---|
| 68 |
vector.project(); |
|---|
| 69 |
|
|---|
| 70 |
vertices.push(vector.x, vector.y); |
|---|
| 71 |
temp.push({z:vector.z, i:i}); |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
var triangles:Array = []; |
|---|
| 75 |
|
|---|
| 76 |
for (i = 0, l = primitive.indices.length; i < l; i += 3) |
|---|
| 77 |
{ |
|---|
| 78 |
var v1:Object = temp[primitive.indices[i ]]; |
|---|
| 79 |
var v2:Object = temp[primitive.indices[i + 1]]; |
|---|
| 80 |
var v3:Object = temp[primitive.indices[i + 2]]; |
|---|
| 81 |
|
|---|
| 82 |
triangles.push({ |
|---|
| 83 |
v1:v1, |
|---|
| 84 |
v2:v2, |
|---|
| 85 |
v3:v3, |
|---|
| 86 |
z:(v1.z + v2.z + v3.z) / 3 |
|---|
| 87 |
}); |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
triangles.sortOn("z", Array.NUMERIC); |
|---|
| 91 |
|
|---|
| 92 |
for each (var triangle:Object in triangles) |
|---|
| 93 |
{ |
|---|
| 94 |
indices.push(triangle.v1.i, triangle.v2.i, triangle.v3.i); |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
canvas.graphics.clear(); |
|---|
| 98 |
canvas.graphics.beginBitmapFill(texture); |
|---|
| 99 |
canvas.graphics.drawTriangles( |
|---|
| 100 |
vertices, indices, primitive.uvtData, TriangleCulling.NEGATIVE |
|---|
| 101 |
); |
|---|
| 102 |
|
|---|
| 103 |
scene.rotationX += 2; |
|---|
| 104 |
scene.rotationY += 4; |
|---|
| 105 |
scene.rotationZ += 3; |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
private function setupStage():void |
|---|
| 109 |
{ |
|---|
| 110 |
FPS.show(stage); |
|---|
| 111 |
|
|---|
| 112 |
stage.showDefaultContextMenu = false; |
|---|
| 113 |
stage.scaleMode = StageScaleMode.NO_SCALE; |
|---|
| 114 |
stage.align = StageAlign.TOP_LEFT; |
|---|
| 115 |
|
|---|
| 116 |
var numPartitions:int = primitive.numPartitions; |
|---|
| 117 |
|
|---|
| 118 |
var textField:TextField = new TextField(); |
|---|
| 119 |
textField.background = true; |
|---|
| 120 |
textField.backgroundColor = 0x000000; |
|---|
| 121 |
textField.textColor = 0xFFFFFF; |
|---|
| 122 |
textField.autoSize = TextFieldAutoSize.LEFT; |
|---|
| 123 |
|
|---|
| 124 |
textField.text = "numPartitions : " + numPartitions + "\n" + |
|---|
| 125 |
"numTriangles : " + numPartitions * numPartitions; |
|---|
| 126 |
textField.x = stage.stageWidth - textField.width; |
|---|
| 127 |
textField.y = stage.stageHeight - textField.height; |
|---|
| 128 |
|
|---|
| 129 |
addChild(textField); |
|---|
| 130 |
|
|---|
| 131 |
stage.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void |
|---|
| 132 |
{ |
|---|
| 133 |
switch (e.keyCode) |
|---|
| 134 |
{ |
|---|
| 135 |
case 37: |
|---|
| 136 |
{ |
|---|
| 137 |
if (primitive.numPartitions <= 3) |
|---|
| 138 |
{ |
|---|
| 139 |
primitive.numPartitions = 3; |
|---|
| 140 |
} |
|---|
| 141 |
else |
|---|
| 142 |
{ |
|---|
| 143 |
primitive.numPartitions--; |
|---|
| 144 |
} |
|---|
| 145 |
break; |
|---|
| 146 |
} |
|---|
| 147 |
case 39: |
|---|
| 148 |
{ |
|---|
| 149 |
primitive.numPartitions++; |
|---|
| 150 |
break; |
|---|
| 151 |
} |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
var numPartitions:int = primitive.numPartitions; |
|---|
| 155 |
|
|---|
| 156 |
textField.text = "numPartitions : " + numPartitions + "\n" + |
|---|
| 157 |
"numTriangles : " + numPartitions * numPartitions; |
|---|
| 158 |
textField.x = stage.stageWidth - textField.width; |
|---|
| 159 |
textField.y = stage.stageHeight - textField.height; |
|---|
| 160 |
}); |
|---|
| 161 |
} |
|---|
| 162 |
} |
|---|
| 163 |
} |
|---|