Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
A Novice's Guide to WebGL
Search
krzychukula
April 26, 2017
Programming
0
38
A Novice's Guide to WebGL
Introduction to 3D, shaders and WebGL.
krzychukula
April 26, 2017
Tweet
Share
More Decks by krzychukula
See All by krzychukula
Introduction to WebGL
krzychukula
1
100
Simplicity
krzychukula
0
57
Angular the Good Parts
krzychukula
0
230
Yield
krzychukula
0
81
Promises, Promises
krzychukula
5
390
Other Decks in Programming
See All in Programming
負債になりにくいCSSをデザイナとつくるには?
fsubal
9
2.3k
sappoRo.R #12 初心者セッション
kosugitti
0
230
Pythonでもちょっとリッチな見た目のアプリを設計してみる
ueponx
1
480
Java Webフレームワークの現状 / java web framework at burikaigi
kishida
9
2.2k
Multi Step Form, Decentralized Autonomous Organization
pumpkiinbell
1
660
ISUCON14公式反省会LT: 社内ISUCONの話
astj
PRO
0
180
時計仕掛けのCompose
mkeeda
1
280
昭和の職場からアジャイルの世界へ
kumagoro95
1
350
データの整合性を保つ非同期処理アーキテクチャパターン / Async Architecture Patterns
mokuo
41
15k
ARA Ansible for the teams
kksat
0
150
Unity Android XR入門
sakutama_11
0
140
DevinとCursorから学ぶAIエージェントメモリーの設計とMoatの考え方
itarutomy
1
640
Featured
See All Featured
Optimizing for Happiness
mojombo
376
70k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
175
51k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
366
25k
Why Our Code Smells
bkeepers
PRO
335
57k
What's in a price? How to price your products and services
michaelherold
244
12k
Speed Design
sergeychernyshev
25
780
It's Worth the Effort
3n
184
28k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
540
Testing 201, or: Great Expectations
jmmastey
41
7.2k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
4
400
Faster Mobile Websites
deanohume
306
31k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.3k
Transcript
A Novice’s Guide to WebGL @Krzychukula
Plan 1. Intro 2. How it all works 3. Triangle
4. Multiple colours 5. Matrix 6. Learn
ASK QUESTIONS!
None
Data Visualisation
What my learning looked like?
But this is just a hobby
OpenGL books Courses Three.js Shaders Workshop WebGL workshop WebGL Books
Disclaimer: mental models over correctness
Who this talk is for?
[-1 …. 0 …. 1]
NO SHINY DEMOS
None
None
Dreyfus model of skill acquisition
Masters of DOOM YOUme
I realised that we are all Novices!
Novice WebGL Programmer!
Let’s start
2. How it all works?
Back in the old days…
None
Graphic Card
None
Map Reduce?
None
None
What is WebGL?
History
Shaders
Vertex Fragment
Vertex Shader Fragment Shader
It has to be FAST
Vertex Shader void main() { gl_Position = vec4(0.0, 0.0, 0.0,
1.0); }
Vertex Shader attribute vec4 a_Position; void main() { gl_Position =
a_Position; }
Fragment Shader void main() { gl_FragColor = vec4(0.0, 1.0, 0.0,
1.0); }
None
Fragment Shader uniform vec4 a_Color; void main() { gl_FragColor =
a_Color; }
But… What about JS?
Context var gl = canvas.getContext("webgl");
Compile Vertex Shader var vertexShader = gl.createShader(gl.VERTEX_SHADER) gl.shaderSource(vertexShader, ` void
main() { … } `) gl.compileShader(vertexShader)
Compile Fragment Shader var fragShader = gl.createShader(gl.FRAGMENT_SHADER) gl.shaderSource(fragShader, ` void
main() { … } `) gl.compileShader(fragShader)
Program var program = gl.createProgram() gl.attachShader(program, vertexShader) gl.attachShader(program, fragmentShader) gl.linkProgram(program)
u_Color var u_Color = gl.getUniformLocation(program, ‘u_Color’); gl.uniform4fv(u_Color, [0.0, 1.0, 0.0,
1.0]);
Fragment Shader uniform vec4 u_Color; void main() { gl_FragColor =
u_Color; }
None
None
1 -1 1
None
0, 0.5
-0.5, -0.5 0, 0.5
-0.5, -0.5 0, 0.5 0.5, -0.5
gl_Position = vec4(0.0, 0.5, 0.0, 1.0); -0.5, -0.5 0, 0.5
-0.5, 0.5
gl_Position = vec4(0.0, 0.5, 0.0, 1.0); gl_Position = vec4(-0.5, -0.5,
0.0, 1.0); gl_Position = vec4(0.5, -0.5, 0.0, 1.0); -0.5, -0.5 0, 0.5 -0.5, 0.5
var positions = new Float32Array([ 0.0, 0.5, 0.0, 1.0, -0.5,
-0.5, 0.0, 1.0, 0.5, -0.5, 0.0, 1.0 ]);
Buffers
var buffer = gl.createBuffer() gl.bindBuffer(gl.ARRAY_BUFFER, buffer) gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW)
var positions = new Float32Array([ 0.0, 0.5, 0.0, 1.0, -0.5,
-0.5, 0.0, 1.0, 0.5, -0.5, 0.0, 1.0 ]);
var a_Position = gl.getAttribLocation(program, ‘a_Position') gl.enableVertexAttribArray(a_Position) gl.vertexAttribPointer(a_Position, 4, gl.FLOAT, false,
0, 0)
gl.drawArrays(gl.TRIANGLES, 0, 3)
None
Vertex Shader Fragment Shaders
None
Fragment Shader attribute vec4 a_Color; void main() { gl_FragColor =
a_Color; }
varying Vertex Shaders Fragment Shaders attributes
Vertex Shader attribute vec4 a_Position; attribute vec4 a_Color; varying vec4
v_Color; void main() { gl_Position = a_Position; v_Color = a_Color; }
Vertex Shader void main(vec4 a_Position, vec4 a_Color) { return {
gl_Position: a_Position, v_Color: a_Color }; }
v_Color = vec4(1.0, 0.0, 0.0, 1.0); v_Color = vec4(0.0, 1.0,
0.0, 1.0); v_Color = vec4(0.0, 0.0, 1.0, 1.0);
What about the middle?
None
None
Fragment Shader varying vec4 v_Color; void main() { gl_FragColor =
v_Color; }
None
var colors = new Float32Array([ 1.0, 0.0, 0.0, 1.0, 0.0,
1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0 ]);
var buffer = gl.createBuffer() gl.bindBuffer(gl.ARRAY_BUFFER, buffer) gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW)
var a_Color = gl.getAttribLocation(program, ‘a_Color') gl.enableVertexAttribArray(a_Color) gl.vertexAttribPointer(a_Position, 4, gl.FLOAT, false,
0, 0)
gl.drawArrays(gl.TRIANGLES, 0, 3)
None
Almost the end
Matrix
diffX diffX rotateAngle scaleX scaleY
Matrix
Don’t worry about Math Linear Algebra
var modelMatrix = new Matrix4() modelMatrix.translate(x, 0, 0, 1) modelMatrix.rotate(angle,
0, 0, 1) In JavaScript
Vertex Shader attribute vec4 a_Position; uniform mat4 u_modelMatrix; void main()
{ gl_Position = u_modelMatrix * a_Position; }
What to learn from?
WebGL Beginner’s Guide Diego Cantor & Brandon Jones
Interactive 3D Graphics Eric Haines & Gundega Dekena
WebGL Programming Guide Kouichi Matsuda & Rodger Lea
The End @Krzychukula
https:/ /hacks.mozilla.org/2017 /01/webgl-2-lands-in-firefox/ https:/ /www.chromeexperiments.com/globe https:/ /codepen.io/krzychukula/pen/qmZaMz?editors=0010 Triangle https:/ /codepen.io/krzychukula/pen/OmXgKe?editors=0010
Triangle 3 colors https:/ /codepen.io/krzychukula/pen/OmXgKe?editors=0010 Triangle 3 colors in one buffer https:/ /www.packtpub.com/game-development/webgl-beginners-guide https:/ /www.udacity.com/course/interactive-3d-graphics--cs291 https:/ /sites.google.com/site/webglbook/
QA (ideas) Libraries? How shadertoy works? How to use one
buffer for vertex and colors data? How textures work? How many lines of code those examples have?