BLOG

Notes from the KAYAC hyper-casual team

My Development Flow for WebGL Content, and What to Get Right

Takumi Fukasawa

Hello! I'm Fukapon , also known as the WebGL guy.

I often get to work on WebGL projects, and I've handled the WebGL side of projects such as the "Summer Wars TV tie-in special site" and "Shin Arima" .

Summer Wars TV tie-in special site: the "Yoroshiku onegaishimaaaasu!!" button | KAYAC

Shin Arima | KAYAC

Here's what I wrote for past Advent Calendars. This is my fourth year in a row taking part.

2015 ... [WebGL isn't scary either! The recommended order for learning canvas libraries efficiently](http://(http://techblog.kayac.com/_article20151204_canvas_library.html)

2016 ... [Goodbye gulp] Building a simple front-end development environment with npm-scripts

2017 ... [WebGL] Sprite animation in 3D space using shaders


Preface

I've developed a lot of WebGL content and also run WebGL study sessions at the company, and one day it struck me that I often talk about specific technologies or techniques, but rarely about the development flow. So I'd like to write down my own development flow and the things I pay attention to.

This is strictly about the big picture, so I won't go into tips for individual technologies such as JS or shaders, or into performance tuning.

What I want to achieve

In my own work, I focus on the following.

  • Check feasibility early
  • Leave time to refine the visuals

Here's the overall flow.

1. Choose a WebGL library
2. Check feasibility
  - WebGL Report
  - WebGL Stats
  - Build a mock
3. Prepare the front-end development environment
  - Build environment
  - Shaders
4. Set up a testing environment
  - External services
  - Test pages
5. Build a GUI editor
6. Set up a load monitoring environment
  - FPS
  - (three.js only) renderer.info
7. Main development
8. Done

By the time you reach step 6, you'll have a GUI like this on screen.

iframe

1. Choose a WebGL library

Strictly speaking, WebGL means the "WebGL API", but with the raw API you end up writing an enormous amount of code and it's hard to move quickly, so I almost always use a library.

For 3D content I use Three.js, and for 2D, PIXI.js. Their documentation is thorough, which is why I've stuck with them for so long.

If there are no particular restrictions, I often load them from a CDN. A CDN is easy, since you just load the library with a script tag, and you can hope to benefit from the CDN's cache as well.

Sometimes I install the npm modules instead and import them as needed. Matching the project's requirements is what matters, so use whichever suits.

2. Check feasibility

A common headache in WebGL development is that behavior varies not only by browser but also by the device's hardware, such as the graphics driver.

The range of supported devices and the specs differ completely from project to project, so an implementation that was fine on the last project can't necessarily be reused as is on the next one. So I check on each device "whether the spec is achievable" and "whether a visual I'd like to try can be included".

In most cases, I use these three to check.

2-1. WebGL Report

A site that shows your device's WebGL support. By opening this page on each device you need to support, you can confirm which features you can and can't use.

WebGL Report

2-2. WebGL Stats

A site that tells you how widely WebGL parameters and extensions are supported across devices in the wild. Some WebGL extensions are supported only on a narrow range of devices, so it's handy for getting the overall picture.

WebGL Stats

2-3. Build a mock

The mock exists mainly to check feasibility, so I build it on the assumption that it won't be used in the main development, and test it on the target devices.

Once, while developing smartphone-only content, I needed to play ten looping videos at the same time and decided to do it with sprite animation. The bottleneck was memory: could I load and display that many sprite sheets at a texture size with acceptable resolution? So I prepared textures in various sizes, built a mock that let me freely change the number and size of textures loaded in the browser, and found a workable compromise.

In the main development, things almost always end up heavier than planned once they're combined with the rest of the implementation, but if you get a rough sense of the direction at the start, the scope of later fixes stays small.

3. Prepare the front-end development environment

3-1. Build environment

Lately I've followed the trend and based things on Webpack. If there are no particular restrictions, I think whatever you like is fine.

A while back I used Gulp, or just npm scripts.

3-2. Shaders

I often use glslify. GLSL involves a lot of reused code, and glslify is handy because it lets you pull in lots of commonly used routines.

These are the glslify modules I use most. glsl-noise provides various kinds of noise such as simplex noise. glsl-blend gives you blend modes such as screen, multiply and overlay, much like Photoshop's layer blending.

On the other hand, I've sometimes written my own mechanism for bundling shaders, when I needed to generate shaders dynamically in JS.

4. Set up a testing environment

When you run into unintended behavior in WebGL development, bugs included, it's often hard to tell where the problem lies. Did I get the JS wrong, is it device-specific, or did I mess up the math... Shaders are especially bad: when there's no compile error but the output isn't what you intended, you have no idea what's going on.

So, to isolate problems, I set up an environment where I can pull a piece out of the development environment and test just that piece.

The key is not to force everything to happen inside the development environment, but to cut things out feature by feature.

4-1. External services

CodePen and jsdo.it let you run JS and the like in an in-browser editor, which is very handy. Besides testing, I also build things like shader mocks there.

CodePen - Front End Developer Playground & Code Editor in the Browser

jsdo.it - Share JavaScript, HTML5 and CSS

4-2. Test pages

You mustn't upload a project's assets to external services, but some things, such as 3D models, can only be checked with those assets. In those cases, I make a test page inside the development environment and check there.

5. Build a GUI editor

For example, when you want to tweak a number slightly, changing it in the code, running it in the browser, and repeating... takes forever.

So I set up a GUI tool that lets you adjust the behavior in the browser. The one I use most is dat.gui. Besides adjusting numbers and colors, you can also call functions, which is handy. You often see it in the Three.js and PIXI.js examples too.

A small change to a number can produce unexpected motion, so you make new discoveries along the way, which is fun.

dat.GUI

dat.GUI in use

Also, with Three.js, I use OrbitControls so I can move the camera around freely.

https://threejs.org/docs/#examples/controls/OrbitControls

6. Set up a load monitoring environment

As development goes on, you sometimes find that things have gotten heavy without you noticing. It's common to think "right, time to tune!", try this and that, and get nowhere.

When tuning performance, you need to identify the cause first. So I show FPS and memory figures on screen to make the metrics that tuning relies on visible.

For how to pin down the cause and identify the problem itself, I refer to chapter 2 of this book, "Introduction to GPU Optimization", which explains it clearly.

Computer Graphics Gems JP 2015 - The Frontiers of Computer Graphics Technology -

Computer Graphics Gems JP 2015 - The Frontiers of Computer Graphics Technology -

  • Authors: 山本醍田,鈴木健太郎,小口貴弘,?吉雄介,白鳥貴亮,向井智彦,五十嵐悠紀,岡部誠,森本有紀,上瀧剛,坂東洋介,加藤諒
  • Publisher: Born Digital
  • Release date: 2015/09/12
  • Media: Large-format book
  • See blogs that include this product (1)

6-1. FPS

FPS is probably the easiest metric for checking performance.

I often use Stats.js. It's available on a CDN too, and incidentally it was made by mrdoob, the creator of Three.js.

const stats = new Stats();
document.body.appendChild(stats);
const tick = () => {
  stats.begin();
  .. various processing
  stats.end();
  requestAnimationFrame(tick);
}
requestAnimationFrame(tick);

Stats.js on screen

6-2. (Three.js only) renderer.info

With Three.js, you can get information such as draw calls and polygon counts from WebGLRenderer.info. I pay particular attention to draw calls and polygon counts.

# For details, see https://threejs.org/docs/#api/en/renderers/WebGLRenderer

- memory
  - geometries
  - textures
- render
  - calls
  - triangles
  - points
  - lines
- programs

For example, you can get the draw calls like this.


const renderer = new THREE.WebGLRenderer(options);
const logElem = document.querySelector(selector);

logElem.textContent = `drawCalls: ${renderer.info.render.calls}`; // show draw calls
... show other logs

renderer.info shown on screen

7. Main development

The GUI and monitoring during main development

Once everything is ready, I charge ahead with development. From here on, markup, testing and performance tuning all get mixed in as development proceeds. The development environment keeps changing too, with npm modules added as needed and so on.

There are various approaches to performance tuning. To repeat myself, the first key is being able to identify the problem. I'd suggest using a book like the one I introduced in step 5 as a reference.

7. Done

Done in no time!

Summary

There are two main points.

  • I made it possible to check logs in the browser
  • I made it possible to adjust things in the browser

It took a while to get to the main development, because I spent time setting up the environment. Game engines such as Unity and Unreal Engine come with all this in place, which is very convenient.

In web development, apart from things like the browser's developer tools, there basically aren't GUI editors like that, but I think having one definitely helps move development forward.

Recently, libraries with editors such as WebGLStudio have appeared, and I seem to see content made with PlayCanvas more often than before. WebGL export from game engines such as Unity and Unreal Engine has also been improving, so I suspect the WebGL development flow is going to change a lot from here on.

Finally

Thank you for reading this far.

KAYAC is looking for people who want to dive in and build WebGL content.