Uniform.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*global define*/
  2. define([
  3. '../Core/defined',
  4. '../Core/defineProperties',
  5. '../Core/DeveloperError',
  6. '../Core/FeatureDetection',
  7. '../Core/Cartesian2',
  8. '../Core/Cartesian3',
  9. '../Core/Cartesian4',
  10. '../Core/Color',
  11. '../Core/Matrix2',
  12. '../Core/Matrix3',
  13. '../Core/Matrix4',
  14. '../Core/RuntimeError'
  15. ], function(
  16. defined,
  17. defineProperties,
  18. DeveloperError,
  19. FeatureDetection,
  20. Cartesian2,
  21. Cartesian3,
  22. Cartesian4,
  23. Color,
  24. Matrix2,
  25. Matrix3,
  26. Matrix4,
  27. RuntimeError) {
  28. "use strict";
  29. /*global console*/
  30. var scratchUniformMatrix2;
  31. var scratchUniformMatrix3;
  32. var scratchUniformMatrix4;
  33. if (FeatureDetection.supportsTypedArrays()) {
  34. scratchUniformMatrix2 = new Float32Array(4);
  35. scratchUniformMatrix3 = new Float32Array(9);
  36. scratchUniformMatrix4 = new Float32Array(16);
  37. }
  38. /**
  39. * @private
  40. */
  41. var Uniform = function(gl, activeUniform, uniformName, location) {
  42. this.value = undefined;
  43. this._value = undefined;
  44. this._gl = gl;
  45. this._activeUniform = activeUniform;
  46. this._uniformName = uniformName;
  47. this._location = location;
  48. /**
  49. * @private
  50. */
  51. this.textureUnitIndex = undefined;
  52. var set;
  53. switch (activeUniform.type) {
  54. case gl.FLOAT:
  55. set = this.setFloat;
  56. break;
  57. case gl.FLOAT_VEC2:
  58. set = this.setFloatVec2;
  59. break;
  60. case gl.FLOAT_VEC3:
  61. set = this.setFloatVec3;
  62. break;
  63. case gl.FLOAT_VEC4:
  64. set = this.setFloatVec4;
  65. break;
  66. case gl.SAMPLER_2D:
  67. case gl.SAMPLER_CUBE:
  68. set = this.setSampler;
  69. break;
  70. case gl.INT:
  71. case gl.BOOL:
  72. set = this.setInt;
  73. break;
  74. case gl.INT_VEC2:
  75. case gl.BOOL_VEC2:
  76. set = this.setIntVec2;
  77. break;
  78. case gl.INT_VEC3:
  79. case gl.BOOL_VEC3:
  80. set = this.setIntVec3;
  81. break;
  82. case gl.INT_VEC4:
  83. case gl.BOOL_VEC4:
  84. set = this.setIntVec4;
  85. break;
  86. case gl.FLOAT_MAT2:
  87. set = this.setMat2;
  88. break;
  89. case gl.FLOAT_MAT3:
  90. set = this.setMat3;
  91. break;
  92. case gl.FLOAT_MAT4:
  93. set = this.setMat4;
  94. break;
  95. default:
  96. throw new RuntimeError('Unrecognized uniform type: ' + activeUniform.type + ' for uniform "' + uniformName + '".');
  97. }
  98. this._set = set;
  99. if ((activeUniform.type === gl.SAMPLER_2D) || (activeUniform.type === gl.SAMPLER_CUBE)) {
  100. this._setSampler = function(textureUnitIndex) {
  101. this.textureUnitIndex = textureUnitIndex;
  102. gl.uniform1i(location, textureUnitIndex);
  103. return textureUnitIndex + 1;
  104. };
  105. }
  106. };
  107. defineProperties(Uniform.prototype, {
  108. name : {
  109. get : function() {
  110. return this._uniformName;
  111. }
  112. },
  113. datatype : {
  114. get : function() {
  115. return this._activeUniform.type;
  116. }
  117. }
  118. });
  119. Uniform.prototype.setFloat = function() {
  120. if (this.value !== this._value) {
  121. this._value = this.value;
  122. this._gl.uniform1f(this._location, this.value);
  123. }
  124. };
  125. Uniform.prototype.setFloatVec2 = function() {
  126. var v = this.value;
  127. if (!Cartesian2.equals(v, this._value)) {
  128. this._value = Cartesian2.clone(v, this._value);
  129. this._gl.uniform2f(this._location, v.x, v.y);
  130. }
  131. };
  132. Uniform.prototype.setFloatVec3 = function() {
  133. var v = this.value;
  134. if (defined(v.red)) {
  135. if (!Color.equals(v, this._value)) {
  136. this._value = Color.clone(v, this._value);
  137. this._gl.uniform3f(this._location, v.red, v.green, v.blue);
  138. }
  139. } else if (defined(v.x)) {
  140. if (!Cartesian3.equals(v, this._value)) {
  141. this._value = Cartesian3.clone(v, this._value);
  142. this._gl.uniform3f(this._location, v.x, v.y, v.z);
  143. }
  144. } else {
  145. throw new DeveloperError('Invalid vec3 value for uniform "' + this._activethis.name + '".');
  146. }
  147. };
  148. Uniform.prototype.setFloatVec4 = function() {
  149. var v = this.value;
  150. if (defined(v.red)) {
  151. if (!Color.equals(v, this._value)) {
  152. this._value = Color.clone(v, this._value);
  153. this._gl.uniform4f(this._location, v.red, v.green, v.blue, v.alpha);
  154. }
  155. } else if (defined(v.x)) {
  156. if (!Cartesian4.equals(v, this._value)) {
  157. this._value = Cartesian4.clone(v, this._value);
  158. this._gl.uniform4f(this._location, v.x, v.y, v.z, v.w);
  159. }
  160. } else {
  161. throw new DeveloperError('Invalid vec4 value for uniform "' + this._activethis.name + '".');
  162. }
  163. };
  164. Uniform.prototype.setSampler = function() {
  165. var gl = this._gl;
  166. gl.activeTexture(gl.TEXTURE0 + this.textureUnitIndex);
  167. gl.bindTexture(this.value._target, this.value._texture);
  168. };
  169. Uniform.prototype.setInt = function() {
  170. if (this.value !== this._value) {
  171. this._value = this.value;
  172. this._gl.uniform1i(this._location, this.value);
  173. }
  174. };
  175. Uniform.prototype.setIntVec2 = function() {
  176. var v = this.value;
  177. if (!Cartesian2.equals(v, this._value)) {
  178. this._value = Cartesian2.clone(v, this._value);
  179. this._gl.uniform2i(this._location, v.x, v.y);
  180. }
  181. };
  182. Uniform.prototype.setIntVec3 = function() {
  183. var v = this.value;
  184. if (!Cartesian3.equals(v, this._value)) {
  185. this._value = Cartesian3.clone(v, this._value);
  186. this._gl.uniform3i(this._location, v.x, v.y, v.z);
  187. }
  188. };
  189. Uniform.prototype.setIntVec4 = function() {
  190. var v = this.value;
  191. if (!Cartesian4.equals(v, this._value)) {
  192. this._value = Cartesian4.clone(v, this._value);
  193. this._gl.uniform4i(this._location, v.x, v.y, v.z, v.w);
  194. }
  195. };
  196. Uniform.prototype.setMat2 = function() {
  197. var v = this.value;
  198. if (!Matrix2.equals(v, this._value)) {
  199. this._value = Matrix2.clone(v, this._value);
  200. this._gl.uniformMatrix2fv(this._location, false, Matrix2.toArray(this.value, scratchUniformMatrix2));
  201. }
  202. };
  203. Uniform.prototype.setMat3 = function() {
  204. var v = this.value;
  205. if (!Matrix3.equals(v, this._value)) {
  206. this._value = Matrix3.clone(v, this._value);
  207. this._gl.uniformMatrix3fv(this._location, false, Matrix3.toArray(this.value, scratchUniformMatrix3));
  208. }
  209. };
  210. Uniform.prototype.setMat4 = function() {
  211. var v = this.value;
  212. if (!Matrix4.equals(v, this._value)) {
  213. this._value = Matrix4.clone(v, this._value);
  214. this._gl.uniformMatrix4fv(this._location, false, Matrix4.toArray(this.value, scratchUniformMatrix4));
  215. }
  216. };
  217. return Uniform;
  218. });