html5 game dev with three.js - hexgl

64
The making of HexGL

Upload: thibaut-despoulain

Post on 15-Jan-2015

6.740 views

Category:

Technology


5 download

DESCRIPTION

These are the slides of my talk about HexGL at the Adobe User Group meetup in the Netherlands. More info: http://bkcore.com/blog/general/adobe-user-group-nl-talk-video-hexgl.html

TRANSCRIPT

Page 1: HTML5 game dev with three.js - HexGL

The making of HexGL

Page 2: HTML5 game dev with three.js - HexGL

• Thibaut Despoulain (@bkcore – bkcore.com)

• 22 year-old student in Computer Engineering

• University of Technology of Belfort-Montbéliard (France)

• Web dev and 3D enthousiast

• The guy behind the HexGL project

Page 3: HTML5 game dev with three.js - HexGL
Page 4: HTML5 game dev with three.js - HexGL

• Fast-paced, futuristic racing game

• Inspired by the F-Zero and Wipeout series

• HTML5, JavaScript, WebGL (via Three.js)

• Less than 2 months

• Just me.

Page 5: HTML5 game dev with three.js - HexGL
Page 6: HTML5 game dev with three.js - HexGL
Page 7: HTML5 game dev with three.js - HexGL
Page 8: HTML5 game dev with three.js - HexGL
Page 9: HTML5 game dev with three.js - HexGL

• JavaScript API

• OpenGL ES 2.0

• Chrome, FireFox, (Opera, Safari)

• <Canvas> (HTML5)

Page 10: HTML5 game dev with three.js - HexGL
Page 11: HTML5 game dev with three.js - HexGL

• Rendering engine

• Maintained by Ricardo Cabello (MrDoob) and Altered Qualia

• R50/stable

• + : Active community, stable, updated frequently

• - : Documentation

Page 12: HTML5 game dev with three.js - HexGL
Page 13: HTML5 game dev with three.js - HexGL
Page 14: HTML5 game dev with three.js - HexGL
Page 15: HTML5 game dev with three.js - HexGL

• First « real » game

• 2 months to learn and code

• Little to no modeling and texturing skills

• Physics? Controls? Gameplay?

Page 16: HTML5 game dev with three.js - HexGL

• Last time I could have 2 months free

• Visibility to get an internship

• Huge learning opportunity

• Explore Three.js for good

Page 17: HTML5 game dev with three.js - HexGL
Page 18: HTML5 game dev with three.js - HexGL

• Third-party physics engine (rejected)

– Slow learning curve

– Not really meant for racing games

Page 19: HTML5 game dev with three.js - HexGL

• Ray casting (rejected)

– Heavy perfomance-wise

– Needs Octree-like structure

– > too much time to learn and implement

Page 20: HTML5 game dev with three.js - HexGL

• Home-made 2D approximation

– Little to no learning curve

– Easy to implement with 2D maps

– Pretty fast

– > But with some limitations

Page 21: HTML5 game dev with three.js - HexGL
Page 22: HTML5 game dev with three.js - HexGL
Page 23: HTML5 game dev with three.js - HexGL

• Home-made 2D approximation

– No track overlap

– Limited track twist and gradient

– Accuracy depends on map resolution

– > Enough for what I had in mind

Page 24: HTML5 game dev with three.js - HexGL
Page 25: HTML5 game dev with three.js - HexGL

• No pixel getter on JS Image object/tag

• Canvas2D to the rescue

Page 26: HTML5 game dev with three.js - HexGL

Load

ing Load data

texture with JS Image object

Dra

win

g Draw it on a Canvas using 2D context

Get

tin

g Get canvas pixels using getImageData()

Page 27: HTML5 game dev with three.js - HexGL

• ImageData (BKcore package)

– Github.com/Bkcore/bkcore-js

var a = new bkcore.ImageData(path, callback);

//…

a.getPixel(x, y);

a.getPixelBilinear(xf, yf);

// -> {r, g, b, a};

Page 28: HTML5 game dev with three.js - HexGL

Game loop:

Convert world position to pixel indexes

Get current pixel intensity (red)

If pixel is not white:

Collision

Test pixels relatively (front, left, right)

:end

Page 29: HTML5 game dev with three.js - HexGL

Front

Left Right

Track Void

Page 30: HTML5 game dev with three.js - HexGL

Front

Left Right

Height map

Gradient

Front

Current

Tilt

Left

Right

Page 31: HTML5 game dev with three.js - HexGL
Page 32: HTML5 game dev with three.js - HexGL
Page 33: HTML5 game dev with three.js - HexGL
Page 34: HTML5 game dev with three.js - HexGL

Three.js JSON model

Python converter

Model.OBJ

Materials.MTL

$ python convert_obj_three.py -i mesh.obj -o mesh.js

Page 35: HTML5 game dev with three.js - HexGL

var scene = new THREE.Scene();var loader = new THREE.JSONLoader();

var createMesh = function(geometry){

var mesh = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial());mesh.position.set(0, 0, 0);mesh.scale.set(3, 3, 3);scene.add(mesh);

};

loader.load("mesh.js", createMesh);

Page 36: HTML5 game dev with three.js - HexGL
Page 37: HTML5 game dev with three.js - HexGL

var renderer = new THREE.WebGLRenderer({

antialias: false,

clearColor: 0x000000

});

renderer.autoClear = false;

renderer.sortObjects = false;

renderer.setSize(width, height);

document.body.appendChild(renderer.domElement);

Page 38: HTML5 game dev with three.js - HexGL

renderer.gammaInput = true;

renderer.gammaOutput = true;

renderer.shadowMapEnabled = true;

renderer.shadowMapSoft = true;

Page 39: HTML5 game dev with three.js - HexGL
Page 40: HTML5 game dev with three.js - HexGL

• Blinn-phong

– Diffuse + Specular + Normal + Environment maps

• THREE.ShaderLib.normal

– > Per vertex point lights

• Custom shader with per-pixel point lights for the road

– > Booster light

Page 41: HTML5 game dev with three.js - HexGL
Page 42: HTML5 game dev with three.js - HexGL

var boosterSprite = new THREE.Sprite(

{

map: spriteTexture,

blending: THREE.AdditiveBlending,

useScreenCoordinates: false,

color: 0xffffff

});

boosterSprite.mergeWith3D = false;

boosterMesh.add(boosterSprite);

Page 43: HTML5 game dev with three.js - HexGL
Page 44: HTML5 game dev with three.js - HexGL

var material = new THREE.ParticleBasicMaterial({

color: 0xffffff,

map: THREE.ImageUtils.loadTexture(“tex.png”),

size: 4,

blending: THREE.AdditiveBlending,

depthTest: false,

transparent: true,

vertexColors: true,sizeAttenuation: true

});

Page 45: HTML5 game dev with three.js - HexGL

var pool = [];

var geometry = new THREE.Geometry();

geometry.dynamic = true;

for(var i = 0; i < 1000; ++i)

{

var p = new bkcore.Particle();

pool.push(p);

geometry.vertices.push(p.position);

geometry.colors.push(p.color);

}

Page 46: HTML5 game dev with three.js - HexGL

bkcore.Particle = function()

{

this.position = new THREE.Vector3();

this.velocity = new THREE.Vector3();

this.force = new THREE.Vector3();

this.color = new THREE.Color(0x000000);

this.basecolor = new THREE.Color(0x000000);

this.life = 0.0;

this.available = true;

}

Page 47: HTML5 game dev with three.js - HexGL

var system = new THREE.ParticleSystem(

geometry,

material

);

system.sort = false;

system.position.set(x, y, z);

system.rotation.set(a, b, c);

scene.add(system);

Page 48: HTML5 game dev with three.js - HexGL

// Particle physics

var p = pool[i];

p.position.addSelf(p.velocity);

//…

geometry.verticesNeedUpdate = true;

geometry.colorsNeedUpdate = true;

Page 49: HTML5 game dev with three.js - HexGL

• Particles (BKcore package)

– Github.com/BKcore/Three-extensions

Page 50: HTML5 game dev with three.js - HexGL

var clouds = new bkcore.Particles({

opacity: 0.8,

tint: 0xffffff, color: 0x666666, color2: 0xa4f1ff,

texture: THREE.ImageUtils.loadTexture(“cloud.png”),

blending: THREE.NormalBlending,

size: 6, life: 60, max: 500,

spawn: new THREE.Vector3(3, 3, 0),

spawnRadius: new THREE.Vector3(1, 1, 2),

velocity: new THREE.Vector3(0, 0, 4),

randomness: new THREE.Vector3(5, 5, 1)

});

Page 51: HTML5 game dev with three.js - HexGL

scene.add(clouds);

// Game loop

clouds.emit(10);

clouds.update(dt);

Page 52: HTML5 game dev with three.js - HexGL
Page 53: HTML5 game dev with three.js - HexGL

• Built-in support for off-screen passes

• Already has some pre-made post effects

– Bloom

– FXAA

• Easy to use and Extend

• Custom shaders

Page 54: HTML5 game dev with three.js - HexGL

var renderTargetParameters = {

minFilter: THREE.LinearFilter,

magFilter: THREE.LinearFilter,

format: THREE.RGBFormat,

stencilBuffer: false

};

var renderTarget = new THREE.WebGLRenderTarget(

width, height,

renderTargetParameters

);

Page 55: HTML5 game dev with three.js - HexGL

var composer = new THREE.EffectComposer(

renderer,

renderTarget

);

composer.addPass( … );

composer.render();

Page 56: HTML5 game dev with three.js - HexGL

• Generic passes– RenderPass

– ShaderPass

– SavePass

– MaskPass

• Pre-made passes– BloomPass

– FilmPass

– Etc.

Page 57: HTML5 game dev with three.js - HexGL

var renderModel = new THREE.RenderPass(

scene,

camera

);

renderModel.clear = false;

composer.addPass(renderModel);

Page 58: HTML5 game dev with three.js - HexGL

var effectBloom = new THREE.BloomPass(

0.8, // Strengh

25, // Kernel size

4, // Sigma

256 // Resolution

);

composer.addPass(effectBloom);

Page 59: HTML5 game dev with three.js - HexGL
Page 60: HTML5 game dev with three.js - HexGL

var hexvignette: {uniforms: {

tDiffuse: { type: "t", value: 0, texture: null },tHex: { type: "t", value: 1, texture: null},size: { type: "f", value: 512.0}, color: { type: "c", value: new THREE.Color(0x458ab1) }

},fragmentShader: [

"uniform float size;","uniform vec3 color;","uniform sampler2D tDiffuse;","uniform sampler2D tHex;",

"varying vec2 vUv;",

"void main() { ... }"

].join("\n")};

Page 61: HTML5 game dev with three.js - HexGL

var effectHex = new THREE.ShaderPass(hexvignette);

effectHex.uniforms['size'].value = 512.0;

effectHex.uniforms['tHex'].texture = hexTexture;

composer.addPass(effectHex);

//…

effectHex.renderToScreen = true;

Page 62: HTML5 game dev with three.js - HexGL
Page 63: HTML5 game dev with three.js - HexGL
Page 64: HTML5 game dev with three.js - HexGL