introduction to webgl and webvr

29
Introduction to WebGL & WebVR Daosheng Mu 2015.10.17

Upload: daosheng-mu

Post on 16-Jan-2017

849 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Introduction to WebGL and WebVR

Introduction to WebGL & WebVR

Daosheng Mu2015.10.17

Page 2: Introduction to WebGL and WebVR

Last Revisions

WebGL

Page 3: Introduction to WebGL and WebVR

WebGL

● WebGL 1.0○ Closely to the OpenGL ES 2.0

API○ 8 texture units○ Must power of two images○ Maximum GLSL Token Size: 256

chars○ Not support compressed texture○ Nesting of Structures in GLSL:

Level 1

● WebGL 2.0○ Closely to OpenGL ES 3.0 API○ 32 texture units○ Supported for non-power-of-two

images. ○ Maximum GLSL Token Size:

1024 chars.○ Support compressed texture○ Nesting of Structures in GLSL:

Level 4

Page 4: Introduction to WebGL and WebVR

WebGL

canvas.getContext(‘webgl’) || canvas.getContext(‘experimental-webgl’);

Page 5: Introduction to WebGL and WebVR

View Frustum

Page 6: Introduction to WebGL and WebVR
Page 7: Introduction to WebGL and WebVR

3D Graphics API

Framework

WebGL

Three.js

Application My Game

● Rendering● Hardware-accelerated

● 3D model loader● Material● Camera

● Gameplay● Controls

Page 8: Introduction to WebGL and WebVR

Camera control document.addEventListener( 'mousemove', onMouseMove, false );document.addEventListener( 'mousedown', onMouseDown, false );document.addEventListener( 'mouseup', onMouseUp, false );

document.addEventListener( 'keydown', onKeyDown, false );document.addEventListener( 'keyup', onKeyUp, false );

For example:

function onKeyDown() {camera.translateZ( moveSpeed );

}

Page 10: Introduction to WebGL and WebVR

Toolkits

● Firefox Developer Edition● WebGLInspector● ShaderEditor

Page 11: Introduction to WebGL and WebVR

Let’s program NOW!

https://goo.gl/rZnHpvgit clone [email protected]:daoshengmu/WebGL-TPE-Meetup.git

Page 12: Introduction to WebGL and WebVR

Editor’s Draft

WebVR

Page 13: Introduction to WebGL and WebVR
Page 14: Introduction to WebGL and WebVR
Page 15: Introduction to WebGL and WebVR

Samsung Gear VR - Dec, 2014

Page 16: Introduction to WebGL and WebVR

Oculus Rift DK2 - July, 2014

Head-Mounted Display(HMD): Gyroscope, Accelerometer, Magnetometer

Position Tracker: Near Infrared CMOS Sensor

Page 17: Introduction to WebGL and WebVR

MozVR - Nov. 2014

● WebVR● Has Landed in Firefox Nightly● Non-e10s support● about:config

○ dom.vr.enabled;true● https://github.com/MozVR/● http://mozvr.com/projects/polarsea/

Page 18: Introduction to WebGL and WebVR

if ( navigator.getVRDevices ) { var vrPromise = navigator.getVRDevices(); vrPromise.then(vrDeviceCallback);}

gfxVROculus

gfxVRCardboardGetAllHMDs()

gfx::VRHMDManager

Page 19: Introduction to WebGL and WebVR

function vrDeviceCallback( devices ) {

if (devices.length) { for (var i = 0; i < devices.length; ++i) {

if (devices[i] instanceof HMDVRDevice) { vrHMD = devices[i]; }

if (vrHMD && devices[i] instanceof PositionSensorVRDevice && devices[i].hardwareUnitId == vrHMD.hardwareUnitId)

{ vrPosSensor = devices[i]; // We just want to get the first VR device. break; } } }

Page 20: Introduction to WebGL and WebVR
Page 21: Introduction to WebGL and WebVR

VR applications need to render two views of the scene.

Page 22: Introduction to WebGL and WebVR

Translation of the left eye

FOV of the left eye FOV of the right eye

Translation of the right eye

VR applications need to render two views of the scene.

Page 23: Introduction to WebGL and WebVR

var leftEyeParams = vrHMD.getEyeParameters(‘left’);var rightEyeParams = vrHMD.getEyeParameters(‘right’);// In metersvar leftEyeTranslation = leftEyeParams.eyeTranslation; var rightEyeTranslation = rightEyeParams.eyeTranslation; var leftEyeFOV = leftEyeParams.recommendedFieldOfView; var rightEyeFOV = rightEyeParams.recommendedFieldOfView;

Page 24: Introduction to WebGL and WebVR

function setViewport( eyeParams ) {var renderRect = eyeParams.renderRect; gl.setViewport( renderRect.x, renderRect.y, renderRect.w, renderRect.h );

}

funtion setViewMatrix( viewMtx, eyeTranslation ) { viewMtx.translate( eyeOffset );

gl.uniformMatrix4fv( viewMatUniform, false, viewMat );

}

function setProjMatrix( eyeFov ) {var projMtx = getProjMtxFromEyeFov( eyeFov );gl.uniformMatrix4fv( projMatUniform, false, projMtx );

}

Page 25: Introduction to WebGL and WebVR

function onRequestAnimationFrame() {if ( vrPosSensor ) {

var state = vrPosSensor. getState();if ( state.hasOrientation ) {

camera.quaternion.set( state.orientation.x,

state.orientation.y, state.orientation.z, state.orientation.w); }

if ( state.hasPosition ) {

} }

render( camera );}

function render( camera ) {

// Left eyesetViewport( leftEyeParams );

setProjMatrix( leftEyeFOV );

setViewMatrix( camera.

matrixWorld, leftEyeTranslation

);

drawScene();

// Right eyesetViewport( rightEyeParams );

setProjMatrix( rightEyeFOV );

setViewMatrix( camera.

matrixWorld, rightEyeTranslation

);

drawScene();

}

Page 26: Introduction to WebGL and WebVR

● Automatically displays image on HMD● Correct pincushion distortion for your device● Correct framerate and Timewarp! (Windows+Oculus)● Chromatic aberration correction

function enterVRFullscreen(canvas) {

canvas.requestFullscreen({ vrDisplay: gHmd });

}

Page 27: Introduction to WebGL and WebVR

Let’s program NOW!

https://goo.gl/Wtxraqgit clone [email protected]:daoshengmu/WebGL-TPE-Meetup.git

Page 28: Introduction to WebGL and WebVR

Conclusion

50 million users

● Made in 1920● Took 38 yrs

● Made in 1939● Took 14 yrs

● Made in 1991● Took 4 yrs

● Made in 2014● Took 18 months

Page 29: Introduction to WebGL and WebVR

Thanks for your attention