@@ -26,7 +26,40 @@ class WebglBaseLine {
2626 this . visible = true ;
2727 this . intensity = 1 ;
2828 }
29+ initProgram ( webgl ) {
30+ if ( ! WebglBaseLine . program ) {
31+ this . createProgram ( webgl ) ;
32+ }
33+ this . _prog = WebglBaseLine . program ;
34+ }
35+ createProgram ( webgl ) {
36+ const vertShader = webgl . createShader ( webgl . VERTEX_SHADER ) ;
37+ webgl . shaderSource ( vertShader , WebglBaseLine . vertCode ) ;
38+ webgl . compileShader ( vertShader ) ;
39+ const fragShader = webgl . createShader ( webgl . FRAGMENT_SHADER ) ;
40+ webgl . shaderSource ( fragShader , WebglBaseLine . fragCode ) ;
41+ webgl . compileShader ( fragShader ) ;
42+ WebglBaseLine . program = webgl . createProgram ( ) ;
43+ webgl . attachShader ( WebglBaseLine . program , vertShader ) ;
44+ webgl . attachShader ( WebglBaseLine . program , fragShader ) ;
45+ webgl . linkProgram ( WebglBaseLine . program ) ;
46+ }
2947}
48+ WebglBaseLine . vertCode = `
49+ attribute vec2 coordinates;
50+ uniform mat2 uscale;
51+ uniform vec2 uoffset;
52+
53+ void main(void) {
54+ gl_Position = vec4(uscale*coordinates + uoffset, 0.0, 1.0);
55+ }` ;
56+ WebglBaseLine . fragCode = `
57+ precision mediump float;
58+ uniform highp vec4 uColor;
59+
60+ void main(void) {
61+ gl_FragColor = uColor;
62+ }` ;
3063
3164/**
3265 * The standard Line class
@@ -385,37 +418,10 @@ class WebGLPlot {
385418 * ```
386419 */
387420 addLine ( line ) {
421+ line . initProgram ( this . webgl ) ;
388422 line . _vbuffer = this . webgl . createBuffer ( ) ;
389423 this . webgl . bindBuffer ( this . webgl . ARRAY_BUFFER , line . _vbuffer ) ;
390424 this . webgl . bufferData ( this . webgl . ARRAY_BUFFER , line . xy , this . webgl . STREAM_DRAW ) ;
391- const vertCode = `
392- attribute vec2 coordinates;
393- uniform mat2 uscale;
394- uniform vec2 uoffset;
395-
396- void main(void) {
397- gl_Position = vec4(uscale*coordinates + uoffset, 0.0, 1.0);
398- }` ;
399- // Create a vertex shader object
400- const vertShader = this . webgl . createShader ( this . webgl . VERTEX_SHADER ) ;
401- // Attach vertex shader source code
402- this . webgl . shaderSource ( vertShader , vertCode ) ;
403- // Compile the vertex shader
404- this . webgl . compileShader ( vertShader ) ;
405- // Fragment shader source code
406- const fragCode = `
407- precision mediump float;
408- uniform highp vec4 uColor;
409- void main(void) {
410- gl_FragColor = uColor;
411- }` ;
412- const fragShader = this . webgl . createShader ( this . webgl . FRAGMENT_SHADER ) ;
413- this . webgl . shaderSource ( fragShader , fragCode ) ;
414- this . webgl . compileShader ( fragShader ) ;
415- line . _prog = this . webgl . createProgram ( ) ;
416- this . webgl . attachShader ( line . _prog , vertShader ) ;
417- this . webgl . attachShader ( line . _prog , fragShader ) ;
418- this . webgl . linkProgram ( line . _prog ) ;
419425 this . webgl . bindBuffer ( this . webgl . ARRAY_BUFFER , line . _vbuffer ) ;
420426 line . _coord = this . webgl . getAttribLocation ( line . _prog , "coordinates" ) ;
421427 this . webgl . vertexAttribPointer ( line . _coord , 2 , this . webgl . FLOAT , false , 0 , 0 ) ;
0 commit comments