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

3d seconds clock

various codes combined especially the quaternion formula via pv3d.org
/**
 * Copyright siaukia ( http://wonderfl.net/user/siaukia )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/cXA4
 */

// forked from ita's forked from: flash on 2009-7-19
// forked from Rnkn's flash on 2009-7-19
// various codes combined especially the quaternion formula via pv3d.org
package {
    import flash.display.*;
    import flash.utils.*;
	import flash.events.*;
    import flash.text.*;
	
	import org.papervision3d.view.*;
	import org.papervision3d.scenes.*;
	import org.papervision3d.core.proto.*;
	import org.papervision3d.core.math.Quaternion;		
	import org.papervision3d.cameras.*;
	import org.papervision3d.materials.*;
	import org.papervision3d.objects.*
	import org.papervision3d.objects.primitives.*
	import org.papervision3d.render.*;
	
	import gs.TweenMax;
	import gs.easing.*;
    
    public class FlashTest extends Sprite {
        
		private var view:Viewport3D;
//		private var camera:CameraObject3D;
		private var scene:Scene3D;
        
        private var arraySeconds:Array = new Array();        
        private var timer:Timer;
		private var secondsHolder:DisplayObject3D = new DisplayObject3D;;
		private var RADIUS:Number = 300;
		private var anglePer:Number = (Math.PI*2) / 60;					
		
		private var startQuaternion:Quaternion = null;
		private var endQuaternion:Quaternion = null;
		private var currentQuaternion:Quaternion = null;
		private var cameraTarget:DisplayObject3D = new DisplayObject3D();
		private var DISTANCE_FROM_PLANE:Number = 200
		private var TWEEN_TIME:Number = .5;

		private var camera:CameraWithSlerp;
		
		private var renderer:BasicRenderEngine = new BasicRenderEngine()		
		
        public function FlashTest():void {			
			view = new Viewport3D(0,0, true, true);
			camera = new CameraWithSlerp();			
			scene = new Scene3D();
			
			camera.x = 0;
			camera.y = 0;
			camera.z = 0;
			
			secondsHolder.rotationX = 30
			
			scene.addChild(secondsHolder);
			addChild(view);
			initSeconds();
			updateSeconds();			
			
			timer = new Timer(1000);
            timer.addEventListener(TimerEvent.TIMER, loop);
            timer.start();
			
			stage.addEventListener(Event.ENTER_FRAME, stageEnterFrame);
        }
		
		private function stageEnterFrame(e:Event):void {
			renderer.renderScene(scene, camera, view);
		}

		private function initSeconds():void {
			
            for(var i:int = 0;i<60;i++){
                var second:TextField = new TextField;
                var format:TextFormat = new TextFormat("Arial Black",30);                
                second.defaultTextFormat = format;
                
                if (i < 10) {
                        second.text = "0" + i;
                }else{
                        second.text = "" + i;
                }
				
				var mc:MovieClip = new MovieClip();
				mc.graphics.beginFill(0xFFFFFF, 0)
				mc.graphics.drawRect(0, 0, 50, 50)
				mc.addChild(second);				
				var bd:BitmapData = new BitmapData(50,50, true, 0);
				bd.draw(mc);
				var planeMaterial:BitmapMaterial = new BitmapMaterial(bd);
				var plane:Plane = new Plane(planeMaterial, 50, 50, 2, 2);
				var planeHolder:DisplayObject3D = new DisplayObject3D();
				
				planeMaterial.doubleSided = true;
				
				planeHolder.x = Math.cos(i*anglePer) * RADIUS;
				planeHolder.y = 0
				planeHolder.z = Math.sin(i*anglePer) * RADIUS;
				plane.useOwnContainer = true;
				plane.name = "plane";
	
				planeHolder.addChild(plane);
				secondsHolder.addChild(planeHolder);
				arraySeconds.push(planeHolder);				
				
				planeHolder.lookAt(DisplayObject3D.ZERO)
				
            }
			
		}
        
        private function loop(event:TimerEvent):void{
			updateSeconds();
		}
		
		private function updateSeconds():void {
			var d:Date = new Date();
			var s:Number = d.getSeconds();
			
			for (var i:int = 0; i <60; i++) {
				if (i == s) {
					TweenMax.to(arraySeconds[i].getChildByName("plane"), .5, {scale:1, ease:Elastic.easeOut})
				} else {
					TweenMax.to(arraySeconds[i].getChildByName("plane"), .5, {scale:.5, ease:Elastic.easeOut})
				}
			}			
			rotateSphere(arraySeconds[s]);			
		}
		
		private function rotateSphere(object:DisplayObject3D):void {
			cameraTarget.copyTransform(object);
			cameraTarget.moveBackward(DISTANCE_FROM_PLANE);	
			createTween(cameraTarget);	
		}
		private function createTween(displayObject3d:DisplayObject3D):void
		{
			//when "slerping", this value is a range from 0 to 1
			//0 being the starting total rotation (AKA transformation)
			//1 being the ending total rotation		
			camera.slerp = 0;
			
			var tweenObject:Object = {};
			tweenObject.x = displayObject3d.x;
			tweenObject.y = displayObject3d.y;
			tweenObject.z = displayObject3d.z;
			tweenObject.ease = Expo.easeInOut;
			tweenObject.slerp = 1;
			tweenObject.onUpdate = cameraUpdateCallback;
			tweenObject.onComplete = cameraCompleteCallback;
			tweenObject.overwrite = true;
		
			startQuaternion = Quaternion.createFromMatrix(camera.transform);
			endQuaternion = Quaternion.createFromMatrix(displayObject3d.transform);	
		
			TweenMax.to(camera, TWEEN_TIME, tweenObject);
		}		
		private function cameraCompleteCallback():void {
		}
		private function cameraUpdateCallback():void {	
			currentQuaternion = Quaternion.slerp(startQuaternion, endQuaternion, camera.slerp);
			camera.transform.copy3x3(currentQuaternion.matrix);
		}		
    }
}

import org.papervision3d.cameras.*

class CameraWithSlerp extends Camera3D
	{
		public var slerp:Number = 0;
		function CameraWithSlerp() {
		}
	}