Search Unity

3D Connexion devices in Unity!!

Discussion in 'Made With Unity' started by HarvesteR, Mar 24, 2011.

  1. HarvesteR

    HarvesteR

    Joined:
    May 22, 2009
    Posts:
    531
    Hello,

    I've been mooching off the cool stuff on this forum for a good while now, so I thought I'd give something back... (granted, it's not a lot, but it's a start)

    Recently I bought myself a SpaceNavigator, which is a 3D mouse... actually, "mouse" isn't the proper word for it... it's a spatial navigation device, so you can view a 3D space without having to break your conventional mouse down (true story).

    I was dissapointed to learn, though, that Unity doesn't support these devices... which is really a shame...
    So, one day I decided to hack up some way to connect it to Unity.

    So here it is. This is a script for GlovePIE, a freeware programmable input emulator. What it does is basically read any imaginable input device and, through scripting, spit out any other form of emulated input device....

    This means GlovePIE can read the SpaceNavigator, and emulate the keyboard and mouse actions needed to pan/rotate/zoom the viewport in Unity.

    So, you can grab GlovePIE 0.43 here... install it, and run the attached script. It should work right off (assuming of course, that Unity is open)...

    I was going to attach the script file in the post, but the forums don't seem to like .PIE files... so I'm just writing it down here. Just copy it into GlovePIE, and run it. (You might also want to save it at some point ;) )

    Code (csharp):
    1.  
    2. // SpaceNavigator in Unity - By Felipe "HarvesteR" Falanghe, 2011
    3. //
    4. // Basically it just detects when the "spaceball" is deflected
    5. // and does the keyboard/mouse equivalent so unity will get it.
    6.  
    7. if (math.abs(spaceball.x) > 10 or math.abs(spaceball.y) > 10) then
    8.    if (!var.pan) then
    9.  
    10.     Press(keyboard.leftAlt)
    11.     Press(mouse.middleButton)
    12.     mouse.SwallowMovement = true
    13.     var.pan = true
    14.    endif
    15.  
    16.    Mouse.DirectInputX += spaceball.x * 0.1
    17.    Mouse.DirectInputY -= spaceball.y * 0.1
    18.  
    19. else
    20.    if(var.pan) then
    21.  
    22.       Release(mouse.middleButton)
    23.       Release(keyboard.leftAlt)
    24.       mouse.SwallowMovement = false
    25.       Mouse.CursorPosX = screen.DesktopWidth / 2
    26.       Mouse.CursorPosY = screen.DesktopHeight / 2
    27.  
    28.       var.pan = false
    29.     endif
    30. endif
    31.  
    32. if (math.abs(spaceball.z) > 10) then
    33.    if (!var.zoom) then
    34.  
    35.     Press(keyboard.leftAlt)
    36.     Press(mouse.rightButton)
    37.     mouse.SwallowMovement = true
    38.     var.zoom = true
    39.    endif
    40.  
    41.    Mouse.DirectInputY -= spaceball.z * 0.1
    42.  
    43. else
    44.    if(var.zoom) then
    45.  
    46.       Release(mouse.rightButton)
    47.       Release(keyboard.leftAlt)
    48.       mouse.SwallowMovement = false
    49.       var.zoom = false
    50.  
    51.       Mouse.CursorPosX = screen.DesktopWidth / 2
    52.       Mouse.CursorPosY = screen.DesktopHeight / 2
    53.     endif
    54. endif
    55.  
    56. if (math.abs(spaceball.yaw) > 50 or math.abs(spaceball.pitch) > 50) then
    57.    if (!var.orbit) then
    58.  
    59.     Press(keyboard.leftAlt)
    60.     Press(mouse.leftButton)
    61.     mouse.SwallowMovement = true
    62.     var.orbit = true
    63.    endif
    64.  
    65.    Mouse.DirectInputX += spaceball.yaw * 0.002
    66.    Mouse.DirectInputY += spaceball.pitch * 0.001
    67.  
    68. else
    69.    if(var.orbit) then
    70.  
    71.       Release(mouse.leftButton)
    72.       Release(keyboard.leftAlt)
    73.       mouse.SwallowMovement = false
    74.       var.orbit = false
    75.  
    76.       Mouse.CursorPosX = screen.DesktopWidth / 2
    77.       Mouse.CursorPosY = screen.DesktopHeight / 2
    78.     endif
    79. endif
    80.  
    81. keyboard.f = spaceball.Fit
    82.  
    83.  

    Some tips to improve usability... This works best if there is a 3DxWare profile set for it:
    - Enable 'Dominant Axis' on the profile
    - Set the zoom axis to Z (fore/aft movement, instead of up/down)
    - Set the overall sensitivity to about 40%
    - Set one of the buttons to 'Fit' (will work as 'F' key in Unity)

    Or just tweak away and have fun!!

    A few things to mind:
    - Since the script is emulating keyboard/mouse actions, there is no way to enable multiple inputs at once, like panning and orbiting at the same time... That's why 'Dominant Axis' should be on.
    - I set the rotational motions for orbiting the camera around the focus... one could also have it look around, but not both at once... I much prefer it whis way though...
    - The script must be running at all times for this to work... you can configure GlovePIE to launch itself, run the profile, minimize to system tray, and launch unity for you if you want... just launch GlovePIE with the -help or -? (can't remember which) parameter to get a list of commandlines.


    Anyways, Enjoy! (and if you hack up something even better, let us know :) )

    Cheers
     
    Last edited: Mar 24, 2011
  2. cemC

    cemC

    Joined:
    Dec 23, 2010
    Posts:
    214
    Good work :)
     
  3. PatHightree

    PatHightree

    Joined:
    Aug 18, 2009
    Posts:
    297
    Well done mate !
    I've been looking for a way to move the scene camera with a space navigator, but its just not exposed in any api...
    Emulating the movement with key/mouse input is a great idea.
     
  4. Euro3d

    Euro3d

    Joined:
    Oct 25, 2011
    Posts:
    11
    AWESOME job thx!!!