Wednesday, March 14, 2012

from my cold, dead, hands...

        Well, ok maybe not quite that dark, but at least I look somewhat civilized when I'm using a Kinect, unlike well...


        Gah, ok ok, I promised myself i'd stop talking smack about former large software companies i may or may not have worked for.  I dunno it's weird, if you're like me, you always feel like once you leave a company in bad blood, you're forever professionally competing with them as long as you're in the same industry.  Not to put too fine a point on anything, or anything, hehe...

        Aaaanyway, so i feel like i'm at a good enough spot with KinectCam to drop some knowledge on some folks if you're interested in playing with this stuff yourself.  I gotta say I might have written Kinect off a bit early, I'm having a good time playing around with it and its ilk.  Granted, none of the games I've ever played on Kinect are doing the sort of stuff I've been doing, I sorta wonder what kind of crazy game you could come up with using the depth stream...Ok, sure, i'm just reaching there, but probably not, I bet someone (Jeff Minter) could come up with some insane depth stream game.  Actually I'm thinking of some sort of pattern recog app that i can feed my CD collection into or maybe just stuff the audio array, you know anything to avoid this sort of thing (Hmm, this would be funnier if you didn't have to squint for the text):

MLFail

        Alright, well, mindless self indulgence aside, let's get to this MS Kinect SDK wrapper.  Odd how this will probably be the only such post I make that's going to apply to this particular wrapper, therein crawling another bug up my nose, the fact that I wish MS would just release an official Unity wrapper/plugin/whatever.  Truth be told though, it probably wouldn't be terribly hard to compile down my own DLL, it's probably more of a time issue to make sure I expose and marshall everything properly, not to mention proper SDK.  But SDK specifics aside, all the maths herein should be pretty easy to re-apply since it's just a few simple space conversion tricks.

        So recap from my previous post, make sure you install the following softwarez:
        I've alluded to the camera control thing for a while, so let's just dive right in and get that taken care of, then I'll do a separate post about the Kinect Wrapper guts.  Again, that probably won't be terribly interesting, mainly it's just for me.

        Alright, so to get stuff working in this environment is pretty simple.  Import the KinectWrapperPackage and you'll have all the scripts and objects you need to get started.  I recommend just starting with a blank scene and build up for there, altho you could pop open the KinectExample scene and make blobs dance around wildly.

        Once you have a new scene (taxing task that that is), all that's required to make it Kinect-aware is to drag a Kinect_Prefab from the Project into the scene, as so:

kinectPrefabSetup

        This scene doesn't actually do anything yet, obviously, but you can use the Kinect_Prefab to setup your camera.  If you play the scene, you may notice the camera moving around a bit and focusing in odd places (hey buddy, my eyes are UP HERE), which can be fixed by setting some values in the KinectSensor attached to the Kinect_Prefab:

kinectSensorSetup

       I've been using the included Skeletal Viewer in the SDK to make sure i'm getting good coverage, once you find some numbers that work, you can always go into the KinectSensor code and set your own defaults if you want.

       Alrighty then, time to get the camera moving.  So, I went the hack route and used Unity's built-in MouseLook script as a jumping off point, as it only required a few minor changes to the camera transform code.  The part that requires a bit of work is getting Kinect skeletal numbers into Unity friendly numbers, and when i say "bit", i mean it, it's actually not that hard.  So the first thing I did was for my own sanity, and that's setup a little GUI indicator that tells me if I'm tracking or not.  Polling the camera for the depth image in this wrapper is a bit annoying because you have to restart Unity every time you stop running.  Might be a fun project to figure that out and re-implement depth and color display as a GUI texture.  Something to keep in mind for my next project.  Anyway, so verifying tracking is pretty simple, i put this all in a script called FromBonePosition:


        Pretty straightforward, we're just pulling some functionality from the included SkeletonWrapper class, which...well, wraps Skeleton functionality.  Truth be told, I feel like this class is a bit of an extra, but whatever.  Anyway, breaking down the code:
  • Really all pollSkeleton() does is call the native NuiSkeletonGetNextFrame(), i'll leave what that probably does as an exercise to the reader.
  • trackedPlayers[] is interesting.  The details of how we get there are a bit unimportant at this stage, the important note is that we're grabbing an enum that tells us what we're tracking.  KinectWrapper initializes the values in trackedPlayers[] to -1 which indicates that we haven't even acquired a skeleton.  The possible enums are NUI_SKELETON_NOT_TRACKED, NUI_SKELETON_POSITION_ONLY, and NUI_SKELETON_TRACKED.  So if either of the two players is acquired, we can say we've tracked.
  • So once we've tracked, we spit out a string to the UI that tells us what's up.  
        Easy-peasy, yeah?  Well fear not, most of the Kinect polling is that straightforward, it's just flipping the numbers that takes some code.  So let's continue by polling the Kinect for a bone position, which we'll use to drive our camera.  Yeah, yeah, i know, classic Kinect "Hello, World!", recreate a mouse driver, but it's an easy place to start.

        Joints are pretty easy to poll, as the SkeletonWrapper class provides a few different arrays that contain joint information and the SDK provides us a convenient enum for specifying which joint to query for data.  So let's add some code to our class:


        Simple, efficient (like the body itself), we add a variable to hold the right hand's position, then we pull a value from the bonePos[] array on the SkeletonWrapper.  You'll notice bonePos[] is a 2D array, the first id is which skeleton to poll (we might be tracking two players), the second id is pretty obviously the joint we want to get the position for.  You can see the full enum definition in the KinectInterop class if you should ever want to poll a different joint, or if you want to update a bunch of joints, or a whole skeleton, or make tea, or something like that.  We output that number to the GUI too, a) for verification purposes and b) so we can set some other numbers later.

       By now we pretty much know all the kinect stuff we need to know to get our camera moving, so let's get our camera look on.  Here's our member list (with comments!):


        And now...Maths!  The basic theory behind what we're doing is pretty simple:
  • Find the distance (as a percent) between the minimum and maximum x (oldX) that we get from the Kinect
  • Repeat for y
  • Cast that into (-1,1) so it mimics Unity's mouse input values
  • Rotate the camera
  • Profit, or at least impress your friends (or your mom)
        This is pretty simple, we can use some of the Unity built-ins to do all the scary maths for us, i'll just post the relevant code here:

       
        So yeah, we're basically parroting MouseLook's rotation setting and replacing Input.GetAxis() with the data we poll from the Kinect.  This SHOULD work, as it's almost verbatim the code I was using minus things like aim assist and project specific passing.  Play around with the OldX and OldY values, the Kinect SDK says that values for X can lie between (-2.2, 2.2) and Y can be (-1.6,1.6), but that's going to vary a bit depending on where you're hanging out.  For example, the values we ended up using were OldX = (-0.3,0.7) and OldY = (1.3,2.4).  Drop a texture onto the gui and set the rectangle based on hand position, that's a pretty simple way to get some good debug.  It's not too tricky to cast your value into screen space, try it out!
        I think that's all I got for now, going to try and port this to OpenNI/PrimeSense just for fun this weekend too.

Sunday, March 11, 2012

to the buttery depth(array)s

UPDATE(20120313):
{
You also need this plugin as well.  If you notice Unity throwing DLLNotFoundExceptions and grousing about MSRKINECTNUI.DLL, you probably don't have it installed.

KinectPlugin.zip
}
        Man, I wish I could talk about all the awesome stuff I saw at GDC, but sadly, most of my GDC was prepping for my talks, running around organizing events, some modicum of time lost commuting from San Jose to San Francisco, and trying not to suffer a total breakdown overall.  Nah, I make it sound worse than it really was, i think the most taxing part of the conference had...not much to do with the actual conference, tho it was def tangential.  As much as I'd like to whine about it here, that's another post for another blog for a much more inebriated state of mind.  I should have my GDC slides up in the very near future, so stay tuned.  I may do some highlights here or something like that.

        What I can show you is these awesome pictures from some of the Tech Art related events.  The trend of Tech Art presence of GDC escalating continued with 2012 in spades, as the photos from Boot Camp and Decompression can attest to:








WP_000293
Boot Camp about to get underway, another great crowd!

WP_000301
Decompressing after the show Friday...

WP_000298
...started with about 15 people back in 2010(?)...

WP_000302
...logged about 50 in 2012.  Not bad!

        But now it's back to the real world and demo crunch, although I have to admit this hasn't been a horrible demo crunch at all, and in fact I can probably blame my own poor scheduling.  Admittedly, I can also blame the fact that I didn't think the problem through and didn't write my own Kinect wrapper back when I started.  Yeah yeah, there are a few out there, but I'm really not sold on any of them.  I used the zigFu one for a bit, but I think i'm philosophically opposed to using that one.  It crashes Unity, it's a known bug, and it seems the answer is "It'll be fixed when our commercial version comes out".  I appreciate everyone needing to make a buck but...no.

        So we decided to take a look at some of the MS SDK based solutions, despite the Microsoft stuff seeming to be a mixed bag just because it feels like there's been so much splintering between SDK releases.  I did find a version from the good folks at Carnegie Mellon's Entertainment Technology Center (ETC), an organization at which I've had the pleasure of speaking, as well as the opportunity to work with many of alums.  Having failed miserably to get a hand tracking based solution working, we decided to focus a bit on skeletal tracking instead.  Now, I feel more and more like this was a failed design decision more than a fail of technology, but at this point, I'll try anything once. Just need to get something usable by Tuesday.

        Quick aside, I really feel at this point like it's my duty to write a good wrapper for Kinect to Unity.  I feel like all the ones that have been made available to date have been written by people who have more experience using SDKs and less experience developing SDKs.  This could just be the fact that there hasn't been alot of feedback, but anyway, I digress...

        So head over to the MS Kinect - MS SDK page at ETC's Unity3d web and grab a few files.  You'll need at minumum:

  • Microsoft Kinect SDK - Grab the one on the page, as this is a beta release.  From what I've heard, the projects don't work with the official release.  Also, i've only loosely verified this, but the drivers in this package only work with XBox 360 Kinects.
  • Kinect Wrapper Example Project - Basic stuff to get you going without too much other overhead
  • DirectX June 2010 SDK - Some of the samples in the Kinect SDK require this.  If you get a redistributable error on installing (s1023 or something like that), uninstall all VS2010 redistributables later than 30139 and try again.  Should be good to go.
  • DirectX End-User Runtime - I believe some of the Unity samples require this

        Install everything in no particular order, plug your Kinect in, and you'll be good to go.  Windows Update might toss you some noise about looking for updated drivers (because like everything MS, it thinks you're stupid), but that probably isn't too big a deal.  Next installment we'll talk about some of the flow in and out of the Unity Kinect wrapper...Fun fun!  I'm using this mainly as a way to step through some key bits of the Nui namespace in prep for more kinect/depth camera work coming up.  I'm working on the final kinect camera driver the next two days, so I'll finally be able to toss that up as part of the wrapper exploration.  It's a fairly simple jumping off point so...anyway.  Sleep comes.



Friday, February 24, 2012

sound advice

        Hey gimme a break, i went to bed at like 1 last night and i've been in a bit of crunch this week.  I keep saying that one of these days I'm going to start a job during a project that's NOT in crunch, but you know, it's a fun crunch.  Had the first dog-n-pony with the execs and I gotta say our team is working on some cool shit.  We got to postpone our demo for a week, which is good because I've only been in the code for about a week and a half, and the scope of our demo is pretty impressive.  I mean, not really, but given the time we've had to put it together...

Photobucket
It seemed like a manageable project at the time..!

        So I'm still working on the Kinect camera tutorial, I'm actually going to go in and tighten up the implementation, along with the graphics on level 3, so I'll have a bit of a better presentation.  It's a bit sad that I didn't have more time to really come up with some cool camera control, was hoping to do a little more than just build a mouse driver type thing, but you know, it's a good starting point.  I feel like the implementation I came up with is pretty solid, it's a good hybrid of a few different paradigms that make good sense.  Anyway, hopefully that's a fairly tantalizing preview.  I'll go over the basics of how to setup the zigFu/OpenNI stuff too so you can further hack away on Kinect.  If you haven't messed around with it yet, you should, I'm surprised it took me this long to get into it.


Thompson Eye Phone and Sogo-7s optional...

        Alright, so to some possibly meatier content, although I may be the only person who's run into this issue.  If that's the case, you guys are all jerks for not posting solutions!!  One of the things we've been beating our heads against somewhat for the last couple days is how to manage sound, in particular sound on events, such as collision.  Now, that's a fairly straightforward problem that you could solve in a few different ways, but here are some ways NOT to solve it:
  • The rather naive and seemingly obvious audio.Play() in Update(), it only seems obvious at very first glance
  • Immediately prior to a seemingly obvious Destroy() call.  Again, it only seems obvious at very first glance
        So, the first method we came up with was a bit brute force I'll admit, but if you know me, that probably comes as no surprise.  We setup a GameObject that's basically an audio container, so actually it's a GameObject and a script that exposes a bunch of public AudioClips.  You probably see where I'm going with this, if you guessed AudioSource.PlayOneShot() or AudioSource.PlayClipAtPoint(), pour yourself an expensive single malt shot.  For some reason, this made the audio sound really wonky, and it wasn't a 3d issue or a playing multiple clips issue.  We could have wrapped either one of these to get a bit more control over the AudioSource, but I felt like that was probably overkill.  For a larger project, I could see the benefit of this though, and it's definitely something I'll be exploring more.  I have a ton of questions about this pattern, performance issues mainly.


Brute Force? Heh, more like ME Force, amirite??

        The second method is about as straightforward, but I had some perf concerns again.  It's funny how I'm still in game developer mode and trying to make the transition to blue sky R&D developer mode.  Performance? Hah, we care not for your framerates and memory budgets!!  Or something...But yeah, basically we ended up attaching multiple AudioSources to the prefab and managing it in script.  The code was pretty simple, something to this effect:


        Obviously this is super naive and you'd want to do a bunch of other checks, but you get the idea, it's sketch code, whadyawant? I'm not very sanguine on this method largely due to the multiple AudioSources on each prefab, again, perf concerns. I may just be paranoid still...

        The solution we ended up going with is not ideal but serves as a bit of a springboard for a pattern I might have used if i had been thinking about it.  For each collision behavior, we exposed an AudioClip for that behavior, then switched out the object's AudioClip in the OnCollisionEnter() based on the name of the colliding object, i.e. We have an object in the world and based on the projectile's name, we swap out the AudioClip and play the AudioSource.  Again, i don't feel like this is super ideal because then we have assets all over the place, but it definitely made life easier for me because I could just concentrate on a behavior at a time. So we're looking at something like this:


        Again, super-naive, there are better ways to do the specifics, but you get the idea.  This seems to be working for now, so i'm going to just STFG and keep in mind what I've learned.

looks-good-to-me-ship-it

        I think, in retro, I would have gone with some method that was a hybrid of all of these, maybe have an audio manager type script attached to the prefab that exposed all the AudioClips i wanted to play then have each collision behavior poke into the audio manager.  I like the idea of keeping collision behaviors as separate script just because i hate looking at big scripts that manage everything.  That could just be me, honestly, I ain't to guud at readin teh codez, not bein a real programmer and all...

        Thoughts?  I'd love to hear from anyone else about how you tackle this sort of thing.  At some point i want to go back into library/SDK developer mode and start wrapping up a bunch of functionality like this for future Unity projects...

Saturday, February 18, 2012

skipping the obligatory intro post, let's dive right in

        So i've been getting my hands dirty with Unity again in a pretty big way.  Seriously, now that i've left the games industry, i'm actually making...well game-ish type things again, and somewhat having fun in the process.  Oh who am i kidding, this is probably the most fun I've had in the last 5 years or so.  Can't put too fine a point on what I'm doing at the new gig, but suffice it to say I'm doing some interaction design stuff that draws heavily on my experience in game development.  It's fun.

        But I'm not here to tantalize or be faux subversive in a rather clumsily veiled attempt to get people to ask me for more information.  Rather, I think i'm going to use this space to share random tips I come across during my game-ish interaction prototype development experience.  Probably some Unity randomness, tons of Maya randomness, and who knows, maybe some of that Python stuff i seem to love so much.

        Alright then, as the title states, let's dive right in.  One of the really interesting things I came across in the Unity reference manual:

"The most convenient way for animators to work is to have a single model containing all animations."

        Hmm...my 10+ years supporting animators in various degrees tells me otherwise, so of course I setup my character with individual animation files.  No referencing or anything pretty like that, this is a pretty quick n dirty project, so no infrastructure or pipe really.  We'll make that up next project, but that's for another post.  Anyway...

        If you're familiar with Unity, you may be aware that when you import an animation, it comes in as a Read-Only asset.  In short, it means you can't add animation events or otherwise edit the animation in Unity.  After a few hours, well ok, maybe only one, I found the accepted workaround, which is to duplicate the animation clip and thereby creating a new animation clip that's writeable.  I quickly found out that many of the explanations i found online where contingent on creating animation the Unity approved way, i.e. one long animation file.  That gives you an asset with a bunch of animation clips as children, like so:


         With this layout, it's pretty easy to break the Read-Only state, you simply select the clip and Edit > Duplicate or Ctrl+D and you're good to go.  The toolflow issues arise when you want to make an animation clip Read-Only and the animation is stored in a separate file.  In that case, you get an asset that looks something like this:


        In this case, you have actually have a bunch of individual assets that Unity collates into a single-ish asset.  The question now is how do you duplicate one of these to break the Read-Only state?  Well, selecting the actual asset and duplicating doesn't do it, it just creates another Read-Only animation entry.  We can attempt to duplicate the entry in the Inspector:


       ...But that just gives us the same end result:(  So what's a bear to do??  Good question, the secret actually lies in the animation asset's hierarchy.  If we expand one of the animation assets, we get all sorts of fun things:


        If we look down near the end of the animation asset's children, we find what we seek, the actual animation clip itself.  THAT'S what we want to duplicate, so we select that and Edit > Duplicate or Ctrl+D.  Now we have a discreet instance of the animation clip:


        Now we just have to drag this instance into the appropriate slot in the character's Animation block in the Inspector, and we're good to go:


        Now we can add Animation Events to our heart's content.  Keep in mind we lose all this data when we reimport, so it's probably a good idea to come up with some sort of automated process for this.  Good case for some sort of metadata system...Maybe we'll talk about that at some point, I do need to learn Unity Editor Scripting, but honestly, I have no idea when I'm going to get back to building big systems again.  So anyway, that's what I got for now, like i said, it's a pretty specific case, but I imagine i can't be the only person who's ever run into this.  Hopefully this helps.

        Hope someone found this useful, next up, some thoughts on SoftKinetic and the iisu SDK...Stay Tuned!