Top Banner
WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming Ashraf Samy Hegab MultiPlay.io
27

TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

May 12, 2015

Download

Technology

Slides used for the Tizen Developer Conference 2013 session on 3d multiplayer gaming.
Excludes source code walkthroughs and demos presented in the talk.
More info: http://multiplay.io
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

WebGL & WebSockets

for 3D Multi-Platform

Multiplayer Gaming

Ashraf Samy Hegab

MultiPlay.io

Page 2: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

2

3D Multiplayer Multi-Platform Games

@multiplayio

Page 3: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

3

@multiplayio

Page 4: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

4

• Going 3D

• WebGL?

• How to draw a cube (source code dive)

• Going Multiplayer

• WebSockets?

• How to move

• Going Multi-platform

• Supporting Tizen

• Supporting iOS, Android, Windows Phone…

Agenda

Page 5: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

5

WebGL?

Tizen

Safari

Android

Internet

Explorer

Blackberry Firefox

Ubuntu

Chrome

Page 6: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

6

How to draw a cube

learningwebgl.com

Page 7: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

7

function drawScene()

{

gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);

gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix);

mat4.identity(mvMatrix);

mat4.translate(mvMatrix, [x, y, z]);

mat4.rotate(mvMatrix, degToRad(xRot), [1, 0, 0]);

mat4.rotate(mvMatrix, degToRad(yRot), [0, 1, 0]);

gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);

gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute,

cubeVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);

gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexTextureCoordBuffer);

gl.vertexAttribPointer(shaderProgram.textureCoordAttribute,

cubeVertexTextureCoordBuffer.itemSize, gl.FLOAT, false, 0, 0);

gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);

setMatrixUniforms();

gl.activeTexture(gl.TEXTURE0);

gl.bindTexture(gl.TEXTURE_2D, crateTexture);

gl.uniform1i(shaderProgram.samplerUniform, 0);

gl.drawElements(gl.TRIANGLES, cubeVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);

}

Page 8: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

8

function drawScene()

{

gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);

gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix);

mat4.identity(mvMatrix);

mat4.translate(mvMatrix, [x, y, z]);

mat4.rotate(mvMatrix, degToRad(xRot), [1, 0, 0]);

mat4.rotate(mvMatrix, degToRad(yRot), [0, 1, 0]);

gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);

gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute,

cubeVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);

gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexTextureCoordBuffer);

gl.vertexAttribPointer(shaderProgram.textureCoordAttribute,

cubeVertexTextureCoordBuffer.itemSize, gl.FLOAT, false, 0, 0);

gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);

setMatrixUniforms();

gl.activeTexture(gl.TEXTURE0);

gl.bindTexture(gl.TEXTURE_2D, crateTexture);

gl.uniform1i(shaderProgram.samplerUniform, 0);

gl.drawElements(gl.TRIANGLES, cubeVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);

}

•Setup view

•Position and rotate

•Set buffers

•Set texture

•Draw

Page 9: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

9

http://multiplay.io/play

Phone Wars WebGL Demo

Page 10: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

Going

Multiplayer

Page 11: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

11

WebSockets?

• TCP

• Persistent

• Bi-directional

• Not UDP

var exampleSocket = new WebSocket("ws://www.example.com/socketserver",

"protocol");

// On connect

exampleSocket.onopen = function (event) {

exampleSocket.send(“Send a message to the server");

};

// Receive message from server

exampleSocket.onmessage = function (event) {

console.log(event.data);

}

Page 12: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

12

WebSockets?

caniuse.com

Page 13: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

13

SocketIO

NodeJS

WebGL

App

Popular Web Multiplayer Stack

MongoDB

Page 14: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

14

var socket = io.connect( serverURL );

socket.on( 'connected', function (userID)

{

socket.userID = userID;

});

function shootAt(thatDamnUserID)

{

socket.emit( 'shoot', thatDamnUserID );

}

How do I shoot? - Client

Page 15: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

15

var sockets = [];

var io = socketio.listen( port );

io.sockets.on( 'connection', function (socket)

{

sockets.push( socket );

var userID = nextUserID++;

socket.emit( 'connected', userID );

socket.on( 'shoot', function (atUserID) )

{

for( var i=0; i<sockets.length; ++i )

{

sockets[i].emit( 'shoot', userID, atUserID );

}

};

});

How do I shoot? - Server

Page 16: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

16

http://multiplay.io/play

Multiplayer Demo

Page 17: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

Going Multi-platform

Page 18: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

18

Supporting Tizen

• <access origin="*"/>

• xhr.state === 0

• Disable Android File Transfer app

• Simulator fast

• Emulator slow but accurate

Page 19: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

19

HTML5 - IndexedDB

• 50mb+

• Is Slow

• Use a priority queue for your requests

• Timestamps lets you know which files to delete

var transaction = db.transaction( "cache", 'readwrite' );

var objectStore = transaction.objectStore( "cache" );

// Get an item via it's key

var index = objectStore.index( "key" );

var request = index.get( key );

request.onsuccess = function(event)

{

var item = event.target.result;

};

Page 20: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

20

HTML5 - WebWorkers

• Is Awesome!

• json.async (https://github.com/ashcairo/json.async)

• Separate file, use inline webworker

var blob = new Blob([

"self.addEventListener( 'message', function (e) { \

var json = JSON.parse( e.data ); \

self.postMessage( json ); \

self.close(); \

}, false );"]);

// Obtain a blob URL reference to our worker 'file'.

var blobURL = window.URL.createObjectURL( blob );

var worker = new Worker( blobURL );

Page 21: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

21

Supporting HTML5 Platforms

• WebGL

• mediump precision most compatible

• IndexedDB

• Some devices require use of setVersion

• Some devices fail on use of setVersion

• Be ready to fallback to localStorage (~5mb)

Page 22: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

22

Supporting iOS, Android, Windows Phone

• OpenGL

• Direct3D

• C++

• Java

• Objective C

• C#

• But it’s possible!

Page 23: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

23

Framework JavaScript AppDevice

Renderer

Android

Renderer

iOS

Renderer

Engine

App

JavaScript Proxy

WebView

Proxy

Renderer

Windows

Renderer

Supporting iOS, Android, Windows Phone

Page 24: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

Eval( isEvil )?

Page 25: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

25

Real-time creation of games

Presenting and demoing

24th July 2013 Anaheim, California

@multiplayio

Page 26: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming

[email protected]

http://multiplay.io

Page 27: TDC 2013 - WebGL & WebSockets for 3D Multi-Platform Multiplayer Gaming