WebGL. Creating Interactive Content with WebGL. Media #WWDC14. Session 509 Dean Jackson and Brady Eidson WebKit Engineers

Similar documents
CPSC 436D Video Game Programming

WebGL: Hands On. DevCon5 NYC Kenneth Russell Software Engineer, Google, Inc. Chair, WebGL Working Group

CS452/552; EE465/505. Image Processing Frame Buffer Objects

Lecture 11 Shaders and WebGL. October 8, 2015

Introduction to Computer Graphics with WebGL

Comp4422. Computer Graphics. Lab 02: WebGL API Prof. George Baciu. PQ838 x7272.

Introduction to OpenGL/GLSL and WebGL GLSL

Rasterization-based pipeline

CS452/552; EE465/505. Review & Examples

CS452/552; EE465/505. Image Formation

Introductory Seminar

Programming with OpenGL Complete Programs Objectives Build a complete first program

2D graphics with WebGL

Converts geometric primitives into images Is split into several independent stages Those are usually executed concurrently

WebGL and GLSL Basics. CS559 Fall 2015 Lecture 10 October 6, 2015

CS452/552; EE465/505. Shadow Mapping in WebGL

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

CSE 167: Introduction to Computer Graphics Lecture #7: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Spring Quarter 2016

OPENGL RENDERING PIPELINE

WebGL A quick introduction. J. Madeira V. 0.2 September 2017

Best practices for effective OpenGL programming. Dan Omachi OpenGL Development Engineer

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

Mining the Rendering Power in Web Browsers. Jianxia Xue Jan. 28, 2014

COMP371 COMPUTER GRAPHICS

PROFESSIONAL. WebGL Programming DEVELOPING 3D GRAPHICS FOR THE WEB. Andreas Anyuru WILEY. John Wiley & Sons, Ltd.

WebGL (Web Graphics Library) is the new standard for 3D graphics on the Web, designed for rendering 2D graphics and interactive 3D graphics.

OpenGL shaders and programming models that provide object persistence

CS 432 Interactive Computer Graphics

CS452/552; EE465/505. Texture Mapping in WebGL

Models and Architectures. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Picking (In WebGL) Picking idea

Objectives. Programming with WebGL Part 1: Background. Retained vs. Immediate Mode Graphics. Early History of APIs. PHIGS and X.

Programming with WebGL Part 1: Background. CS 432 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Buffers. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

CS452/552; EE465/505. Overview of Computer Graphics

CISC 3620 Lecture 7 Lighting and shading. Topics: Exam results Buffers Texture mapping intro Texture mapping basics WebGL texture mapping

Discussion 3. PPM loading Texture rendering in OpenGL

Game Graphics & Real-time Rendering

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 4: Three Dimensions

API Background. Prof. George Wolberg Dept. of Computer Science City College of New York

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

Graphics Programming. Computer Graphics, VT 2016 Lecture 2, Chapter 2. Fredrik Nysjö Centre for Image analysis Uppsala University

CSE 167: Introduction to Computer Graphics Lecture #13: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2015

Information Coding / Computer Graphics, ISY, LiTH. OpenGL! ! where it fits!! what it contains!! how you work with it 11(40)

Programming with OpenGL Part 3: Shaders. Ed Angel Professor of Emeritus of Computer Science University of New Mexico

BCA611 Video Oyunları için 3B Grafik

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

OpenGL ES for iphone Games. Erik M. Buck

X. GPU Programming. Jacobs University Visualization and Computer Graphics Lab : Advanced Graphics - Chapter X 1

CS 380 Introduction to Computer Graphics. LAB (1) : OpenGL Tutorial Reference : Foundations of 3D Computer Graphics, Steven J.

Introducing the Modern WebKit API

Web-Based Visualization

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 3: Shaders

Overview. By end of the week:

I Can t Believe It s Not

Lecture 5 Vertex and Fragment Shaders-1. CITS3003 Graphics & Animation

Introduction to Computer Graphics with WebGL

OUTLINE. Learn the basic design of a graphics system Introduce pipeline architecture Examine software components for a graphics system

Information Coding / Computer Graphics, ISY, LiTH GLSL. OpenGL Shading Language. Language with syntax similar to C

Game Graphics & Real-time Rendering

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Introduction to Shaders.

CS4621/5621 Fall Computer Graphics Practicum Intro to OpenGL/GLSL

Steiner- Wallner- Podaras

Mobile Application Programing: Android. OpenGL Operation

Computergraphics Exercise 15/ Shading & Texturing

CS 432 Interactive Computer Graphics

Working with Metal Overview

Working With Metal Advanced

Practical Texturing (WebGL) CS559 Fall 2016 Lecture 20 November 7th 2016

Ciril Bohak. - INTRODUCTION TO WEBGL

WebGL. Announcements. WebGL for Graphics Developers. WebGL for Web Developers. Homework 5 due Monday, 04/16. Final on Tuesday, 05/01

Over the past 15 years, OpenGL has become

Geometry Shaders. And how to use them

OpenGL pipeline Evolution and OpenGL Shading Language (GLSL) Part 2/3 Vertex and Fragment Shaders

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 2: First Program

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

Information Coding / Computer Graphics, ISY, LiTH GLSL. OpenGL Shading Language. Language with syntax similar to C

Introduction to Computer Graphics with WebGL

CS 432 Interactive Computer Graphics

JavaScript. Interpolated points

Introduction to GLSL. Announcements. Agenda. Course Contents. Patrick Cozzi University of Pennsylvania CIS Fall 2012

Fog example. Fog is atmospheric effect. Better realism, helps determine distances

INTRODUCTION TO OPENGL PIPELINE

CS475/CS675 - Computer Graphics. OpenGL Drawing

The Application Stage. The Game Loop, Resource Management and Renderer Design

OPENGL AND GLSL. Computer Graphics

GLSL 1: Basics. J.Tumblin-Modified SLIDES from:

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

Yazhuo Liu Homework 3

COPYRIGHTED MATERIAL. 1Introducing WebGL THE BASICS OF WEBGL

Shaders. Slide credit to Prof. Zwicker

The Graphics Pipeline and OpenGL IV: Stereo Rendering, Depth of Field Rendering, Multi-pass Rendering!

Copyright Khronos Group 2012 Page 1. Teaching GL. Dave Shreiner Director, Graphics and GPU Computing, ARM 1 December 2012

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

Dave Shreiner, ARM March 2009

Today s Agenda. Basic design of a graphics system. Introduction to OpenGL

Programmable GPUs. Real Time Graphics 11/13/2013. Nalu 2004 (NVIDIA Corporation) GeForce 6. Virtua Fighter 1995 (SEGA Corporation) NV1

Supplement to Lecture 22

Mobile Application Programing: Android. OpenGL Operation

Transcription:

Media #WWDC14 WebGL Creating Interactive Content with WebGL Session 509 Dean Jackson and Brady Eidson WebKit Engineers 2014 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

JavaScript

AngryBots by Unity

AngryBots by Unity

Swooop by PlayCanvas

Swooop by PlayCanvas

What You Will Learn

What You Will Learn Setting up WebGL in your page

What You Will Learn Setting up WebGL in your page How to do basic drawing

What You Will Learn Setting up WebGL in your page How to do basic drawing Advanced rendering and animation

What You Will Learn Setting up WebGL in your page How to do basic drawing Advanced rendering and animation Relationship to other HTML features

three.js MontageJS Copperlicht Babylon JS Turbulenz Cesium Goo Engine

Motivation

Motivation Powerful graphics in Web content

Motivation Powerful graphics in Web content Interoperability through Open Standard

BEGIN { print "Hello, world!" } display dialog "Hello, world!" (princ "Hello, world!") #include <stdio.h> int main() { printf("hello, world!\n"); return 0; } program HelloWorld; begin WriteLn('Hello, world!'); end. print "Hello, world!" package main import "fmt" func main() { fmt.println("hello, world!") } #import <Foundation/Foundation.h> int main(void) { NSLog(@"Hello, world!\n"); return 0; } PRINT "Hello, world!" DISPLAY 'Hello, world!'. STOP RUN. HAI CAN HAS STDIO? VISIBLE "HAI WORLD!" KTHXBYE public class HelloWorld { public static void main(string[] args) { System.out.println("Hello, world!"); } } <?php echo 'Hello, world!';?> puts "Hello, world!" %!PS /Courier 72 selectfont 20 20 moveto (Hello World!) show showpage #include <iostream> int main() { std::cout << "Hello, world!" << std::endl; return 0; }

Creating, Configuring, and Drawing

Somewhere to Draw Canvas

Somewhere to Draw Canvas <canvas> element Or create one via JavaScript: var canvas = document.createelement("canvas");

Somewhere to Draw Canvas var canvas = document.queryselector("canvas");

Somewhere to Draw Canvas var canvas = document.queryselector("canvas");! canvas.width = 600 * window.devicepixelratio; canvas.height = 400 * window.devicepixelratio;

Something to Draw With Creating the WebGLRenderingContext

Something to Draw With Creating the WebGLRenderingContext var gl = canvas.getcontext("webgl");

Configuring the System Creating and setting up the resources

Configuring the System Creating and setting up the resources Before we can do the actual draw operation:

Configuring the System Creating and setting up the resources Before we can do the actual draw operation: Create some buffers

Configuring the System Creating and setting up the resources Before we can do the actual draw operation: Create some buffers Create a program to use while drawing

(0.55, 0.75) (-0.8, -0.3) (0.7, -0.8)

(0.55, 0.75) (-0.8, -0.3) (0.7, -0.8)

-0.8-0.3 0.7-0.8 0.55 0.75 (0.55, 0.75) (-0.8, -0.3) (0.7, -0.8)

-0.8-0.3 0.7-0.8 0.55 0.75 (0.55, 0.75) (-0.8, -0.3) (0.7, -0.8)

-0.8-0.3 0.7-0.8 0.55 0.75 (0.55, 0.75) (-0.8, -0.3) (0.7, -0.8)

var vertices = new Float32Array([ -0.8, -0.3, 0.7, -0.8, 0.55, 0.75 ]);

var vertices = new Float32Array([ -0.8, -0.3, 0.7, -0.8, 0.55, 0.75 ]);

var vertices = new Float32Array([ -0.8, -0.3, 0.7, -0.8, 0.55, 0.75 ]);

var vertices = new Float32Array([ -0.8, -0.3, 0.7, -0.8, 0.55, 0.75 ]);! var trianglebuffer = gl.createbuffer();

var vertices = new Float32Array([ -0.8, -0.3, 0.7, -0.8, 0.55, 0.75 ]);! var trianglebuffer = gl.createbuffer(); gl.bindbuffer(gl.array_buffer, trianglebuffer); gl.bufferdata(gl.array_buffer, vertices, gl.static_draw);

JavaScript

JavaScript

JavaScript

WebGL Rendering Pipeline JavaScript

WebGL Rendering Pipeline

WebGL Color Primitive Vertex Processing Rasterizer Fragment Depth / Dither Buffer Assembly Shader Stencil Blend Rendering Pipeline

Primitive Processing Vertex Shader Primitive Assembly Rasterizer Fragment Shader Depth/ Stencil Color Buffer Blend Dither

Primitive Processing Vertex Shader Primitive Assembly Rasterizer Fragment Shader Depth/ Stencil Color Buffer Blend Dither

Vertex Shader Fragment Shader JavaScript

Vertex Shader Fragment Shader JavaScript

Vertex Shader Fragment Shader JavaScript Program

-0.8-0.3 0.7-0.8 0.55 0.75 Vertex Shader

-0.8-0.3 0.7-0.8 0.55 0.75 Vertex Shader Vertex Shader Vertex Shader

-0.8-0.3 0.7-0.8 0.55 0.75 Vertex Shader Vertex Shader Vertex Shader

-0.8-0.3 0.7-0.8 0.55 0.75 Vertex Shader Vertex Shader Vertex Shader Fragment Shader Fragment Shader Fragment Shader Fragment Shader Fragment Shader Fragment Shader Fragment Shader Fragment Shader

var vertexshader = gl.createshader(gl.vertex_shader); gl.shadersource(vertexshader, "... source code..."); gl.compileshader(vertexshader);

var vertexshader = gl.createshader(gl.vertex_shader); gl.shadersource(vertexshader, "... source code..."); gl.compileshader(vertexshader);

var vertexshader = gl.createshader(gl.vertex_shader); gl.shadersource(vertexshader, "... source code..."); gl.compileshader(vertexshader);

var vertexshader = gl.createshader(gl.vertex_shader); gl.shadersource(vertexshader, "... source code..."); gl.compileshader(vertexshader);! var fragmentshader = gl.createshader(gl.fragment_shader); gl.shadersource(fragmentshader, "... source code..."); gl.compileshader(fragmentshader);

var vertexshader = gl.createshader(gl.vertex_shader); gl.shadersource(vertexshader, "... source code..."); gl.compileshader(vertexshader);! var fragmentshader = gl.createshader(gl.fragment_shader); gl.shadersource(fragmentshader, "... source code..."); gl.compileshader(fragmentshader);! var program = gl.createprogram();

var vertexshader = gl.createshader(gl.vertex_shader); gl.shadersource(vertexshader, "... source code..."); gl.compileshader(vertexshader);! var fragmentshader = gl.createshader(gl.fragment_shader); gl.shadersource(fragmentshader, "... source code..."); gl.compileshader(fragmentshader);! var program = gl.createprogram(); gl.attachshader(program, vertexshader); gl.attachshader(program, fragmentshader);

var vertexshader = gl.createshader(gl.vertex_shader); gl.shadersource(vertexshader, "... source code..."); gl.compileshader(vertexshader);! var fragmentshader = gl.createshader(gl.fragment_shader); gl.shadersource(fragmentshader, "... source code..."); gl.compileshader(fragmentshader);! var program = gl.createprogram(); gl.attachshader(program, vertexshader); gl.attachshader(program, fragmentshader); gl.linkprogram(program);

var vertexshader = gl.createshader(gl.vertex_shader); gl.shadersource(vertexshader, "... source code..."); gl.compileshader(vertexshader);! var fragmentshader = gl.createshader(gl.fragment_shader); gl.shadersource(fragmentshader, "... source code..."); gl.compileshader(fragmentshader);! var program = gl.createprogram(); gl.attachshader(program, vertexshader); gl.attachshader(program, fragmentshader); gl.linkprogram(program);! gl.useprogram(program);

var positionattribute = gl.getattriblocation(program, "aposition"); gl.enablevertexattribarray(positionattribute);

var positionattribute = gl.getattriblocation(program, "aposition"); gl.enablevertexattribarray(positionattribute);! gl.bindbuffer(gl.array_buffer, trianglebuffer); gl.vertexattribpointer(positionattribute, 2, gl.float, false, 0, 0);

var positionattribute = gl.getattriblocation(program, "aposition"); gl.enablevertexattribarray(positionattribute);! gl.bindbuffer(gl.array_buffer, trianglebuffer); gl.vertexattribpointer(positionattribute, 2, gl.float, false, 0, 0);

var positionattribute = gl.getattriblocation(program, "aposition"); gl.enablevertexattribarray(positionattribute);! gl.bindbuffer(gl.array_buffer, trianglebuffer); gl.vertexattribpointer(positionattribute, 2, gl.float, false, 0, 0);! gl.drawarrays(gl.triangles, 0, 3);

<!DOCTYPE html> <head> <title>webgl Triangle</title> <style> canvas { width: 600px; height: 400px; } </style> </head> <script id="vertexshadersource" type="text/glsl"> // Simple vertex shader. Simply returns the position it was provided. attribute vec4 position; void main() { gl_position = position; } </script> <script id="fragmentshadersource" type="text/glsl"> // Extremely simple fragment shader. Draws every pixel red. #ifdef GL_ES precision mediump float; #endif! void main() {

gl.bindbuffer(gl.array_buffer, trianglebuffer); gl.bufferdata(gl.array_buffer, vertices, gl.static_draw);!! // ---- DRAWING ---- // Clear to black. gl.clearcolor(0, 0, 0, 1); gl.clear(gl.color_buffer_bit);! // Bind the vertex attributes for the draw operation. We first make // sure we're talking to the correct buffer, then we're going to // associate that buffer with the vertex attribute from the program. // Our buffer is an array of (x,y) points, so 2 floating point values // per vertex. gl.bindbuffer(gl.array_buffer, trianglebuffer); gl.vertexattribpointer(positionattribute, 2, gl.float, false, 0, 0);! } // The actual draw call. We know there are 3 points in the buffer // (a triangle). gl.drawarrays(gl.triangles, 0, 3);! window.addeventlistener("load", drawtriangle, false); </script> <body>

Shaders

GLSL

GLSL C-like language designed for parallel graphics

GLSL C-like language designed for parallel graphics Primitives: Vectors, matrices

GLSL C-like language designed for parallel graphics Primitives: Vectors, matrices Built-in functions: Trigonometry, vector math, smoothing, and clamping

-0.8-0.3 0.7-0.8 0.55 0.75 Vertex Shader Vertex Shader Vertex Shader Fragment Shader Fragment Shader Fragment Shader Fragment Shader Fragment Shader Fragment Shader Fragment Shader Fragment Shader

-0.8-0.3 0.7-0.8 0.55 0.75 Vertex Shader Fragment Shader

-0.8-0.3 0.7-0.8 0.55 0.75 1 100 2 987 3 46 Vertex Shader Fragment Shader

-0.8-0.3 0.7-0.8 0.55 0.75 1 100 2 987 3 46 Vertex Shader Global Constants (uniforms) Fragment Shader

-0.8-0.3 0.7-0.8 0.55 0.75 1 100 2 987 3 46 Vertex Shader gl_position Global Constants (uniforms) Fragment Shader

-0.8-0.3 0.7-0.8 0.55 0.75 1 100 2 987 3 46 Vertex Shader gl_position Global Constants (uniforms) Fragment Shader gl_fragcolor

Vertex Shader Source Code attribute vec4 aposition;! void main() { gl_position = aposition; }

Vertex Shader Source Code attribute vec4 aposition;! void main() { gl_position = aposition; }

Vertex Shader Source Code attribute vec4 aposition;! void main() { gl_position = aposition; }

Fragment Shader Source Code precision mediump float;! void main() { gl_fragcolor = vec4(1.0, 0.0, 0.0, 1.0); }

Fragment Shader Source Code precision mediump float;! void main() { gl_fragcolor = vec4(1.0, 0.0, 0.0, 1.0); }

Fragment Shader Source Code precision mediump float;! void main() { gl_fragcolor = vec4(1.0, 0.0, 0.0, 1.0); }

Fragment Shader Source Code precision mediump float;! void main() { gl_fragcolor = vec4(1.0, 0.0, 0.0, 1.0); } red green blue alpha

Demo Live Shader Editor

Shaders C-like programs that run on the GPU Control over vertex positions and pixel colors Extremely powerful

Advanced Rendering Merging WebGL with the rest of HTML Brady Eidson WebKit Engineer

var vertices = new Float32Array([ -0.8, -0.3, 0.7, -0.8, 0.55, 0.75 ]);! var trianglebuffer = gl.createbuffer(); gl.bindbuffer(gl.array_buffer, trianglebuffer); gl.bufferdata(gl.array_buffer, vertices, gl.static_draw);

var vertices = new Float32Array([... ]);! var discbuffer = gl.createbuffer(); gl.bindbuffer(gl.array_buffer, discbuffer); gl.bufferdata(gl.array_buffer, vertices, gl.static_draw);

var vertices = new Float32Array([ -0.020478, 28.7138, 0.149324, -0.020478, 70.3138, 0.149324, 17.830223, 28.7138, 0.149324, 17.830223, 28.7138, 0.149324, 17.830223, 70.3138, 0.149324, 17.830223, 70.3138, 0.149324, 15.438682, 28.7138, -8.776026, 15.438682, 28.7138, -8.776026, 15.438682, 70.3138, -8.776026, 15.438682, 70.3138, -8.776026, 8.904872, 28.7138, -15.309836, 8.904872, 28.7138, -15.309836, 8.904872, 70.3138, -15.309836, 8.904872, 70.3138, -15.309836, -0.020478, 28.7138, -17.701376, -0.020478, 28.7138, -17.701376, -0.020478, 70.3138, -17.701376, -0.020478, 70.3138, -17.701376, -8.945828, 28.7138, -15.309836,

-30.006249, 40.359541, 324.66346, -60.997415, 40.359541, 320.124634, -90.967013, 40.359541, 312.736303, -119.770729, 40.359541, 302.642778, -147.264255, 40.359541, 289.988371, -173.303279, 40.359541, 274.91739, -197.743491, 40.359541, 257.574148, -220.44058, 40.359541, 238.102954, -241.250235, 40.359541, 216.64812, -260.028146, 40.359541, 193.353956, -276.630002, 40.359541, 168.364772, -290.911493, 40.359541, 141.82488, -302.728307, 40.359541, 113.878589, -311.936135, 40.359541, 84.670211, -318.390665, 40.359541, 54.344056, -321.092812, 40.359541, 33.576897, -322.637823, 40.359541, 1.708471, -321.092812, 40.359541, -30.159955, -316.553985, 40.359541, -61.151122, -309.165655, 40.359541, -91.120719, -299.07213, 40.359541, -119.924436, -286.417722, 40.359541, -147.417961,

Any Data You Want

Any Data You Want for Any Point You Want

var vertices = new Float32Array([ -0.020478, 28.7138, 0.149324, -0.020478, 70.3138, 0.149324, 17.830223, 28.7138, 0.149324, 17.830223, 28.7138, 0.149324, 17.830223, 70.3138, 0.149324, 17.830223, 70.3138, 0.149324, 15.438682, 28.7138, -8.776026, 15.438682, 28.7138, -8.776026, 15.438682, 70.3138, -8.776026, 15.438682, 70.3138, -8.776026, ]);! var geometrybuffer = gl.createbuffer(); gl.bindbuffer(gl.array_buffer, geometrybuffer); gl.bufferdata(gl.array_buffer, vertices, gl.static_draw);

var texturecoords = new Float32Array([ 0.498234, 0.530450 0.498234, 0.600143 0.512222, 0.530450 0.512222, 0.530450 0.512222, 0.600143 0.512222, 0.600143 0.509996, 0.530450 0.509996, 0.530450 0.509996, 0.600143 0.509996, 0.600143 ]);! var texturebuffer = gl.createbuffer(); gl.bindbuffer(gl.array_buffer, texturebuffer); gl.bufferdata(gl.array_buffer, texturecoords, gl.static_draw);

Configuring Our Shaders var positionattribute = gl.getattriblocation(program, "aposition"); gl.enablevertexattribarray(positionattribute);! gl.bindbuffer(gl.array_buffer, positionbuffer); gl.vertexattribpointer(positionattribute, 3, gl.float, false, 0, 0);

Configuring Our Shaders var positionattribute = gl.getattriblocation(program, "aposition"); gl.enablevertexattribarray(positionattribute); gl.bindbuffer(gl.array_buffer, positionbuffer); gl.vertexattribpointer(positionattribute, 3, gl.float, false, 0, 0);! var textureattribute = gl.getattriblocation(program, "atexturecoord"); gl.enablevertexattribarray(textureattribute);!

Configuring Our Shaders var positionattribute = gl.getattriblocation(program, "aposition"); gl.enablevertexattribarray(positionattribute); gl.bindbuffer(gl.array_buffer, positionbuffer); gl.vertexattribpointer(positionattribute, 3, gl.float, false, 0, 0);! var textureattribute = gl.getattriblocation(program, "atexturecoord"); gl.enablevertexattribarray(textureattribute); gl.bindbuffer(gl.array_buffer, texturebuffer); gl.vertexattribpointer(textureattribute, 2, gl.float, false, 0, 0);

Vertex Shader Source Code attribute vec4 aposition;! void main() { gl_position = aposition; }

Vertex Shader Source Code attribute vec4 aposition; attribute vec2 atexturecoord;! void main() { gl_position = aposition; }

Vertex Shader Source Code attribute vec4 aposition; attribute vec2 atexturecoord;! varying vec2 vtexturecoord;! void main() { gl_position = aposition; }

Vertex Shader Source Code attribute vec4 aposition; attribute vec2 atexturecoord;! varying vec2 vtexturecoord;! void main() { gl_position = aposition; vtexturecoord = atexturecoord; }

Fragment Shader Source Code precision mediump float;! void main() { gl_fragcolor = vec4(1.0, 0.0, 0.0, 1.0); }

Fragment Shader Source Code precision mediump float;! varying vec2 vtexturecoord;! void main() { gl_fragcolor = vec4(1.0, 0.0, 0.0, 1.0); }

Configuring Our Shaders var positionattribute = gl.getattriblocation(program, "aposition"); gl.enablevertexattribarray(positionattribute); gl.bindbuffer(gl.array_buffer, positionbuffer); gl.vertexattribpointer(positionattribute, 3, gl.float, false, 0, 0);! var textureattribute = gl.getattriblocation(program, "atexturecoord"); gl.enablevertexattribarray(textureattribute); gl.bindbuffer(gl.array_buffer, texturebuffer); gl.vertexattribpointer(textureattribute, 2, gl.float, false, 0, 0);

Configuring Our Shaders var positionattribute = gl.getattriblocation(program, "aposition"); gl.enablevertexattribarray(positionattribute); gl.bindbuffer(gl.array_buffer, positionbuffer); gl.vertexattribpointer(positionattribute, 3, gl.float, false, 0, 0);! var textureattribute = gl.getattriblocation(program, "atexturecoord"); gl.enablevertexattribarray(textureattribute); gl.bindbuffer(gl.array_buffer, texturebuffer); gl.vertexattribpointer(textureattribute, 2, gl.float, false, 0, 0);! var sampleruniform = gl.getuniformlocation(program, "sampler");

Fragment Shader Source Code precision mediump float;! varying vec2 vtexturecoord;! void main() { gl_fragcolor = vec4(1.0, 0.0, 0.0, 1.0); }

Fragment Shader Source Code precision mediump float;! varying vec2 vtexturecoord;! uniform sampler2d sampler;! void main() { gl_fragcolor = vec4(1.0, 0.0, 0.0, 1.0); }

Fragment Shader Source Code precision mediump float;! varying vec2 vtexturecoord;! uniform sampler2d sampler;! void main() { gl_fragcolor = texture2d(sampler, vtexturecoord); }

Texture Sources

Texture Sources <img> element

Texture Sources <img> element XMLHttpRequest

Texture Sources <img> element XMLHttpRequest <video> element

Texture Sources <img> element XMLHttpRequest <video> element <canvas> element

<img id= TextureImage src= FlatCompass.png >

<img id= TextureImage src= FlatCompass.png >! <script type= text/javascript > var texture = gl.createtexture(); </script>

<img id= TextureImage src= FlatCompass.png >! <script type= text/javascript > var texture = gl.createtexture(); gl.activetexture(gl.texture0); gl.bindtexture(gl.texture_2d, texture); </script>

<img id= TextureImage src= FlatCompass.png >! <script type= text/javascript > var texture = gl.createtexture(); gl.activetexture(gl.texture0); gl.bindtexture(gl.texture_2d, texture);! var image = document.getelementbyid( TextureImage ); </script>

<img id= TextureImage src= FlatCompass.png >! <script type= text/javascript > var texture = gl.createtexture(); gl.activetexture(gl.texture0); gl.bindtexture(gl.texture_2d, texture);! var image = document.getelementbyid( TextureImage );! gl.teximage2d(gl.texture_2d, 0, gl.rgba, gl.rgba, gl.unsigned_byte, image); </script>

<img id= TextureImage src= FlatCompass.png >! <script type= text/javascript > var texture = gl.createtexture(); gl.activetexture(gl.texture0); gl.bindtexture(gl.texture_2d, texture);! var image = document.getelementbyid( TextureImage );! gl.teximage2d(gl.texture_2d, 0, gl.rgba, gl.rgba, gl.unsigned_byte, image); </script>

<img id= TextureImage src= FlatCompass.png >! <script type= text/javascript > var texture = gl.createtexture(); gl.activetexture(gl.texture0); gl.bindtexture(gl.texture_2d, texture);! var image = document.getelementbyid( TextureImage );! gl.teximage2d(gl.texture_2d, 0, gl.rgba, gl.rgba, gl.unsigned_byte, image); gl.uniform1i(sampleruniform, 0); </script>

Demo Building a compass

The Web Platform

The Web Platform

The Web Platform

The Web Platform

The Web Platform

When to Draw

FIXME: The contents of www.apple.com/safari will likely be different later on June 2nd, should update the video

FIXME: The contents of www.apple.com/safari will likely be different later on June 2nd, should update the video

Do Not Use Timers!

requestanimationframe()

requestanimationframe() 1 Call requestanimationframe() with a callback

requestanimationframe() 1 Call requestanimationframe() with a callback var drawingcallback = function() { }

requestanimationframe() 1 Call requestanimationframe() with a callback var drawingcallback = function() { }! requestanimationframe(drawingcallback);

requestanimationframe() 2 Draw your scene var drawingcallback = function() { }! requestanimationframe(drawingcallback);

requestanimationframe() 2 Draw your scene var drawingcallback = function() { updatephysics(); drawcompass(); drawterrain(); }! requestanimationframe(drawingcallback);

requestanimationframe() 3 Request the next callback var drawingcallback = function() { updatephysics(); drawcompass(); drawterrain(); }! requestanimationframe(drawingcallback);

requestanimationframe() 3 Request the next callback var drawingcallback = function() { updatephysics(); drawcompass(); drawterrain(); requestanimationframe(drawingcallback); }! requestanimationframe(drawingcallback);

Demo Grand finale

Wrapping Up

Wrapping Up Rich, powerful, fast graphics

Wrapping Up Rich, powerful, fast graphics WebGL available in Safari on OS X Yosemite and ios 8

Wrapping Up Rich, powerful, fast graphics WebGL available in Safari on OS X Yosemite and ios 8 Also available in WKWebView

More Information Evangelism evangelism@apple.com! Safari for Developers http://developer.apple.com/safari/ WebKit http://webkit.org/ Developer Technical Support http://developer.apple.com/contact Apple Developer Forums http://devforums.apple.com

Related Sessions Introducing the Modern WebKit API Nob Hill Tuesday 2:00PM Web Inspector and Modern JavaScript Russian Hill Thursday 10:15AM

Labs Safari and WebKit Lab Media Lab B Wednesday 4:30PM Safari and WebKit Lab Media Lab B Thursday 2:00PM