@@ -7,8 +7,9 @@ module.exports = createUniformWrapper
7
7
8
8
//Binds a function and returns a value
9
9
function identity ( x ) {
10
- var c = new Function ( 'y' , 'return function(){return y}' )
11
- return c ( x )
10
+ return function ( ) {
11
+ return x
12
+ }
12
13
}
13
14
14
15
function makeVector ( length , fill ) {
@@ -22,50 +23,83 @@ function makeVector(length, fill) {
22
23
//Create shims for uniforms
23
24
function createUniformWrapper ( gl , wrapper , uniforms , locations ) {
24
25
25
- function makeGetter ( index ) {
26
- var proc = new Function (
27
- 'gl'
28
- , 'wrapper'
29
- , 'locations'
30
- , 'return function(){return gl.getUniform(wrapper.program,locations[' + index + '])}' )
31
- return proc ( gl , wrapper , locations )
26
+ function makeGetter ( idx ) {
27
+ return function ( gl , wrapper , locations ) {
28
+ return gl . getUniform ( wrapper . program , locations [ idx ] )
29
+ }
32
30
}
33
31
34
- function makePropSetter ( path , index , type ) {
35
- switch ( type ) {
36
- case 'bool' :
37
- case 'int' :
38
- case 'sampler2D' :
39
- case 'samplerCube' :
40
- return 'gl.uniform1i(locations[' + index + '],obj' + path + ')'
41
- case 'float' :
42
- return 'gl.uniform1f(locations[' + index + '],obj' + path + ')'
43
- default :
44
- var vidx = type . indexOf ( 'vec' )
45
- if ( 0 <= vidx && vidx <= 1 && type . length === 4 + vidx ) {
46
- var d = type . charCodeAt ( type . length - 1 ) - 48
47
- if ( d < 2 || d > 4 ) {
48
- throw new GLError ( '' , 'Invalid data type' )
32
+ function makeSetter ( type ) {
33
+ return function updateProperty ( obj ) {
34
+ var indices = enumerateIndices ( '' , type )
35
+ for ( var i = 0 ; i < indices . length ; ++ i ) {
36
+ var item = indices [ i ]
37
+ var path = item [ 0 ]
38
+ var idx = item [ 1 ]
39
+ if ( locations [ idx ] ) {
40
+ var objPath = obj
41
+ if ( typeof path === 'string' && (
42
+ path . indexOf ( '.' ) === 0 ||
43
+ path . indexOf ( '[' ) === 0
44
+ ) ) {
45
+ var key = path
46
+ if ( path . indexOf ( '.' ) === 0 ) {
47
+ key = path . slice ( 1 )
48
+ }
49
+
50
+ if ( key . indexOf ( ']' ) === key . length - 1 ) {
51
+ var j = key . indexOf ( '[' )
52
+ var k1 = key . slice ( 0 , j )
53
+ var k2 = key . slice ( j + 1 , key . length - 1 )
54
+ objPath = k1 ? obj [ k1 ] [ k2 ] : obj [ k2 ]
55
+ } else {
56
+ objPath = obj [ key ]
57
+ }
49
58
}
50
- switch ( type . charAt ( 0 ) ) {
51
- case 'b' :
52
- case 'i' :
53
- return 'gl.uniform' + d + 'iv(locations[' + index + '],obj' + path + ')'
54
- case 'v' :
55
- return 'gl.uniform' + d + 'fv(locations[' + index + '],obj' + path + ')'
59
+
60
+ var t = uniforms [ idx ] . type
61
+ var d
62
+ switch ( t ) {
63
+ case 'bool' :
64
+ case 'int' :
65
+ case 'sampler2D' :
66
+ case 'samplerCube' :
67
+ gl . uniform1i ( locations [ idx ] , objPath )
68
+ break
69
+ case 'float' :
70
+ gl . uniform1f ( locations [ idx ] , objPath )
71
+ break
56
72
default :
57
- throw new GLError ( '' , 'Unrecognized data type for vector ' + name + ': ' + type )
58
- }
59
- } else if ( type . indexOf ( 'mat' ) === 0 && type . length === 4 ) {
60
- var d = type . charCodeAt ( type . length - 1 ) - 48
61
- if ( d < 2 || d > 4 ) {
62
- throw new GLError ( '' , 'Invalid uniform dimension type for matrix ' + name + ': ' + type )
73
+ var vidx = t . indexOf ( 'vec' )
74
+ if ( 0 <= vidx && vidx <= 1 && t . length === 4 + vidx ) {
75
+ d = t . charCodeAt ( t . length - 1 ) - 48
76
+ if ( d < 2 || d > 4 ) {
77
+ throw new GLError ( '' , 'Invalid data type' )
78
+ }
79
+ switch ( t . charAt ( 0 ) ) {
80
+ case 'b' :
81
+ case 'i' :
82
+ gl [ 'uniform' + d + 'iv' ] ( locations [ idx ] , objPath )
83
+ break
84
+ case 'v' :
85
+ gl [ 'uniform' + d + 'fv' ] ( locations [ idx ] , objPath )
86
+ break
87
+ default :
88
+ throw new GLError ( '' , 'Unrecognized data type for vector ' + name + ': ' + t )
89
+ }
90
+ } else if ( t . indexOf ( 'mat' ) === 0 && t . length === 4 ) {
91
+ d = t . charCodeAt ( t . length - 1 ) - 48
92
+ if ( d < 2 || d > 4 ) {
93
+ throw new GLError ( '' , 'Invalid uniform dimension type for matrix ' + name + ': ' + t )
94
+ }
95
+ gl [ 'uniformMatrix' + d + 'fv' ] ( locations [ idx ] , false , objPath )
96
+ break
97
+ } else {
98
+ throw new GLError ( '' , 'Unknown uniform data type for ' + name + ': ' + t )
99
+ }
63
100
}
64
- return 'gl.uniformMatrix' + d + 'fv(locations[' + index + '],false,obj' + path + ')'
65
- } else {
66
- throw new GLError ( '' , 'Unknown uniform data type for ' + name + ': ' + type )
67
101
}
68
- break
102
+ }
69
103
}
70
104
}
71
105
@@ -91,21 +125,6 @@ function createUniformWrapper(gl, wrapper, uniforms, locations) {
91
125
return indices
92
126
}
93
127
94
- function makeSetter ( type ) {
95
- var code = [ 'return function updateProperty(obj){' ]
96
- var indices = enumerateIndices ( '' , type )
97
- for ( var i = 0 ; i < indices . length ; ++ i ) {
98
- var item = indices [ i ]
99
- var path = item [ 0 ]
100
- var idx = item [ 1 ]
101
- if ( locations [ idx ] ) {
102
- code . push ( makePropSetter ( path , idx , uniforms [ idx ] . type ) )
103
- }
104
- }
105
- code . push ( 'return obj}' )
106
- var proc = new Function ( 'gl' , 'locations' , code . join ( '\n' ) )
107
- return proc ( gl , locations )
108
- }
109
128
110
129
function defaultValue ( type ) {
111
130
switch ( type ) {
@@ -137,7 +156,6 @@ function createUniformWrapper(gl, wrapper, uniforms, locations) {
137
156
} else {
138
157
throw new GLError ( '' , 'Unknown uniform data type for ' + name + ': ' + type )
139
158
}
140
- break
141
159
}
142
160
}
143
161
0 commit comments