“Easily create two sided 3D DisplayObjects and flip card effects using Flash Player 10 and the new 3D API …”

PaperSprite Demo

I’ve made a couple of small improvements to the PaperSprite class:

  1. The visible face of the plane is updated automatically only when required. This saves on processing load and also means you can just set up the PaperSprite and not have to worry about anything else, just tween the crap out of all its properties and it won’t mind one bit. (Thanks to Jesse for the pointers re. using stage.invalidate for this process)
  2. The back face is now automatically flipped horizontally, so text and graphics are no longer mirrored and will display correctly
  3. I’ve changed the logic which sets the dynamic registration point for the pivot. This should now correctly line up the front and back faces of the 3D plane, regardless of where their individual registration points are.

Also, some people have asked how to use it, so here is a really quick example to get you up and running with PaperSprite if you are not familiar with using classes and are using the Flash IDE to compile your movie:

  1. First, download the PaperSprite class and put the soulwire folder in the same directory as your FLA or source code.
  2. Then paste this code into the main timeline of your FLA:

// Import the PaperSprite class

import soulwire.display.PaperSprite;

/*
Create a new PaperSprite:

If your front and back faces already exist, you can pass them straight
into the constructor, like so:

myPaperSprite = new PaperSprite( myFrontMc, myBackMc );
*/

var myPaperSprite:PaperSprite = new PaperSprite();

/*
Optionally specify a pivot point, in this example the centre is used
(this is also the default so there is no need to set this normally).

To pivot around the top left use:
myPaperSprite.pivot = new Point(0,0);

or for bottom right:
myPaperSprite.pivot = new Point(1,1);

and so on...
*/

myPaperSprite.pivot = new Point(0.5,0.5);

// Centre it on the stage

myPaperSprite.x = stage.stageWidth / 2;
myPaperSprite.y = stage.stageHeight / 2;

/*
Set the front and back faces:

These can be any type of DisplayObject, for example, to use a MovieClip
from your library use:

myPaperSprite.front = new MyLibraryClip();
*/

myPaperSprite.front = createSprite(0x66FF00);
myPaperSprite.back = createSprite(0xFF3399);

// Add the PaperSprite to the display list

addChild ( myPaperSprite );

// This method just creates coloured boxes for the demo

function createSprite (colour:uint):Sprite
{
	var mySprite:Sprite = new Sprite();
	mySprite.graphics.beginFill ( colour );
	mySprite.graphics.drawRect (0,0,100,100);
	mySprite.graphics.endFill ();
	return mySprite;
}

// Listen for the enterFrame event

addEventListener ( Event.ENTER_FRAME, spin );

/*
Move and rotate your PaperSprite in any way you want - it will
update it's visible face automatically!
*/

function spin ( event:Event ):void
{
	var mX:Number = ((mouseX / stage.stageWidth) - 0.5) * 360;
	var mY:Number = ((mouseY / stage.stageHeight) - 0.5) * 360;

	myPaperSprite.rotationY += (mX - myPaperSprite.rotationY) / 40;
	myPaperSprite.rotationX += (mY - myPaperSprite.rotationX) / 40;
}

Publish the movie and you’re all done!

Background

Flash Player 10’s new 3D capabilities are a much welcomed and exiting addition, and although their implementation at first seemed a little obtuse after using libraries such as Papervision3D, Adobe’s implementation is beginning to make a lot more sense to me. Rather than being handed a complete 3D library, developer’s have been given the necessary building blocks to use 3D, or 2.5D with less ‘hacks’ and arguably better performance where it matters.

Time will tell, but I personally think this will be a positive development for existing frameworks like Papervision and Sandy, because whilst Flash can now natively handle certain tasks like triangle drawing, 3D matrices etc – implementing cameras and shaders, loading and building meshes and so on is something which developers will still require. If this can plug neatly into Flash Player’s capabilities and possibly yield increased performance then all the better.

In more basic situations though, the ability to rotate a DisplayObject around its X, Y and Z axis, as well as finally having a Z property, is useful for adding basic 3D effects, even if your project doesn’t require anything more sophisticated than that.

But this is where some gaps begin to arise. Z sorting, for example, will be required, and Ralph Hauwert (a member of the Papervision3D team) has already written a useful script for this.

Two Sided DisplayObjects

Another common use for Flash Player 10’s 3D capabilities is to create double sided sprites or planes, than can be rotated to reveal alternate content on either side. Again, I would have expected this to be a new property of DisplayObjectContainer, so that front and back could be passed to it as DisplayObjects. This isn’t the case though, so it requires some minimal work around.

Lee Brimelow demonstrated one approach to this, which was to monitor the rotation of a DisplayObject, and swap the visible faces accordingly (Jesse Freeman also used this technique). This works great in many cases, however it doesn’t give the desired results when the parent DisplayObject is off centre and therefore subject to the perspective of the 3D environment. It also means that if you are rotating the DisplayObject around multiple axis then the calculations required get slightly more complex.

The Solution

As ever, the wise and prolific Senocular came to the rescue. He described a technique whereby 3 points are placed on the surface of the DisplayObject, and by determining the winding direction of these points when translated from 3D to 2D space; one is able to determine which way the DisplayObject is facing. Because this is relative to the perspective used to produce this transformation, the result is true to the point of view of the user.

Senocular published his helper class which will return a Boolean value indicating whether a given DisplayObject is front facing or not, and so I wish to stress that all credit is his for his solution and wonderfully clear explanation of the process.

I did feel that it might be beneficial though to wrap this up, along with other functionality, into a class which solved the double sided DisplayObject issue, and so I introduce the PaperSprite class (I chose to call it PaperSprite so that it isn’t confused with Sprite3D if you use another 3D library).

How to use PaperSprite

Creating a two sided Plane using PaperSprite is simple:

import soulwire.display.PaperSprite;

var mySprite:PaperSprite = new PaperSprite();

// Any type of DisplayObject can be used for each face
mySprite.front = new Sprite();
mySprite.back = new Bitmap( myImage );

So you can use any DisplayObject for the front and reverse sides; MovieClips, Sprites, Bitmaps, Shapes, Loaders etc. These can also be set via the constructor

Another property of PaperSprite is whether is updates it’s display automatically. If true, once the PaperSprite is added to the stage it will update its visible face autonomously, however if you have a more general render loop where you’re controlling your 3D scene from, you can set this to false and then call the update method when calculating the visible face is required:

Thanks to Jesse Freeman for suggesting a method to limit unnecessary calls to the update method. By overriding the setters for the position and rotation properties and using stage.invalidate(), then listening for the render event (Event.RENDER), the calculations needed to determine the visible face of the PaperSprite need only be made if one or more properties have been changed and if the PaperSprite needs to be rendered. Therefore there is no longer a need for autoUpdate, everything is handled internally by the PaperSprite class. Cheers Jesse! :)

…The other useful feature is the pivot property. By default, a PaperSprite will pivot (rotate around) its centre point, which will be automatically calculated for each face depending on its respective dimensions. Alternatively, if you want the PaperSprite to rotate around the top left corner, bottom right, or indeed any point on its surface, you can set the pivot property, which is a Point object and requires 2 normalised values, representing the fraction of the width and height of the PaperSprite at which you want to place the pivot.

So to set the PaperSprite to rotate around its centre (this is set by default) you can use:

mySprite.pivot = new Point( 0.5, 0.5 );

And its Top Left corner:

mySprite.pivot = new Point( 0.0, 0.0 );

And its top centre:

mySprite.pivot = new Point( 0.5, 0.0 );

And so on!

You can also access its isFrontFacing property at any time:

if ( mySprite.isFrontFacing )
{
	doSomethingGood();
}

An finally, removing the front or back face from its display list (or indeed not defining one at all) will cause the PaperSprite to display the available face at all times, essentially giving it the default behaviour of a one sided Sprite.

Download the PaperSprite class

Again, this is really just a glorified augmentation of Senocular’s solution to finding the front facing side of a DisplayObject in 3D. All credit should go to him for creating such an elegant solution. I built this class in order to simplify its use and provide the extra functionality needed to quickly start working with a two-sided DisplayObject.

Download: PaperSprite Class & Demo
Posted on 30 Dec 2008
79 Comments
4 Trackbacks

Meta

Double Sided 3D Plane in FP10 was posted on December 30th 2008 in the category Code / Actionscript 3.0, Code, Flash, Lab, Open Source and tagged; , , , , , , .

You can Leave a comment.

Twitter <follow>

March 18th 2010 - 5:29pm

We've missed you Morris: RT @memotv: This guy is nuts. New trailer for Chris Morris' jihadist comedy Four Lions http://is.gd/aNzki

Discussion

83 Responses to Double Sided 3D Plane in FP10

Leave a Reply

Pingbacks / Trackbacks

  1. 1 year ago Senocular’s Two Sided 3D Clip

    [...] technique. The class is worth checking out – it’s nicely designed… read more about it here. This entry was posted in 3D, MovieClip and tagged actionscript, cross product, flash, polygon [...]

  2. 4 months ago 3D Cards in Flash « Bird In The City.com

    [...] Link Filed under: 3D, AS 3.0, Flash Leave a comment Comments (0) Trackbacks (0) ( subscribe to comments on this post ) [...]

  3. 1 month ago MARK/SAM » Blog Archive » Really Cool Two Sided 3D

    [...] found this test on Soulwire. It is an extension of papervision written by Soulwire called PaperSprite. It’s unique [...]

  4. 1 month ago Flash 10 3D example | dehash

    [...] December 23rd, 2009 | Author: dehash 3D Flash Player 10 example and code using PaperSprite and the new built in 3D instead of PaperVision. The output is similar to similar to the [...]

  1. Brian 8 months ago

    I’m having a problem where if I use two movieclips for the front and back, that the movieclip assigned to the back shows up over the top of the front. As soon as I activate the spinning function, back mc dissapears immediately and everything works properly. Have you ever experienced that? Here is the code I am using:

     import gs.TweenLite;
    import gs.easing.*;
    import com.soulwire.display.PaperSprite;
    
    var myPaperSprite:PaperSprite = new PaperSprite();
    
    function init():void
    {
    	myPaperSprite.front = new cs0_thumb0();
    	myPaperSprite.back = new cs0_thumb1();
    
    	myPaperSprite.x = stage.stageWidth / 2;
    	myPaperSprite.y = stage.stageHeight / 2;
    
    	addChild (myPaperSprite);
    }
    
    myPaperSprite.addEventListener(MouseEvent.CLICK, spin);
    function spin ( event:Event ):void
    {
    	TweenLite.to(myPaperSprite, 1, {rotationY:"180"});
    }
    
    init();

    Reply to this comment

  2. David Ortiz 8 months ago

    Hi, Not sure if anyone has run into this problem but if i add a single PaperSprite with 2 sides, and make the pivot = new Point( 1, .5 ), then apply a rotationY of -180 using TweenMax, how come it doesn’t mimic a door opening outward.

    It rotates on the y-axis, inward. But then if i move my papersprite y position down maybe 400 pixels, it rotates outward.

     [
    ps = new PaperSprite(new MyClip1(), new MyClip2());
    this.addChild(ps);
    ps.pivot = new Point(1, .5);
    TweenMax.to(ps, 3, { rotationY:-180 } ); ] 

    Reply to this comment

  3. Zack J. 6 months ago

    Hey, just downloaded the new version. I used a modified version of the original on an old project and I’m glad to see the new features.

    One change I had to make (and maybe you already provided for this and I didn’t see it) was to set back.visible to false in the constructor. I found that if both MCs were on stage but the back was higher in the display list than the front (a common scenario if the scene was designed in the Flash IDE), a reversed “back” would be in front until I actually invalidated the thing with an animation.

    So like I said, maybe there’s already a solution somewhere in there. If not, there’s a tweak for ya.

    Reply to this comment

  4. Adrian Parr 5 months ago

    Hi Justin,

    Thanks for this class. It is very handy. Nice work.
    I can second what Kack J said about the back being visible straight away.
    The workaround is to modify the constructor in PaperSprite,as to read …

    public function PaperSprite( __front:DisplayObject = null, __back:DisplayObject = null )
    {
    if ( __front )
    {
    front = __front;
    }

    if ( __back )
    {
    back = __back;
    back.visible = false;
    }

    addEventListener( Event.RENDER, update );
    }

    Reply to this comment

  5. 95Ghz 5 months ago

    I must say, this is a REALLY cool thing you got here! Much easier and simpler to setup than PV3D.

    However, it is just me or do movieclips on the back side appear blurrier than on the front side?

    Also, another strange quirk…adding radio buttons to movieclip used as one of the sides makes it so it doesn’t flip perfectly on the vertical axis, ie, the side w/o the radio buttons will be shifted lower a bit.

    Other than that, this thing is great!!! Would love to see cleaner text on the backside though. That’s what I always hated about using PV3D, getting text to render cleanly and as nice as a 1:1 screen pixel scale would look.

    Reply to this comment

  6. 95Ghz 5 months ago

    Another quirk I just discovered…

    Convert a layer that contains a dynamic text field into a mask layer.
    Put a colored rectangle in the layer below.
    Lock both layers.

    The mask works in the IDE. However, when you compile, it no longer works. But convert that dynamic textfield into a static textfield and all is happy in the valley again!

    Just reporting :)

    Not a big deal since there is a workaround for this. I think the blurry backside is pretty high priority since quite a few comments have reported it.

    Thanks again, your hard work is appreciated!

    Reply to this comment

  7. Daniel Sands 5 months ago

    Hi There,

    Your class has proven to be very very handy, but I’ve ran in to a problem, hopefully it will be a simple one to fix, so here goes:

    Basically, I’m trying to create a simple book, which starts out closed, and then opens once clicked, to reveal the inside contents (i.e. the other side of the paper sprite) this all worked fine. So I then created a function which could zoom in to a particular section on the page, which worked – but resulted in the contents becoming blurred. To get around this I thought it would be a simple case of creating the actual pages twice as big as they needed to be, and then scaling them to .scaleX = .5; scaleY = .5

    So, I now have 2 Sprites which represent 2 sides of a page, I set each sprites scale to .5, and apply these sprites to “MyPaperSprite”, which is fine – until I come to tween the “rotationY” – for some reason, when i do:

     var tween1:Tween = new Tween(myPaperSprite,"rotationY",Strong.easeOut,0,180,1.5,true);

    - the sprite on the inside seems to be morphed, and the Y scale seems to revert back to 1, causing the effect to be broken, as I want the scale to stay at .5 until I then zoom in to the image…

    I tried setting myPaperSprite.back.scaleY = .5; and calling the “update” function, but it seems to ignore it.

    My explanation of this problem may not make much sense, so if you want me to send you an example file please email me and I’ll be happy to provide the fla file I’m working on.

    I look forward to hearing your response.

    Reply to this comment

  8. Daniel Sands 5 months ago

    Update:
    I added:

    tween1.addEventListener(TweenEvent.MOTION_FINISH, theFuncMan);
    	function theFuncMan(e:TweenEvent):void
    	{
    		myPaperSprite.scaleX = .5;
    		myPaperSprite.update();
    	}
     


    And this seems to then set the scale correctly at the end of the tween, but why is the scale being played around with in the first place? I’m tweening the rotation, not scale?

    Help!

    Reply to this comment

  9. Alex 3 months ago

    Hi there, thanks for the great class!
    However, I do have a question:

    I already got a movieclip on the stage with some rotation animations on in (Tweenmax).
    So the only thing I have to do, is attaching a back to the mc. Is this possible with your class?
    Thanks in advance!

    Reply to this comment

  10. tapan 2 months ago

    I am just getting started with learning some new things flash. I was trying to implement this and start with duplicating the page and actions on this page. So far every time I try to change something the green box disappears. I want to make the same kind of page or image above…just with a different base image.

    would love some advice

    Reply to this comment

  11. Jeffrey 2 months ago

    Soulwire,

    Thank you for creating this great tool!

    I encountered a situation where my top most layer in the Flash IDE was a mask layer and as a result my addChild (myPaperSprite) child was being added to the top of the display list but BELOW the mask. This caused my rotating papersprite to be masked as well.

    Research uncovered this forum thread:

    http://www.actionscript.org/forums/showthread.php3?t=177448

    which provided an effective workaround using the Flash IDE alone. Specifically, the last two postings in the thread led the way to my solution of placing a “dummy” layer above the mask layer and populating it with some small drawing object off stage.

    Reply to this comment

  12. p48l0 2 months ago

    good job dude, amazing class it helps a lot!

    Reply to this comment