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

forked from: forked from: s3d

...
@author lizhi http://game-develop.net/
Get Adobe Flash player
by lizhi 09 Jan 2013
/**
 * Copyright lizhi ( http://wonderfl.net/user/lizhi )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/qaRw
 */

package  
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.StageQuality;
	import flash.events.Event;
	import flash.geom.Vector3D;
	import flash.geom.Point;
	/**
	 * ...
	 * @author lizhi http://game-develop.net/
	 */
	public class Test3D3 extends Sprite
	{
		private var obj3d:Obj3D;
		private var offset:Array = [new Point,new Point];
		private var map:BitmapData;
		public function Test3D3() 
		{
			map = new BitmapData(400, 400);
			obj3d = createSphere(120, 46, 30);
			obj3d.position.z = 150;
			stage.quality = StageQuality.MEDIUM;
			addEventListener(Event.ENTER_FRAME, enterFrame);

		}
		
		private function enterFrame(e:Event):void 
		{
			offset[0].x += 7//(mouseX - stage.stageWidth / 2) / stage.stageWidth * 10;
			offset[1].y += 7//(mouseY - stage.stageHeight / 2) / stage.stageHeight * 10;
			map.perlinNoise(160, 160, 2, 1, true, true, 7, true, offset);
			graphics.clear();
			obj3d.rotation.x += .01;
			obj3d.rotation.y += .02;
			obj3d.rotation.z += .03;
			graphics.lineStyle(0);
			for (var i:int = obj3d.vs.length - 1; i >= 0; i-- ) {
				var v:Vector3D = obj3d.vs[i];
				var v2:Vector3D = obj3d.vs2[i];
				var uv:Vector3D = obj3d.uvs[i];
				var p:Number = 2 / 3 + (map.getPixel((map.width - 1) * uv.x, (map.height - 1) * uv.y) & 0xff) / 0xff / 3;
				//p = 1;
				var y:Number = v.y * Math.cos(obj3d.rotation.x)*p - v.z * Math.sin(obj3d.rotation.x)*p;
				var z:Number = v.z * Math.cos(obj3d.rotation.x)*p + v.y * Math.sin(obj3d.rotation.x)*p;
				var x:Number = v.x * Math.cos(obj3d.rotation.y)*p - z * Math.sin(obj3d.rotation.y);
				z = z * Math.cos(obj3d.rotation.y) + v.x * Math.sin(obj3d.rotation.y)*p;
				var x_:Number = x * Math.cos(obj3d.rotation.z) - y * Math.sin(obj3d.rotation.z);  
				y = y * Math.cos(obj3d.rotation.z) + x * Math.sin(obj3d.rotation.z);
				z += obj3d.position.z;
				var fz:Number = 100 / z;
				v2.x = x_ * fz+200;
				v2.y = y * fz + 200;
				v2.z = z;
				//graphics.drawCircle(v2.x, v2.y, 3*fz);
			}
			/*for each(var line:Point in obj3d.lines) {
				graphics.moveTo(obj3d.vs2[line.x].x,obj3d.vs2[line.x].y);
				graphics.lineTo(obj3d.vs2[line.y].x,obj3d.vs2[line.y].y);
			}*/
			for each(var cline:Vector3D in obj3d.clines) {
				graphics.moveTo(obj3d.vs2[cline.x].x,obj3d.vs2[cline.x].y);
				graphics.curveTo(obj3d.vs2[cline.y].x,obj3d.vs2[cline.y].y,obj3d.vs2[cline.z].x,obj3d.vs2[cline.z].y);
			}
		}
		
		public function createSphere(r:Number, nv:int = 20, nh:int = 30):Obj3D {
			var obj3d:Obj3D = new Obj3D;
			obj3d.vs.push(new Vector3D(0, -r));
			obj3d.uvs.push(new Vector3D(.5,0));
			obj3d.vs2.push(new Vector3D);
			for (var i:int = 1; i <= nv;i++ ) {
				var az:Number = (i / (nv + 1) - .5) * Math.PI;
				var uvv:Number = i / (nv + 1);
				for (var j:int = 0; j < nh; j++ ) {
					obj3d.uvs.push(new Vector3D(j/nh,uvv));
					var v:Vector3D = new Vector3D(r);
					var x:Number = v.x * Math.cos(az);  
					v.y = v.x * Math.sin(az);
					var ay:Number = j / nh * 2 * Math.PI;
					v.x = x * Math.cos(ay);
					v.z = x * Math.sin(ay);
					obj3d.vs.push(v);
					obj3d.vs2.push(new Vector3D);
					var a:int = (i - 1) * nh + j+1;
					var b:int = j == (nh-1)?a - nh + 1:a + 1;
					var c:int = i == nv?nv * nh + 1:a + nh;
					var d:int = j == (nh-1)?c - nh + 1:c + 1;
					obj3d.lines.push(new Point(a, b),new Point(a,c));
					obj3d.ts.push(new Vector3D(a, b, c), new Vector3D(b, c, d));
				}
			}
			obj3d.vs.push(new Vector3D(0, r));
			obj3d.vs2.push(new Vector3D);
			obj3d.uvs.push(new Vector3D(.5,1));
			for (j = 0; j < nh; j++ ) {
				obj3d.lines.push(new Point(0, j + 1));
			}
			
			for (i = 1; i <= nv; i++ ) {
				uvv = i / (nv + 1);
				for (j = 0; j < nh; j++ ) {
					a = (i - 1) * nh + j + 1;
					b = j == (nh - 1)?a - nh + 1:a + 1;
					var av:Vector3D = obj3d.vs[a];
					var bv:Vector3D = obj3d.vs[b];
					var abv:Vector3D = new Vector3D((av.x + bv.x) / 2, (av.y + bv.y) / 2, (av.z + bv.z) / 2);
					obj3d.uvs.push(new Vector3D((j+.5)/nh,uvv));
					obj3d.vs.push(abv);
					obj3d.vs2.push(new Vector3D);
					obj3d.clines.push(new Vector3D(j == 0?obj3d.vs.length - 3 + nh*2:obj3d.vs.length - 3, a, obj3d.vs.length - 1));
					
					c = i == nv?nv * nh + 1:a + nh;
					var cv:Vector3D = obj3d.vs[c];
					var acv:Vector3D = new Vector3D((av.x + cv.x) / 2, (av.y + cv.y) / 2, (av.z + cv.z) / 2);
					obj3d.uvs.push(new Vector3D(j / nh, uvv + .5 / (nv + 1)));
					obj3d.vs.push(acv);
					obj3d.vs2.push(new Vector3D);
					obj3d.clines.push(new Vector3D(i==1?0:obj3d.vs.length - 1-nh*2, a, obj3d.vs.length - 1));
				}
			}
			
			for (j = 0; j < nh; j++ ) {
				obj3d.clines.push(new Vector3D(nv*nh+1,nv*nh+1,obj3d.vs.length-1-j*2));
			}
			
			return obj3d;
		}
	}
}
import flash.geom.*;
class Obj3D {
	public var rotation:Vector3D = new Vector3D;//角度
	public var position:Vector3D = new Vector3D;//位置
	public var vs:Vector.<Vector3D> = new Vector.<Vector3D>;//顶点
	public var uvs:Vector.<Vector3D> = new Vector.<Vector3D>;//uv
	public var vs2:Vector.<Vector3D> = new Vector.<Vector3D>;//屏幕坐标顶点
	public var lines:Vector.<Point> = new Vector.<Point>;//直线
	public var clines:Vector.<Vector3D> = new Vector.<Vector3D>;//曲线
	public var ts:Vector.<Vector3D> = new Vector.<Vector3D>;//三角形
}