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

surface 3d

click : toggle wireframe
shift+click : toggle camera
doubleclick : toggle fullscreen
mousewheel : change height
http://ys.hu
Get Adobe Flash player
by ysle 22 May 2009
package
{
        //    click : toggle wireframe
        //    shift+click : toggle camera
        //    doubleclick : toggle fullscreen
        //    mousewheel : change height
        
        //    http://ys.hu
    
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix3D;
	import flash.geom.PerspectiveProjection;
	import flash.geom.Point;
	import flash.geom.Utils3D;
	import flash.geom.Vector3D;
	import flash.media.Camera;
	import flash.media.Video;

	[SWF(backgroundColor="0x000000", frameRate="30")]
	public class Grid extends Sprite
	{
		private var camera:Camera
		private var video:Video = new Video(320,240)
		
		private var matrix3D:Matrix3D                   = new Matrix3D()
		private var vertices:Vector.<Number>            = new Vector.<Number>()
		private var projectedVertices:Vector.<Number>	= new Vector.<Number>()
		private var uvtData:Vector.<Number>		= new Vector.<Number>()
		private var indices:Vector.<int>		= new Vector.<int>()

		private var grid:Sprite = new Sprite()

		private var cols:Number = 33
		private var rows:Number = 25
		private var padding:Number = 10

		private var bmd:BitmapData = new BitmapData(320, 240, false, 0)
		private var wireframe:Boolean = false
		
		private var interactive:Boolean = false
		
		private var seed:Number
		private var scroll:Number = 0
		private var max:Number = 80
		
		private var projection:Sprite = new Sprite()

		public function Grid() 
		{
			seed = int(Math.random()*100)

			var f:Number

			for (var r:Number = 0; r < rows; r++ )
			{
				for (var c:Number = 0; c < cols; c++ )
				{
					vertices.push((bmd.width / -2) + c * padding, (bmd.height / -2) + r * padding, 0)
					uvtData.push((c / (cols - 1)), (r / (rows - 1)), 0)
				}
			}

			for (r = 0; r < rows - 1; r++ ) for (c = 0; c < cols - 1; c++ )
			{
				indices.push((f = r * cols) + c, f + c + 1, f + c + cols)
				indices.push(f + c + 1, f + c + 1+ cols, f + c + cols)
			}

			with (addChild(projection = new Sprite()))
			{
				transform.perspectiveProjection = new PerspectiveProjection()
				transform.perspectiveProjection.fieldOfView = 0.000001
				visible = false
				with (addChild(new Bitmap(bmd)))
				{
					x = -160
					y = -120
					z = -40
				}
			}

			addChild(grid)

			stage.align = 'TL'
			stage.scaleMode = 'noScale'
			stage.mouseChildren = false
			stage.doubleClickEnabled = true
			stage.addEventListener('mouseDown',		function (e:MouseEvent):void
			{
				if(e.shiftKey) video.attachCamera(camera = camera?null:Camera.getCamera())
				else projection.visible = wireframe = !wireframe; onStageResize()
			})
			stage.addEventListener('doubleClick',	function (...e):void { try { stage.displayState = (stage.displayState == 'normal')?'fullScreen':'normal' } catch (e:*) { } } )
			stage.addEventListener('mouseMove',		function (...e):void { interactive = true })
			stage.addEventListener('mouseLeave',	function (...e):void { interactive = false })
			stage.addEventListener('mouseWheel',	function (e:MouseEvent):void { max = Math.min(200, Math.max(0, max += e.delta)) })
			stage.addEventListener('enterFrame',	onEnterFrame)
			stage.addEventListener('resize',		onStageResize)
			onStageResize()
		}

		private function onEnterFrame(...e):void 
		{
			if (camera) bmd.draw(video) else bmd.perlinNoise(100, 100, 1, seed, false, false, 4, false, [new Point(scroll++, scroll)])

			var i:Number = 0
			for (var r:Number = 0; r < rows; r++ ) for (var c:Number = 0; c < cols; c++ ) if (++i) vertices[i * 3 - 1] = (bmd.getPixel(Math.min(c * padding, 319), Math.min(r * padding, 239)) / (camera?0xffffff:0x0000ff)) * max

			var ry:Number = 60 - (interactive? ( 1 - stage.mouseX / stage.stageWidth) : 0.5) * 120
			var rx:Number = -25 + (interactive? ( 1 - stage.mouseY / stage.stageHeight) : 0.25) * 100

			matrix3D = new Matrix3D()
			matrix3D.prependRotation(projection.rotationY = ry, Vector3D.Y_AXIS)
			matrix3D.prependRotation(projection.rotationX = rx, Vector3D.X_AXIS)
			
			Utils3D.projectVectors(matrix3D, vertices, projectedVertices, uvtData)

			grid.graphics.clear()  
			if (!wireframe) grid.graphics.beginBitmapFill(bmd)
			grid.graphics.lineStyle(1, wireframe?0xffffff:0x0000ff, wireframe?.5:.2)

			grid.graphics.drawTriangles(projectedVertices, indices , uvtData, 'negative')
			grid.graphics.endFill()
		}
		
		private function onStageResize(...e):void
 		{
			grid.x = projection.x = stage.stageWidth / 2
			grid.y = projection.y = stage.stageHeight / 2
		}

	}
	
}