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
37
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
53
Angular the Good Parts
krzychukula
0
230
Yield
krzychukula
0
78
Promises, Promises
krzychukula
5
380
Other Decks in Programming
See All in Programming
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
600
Content Security Policy入門 セキュリティ設定と 違反レポートのはじめ方 / Introduction to Content Security Policy Getting Started with Security Configuration and Violation Reporting
uskey512
1
520
Quine, Polyglot, 良いコード
qnighy
4
640
推し活の ハイトラフィックに立ち向かう Railsとアーキテクチャ - Kaigi on Rails 2024
falcon8823
6
2.9k
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
470
どうして僕の作ったクラスが手続き型と言われなきゃいけないんですか
akikogoto
1
110
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
170
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
220
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
830
Click-free releases & the making of a CLI app
oheyadam
2
110
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.1k
JavaでLチカしたい! / JJUG CCC 2024 Fall LT
nhayato
0
140
Featured
See All Featured
Testing 201, or: Great Expectations
jmmastey
38
7.1k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
The Language of Interfaces
destraynor
154
24k
Ruby is Unlike a Banana
tanoku
96
11k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Faster Mobile Websites
deanohume
305
30k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Into the Great Unknown - MozCon
thekraken
32
1.5k
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?