Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

WebVR/WebGL question

Discussion in 'Unity 5 Pre-order Beta' started by lazygunn, Dec 14, 2014.

  1. lazygunn

    lazygunn

    Joined:
    Jul 24, 2011
    Posts:
    2,749
    Hey, hope this question/current progress is in the right place, but i thought it could be very useful for oculus devs if some information was available for how this might be done, or if Unity would facilitate some aspect of this in 5, even if just adding the relevant webvr boilerplate to the generated index.html

    WebVr looks really solid so far, I haven't yet had the opportunity to try using it (In one of the firefox nightlies in which it features, downloadable at mozvr.com) to send the webgl context to the HMD and control it using the input from the browser rather than the oculus unity plugin (That's coming today I think, after I get something together to produce a suitable output and get the HMD input from the browser), i don't even know if this will work

    Has this been considered? It's true the actual support for webvr, despite enjoying cooperation between both firefox and chrome devs, is limited to nightly builds of these browsers, it would be very enthusing to feel Unity have something in mind

    If this is a repeat post (I didnt find anything in search), apologies
     
    Smarties likes this.
  2. lazygunn

    lazygunn

    Joined:
    Jul 24, 2011
    Posts:
    2,749
    I actually just shut up and had a go instead, and it was a lot easier than i expected. So i'm not sure wether it's Unity's responsibility. I suppose it could be an easy option to add maybe when webvr is presumably moved to the main firefox and chrome branches. Cheers anyway
     
  3. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    Using the normal standalone Oculus plugin would not be possible in Unity WebGL, so the only way to support VR in WebGL would indeed be through WebVR.

    So, we currently don't have support for WebVR in Unity WebGL. You could possibly build a JavaScript plugin to add support yourself, though (I have not tried this yet).

    Adding support is probably not difficult, so I think it is likely that we will do so at some point in the future. For 5.0, though, our goal is to stabilize the release, we will not add any new features at this point.
     
  4. lazygunn

    lazygunn

    Joined:
    Jul 24, 2011
    Posts:
    2,749
    Don't worry, i really think it may be not too difficult at all, i'll post changes needed here if you like, when i'm done. I'm hanging onto the oculus implementation for some of the features but removing the need for the dlls and replacing with more suitable alternatives. Hopefully it will be useable. With any luck it'll all be done before the day is out
     
  5. lazygunn

    lazygunn

    Joined:
    Jul 24, 2011
    Posts:
    2,749
    Well it took a fair bit more than that day, but yes it's entirely doable
     
  6. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    Nice. Care to share what you did and how (or at least an overview if you don't want to share code)? I'm sure other people would be very interested in this as well.
     
  7. lazygunn

    lazygunn

    Joined:
    Jul 24, 2011
    Posts:
    2,749
    Ahh yeah! I was intending to do just that, but my code's a jumble as usual and i guess there's a few folk who might get grumpy but its not their work, so hey

    I'll figure out some key points and if you can forgive the pretty backwards code, some scripts that might make it quite clear, i already know others could do a far superior effort to my own

    I think if you can give me a while, i can finish using my implementation for what I intended it for and avoid making people grumpy, and have a pretty smashing demo going for it, and i can give out some cogent help

    I can i think give a few pointers though - i used 2 apis really, the WebVR API, which is in particular firefox nightlies http://mozvr.com can help, with links to the firefox nightlys and a github with most the information you need to get it all going, although chrome are being lively too. Secondly I used the Gamepad API for a controller, i didnt implement it very gracefully,but with both this and webvr you can much identify what you'll need to run on the page, which is generally the eventlisteners and such that will detect your hardware, and what would be more suited bringing into the unity creation

    In terms of getting things into the build, and maybe this is something that could be extended upon in future because while im sure there are more performant and more elegant ways to do it, my go at it felt crude. I made a function on the page js that would bring together any useful values exposed by the apis based on their role. So i had a function that contained the values to initialise a vr camera rig, one that grabbed all the output of the sensor device and so on

    You can execute that function from within your creation using Application.ExternalCall(function here) and then from the called js function, send desired information using SendMessage(GameObject, Function, string). This did get a bit messy as there's sometimes a lot of values to move and only one string, well it just makes for a nice long strong you can punctuate with delimeters to help string.Split it back into its constituent parts back in Unity. Thats how i did it anyways, i'd like to hear of alternatives for sure.

    In terms of setting up in Unity, i spent quite a long time trying to follow the example code involving matrices etc etc and in the end i gave up and saw what Unity could do instead. All you need for a suitable camera setup say, for a first person controller, is the controller, a child gameobject to act like a 'head', this will be oriented by the hmd.sensor information beamed in via my clumsy communication channel, the values for the Quaternion map nearly 1 to 1, or are easily fixable with tweaks

    Position scanning isnt any more complicated, just keep the 'head' working around local transform

    To the head you can add two cameras, have their rects set up so they take half the viewport each, and place them directly together, in the center fore of the head. The initialisation values webvr supplies include a suitable eye offset for each eye based im assuming on a default ipd or the one saved in your oculus service profile

    I'm getting a bit scatterbrained atm because tired so i'm sorry if this doesnt help much, you're welcome to ask questions and if noone else has gotten to it by the time ive done it, i should be able to offer some code and more experienced insight and hopefully quite a surprising thing to come out of a browser

    Hope that helps a bit for now

    Edit: Ahh i just remembered a fairly helpful other thing - webvr will distort each eye view automatically, you dont have to do it yourself, and this can save a bit of hassle
     
    lmbarns, jashan and jonas-echterhoff like this.
  8. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    Thanks. FWIW, it will probably be cleaner and simpler to use a jslib JavaScript plugin instead of ExternalCall to integrate the JS. See the unity 5 webgl docs for more info.
     
  9. lazygunn

    lazygunn

    Joined:
    Jul 24, 2011
    Posts:
    2,749
    Ahh yep i did have a look at that, it was a bit of a swing between the two approaches but i just went the way i did because i needed to get it it to this kind of stage quickly without deliberating too long about two approaches i'm equally clueless about (it works and now tech people cant complain). Do you have any insight into the relative performance of each of the approaches?

    Obviously the platforms very early on and will develop over time but when simple changes can have really dramatic results with it at the moment, a travellers guide would be great, if anyone ends up blogging about it

    Things can get pretty.. random with webGL, i've been finding, with some small changes having dramatic effects on performance, good and bad, it'll definitely be nice to have an idea of the best approach to something like the package/page communication for example, but really quite a few others where the platform has expected idiosyncracies compared to working in what feels a more stable environment and optimisation is long established.

    I think the webgl move has been one of the neatest things Unity has done btw, very much enjoying it, and i think the quality of the stuff that people will be seeing on it will explode quite soon. Dont get me started on a streamlined browser deliverable VR thigamabob a bit down the line.

    EDIT: I imagine this might possibly be already on the cards, but being able to have your own js scripts added to the Unity generated when you build would be pretty useful, if that makes sense
     
  10. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    Performance is not so much the question here (unless you are calling the JS functions thousands of times a frame, then jslib would be faster then Application.ExternalCall). It's more about having a tighter and simpler integration.

    Actually, this is exactly what the jslib approach does (you add js files to your project, and they will be merged into the generated js output).
     
    Last edited: Dec 17, 2014
    lazygunn likes this.
  11. lazygunn

    lazygunn

    Joined:
    Jul 24, 2011
    Posts:
    2,749
    That was extremely illuminating, cheers! I think after a sleep i'll look more into the jslib approach, it seems to be the answer to most of my frustrations, thanks again

    Eit: In fact if it's as tidy as i imagine it could be, i dont think i'd mind putting together something that might help with webvr,with any hope it'd make it all quite agreeable.

    It may be something i'll have to write helpful stuff about anyways, so i'll keep it in mind over the next few weeks, assuming someone else doesnt step up
     
    Last edited: Dec 17, 2014
  12. lazygunn

    lazygunn

    Joined:
    Jul 24, 2011
    Posts:
    2,749
    Hello, just a heads up to say that if you randomly disconnect your gamepad using firefox in webgl, it bails with an exception pointing to SDL.queryJoystick, which I found in the very long first line of the build js. Not a problem with chrome however. I've also found chrome to be significantly smoother than firefox at equivalent things, i'm hitting 'vr speeds' as in 75fps+ in chrome (not that webvr currently uses the 75hz standard yet, it's still set at 60hz) in slowly-becoming-quite-heavy circumstances where firefox can struggle a bit, i dont know if this is down to chrome generally being stronger with webgl than firefox in my experience, with my hardware

    I've had a look at the jslib features and i suppose i'm hamstrung a little by having very little idea how to refactor some js to suit use in a jslib, which still leaves me putting at least a single additional .js in the index.html. Having the script there also means i can play around with some settings/variables via the js quickly and conveniently without needing a rebuild, so i suppose if it were possible, or if it already exists, a way to conveniently add custom script files to the html at build it would avoid a not-awful but quite inconvenient fiddle after a build, which is definitely good as the build times are lengthy and often, as i think there remains a reliance on externally provided features (libraries and apis) to get the webgl build to show its strengths. This is not a bad thing however imo, unity's webgl i think has an enormous amount of material it can use to augment the experience, already provided by the multitude of generous javascript developers, and i suppose the growing apis of browsers

    And i think i got a green light on going ahead and doing something dead fancy with the webgl/webvr combination and if it goes well i'll def post the results here and provide some example code to get the thing running early in january. I might get a little evangelistic here and i wish i could share what i'm up to, because unity has taken webvr to a point orders of magnitude richer than anything that's shown up in webvr so far and it's just one of those things where you want to show people 'look at this!'

    The graphics being based around ogles 2 has not proven a restriction either, with very pleasing results gotten, and it's quite interesting working with it without having been restricted by the shortcomings of mobile processing ability