Finding 2D bounding box for an object when drawn?

139 views
Skip to first unread message

bakedbeing

unread,
Nov 6, 2008, 6:16:53 PM11/6/08
to away3d.dev
Heya again lads!

Is there a direct way to find a 2D bounding box for an object as
finally drawn to the view?

I want to find the top left and bottom right of the object, so I can
crop my swf to just the edges of the object (move the view object,
resize the swf).

I couldn't find anything in the object3D properties or in view3D.

I was thinking a long way would be to draw the whole view to a
bitmapData object with BitmapRenderSession and trawl that for pixel
data, but I'm hoping there's something easier? A sprite object inside
Projector who's x,y, height and width (in 2D) I can grab??

Thanks!

bakedbeing

unread,
Nov 6, 2008, 6:44:54 PM11/6/08
to away3d.dev
Is there a way to access the graphics container in the view3D or in
the session object? Something that will give me access to the sprite
created for each object?

Mango

unread,
Nov 6, 2008, 10:07:27 PM11/6/08
to away3d.dev
Hello!

The data in these containers doesn't live long enough to access. I
don't think you can retrieve the properties of the objects without
inserting code into the Away3D classes (and the height and width
default to swf height and width anyway).

Object3D provides objectWidth, objectHeight, and objectDepth
properties, and from these you can calculate with the projection the
width and height of the drawn graphic. Unfortunately, I do not have
the time (or knowledge, probably) to show you how to do this.

I'm curious as to why you need these values, though. To what end does
resizing the swf serve? :)

bakedbeing

unread,
Nov 6, 2008, 10:24:36 PM11/6/08
to away3d.dev
Heya Mango, thanks for the response. Yeah I was thinking there's ways
to do it using Matrix math and all the values in the render scene, but
it's well beyond me to do, and I was hoping that the data would be
somewhere in the 3D pipeline and just need to be fished out.

I'm overlaying a 3D object on Google Street View, and it's moving
pretty well with the maps, but if you completely cover the streetview
with another div with flash on it, you can't click and drag on the
streetview below. So I need to calculate my bottle's position, render
it, then move my view and move/crop my swf so it's only the size of
the drawn 3D object :)

Mango

unread,
Nov 6, 2008, 11:15:40 PM11/6/08
to away3d.dev
I'm not sure if this is easier or more useful, but you can use the
Google Maps API for Flash, add the object to a sprite, and set the
sprite to the background of the view:

var view:View3D = new View3D(...);
var streetView:Sprite = //google street view API
view.background = streetView;

In this way, transparent space will not interfere with mouse clicks as
long as you're not using something that fills the entire view (fog
filter, etc).

Quote:
"The Google Maps API now provides a Street View service for obtaining
and manipulating the imagery used in Google Maps Street View!"

I have no experience with street view or google maps, so this info may
be completely irrevalant or useless. :P

Mango

unread,
Nov 6, 2008, 11:21:07 PM11/6/08
to away3d.dev

bakedbeing

unread,
Nov 7, 2008, 12:42:52 AM11/7/08
to away3d.dev
Heya Mango, we were thinking the same here, but it doesn't look like
the actionscript API for google maps includes streetview yet, for that
you need to be using a browser and javascript as far as I can tell :(

Rob Bateman

unread,
Nov 7, 2008, 8:02:17 AM11/7/08
to away3...@googlegroups.com
Hey bakedbeing


have you tried using the bounding radius property yet? you can access the containing radius of any object through the property boundingRadius. This doesn't give you a screen bounding box, but you could use a perspective calulation on the radius to get a view value and work from there.

another quick hack that may be simpler to execute is just taking the rectangle returned from getBounds() on the view! this will give you a Rectangle instance that you can extract all relevant data relating to the bounds of the object inside.

atb

Rob
--
Rob Bateman
Flash Development & Consultancy

rob.b...@gmail.com
www.infiniteturtles.co.uk
www.away3d.com

bakedbeing

unread,
Nov 10, 2008, 7:46:41 PM11/10/08
to away3d.dev
Hey thanks for the tip Rob. I've tried using getBounds on the view,
but it's giving me a bounding rectangle the size of the whole canvas.
Do you know what containers I could look at inside the object to get
something that matches the outline of our object?

On Nov 8, 12:02 am, "Rob Bateman" <rob.bate...@gmail.com> wrote:
> Hey bakedbeing
>
> have you tried using the bounding radius property yet? you can access the
> containing radius of any object through the property boundingRadius. This
> doesn't give you a screen bounding box, but you could use a perspective
> calulation on the radius to get a view value and work from there.
>
> another quick hack that may be simpler to execute is just taking the
> rectangle returned from getBounds() on the view! this will give you a
> Rectangle instance that you can extract all relevant data relating to the
> bounds of the object inside.
>
> atb
>
> Rob
>
> rob.bate...@gmail.comwww.infiniteturtles.co.ukwww.away3d.com

Peter Kapelyan

unread,
Nov 10, 2008, 9:35:31 PM11/10/08
to away3...@googlegroups.com
If your object is the only thing in that view, then the width and height of the view itself should be the number you are looking for (quick hack).
 
I guess you can try this:

hideeverything but the object,
render the view,
get the width and height of the view,
turn on all objects
render again...

If it works i guess its not that bad, it's only rendering that one object twice (should be fast).
Hope it works and sorry if my idea or dirty hack doesn't work, but it's more fun for me than thinking of the math!

-Pete
--
___________________

Actionscript 3.0 Flash 3D Graphics Engine

HTTP://AWAY3D.COM

Peter Kapelyan

unread,
Nov 10, 2008, 10:13:24 PM11/10/08
to away3...@googlegroups.com
Ahh, I just tried it and maybe that's what you already tried, the width of the view does return the full view width...darn.

Mango

unread,
Nov 11, 2008, 1:49:57 AM11/11/08
to away3d.dev
Rob and Peter both have the right idea. Unfortunately, when you add
the view sprite to the parent, it inherits the parent's dimensions.
Solution? Create two identical scenes, but only add one to the parent
display object.

Example:
http://www.mediafire.com/?sharekey=c8a7966316f99e07d2db6fb9a8902bda
(Sorry about the hosting link. My site is not up currently, so I
cannot host it properly. Also, the file upload on this site doesn't
appear to load swfs. Feel free to rehost.)

AS3:
public class BoundTest extends Sprite {
private var view:View3D;
private var view2:View3D;
private var cube:Cube;
private var cube2:Cube;
private var rect:Rectangle;

public function BoundTest() {
init();
}

private function init():void {
view = new View3D({x:200, y:200, clip:new RectangleClipping(-190,
-150, 190, 150)});
view2 = new View3D({x:200, y:200, clip:new RectangleClipping(-190,
-150, 190, 150)});
cube = new Cube();
cube2 = new Cube();
view.scene.addChild(cube);
view2.scene.addChild(cube2);

addChild(view);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

private function onEnterFrame(event:Event):void {
cube.rotationX += 5;
cube.rotationY += 5;
cube.rotationZ += 5;
cube2.rotationX += 5;
cube2.rotationY += 5;
cube2.rotationZ += 5;

view2.render();
rect = view2.getBounds(this);
view.render();

trace(rect.width + ", " + rect.height);
}
}// of class

Mango

unread,
Nov 11, 2008, 2:06:31 AM11/11/08
to away3d.dev
One thing I forgot to note:

Notice the RectangleClipping added to both views. For the view that is
not added to the parent display object, it is necessary to specify a
Clipping, because the view Sprite does not have access to stage
properties.

Peter Kapelyan

unread,
Nov 11, 2008, 9:33:34 AM11/11/08
to away3...@googlegroups.com
That is really great!
 
Can you add this example here please, I think it will help others:

http://away3d.com/examples.php?add=new

Thank you for taking your time to help out, and for your example!
 
-Pete

Mango

unread,
Nov 11, 2008, 6:11:23 PM11/11/08
to away3d.dev
Sure, I have submitted the example.

Mango

unread,
Nov 11, 2008, 6:16:38 PM11/11/08
to away3d.dev
bakedbeing has also posted two great examples that use
session.getContainer() to find the bounds of a single object.

On Nov 11, 8:33 am, "Peter Kapelyan" <flashn...@gmail.com> wrote:

bakedbeing

unread,
Nov 11, 2008, 6:50:58 PM11/11/08
to away3d.dev
Heya gents, thanks for working so hard on that solution Mango, I'll
have a good look through it this afternoon. As you mentioned above,
after a morning of digging through the source I managed to figure out
that the contents of the session.container object report their bounds
as being flush with their edges, not the entire view. I'll post the
code here for anyone who should come looking:


Here's a way to use the Session object within your View3D to find the
bounds of an object you've drawn to the screen with view.render().
This example is built for a single object, in my case an imported OBJ
file. The coordinates returned are relative to (this), the movie
within which the view object is contained.

// view has been defined previously, it's our View3D object.
var sessionContainer:DisplayObjectContainer =
view.session.getContainer(view) as DisplayObjectContainer;
var drawnObject = sessionContainer.getChildAt(0);
var viewBounds = drawnObject.getRect(this);
trace("vb: " + viewBounds.left + "," + viewBounds.top + "/" +
viewBounds.right + "," +viewBounds.bottom);

The next challenge is to find a way to wait until the view has been
successfully rendered. Right now I'm using onEnterframe and I think
it's lagging a frame or two behind.

bakedbeing

unread,
Nov 11, 2008, 6:53:54 PM11/11/08
to away3d.dev
Oh yeah Mango I wasn't sure where the example fitted best so I
submitted it to (I think) the view and session pages.

On Nov 12, 10:16 am, Mango <whiskers8...@yahoo.com> wrote:

Mango

unread,
Nov 11, 2008, 7:29:46 PM11/11/08
to away3d.dev
Hmm. If I remember correctly, you're overlaying a page with the swf,
and you need the swf to resize and reposition dynamically. How are you
communicating with the DOM to achieve this? It's possible that the
delay you're experiencing is caused by interaction between flash
player and the external scripts.
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages