|
pool338, Jr. Member
Posted: 17 July 2012 06:51 AM Total Posts: 37
Hello everyone,
I have a little project, that imports OBJ Files, displays the model and the user can interact (zoom, rotate) it with the mouse. Everything works fine, as long as the model inside the OBJ File is centered at (0,0,0). But whe it is not, then zoom and rotate don’t work as expected, because the camera looks at (0,0,0).
Is there any solution, to set the center of a loaded model to the center of the view?
|
Babylon, Jr. Member
Posted: 17 July 2012 08:29 AM Total Posts: 39
[ # 1 ]
could you not set the models X position to (0 - (model.width/2)) and the Y to (0 - (model.width/2)) ?
|
pool338, Jr. Member
Posted: 17 July 2012 08:36 AM Total Posts: 37
[ # 2 ]
@Babylon: Hi, sure can I set the (x,y,z) Points of the model, but i don’t know how to get the with/height/depth of my model.
I get the model via Loader3D, and this doesn’t have a with/height/depth attribute:
_loader = new Loader3D(); _loader.addEventListener(LoaderEvent.RESOURCE_COMPLETE, onResourceComplete); _loader.load(new URLRequest(fileName)); _view.scene.addChild(_loader);
|
Richard Olsson, Administrator
Posted: 17 July 2012 09:32 AM Total Posts: 1192
[ # 3 ]
Look at the object’s bounds property, which contains information about the bounding volume of the mesh or container (which is the case for Loader3D.)
|
pool338, Jr. Member
Posted: 17 July 2012 10:03 AM Total Posts: 37
[ # 4 ]
@Richard: _loader.bounds does not exist (I think).
|
Richard Olsson, Administrator
Posted: 17 July 2012 10:07 AM Total Posts: 1192
[ # 5 ]
Ah, you’re right. It’s not public on any base classes before Mesh. Well, then you’ll have to calculate the bounds of the container manually, by listening for MESH_COMPLETE and adding the bounds of all the meshes.
|
pool338, Jr. Member
Posted: 17 July 2012 11:45 AM Total Posts: 37
[ # 6 ]
@Richard: I was able to compute the meshes bounds. But how do I now calculate the x,y,z coordinates for the loader.
_loader.x = (0-width/2), _loader.y = (0-height/2), _loader.z = (0-depth/2) doesn’t seem to be right.
For example if I have a model that only needs to be moved along the z-Axis it also would be moved along the x- and y- axis.
|
Richard Olsson, Administrator
Posted: 17 July 2012 11:48 AM Total Posts: 1192
[ # 7 ]
What you’re doing assumes both that width/height/depth have been calculated correctly, and that the origin of your model is in the top, left, near corner of the mesh. You’ll need to consider the min/max values on the bounds to calculate how much the mesh should be moved.
|
pool338, Jr. Member
Posted: 17 July 2012 11:57 AM Total Posts: 37
[ # 8 ]
Here is what I’m currently doing for each mesh of the model:
1. Bounds.getMeshBounds(mesh)
2. depth += Bounds.depth
3. width += Bounds.width
4. height += Bounds.height
After that I set x,y,z of loader with the formula in the previous post
|
Richard Olsson, Administrator
Posted: 17 July 2012 12:03 PM Total Posts: 1192
[ # 9 ]
That won’t work. Just adding the bounds’ dimensions together like that would not work if you have two meshes whose bounding volumes overlap. Imagine if they are both 200 units wide, but have a 100 unit overlap meaning that their total width is 300 units. Your calculations would arrive at 400 units.
But, like I said, don’t just assume that the origin of the meshes is in the top, left, near corner. Instead, take the minX, minY and minZ into account so that you don’t move the mesh too much.
|
pool338, Jr. Member
Posted: 17 July 2012 12:14 PM Total Posts: 37
[ # 10 ]
@Richard: I know u are a pro in this 3D stuff, but I’m not. So thank you very much for your hints, but I’m not able to follow your instructions. Could you please eyplain in more detail?
|
Richard Olsson, Administrator
Posted: 17 July 2012 12:24 PM Total Posts: 1192
[ # 11 ]
I’m sorry if I’m being vague. This is not different from regular Flash though, so if you’re more comfortable working with the regular display list, lets try an analogy.
When you want to center something in Flash, you need to know two things: It’s dimensions, and the position of it’s origin with regards to the bounding rectangle. The origin is the tricky part. Either you make sure that all your assets have their origins in the same position, e.g. in the top left corner or in the center. Or you have to be able to figure out where the origin is, often relative to the top left corner of the graphics.
To do this in Flash you might use getBounds(), which gives you a Rectangle object with properties left, top, right and bottom that define how far the graphics extend from the origin in those four directions.
If both top and left are zero, you know that the origin is in the top left corner. If left==right and top==bottom you know that the origin is in the center.
However, if none of these are true, you need to use a general purpose method to calculating how much you should offset the object to have it appear as if the graphics are in the center.
Now, in Away3D the bounds object is essentially the same thing as the getBounds() rectangle in the regular display list, but instead of top, right, bottom and left the properties are called maxY, maxX, minY and minX. And then of course there is a third dimension, adding the minZ and maxZ properties.
So what you want to do is apply the exact same pattern that you would in regular Flash, but using the Away3D bounds object, and with an extra dimension.
Does that help?
|
|
pool338, Jr. Member
Posted: 17 July 2012 12:34 PM Total Posts: 37
[ # 13 ]
So, to calculate the width of my complete model i would check the minX of all meshes and take the smallest; check maxX of all meshes and take the highest and then the width would be the difference of this two numbers.
Same way for height and depth.
Is that right so far? If so, what than are the values for models x,y,z?
|
Richard Olsson, Administrator
Posted: 17 July 2012 12:44 PM Total Posts: 1192
[ # 14 ]
You are correct in how you should be calculating the bounds.
Please read the articles linked above, and experiment some with the exact equation, but unless I’m mistaken the position (on each axis) should be:
pos = -(max-min) / 2 - min
|
pool338, Jr. Member
Posted: 17 July 2012 01:31 PM Total Posts: 37
[ # 15 ]
@Richard: Thank you for your help.
|