Hi Fabrice , did you implement the Ray Class in the example you
have given here for sphere intersection . I have tried everything
and still ray.getIntersect () returns null . What can be wrong in
what I do here ?
////////////////////////code//////////////////////////////////
package
{
import away3d.core.base.Face;
import away3d.core.base.Segment;
import away3d.core.base.Vertex;
import away3d.core.draw.ScreenVertex;
import away3d.core.math.Number3D;
import away3d.core.utils.Cast;
import away3d.materials.BitmapMaterial;
import away3d.materials.ColorMaterial;
import away3d.materials.utils.data.Ray;
import away3d.primitives.Sphere;
import away3d.sprites.DirSprite2D;
import away3d.sprites.Sprite2D;
import away3d.sprites.Sprite3D;
import flash.display.BitmapData;
import flash.events.Event;
import utils.RayDebugger;
[SWF(backgroundColor="#677999", frameRate="30", quality="LOW",
width="800", height="600")]
public class RayTracingDemo extends AwayTemplate
{
private var _rayDebug:RayDebugger;
private var _sp:Sphere;
private var _ray:Ray;
private var _screenVertex:ScreenVertex;
private var _vertex:Vertex;
private var _sprite3:Sprite3D;
private var _traceDefaultPos:Number3D=new Number3D
(-100,100,-100);
public function RayTracingDemo()
{
super();
}
override protected function initGeometry() : void{
RayDebugger.setView(_view);
_sp=new Sphere({radius:30,material:new ColorMaterial
(0x339955)});
_view.scene.addChild(_sp);
_sp.z=500;
_sp.y=0;
_sp.x=0;
_ray=new Ray();
}
override protected function onEnterFrame(e:Event) : void{
super.onEnterFrame(e);
var mouseDir1:Number3D=_cam.unproject
(mouseX-400,mouseY-300);
mouseDir1.x*=10;
mouseDir1.y*=10;
mouseDir1.z*=10;
_ray.orig= _cam.position;
_ray.dir= mouseDir1;///_sp.position;///
_cam.transform.forward;
for(var b:int=0;b<_sp.faces.length;++b){
var face:Face=_sp.faces[b];
_ray.orig= _traceDefaultPos;
_ray.dir= mouseDir1;d;
trace( _ray.getIntersect
(_ray.orig,
_ray.dir,face.v0.position,face.v1.position,face.v2.position));
}
}
RayDebugger.traceRay(_ray.orig,_ray.dir,
0x228833,false);
RayDebugger.traceViewPortRect
(0,0,800,600,400,300);
}
}
}
}
On Wed, May 5, 2010 at 5:59 PM, Fabrice3D <[email protected]>
wrote:
here you go...
Fabrice
On May 5, 2010, at 4:37 PM, devlander wrote:
> Fabrice where can I get your RayDebug debug class ?
>
> I tried to apply the screen offset but still no success.
>
> Thanks
>
> On May 5, 3:13 pm, Michael Iv <[email protected]> wrote:
>> That is interesting point . I wll try this . Sorry I cant see
the white
>> pixels , it is too far from the screen :)
>>
>>
>>
>>
>>
>>
>>
>> On Wed, May 5, 2010 at 4:07 PM, Fabrice3D <[email protected]>
wrote:
>>> I think I may know why you miss....
>>> in demo I have offseted the screen, because coordinates flash
are top left.
>>> pixel = _view.camera.unproject(startx-400, starty+j-300);
>>
>>> here 400 and 300 being hardcoded as 1/2 values of my screen
800x600
>>
>>> Fabrice
>>
>>> On May 5, 2010, at 3:01 PM, Michael Iv wrote:
>>
>>> I am quite sure that the ray class casts the ray in right
direction , but
>>> the problem that I could not understand was why no intersection
was returned
>>> .
>>> In your example do you receive the intersection of these rays
with the
>>> globe ?
>>
>>> On Wed, May 5, 2010 at 3:51 PM, Fabrice3D <[email protected]>
wrote:
>>
>>>> made little demo to show you thats this is probably what is
going on in
>>>> your code.
>>
>>>> --> this demo projects a stroke 8 px wide in center screen
(800x600), in
>>>> this case at 0,0 to 0,400 x/y
>>>> the ray debugger shows you the camera, viewport, and the stroke.
>>>> the debug rays are using the unproject camera method. (debug
rays
>>>> projected each 20 pixels only first column to stay friend with
the flash
>>>> player)
>>>> as you can see the direction obtained once normalized, is what
you want.
>>>> And also shows Ray class is not wrong.
>>
>>>> Now, the sphere in the world is still object space. not having
any of the
>>>> camera perpective.
>>>> Changing camera after calcs to hovercam to explore, you can
see the debug
>>>> trace shows that once you put yourself behind the camera
>>>> that the result of the stroke does match the rays using the
Ray class
>>>> where no perpective or lens corrections are applied.
>>>> except its obvious that the rays do not fit the image where
the white (the
>>>> hit) should be way smaller if in same space as camera
>>
>>>> http://www.closier.nl/playground/RaySpace.swf
>>
>>>> Again, I'll try improve that class to make it smarter.. its
all a question
>>>> of time :)
>>
>>>> Fabrice
>>
>>>> On May 5, 2010, at 1:09 PM, Michael Iv wrote:
>>
>>>> I was looking at the following function in BitmapMaterial Class:
>>>> if(showNormals){
>>
>>>> _nn.rotate(tri.faceVO.face.normal,
>>>> tri.view.cameraVarsStore.viewTransformDictionary[tri.source]);
>>
>>>> _sv0x = (tri.v0x + tri.v1x + tri.v2x) / 3;
>>>> _sv0y = (tri.v0y + tri.v1y + tri.v2y) / 3;
>>
>>>> _sv1x = (_sv0x - (30*_nn.x));
>>>> _sv1y = (_sv0y - (30*_nn.y));
>>
>>>> _session.renderLine(_sv0x, _sv0y, _sv1x,
_sv1y, 0,
>>>> 0xFF00FF, 1);
>>>> }
>>>> }
>>
>>>> And I can see ( if I am right) that you are setting _nn roation
>>>> multiplying face normal by face matrix that was received from
camera space
>>>> .
>>>> May be that is what has to be done when getting faces for
getIntersection
>>>> ?
>>
>>>> On Tue, May 4, 2010 at 12:11 PM, devlander
<[email protected]>wrote:
>>
>>>>> ok Fabrice.
>>>>> Please notify us if you can get something working ( this is a
critical
>>>>> feature for my project ).
>>
>>>>> Thanks for your support.
>>
>>>>> On May 4, 9:44 am, Fabrice3D <[email protected]> wrote:
>>>>>> Looking at it asap as I need it as well...
>>
>>>>>> On May 4, 2010, at 7:18 AM, Michael Iv wrote:
>>
>>>>>>> Now I can see I am not only one having that problem
>>
>>>>>>> Sent from my iPhone
>>
>>>>>>> On May 4, 2010, at 3:49 AM, devlander
<[email protected]>
>>>>> wrote:
>>
>>>>>>>>
=================================================================
>>>>>>>> Please forget about my previous post (many copy&past
mistakes in the
>>>>>>>> code snippet)
>>>>>>>>
=================================================================
>>
>>>>>>>> Here is my code (corrected):
>>
>>>>>>>> private function _pickMesh(mesh:Mesh):PickingResult {
>>
>>>>>>>> var pr:PickingResult = new PickingResult(); // Store
picking
>>>>>>>> data here
>>
>>>>>>>> var rayPos:Number3D = view3D.camera.unproject
(view3D.mouseX,
>>>>>>>> view3D.mouseY);
>>
>>>>>>>> var rayDir:Number3D = new Number3D();
>>>>>>>> rayDir.x = rayPos.x;
>>>>>>>> rayDir.y = rayPos.y;
>>>>>>>> rayDir.z = rayPos.z;
>>>>>>>> rayDir.normalize();
>>>>>>>> rayPos.add(rayPos, view3D.camera.position);
>>
>>>>>>>> var ray:Ray = new Ray();
>>>>>>>> var pickedPoint:Number3D;
>>
>>>>>>>> for each (var face:Face in mesh.faces) {
>>
>>>>>>>> pickedPoint = ray.getIntersect(rayPos ,
rayDir ,
>>>>>>>> face.v0.position , face.v1.position , face.v2.position);
>>
>>>>>>>> if (pickedPoint!=null) {
>>>>>>>> pr.hasHit = true;
>>>>>>>> pr.point = pickedPoint;
>>>>>>>> pr.face = face;
>>>>>>>> break;
>>>>>>>> }
>>>>>>>> }
>>
>>>>>>>> pr.mesh = mesh;
>>
>>>>>>>> return pr;
>>
>>>>>>>> }
>>
>>>>>>>> The problem is I always get null.
>>
>>>>>>>> Note: Since my mesh is a child of an Objectcontainer3D
element I
>>>>> have
>>>>>>>> called mesh.applyRotations()
>>>>>>>> to make sure all the mesh faces are computed relative to
the world
>>>>>>>> coord system.
>>
>>>>>>>> I suspect there is a problem in the computation of the ray
params
>>>>>>>> (position or direction).
>>
>>>>>>>> Fabrice can you confirm that the signature of the
Ray.getIntersect
>>>>>>>> method is as follow (no info is given in the class):
>>
>>>>>>>> p0: ray direction (or is it position ?)
>>>>>>>> p1: ray position [in world coord system] (or is it
direction?)
>>>>>>>> v0: face.v0.position [in global world coord system]
>>>>>>>> v1: face.v1.position [in global world coord system]
>>>>>>>> v2: face.v2.position [in global world coord system]
>>
>>>>>>>> Thanks for any help on this.
>>
>>>>>>>> On May 4, 2:33 am, devlander <[email protected]>
wrote:
>>>>>>>>> Hello, i am having the exact same problem here.
>>
>>>>>>>>> I am trying to determine the intersection on a 3D object
at a given
>>>>>>>>> viewport location (view3D.mouseX, view3D.mouseY):
>>
>>>>>>>>> Here is my code:
>>
>>>>>>>>> private function _pickMesh(mesh:Mesh):PickingResult {
>>
>>>>>>>>> var pr:PickingResult = new PickingResult(); //
Store
>>>>> picking data
>>>>>>>>> here
>>
>>>>>>>>> var rayPos:Number3D =
>>>>> view3D.camera.unproject(view3D.mouseX,
>>>>>>>>> view3D.mouseY);
>>
>>>>>>>>> // info retreived from here
>>>>>>>>> //
>>>>> http://www.mail-archive.com/[email protected]/msg09318.html
>>
>>>>>>>>> var rayDir:Number3D = new Number3D();
>>>>>>>>> rayDir.x = pMouse.x;
>>>>>>>>> rayDir.y = pMouse.y;
>>>>>>>>> rayDir.z = pMouse.z;
>>>>>>>>> rayDir.normalize();
>>>>>>>>> rayPos.add(rayPos, _view3D.camera.position);
>>
>>>>>>>>> var ray:Ray = new Ray();
>>>>>>>>> var pickedPoint:Number3D;
>>
>>>>>>>>> for each (var face:Face in mesh.faces) {
>>
>>>>>>>>> pickedPoint = ray.getIntersect(rayPos ,
rayDir ,
>>>>> face.v0.position ,
>>>>>>>>> face.v1.position , face.v2.position);
>>
>>>>>>>>> if (pickedPoint!=null) {
>>>>>>>>> pr.hasHit = true;
>>>>>>>>> pr.point = pickedPoint;
>>>>>>>>> pr.face = face;
>>>>>>>>> break;
>>>>>>>>> }
>>>>>>>>> }
>>
>>>>>>>>> pr.mesh = mesh;
>>
>>>>>>>>> return pr;
>>
>>>>>>>>> }
>>
>>>>>>>>> The problem is I always get null.
>>
>>>>>>>>> Note: Since my mesh is a child of an Objectcontainer3D
element I
>>>>> have
>>>>>>>>> called mesh.applyRotations()
>>>>>>>>> to make sure all the mesh faces are computed relative to
the world
>>>>>>>>> coord system.
>>
>>>>>>>>> I suspect there is a problem in the computation of the
ray params
>>>>>>>>> (position or direction).
>>
>>>>>>>>> Fabrice can you confirm that the signature of the
Ray.getIntersect
>>>>>>>>> method is as follow (no info is given in the class):
>>
>>>>>>>>> p0: ray direction (or is it position ?)
>>>>>>>>> p1: ray position [in world coord system] (or is it
direction?)
>>>>>>>>> v0: face.v0.position [in global world coord system]
>>>>>>>>> v1: face.v0.position [in global world coord system]
>>>>>>>>> v2: face.v0.position [in global world coord system]
>>
>>>>>>>>> Thanks
>>
>>>>>>>>> On May 4, 12:17 am, Michael Iv <[email protected]>
wrote:
>>
>>>>>>>>>> Fabrice i am trying it already for 4 hours with Sphere ,
but it
>>>>> returns null
>>>>>>>>>> most of the time
>>
>>>>>>>>>> ///// it runs on enter frame////////////////
>>>>>>>>>> for(var i:int=0;i<_sf.faces.length;++
>>>>>>>>>> i){
>>
>>>>> trace(_ray.getIntersect(_ray.orig,_ray.dir,_sf.faces
[i].v0.position,_sf.faces[i].v1.position,_sf.faces[i].v2.position));
>>>>>>>>>> }
>>>>>>>>>> and basi
>>
>>>>>>>>>> On Tue, May 4, 2010 at 1:15 AM, Fabrice3D
<[email protected]>
>>>>> wrote:
>>>>>>>>>>> dir, origin, v0, v1, v2
>>>>>>>>>>> to check if a ray hits a triangle
>>
>>>>>>>>>>> Fabrice
>>
>>>>>>>>>>> On May 3, 2010, at 9:43 PM, Michael Iv wrote:
>>
>>>>>>>>>>> Someone can tell me what are* v0,v1,v2* in*
>>>>> Ray.getIntersect(p0:Number3D,
>>>>>>>>>>> p1:Number3D, v0:Number3D, v1:Number3D,
v2:Number3D):Number3D* ???
>>>>>>>>>>> Just can't find any example explaining these
>>
>>>>>>>>>>> Thanks!
>>
>>>>>>>>>>> --
>>>>>>>>>>> Michael Ivanov ,Programmer
>>>>>>>>>>> Neurotech Solutions Ltd.
>>>>>>>>>>> Flex|Air |3D|Unity|
>>>>>>>>>>> www.neurotechresearch.com
>>
>> ...
>>
>> read more ยป
--
Michael Ivanov ,Programmer
Neurotech Solutions Ltd.
Flex|Air |3D|Unity|
www.neurotechresearch.com
http://blog.alladvanced.net
http://www.meetup.com/GO3D-Games-Opensource-3D/
Tel:054-4962254
[email protected]
[email protected]