/*
* drawTrianglesを使ったBallクラス
*/
package {
import flash.display.*;
import flash.geom.Matrix3D;
import flash.geom.Vector3D;
import flash.events.Event;
[SWF(width=400, height=400, backgroundColor=0x000000, frameRate=24)]
public class BallsMouseTest extends Sprite {
private var matrix:Matrix3D = new Matrix3D();
public var ballA:Ball;
public var ballB:Ball;
public function BallsMouseTest():void {
ballA = new Ball();
ballA.radius = 100;
ballA.facesV = 2;
ballA.facesH = 3;
ballA.faceColor = 0x0000ff;
ballA.lineColor = 0xffff00;
ballA.x = 125;
ballA.y = 125;
ballA.z = 0;
ballA.render();
addChild(ballA);
ballB = new Ball();
ballB.radius = 100;
ballB.facesV = 7;
ballB.facesH = 18;
ballB.faceColor = 0xff0000;
ballB.lineColor = 0xcccccc;
ballB.x = 275;
ballB.y = 275;
ballB.z = 0;
ballB.render();
addChild(ballB);
addEventListener(Event.ENTER_FRAME, rotate);
}
public function rotate(eventObject:Event):void {
ballA._matrix.prependRotation(2, Vector3D.Y_AXIS);
ballA.render();
ballB._matrix.prependRotation(2, Vector3D.X_AXIS);
ballB.render();
}
}
}
import flash.display.Sprite;
import flash.display.TriangleCulling;
import flash.events.MouseEvent;
import flash.geom.Vector3D;
import flash.geom.Matrix3D;
import flash.geom.Utils3D;
class Ball extends Sprite {
public var radius:Number;
public var faceColor:Number;
public var lineColor:Number;
public var facesV:Number;
public var facesH:Number;
public var pZ:Number;
public var _matrix:Matrix3D = new Matrix3D();
private var apexNum:Number;
private var apexes:Vector.<Number> = new Vector.<Number>();
private var indices:Vector.<int>;
public function Ball():void {
addEventListener(MouseEvent.MOUSE_OVER, onMouse);
addEventListener(MouseEvent.MOUSE_OUT, outMouse);
}
public function setApexes():void {
var faceVr:Number = -Math.PI/facesV;
var faceHr:Number = -Math.PI*2/facesH;
for (var v:uint = 0; v <= facesV; v++) {
var nVr:Number = faceVr*v;
var nD :Number = radius * Math.sin(nVr);
var nY :Number = radius * Math.cos(nVr);
for (var h:uint = 0; h <= facesH; h++) {
var nHr:Number = faceHr*h;
var nX :Number = nD * Math.sin(nHr);
var nZ :Number = nD * Math.cos(nHr);
apexes.push(nX,nY,nZ);
}
}
indices = new Vector.<int>();
indices.push(0,1,2,3,2,1);
}
public function render():void {
if (!apexes.length) setApexes();
var vout :Vector.<Number> = new Vector.<Number>();
var vrend:Vector.<Number> = new Vector.<Number>();
var uvt :Vector.<Number> = new Vector.<Number>();
var index:uint;
_matrix.transformVectors(apexes, vout);
graphics.clear();
for (var v:int = 0; v < facesV; v++) {
for (var h:uint = 0; h < facesH; h++) {
index = (v*(facesH+1)+h)*3;
var myApexes:Vector.<Number> = new Vector.<Number>();
myApexes.push(vout[index+0],vout[index+1],vout[index+2]);
myApexes.push(vout[index+3],vout[index+4],vout[index+5]);
index += (facesH+1)*3;
myApexes.push(vout[index+0],vout[index+1],vout[index+2]);
myApexes.push(vout[index+3],vout[index+4],vout[index+5]);
Utils3D.projectVectors(_matrix,myApexes,vrend,uvt);
graphics.beginFill(faceColor);
graphics.lineStyle(1,lineColor);
graphics.drawTriangles(vrend,indices,null,TriangleCulling.POSITIVE);
graphics.endFill();
}
}
}
private function onMouse(eventObject:MouseEvent):void {
alpha = 0.5;
}
private function outMouse(eventObject:MouseEvent):void {
alpha = 1;
}
}