forked from: forked from: flash on 2010-2-12

by dizgid forked from forked from: flash on 2010-2-12 (diff: 62)
♥0 | Line 139 | Modified 2010-02-27 17:03:11 | MIT License
play

ActionScript3 source code

/**
 * Copyright dizgid ( http://wonderfl.net/user/dizgid )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/yOZq
 */

// forked from dizgid's forked from: flash on 2010-2-12
// forked from dizgid's flash on 2010-2-12
package {
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextField;
	import flash.display.Graphics;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.*;
    
    [SWF(backgroundColor="#FFFFFF", frameRate=24)]
    public class FlashTest extends Sprite {
    	
		private var my_mc:Plane;
		private var my_mc1:Plane;
		
		private var _camVec:Vector3D = new Vector3D(0,400,-880);
		private var _planePos:Vector3D = new Vector3D(500,400,0);
		private var _field:Sprite;
		
		public var _scene:Sprite;
		public var _cam:Sprite;
		public var _camTarget:Sprite;
		
		public var _focalLength:Number;
		
        public function FlashTest() {
            // write as3 code here..
            
			Debug.init();
			this.addChild(Debug.container);
            
            _scene  = new Sprite();
            this.addChild(_scene);
            
            _field  = new Sprite();
            _scene.addChild(_field);
            
            _cam = new Sprite();
            _scene.addChild(_cam);
            
            _cam.x = _camVec.x;
            _cam.y = _camVec.y;
            _cam.z = _camVec.z;
            
            _camTarget = new Sprite();
            _scene.addChild( _camTarget);
            _camTarget.x = 0;
            _camTarget.y = 0;
            _camTarget.z = 0;
            
            
            _scene.x = stage.stageWidth/2;
            _scene.y = stage.stageHeight/2;
           _scene.z = 0;
            //
            my_mc = new Plane();
            my_mc.x = _planePos.x;
            my_mc.y = _planePos.y;
            my_mc.z = _planePos.z;
            my_mc.rotationY = 90;
            //my_mc.rotationX = -50;

			_field.addChild(my_mc);
			
			my_mc1 = new Plane();
            my_mc1.x = -_planePos.x;
            my_mc1.y = -_planePos.y;
            my_mc1.z = _planePos.z;
            my_mc1.rotationY = -90;

			_field.addChild(my_mc1);
			
			_focalLength = root.transform.perspectiveProjection.focalLength
			
			addEventListener(Event.ENTER_FRAME, update);
        }
	    private function update(eventObject:Event):void {
	    _cam.transform.matrix3D.pointAt(_camTarget.transform.matrix3D.position, new Vector3D(0, 0, 1), new Vector3D(0, -1, 0));
	    	var mat:Matrix3D = _cam.transform.getRelativeMatrix3D(_scene);
	    mat.invert();
	    mat.appendTranslation(stage.stageWidth/2, stage.stageHeight/2,0);
	    
	    	_scene.transform.matrix3D = mat;
	    	
	    		_field.rotationY += 1.0;
	    	
		 // my_mc.rotationY += 1.0;
		 // my_mc.rotationX += 1.0;
		  
		  //my_mc1.rotationY += 1.0;
		  //my_mc1.rotationY += 1.0;
		  
		  my_mc.update(this);
		 my_mc1.update(this);
		 
		  //Debug.log(String(_focalLength));
		} 
    }
}


import flash.display.Sprite;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.*;

class Plane extends Sprite{
	
	public function Plane(){
		var size:Number = 300;
			var g:Graphics = this.graphics;
            g.beginFill(0xff0000);
            g.drawRect(-size/2, -size/2, size, size);
            g.endFill();
            
            var txt:TextField = new TextField();
            txt.defaultTextFormat = new TextFormat(null, 90);
            txt.autoSize = TextFieldAutoSize.LEFT;
            txt.text = "ABC";
            txt.x = -txt.width / 2;
            txt.y = -txt.height / 2;
            this.addChild(txt);
	}
	
	public function update(world:FlashTest):void
	{
		var _cam:Sprite = world._cam;
		var _camTarget:Sprite = world._camTarget;
		var target:Sprite = world._scene;
		
		var mat:Matrix3D = this.transform.getRelativeMatrix3D(target);
		var ftontVec:Vector3D = mat.deltaTransformVector(new Vector3D(0, 0, 1));
		 var p:Vector3D = mat.position;
		 
		 var _camPos:Vector3D = _cam.transform.getRelativeMatrix3D(target).position;
		 var _camTargetPos:Vector3D = _camTarget.transform.getRelativeMatrix3D(target).position;
		 var _camVec:Vector3D = new Vector3D(0,0,1);
		 
		 var camVec:Vector3D = _camTargetPos.subtract(_camPos);
		 camVec.normalize();
		 Debug.log(String(camVec));
		 //var focalVec:Vector3D = new Vector3D(0,0,world._focalLength);
		 camVec.scaleBy(world._focalLength);
		 var focalVec:Vector3D = camVec;
		p = p.add(focalVec);
		 
		 //Debug.log(String(target.z));
		 var tVec:Vector3D = _camPos.subtract(p);
		 
		  var nDotProduct:Number = tVec.dotProduct(ftontVec);
		  
		  if (nDotProduct < 0) {
		    this.alpha = 0.5;
		  } else {
		    this.alpha = 1;
		  }
	}
}


import flash.display.Sprite
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextField;

class Debug extends Sprite{
	public static var debugTxt:TextField;
	public static var container:Sprite;
	
	public function Debug():void
	{
		
	}
	public static function init():void
	{
		debugTxt = new TextField();
		debugTxt.width = 400;
		debugTxt.text = "";
		Debug.container = new Sprite();
		Debug.container.addChild(debugTxt);
		
	}
	public static function log(str:String):void
	{
		debugTxt.text = "";
		debugTxt.appendText(str + "\n");
	}
}