1 of 48

Multiplayer WebVR

WebGL, WebVR, WebRTC - let’s do this!

@g33konaut

2 of 48

How it began...

@g33konaut

3 of 48

How it began...

@g33konaut

4 of 48

So… what’s this about?

@g33konaut

5 of 48

@g33konaut

6 of 48

@g33konaut

7 of 48

@g33konaut

8 of 48

Demo time

bit.ly/muchvr

@g33konaut

9 of 48

@g33konaut

10 of 48

Ingredients

  1. interactive 3D content in the browser: WebGL
  2. VR rendering & controls in the browser: WebVR
  3. High speed networking in the browser: WebRTC

@g33konaut

11 of 48

  1. WebGL

@g33konaut

12 of 48

WebGL: Vertices

@g33konaut

13 of 48

WebGL: Faces

@g33konaut

14 of 48

WebGL: TypedArrays

[ , , , ]

[ , ]

@g33konaut

15 of 48

WebGL: Pipeline

@g33konaut

16 of 48

Three.js as an abstraction on top

var scene = new THREE.Scene(),

camera = new THREE.PerspectiveCamera(...),

renderer = new THREE.WebGLRenderer()

var box = new THREE.Mesh(...)

scene.add(box)

renderer.render(scene, camera) // rinse, repeat

@g33konaut

17 of 48

@g33konaut

18 of 48

@g33konaut

19 of 48

@g33konaut

20 of 48

@g33konaut

21 of 48

WebVR with Three.js

var vrEffect = new THREE.VREffect(renderer),

vrControls = new THREE.VRControls()

vrControls.update()

vrEffect.render(scene, camera)

@g33konaut

22 of 48

Why is it so awesome?

  • Cardboard & Oculus - same WebVR APIs
  • Firefox Nightly & experimental Chromium build
  • Low cost to get into it, great community behind it
  • allows you to reach people on a much deeper level

@g33konaut

23 of 48

2. WebRTC

@g33konaut

24 of 48

Bye, servers!

@g33konaut

25 of 48

LOL NOPE - we need signalling

@g33konaut

26 of 48

Oh, and the usual suspects...

@g33konaut

27 of 48

Meh

@g33konaut

28 of 48

Show me the options!

WebRTC PeerConnection + DataChannel

TCP/UDP/SCTP, reliable or unreliable

Websockets

TCP-like, reliable

@g33konaut

29 of 48

Show me the options!

WebRTC PeerConnection + DataChannel

TCP/UDP/SCTP, reliable or unreliable

Websockets

TCP-like, reliable

@g33konaut

30 of 48

Ok, let’s talk numbers

Per player: position + orientation

@g33konaut

31 of 48

Ok, let’s talk numbers

Per player: x, y, z + pitch, roll, yaw

example: 150.384, 10.2, -95.8, 1.53873, 2.63980, 1.87532

Hey, that’s in fact 6 Float32 values.

6 * 32 bit = 192 bit * 60 fps = 11,5 kbps

@g33konaut

32 of 48

@g33konaut

33 of 48

How about speed?

Websocket: ~150ms

WebRTC: ~10ms

@g33konaut

34 of 48

Ahem...

@g33konaut

35 of 48

...well...

@g33konaut

36 of 48

Ingredients, revisited

  • interactive 3D content in the browser: WebGL
  • VR rendering & controls in the browser: WebVR
  • High speed networking in the browser: Websockets

@g33konaut

37 of 48

The server

@g33konaut

38 of 48

Our server

  1. manages a list of players & their state
  2. broadcasts player’s messages

Needs these message types:

Type (binary)

00

01

10

11

Meaning

Player moving

Unused

Player connect

Player disconnect

Payload

Position / Rotation

Unused

Position / Rotation

-

@g33konaut

39 of 48

The protocol

  • should be binary
  • as small as possible

Header

Uint8

2 bit type�6 bit Player ID

Position

Float32[3]

X,Y,Z

Rotation

Float32[3]

X,Y,Z

@g33konaut

40 of 48

The overhead

ws-frame = frame-fin ; 1 bit in length� frame-rsv1 ; 1 bit in length� frame-rsv2 ; 1 bit in length� frame-rsv3 ; 1 bit in length� frame-opcode ; 4 bits in length� frame-masked ; 1 bit in length� frame-payload-length ; either 7, 7+16 or 7+64 bits in length� frame-payload-data ; n*8 bits in length, where n >= 0

Our packet = 216 bit (27 byte) in total

@g33konaut

41 of 48

CODE

@g33konaut

42 of 48

The Client

@g33konaut

43 of 48

The client...

  1. sets up the game world & game state
  2. runs the 3D rendering
  3. receives messages from the server about other players
  4. sends messages to the server about player movement

@g33konaut

44 of 48

CODE

@g33konaut

45 of 48

We did it!

@g33konaut

46 of 48

So what’s next?

@g33konaut

47 of 48

Next steps

  • some game mechanics
  • Gamepad API
  • support for LEAPMotion / Myo

@g33konaut

48 of 48

Thanks all folks!

@g33konaut