In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

ろくろで何作る?

作ってからも回しちゃおう
Get Adobe Flash player
by Kay 27 Feb 2009

    Tags

    3d
    Embed
/*
 * 作ってからも回しちゃおう
 */
package {
	import flash.display.*;
	import flash.geom.*;
	import flash.events.Event;
	import flash.net.*; 

	[SWF(width=400, height=400, backgroundColor=0x666633)]
	public class PpottersWheel extends Sprite {
		public  var _obj:TurnObject = new TurnObject();
		public  var matrix      :Matrix3D = new Matrix3D();
		public  var bitmap      :Bitmap;
		public  var bitmapData  :BitmapData;
		private var nSensitivity:Number = 0.1;

		public function PpottersWheel():void {
			// 色
			_obj.color = 0x333333;
			// 分割数
			_obj.split = 16;
			// 断面図のx,y座標
			_obj.contours.push(
				  0,   0,
				 70,  25,
				 90, 100,
				120,  50,
				 90,   0,
				 30, -50,
				 90,-100,
				  0,-100
				  );
			// 表示位置
			_obj.x = stage.stageWidth/2;
			_obj.y = stage.stageHeight/2;
			_obj.z = 0;
			_obj.light = new Vector3D(1,1,1);
			_obj.bitmapData = createBitmap();
			_obj.render(matrix);
			addChild(_obj);
			_obj.addEventListener(Event.ENTER_FRAME, xRotate);
		}
        public function createBitmap():BitmapData {
			var size:Number = 400;
			var bitmapData:BitmapData = new BitmapData(size,size,false,0xffffff);
			var _sprite:Sprite = new Sprite();
            var matrix:Matrix = new Matrix();
            matrix.createGradientBox(size, size);
            _sprite.graphics.beginGradientFill(GradientType.RADIAL,
                                       [_obj.color, 0x886666, _obj.color, 0x669966, _obj.color],
                                       [1, 1, 1, 1, 1],
                                       [100, 140, 200, 220, 240],
                                       matrix,
                                       SpreadMethod.PAD);
            _sprite.graphics.drawRect(0, 0, size, size);
			bitmapData.draw(_sprite);
			return bitmapData;
        }
		public function xRotate(eventObject:Event):void{
			var nRotationX:Number = (mouseY-stage.stageWidth/2) * nSensitivity;
			var nRotationY:Number = -(mouseX-stage.stageHeight/2) * nSensitivity;
			matrix.appendRotation(nRotationX, Vector3D.X_AXIS);
			matrix.appendRotation(nRotationY, Vector3D.Y_AXIS);
			_obj.render(matrix);
		}
	}
}

import flash.display.*;
import flash.geom.*;
import flash.filters.ColorMatrixFilter;
import flash.events.Event;

class TurnObject extends Sprite {
	public  var color      :Number;
	public  var split      :uint;
	public  var splitRadian:Number;
	public  var light      :Vector3D;
	public  var contours   :Vector.<Number> = new Vector.<Number>();
	public  var apexes     :Vector.<Number> = new Vector.<Number>();
	private var flatApexes :Vector.<Number> = new Vector.<Number>();
	private var uvtData    :Vector.<Number> = new Vector.<Number>();
	private var indices    :Vector.<int>    = new Vector.<int>();
	public  var bitmapData :BitmapData;

	public function TurnObject():void {
	}
	public function setApexes():void {
		var nContours:uint = Math.floor(contours.length/2);
		for (var v:Number = 0; v < nContours; v++) {
			var nDist:Number = contours[v*2];
			var nY:Number    = contours[v*2+1];
			for (var h:Number = 0; h <= split; h++) {
				var nRad:Number = Math.PI * 2 / split * h;
				var nX:Number = nDist * Math.sin(nRad);
				var nZ:Number = nDist * Math.cos(nRad);
				apexes.push(nX,nY,nZ);
				uvtData.push(h/split,v/nContours,1);
				indices.push(v*(split+1) + h);
			}
		}
	}
	public function render(matrix:Matrix3D):void {
		if (apexes.length == 0) {
			setApexes();
		}
		var i:int;
		var faces_array:Array = new Array();
		var nC:int = numChildren;
		for (i=0; i < nC; ++i) {
			removeChildAt(0);
		}
		Utils3D.projectVectors(matrix,apexes,flatApexes,uvtData);

		var tempIndices:Vector.<int> = new Vector.<int>;
		tempIndices.push(0,1,2,3,2,1);
		var cLen:Number = contours.length/2;
		for (var v:uint=0; v < cLen-1; v++) {
			for (var s:uint=0; s < split; s++) {
				if ((v+1)*(split+1)+s+1 <= indices.length) {
					var tempFlatApexes:Vector.<Number> = new Vector.<Number>();
					var tempUvtData   :Vector.<Number> = new Vector.<Number>();
					var rI            :Vector.<int>    = new Vector.<int>();
					rI.push(indices[ v   *(split+1)+s+0]);
					rI.push(indices[ v   *(split+1)+s+1]);
					rI.push(indices[(v+1)*(split+1)+s+0]);
					rI.push(indices[(v+1)*(split+1)+s+1]);
					tempFlatApexes.push(
						flatApexes[rI[0]*2], flatApexes[rI[0]*2+1],
						flatApexes[rI[1]*2], flatApexes[rI[1]*2+1],
						flatApexes[rI[2]*2], flatApexes[rI[2]*2+1],
						flatApexes[rI[3]*2], flatApexes[rI[3]*2+1]
						);
					tempUvtData.push(
						uvtData[rI[0]*3], uvtData[rI[0]*3+1], uvtData[rI[0]*3+2],
						uvtData[rI[1]*3], uvtData[rI[1]*3+1], uvtData[rI[1]*3+2],
						uvtData[rI[2]*3], uvtData[rI[2]*3+1], uvtData[rI[2]*3+2],
						uvtData[rI[3]*3], uvtData[rI[3]*3+1], uvtData[rI[3]*3+2]
						);
					var p0:Vector3D = Utils3D.projectVector(matrix,new Vector3D(apexes[rI[0]*3], apexes[rI[0]*3+1], apexes[rI[0]*3+2]));
					var p1:Vector3D = Utils3D.projectVector(matrix,new Vector3D(apexes[rI[1]*3], apexes[rI[1]*3+1], apexes[rI[1]*3+2]));
					var p2:Vector3D = Utils3D.projectVector(matrix,new Vector3D(apexes[rI[2]*3], apexes[rI[2]*3+1], apexes[rI[2]*3+2]));
					var p3:Vector3D = Utils3D.projectVector(matrix,new Vector3D(apexes[rI[3]*3], apexes[rI[3]*3+1], apexes[rI[3]*3+2]));
					var _face:Sprite = new Sprite();
					_face.graphics.clear();
					if (bitmapData) {
						_face.graphics.beginBitmapFill(bitmapData);
						_face.graphics.drawTriangles(tempFlatApexes,tempIndices,tempUvtData,TriangleCulling.NEGATIVE);
						_face.graphics.endFill();
					}
					var vA:Vector3D = p0.clone();
					var vB:Vector3D = p0.clone();
					vA.incrementBy(p1);
					vB.incrementBy(p2);
					var vC:Vector3D = vA.crossProduct(vB);
					vC.normalize();
					var nW:Number = vC.dotProduct(light);
					var nBrigtness:Number = nW * 32;
					var filter:ColorMatrixFilter = new ColorMatrixFilter([
						1,0,0,0,nBrigtness,
						0,1,0,0,nBrigtness,
						0,0,1,0,nBrigtness,
						0,0,0,1,0]);
					_face.filters = [filter];
					var vAvr:Vector3D = p1.add(p2);
					var nZ:Number = Vector3D.distance(new Vector3D(0,0,1000),vAvr);
					faces_array.push({face:_face, z:nZ});
				}
			}
		}
		faces_array.sortOn("z",Array.NUMERIC);
		var nLength:int = faces_array.length;
		for (i=0; i < nLength; ++i) {
			addChild(faces_array[i].face);
		}
	}
}