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

グラデーションメッシュの出来損ないの事

Pixel Benderは
http://www.petrileskinen.fi/Actionscript/ThreePointGradient/ThreePointGradient.html
のソースそのままです


http://unbland.org/blog/2008/05/20/24.html
の記事を参考にさせていただきました
*
Get Adobe Flash player
by zahir 01 Feb 2009
    Embed
/*
Pixel Benderは
http://www.petrileskinen.fi/Actionscript/ThreePointGradient/ThreePointGradient.html
のソースそのままです


http://unbland.org/blog/2008/05/20/24.html
の記事を参考にさせていただきました
 * */
package {
	import __AS3__.vec.Vector;
	
	import flash.display.*;
	import flash.events.Event;
	import flash.geom.*;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.*;

	public class GradientMesh extends Sprite{
		private static const _data:Vector.<int> = Vector.<int>([165, 1, 0, 0, 0, 164, 18, 0, 84, 104, 114, 101, 101, 80, 111, 105, 110, 116, 71, 114, 97, 100, 105, 101, 110, 116, 160, 12, 110, 97, 109, 101, 115, 112, 97, 99, 101, 0, 80, 101, 116, 114, 105, 32, 76, 101, 115, 107, 105, 110, 101, 110, 58, 58, 69, 120, 97, 109, 112, 108, 101, 0, 160, 12, 118, 101, 110, 100, 111, 114, 0, 80, 101, 116, 114, 105, 32, 76, 101, 115, 107, 105, 110, 101, 110, 0, 160, 8, 118, 101, 114, 115, 105, 111, 110, 0, 1, 0, 160, 12, 100, 101, 115, 99, 114, 105, 112, 116, 105, 111, 110, 0, 67, 114, 101, 97, 116, 101, 115, 32, 97, 32, 103, 114, 97, 100, 105, 101, 110, 116, 32, 102, 105, 108, 108, 32, 117, 115, 105, 110, 103, 32, 116, 104, 114, 101, 101, 32, 115, 112, 101, 99, 105, 102, 105, 101, 100, 32, 112, 111, 105, 110, 116, 115, 32, 97, 110, 100, 32, 99, 111, 108, 111, 114, 115, 46, 0, 161, 1, 2, 0, 0, 12, 95, 79, 117, 116, 67, 111, 111, 114, 100, 0, 161, 1, 2, 0, 0, 3, 112, 111, 105, 110, 116, 49, 0, 162, 2, 109, 105, 110, 86, 97, 108, 117, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 162, 2, 109, 97, 120, 86, 97, 108, 117, 101, 0, 69, 122, 0, 0, 69, 122, 0, 0, 161, 1, 4, 1, 0, 15, 99, 111, 108, 111, 114, 49, 0, 162, 4, 100, 101, 102, 97, 117, 108, 116, 86, 97, 108, 117, 101, 0, 63, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 63, 128, 0, 0, 161, 1, 2, 2, 0, 12, 112, 111, 105, 110, 116, 50, 0, 162, 2, 109, 105, 110, 86, 97, 108, 117, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 162, 2, 109, 97, 120, 86, 97, 108, 117, 101, 0, 69, 122, 0, 0, 69, 122, 0, 0, 162, 2, 100, 101, 102, 97, 117, 108, 116, 86, 97, 108, 117, 101, 0, 67, 250, 0, 0, 67, 250, 0, 0, 161, 1, 4, 3, 0, 15, 99, 111, 108, 111, 114, 50, 0, 162, 4, 100, 101, 102, 97, 117, 108, 116, 86, 97, 108, 117, 101, 0, 0, 0, 0, 0, 63, 128, 0, 0, 0, 0, 0, 0, 63, 128, 0, 0, 161, 1, 2, 2, 0, 3, 112, 111, 105, 110, 116, 51, 0, 162, 2, 109, 105, 110, 86, 97, 108, 117, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 162, 2, 109, 97, 120, 86, 97, 108, 117, 101, 0, 69, 122, 0, 0, 69, 122, 0, 0, 162, 2, 100, 101, 102, 97, 117, 108, 116, 86, 97, 108, 117, 101, 0, 0, 0, 0, 0, 67, 250, 0, 0, 161, 1, 4, 4, 0, 15, 99, 111, 108, 111, 114, 51, 0, 162, 4, 100, 101, 102, 97, 117, 108, 116, 86, 97, 108, 117, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 63, 128, 0, 0, 63, 128, 0, 0, 161, 2, 4, 5, 0, 15, 100, 115, 116, 0, 29, 6, 0, 193, 2, 0, 16, 0, 2, 6, 0, 193, 0, 0, 176, 0, 29, 6, 0, 49, 6, 0, 16, 0, 29, 6, 0, 193, 2, 0, 176, 0, 2, 6, 0, 193, 0, 0, 176, 0, 29, 7, 0, 193, 6, 0, 16, 0, 29, 8, 0, 128, 7, 0, 64, 0, 50, 6, 0, 128, 0, 0, 0, 0, 2, 6, 0, 128, 6, 0, 192, 0, 29, 8, 0, 64, 6, 0, 0, 0, 50, 6, 0, 128, 0, 0, 0, 0, 2, 6, 0, 128, 7, 0, 0, 0, 29, 8, 0, 32, 6, 0, 0, 0, 29, 8, 0, 16, 6, 0, 128, 0, 29, 6, 0, 128, 6, 0, 128, 0, 3, 6, 0, 128, 7, 0, 64, 0, 29, 6, 0, 64, 7, 0, 0, 0, 3, 6, 0, 64, 6, 0, 192, 0, 29, 7, 0, 32, 6, 0, 0, 0, 2, 7, 0, 32, 6, 0, 64, 0, 29, 10, 0, 243, 7, 0, 170, 0, 4, 9, 0, 4, 10, 0, 0, 0, 3, 9, 0, 4, 8, 0, 0, 0, 29, 8, 0, 4, 9, 0, 0, 0, 29, 6, 0, 193, 0, 0, 16, 0, 2, 6, 0, 193, 0, 0, 176, 0, 29, 7, 0, 49, 6, 0, 16, 0, 34, 7, 0, 52, 8, 0, 0, 0, 29, 6, 0, 193, 7, 0, 176, 0, 29, 7, 0, 49, 6, 0, 16, 0, 50, 9, 0, 128, 0, 0, 0, 0, 50, 9, 0, 64, 63, 128, 0, 0, 10, 7, 0, 32, 9, 0, 0, 0, 29, 9, 0, 32, 7, 0, 128, 0, 10, 7, 0, 16, 9, 0, 0, 0, 29, 9, 0, 16, 7, 0, 192, 0, 9, 9, 0, 32, 9, 0, 64, 0, 29, 10, 0, 128, 9, 0, 128, 0, 9, 9, 0, 16, 9, 0, 64, 0, 29, 10, 0, 64, 9, 0, 192, 0, 29, 6, 0, 193, 9, 0, 176, 0, 29, 9, 0, 128, 6, 0, 0, 0, 29, 9, 0, 64, 6, 0, 0, 0, 29, 9, 0, 32, 6, 0, 0, 0, 29, 9, 0, 16, 6, 0, 0, 0, 50, 10, 0, 240, 63, 128, 0, 0, 2, 10, 0, 243, 9, 0, 27, 0, 29, 11, 0, 243, 1, 0, 27, 0, 3, 11, 0, 243, 10, 0, 27, 0, 29, 12, 0, 243, 3, 0, 27, 0, 3, 12, 0, 243, 9, 0, 27, 0, 1, 11, 0, 243, 12, 0, 27, 0, 29, 9, 0, 128, 6, 0, 64, 0, 29, 9, 0, 64, 6, 0, 64, 0, 29, 9, 0, 32, 6, 0, 64, 0, 29, 9, 0, 16, 6, 0, 64, 0, 50, 10, 0, 240, 63, 128, 0, 0, 2, 10, 0, 243, 9, 0, 27, 0, 29, 12, 0, 243, 11, 0, 27, 0, 3, 12, 0, 243, 10, 0, 27, 0, 29, 13, 0, 243, 4, 0, 27, 0, 3, 13, 0, 243, 9, 0, 27, 0, 1, 12, 0, 243, 13, 0, 27, 0, 29, 5, 0, 243, 12, 0, 27, 0 ]);
		private static var _byteCode:ByteArray;
		
		private var dp_arr:Array;
		private var w:int = 6;
		private var h:int = 6;
		
		private var myShader:Shader;
		private var s:Shape;
		
		public function GradientMesh(){
			s = new Shape();
			initShader();
			//onLoad("asset/ThreePointGradient.pbj");
		}
		private function initShader():void{
			if (!_byteCode){
				_byteCode = new ByteArray();
				var len:int = _data.length;
				for (var i:int = 0; i < len; i++){
					_byteCode.writeByte(_data[i]);
				}
				setShader( _byteCode );
				setPoligon();
			}
		}
		private function onLoad( _url:String ):void{
			trace("vre.pbj")
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.BINARY;
			loader.addEventListener(Event.COMPLETE, onLoadComplete);
			loader.load(new URLRequest( _url ));
		}
		private function onLoadComplete(e:Event):void{
			var l:URLLoader = e.target as URLLoader;
			setShader( l.data );
			
			/* Shader データをByteCode出力
			Exporter.Export( l.data );
			//*/
		}
		private function setShader( _bc:ByteArray):void{
			myShader = new Shader();
			myShader.byteCode = _bc;
			//myRender()
		}
		
		private function setPoligon():void{
			
			dp_arr = addPoligon(h,w);
			
			for(var i:int = 1; i<h; i++){
				for(var j:int = 1; j<w; j++){
					addTri( dp_arr[i-1][j-1],  dp_arr[i-1][j], dp_arr[i][j-1], dp_arr[i][j]);
					drawTri( dp_arr[i-1][j-1],  dp_arr[i-1][j], dp_arr[i][j-1], dp_arr[i][j]);
				}
			}
			
			this.addChild(s);
			
			// ドラッグするポインタはあと吊るし
			for(var i:int = 0; i<h; i++){
				for(var j:int = 0; j<w; j++){
					this.addChild( dp_arr[i][j] );
				}
			}
		}
		private function addTri(p1:DragPoint, p2:DragPoint, p3:DragPoint, p4:DragPoint):void{
			this.addChild( new TrianglGradient( myShader, p1.p, p2.p, p3.p) );
			this.addChild( new TrianglGradient( myShader, p4.p, p2.p, p3.p) );
		}
		public function reDraw():void{
			s.graphics.clear();
			for(var i:int = 1; i<h; i++){
				for(var j:int = 1; j<w; j++){
					drawTri( dp_arr[i-1][j-1],  dp_arr[i-1][j], dp_arr[i][j-1], dp_arr[i][j]);
				}
			}
		}
		private function drawTri(p1:DragPoint, p2:DragPoint, p3:DragPoint, p4:DragPoint):void{
			var g:Graphics = s.graphics;
			
			g.lineStyle(0,0);
			g.moveTo(p1.x, p1.y);
			g.lineTo(p2.x, p2.y);
			g.lineTo(p3.x, p3.y);
			g.lineTo(p1.x, p1.y);
			
			g.moveTo(p4.x, p4.y);
			g.lineTo(p2.x, p2.y);
			g.lineTo(p3.x, p3.y);
			g.lineTo(p4.x, p4.y);
		} 
		
		private function addPoligon(_i:int = 2, _j:int = 2, margin:int = 50):Array{
			var _arr:Array = [];
			
			for(var i:int = 0; i<_i; i++){
				var _arr2:Array = [];
				for(var j:int = 0; j<_j; j++){
					_arr2.push( new DragPoint(this, new Point(j*margin + margin, i*margin + margin), 0xFFFFFF * Math.random(), 1) );
				}
				_arr.push(_arr2);
			}
			return _arr;
		}
		
		private function myRender():void{
			myShader.data.point1.value = [50, 0];
			myShader.data.point2.value = [0, 50];
			myShader.data.point3.value = [100, 50];
		
			// setting Colors
			myShader.data.color1.value = [ 1, 0, 0, 1.0];
			myShader.data.color2.value = [ 0, 1, 0, 1.0 ];
			myShader.data.color3.value = [ 0, 0, 1, 1.0 ];
			
			var g:Graphics = this.graphics;
			g.beginShaderFill( myShader );
			g.moveTo(50,0);
			g.lineTo(0,50);
			g.lineTo(100,50);
			g.lineTo(50,0);
		}
	}
}
import flash.display.*;
class DragPoint extends Sprite{
	private var g:Graphics;
	public var p:Pointer;
	private var myParent:GradientMesh;
	public function DragPoint( _parent:GradientMesh, _p:Point, _color:uint, _aa:Number):void{
		p = new Pointer( _p, _color, _aa);
		
		g = this.graphics;
		g.lineStyle(0, 0x333333);
		g.beginFill( _color );
		g.drawCircle(0,0,4);
		g.endFill();
		
		this.x = _p.x;
		this.y = _p.y;
		
		myParent = _parent;
		
		this.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
	}
	private function onDown(e:MouseEvent):void{
		this.stage.addEventListener( MouseEvent.MOUSE_MOVE, onMove);
		this.stage.addEventListener( MouseEvent.MOUSE_UP, onUp);
	}
	private function onMove(e:MouseEvent):void{
		p.x = this.x += this.mouseX;
		p.y = this.y += this.mouseY;
		myParent.reDraw();
		p.movePos();
	}
	private function onUp(e:MouseEvent):void{
		this.stage.removeEventListener( MouseEvent.MOUSE_MOVE, onMove);
		this.stage.removeEventListener( MouseEvent.MOUSE_UP, onUp);
	}
}

import flash.display.*;
class TrianglGradient extends Sprite{
	private var shader:Shader;
	private var g:Graphics;
	
	private var p1:Pointer, p2:Pointer, p3:Pointer;
	public function TrianglGradient(_shader:Shader, _p1:Pointer, _p2:Pointer, _p3:Pointer):void{
		
		p1 = _p1;
		p1.setTriangls( this, 1);
		p2 = _p2;
		p2.setTriangls( this, 2);
		p3 = _p3;
		p3.setTriangls( this, 3);
		
		g = this.graphics;
		shader = _shader;
		
		setShader();
	}
	public function setShader():void{
		
		// setting Points
		shader.data.point1.value = [p1.x, p1.y];
		shader.data.point2.value = [p2.x, p2.y];
		shader.data.point3.value = [p3.x, p3.y];
		
		// setting Colors
		shader.data.color1.value = [ p1.rr, p1.gg, p1.bb, p1.aa ];
		shader.data.color2.value = [ p2.rr, p2.gg, p2.bb, p2.aa ];
		shader.data.color3.value = [ p3.rr, p3.gg, p3.bb, p3.aa ];
		
		onFill();
	}
	private function onFill():void{
		g.clear();
		g.beginShaderFill( shader );
		g.moveTo( p1.x, p1.y);
		g.lineTo( p2.x, p2.y);
		g.lineTo( p3.x, p3.y);
		g.lineTo( p1.x, p1.y);
		g.endFill();
	}
}

import flash.geom.Point;
class Pointer{
	public var color:uint;
	public var rr:Number, gg:Number, bb:Number, aa:Number;
	public var x:int, y:int;
	public function Pointer(_p:Point, _color:uint, _alpha:Number):void{
		x = _p.x;
		y = _p.y;
		
		color = _color;
		
		rr = ( _color >> 16 ) / 255;
		gg = ( _color >> 8 &0xFF ) / 255;
		bb = ( _color &0xFF ) / 255;
		
		aa = _alpha;
	}
	public function movePos():void{
		for(var i:int = 0; i<len; i++){
			var t:TrianglGradient = tri_arr[i].Tri as TrianglGradient;
			t.setShader();
		}
	}
	public function setPos(_p:Point):void{
		x = _p.x;
		y = _p.y;
		
		for(var i:int = 0; i<len; i++){
			var t:TrianglGradient = tri_arr[i].Tri as TrianglGradient;
			t.setShader();
		}
	}
	public function setColor(_color:uint):void{
		color = _color;
		
		rr = ( _color >> 16 ) / 255;
		gg = ( _color >> 8 &0xFF ) / 255;
		bb = ( _color &0xFF ) / 255;
		
		for(var i:int = 0; i<len; i++){
			var t:TrianglGradient = tri_arr[i].Tri as TrianglGradient;
			t.setShader();
		}
	}
	
	private var tri_arr:Array = [];
	private var len:int = 0;
	public function setTriangls(triangls:TrianglGradient, num:int):void{
		tri_arr.push({"Tri":triangls, "num":num});
		len = tri_arr.length;
	}
	
	public function $toString():String{
		var str:String = " x:" + x + " y:" + y;
		str += " Color:" + color;
		str += " R:" + (color >> 16) + "(" + rr + ")";
		str += " G:" + (color >> 8 &0xFF) + "(" + gg + ")"; 
		str += " B:" + (color &0xFF) + "("+ bb + ")" + " alpha:" + aa;
		return str;
	}
}

import flash.utils.ByteArray;
import flash.events.MouseEvent;
class Exporter{
	public static function Export( _data:* ):void{
		
		var ba:ByteArray = new ByteArray();
		ba.writeBytes( _data );
		
		var len:int = ba.length;
		var str:String = "["
		
		for(var i:int = 0; i<len; i++){
			str += i==len-1 ? ba[i] : ba[i] + ", ";
		}
		
		str += " ]";
		
		trace(str);
	}
}