Unproject issue with broomstick

Software: Away3D 4.x

hiroalex, Newbie
Posted: 19 July 2011 01:15 AM   Total Posts: 15

Hi,

First of all I would like to congratulate you for the good job you do with Away3D, and particularly with the Broomstick version.
So, I am evaluating this broomstick version and I am trying to use the unproject method from the Camera3D class, but either I don’t understand how it works, either the results returned are weird.
Here is a code sample I try to run. The goal is to create a Sprite3D where the user doubleClick :

<?xml version=“1.0” encoding=“utf-8”?>
<s:Application xmlns:fx=“http://ns.adobe.com/mxml/2009”
    xmlns:s=“library://ns.adobe.com/flex/spark”  backgroundAlpha=“0”    xmlns:mx=“library://ns.adobe.com/flex/mx” minWidth=“955” minHeight=“600” creati>
<fx:Declarations>
  <!—Place non-visual elements (e.g., services, value objects) here—>
</fx:Declarations>

<fx:Script>
  <![CDATA[
  import away3d.cameras.lenses.PerspectiveLens;
  import away3d.containers.View3D;
  import away3d.entities.Sprite3D;
  import away3d.materials.ColorMaterial;
   
  private var _view:View3D;
 
  private function init():void
  {
  _view=new View3D();
  _view.width=500;
  _view.height=500;
 
 
 
  var lens:PerspectiveLens=new PerspectiveLens();
  lens.far=60000;
 
  _view.camera.lens=lens;
  _view.camera.x=0;
  _view.camera.y=0;
  _view.camera.z=0;
 
  uiComponent.addEventListener(MouseEvent.DOUBLE_CLICK,onDoubleClick);
 
  uiComponent.addChild(_view);
 
  }
 
  private function onDoubleClick(evt:MouseEvent):void
  {
  var p:Point = new Point(evt.localX-(uiComponent.width/2),evt.localY-(uiComponent.height/2));
  //var p:Point = new Point(evt.localX,evt.localY);
 
  trace(”********************************************************************”);
  trace(“point = “+p);
 
     
  var vector3d:Vector3D=_view.camera.unproject(p.x,p.y); 
 
  var mat:ColorMaterial=new ColorMaterial(0xff0000);
 
  var tag:Sprite3D=new Sprite3D(mat,5,5);
  tag.x=vector3d.x;
  tag.y=vector3d.y;
  tag.z=vector3d.z;
 
 
 
  _view.scene.addChild(tag);
 
 
  trace(“vector3d = “+vector3d);
 
  }
 
 
  ]]>
</fx:Script>
 



<mx:UIComponent width=“100%” height=“100” id=“uiComponent”>

</s:Application>

I don’t understand where I am wrong, it would be great if you could give a hand for this.
Take care,

Alex

   

Avatar
SasMaster, Sr. Member
Posted: 19 July 2011 11:35 AM   Total Posts: 127   [ # 1 ]

SO what is your result in this code?

In fact ,what I see in your code is that your uicomponent size is 100%  that means it probably stretch across the full width/height of the application window which is at least 955/600,right?Now ,you view is only 500/500 ! That ,means that there is no way your screen point corresponds to its 3d counterpart because your screens (uicomp and view) have different dimensions.So if you want it work correctly ,make the size of view the same as of the container that contains it or pick the 2d point not on the container but on the view itself.

 Signature 

This looks like a job for superman
http://blog.alladvanced.net

   

hiroalex, Newbie
Posted: 25 July 2011 08:21 PM   Total Posts: 15   [ # 2 ]

Actually I found out the problem. The unproject method accepts only values between -1 and +1 (with 0,0 the center of the view) for X and Y. That’s why it didnt work properly with real screen coordinates. I couldn’t see any documentation about this, that’s why I struggled a bit…

Alex

   

Avatar
David Lenaerts, Administrator
Posted: 25 July 2011 09:39 PM   Total Posts: 80   [ # 3 ]

You should use _view.unproject if you’re working with view coordinates. Camera3D::project uses the only 2D coordinate system it knows as input: its own normalized projection plane. Otherwise, if a single camera is used for multiple views, the resulting unproject code is unpredictable (“which view coordinates are being passed?”)

   

Avatar
SasMaster, Sr. Member
Posted: 17 November 2011 02:42 PM   Total Posts: 127   [ # 4 ]

@David ,it would be nice to get a short explanation how _view,unproject is supposed to work.The thing is that when doing something lile

_view.unproject(mouseX,mouseY) ;
while moving on top of a geometry,only z coordinate of the returned vector looks reasonable. X and Y return some weird numbers

 Signature 

This looks like a job for superman
http://blog.alladvanced.net

   

Avatar
David Lenaerts, Administrator
Posted: 18 November 2011 11:29 AM   Total Posts: 80   [ # 5 ]

_view.unproject(mouseX, mouseY); gives you the world-space coordinates of the point on the actual projection plane (the near plane of the camera - imagine the plane actually placed in the scene; you’re querying for the global position of specific points on that plane).
This point, in combination with the camera’s position, allows you to construct a world space view ray, as “ray = camera.position + (unprojection - camera.position)*t”, t >= 0

   

Avatar
SasMaster, Sr. Member
Posted: 18 November 2011 12:32 PM   Total Posts: 127   [ # 6 ]

My understanding of away3d unproject was the point in the view frustum and not on the near plane surface. If I remember, in away 3x it returned this very type of coordinates .
But that is fine.Fabrice gave idea to look how it works in Drag3d.
Thanks David.

 Signature 

This looks like a job for superman
http://blog.alladvanced.net

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X