Fun with Ribbons3D

Posted on January 13, 2009
Filed Under Actionscript 3.0, experiments, Opensource | Comments Off

experiment_3d_line

Old experiment I found with the ribbon3D class thought might as well post it. With many thanx to Soulwire for writing this class. Based on where your mouse positions it tweens to that location via bezier points.

  1. package
  2. {
  3.  // papervision
  4.  import org.papervision3d.core.effects.view.ReflectionView;
  5.  import org.papervision3d.lights.PointLight3D;
  6.  import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
  7.  import org.papervision3d.objects.DisplayObject3D;
  8.  
  9.  // tweenmax
  10.  import gs.TweenMax;
  11.  import gs.easing.Linear;
  12.  
  13.  // flash
  14.  import flash.display.DisplayObjectContainer;
  15.  import flash.events.Event;
  16.  import flash.filters.BlurFilter;
  17.  
  18.  
  19.  public class CirclingRibbon extends ReflectionView
  20.  {
  21.   private var _light:PointLight3D;
  22.   private var _ribbon:Ribbon3D;
  23.   private var _universe:DisplayObject3D;
  24.   private var _centerX:Number = 0;
  25.   private var _centerY:Number = 0;
  26.   private static const UNIVERSE:Number = 500;
  27.  
  28.   public function CirclingRibbon()
  29.   {
  30.    initialize();
  31.   }
  32.  
  33.   private function initialize():void
  34.   {
  35.    // setup reflection
  36.    viewportReflection.filters = [new BlurFilter(6,6,1)];
  37.    setReflectionColor(.25, .25, .25);
  38.    surfaceHeight = -400;
  39.    
  40.    camera.z = -1500;
  41.    
  42.    // light
  43.    _light = new PointLight3D();
  44.    _light.x = camera.x;
  45.    _light.y = camera.y500;
  46.    _light.z = -camera.z;
  47.    
  48.    viewport.filters = [new BlurFilter(2,2,1)];
  49.    
  50.    // universe
  51.    _universe = new DisplayObject3D();
  52.    scene.addChild(_universe);
  53.    
  54.    // setup line material
  55.    var fMat:FlatShadeMaterial = new FlatShadeMaterial(_light, 0XE20074, 0xF7F7F7);
  56.    fMat.doubleSided = true;
  57.    
  58.    // setup line
  59.    _ribbon = new Ribbon3D(fMat, 40, 40);
  60.    _universe.addChild(_ribbon);
  61.    
  62.    addEventListener(Event.ENTER_FRAME, update);
  63.    
  64.    tween(_ribbon);
  65.   }
  66.  
  67.   public function setStage(inClip:DisplayObjectContainer)
  68.   {
  69.    _centerX = inClip.stage.stageWidth * .5;
  70.    _centerY = inClip.stage.stageHeight * .5;
  71.   }
  72.  
  73.   private function tween(object:Ribbon3D):void
  74.   {
  75.    if(object is Ribbon3D)
  76.    {
  77.     var b:Array = new Array();
  78.     var d:int = random( 3, 6 );
  79.     for (var i:int = 0; i < d * .6 << 0; i++)
  80.     {
  81.      b.push( {
  82.        x:random( -UNIVERSE, UNIVERSE ),
  83.        y:400, // we don't want this to be lower than the reflectionViews surface
  84.        z:random( -UNIVERSE, UNIVERSE )
  85.        } );
  86.     }
  87.    
  88.     var posX:Number = -_centerX + mouseX;
  89.     var posY:Number = _centerY + -mouseY;
  90.     var posZ:Number = (_ribbon.x – _ribbon.y) * 2;
  91.     TweenMax.to(_ribbon, 1, {x:posX, y:posY, z:posZ, ease:Linear.easeNone, bezierThrough:b, onComplete: tween, onCompleteParams: [object] });
  92.    }
  93.   }
  94.  
  95.   private function update(event:Event):void
  96.   {
  97.    _ribbon.draw();
  98.    _universe.yaw(1);
  99.    camera.lookAt(_universe);
  100.    singleRender();
  101.   }
  102.  
  103. // ———————————————————————————–
  104.   public static function random( min:Number, max:Number = NaN ):Number
  105.   {
  106.    if (isNaN(max)) { max = min; min = 0; }
  107.    return Math.random() * (maxmin) + min;
  108.   }
  109.  }
  110. }

Now add it to your stage and set the stage:

  1. var cRibbon:CirclingRibbon = new CirclingRibbon();
  2. cRibbon.setStage(this);
  3. addChild(cRibbon);

Comments

Comments are closed.

-->