WebGLUniforms.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /**
  2. * @author tschw
  3. *
  4. * Uniforms of a program.
  5. * Those form a tree structure with a special top-level container for the root,
  6. * which you get by calling 'new WebGLUniforms( gl, program, renderer )'.
  7. *
  8. *
  9. * Properties of inner nodes including the top-level container:
  10. *
  11. * .seq - array of nested uniforms
  12. * .map - nested uniforms by name
  13. *
  14. *
  15. * Methods of all nodes except the top-level container:
  16. *
  17. * .setValue( gl, value, [renderer] )
  18. *
  19. * uploads a uniform value(s)
  20. * the 'renderer' parameter is needed for sampler uniforms
  21. *
  22. *
  23. * Static methods of the top-level container (renderer factorizations):
  24. *
  25. * .upload( gl, seq, values, renderer )
  26. *
  27. * sets uniforms in 'seq' to 'values[id].value'
  28. *
  29. * .seqWithValue( seq, values ) : filteredSeq
  30. *
  31. * filters 'seq' entries with corresponding entry in values
  32. *
  33. *
  34. * Methods of the top-level container (renderer factorizations):
  35. *
  36. * .setValue( gl, name, value )
  37. *
  38. * sets uniform with name 'name' to 'value'
  39. *
  40. * .set( gl, obj, prop )
  41. *
  42. * sets uniform from object and property with same name than uniform
  43. *
  44. * .setOptional( gl, obj, prop )
  45. *
  46. * like .set for an optional property of the object
  47. *
  48. */
  49. import { CubeTexture } from '../../textures/CubeTexture.js';
  50. import { Texture } from '../../textures/Texture.js';
  51. var emptyTexture = new Texture();
  52. var emptyCubeTexture = new CubeTexture();
  53. // --- Base for inner nodes (including the root) ---
  54. function UniformContainer() {
  55. this.seq = [];
  56. this.map = {};
  57. }
  58. // --- Utilities ---
  59. // Array Caches (provide typed arrays for temporary by size)
  60. var arrayCacheF32 = [];
  61. var arrayCacheI32 = [];
  62. // Float32Array caches used for uploading Matrix uniforms
  63. var mat4array = new Float32Array( 16 );
  64. var mat3array = new Float32Array( 9 );
  65. // Flattening for arrays of vectors and matrices
  66. function flatten( array, nBlocks, blockSize ) {
  67. var firstElem = array[ 0 ];
  68. if ( firstElem <= 0 || firstElem > 0 ) return array;
  69. // unoptimized: ! isNaN( firstElem )
  70. // see http://jacksondunstan.com/articles/983
  71. var n = nBlocks * blockSize,
  72. r = arrayCacheF32[ n ];
  73. if ( r === undefined ) {
  74. r = new Float32Array( n );
  75. arrayCacheF32[ n ] = r;
  76. }
  77. if ( nBlocks !== 0 ) {
  78. firstElem.toArray( r, 0 );
  79. for ( var i = 1, offset = 0; i !== nBlocks; ++ i ) {
  80. offset += blockSize;
  81. array[ i ].toArray( r, offset );
  82. }
  83. }
  84. return r;
  85. }
  86. // Texture unit allocation
  87. function allocTexUnits( renderer, n ) {
  88. var r = arrayCacheI32[ n ];
  89. if ( r === undefined ) {
  90. r = new Int32Array( n );
  91. arrayCacheI32[ n ] = r;
  92. }
  93. for ( var i = 0; i !== n; ++ i )
  94. r[ i ] = renderer.allocTextureUnit();
  95. return r;
  96. }
  97. // --- Setters ---
  98. // Note: Defining these methods externally, because they come in a bunch
  99. // and this way their names minify.
  100. // Single scalar
  101. function setValue1f( gl, v ) {
  102. gl.uniform1f( this.addr, v );
  103. }
  104. function setValue1i( gl, v ) {
  105. gl.uniform1i( this.addr, v );
  106. }
  107. // Single float vector (from flat array or THREE.VectorN)
  108. function setValue2fv( gl, v ) {
  109. if ( v.x === undefined ) {
  110. gl.uniform2fv( this.addr, v );
  111. } else {
  112. gl.uniform2f( this.addr, v.x, v.y );
  113. }
  114. }
  115. function setValue3fv( gl, v ) {
  116. if ( v.x !== undefined ) {
  117. gl.uniform3f( this.addr, v.x, v.y, v.z );
  118. } else if ( v.r !== undefined ) {
  119. gl.uniform3f( this.addr, v.r, v.g, v.b );
  120. } else {
  121. gl.uniform3fv( this.addr, v );
  122. }
  123. }
  124. function setValue4fv( gl, v ) {
  125. if ( v.x === undefined ) {
  126. gl.uniform4fv( this.addr, v );
  127. } else {
  128. gl.uniform4f( this.addr, v.x, v.y, v.z, v.w );
  129. }
  130. }
  131. // Single matrix (from flat array or MatrixN)
  132. function setValue2fm( gl, v ) {
  133. gl.uniformMatrix2fv( this.addr, false, v.elements || v );
  134. }
  135. function setValue3fm( gl, v ) {
  136. if ( v.elements === undefined ) {
  137. gl.uniformMatrix3fv( this.addr, false, v );
  138. } else {
  139. mat3array.set( v.elements );
  140. gl.uniformMatrix3fv( this.addr, false, mat3array );
  141. }
  142. }
  143. function setValue4fm( gl, v ) {
  144. if ( v.elements === undefined ) {
  145. gl.uniformMatrix4fv( this.addr, false, v );
  146. } else {
  147. mat4array.set( v.elements );
  148. gl.uniformMatrix4fv( this.addr, false, mat4array );
  149. }
  150. }
  151. // Single texture (2D / Cube)
  152. function setValueT1( gl, v, renderer ) {
  153. var unit = renderer.allocTextureUnit();
  154. gl.uniform1i( this.addr, unit );
  155. renderer.setTexture2D( v || emptyTexture, unit );
  156. }
  157. function setValueT6( gl, v, renderer ) {
  158. var unit = renderer.allocTextureUnit();
  159. gl.uniform1i( this.addr, unit );
  160. renderer.setTextureCube( v || emptyCubeTexture, unit );
  161. }
  162. // Integer / Boolean vectors or arrays thereof (always flat arrays)
  163. function setValue2iv( gl, v ) {
  164. gl.uniform2iv( this.addr, v );
  165. }
  166. function setValue3iv( gl, v ) {
  167. gl.uniform3iv( this.addr, v );
  168. }
  169. function setValue4iv( gl, v ) {
  170. gl.uniform4iv( this.addr, v );
  171. }
  172. // Helper to pick the right setter for the singular case
  173. function getSingularSetter( type ) {
  174. switch ( type ) {
  175. case 0x1406: return setValue1f; // FLOAT
  176. case 0x8b50: return setValue2fv; // _VEC2
  177. case 0x8b51: return setValue3fv; // _VEC3
  178. case 0x8b52: return setValue4fv; // _VEC4
  179. case 0x8b5a: return setValue2fm; // _MAT2
  180. case 0x8b5b: return setValue3fm; // _MAT3
  181. case 0x8b5c: return setValue4fm; // _MAT4
  182. case 0x8b5e: case 0x8d66: return setValueT1; // SAMPLER_2D, SAMPLER_EXTERNAL_OES
  183. case 0x8b60: return setValueT6; // SAMPLER_CUBE
  184. case 0x1404: case 0x8b56: return setValue1i; // INT, BOOL
  185. case 0x8b53: case 0x8b57: return setValue2iv; // _VEC2
  186. case 0x8b54: case 0x8b58: return setValue3iv; // _VEC3
  187. case 0x8b55: case 0x8b59: return setValue4iv; // _VEC4
  188. }
  189. }
  190. // Array of scalars
  191. function setValue1fv( gl, v ) {
  192. gl.uniform1fv( this.addr, v );
  193. }
  194. function setValue1iv( gl, v ) {
  195. gl.uniform1iv( this.addr, v );
  196. }
  197. // Array of vectors (flat or from THREE classes)
  198. function setValueV2a( gl, v ) {
  199. gl.uniform2fv( this.addr, flatten( v, this.size, 2 ) );
  200. }
  201. function setValueV3a( gl, v ) {
  202. gl.uniform3fv( this.addr, flatten( v, this.size, 3 ) );
  203. }
  204. function setValueV4a( gl, v ) {
  205. gl.uniform4fv( this.addr, flatten( v, this.size, 4 ) );
  206. }
  207. // Array of matrices (flat or from THREE clases)
  208. function setValueM2a( gl, v ) {
  209. gl.uniformMatrix2fv( this.addr, false, flatten( v, this.size, 4 ) );
  210. }
  211. function setValueM3a( gl, v ) {
  212. gl.uniformMatrix3fv( this.addr, false, flatten( v, this.size, 9 ) );
  213. }
  214. function setValueM4a( gl, v ) {
  215. gl.uniformMatrix4fv( this.addr, false, flatten( v, this.size, 16 ) );
  216. }
  217. // Array of textures (2D / Cube)
  218. function setValueT1a( gl, v, renderer ) {
  219. var n = v.length,
  220. units = allocTexUnits( renderer, n );
  221. gl.uniform1iv( this.addr, units );
  222. for ( var i = 0; i !== n; ++ i ) {
  223. renderer.setTexture2D( v[ i ] || emptyTexture, units[ i ] );
  224. }
  225. }
  226. function setValueT6a( gl, v, renderer ) {
  227. var n = v.length,
  228. units = allocTexUnits( renderer, n );
  229. gl.uniform1iv( this.addr, units );
  230. for ( var i = 0; i !== n; ++ i ) {
  231. renderer.setTextureCube( v[ i ] || emptyCubeTexture, units[ i ] );
  232. }
  233. }
  234. // Helper to pick the right setter for a pure (bottom-level) array
  235. function getPureArraySetter( type ) {
  236. switch ( type ) {
  237. case 0x1406: return setValue1fv; // FLOAT
  238. case 0x8b50: return setValueV2a; // _VEC2
  239. case 0x8b51: return setValueV3a; // _VEC3
  240. case 0x8b52: return setValueV4a; // _VEC4
  241. case 0x8b5a: return setValueM2a; // _MAT2
  242. case 0x8b5b: return setValueM3a; // _MAT3
  243. case 0x8b5c: return setValueM4a; // _MAT4
  244. case 0x8b5e: return setValueT1a; // SAMPLER_2D
  245. case 0x8b60: return setValueT6a; // SAMPLER_CUBE
  246. case 0x1404: case 0x8b56: return setValue1iv; // INT, BOOL
  247. case 0x8b53: case 0x8b57: return setValue2iv; // _VEC2
  248. case 0x8b54: case 0x8b58: return setValue3iv; // _VEC3
  249. case 0x8b55: case 0x8b59: return setValue4iv; // _VEC4
  250. }
  251. }
  252. // --- Uniform Classes ---
  253. function SingleUniform( id, activeInfo, addr ) {
  254. this.id = id;
  255. this.addr = addr;
  256. this.setValue = getSingularSetter( activeInfo.type );
  257. // this.path = activeInfo.name; // DEBUG
  258. }
  259. function PureArrayUniform( id, activeInfo, addr ) {
  260. this.id = id;
  261. this.addr = addr;
  262. this.size = activeInfo.size;
  263. this.setValue = getPureArraySetter( activeInfo.type );
  264. // this.path = activeInfo.name; // DEBUG
  265. }
  266. function StructuredUniform( id ) {
  267. this.id = id;
  268. UniformContainer.call( this ); // mix-in
  269. }
  270. StructuredUniform.prototype.setValue = function ( gl, value ) {
  271. // Note: Don't need an extra 'renderer' parameter, since samplers
  272. // are not allowed in structured uniforms.
  273. var seq = this.seq;
  274. for ( var i = 0, n = seq.length; i !== n; ++ i ) {
  275. var u = seq[ i ];
  276. u.setValue( gl, value[ u.id ] );
  277. }
  278. };
  279. // --- Top-level ---
  280. // Parser - builds up the property tree from the path strings
  281. var RePathPart = /([\w\d_]+)(\])?(\[|\.)?/g;
  282. // extracts
  283. // - the identifier (member name or array index)
  284. // - followed by an optional right bracket (found when array index)
  285. // - followed by an optional left bracket or dot (type of subscript)
  286. //
  287. // Note: These portions can be read in a non-overlapping fashion and
  288. // allow straightforward parsing of the hierarchy that WebGL encodes
  289. // in the uniform names.
  290. function addUniform( container, uniformObject ) {
  291. container.seq.push( uniformObject );
  292. container.map[ uniformObject.id ] = uniformObject;
  293. }
  294. function parseUniform( activeInfo, addr, container ) {
  295. var path = activeInfo.name,
  296. pathLength = path.length;
  297. // reset RegExp object, because of the early exit of a previous run
  298. RePathPart.lastIndex = 0;
  299. for ( ; ; ) {
  300. var match = RePathPart.exec( path ),
  301. matchEnd = RePathPart.lastIndex,
  302. id = match[ 1 ],
  303. idIsIndex = match[ 2 ] === ']',
  304. subscript = match[ 3 ];
  305. if ( idIsIndex ) id = id | 0; // convert to integer
  306. if ( subscript === undefined || subscript === '[' && matchEnd + 2 === pathLength ) {
  307. // bare name or "pure" bottom-level array "[0]" suffix
  308. addUniform( container, subscript === undefined ?
  309. new SingleUniform( id, activeInfo, addr ) :
  310. new PureArrayUniform( id, activeInfo, addr ) );
  311. break;
  312. } else {
  313. // step into inner node / create it in case it doesn't exist
  314. var map = container.map, next = map[ id ];
  315. if ( next === undefined ) {
  316. next = new StructuredUniform( id );
  317. addUniform( container, next );
  318. }
  319. container = next;
  320. }
  321. }
  322. }
  323. // Root Container
  324. function WebGLUniforms( gl, program, renderer ) {
  325. UniformContainer.call( this );
  326. this.renderer = renderer;
  327. var n = gl.getProgramParameter( program, gl.ACTIVE_UNIFORMS );
  328. for ( var i = 0; i < n; ++ i ) {
  329. var info = gl.getActiveUniform( program, i ),
  330. addr = gl.getUniformLocation( program, info.name );
  331. parseUniform( info, addr, this );
  332. }
  333. }
  334. WebGLUniforms.prototype.setValue = function ( gl, name, value ) {
  335. var u = this.map[ name ];
  336. if ( u !== undefined ) u.setValue( gl, value, this.renderer );
  337. };
  338. WebGLUniforms.prototype.setOptional = function ( gl, object, name ) {
  339. var v = object[ name ];
  340. if ( v !== undefined ) this.setValue( gl, name, v );
  341. };
  342. // Static interface
  343. WebGLUniforms.upload = function ( gl, seq, values, renderer ) {
  344. for ( var i = 0, n = seq.length; i !== n; ++ i ) {
  345. var u = seq[ i ],
  346. v = values[ u.id ];
  347. if ( v.needsUpdate !== false ) {
  348. // note: always updating when .needsUpdate is undefined
  349. u.setValue( gl, v.value, renderer );
  350. }
  351. }
  352. };
  353. WebGLUniforms.seqWithValue = function ( seq, values ) {
  354. var r = [];
  355. for ( var i = 0, n = seq.length; i !== n; ++ i ) {
  356. var u = seq[ i ];
  357. if ( u.id in values ) r.push( u );
  358. }
  359. return r;
  360. };
  361. export { WebGLUniforms };
粤ICP备19079148号