Search Unity

Raycasting and Faces

Discussion in 'Scripting' started by marty, Jun 21, 2006.

  1. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Is there a way that I can cast a ray at a mesh then derive from it the exact face on the mesh that the ray has hit?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Not at the moment. What exactly do you want to do? Maybe there is a better / another way to do it.
     
  3. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Heya, Joe!

    Basically, just that. I have an object that is tracking the surface of a larger mesh, using a cast ray. And, I need to know which face it is over so that I can do face-level mesh stuff with it.
     
  4. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    One thing I'd love to be able to do is change the texture of the face under the ray - sort of like being able to paint the mesh.

    If you can do that, then you can paint a heightmap and use it rebuild the mesh in realtime. That way you could make something like paint deformation in Maya.
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Thats quite doable and even if you had a Raycast faces, one probably wouldn't use use it for this.

    There are two ways to do this.
    1. Mesh based
    2. Heightmap based

    1. The way this is done usually is by getting all nearby vertices within a certain radius. Then deforming those with some fall off.

    So what you do is cast a ray, which gives you back the impact point. Then you loop through all vertices checking their distance to the impact point and ignore them if beyond the radius. Those inside of the radius you move up/down with some fall off. So vertices close to the impact point are moved more than those at the edge of the radius.

    This is kind of nice because you can deform the mesh in arbitrary directions, not only along the y axis

    2. the x-z position of the impact point tells you which pixel will be affected. Then it's just some image operations to move lighten / darken the texture in those areas.
     
  6. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Cool!

    Okay, so what if I want to change the texture on the face hit by the ray, thus allowing me to literally "paint" the mesh?
     
  7. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Same thing, you can't really do that with just a face index and uv in that face correctly anyway.

    What you really need here is to loop through all triangles and calculate for all pixels how far away the pixel (mapped to the 3d surface) is from the impact point.

    If you do it with heightmaps it gets simpler but it's really no problem to do without a face index in the raycast hit.
     
  8. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
  9. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    :eek:

    woah... didn't know zbrush ran in the web player!

    [edit... safari seems to be hanging a bit on the web player. just started playing with 1.5 today. i couldn't pinpoint what was causing it on my project. just trying this one now, i played it once and all was well. closed the window and went to the forum index. came back and launched it again it. safari crashed. Don't know much more. 733 quicksilver g4, 1gb ram, 256 radeon 9800, 10.3.9, safari 1.3.1]

    [edit 2... just tried it with mine and the one above. if i close the window, i can browse other web pages. if i come back and launch the game a second time, safari crashes. if i close the window and immediately launch the game again, safari crashes. if i quit safari after playing, whether still in the game window or after i close it and browse another site, safari crashes. sorry for getting ot and that mesh painting is so cool!]
     
  10. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
  11. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    can we get the project files for meshpaint? :wink:

    cheers.
     
  12. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    that did the trick joe. thanks!

    >>can we get the project files for meshpaint?
    Yes please - it's way cool! Way over my head though!
     
  13. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Joe, you are truly the man.

    Is there any chance you can share the code on your mesh painter though?

    Or even better, just a simple demo that just draws pixels on the surface of a mesh, eliminating all of the added complexity (doscumented elsewhere) of painting the heightmap.

    TIA
     
  14. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
  15. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Thanks, Joe!